VirtualBox

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

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

VMM: VMCPU::enmState.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 66.0 KB
Line 
1/* $Id: GVMMR0.cpp 19435 2009-05-06 14:01:15Z 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
61
62/*******************************************************************************
63* Structures and Typedefs *
64*******************************************************************************/
65
66/**
67 * Global VM handle.
68 */
69typedef struct GVMHANDLE
70{
71 /** The index of the next handle in the list (free or used). (0 is nil.) */
72 uint16_t volatile iNext;
73 /** Our own index / handle value. */
74 uint16_t iSelf;
75 /** The pointer to the ring-0 only (aka global) VM structure. */
76 PGVM pGVM;
77 /** The ring-0 mapping of the shared VM instance data. */
78 PVM pVM;
79 /** The virtual machine object. */
80 void *pvObj;
81 /** The session this VM is associated with. */
82 PSUPDRVSESSION pSession;
83 /** The ring-0 handle of the EMT0 thread.
84 * This is used for ownership checks as well as looking up a VM handle by thread
85 * at times like assertions. */
86 RTNATIVETHREAD hEMT0;
87 /** The process ID of the handle owner.
88 * This is used for access checks. */
89 RTPROCESS ProcId;
90} GVMHANDLE;
91/** Pointer to a global VM handle. */
92typedef GVMHANDLE *PGVMHANDLE;
93
94/** Number of GVM handles (including the NIL handle). */
95#if HC_ARCH_BITS == 64
96# define GVMM_MAX_HANDLES 1024
97#else
98# define GVMM_MAX_HANDLES 128
99#endif
100
101/**
102 * The GVMM instance data.
103 */
104typedef struct GVMM
105{
106 /** Eyecatcher / magic. */
107 uint32_t u32Magic;
108 /** The index of the head of the free handle chain. (0 is nil.) */
109 uint16_t volatile iFreeHead;
110 /** The index of the head of the active handle chain. (0 is nil.) */
111 uint16_t volatile iUsedHead;
112 /** The number of VMs. */
113 uint16_t volatile cVMs;
114// /** The number of halted EMT threads. */
115// uint16_t volatile cHaltedEMTs;
116 /** The lock used to serialize VM creation, destruction and associated events that
117 * isn't performance critical. Owners may acquire the list lock. */
118 RTSEMFASTMUTEX CreateDestroyLock;
119 /** The lock used to serialize used list updates and accesses.
120 * This indirectly includes scheduling since the scheduler will have to walk the
121 * used list to examin running VMs. Owners may not acquire any other locks. */
122 RTSEMFASTMUTEX UsedLock;
123 /** The handle array.
124 * The size of this array defines the maximum number of currently running VMs.
125 * The first entry is unused as it represents the NIL handle. */
126 GVMHANDLE aHandles[GVMM_MAX_HANDLES];
127
128 /** @gcfgm{/GVMM/cVMsMeansCompany, 32-bit, 0, UINT32_MAX, 1}
129 * The number of VMs that means we no longer consider ourselves alone on a CPU/Core.
130 */
131 uint32_t cVMsMeansCompany;
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->cVMsMeansCompany = 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)\n", pGVMM->iUsedHead, pGVMM->cVMs);
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, "cVMsMeansCompany"))
327 {
328 if (u64Value <= UINT32_MAX)
329 pGVMM->cVMsMeansCompany = 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, "cVMsMeansCompany"))
395 *pu64Value = pGVMM->cVMsMeansCompany;
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 rc = RTR0MemObjAllocLow(&pGVM->gvmm.s.VMMemObj, cPages << PAGE_SHIFT, false /* fExecutable */);
599 if (RT_SUCCESS(rc))
600 {
601 PVM pVM = (PVM)RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj); AssertPtr(pVM);
602 memset(pVM, 0, cPages << PAGE_SHIFT);
603 pVM->enmVMState = VMSTATE_CREATING;
604 pVM->pVMR0 = pVM;
605 pVM->pSession = pSession;
606 pVM->hSelf = iHandle;
607 pVM->cbSelf = cbVM;
608 pVM->cCPUs = cCpus;
609 pVM->offVMCPU = RT_UOFFSETOF(VM, aCpus);
610
611 rc = RTR0MemObjAllocPage(&pGVM->gvmm.s.VMPagesMemObj, cPages * sizeof(SUPPAGE), false /* fExecutable */);
612 if (RT_SUCCESS(rc))
613 {
614 PSUPPAGE paPages = (PSUPPAGE)RTR0MemObjAddress(pGVM->gvmm.s.VMPagesMemObj); AssertPtr(paPages);
615 for (uint32_t iPage = 0; iPage < cPages; iPage++)
616 {
617 paPages[iPage].uReserved = 0;
618 paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pGVM->gvmm.s.VMMemObj, iPage);
619 Assert(paPages[iPage].Phys != NIL_RTHCPHYS);
620 }
621
622 /*
623 * Map them into ring-3.
624 */
625 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMMapObj, pGVM->gvmm.s.VMMemObj, (RTR3PTR)-1, 0,
626 RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
627 if (RT_SUCCESS(rc))
628 {
629 pVM->pVMR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMMapObj);
630 AssertPtr((void *)pVM->pVMR3);
631
632 /* Initialize all the VM pointers. */
633 for (uint32_t i = 0; i < cCpus; i++)
634 {
635 pVM->aCpus[i].pVMR0 = pVM;
636 pVM->aCpus[i].pVMR3 = pVM->pVMR3;
637 }
638
639 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMPagesMapObj, pGVM->gvmm.s.VMPagesMemObj, (RTR3PTR)-1, 0,
640 RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
641 if (RT_SUCCESS(rc))
642 {
643 pVM->paVMPagesR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMPagesMapObj);
644 AssertPtr((void *)pVM->paVMPagesR3);
645
646 /* complete the handle - take the UsedLock sem just to be careful. */
647 rc = gvmmR0UsedLock(pGVMM);
648 AssertRC(rc);
649
650 pHandle->pVM = pVM;
651 pHandle->pGVM = pGVM;
652 pHandle->hEMT0 = hEMT0;
653 pHandle->ProcId = ProcId;
654 pGVM->pVM = pVM;
655 pGVM->aCpus[0].hEMT = hEMT0;
656
657 gvmmR0UsedUnlock(pGVMM);
658 gvmmR0CreateDestroyUnlock(pGVMM);
659
660 *ppVM = pVM;
661 Log(("GVMMR0CreateVM: pVM=%p pVMR3=%p pGVM=%p hGVM=%d\n", pVM, pVM->pVMR3, pGVM, iHandle));
662 return VINF_SUCCESS;
663 }
664
665 RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */);
666 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
667 }
668 RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */);
669 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
670 }
671 RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */);
672 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
673 }
674 }
675 }
676 /* else: The user wasn't permitted to create this VM. */
677
678 /*
679 * The handle will be freed by gvmmR0HandleObjDestructor as we release the
680 * object reference here. A little extra mess because of non-recursive lock.
681 */
682 void *pvObj = pHandle->pvObj;
683 pHandle->pvObj = NULL;
684 gvmmR0CreateDestroyUnlock(pGVMM);
685
686 SUPR0ObjRelease(pvObj, pSession);
687
688 SUPR0Printf("GVMMR0CreateVM: failed, rc=%d\n", rc);
689 return rc;
690 }
691
692 rc = VERR_NO_MEMORY;
693 }
694 else
695 rc = VERR_INTERNAL_ERROR;
696 }
697 else
698 rc = VERR_GVM_TOO_MANY_VMS;
699
700 gvmmR0CreateDestroyUnlock(pGVMM);
701 return rc;
702}
703
704
705/**
706 * Initializes the per VM data belonging to GVMM.
707 *
708 * @param pGVM Pointer to the global VM structure.
709 */
710static void gvmmR0InitPerVMData(PGVM pGVM)
711{
712 AssertCompile(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
713 AssertCompile(RT_SIZEOFMEMB(GVMCPU,gvmm.s) <= RT_SIZEOFMEMB(GVMCPU,gvmm.padding));
714 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
715 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
716 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
717 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
718 pGVM->gvmm.s.fDoneVMMR0Init = false;
719 pGVM->gvmm.s.fDoneVMMR0Term = false;
720
721 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
722 {
723 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
724 pGVM->aCpus[i].hEMT = NIL_RTNATIVETHREAD;
725 }
726}
727
728
729/**
730 * Does the VM initialization.
731 *
732 * @returns VBox status code.
733 * @param pVM Pointer to the shared VM structure.
734 */
735GVMMR0DECL(int) GVMMR0InitVM(PVM pVM)
736{
737 LogFlow(("GVMMR0InitVM: pVM=%p\n", pVM));
738
739 /*
740 * Validate the VM structure, state and handle.
741 */
742 PGVM pGVM;
743 PGVMM pGVMM;
744 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
745 if (RT_SUCCESS(rc))
746 {
747 if ( !pGVM->gvmm.s.fDoneVMMR0Init
748 && pGVM->aCpus[0].gvmm.s.HaltEventMulti == NIL_RTSEMEVENTMULTI)
749 {
750 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
751 {
752 rc = RTSemEventMultiCreate(&pGVM->aCpus[i].gvmm.s.HaltEventMulti);
753 if (RT_FAILURE(rc))
754 {
755 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
756 break;
757 }
758 }
759 }
760 else
761 rc = VERR_WRONG_ORDER;
762 }
763
764 LogFlow(("GVMMR0InitVM: returns %Rrc\n", rc));
765 return rc;
766}
767
768
769/**
770 * Indicates that we're done with the ring-0 initialization
771 * of the VM.
772 *
773 * @param pVM Pointer to the shared VM structure.
774 * @thread EMT(0)
775 */
776GVMMR0DECL(void) GVMMR0DoneInitVM(PVM pVM)
777{
778 /* Validate the VM structure, state and handle. */
779 PGVM pGVM;
780 PGVMM pGVMM;
781 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
782 AssertRCReturnVoid(rc);
783
784 /* Set the indicator. */
785 pGVM->gvmm.s.fDoneVMMR0Init = true;
786}
787
788
789/**
790 * Indicates that we're doing the ring-0 termination of the VM.
791 *
792 * @returns true if termination hasn't been done already, false if it has.
793 * @param pVM Pointer to the shared VM structure.
794 * @param pGVM Pointer to the global VM structure. Optional.
795 * @thread EMT(0)
796 */
797GVMMR0DECL(bool) GVMMR0DoingTermVM(PVM pVM, PGVM pGVM)
798{
799 /* Validate the VM structure, state and handle. */
800 AssertPtrNullReturn(pGVM, false);
801 AssertReturn(!pGVM || pGVM->u32Magic == GVM_MAGIC, false);
802 if (!pGVM)
803 {
804 PGVMM pGVMM;
805 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
806 AssertRCReturn(rc, false);
807 }
808
809 /* Set the indicator. */
810 if (pGVM->gvmm.s.fDoneVMMR0Term)
811 return false;
812 pGVM->gvmm.s.fDoneVMMR0Term = true;
813 return true;
814}
815
816
817/**
818 * Destroys the VM, freeing all associated resources (the ring-0 ones anyway).
819 *
820 * This is call from the vmR3DestroyFinalBit and from a error path in VMR3Create,
821 * and the caller is not the EMT thread, unfortunately. For security reasons, it
822 * would've been nice if the caller was actually the EMT thread or that we somehow
823 * could've associated the calling thread with the VM up front.
824 *
825 * @returns VBox status code.
826 * @param pVM Where to store the pointer to the VM structure.
827 *
828 * @thread EMT(0) if it's associated with the VM, otherwise any thread.
829 */
830GVMMR0DECL(int) GVMMR0DestroyVM(PVM pVM)
831{
832 LogFlow(("GVMMR0DestroyVM: pVM=%p\n", pVM));
833 PGVMM pGVMM;
834 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
835
836
837 /*
838 * Validate the VM structure, state and caller.
839 */
840 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
841 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
842 AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_TERMINATED, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
843
844 uint32_t hGVM = pVM->hSelf;
845 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
846 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
847
848 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
849 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
850
851 RTPROCESS ProcId = RTProcSelf();
852 RTNATIVETHREAD hSelf = RTThreadNativeSelf();
853 AssertReturn( ( pHandle->hEMT0 == hSelf
854 && pHandle->ProcId == ProcId)
855 || pHandle->hEMT0 == NIL_RTNATIVETHREAD, VERR_NOT_OWNER);
856
857 /*
858 * Lookup the handle and destroy the object.
859 * Since the lock isn't recursive and we'll have to leave it before dereferencing the
860 * object, we take some precautions against racing callers just in case...
861 */
862 int rc = gvmmR0CreateDestroyLock(pGVMM);
863 AssertRC(rc);
864
865 /* be careful here because we might theoretically be racing someone else cleaning up. */
866 if ( pHandle->pVM == pVM
867 && ( ( pHandle->hEMT0 == hSelf
868 && pHandle->ProcId == ProcId)
869 || pHandle->hEMT0 == NIL_RTNATIVETHREAD)
870 && VALID_PTR(pHandle->pvObj)
871 && VALID_PTR(pHandle->pSession)
872 && VALID_PTR(pHandle->pGVM)
873 && pHandle->pGVM->u32Magic == GVM_MAGIC)
874 {
875 void *pvObj = pHandle->pvObj;
876 pHandle->pvObj = NULL;
877 gvmmR0CreateDestroyUnlock(pGVMM);
878
879 SUPR0ObjRelease(pvObj, pHandle->pSession);
880 }
881 else
882 {
883 SUPR0Printf("GVMMR0DestroyVM: pHandle=%p:{.pVM=%p, .hEMT0=%p, .ProcId=%u, .pvObj=%p} pVM=%p hSelf=%p\n",
884 pHandle, pHandle->pVM, pHandle->hEMT0, pHandle->ProcId, pHandle->pvObj, pVM, hSelf);
885 gvmmR0CreateDestroyUnlock(pGVMM);
886 rc = VERR_INTERNAL_ERROR;
887 }
888
889 return rc;
890}
891
892
893/**
894 * Performs VM cleanup task as part of object destruction.
895 *
896 * @param pGVM The GVM pointer.
897 */
898static void gvmmR0CleanupVM(PGVM pGVM)
899{
900 if ( pGVM->gvmm.s.fDoneVMMR0Init
901 && !pGVM->gvmm.s.fDoneVMMR0Term)
902 {
903 if ( pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ
904 && RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj) == pGVM->pVM)
905 {
906 LogFlow(("gvmmR0CleanupVM: Calling VMMR0TermVM\n"));
907 VMMR0TermVM(pGVM->pVM, pGVM);
908 }
909 else
910 AssertMsgFailed(("gvmmR0CleanupVM: VMMemObj=%p pVM=%p\n", pGVM->gvmm.s.VMMemObj, pGVM->pVM));
911 }
912
913 GMMR0CleanupVM(pGVM);
914}
915
916
917/**
918 * Handle destructor.
919 *
920 * @param pvGVMM The GVM instance pointer.
921 * @param pvHandle The handle pointer.
922 */
923static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle)
924{
925 LogFlow(("gvmmR0HandleObjDestructor: %p %p %p\n", pvObj, pvGVMM, pvHandle));
926
927 /*
928 * Some quick, paranoid, input validation.
929 */
930 PGVMHANDLE pHandle = (PGVMHANDLE)pvHandle;
931 AssertPtr(pHandle);
932 PGVMM pGVMM = (PGVMM)pvGVMM;
933 Assert(pGVMM == g_pGVMM);
934 const uint16_t iHandle = pHandle - &pGVMM->aHandles[0];
935 if ( !iHandle
936 || iHandle >= RT_ELEMENTS(pGVMM->aHandles)
937 || iHandle != pHandle->iSelf)
938 {
939 SUPR0Printf("GVM: handle %d is out of range or corrupt (iSelf=%d)!\n", iHandle, pHandle->iSelf);
940 return;
941 }
942
943 int rc = gvmmR0CreateDestroyLock(pGVMM);
944 AssertRC(rc);
945 rc = gvmmR0UsedLock(pGVMM);
946 AssertRC(rc);
947
948 /*
949 * This is a tad slow but a doubly linked list is too much hazzle.
950 */
951 if (RT_UNLIKELY(pHandle->iNext >= RT_ELEMENTS(pGVMM->aHandles)))
952 {
953 SUPR0Printf("GVM: used list index %d is out of range!\n", pHandle->iNext);
954 gvmmR0UsedUnlock(pGVMM);
955 gvmmR0CreateDestroyUnlock(pGVMM);
956 return;
957 }
958
959 if (pGVMM->iUsedHead == iHandle)
960 pGVMM->iUsedHead = pHandle->iNext;
961 else
962 {
963 uint16_t iPrev = pGVMM->iUsedHead;
964 int c = RT_ELEMENTS(pGVMM->aHandles) + 2;
965 while (iPrev)
966 {
967 if (RT_UNLIKELY(iPrev >= RT_ELEMENTS(pGVMM->aHandles)))
968 {
969 SUPR0Printf("GVM: used list index %d is out of range!\n");
970 gvmmR0UsedUnlock(pGVMM);
971 gvmmR0CreateDestroyUnlock(pGVMM);
972 return;
973 }
974 if (RT_UNLIKELY(c-- <= 0))
975 {
976 iPrev = 0;
977 break;
978 }
979
980 if (pGVMM->aHandles[iPrev].iNext == iHandle)
981 break;
982 iPrev = pGVMM->aHandles[iPrev].iNext;
983 }
984 if (!iPrev)
985 {
986 SUPR0Printf("GVM: can't find the handle previous previous of %d!\n", pHandle->iSelf);
987 gvmmR0UsedUnlock(pGVMM);
988 gvmmR0CreateDestroyUnlock(pGVMM);
989 return;
990 }
991
992 Assert(pGVMM->aHandles[iPrev].iNext == iHandle);
993 pGVMM->aHandles[iPrev].iNext = pHandle->iNext;
994 }
995 pHandle->iNext = 0;
996 pGVMM->cVMs--;
997
998 gvmmR0UsedUnlock(pGVMM);
999
1000 /*
1001 * Do the global cleanup round.
1002 */
1003 PGVM pGVM = pHandle->pGVM;
1004 if ( VALID_PTR(pGVM)
1005 && pGVM->u32Magic == GVM_MAGIC)
1006 {
1007 gvmmR0CleanupVM(pGVM);
1008
1009 /*
1010 * Do the GVMM cleanup - must be done last.
1011 */
1012 /* The VM and VM pages mappings/allocations. */
1013 if (pGVM->gvmm.s.VMPagesMapObj != NIL_RTR0MEMOBJ)
1014 {
1015 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMapObj, false /* fFreeMappings */); AssertRC(rc);
1016 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
1017 }
1018
1019 if (pGVM->gvmm.s.VMMapObj != NIL_RTR0MEMOBJ)
1020 {
1021 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */); AssertRC(rc);
1022 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
1023 }
1024
1025 if (pGVM->gvmm.s.VMPagesMemObj != NIL_RTR0MEMOBJ)
1026 {
1027 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */); AssertRC(rc);
1028 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
1029 }
1030
1031 if (pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ)
1032 {
1033 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */); AssertRC(rc);
1034 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
1035 }
1036
1037 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
1038 {
1039 if (pGVM->aCpus[i].gvmm.s.HaltEventMulti != NIL_RTSEMEVENTMULTI)
1040 {
1041 rc = RTSemEventMultiDestroy(pGVM->aCpus[i].gvmm.s.HaltEventMulti); AssertRC(rc);
1042 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
1043 }
1044 }
1045
1046 /* the GVM structure itself. */
1047 pGVM->u32Magic |= UINT32_C(0x80000000);
1048 RTMemFree(pGVM);
1049 }
1050 /* else: GVMMR0CreateVM cleanup. */
1051
1052 /*
1053 * Free the handle.
1054 * Reacquire the UsedLock here to since we're updating handle fields.
1055 */
1056 rc = gvmmR0UsedLock(pGVMM);
1057 AssertRC(rc);
1058
1059 pHandle->iNext = pGVMM->iFreeHead;
1060 pGVMM->iFreeHead = iHandle;
1061 ASMAtomicXchgPtr((void * volatile *)&pHandle->pGVM, NULL);
1062 ASMAtomicXchgPtr((void * volatile *)&pHandle->pVM, NULL);
1063 ASMAtomicXchgPtr((void * volatile *)&pHandle->pvObj, NULL);
1064 ASMAtomicXchgPtr((void * volatile *)&pHandle->pSession, NULL);
1065 ASMAtomicXchgSize(&pHandle->hEMT0, NIL_RTNATIVETHREAD);
1066 ASMAtomicXchgSize(&pHandle->ProcId, NIL_RTPROCESS);
1067
1068 gvmmR0UsedUnlock(pGVMM);
1069 gvmmR0CreateDestroyUnlock(pGVMM);
1070 LogFlow(("gvmmR0HandleObjDestructor: returns\n"));
1071}
1072
1073
1074/**
1075 * Registers the calling thread as the EMT of a Virtual CPU.
1076 *
1077 * Note that VCPU 0 is automatically registered during VM creation.
1078 *
1079 * @returns VBox status code
1080 * @param pVM The shared VM structure (the ring-0 mapping).
1081 * @param idCpu VCPU id.
1082 */
1083GVMMR0DECL(int) GVMMR0RegisterVCpu(PVM pVM, VMCPUID idCpu)
1084{
1085 AssertReturn(idCpu != 0, VERR_NOT_OWNER);
1086
1087 /*
1088 * Validate the VM structure, state and handle.
1089 */
1090 PGVM pGVM;
1091 PGVMM pGVMM;
1092 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, false /* fTakeUsedLock */);
1093 if (RT_FAILURE(rc))
1094 return rc;
1095
1096 AssertReturn(idCpu < pVM->cCPUs, VERR_INVALID_CPU_ID);
1097 AssertReturn(pGVM->aCpus[idCpu].hEMT == NIL_RTNATIVETHREAD, VERR_ACCESS_DENIED);
1098
1099 pGVM->aCpus[idCpu].hEMT = RTThreadNativeSelf();
1100 return VINF_SUCCESS;
1101}
1102
1103
1104/**
1105 * Lookup a GVM structure by its handle.
1106 *
1107 * @returns The GVM pointer on success, NULL on failure.
1108 * @param hGVM The global VM handle. Asserts on bad handle.
1109 */
1110GVMMR0DECL(PGVM) GVMMR0ByHandle(uint32_t hGVM)
1111{
1112 PGVMM pGVMM;
1113 GVMM_GET_VALID_INSTANCE(pGVMM, NULL);
1114
1115 /*
1116 * Validate.
1117 */
1118 AssertReturn(hGVM != NIL_GVM_HANDLE, NULL);
1119 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), NULL);
1120
1121 /*
1122 * Look it up.
1123 */
1124 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1125 AssertPtrReturn(pHandle->pVM, NULL);
1126 AssertPtrReturn(pHandle->pvObj, NULL);
1127 PGVM pGVM = pHandle->pGVM;
1128 AssertPtrReturn(pGVM, NULL);
1129 AssertReturn(pGVM->pVM == pHandle->pVM, NULL);
1130
1131 return pHandle->pGVM;
1132}
1133
1134
1135/**
1136 * Lookup a GVM structure by the shared VM structure.
1137 *
1138 * The calling thread must be in the same process as the VM. All current lookups
1139 * are by threads inside the same process, so this will not be an issue.
1140 *
1141 * @returns VBox status code.
1142 * @param pVM The shared VM structure (the ring-0 mapping).
1143 * @param ppGVM Where to store the GVM pointer.
1144 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1145 * @param fTakeUsedLock Whether to take the used lock or not.
1146 * Be very careful if not taking the lock as it's possible that
1147 * the VM will disappear then.
1148 *
1149 * @remark This will not assert on an invalid pVM but try return sliently.
1150 */
1151static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock)
1152{
1153 RTPROCESS ProcId = RTProcSelf();
1154 PGVMM pGVMM;
1155 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1156
1157 /*
1158 * Validate.
1159 */
1160 if (RT_UNLIKELY( !VALID_PTR(pVM)
1161 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
1162 return VERR_INVALID_POINTER;
1163 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
1164 || pVM->enmVMState >= VMSTATE_TERMINATED))
1165 return VERR_INVALID_POINTER;
1166
1167 uint16_t hGVM = pVM->hSelf;
1168 if (RT_UNLIKELY( hGVM == NIL_GVM_HANDLE
1169 || hGVM >= RT_ELEMENTS(pGVMM->aHandles)))
1170 return VERR_INVALID_HANDLE;
1171
1172 /*
1173 * Look it up.
1174 */
1175 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1176 PGVM pGVM;
1177 if (fTakeUsedLock)
1178 {
1179 int rc = gvmmR0UsedLock(pGVMM);
1180 AssertRCReturn(rc, rc);
1181
1182 pGVM = pHandle->pGVM;
1183 if (RT_UNLIKELY( pHandle->pVM != pVM
1184 || pHandle->ProcId != ProcId
1185 || !VALID_PTR(pHandle->pvObj)
1186 || !VALID_PTR(pGVM)
1187 || pGVM->pVM != pVM))
1188 {
1189 gvmmR0UsedUnlock(pGVMM);
1190 return VERR_INVALID_HANDLE;
1191 }
1192 }
1193 else
1194 {
1195 if (RT_UNLIKELY(pHandle->pVM != pVM))
1196 return VERR_INVALID_HANDLE;
1197 if (RT_UNLIKELY(pHandle->ProcId != ProcId))
1198 return VERR_INVALID_HANDLE;
1199 if (RT_UNLIKELY(!VALID_PTR(pHandle->pvObj)))
1200 return VERR_INVALID_HANDLE;
1201
1202 pGVM = pHandle->pGVM;
1203 if (RT_UNLIKELY(!VALID_PTR(pGVM)))
1204 return VERR_INVALID_HANDLE;
1205 if (RT_UNLIKELY(pGVM->pVM != pVM))
1206 return VERR_INVALID_HANDLE;
1207 }
1208
1209 *ppGVM = pGVM;
1210 *ppGVMM = pGVMM;
1211 return VINF_SUCCESS;
1212}
1213
1214
1215/**
1216 * Lookup a GVM structure by the shared VM structure.
1217 *
1218 * @returns The GVM pointer on success, NULL on failure.
1219 * @param pVM The shared VM structure (the ring-0 mapping).
1220 *
1221 * @remark This will not take the 'used'-lock because it doesn't do
1222 * nesting and this function will be used from under the lock.
1223 */
1224GVMMR0DECL(PGVM) GVMMR0ByVM(PVM pVM)
1225{
1226 PGVM pGVM;
1227 PGVMM pGVMM;
1228 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, false /* fTakeUsedLock */);
1229 if (RT_SUCCESS(rc))
1230 return pGVM;
1231 AssertRC(rc);
1232 return NULL;
1233}
1234
1235
1236/**
1237 * Lookup a GVM structure by the shared VM structure and ensuring that the
1238 * caller is an EMT thread.
1239 *
1240 * @returns VBox status code.
1241 * @param pVM The shared VM structure (the ring-0 mapping).
1242 * @param idCpu The Virtual CPU ID of the calling EMT.
1243 * @param ppGVM Where to store the GVM pointer.
1244 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1245 * @thread EMT
1246 *
1247 * @remark This will assert in all failure paths.
1248 */
1249static int gvmmR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM, PGVMM *ppGVMM)
1250{
1251 PGVMM pGVMM;
1252 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1253
1254 /*
1255 * Validate.
1256 */
1257 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1258 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1259
1260 uint16_t hGVM = pVM->hSelf;
1261 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
1262 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
1263
1264 /*
1265 * Look it up.
1266 */
1267 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1268 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
1269 RTPROCESS ProcId = RTProcSelf();
1270 AssertReturn(pHandle->ProcId == ProcId, VERR_NOT_OWNER);
1271 AssertPtrReturn(pHandle->pvObj, VERR_INTERNAL_ERROR);
1272
1273 PGVM pGVM = pHandle->pGVM;
1274 AssertPtrReturn(pGVM, VERR_INTERNAL_ERROR);
1275 AssertReturn(pGVM->pVM == pVM, VERR_INTERNAL_ERROR);
1276 RTNATIVETHREAD hAllegedEMT = RTThreadNativeSelf();
1277 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
1278 AssertReturn(pGVM->aCpus[idCpu].hEMT == hAllegedEMT, VERR_INTERNAL_ERROR);
1279
1280 *ppGVM = pGVM;
1281 *ppGVMM = pGVMM;
1282 return VINF_SUCCESS;
1283}
1284
1285
1286/**
1287 * Lookup a GVM structure by the shared VM structure
1288 * and ensuring that the caller is the EMT thread.
1289 *
1290 * @returns VBox status code.
1291 * @param pVM The shared VM structure (the ring-0 mapping).
1292 * @param idCpu The Virtual CPU ID of the calling EMT.
1293 * @param ppGVM Where to store the GVM pointer.
1294 * @thread EMT
1295 */
1296GVMMR0DECL(int) GVMMR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM)
1297{
1298 AssertPtrReturn(ppGVM, VERR_INVALID_POINTER);
1299 PGVMM pGVMM;
1300 return gvmmR0ByVMAndEMT(pVM, idCpu, ppGVM, &pGVMM);
1301}
1302
1303
1304/**
1305 * Lookup a VM by its global handle.
1306 *
1307 * @returns The VM handle on success, NULL on failure.
1308 * @param hGVM The global VM handle. Asserts on bad handle.
1309 */
1310GVMMR0DECL(PVM) GVMMR0GetVMByHandle(uint32_t hGVM)
1311{
1312 PGVM pGVM = GVMMR0ByHandle(hGVM);
1313 return pGVM ? pGVM->pVM : NULL;
1314}
1315
1316
1317/**
1318 * Looks up the VM belonging to the specified EMT thread.
1319 *
1320 * This is used by the assertion machinery in VMMR0.cpp to avoid causing
1321 * unnecessary kernel panics when the EMT thread hits an assertion. The
1322 * call may or not be an EMT thread.
1323 *
1324 * @returns The VM handle on success, NULL on failure.
1325 * @param hEMT The native thread handle of the EMT.
1326 * NIL_RTNATIVETHREAD means the current thread
1327 */
1328GVMMR0DECL(PVM) GVMMR0GetVMByEMT(RTNATIVETHREAD hEMT)
1329{
1330 /*
1331 * No Assertions here as we're usually called in a AssertMsgN or
1332 * RTAssert* context.
1333 */
1334 PGVMM pGVMM = g_pGVMM;
1335 if ( !VALID_PTR(pGVMM)
1336 || pGVMM->u32Magic != GVMM_MAGIC)
1337 return NULL;
1338
1339 if (hEMT == NIL_RTNATIVETHREAD)
1340 hEMT = RTThreadNativeSelf();
1341 RTPROCESS ProcId = RTProcSelf();
1342
1343 /*
1344 * Search the handles in a linear fashion as we don't dare to take the lock (assert).
1345 */
1346 for (unsigned i = 1; i < RT_ELEMENTS(pGVMM->aHandles); i++)
1347 {
1348 if ( pGVMM->aHandles[i].iSelf == i
1349 && pGVMM->aHandles[i].ProcId == ProcId
1350 && VALID_PTR(pGVMM->aHandles[i].pvObj)
1351 && VALID_PTR(pGVMM->aHandles[i].pVM)
1352 && VALID_PTR(pGVMM->aHandles[i].pGVM))
1353 {
1354 if (pGVMM->aHandles[i].hEMT0 == hEMT)
1355 return pGVMM->aHandles[i].pVM;
1356
1357 /* This is fearly safe with the current process per VM approach. */
1358 PGVM pGVM = pGVMM->aHandles[i].pGVM;
1359 VMCPUID const cCpus = pGVM->cCpus;
1360 if ( cCpus < 1
1361 || cCpus > VMM_MAX_CPU_COUNT)
1362 continue;
1363 for (VMCPUID idCpu = 1; idCpu < cCpus; idCpu++)
1364 if (pGVM->aCpus[idCpu].hEMT == hEMT)
1365 return pGVMM->aHandles[i].pVM;
1366 }
1367 }
1368 return NULL;
1369}
1370
1371
1372/**
1373 * This is will wake up expired and soon-to-be expired VMs.
1374 *
1375 * @returns Number of VMs that has been woken up.
1376 * @param pGVMM Pointer to the GVMM instance data.
1377 * @param u64Now The current time.
1378 */
1379static unsigned gvmmR0SchedDoWakeUps(PGVMM pGVMM, uint64_t u64Now)
1380{
1381/** @todo Rewrite this algorithm. See performance defect XYZ. */
1382
1383 /*
1384 * The first pass will wake up VMs which have actually expired
1385 * and look for VMs that should be woken up in the 2nd and 3rd passes.
1386 */
1387 unsigned cWoken = 0;
1388 unsigned cHalted = 0;
1389 unsigned cTodo2nd = 0;
1390 unsigned cTodo3rd = 0;
1391 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1392 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1393 i = pGVMM->aHandles[i].iNext)
1394 {
1395 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1396 if ( VALID_PTR(pCurGVM)
1397 && pCurGVM->u32Magic == GVM_MAGIC)
1398 {
1399 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1400 {
1401 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1402
1403 uint64_t u64 = pCurGVCpu->gvmm.s.u64HaltExpire;
1404 if (u64)
1405 {
1406 if (u64 <= u64Now)
1407 {
1408 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1409 {
1410 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1411 AssertRC(rc);
1412 cWoken++;
1413 }
1414 }
1415 else
1416 {
1417 cHalted++;
1418 if (u64 <= u64Now + pGVMM->nsEarlyWakeUp1)
1419 cTodo2nd++;
1420 else if (u64 <= u64Now + pGVMM->nsEarlyWakeUp2)
1421 cTodo3rd++;
1422 }
1423 }
1424 }
1425 }
1426 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1427 }
1428
1429 if (cTodo2nd)
1430 {
1431 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1432 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1433 i = pGVMM->aHandles[i].iNext)
1434 {
1435 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1436 if ( VALID_PTR(pCurGVM)
1437 && pCurGVM->u32Magic == GVM_MAGIC)
1438 {
1439 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1440 {
1441 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1442
1443 if ( pCurGVCpu->gvmm.s.u64HaltExpire
1444 && pCurGVCpu->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp1)
1445 {
1446 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1447 {
1448 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1449 AssertRC(rc);
1450 cWoken++;
1451 }
1452 }
1453 }
1454 }
1455 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1456 }
1457 }
1458
1459 if (cTodo3rd)
1460 {
1461 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1462 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1463 i = pGVMM->aHandles[i].iNext)
1464 {
1465 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1466 if ( VALID_PTR(pCurGVM)
1467 && pCurGVM->u32Magic == GVM_MAGIC)
1468 {
1469 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1470 {
1471 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1472
1473 if ( pCurGVCpu->gvmm.s.u64HaltExpire
1474 && pCurGVCpu->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp2)
1475 {
1476 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1477 {
1478 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1479 AssertRC(rc);
1480 cWoken++;
1481 }
1482 }
1483 }
1484 }
1485 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1486 }
1487 }
1488
1489 return cWoken;
1490}
1491
1492
1493/**
1494 * Halt the EMT thread.
1495 *
1496 * @returns VINF_SUCCESS normal wakeup (timeout or kicked by other thread).
1497 * VERR_INTERRUPTED if a signal was scheduled for the thread.
1498 * @param pVM Pointer to the shared VM structure.
1499 * @param idCpu The Virtual CPU ID of the calling EMT.
1500 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
1501 * @thread EMT(idCpu).
1502 */
1503GVMMR0DECL(int) GVMMR0SchedHalt(PVM pVM, VMCPUID idCpu, uint64_t u64ExpireGipTime)
1504{
1505 LogFlow(("GVMMR0SchedHalt: pVM=%p\n", pVM));
1506
1507 /*
1508 * Validate the VM structure, state and handle.
1509 */
1510 PGVM pGVM;
1511 PGVMM pGVMM;
1512 int rc = gvmmR0ByVMAndEMT(pVM, idCpu, &pGVM, &pGVMM);
1513 if (RT_FAILURE(rc))
1514 return rc;
1515 pGVM->gvmm.s.StatsSched.cHaltCalls++;
1516
1517 PGVMCPU pCurGVCpu = &pGVM->aCpus[idCpu];
1518 Assert(idCpu < pGVM->cCpus);
1519 Assert(!pCurGVCpu->gvmm.s.u64HaltExpire);
1520
1521 /*
1522 * Take the UsedList semaphore, get the current time
1523 * and check if anyone needs waking up.
1524 * Interrupts must NOT be disabled at this point because we ask for GIP time!
1525 */
1526 rc = gvmmR0UsedLock(pGVMM);
1527 AssertRC(rc);
1528
1529 pCurGVCpu->gvmm.s.iCpuEmt = ASMGetApicId();
1530
1531 Assert(ASMGetFlags() & X86_EFL_IF);
1532 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
1533 pGVM->gvmm.s.StatsSched.cHaltWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
1534
1535 /*
1536 * Go to sleep if we must...
1537 */
1538 if ( u64Now < u64ExpireGipTime
1539 && u64ExpireGipTime - u64Now > (pGVMM->cVMs > pGVMM->cVMsMeansCompany
1540 ? pGVMM->nsMinSleepCompany
1541 : pGVMM->nsMinSleepAlone))
1542 {
1543 pGVM->gvmm.s.StatsSched.cHaltBlocking++;
1544 ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, u64ExpireGipTime);
1545 gvmmR0UsedUnlock(pGVMM);
1546
1547 uint32_t cMillies = (u64ExpireGipTime - u64Now) / 1000000;
1548 rc = RTSemEventMultiWaitNoResume(pCurGVCpu->gvmm.s.HaltEventMulti, cMillies ? cMillies : 1);
1549 ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0);
1550 if (rc == VERR_TIMEOUT)
1551 {
1552 pGVM->gvmm.s.StatsSched.cHaltTimeouts++;
1553 rc = VINF_SUCCESS;
1554 }
1555 }
1556 else
1557 {
1558 pGVM->gvmm.s.StatsSched.cHaltNotBlocking++;
1559 gvmmR0UsedUnlock(pGVMM);
1560 }
1561
1562 /* Make sure false wake up calls (gvmmR0SchedDoWakeUps) cause us to spin. */
1563 RTSemEventMultiReset(pCurGVCpu->gvmm.s.HaltEventMulti);
1564
1565 return rc;
1566}
1567
1568
1569/**
1570 * Worker for GVMMR0SchedWakeUp and GVMMR0SchedWakeUpAndPokeCpus that wakes up
1571 * the a sleeping EMT.
1572 *
1573 * @retval VINF_SUCCESS if successfully woken up.
1574 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
1575 *
1576 * @param pGVM The global (ring-0) VM structure.
1577 * @param pGVCpu The global (ring-0) VCPU structure.
1578 */
1579DECLINLINE(int) gvmmR0SchedWakeUpOne(PGVM pGVM, PGVMCPU pGVCpu)
1580{
1581 int rc;
1582 pGVM->gvmm.s.StatsSched.cWakeUpCalls++;
1583
1584 /*
1585 * Signal the semaphore regardless of whether it's current blocked on it.
1586 *
1587 * The reason for this is that there is absolutely no way we can be 100%
1588 * certain that it isn't *about* go to go to sleep on it and just got
1589 * delayed a bit en route. So, we will always signal the semaphore when
1590 * the it is flagged as halted in the VMM.
1591 */
1592/** @todo we can optimize some of that by means of the pVCpu->enmState now. */
1593 if (pGVCpu->gvmm.s.u64HaltExpire)
1594 {
1595 rc = VINF_SUCCESS;
1596 ASMAtomicXchgU64(&pGVCpu->gvmm.s.u64HaltExpire, 0);
1597 }
1598 else
1599 {
1600 rc = VINF_GVM_NOT_BLOCKED;
1601 pGVM->gvmm.s.StatsSched.cWakeUpNotHalted++;
1602 }
1603
1604 int rc2 = RTSemEventMultiSignal(pGVCpu->gvmm.s.HaltEventMulti);
1605 AssertRC(rc2);
1606
1607 return rc;
1608}
1609
1610
1611/**
1612 * Wakes up the halted EMT thread so it can service a pending request.
1613 *
1614 * @returns VBox status code.
1615 * @retval VINF_SUCCESS if successfully woken up.
1616 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
1617 *
1618 * @param pVM Pointer to the shared VM structure.
1619 * @param idCpu The Virtual CPU ID of the EMT to wake up.
1620 * @thread Any but EMT.
1621 */
1622GVMMR0DECL(int) GVMMR0SchedWakeUp(PVM pVM, VMCPUID idCpu)
1623{
1624 /*
1625 * Validate input and take the UsedLock.
1626 */
1627 PGVM pGVM;
1628 PGVMM pGVMM;
1629 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
1630 if (RT_SUCCESS(rc))
1631 {
1632 if (idCpu < pGVM->cCpus)
1633 {
1634 /*
1635 * Do the actual job.
1636 */
1637 rc = gvmmR0SchedWakeUpOne(pGVM, &pGVM->aCpus[idCpu]);
1638
1639 /*
1640 * While we're here, do a round of scheduling.
1641 */
1642 Assert(ASMGetFlags() & X86_EFL_IF);
1643 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
1644 pGVM->gvmm.s.StatsSched.cWakeUpWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
1645 }
1646 else
1647 rc = VERR_INVALID_CPU_ID;
1648
1649 int rc2 = gvmmR0UsedUnlock(pGVMM);
1650 AssertRC(rc2);
1651 }
1652
1653 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
1654 return rc;
1655}
1656
1657
1658/**
1659 * Worker common to GVMMR0SchedPoke and GVMMR0SchedWakeUpAndPokeCpus that pokes
1660 * the Virtual CPU if it's still busy executing guest code.
1661 *
1662 * @returns VBox status code.
1663 * @retval VINF_SUCCESS if poked successfully.
1664 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
1665 *
1666 * @param pGVM The global (ring-0) VM structure.
1667 * @param pVCpu The Virtual CPU handle.
1668 */
1669DECLINLINE(int) gvmmR0SchedPokeOne(PGVM pGVM, PVMCPU pVCpu)
1670{
1671 if (pVCpu->enmState != VMCPUSTATE_STARTED_EXEC)
1672 return VINF_GVM_NOT_BUSY_IN_GC;
1673
1674 /** @todo do the actual poking, need to get the current cpu id from HWACC or
1675 * somewhere and then call RTMpPokeCpu(). */
1676
1677 return VINF_SUCCESS;
1678}
1679
1680
1681/**
1682 * Pokes an EMT if it's still busy running guest code.
1683 *
1684 * @returns VBox status code.
1685 * @retval VINF_SUCCESS if poked successfully.
1686 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
1687 *
1688 * @param pVM Pointer to the shared VM structure.
1689 * @param idCpu The ID of the virtual CPU to poke.
1690 */
1691GVMMR0DECL(int) GVMMR0SchedPoke(PVM pVM, VMCPUID idCpu)
1692{
1693 /*
1694 * Validate input and take the UsedLock.
1695 */
1696 PGVM pGVM;
1697 PGVMM pGVMM;
1698 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
1699 if (RT_SUCCESS(rc))
1700 {
1701 if (idCpu < pGVM->cCpus)
1702 rc = gvmmR0SchedPokeOne(pGVM, &pVM->aCpus[idCpu]);
1703 else
1704 rc = VERR_INVALID_CPU_ID;
1705
1706 int rc2 = gvmmR0UsedUnlock(pGVMM);
1707 AssertRC(rc2);
1708 }
1709
1710 LogFlow(("GVMMR0SchedWakeUpAndPokeCpus: returns %Rrc\n", rc));
1711 return rc;
1712
1713}
1714
1715
1716/**
1717 * Wakes up a set of halted EMT threads so they can service pending request.
1718 *
1719 * @returns VBox status code, no informational stuff.
1720 *
1721 * @param pVM Pointer to the shared VM structure.
1722 * @param pSleepSet The set of sleepers to wake up.
1723 * @param pPokeSet The set of CPUs to poke.
1724 */
1725GVMMR0DECL(int) GVMMR0SchedWakeUpAndPokeCpus(PVM pVM, PCVMCPUSET pSleepSet, PCVMCPUSET pPokeSet)
1726{
1727 AssertPtrReturn(pSleepSet, VERR_INVALID_POINTER);
1728 AssertPtrReturn(pPokeSet, VERR_INVALID_POINTER);
1729 RTNATIVETHREAD hSelf = RTThreadNativeSelf();
1730
1731 /*
1732 * Validate input and take the UsedLock.
1733 */
1734 PGVM pGVM;
1735 PGVMM pGVMM;
1736 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
1737 if (RT_SUCCESS(rc))
1738 {
1739 rc = VINF_SUCCESS;
1740 VMCPUID idCpu = pGVM->cCpus;
1741 while (idCpu-- > 0)
1742 {
1743 /* Don't try poke or wake up ourselves. */
1744 if (pGVM->aCpus[idCpu].hEMT == hSelf)
1745 continue;
1746
1747 /* just ignore errors for now. */
1748 if (VMCPUSET_IS_PRESENT(pSleepSet, idCpu))
1749 gvmmR0SchedWakeUpOne(pGVM, &pGVM->aCpus[idCpu]);
1750 else if (VMCPUSET_IS_PRESENT(pPokeSet, idCpu))
1751 gvmmR0SchedPokeOne(pGVM, &pVM->aCpus[idCpu]);
1752 }
1753
1754 int rc2 = gvmmR0UsedUnlock(pGVMM);
1755 AssertRC(rc2);
1756 }
1757
1758 LogFlow(("GVMMR0SchedWakeUpAndPokeCpus: returns %Rrc\n", rc));
1759 return rc;
1760}
1761
1762
1763/**
1764 * VMMR0 request wrapper for GVMMR0SchedWakeUpAndPokeCpus.
1765 *
1766 * @returns see GVMMR0SchedWakeUpAndPokeCpus.
1767 * @param pVM Pointer to the shared VM structure.
1768 * @param pReq The request packet.
1769 */
1770GVMMR0DECL(int) GVMMR0SchedWakeUpAndPokeCpusReq(PVM pVM, PGVMMSCHEDWAKEUPANDPOKECPUSREQ pReq)
1771{
1772 /*
1773 * Validate input and pass it on.
1774 */
1775 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
1776 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
1777
1778 return GVMMR0SchedWakeUpAndPokeCpus(pVM, &pReq->SleepSet, &pReq->PokeSet);
1779}
1780
1781
1782
1783/**
1784 * Poll the schedule to see if someone else should get a chance to run.
1785 *
1786 * This is a bit hackish and will not work too well if the machine is
1787 * under heavy load from non-VM processes.
1788 *
1789 * @returns VINF_SUCCESS if not yielded.
1790 * VINF_GVM_YIELDED if an attempt to switch to a different VM task was made.
1791 * @param pVM Pointer to the shared VM structure.
1792 * @param idCpu The Virtual CPU ID of the calling EMT.
1793 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
1794 * @param fYield Whether to yield or not.
1795 * This is for when we're spinning in the halt loop.
1796 * @thread EMT(idCpu).
1797 */
1798GVMMR0DECL(int) GVMMR0SchedPoll(PVM pVM, VMCPUID idCpu, bool fYield)
1799{
1800 /*
1801 * Validate input.
1802 */
1803 PGVM pGVM;
1804 PGVMM pGVMM;
1805 int rc = gvmmR0ByVMAndEMT(pVM, idCpu, &pGVM, &pGVMM);
1806 if (RT_SUCCESS(rc))
1807 {
1808 rc = gvmmR0UsedLock(pGVMM);
1809 AssertRC(rc);
1810 pGVM->gvmm.s.StatsSched.cPollCalls++;
1811
1812 Assert(ASMGetFlags() & X86_EFL_IF);
1813 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
1814
1815 if (!fYield)
1816 pGVM->gvmm.s.StatsSched.cPollWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
1817 else
1818 {
1819 /** @todo implement this... */
1820 rc = VERR_NOT_IMPLEMENTED;
1821 }
1822
1823 gvmmR0UsedUnlock(pGVMM);
1824 }
1825
1826 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
1827 return rc;
1828}
1829
1830
1831
1832/**
1833 * Retrieves the GVMM statistics visible to the caller.
1834 *
1835 * @returns VBox status code.
1836 *
1837 * @param pStats Where to put the statistics.
1838 * @param pSession The current session.
1839 * @param pVM The VM to obtain statistics for. Optional.
1840 */
1841GVMMR0DECL(int) GVMMR0QueryStatistics(PGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
1842{
1843 LogFlow(("GVMMR0QueryStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
1844
1845 /*
1846 * Validate input.
1847 */
1848 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1849 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
1850 pStats->cVMs = 0; /* (crash before taking the sem...) */
1851
1852 /*
1853 * Take the lock and get the VM statistics.
1854 */
1855 PGVMM pGVMM;
1856 if (pVM)
1857 {
1858 PGVM pGVM;
1859 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
1860 if (RT_FAILURE(rc))
1861 return rc;
1862 pStats->SchedVM = pGVM->gvmm.s.StatsSched;
1863 }
1864 else
1865 {
1866 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1867 memset(&pStats->SchedVM, 0, sizeof(pStats->SchedVM));
1868
1869 int rc = gvmmR0UsedLock(pGVMM);
1870 AssertRCReturn(rc, rc);
1871 }
1872
1873 /*
1874 * Enumerate the VMs and add the ones visibile to the statistics.
1875 */
1876 pStats->cVMs = 0;
1877 memset(&pStats->SchedSum, 0, sizeof(pStats->SchedSum));
1878
1879 for (unsigned i = pGVMM->iUsedHead;
1880 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1881 i = pGVMM->aHandles[i].iNext)
1882 {
1883 PGVM pGVM = pGVMM->aHandles[i].pGVM;
1884 void *pvObj = pGVMM->aHandles[i].pvObj;
1885 if ( VALID_PTR(pvObj)
1886 && VALID_PTR(pGVM)
1887 && pGVM->u32Magic == GVM_MAGIC
1888 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
1889 {
1890 pStats->cVMs++;
1891
1892 pStats->SchedSum.cHaltCalls += pGVM->gvmm.s.StatsSched.cHaltCalls;
1893 pStats->SchedSum.cHaltBlocking += pGVM->gvmm.s.StatsSched.cHaltBlocking;
1894 pStats->SchedSum.cHaltTimeouts += pGVM->gvmm.s.StatsSched.cHaltTimeouts;
1895 pStats->SchedSum.cHaltNotBlocking += pGVM->gvmm.s.StatsSched.cHaltNotBlocking;
1896 pStats->SchedSum.cHaltWakeUps += pGVM->gvmm.s.StatsSched.cHaltWakeUps;
1897
1898 pStats->SchedSum.cWakeUpCalls += pGVM->gvmm.s.StatsSched.cWakeUpCalls;
1899 pStats->SchedSum.cWakeUpNotHalted += pGVM->gvmm.s.StatsSched.cWakeUpNotHalted;
1900 pStats->SchedSum.cWakeUpWakeUps += pGVM->gvmm.s.StatsSched.cWakeUpWakeUps;
1901
1902 pStats->SchedSum.cPollCalls += pGVM->gvmm.s.StatsSched.cPollCalls;
1903 pStats->SchedSum.cPollHalts += pGVM->gvmm.s.StatsSched.cPollHalts;
1904 pStats->SchedSum.cPollWakeUps += pGVM->gvmm.s.StatsSched.cPollWakeUps;
1905 }
1906 }
1907
1908 gvmmR0UsedUnlock(pGVMM);
1909
1910 return VINF_SUCCESS;
1911}
1912
1913
1914/**
1915 * VMMR0 request wrapper for GVMMR0QueryStatistics.
1916 *
1917 * @returns see GVMMR0QueryStatistics.
1918 * @param pVM Pointer to the shared VM structure. Optional.
1919 * @param pReq The request packet.
1920 */
1921GVMMR0DECL(int) GVMMR0QueryStatisticsReq(PVM pVM, PGVMMQUERYSTATISTICSSREQ pReq)
1922{
1923 /*
1924 * Validate input and pass it on.
1925 */
1926 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
1927 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
1928
1929 return GVMMR0QueryStatistics(&pReq->Stats, pReq->pSession, pVM);
1930}
1931
1932
1933/**
1934 * Resets the specified GVMM statistics.
1935 *
1936 * @returns VBox status code.
1937 *
1938 * @param pStats Which statistics to reset, that is, non-zero fields indicates which to reset.
1939 * @param pSession The current session.
1940 * @param pVM The VM to reset statistics for. Optional.
1941 */
1942GVMMR0DECL(int) GVMMR0ResetStatistics(PCGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
1943{
1944 LogFlow(("GVMMR0ResetStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
1945
1946 /*
1947 * Validate input.
1948 */
1949 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1950 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
1951
1952 /*
1953 * Take the lock and get the VM statistics.
1954 */
1955 PGVMM pGVMM;
1956 if (pVM)
1957 {
1958 PGVM pGVM;
1959 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
1960 if (RT_FAILURE(rc))
1961 return rc;
1962# define MAYBE_RESET_FIELD(field) \
1963 do { if (pStats->SchedVM. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
1964 MAYBE_RESET_FIELD(cHaltCalls);
1965 MAYBE_RESET_FIELD(cHaltBlocking);
1966 MAYBE_RESET_FIELD(cHaltTimeouts);
1967 MAYBE_RESET_FIELD(cHaltNotBlocking);
1968 MAYBE_RESET_FIELD(cHaltWakeUps);
1969 MAYBE_RESET_FIELD(cWakeUpCalls);
1970 MAYBE_RESET_FIELD(cWakeUpNotHalted);
1971 MAYBE_RESET_FIELD(cWakeUpWakeUps);
1972 MAYBE_RESET_FIELD(cPollCalls);
1973 MAYBE_RESET_FIELD(cPollHalts);
1974 MAYBE_RESET_FIELD(cPollWakeUps);
1975# undef MAYBE_RESET_FIELD
1976 }
1977 else
1978 {
1979 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1980
1981 int rc = gvmmR0UsedLock(pGVMM);
1982 AssertRCReturn(rc, rc);
1983 }
1984
1985 /*
1986 * Enumerate the VMs and add the ones visibile to the statistics.
1987 */
1988 if (ASMMemIsAll8(&pStats->SchedSum, sizeof(pStats->SchedSum), 0))
1989 {
1990 for (unsigned i = pGVMM->iUsedHead;
1991 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1992 i = pGVMM->aHandles[i].iNext)
1993 {
1994 PGVM pGVM = pGVMM->aHandles[i].pGVM;
1995 void *pvObj = pGVMM->aHandles[i].pvObj;
1996 if ( VALID_PTR(pvObj)
1997 && VALID_PTR(pGVM)
1998 && pGVM->u32Magic == GVM_MAGIC
1999 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
2000 {
2001# define MAYBE_RESET_FIELD(field) \
2002 do { if (pStats->SchedSum. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
2003 MAYBE_RESET_FIELD(cHaltCalls);
2004 MAYBE_RESET_FIELD(cHaltBlocking);
2005 MAYBE_RESET_FIELD(cHaltTimeouts);
2006 MAYBE_RESET_FIELD(cHaltNotBlocking);
2007 MAYBE_RESET_FIELD(cHaltWakeUps);
2008 MAYBE_RESET_FIELD(cWakeUpCalls);
2009 MAYBE_RESET_FIELD(cWakeUpNotHalted);
2010 MAYBE_RESET_FIELD(cWakeUpWakeUps);
2011 MAYBE_RESET_FIELD(cPollCalls);
2012 MAYBE_RESET_FIELD(cPollHalts);
2013 MAYBE_RESET_FIELD(cPollWakeUps);
2014# undef MAYBE_RESET_FIELD
2015 }
2016 }
2017 }
2018
2019 gvmmR0UsedUnlock(pGVMM);
2020
2021 return VINF_SUCCESS;
2022}
2023
2024
2025/**
2026 * VMMR0 request wrapper for GVMMR0ResetStatistics.
2027 *
2028 * @returns see GVMMR0ResetStatistics.
2029 * @param pVM Pointer to the shared VM structure. Optional.
2030 * @param pReq The request packet.
2031 */
2032GVMMR0DECL(int) GVMMR0ResetStatisticsReq(PVM pVM, PGVMMRESETSTATISTICSSREQ pReq)
2033{
2034 /*
2035 * Validate input and pass it on.
2036 */
2037 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
2038 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
2039
2040 return GVMMR0ResetStatistics(&pReq->Stats, pReq->pSession, pVM);
2041}
2042
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