VirtualBox

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

Last change on this file since 48366 was 48208, checked in by vboxsync, 11 years ago

VMM: Cleanup, hungarian and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 87.0 KB
Line 
1/* $Id: GVMMR0.cpp 48208 2013-08-30 22:05:31Z vboxsync $ */
2/** @file
3 * GVMM - Global VM Manager.
4 */
5
6/*
7 * Copyright (C) 2007-2012 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. Its main function at the moment is
22 * to manage a list of all running VMs, keep a ring-0 only structure (GVM) for
23 * each of them, and assign them unique identifiers (so GMM can track page
24 * owners). The GVMM also manage some of the host CPU resources, like the
25 * periodic preemption timer.
26 *
27 * The GVMM will create a ring-0 object for each VM when it is registered, this
28 * is both for session cleanup purposes and for having a point where it is
29 * possible to implement usage polices later (in SUPR0ObjRegister).
30 *
31 *
32 * @section sec_gvmm_ppt Periodic Preemption Timer (PPT)
33 *
34 * On system that sports a high resolution kernel timer API, we use per-cpu
35 * timers to generate interrupts that preempts VT-x, AMD-V and raw-mode guest
36 * execution. The timer frequency is calculating by taking the max
37 * TMCalcHostTimerFrequency for all VMs running on a CPU for the last ~160 ms
38 * (RT_ELEMENTS((PGVMMHOSTCPU)0, Ppt.aHzHistory) *
39 * GVMMHOSTCPU_PPT_HIST_INTERVAL_NS).
40 *
41 * The TMCalcHostTimerFrequency() part of the things gets its takes the max
42 * TMTimerSetFrequencyHint() value and adjusts by the current catch-up percent,
43 * warp drive percent and some fudge factors. VMMR0.cpp reports the result via
44 * GVMMR0SchedUpdatePeriodicPreemptionTimer() before switching to the VT-x,
45 * AMD-V and raw-mode execution environments.
46 */
47
48
49/*******************************************************************************
50* Header Files *
51*******************************************************************************/
52#define LOG_GROUP LOG_GROUP_GVMM
53#include <VBox/vmm/gvmm.h>
54#include <VBox/vmm/gmm.h>
55#include "GVMMR0Internal.h"
56#include <VBox/vmm/gvm.h>
57#include <VBox/vmm/vm.h>
58#include <VBox/vmm/vmcpuset.h>
59#include <VBox/vmm/vmm.h>
60#include <VBox/param.h>
61#include <VBox/err.h>
62
63#include <iprt/asm.h>
64#include <iprt/asm-amd64-x86.h>
65#include <iprt/mem.h>
66#include <iprt/semaphore.h>
67#include <iprt/time.h>
68#include <VBox/log.h>
69#include <iprt/thread.h>
70#include <iprt/process.h>
71#include <iprt/param.h>
72#include <iprt/string.h>
73#include <iprt/assert.h>
74#include <iprt/mem.h>
75#include <iprt/memobj.h>
76#include <iprt/mp.h>
77#include <iprt/cpuset.h>
78#include <iprt/spinlock.h>
79#include <iprt/timer.h>
80
81#include "dtrace/VBoxVMM.h"
82
83
84/*******************************************************************************
85* Defined Constants And Macros *
86*******************************************************************************/
87#if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
88/** Define this to enable the periodic preemption timer. */
89# define GVMM_SCHED_WITH_PPT
90#endif
91
92
93/*******************************************************************************
94* Structures and Typedefs *
95*******************************************************************************/
96
97/**
98 * Global VM handle.
99 */
100typedef struct GVMHANDLE
101{
102 /** The index of the next handle in the list (free or used). (0 is nil.) */
103 uint16_t volatile iNext;
104 /** Our own index / handle value. */
105 uint16_t iSelf;
106 /** The process ID of the handle owner.
107 * This is used for access checks. */
108 RTPROCESS ProcId;
109 /** The pointer to the ring-0 only (aka global) VM structure. */
110 PGVM pGVM;
111 /** The ring-0 mapping of the shared VM instance data. */
112 PVM pVM;
113 /** The virtual machine object. */
114 void *pvObj;
115 /** The session this VM is associated with. */
116 PSUPDRVSESSION pSession;
117 /** The ring-0 handle of the EMT0 thread.
118 * This is used for ownership checks as well as looking up a VM handle by thread
119 * at times like assertions. */
120 RTNATIVETHREAD hEMT0;
121} GVMHANDLE;
122/** Pointer to a global VM handle. */
123typedef GVMHANDLE *PGVMHANDLE;
124
125/** Number of GVM handles (including the NIL handle). */
126#if HC_ARCH_BITS == 64
127# define GVMM_MAX_HANDLES 8192
128#else
129# define GVMM_MAX_HANDLES 128
130#endif
131
132/**
133 * Per host CPU GVMM data.
134 */
135typedef struct GVMMHOSTCPU
136{
137 /** Magic number (GVMMHOSTCPU_MAGIC). */
138 uint32_t volatile u32Magic;
139 /** The CPU ID. */
140 RTCPUID idCpu;
141 /** The CPU set index. */
142 uint32_t idxCpuSet;
143
144#ifdef GVMM_SCHED_WITH_PPT
145 /** Periodic preemption timer data. */
146 struct
147 {
148 /** The handle to the periodic preemption timer. */
149 PRTTIMER pTimer;
150 /** Spinlock protecting the data below. */
151 RTSPINLOCK hSpinlock;
152 /** The smalles Hz that we need to care about. (static) */
153 uint32_t uMinHz;
154 /** The number of ticks between each historization. */
155 uint32_t cTicksHistoriziationInterval;
156 /** The current historization tick (counting up to
157 * cTicksHistoriziationInterval and then resetting). */
158 uint32_t iTickHistorization;
159 /** The current timer interval. This is set to 0 when inactive. */
160 uint32_t cNsInterval;
161 /** The current timer frequency. This is set to 0 when inactive. */
162 uint32_t uTimerHz;
163 /** The current max frequency reported by the EMTs.
164 * This gets historicize and reset by the timer callback. This is
165 * read without holding the spinlock, so needs atomic updating. */
166 uint32_t volatile uDesiredHz;
167 /** Whether the timer was started or not. */
168 bool volatile fStarted;
169 /** Set if we're starting timer. */
170 bool volatile fStarting;
171 /** The index of the next history entry (mod it). */
172 uint32_t iHzHistory;
173 /** Historicized uDesiredHz values. The array wraps around, new entries
174 * are added at iHzHistory. This is updated approximately every
175 * GVMMHOSTCPU_PPT_HIST_INTERVAL_NS by the timer callback. */
176 uint32_t aHzHistory[8];
177 /** Statistics counter for recording the number of interval changes. */
178 uint32_t cChanges;
179 /** Statistics counter for recording the number of timer starts. */
180 uint32_t cStarts;
181 } Ppt;
182#endif /* GVMM_SCHED_WITH_PPT */
183
184} GVMMHOSTCPU;
185/** Pointer to the per host CPU GVMM data. */
186typedef GVMMHOSTCPU *PGVMMHOSTCPU;
187/** The GVMMHOSTCPU::u32Magic value (Petra, Tanya & Rachel Haden). */
188#define GVMMHOSTCPU_MAGIC UINT32_C(0x19711011)
189/** The interval on history entry should cover (approximately) give in
190 * nanoseconds. */
191#define GVMMHOSTCPU_PPT_HIST_INTERVAL_NS UINT32_C(20000000)
192
193
194/**
195 * The GVMM instance data.
196 */
197typedef struct GVMM
198{
199 /** Eyecatcher / magic. */
200 uint32_t u32Magic;
201 /** The index of the head of the free handle chain. (0 is nil.) */
202 uint16_t volatile iFreeHead;
203 /** The index of the head of the active handle chain. (0 is nil.) */
204 uint16_t volatile iUsedHead;
205 /** The number of VMs. */
206 uint16_t volatile cVMs;
207 /** Alignment padding. */
208 uint16_t u16Reserved;
209 /** The number of EMTs. */
210 uint32_t volatile cEMTs;
211 /** The number of EMTs that have halted in GVMMR0SchedHalt. */
212 uint32_t volatile cHaltedEMTs;
213 /** Alignment padding. */
214 uint32_t u32Alignment;
215 /** When the next halted or sleeping EMT will wake up.
216 * This is set to 0 when it needs recalculating and to UINT64_MAX when
217 * there are no halted or sleeping EMTs in the GVMM. */
218 uint64_t uNsNextEmtWakeup;
219 /** The lock used to serialize VM creation, destruction and associated events that
220 * isn't performance critical. Owners may acquire the list lock. */
221 RTSEMFASTMUTEX CreateDestroyLock;
222 /** The lock used to serialize used list updates and accesses.
223 * This indirectly includes scheduling since the scheduler will have to walk the
224 * used list to examin running VMs. Owners may not acquire any other locks. */
225 RTSEMFASTMUTEX UsedLock;
226 /** The handle array.
227 * The size of this array defines the maximum number of currently running VMs.
228 * The first entry is unused as it represents the NIL handle. */
229 GVMHANDLE aHandles[GVMM_MAX_HANDLES];
230
231 /** @gcfgm{/GVMM/cEMTsMeansCompany, 32-bit, 0, UINT32_MAX, 1}
232 * The number of EMTs that means we no longer consider ourselves alone on a
233 * CPU/Core.
234 */
235 uint32_t cEMTsMeansCompany;
236 /** @gcfgm{/GVMM/MinSleepAlone,32-bit, 0, 100000000, 750000, ns}
237 * The minimum sleep time for when we're alone, in nano seconds.
238 */
239 uint32_t nsMinSleepAlone;
240 /** @gcfgm{/GVMM/MinSleepCompany,32-bit,0, 100000000, 15000, ns}
241 * The minimum sleep time for when we've got company, in nano seconds.
242 */
243 uint32_t nsMinSleepCompany;
244 /** @gcfgm{/GVMM/EarlyWakeUp1, 32-bit, 0, 100000000, 25000, ns}
245 * The limit for the first round of early wakeups, given in nano seconds.
246 */
247 uint32_t nsEarlyWakeUp1;
248 /** @gcfgm{/GVMM/EarlyWakeUp2, 32-bit, 0, 100000000, 50000, ns}
249 * The limit for the second round of early wakeups, given in nano seconds.
250 */
251 uint32_t nsEarlyWakeUp2;
252
253 /** The number of entries in the host CPU array (aHostCpus). */
254 uint32_t cHostCpus;
255 /** Per host CPU data (variable length). */
256 GVMMHOSTCPU aHostCpus[1];
257} GVMM;
258/** Pointer to the GVMM instance data. */
259typedef GVMM *PGVMM;
260
261/** The GVMM::u32Magic value (Charlie Haden). */
262#define GVMM_MAGIC UINT32_C(0x19370806)
263
264
265
266/*******************************************************************************
267* Global Variables *
268*******************************************************************************/
269/** Pointer to the GVMM instance data.
270 * (Just my general dislike for global variables.) */
271static PGVMM g_pGVMM = NULL;
272
273/** Macro for obtaining and validating the g_pGVMM pointer.
274 * On failure it will return from the invoking function with the specified return value.
275 *
276 * @param pGVMM The name of the pGVMM variable.
277 * @param rc The return value on failure. Use VERR_GVMM_INSTANCE for VBox
278 * status codes.
279 */
280#define GVMM_GET_VALID_INSTANCE(pGVMM, rc) \
281 do { \
282 (pGVMM) = g_pGVMM;\
283 AssertPtrReturn((pGVMM), (rc)); \
284 AssertMsgReturn((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic), (rc)); \
285 } while (0)
286
287/** Macro for obtaining and validating the g_pGVMM pointer, void function variant.
288 * On failure it will return from the invoking function.
289 *
290 * @param pGVMM The name of the pGVMM variable.
291 */
292#define GVMM_GET_VALID_INSTANCE_VOID(pGVMM) \
293 do { \
294 (pGVMM) = g_pGVMM;\
295 AssertPtrReturnVoid((pGVMM)); \
296 AssertMsgReturnVoid((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic)); \
297 } while (0)
298
299
300/*******************************************************************************
301* Internal Functions *
302*******************************************************************************/
303static void gvmmR0InitPerVMData(PGVM pGVM);
304static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle);
305static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock);
306static int gvmmR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM, PGVMM *ppGVMM);
307#ifdef GVMM_SCHED_WITH_PPT
308static DECLCALLBACK(void) gvmmR0SchedPeriodicPreemptionTimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
309#endif
310
311
312/**
313 * Initializes the GVMM.
314 *
315 * This is called while owning the loader semaphore (see supdrvIOCtl_LdrLoad()).
316 *
317 * @returns VBox status code.
318 */
319GVMMR0DECL(int) GVMMR0Init(void)
320{
321 LogFlow(("GVMMR0Init:\n"));
322
323 /*
324 * Allocate and initialize the instance data.
325 */
326 uint32_t cHostCpus = RTMpGetArraySize();
327 AssertMsgReturn(cHostCpus > 0 && cHostCpus < _64K, ("%d", (int)cHostCpus), VERR_GVMM_HOST_CPU_RANGE);
328
329 PGVMM pGVMM = (PGVMM)RTMemAllocZ(RT_UOFFSETOF(GVMM, aHostCpus[cHostCpus]));
330 if (!pGVMM)
331 return VERR_NO_MEMORY;
332 int rc = RTSemFastMutexCreate(&pGVMM->CreateDestroyLock);
333 if (RT_SUCCESS(rc))
334 {
335 rc = RTSemFastMutexCreate(&pGVMM->UsedLock);
336 if (RT_SUCCESS(rc))
337 {
338 pGVMM->u32Magic = GVMM_MAGIC;
339 pGVMM->iUsedHead = 0;
340 pGVMM->iFreeHead = 1;
341
342 /* the nil handle */
343 pGVMM->aHandles[0].iSelf = 0;
344 pGVMM->aHandles[0].iNext = 0;
345
346 /* the tail */
347 unsigned i = RT_ELEMENTS(pGVMM->aHandles) - 1;
348 pGVMM->aHandles[i].iSelf = i;
349 pGVMM->aHandles[i].iNext = 0; /* nil */
350
351 /* the rest */
352 while (i-- > 1)
353 {
354 pGVMM->aHandles[i].iSelf = i;
355 pGVMM->aHandles[i].iNext = i + 1;
356 }
357
358 /* The default configuration values. */
359 uint32_t cNsResolution = RTSemEventMultiGetResolution();
360 pGVMM->cEMTsMeansCompany = 1; /** @todo should be adjusted to relative to the cpu count or something... */
361 if (cNsResolution >= 5*RT_NS_100US)
362 {
363 pGVMM->nsMinSleepAlone = 750000 /* ns (0.750 ms) */; /** @todo this should be adjusted to be 75% (or something) of the scheduler granularity... */
364 pGVMM->nsMinSleepCompany = 15000 /* ns (0.015 ms) */;
365 pGVMM->nsEarlyWakeUp1 = 25000 /* ns (0.025 ms) */;
366 pGVMM->nsEarlyWakeUp2 = 50000 /* ns (0.050 ms) */;
367 }
368 else if (cNsResolution > RT_NS_100US)
369 {
370 pGVMM->nsMinSleepAlone = cNsResolution / 2;
371 pGVMM->nsMinSleepCompany = cNsResolution / 4;
372 pGVMM->nsEarlyWakeUp1 = 0;
373 pGVMM->nsEarlyWakeUp2 = 0;
374 }
375 else
376 {
377 pGVMM->nsMinSleepAlone = 2000;
378 pGVMM->nsMinSleepCompany = 2000;
379 pGVMM->nsEarlyWakeUp1 = 0;
380 pGVMM->nsEarlyWakeUp2 = 0;
381 }
382
383 /* The host CPU data. */
384 pGVMM->cHostCpus = cHostCpus;
385 uint32_t iCpu = cHostCpus;
386 RTCPUSET PossibleSet;
387 RTMpGetSet(&PossibleSet);
388 while (iCpu-- > 0)
389 {
390 pGVMM->aHostCpus[iCpu].idxCpuSet = iCpu;
391#ifdef GVMM_SCHED_WITH_PPT
392 pGVMM->aHostCpus[iCpu].Ppt.pTimer = NULL;
393 pGVMM->aHostCpus[iCpu].Ppt.hSpinlock = NIL_RTSPINLOCK;
394 pGVMM->aHostCpus[iCpu].Ppt.uMinHz = 5; /** @todo Add some API which figures this one out. (not *that* important) */
395 pGVMM->aHostCpus[iCpu].Ppt.cTicksHistoriziationInterval = 1;
396 //pGVMM->aHostCpus[iCpu].Ppt.iTickHistorization = 0;
397 //pGVMM->aHostCpus[iCpu].Ppt.cNsInterval = 0;
398 //pGVMM->aHostCpus[iCpu].Ppt.uTimerHz = 0;
399 //pGVMM->aHostCpus[iCpu].Ppt.uDesiredHz = 0;
400 //pGVMM->aHostCpus[iCpu].Ppt.fStarted = false;
401 //pGVMM->aHostCpus[iCpu].Ppt.fStarting = false;
402 //pGVMM->aHostCpus[iCpu].Ppt.iHzHistory = 0;
403 //pGVMM->aHostCpus[iCpu].Ppt.aHzHistory = {0};
404#endif
405
406 if (RTCpuSetIsMember(&PossibleSet, iCpu))
407 {
408 pGVMM->aHostCpus[iCpu].idCpu = RTMpCpuIdFromSetIndex(iCpu);
409 pGVMM->aHostCpus[iCpu].u32Magic = GVMMHOSTCPU_MAGIC;
410
411#ifdef GVMM_SCHED_WITH_PPT
412 rc = RTTimerCreateEx(&pGVMM->aHostCpus[iCpu].Ppt.pTimer,
413 50*1000*1000 /* whatever */,
414 RTTIMER_FLAGS_CPU(iCpu) | RTTIMER_FLAGS_HIGH_RES,
415 gvmmR0SchedPeriodicPreemptionTimerCallback,
416 &pGVMM->aHostCpus[iCpu]);
417 if (RT_SUCCESS(rc))
418 rc = RTSpinlockCreate(&pGVMM->aHostCpus[iCpu].Ppt.hSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "GVMM/CPU");
419 if (RT_FAILURE(rc))
420 {
421 while (iCpu < cHostCpus)
422 {
423 RTTimerDestroy(pGVMM->aHostCpus[iCpu].Ppt.pTimer);
424 RTSpinlockDestroy(pGVMM->aHostCpus[iCpu].Ppt.hSpinlock);
425 pGVMM->aHostCpus[iCpu].Ppt.hSpinlock = NIL_RTSPINLOCK;
426 iCpu++;
427 }
428 break;
429 }
430#endif
431 }
432 else
433 {
434 pGVMM->aHostCpus[iCpu].idCpu = NIL_RTCPUID;
435 pGVMM->aHostCpus[iCpu].u32Magic = 0;
436 }
437 }
438 if (RT_SUCCESS(rc))
439 {
440 g_pGVMM = pGVMM;
441 LogFlow(("GVMMR0Init: pGVMM=%p cHostCpus=%u\n", pGVMM, cHostCpus));
442 return VINF_SUCCESS;
443 }
444
445 /* bail out. */
446 RTSemFastMutexDestroy(pGVMM->UsedLock);
447 pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
448 }
449 RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
450 pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
451 }
452
453 RTMemFree(pGVMM);
454 return rc;
455}
456
457
458/**
459 * Terminates the GVM.
460 *
461 * This is called while owning the loader semaphore (see supdrvLdrFree()).
462 * And unless something is wrong, there should be absolutely no VMs
463 * registered at this point.
464 */
465GVMMR0DECL(void) GVMMR0Term(void)
466{
467 LogFlow(("GVMMR0Term:\n"));
468
469 PGVMM pGVMM = g_pGVMM;
470 g_pGVMM = NULL;
471 if (RT_UNLIKELY(!VALID_PTR(pGVMM)))
472 {
473 SUPR0Printf("GVMMR0Term: pGVMM=%p\n", pGVMM);
474 return;
475 }
476
477 /*
478 * First of all, stop all active timers.
479 */
480 uint32_t cActiveTimers = 0;
481 uint32_t iCpu = pGVMM->cHostCpus;
482 while (iCpu-- > 0)
483 {
484 ASMAtomicWriteU32(&pGVMM->aHostCpus[iCpu].u32Magic, ~GVMMHOSTCPU_MAGIC);
485#ifdef GVMM_SCHED_WITH_PPT
486 if ( pGVMM->aHostCpus[iCpu].Ppt.pTimer != NULL
487 && RT_SUCCESS(RTTimerStop(pGVMM->aHostCpus[iCpu].Ppt.pTimer)))
488 cActiveTimers++;
489#endif
490 }
491 if (cActiveTimers)
492 RTThreadSleep(1); /* fudge */
493
494 /*
495 * Invalidate the and free resources.
496 */
497 pGVMM->u32Magic = ~GVMM_MAGIC;
498 RTSemFastMutexDestroy(pGVMM->UsedLock);
499 pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
500 RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
501 pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
502
503 pGVMM->iFreeHead = 0;
504 if (pGVMM->iUsedHead)
505 {
506 SUPR0Printf("GVMMR0Term: iUsedHead=%#x! (cVMs=%#x cEMTs=%#x)\n", pGVMM->iUsedHead, pGVMM->cVMs, pGVMM->cEMTs);
507 pGVMM->iUsedHead = 0;
508 }
509
510#ifdef GVMM_SCHED_WITH_PPT
511 iCpu = pGVMM->cHostCpus;
512 while (iCpu-- > 0)
513 {
514 RTTimerDestroy(pGVMM->aHostCpus[iCpu].Ppt.pTimer);
515 pGVMM->aHostCpus[iCpu].Ppt.pTimer = NULL;
516 RTSpinlockDestroy(pGVMM->aHostCpus[iCpu].Ppt.hSpinlock);
517 pGVMM->aHostCpus[iCpu].Ppt.hSpinlock = NIL_RTSPINLOCK;
518 }
519#endif
520
521 RTMemFree(pGVMM);
522}
523
524
525/**
526 * A quick hack for setting global config values.
527 *
528 * @returns VBox status code.
529 *
530 * @param pSession The session handle. Used for authentication.
531 * @param pszName The variable name.
532 * @param u64Value The new value.
533 */
534GVMMR0DECL(int) GVMMR0SetConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t u64Value)
535{
536 /*
537 * Validate input.
538 */
539 PGVMM pGVMM;
540 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
541 AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
542 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
543
544 /*
545 * String switch time!
546 */
547 if (strncmp(pszName, RT_STR_TUPLE("/GVMM/")))
548 return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
549 int rc = VINF_SUCCESS;
550 pszName += sizeof("/GVMM/") - 1;
551 if (!strcmp(pszName, "cEMTsMeansCompany"))
552 {
553 if (u64Value <= UINT32_MAX)
554 pGVMM->cEMTsMeansCompany = u64Value;
555 else
556 rc = VERR_OUT_OF_RANGE;
557 }
558 else if (!strcmp(pszName, "MinSleepAlone"))
559 {
560 if (u64Value <= RT_NS_100MS)
561 pGVMM->nsMinSleepAlone = u64Value;
562 else
563 rc = VERR_OUT_OF_RANGE;
564 }
565 else if (!strcmp(pszName, "MinSleepCompany"))
566 {
567 if (u64Value <= RT_NS_100MS)
568 pGVMM->nsMinSleepCompany = u64Value;
569 else
570 rc = VERR_OUT_OF_RANGE;
571 }
572 else if (!strcmp(pszName, "EarlyWakeUp1"))
573 {
574 if (u64Value <= RT_NS_100MS)
575 pGVMM->nsEarlyWakeUp1 = u64Value;
576 else
577 rc = VERR_OUT_OF_RANGE;
578 }
579 else if (!strcmp(pszName, "EarlyWakeUp2"))
580 {
581 if (u64Value <= RT_NS_100MS)
582 pGVMM->nsEarlyWakeUp2 = u64Value;
583 else
584 rc = VERR_OUT_OF_RANGE;
585 }
586 else
587 rc = VERR_CFGM_VALUE_NOT_FOUND;
588 return rc;
589}
590
591
592/**
593 * A quick hack for getting global config values.
594 *
595 * @returns VBox status code.
596 *
597 * @param pSession The session handle. Used for authentication.
598 * @param pszName The variable name.
599 * @param u64Value The new value.
600 */
601GVMMR0DECL(int) GVMMR0QueryConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t *pu64Value)
602{
603 /*
604 * Validate input.
605 */
606 PGVMM pGVMM;
607 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
608 AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
609 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
610 AssertPtrReturn(pu64Value, VERR_INVALID_POINTER);
611
612 /*
613 * String switch time!
614 */
615 if (strncmp(pszName, RT_STR_TUPLE("/GVMM/")))
616 return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
617 int rc = VINF_SUCCESS;
618 pszName += sizeof("/GVMM/") - 1;
619 if (!strcmp(pszName, "cEMTsMeansCompany"))
620 *pu64Value = pGVMM->cEMTsMeansCompany;
621 else if (!strcmp(pszName, "MinSleepAlone"))
622 *pu64Value = pGVMM->nsMinSleepAlone;
623 else if (!strcmp(pszName, "MinSleepCompany"))
624 *pu64Value = pGVMM->nsMinSleepCompany;
625 else if (!strcmp(pszName, "EarlyWakeUp1"))
626 *pu64Value = pGVMM->nsEarlyWakeUp1;
627 else if (!strcmp(pszName, "EarlyWakeUp2"))
628 *pu64Value = pGVMM->nsEarlyWakeUp2;
629 else
630 rc = VERR_CFGM_VALUE_NOT_FOUND;
631 return rc;
632}
633
634
635/**
636 * Try acquire the 'used' lock.
637 *
638 * @returns IPRT status code, see RTSemFastMutexRequest.
639 * @param pGVMM The GVMM instance data.
640 */
641DECLINLINE(int) gvmmR0UsedLock(PGVMM pGVMM)
642{
643 LogFlow(("++gvmmR0UsedLock(%p)\n", pGVMM));
644 int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
645 LogFlow(("gvmmR0UsedLock(%p)->%Rrc\n", pGVMM, rc));
646 return rc;
647}
648
649
650/**
651 * Release the 'used' lock.
652 *
653 * @returns IPRT status code, see RTSemFastMutexRelease.
654 * @param pGVMM The GVMM instance data.
655 */
656DECLINLINE(int) gvmmR0UsedUnlock(PGVMM pGVMM)
657{
658 LogFlow(("--gvmmR0UsedUnlock(%p)\n", pGVMM));
659 int rc = RTSemFastMutexRelease(pGVMM->UsedLock);
660 AssertRC(rc);
661 return rc;
662}
663
664
665/**
666 * Try acquire the 'create & destroy' lock.
667 *
668 * @returns IPRT status code, see RTSemFastMutexRequest.
669 * @param pGVMM The GVMM instance data.
670 */
671DECLINLINE(int) gvmmR0CreateDestroyLock(PGVMM pGVMM)
672{
673 LogFlow(("++gvmmR0CreateDestroyLock(%p)\n", pGVMM));
674 int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
675 LogFlow(("gvmmR0CreateDestroyLock(%p)->%Rrc\n", pGVMM, rc));
676 return rc;
677}
678
679
680/**
681 * Release the 'create & destroy' lock.
682 *
683 * @returns IPRT status code, see RTSemFastMutexRequest.
684 * @param pGVMM The GVMM instance data.
685 */
686DECLINLINE(int) gvmmR0CreateDestroyUnlock(PGVMM pGVMM)
687{
688 LogFlow(("--gvmmR0CreateDestroyUnlock(%p)\n", pGVMM));
689 int rc = RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
690 AssertRC(rc);
691 return rc;
692}
693
694
695/**
696 * Request wrapper for the GVMMR0CreateVM API.
697 *
698 * @returns VBox status code.
699 * @param pReq The request buffer.
700 */
701GVMMR0DECL(int) GVMMR0CreateVMReq(PGVMMCREATEVMREQ pReq)
702{
703 /*
704 * Validate the request.
705 */
706 if (!VALID_PTR(pReq))
707 return VERR_INVALID_POINTER;
708 if (pReq->Hdr.cbReq != sizeof(*pReq))
709 return VERR_INVALID_PARAMETER;
710 if (!VALID_PTR(pReq->pSession))
711 return VERR_INVALID_POINTER;
712
713 /*
714 * Execute it.
715 */
716 PVM pVM;
717 pReq->pVMR0 = NULL;
718 pReq->pVMR3 = NIL_RTR3PTR;
719 int rc = GVMMR0CreateVM(pReq->pSession, pReq->cCpus, &pVM);
720 if (RT_SUCCESS(rc))
721 {
722 pReq->pVMR0 = pVM;
723 pReq->pVMR3 = pVM->pVMR3;
724 }
725 return rc;
726}
727
728
729/**
730 * Allocates the VM structure and registers it with GVM.
731 *
732 * The caller will become the VM owner and there by the EMT.
733 *
734 * @returns VBox status code.
735 * @param pSession The support driver session.
736 * @param cCpus Number of virtual CPUs for the new VM.
737 * @param ppVM Where to store the pointer to the VM structure.
738 *
739 * @thread EMT.
740 */
741GVMMR0DECL(int) GVMMR0CreateVM(PSUPDRVSESSION pSession, uint32_t cCpus, PVM *ppVM)
742{
743 LogFlow(("GVMMR0CreateVM: pSession=%p\n", pSession));
744 PGVMM pGVMM;
745 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
746
747 AssertPtrReturn(ppVM, VERR_INVALID_POINTER);
748 *ppVM = NULL;
749
750 if ( cCpus == 0
751 || cCpus > VMM_MAX_CPU_COUNT)
752 return VERR_INVALID_PARAMETER;
753
754 RTNATIVETHREAD hEMT0 = RTThreadNativeSelf();
755 AssertReturn(hEMT0 != NIL_RTNATIVETHREAD, VERR_GVMM_BROKEN_IPRT);
756 RTPROCESS ProcId = RTProcSelf();
757 AssertReturn(ProcId != NIL_RTPROCESS, VERR_GVMM_BROKEN_IPRT);
758
759 /*
760 * The whole allocation process is protected by the lock.
761 */
762 int rc = gvmmR0CreateDestroyLock(pGVMM);
763 AssertRCReturn(rc, rc);
764
765 /*
766 * Allocate a handle first so we don't waste resources unnecessarily.
767 */
768 uint16_t iHandle = pGVMM->iFreeHead;
769 if (iHandle)
770 {
771 PGVMHANDLE pHandle = &pGVMM->aHandles[iHandle];
772
773 /* consistency checks, a bit paranoid as always. */
774 if ( !pHandle->pVM
775 && !pHandle->pGVM
776 && !pHandle->pvObj
777 && pHandle->iSelf == iHandle)
778 {
779 pHandle->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_VM, gvmmR0HandleObjDestructor, pGVMM, pHandle);
780 if (pHandle->pvObj)
781 {
782 /*
783 * Move the handle from the free to used list and perform permission checks.
784 */
785 rc = gvmmR0UsedLock(pGVMM);
786 AssertRC(rc);
787
788 pGVMM->iFreeHead = pHandle->iNext;
789 pHandle->iNext = pGVMM->iUsedHead;
790 pGVMM->iUsedHead = iHandle;
791 pGVMM->cVMs++;
792
793 pHandle->pVM = NULL;
794 pHandle->pGVM = NULL;
795 pHandle->pSession = pSession;
796 pHandle->hEMT0 = NIL_RTNATIVETHREAD;
797 pHandle->ProcId = NIL_RTPROCESS;
798
799 gvmmR0UsedUnlock(pGVMM);
800
801 rc = SUPR0ObjVerifyAccess(pHandle->pvObj, pSession, NULL);
802 if (RT_SUCCESS(rc))
803 {
804 /*
805 * Allocate the global VM structure (GVM) and initialize it.
806 */
807 PGVM pGVM = (PGVM)RTMemAllocZ(RT_UOFFSETOF(GVM, aCpus[cCpus]));
808 if (pGVM)
809 {
810 pGVM->u32Magic = GVM_MAGIC;
811 pGVM->hSelf = iHandle;
812 pGVM->pVM = NULL;
813 pGVM->cCpus = cCpus;
814
815 gvmmR0InitPerVMData(pGVM);
816 GMMR0InitPerVMData(pGVM);
817
818 /*
819 * Allocate the shared VM structure and associated page array.
820 */
821 const uint32_t cbVM = RT_UOFFSETOF(VM, aCpus[cCpus]);
822 const uint32_t cPages = RT_ALIGN_32(cbVM, PAGE_SIZE) >> PAGE_SHIFT;
823 rc = RTR0MemObjAllocLow(&pGVM->gvmm.s.VMMemObj, cPages << PAGE_SHIFT, false /* fExecutable */);
824 if (RT_SUCCESS(rc))
825 {
826 PVM pVM = (PVM)RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj); AssertPtr(pVM);
827 memset(pVM, 0, cPages << PAGE_SHIFT);
828 pVM->enmVMState = VMSTATE_CREATING;
829 pVM->pVMR0 = pVM;
830 pVM->pSession = pSession;
831 pVM->hSelf = iHandle;
832 pVM->cbSelf = cbVM;
833 pVM->cCpus = cCpus;
834 pVM->uCpuExecutionCap = 100; /* default is no cap. */
835 pVM->offVMCPU = RT_UOFFSETOF(VM, aCpus);
836 AssertCompileMemberAlignment(VM, cpum, 64);
837 AssertCompileMemberAlignment(VM, tm, 64);
838 AssertCompileMemberAlignment(VM, aCpus, PAGE_SIZE);
839
840 rc = RTR0MemObjAllocPage(&pGVM->gvmm.s.VMPagesMemObj, cPages * sizeof(SUPPAGE), false /* fExecutable */);
841 if (RT_SUCCESS(rc))
842 {
843 PSUPPAGE paPages = (PSUPPAGE)RTR0MemObjAddress(pGVM->gvmm.s.VMPagesMemObj); AssertPtr(paPages);
844 for (uint32_t iPage = 0; iPage < cPages; iPage++)
845 {
846 paPages[iPage].uReserved = 0;
847 paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pGVM->gvmm.s.VMMemObj, iPage);
848 Assert(paPages[iPage].Phys != NIL_RTHCPHYS);
849 }
850
851 /*
852 * Map them into ring-3.
853 */
854 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMMapObj, pGVM->gvmm.s.VMMemObj, (RTR3PTR)-1, 0,
855 RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
856 if (RT_SUCCESS(rc))
857 {
858 pVM->pVMR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMMapObj);
859 AssertPtr((void *)pVM->pVMR3);
860
861 /* Initialize all the VM pointers. */
862 for (uint32_t i = 0; i < cCpus; i++)
863 {
864 pVM->aCpus[i].pVMR0 = pVM;
865 pVM->aCpus[i].pVMR3 = pVM->pVMR3;
866 pVM->aCpus[i].idHostCpu = NIL_RTCPUID;
867 pVM->aCpus[i].hNativeThreadR0 = NIL_RTNATIVETHREAD;
868 }
869
870 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMPagesMapObj, pGVM->gvmm.s.VMPagesMemObj, (RTR3PTR)-1,
871 0 /* uAlignment */, RTMEM_PROT_READ | RTMEM_PROT_WRITE,
872 NIL_RTR0PROCESS);
873 if (RT_SUCCESS(rc))
874 {
875 pVM->paVMPagesR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMPagesMapObj);
876 AssertPtr((void *)pVM->paVMPagesR3);
877
878 /* complete the handle - take the UsedLock sem just to be careful. */
879 rc = gvmmR0UsedLock(pGVMM);
880 AssertRC(rc);
881
882 pHandle->pVM = pVM;
883 pHandle->pGVM = pGVM;
884 pHandle->hEMT0 = hEMT0;
885 pHandle->ProcId = ProcId;
886 pGVM->pVM = pVM;
887 pGVM->aCpus[0].hEMT = hEMT0;
888 pVM->aCpus[0].hNativeThreadR0 = hEMT0;
889 pGVMM->cEMTs += cCpus;
890
891 rc = VMMR0ThreadCtxHooksCreate(&pVM->aCpus[0]);
892 if (RT_SUCCESS(rc))
893 {
894 VBOXVMM_R0_GVMM_VM_CREATED(pGVM, pVM, ProcId, (void *)hEMT0, cCpus);
895
896 gvmmR0UsedUnlock(pGVMM);
897 gvmmR0CreateDestroyUnlock(pGVMM);
898
899 *ppVM = pVM;
900 Log(("GVMMR0CreateVM: pVM=%p pVMR3=%p pGVM=%p hGVM=%d\n", pVM, pVM->pVMR3, pGVM, iHandle));
901 return VINF_SUCCESS;
902 }
903 }
904
905 RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */);
906 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
907 }
908 RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */);
909 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
910 }
911 RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */);
912 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
913 }
914 }
915 }
916 /* else: The user wasn't permitted to create this VM. */
917
918 /*
919 * The handle will be freed by gvmmR0HandleObjDestructor as we release the
920 * object reference here. A little extra mess because of non-recursive lock.
921 */
922 void *pvObj = pHandle->pvObj;
923 pHandle->pvObj = NULL;
924 gvmmR0CreateDestroyUnlock(pGVMM);
925
926 SUPR0ObjRelease(pvObj, pSession);
927
928 SUPR0Printf("GVMMR0CreateVM: failed, rc=%d\n", rc);
929 return rc;
930 }
931
932 rc = VERR_NO_MEMORY;
933 }
934 else
935 rc = VERR_GVMM_IPE_1;
936 }
937 else
938 rc = VERR_GVM_TOO_MANY_VMS;
939
940 gvmmR0CreateDestroyUnlock(pGVMM);
941 return rc;
942}
943
944
945/**
946 * Initializes the per VM data belonging to GVMM.
947 *
948 * @param pGVM Pointer to the global VM structure.
949 */
950static void gvmmR0InitPerVMData(PGVM pGVM)
951{
952 AssertCompile(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
953 AssertCompile(RT_SIZEOFMEMB(GVMCPU,gvmm.s) <= RT_SIZEOFMEMB(GVMCPU,gvmm.padding));
954 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
955 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
956 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
957 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
958 pGVM->gvmm.s.fDoneVMMR0Init = false;
959 pGVM->gvmm.s.fDoneVMMR0Term = false;
960
961 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
962 {
963 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
964 pGVM->aCpus[i].hEMT = NIL_RTNATIVETHREAD;
965 }
966}
967
968
969/**
970 * Does the VM initialization.
971 *
972 * @returns VBox status code.
973 * @param pVM Pointer to the VM.
974 */
975GVMMR0DECL(int) GVMMR0InitVM(PVM pVM)
976{
977 LogFlow(("GVMMR0InitVM: pVM=%p\n", pVM));
978
979 /*
980 * Validate the VM structure, state and handle.
981 */
982 PGVM pGVM;
983 PGVMM pGVMM;
984 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
985 if (RT_SUCCESS(rc))
986 {
987 if ( !pGVM->gvmm.s.fDoneVMMR0Init
988 && pGVM->aCpus[0].gvmm.s.HaltEventMulti == NIL_RTSEMEVENTMULTI)
989 {
990 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
991 {
992 rc = RTSemEventMultiCreate(&pGVM->aCpus[i].gvmm.s.HaltEventMulti);
993 if (RT_FAILURE(rc))
994 {
995 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
996 break;
997 }
998 }
999 }
1000 else
1001 rc = VERR_WRONG_ORDER;
1002 }
1003
1004 LogFlow(("GVMMR0InitVM: returns %Rrc\n", rc));
1005 return rc;
1006}
1007
1008
1009/**
1010 * Indicates that we're done with the ring-0 initialization
1011 * of the VM.
1012 *
1013 * @param pVM Pointer to the VM.
1014 * @thread EMT(0)
1015 */
1016GVMMR0DECL(void) GVMMR0DoneInitVM(PVM pVM)
1017{
1018 /* Validate the VM structure, state and handle. */
1019 PGVM pGVM;
1020 PGVMM pGVMM;
1021 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
1022 AssertRCReturnVoid(rc);
1023
1024 /* Set the indicator. */
1025 pGVM->gvmm.s.fDoneVMMR0Init = true;
1026}
1027
1028
1029/**
1030 * Indicates that we're doing the ring-0 termination of the VM.
1031 *
1032 * @returns true if termination hasn't been done already, false if it has.
1033 * @param pVM Pointer to the VM.
1034 * @param pGVM Pointer to the global VM structure. Optional.
1035 * @thread EMT(0)
1036 */
1037GVMMR0DECL(bool) GVMMR0DoingTermVM(PVM pVM, PGVM pGVM)
1038{
1039 /* Validate the VM structure, state and handle. */
1040 AssertPtrNullReturn(pGVM, false);
1041 AssertReturn(!pGVM || pGVM->u32Magic == GVM_MAGIC, false);
1042 if (!pGVM)
1043 {
1044 PGVMM pGVMM;
1045 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
1046 AssertRCReturn(rc, false);
1047 }
1048
1049 /* Set the indicator. */
1050 if (pGVM->gvmm.s.fDoneVMMR0Term)
1051 return false;
1052 pGVM->gvmm.s.fDoneVMMR0Term = true;
1053 return true;
1054}
1055
1056
1057/**
1058 * Destroys the VM, freeing all associated resources (the ring-0 ones anyway).
1059 *
1060 * This is call from the vmR3DestroyFinalBit and from a error path in VMR3Create,
1061 * and the caller is not the EMT thread, unfortunately. For security reasons, it
1062 * would've been nice if the caller was actually the EMT thread or that we somehow
1063 * could've associated the calling thread with the VM up front.
1064 *
1065 * @returns VBox status code.
1066 * @param pVM Pointer to the VM.
1067 *
1068 * @thread EMT(0) if it's associated with the VM, otherwise any thread.
1069 */
1070GVMMR0DECL(int) GVMMR0DestroyVM(PVM pVM)
1071{
1072 LogFlow(("GVMMR0DestroyVM: pVM=%p\n", pVM));
1073 PGVMM pGVMM;
1074 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
1075
1076 /*
1077 * Validate the VM structure, state and caller.
1078 */
1079 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1080 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1081 AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_TERMINATED, ("%d\n", pVM->enmVMState),
1082 VERR_WRONG_ORDER);
1083
1084 uint32_t hGVM = pVM->hSelf;
1085 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
1086 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
1087
1088 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1089 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
1090
1091 RTPROCESS ProcId = RTProcSelf();
1092 RTNATIVETHREAD hSelf = RTThreadNativeSelf();
1093 AssertReturn( ( pHandle->hEMT0 == hSelf
1094 && pHandle->ProcId == ProcId)
1095 || pHandle->hEMT0 == NIL_RTNATIVETHREAD, VERR_NOT_OWNER);
1096
1097 /*
1098 * Lookup the handle and destroy the object.
1099 * Since the lock isn't recursive and we'll have to leave it before dereferencing the
1100 * object, we take some precautions against racing callers just in case...
1101 */
1102 int rc = gvmmR0CreateDestroyLock(pGVMM);
1103 AssertRC(rc);
1104
1105 /* Be careful here because we might theoretically be racing someone else cleaning up. */
1106 if ( pHandle->pVM == pVM
1107 && ( ( pHandle->hEMT0 == hSelf
1108 && pHandle->ProcId == ProcId)
1109 || pHandle->hEMT0 == NIL_RTNATIVETHREAD)
1110 && VALID_PTR(pHandle->pvObj)
1111 && VALID_PTR(pHandle->pSession)
1112 && VALID_PTR(pHandle->pGVM)
1113 && pHandle->pGVM->u32Magic == GVM_MAGIC)
1114 {
1115 void *pvObj = pHandle->pvObj;
1116 pHandle->pvObj = NULL;
1117 gvmmR0CreateDestroyUnlock(pGVMM);
1118
1119 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1120 VMMR0ThreadCtxHooksRelease(&pVM->aCpus[idCpu]);
1121
1122 SUPR0ObjRelease(pvObj, pHandle->pSession);
1123 }
1124 else
1125 {
1126 SUPR0Printf("GVMMR0DestroyVM: pHandle=%p:{.pVM=%p, .hEMT0=%p, .ProcId=%u, .pvObj=%p} pVM=%p hSelf=%p\n",
1127 pHandle, pHandle->pVM, pHandle->hEMT0, pHandle->ProcId, pHandle->pvObj, pVM, hSelf);
1128 gvmmR0CreateDestroyUnlock(pGVMM);
1129 rc = VERR_GVMM_IPE_2;
1130 }
1131
1132 return rc;
1133}
1134
1135
1136/**
1137 * Performs VM cleanup task as part of object destruction.
1138 *
1139 * @param pGVM The GVM pointer.
1140 */
1141static void gvmmR0CleanupVM(PGVM pGVM)
1142{
1143 if ( pGVM->gvmm.s.fDoneVMMR0Init
1144 && !pGVM->gvmm.s.fDoneVMMR0Term)
1145 {
1146 if ( pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ
1147 && RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj) == pGVM->pVM)
1148 {
1149 LogFlow(("gvmmR0CleanupVM: Calling VMMR0TermVM\n"));
1150 VMMR0TermVM(pGVM->pVM, pGVM);
1151 }
1152 else
1153 AssertMsgFailed(("gvmmR0CleanupVM: VMMemObj=%p pVM=%p\n", pGVM->gvmm.s.VMMemObj, pGVM->pVM));
1154 }
1155
1156 GMMR0CleanupVM(pGVM);
1157}
1158
1159
1160/**
1161 * Handle destructor.
1162 *
1163 * @param pvGVMM The GVM instance pointer.
1164 * @param pvHandle The handle pointer.
1165 */
1166static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle)
1167{
1168 LogFlow(("gvmmR0HandleObjDestructor: %p %p %p\n", pvObj, pvGVMM, pvHandle));
1169
1170 /*
1171 * Some quick, paranoid, input validation.
1172 */
1173 PGVMHANDLE pHandle = (PGVMHANDLE)pvHandle;
1174 AssertPtr(pHandle);
1175 PGVMM pGVMM = (PGVMM)pvGVMM;
1176 Assert(pGVMM == g_pGVMM);
1177 const uint16_t iHandle = pHandle - &pGVMM->aHandles[0];
1178 if ( !iHandle
1179 || iHandle >= RT_ELEMENTS(pGVMM->aHandles)
1180 || iHandle != pHandle->iSelf)
1181 {
1182 SUPR0Printf("GVM: handle %d is out of range or corrupt (iSelf=%d)!\n", iHandle, pHandle->iSelf);
1183 return;
1184 }
1185
1186 int rc = gvmmR0CreateDestroyLock(pGVMM);
1187 AssertRC(rc);
1188 rc = gvmmR0UsedLock(pGVMM);
1189 AssertRC(rc);
1190
1191 /*
1192 * This is a tad slow but a doubly linked list is too much hassle.
1193 */
1194 if (RT_UNLIKELY(pHandle->iNext >= RT_ELEMENTS(pGVMM->aHandles)))
1195 {
1196 SUPR0Printf("GVM: used list index %d is out of range!\n", pHandle->iNext);
1197 gvmmR0UsedUnlock(pGVMM);
1198 gvmmR0CreateDestroyUnlock(pGVMM);
1199 return;
1200 }
1201
1202 if (pGVMM->iUsedHead == iHandle)
1203 pGVMM->iUsedHead = pHandle->iNext;
1204 else
1205 {
1206 uint16_t iPrev = pGVMM->iUsedHead;
1207 int c = RT_ELEMENTS(pGVMM->aHandles) + 2;
1208 while (iPrev)
1209 {
1210 if (RT_UNLIKELY(iPrev >= RT_ELEMENTS(pGVMM->aHandles)))
1211 {
1212 SUPR0Printf("GVM: used list index %d is out of range!\n", iPrev);
1213 gvmmR0UsedUnlock(pGVMM);
1214 gvmmR0CreateDestroyUnlock(pGVMM);
1215 return;
1216 }
1217 if (RT_UNLIKELY(c-- <= 0))
1218 {
1219 iPrev = 0;
1220 break;
1221 }
1222
1223 if (pGVMM->aHandles[iPrev].iNext == iHandle)
1224 break;
1225 iPrev = pGVMM->aHandles[iPrev].iNext;
1226 }
1227 if (!iPrev)
1228 {
1229 SUPR0Printf("GVM: can't find the handle previous previous of %d!\n", pHandle->iSelf);
1230 gvmmR0UsedUnlock(pGVMM);
1231 gvmmR0CreateDestroyUnlock(pGVMM);
1232 return;
1233 }
1234
1235 Assert(pGVMM->aHandles[iPrev].iNext == iHandle);
1236 pGVMM->aHandles[iPrev].iNext = pHandle->iNext;
1237 }
1238 pHandle->iNext = 0;
1239 pGVMM->cVMs--;
1240
1241 /*
1242 * Do the global cleanup round.
1243 */
1244 PGVM pGVM = pHandle->pGVM;
1245 if ( VALID_PTR(pGVM)
1246 && pGVM->u32Magic == GVM_MAGIC)
1247 {
1248 pGVMM->cEMTs -= pGVM->cCpus;
1249 gvmmR0UsedUnlock(pGVMM);
1250
1251 gvmmR0CleanupVM(pGVM);
1252
1253 /*
1254 * Do the GVMM cleanup - must be done last.
1255 */
1256 /* The VM and VM pages mappings/allocations. */
1257 if (pGVM->gvmm.s.VMPagesMapObj != NIL_RTR0MEMOBJ)
1258 {
1259 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMapObj, false /* fFreeMappings */); AssertRC(rc);
1260 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
1261 }
1262
1263 if (pGVM->gvmm.s.VMMapObj != NIL_RTR0MEMOBJ)
1264 {
1265 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */); AssertRC(rc);
1266 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
1267 }
1268
1269 if (pGVM->gvmm.s.VMPagesMemObj != NIL_RTR0MEMOBJ)
1270 {
1271 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */); AssertRC(rc);
1272 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
1273 }
1274
1275 if (pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ)
1276 {
1277 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */); AssertRC(rc);
1278 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
1279 }
1280
1281 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
1282 {
1283 if (pGVM->aCpus[i].gvmm.s.HaltEventMulti != NIL_RTSEMEVENTMULTI)
1284 {
1285 rc = RTSemEventMultiDestroy(pGVM->aCpus[i].gvmm.s.HaltEventMulti); AssertRC(rc);
1286 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
1287 }
1288 }
1289
1290 /* the GVM structure itself. */
1291 pGVM->u32Magic |= UINT32_C(0x80000000);
1292 RTMemFree(pGVM);
1293
1294 /* Re-acquire the UsedLock before freeing the handle since we're updating handle fields. */
1295 rc = gvmmR0UsedLock(pGVMM);
1296 AssertRC(rc);
1297 }
1298 /* else: GVMMR0CreateVM cleanup. */
1299
1300 /*
1301 * Free the handle.
1302 */
1303 pHandle->iNext = pGVMM->iFreeHead;
1304 pGVMM->iFreeHead = iHandle;
1305 ASMAtomicWriteNullPtr(&pHandle->pGVM);
1306 ASMAtomicWriteNullPtr(&pHandle->pVM);
1307 ASMAtomicWriteNullPtr(&pHandle->pvObj);
1308 ASMAtomicWriteNullPtr(&pHandle->pSession);
1309 ASMAtomicWriteHandle(&pHandle->hEMT0, NIL_RTNATIVETHREAD);
1310 ASMAtomicWriteU32(&pHandle->ProcId, NIL_RTPROCESS);
1311
1312 gvmmR0UsedUnlock(pGVMM);
1313 gvmmR0CreateDestroyUnlock(pGVMM);
1314 LogFlow(("gvmmR0HandleObjDestructor: returns\n"));
1315}
1316
1317
1318/**
1319 * Registers the calling thread as the EMT of a Virtual CPU.
1320 *
1321 * Note that VCPU 0 is automatically registered during VM creation.
1322 *
1323 * @returns VBox status code
1324 * @param pVM Pointer to the VM.
1325 * @param idCpu VCPU id.
1326 */
1327GVMMR0DECL(int) GVMMR0RegisterVCpu(PVM pVM, VMCPUID idCpu)
1328{
1329 AssertReturn(idCpu != 0, VERR_NOT_OWNER);
1330
1331 /*
1332 * Validate the VM structure, state and handle.
1333 */
1334 PGVM pGVM;
1335 PGVMM pGVMM;
1336 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, false /* fTakeUsedLock */);
1337 if (RT_FAILURE(rc))
1338 return rc;
1339
1340 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
1341 AssertReturn(pGVM->aCpus[idCpu].hEMT == NIL_RTNATIVETHREAD, VERR_ACCESS_DENIED);
1342 Assert(pGVM->cCpus == pVM->cCpus);
1343 Assert(pVM->aCpus[idCpu].hNativeThreadR0 == NIL_RTNATIVETHREAD);
1344
1345 pVM->aCpus[idCpu].hNativeThreadR0 = pGVM->aCpus[idCpu].hEMT = RTThreadNativeSelf();
1346
1347 rc = VMMR0ThreadCtxHooksCreate(&pVM->aCpus[idCpu]);
1348 return rc;
1349}
1350
1351
1352/**
1353 * Lookup a GVM structure by its handle.
1354 *
1355 * @returns The GVM pointer on success, NULL on failure.
1356 * @param hGVM The global VM handle. Asserts on bad handle.
1357 */
1358GVMMR0DECL(PGVM) GVMMR0ByHandle(uint32_t hGVM)
1359{
1360 PGVMM pGVMM;
1361 GVMM_GET_VALID_INSTANCE(pGVMM, NULL);
1362
1363 /*
1364 * Validate.
1365 */
1366 AssertReturn(hGVM != NIL_GVM_HANDLE, NULL);
1367 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), NULL);
1368
1369 /*
1370 * Look it up.
1371 */
1372 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1373 AssertPtrReturn(pHandle->pVM, NULL);
1374 AssertPtrReturn(pHandle->pvObj, NULL);
1375 PGVM pGVM = pHandle->pGVM;
1376 AssertPtrReturn(pGVM, NULL);
1377 AssertReturn(pGVM->pVM == pHandle->pVM, NULL);
1378
1379 return pHandle->pGVM;
1380}
1381
1382
1383/**
1384 * Lookup a GVM structure by the shared VM structure.
1385 *
1386 * The calling thread must be in the same process as the VM. All current lookups
1387 * are by threads inside the same process, so this will not be an issue.
1388 *
1389 * @returns VBox status code.
1390 * @param pVM Pointer to the VM.
1391 * @param ppGVM Where to store the GVM pointer.
1392 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1393 * @param fTakeUsedLock Whether to take the used lock or not.
1394 * Be very careful if not taking the lock as it's possible that
1395 * the VM will disappear then.
1396 *
1397 * @remark This will not assert on an invalid pVM but try return silently.
1398 */
1399static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock)
1400{
1401 RTPROCESS ProcId = RTProcSelf();
1402 PGVMM pGVMM;
1403 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
1404
1405 /*
1406 * Validate.
1407 */
1408 if (RT_UNLIKELY( !VALID_PTR(pVM)
1409 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
1410 return VERR_INVALID_POINTER;
1411 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
1412 || pVM->enmVMState >= VMSTATE_TERMINATED))
1413 return VERR_INVALID_POINTER;
1414
1415 uint16_t hGVM = pVM->hSelf;
1416 if (RT_UNLIKELY( hGVM == NIL_GVM_HANDLE
1417 || hGVM >= RT_ELEMENTS(pGVMM->aHandles)))
1418 return VERR_INVALID_HANDLE;
1419
1420 /*
1421 * Look it up.
1422 */
1423 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1424 PGVM pGVM;
1425 if (fTakeUsedLock)
1426 {
1427 int rc = gvmmR0UsedLock(pGVMM);
1428 AssertRCReturn(rc, rc);
1429
1430 pGVM = pHandle->pGVM;
1431 if (RT_UNLIKELY( pHandle->pVM != pVM
1432 || pHandle->ProcId != ProcId
1433 || !VALID_PTR(pHandle->pvObj)
1434 || !VALID_PTR(pGVM)
1435 || pGVM->pVM != pVM))
1436 {
1437 gvmmR0UsedUnlock(pGVMM);
1438 return VERR_INVALID_HANDLE;
1439 }
1440 }
1441 else
1442 {
1443 if (RT_UNLIKELY(pHandle->pVM != pVM))
1444 return VERR_INVALID_HANDLE;
1445 if (RT_UNLIKELY(pHandle->ProcId != ProcId))
1446 return VERR_INVALID_HANDLE;
1447 if (RT_UNLIKELY(!VALID_PTR(pHandle->pvObj)))
1448 return VERR_INVALID_HANDLE;
1449
1450 pGVM = pHandle->pGVM;
1451 if (RT_UNLIKELY(!VALID_PTR(pGVM)))
1452 return VERR_INVALID_HANDLE;
1453 if (RT_UNLIKELY(pGVM->pVM != pVM))
1454 return VERR_INVALID_HANDLE;
1455 }
1456
1457 *ppGVM = pGVM;
1458 *ppGVMM = pGVMM;
1459 return VINF_SUCCESS;
1460}
1461
1462
1463/**
1464 * Lookup a GVM structure by the shared VM structure.
1465 *
1466 * @returns VBox status code.
1467 * @param pVM Pointer to the VM.
1468 * @param ppGVM Where to store the GVM pointer.
1469 *
1470 * @remark This will not take the 'used'-lock because it doesn't do
1471 * nesting and this function will be used from under the lock.
1472 */
1473GVMMR0DECL(int) GVMMR0ByVM(PVM pVM, PGVM *ppGVM)
1474{
1475 PGVMM pGVMM;
1476 return gvmmR0ByVM(pVM, ppGVM, &pGVMM, false /* fTakeUsedLock */);
1477}
1478
1479
1480/**
1481 * Lookup a GVM structure by the shared VM structure and ensuring that the
1482 * caller is an EMT thread.
1483 *
1484 * @returns VBox status code.
1485 * @param pVM Pointer to the VM.
1486 * @param idCpu The Virtual CPU ID of the calling EMT.
1487 * @param ppGVM Where to store the GVM pointer.
1488 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1489 * @thread EMT
1490 *
1491 * @remark This will assert in all failure paths.
1492 */
1493static int gvmmR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM, PGVMM *ppGVMM)
1494{
1495 PGVMM pGVMM;
1496 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
1497
1498 /*
1499 * Validate.
1500 */
1501 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1502 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1503
1504 uint16_t hGVM = pVM->hSelf;
1505 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
1506 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
1507
1508 /*
1509 * Look it up.
1510 */
1511 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1512 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
1513 RTPROCESS ProcId = RTProcSelf();
1514 AssertReturn(pHandle->ProcId == ProcId, VERR_NOT_OWNER);
1515 AssertPtrReturn(pHandle->pvObj, VERR_NOT_OWNER);
1516
1517 PGVM pGVM = pHandle->pGVM;
1518 AssertPtrReturn(pGVM, VERR_NOT_OWNER);
1519 AssertReturn(pGVM->pVM == pVM, VERR_NOT_OWNER);
1520 RTNATIVETHREAD hAllegedEMT = RTThreadNativeSelf();
1521 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
1522 AssertReturn(pGVM->aCpus[idCpu].hEMT == hAllegedEMT, VERR_NOT_OWNER);
1523
1524 *ppGVM = pGVM;
1525 *ppGVMM = pGVMM;
1526 return VINF_SUCCESS;
1527}
1528
1529
1530/**
1531 * Lookup a GVM structure by the shared VM structure
1532 * and ensuring that the caller is the EMT thread.
1533 *
1534 * @returns VBox status code.
1535 * @param pVM Pointer to the VM.
1536 * @param idCpu The Virtual CPU ID of the calling EMT.
1537 * @param ppGVM Where to store the GVM pointer.
1538 * @thread EMT
1539 */
1540GVMMR0DECL(int) GVMMR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM)
1541{
1542 AssertPtrReturn(ppGVM, VERR_INVALID_POINTER);
1543 PGVMM pGVMM;
1544 return gvmmR0ByVMAndEMT(pVM, idCpu, ppGVM, &pGVMM);
1545}
1546
1547
1548/**
1549 * Lookup a VM by its global handle.
1550 *
1551 * @returns Pointer to the VM on success, NULL on failure.
1552 * @param hGVM The global VM handle. Asserts on bad handle.
1553 */
1554GVMMR0DECL(PVM) GVMMR0GetVMByHandle(uint32_t hGVM)
1555{
1556 PGVM pGVM = GVMMR0ByHandle(hGVM);
1557 return pGVM ? pGVM->pVM : NULL;
1558}
1559
1560
1561/**
1562 * Looks up the VM belonging to the specified EMT thread.
1563 *
1564 * This is used by the assertion machinery in VMMR0.cpp to avoid causing
1565 * unnecessary kernel panics when the EMT thread hits an assertion. The
1566 * call may or not be an EMT thread.
1567 *
1568 * @returns Pointer to the VM on success, NULL on failure.
1569 * @param hEMT The native thread handle of the EMT.
1570 * NIL_RTNATIVETHREAD means the current thread
1571 */
1572GVMMR0DECL(PVM) GVMMR0GetVMByEMT(RTNATIVETHREAD hEMT)
1573{
1574 /*
1575 * No Assertions here as we're usually called in a AssertMsgN or
1576 * RTAssert* context.
1577 */
1578 PGVMM pGVMM = g_pGVMM;
1579 if ( !VALID_PTR(pGVMM)
1580 || pGVMM->u32Magic != GVMM_MAGIC)
1581 return NULL;
1582
1583 if (hEMT == NIL_RTNATIVETHREAD)
1584 hEMT = RTThreadNativeSelf();
1585 RTPROCESS ProcId = RTProcSelf();
1586
1587 /*
1588 * Search the handles in a linear fashion as we don't dare to take the lock (assert).
1589 */
1590 for (unsigned i = 1; i < RT_ELEMENTS(pGVMM->aHandles); i++)
1591 {
1592 if ( pGVMM->aHandles[i].iSelf == i
1593 && pGVMM->aHandles[i].ProcId == ProcId
1594 && VALID_PTR(pGVMM->aHandles[i].pvObj)
1595 && VALID_PTR(pGVMM->aHandles[i].pVM)
1596 && VALID_PTR(pGVMM->aHandles[i].pGVM))
1597 {
1598 if (pGVMM->aHandles[i].hEMT0 == hEMT)
1599 return pGVMM->aHandles[i].pVM;
1600
1601 /* This is fearly safe with the current process per VM approach. */
1602 PGVM pGVM = pGVMM->aHandles[i].pGVM;
1603 VMCPUID const cCpus = pGVM->cCpus;
1604 if ( cCpus < 1
1605 || cCpus > VMM_MAX_CPU_COUNT)
1606 continue;
1607 for (VMCPUID idCpu = 1; idCpu < cCpus; idCpu++)
1608 if (pGVM->aCpus[idCpu].hEMT == hEMT)
1609 return pGVMM->aHandles[i].pVM;
1610 }
1611 }
1612 return NULL;
1613}
1614
1615
1616/**
1617 * This is will wake up expired and soon-to-be expired VMs.
1618 *
1619 * @returns Number of VMs that has been woken up.
1620 * @param pGVMM Pointer to the GVMM instance data.
1621 * @param u64Now The current time.
1622 */
1623static unsigned gvmmR0SchedDoWakeUps(PGVMM pGVMM, uint64_t u64Now)
1624{
1625 /*
1626 * Skip this if we've got disabled because of high resolution wakeups or by
1627 * the user.
1628 */
1629 if ( !pGVMM->nsEarlyWakeUp1
1630 && !pGVMM->nsEarlyWakeUp2)
1631 return 0;
1632
1633/** @todo Rewrite this algorithm. See performance defect XYZ. */
1634
1635 /*
1636 * A cheap optimization to stop wasting so much time here on big setups.
1637 */
1638 const uint64_t uNsEarlyWakeUp2 = u64Now + pGVMM->nsEarlyWakeUp2;
1639 if ( pGVMM->cHaltedEMTs == 0
1640 || uNsEarlyWakeUp2 > pGVMM->uNsNextEmtWakeup)
1641 return 0;
1642
1643 /*
1644 * The first pass will wake up VMs which have actually expired
1645 * and look for VMs that should be woken up in the 2nd and 3rd passes.
1646 */
1647 const uint64_t uNsEarlyWakeUp1 = u64Now + pGVMM->nsEarlyWakeUp1;
1648 uint64_t u64Min = UINT64_MAX;
1649 unsigned cWoken = 0;
1650 unsigned cHalted = 0;
1651 unsigned cTodo2nd = 0;
1652 unsigned cTodo3rd = 0;
1653 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1654 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1655 i = pGVMM->aHandles[i].iNext)
1656 {
1657 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1658 if ( VALID_PTR(pCurGVM)
1659 && pCurGVM->u32Magic == GVM_MAGIC)
1660 {
1661 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1662 {
1663 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1664 uint64_t u64 = ASMAtomicUoReadU64(&pCurGVCpu->gvmm.s.u64HaltExpire);
1665 if (u64)
1666 {
1667 if (u64 <= u64Now)
1668 {
1669 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1670 {
1671 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1672 AssertRC(rc);
1673 cWoken++;
1674 }
1675 }
1676 else
1677 {
1678 cHalted++;
1679 if (u64 <= uNsEarlyWakeUp1)
1680 cTodo2nd++;
1681 else if (u64 <= uNsEarlyWakeUp2)
1682 cTodo3rd++;
1683 else if (u64 < u64Min)
1684 u64 = u64Min;
1685 }
1686 }
1687 }
1688 }
1689 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1690 }
1691
1692 if (cTodo2nd)
1693 {
1694 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1695 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1696 i = pGVMM->aHandles[i].iNext)
1697 {
1698 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1699 if ( VALID_PTR(pCurGVM)
1700 && pCurGVM->u32Magic == GVM_MAGIC)
1701 {
1702 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1703 {
1704 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1705 uint64_t u64 = ASMAtomicUoReadU64(&pCurGVCpu->gvmm.s.u64HaltExpire);
1706 if ( u64
1707 && u64 <= uNsEarlyWakeUp1)
1708 {
1709 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1710 {
1711 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1712 AssertRC(rc);
1713 cWoken++;
1714 }
1715 }
1716 }
1717 }
1718 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1719 }
1720 }
1721
1722 if (cTodo3rd)
1723 {
1724 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1725 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1726 i = pGVMM->aHandles[i].iNext)
1727 {
1728 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1729 if ( VALID_PTR(pCurGVM)
1730 && pCurGVM->u32Magic == GVM_MAGIC)
1731 {
1732 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1733 {
1734 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1735 uint64_t u64 = ASMAtomicUoReadU64(&pCurGVCpu->gvmm.s.u64HaltExpire);
1736 if ( u64
1737 && u64 <= uNsEarlyWakeUp2)
1738 {
1739 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1740 {
1741 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1742 AssertRC(rc);
1743 cWoken++;
1744 }
1745 }
1746 }
1747 }
1748 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1749 }
1750 }
1751
1752 /*
1753 * Set the minimum value.
1754 */
1755 pGVMM->uNsNextEmtWakeup = u64Min;
1756
1757 return cWoken;
1758}
1759
1760
1761/**
1762 * Halt the EMT thread.
1763 *
1764 * @returns VINF_SUCCESS normal wakeup (timeout or kicked by other thread).
1765 * VERR_INTERRUPTED if a signal was scheduled for the thread.
1766 * @param pVM Pointer to the VM.
1767 * @param idCpu The Virtual CPU ID of the calling EMT.
1768 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
1769 * @thread EMT(idCpu).
1770 */
1771GVMMR0DECL(int) GVMMR0SchedHalt(PVM pVM, VMCPUID idCpu, uint64_t u64ExpireGipTime)
1772{
1773 LogFlow(("GVMMR0SchedHalt: pVM=%p\n", pVM));
1774
1775 /*
1776 * Validate the VM structure, state and handle.
1777 */
1778 PGVM pGVM;
1779 PGVMM pGVMM;
1780 int rc = gvmmR0ByVMAndEMT(pVM, idCpu, &pGVM, &pGVMM);
1781 if (RT_FAILURE(rc))
1782 return rc;
1783 pGVM->gvmm.s.StatsSched.cHaltCalls++;
1784
1785 PGVMCPU pCurGVCpu = &pGVM->aCpus[idCpu];
1786 Assert(!pCurGVCpu->gvmm.s.u64HaltExpire);
1787
1788 /*
1789 * Take the UsedList semaphore, get the current time
1790 * and check if anyone needs waking up.
1791 * Interrupts must NOT be disabled at this point because we ask for GIP time!
1792 */
1793 rc = gvmmR0UsedLock(pGVMM);
1794 AssertRC(rc);
1795
1796 pCurGVCpu->gvmm.s.iCpuEmt = ASMGetApicId();
1797
1798 /* GIP hack: We might are frequently sleeping for short intervals where the
1799 difference between GIP and system time matters on systems with high resolution
1800 system time. So, convert the input from GIP to System time in that case. */
1801 Assert(ASMGetFlags() & X86_EFL_IF);
1802 const uint64_t u64NowSys = RTTimeSystemNanoTS();
1803 const uint64_t u64NowGip = RTTimeNanoTS();
1804 pGVM->gvmm.s.StatsSched.cHaltWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64NowGip);
1805
1806 /*
1807 * Go to sleep if we must...
1808 * Cap the sleep time to 1 second to be on the safe side.
1809 */
1810 uint64_t cNsInterval = u64ExpireGipTime - u64NowGip;
1811 if ( u64NowGip < u64ExpireGipTime
1812 && cNsInterval >= (pGVMM->cEMTs > pGVMM->cEMTsMeansCompany
1813 ? pGVMM->nsMinSleepCompany
1814 : pGVMM->nsMinSleepAlone))
1815 {
1816 pGVM->gvmm.s.StatsSched.cHaltBlocking++;
1817 if (cNsInterval > RT_NS_1SEC)
1818 u64ExpireGipTime = u64NowGip + RT_NS_1SEC;
1819 if (u64ExpireGipTime < pGVMM->uNsNextEmtWakeup)
1820 pGVMM->uNsNextEmtWakeup = u64ExpireGipTime;
1821 ASMAtomicWriteU64(&pCurGVCpu->gvmm.s.u64HaltExpire, u64ExpireGipTime);
1822 ASMAtomicIncU32(&pGVMM->cHaltedEMTs);
1823 gvmmR0UsedUnlock(pGVMM);
1824
1825 rc = RTSemEventMultiWaitEx(pCurGVCpu->gvmm.s.HaltEventMulti,
1826 RTSEMWAIT_FLAGS_ABSOLUTE | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_INTERRUPTIBLE,
1827 u64NowGip > u64NowSys ? u64ExpireGipTime : u64NowSys + cNsInterval);
1828
1829 ASMAtomicWriteU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0);
1830 ASMAtomicDecU32(&pGVMM->cHaltedEMTs);
1831
1832 /* Reset the semaphore to try prevent a few false wake-ups. */
1833 if (rc == VINF_SUCCESS)
1834 RTSemEventMultiReset(pCurGVCpu->gvmm.s.HaltEventMulti);
1835 else if (rc == VERR_TIMEOUT)
1836 {
1837 pGVM->gvmm.s.StatsSched.cHaltTimeouts++;
1838 rc = VINF_SUCCESS;
1839 }
1840 }
1841 else
1842 {
1843 pGVM->gvmm.s.StatsSched.cHaltNotBlocking++;
1844 gvmmR0UsedUnlock(pGVMM);
1845 RTSemEventMultiReset(pCurGVCpu->gvmm.s.HaltEventMulti);
1846 }
1847
1848 return rc;
1849}
1850
1851
1852/**
1853 * Worker for GVMMR0SchedWakeUp and GVMMR0SchedWakeUpAndPokeCpus that wakes up
1854 * the a sleeping EMT.
1855 *
1856 * @retval VINF_SUCCESS if successfully woken up.
1857 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
1858 *
1859 * @param pGVM The global (ring-0) VM structure.
1860 * @param pGVCpu The global (ring-0) VCPU structure.
1861 */
1862DECLINLINE(int) gvmmR0SchedWakeUpOne(PGVM pGVM, PGVMCPU pGVCpu)
1863{
1864 pGVM->gvmm.s.StatsSched.cWakeUpCalls++;
1865
1866 /*
1867 * Signal the semaphore regardless of whether it's current blocked on it.
1868 *
1869 * The reason for this is that there is absolutely no way we can be 100%
1870 * certain that it isn't *about* go to go to sleep on it and just got
1871 * delayed a bit en route. So, we will always signal the semaphore when
1872 * the it is flagged as halted in the VMM.
1873 */
1874/** @todo we can optimize some of that by means of the pVCpu->enmState now. */
1875 int rc;
1876 if (pGVCpu->gvmm.s.u64HaltExpire)
1877 {
1878 rc = VINF_SUCCESS;
1879 ASMAtomicWriteU64(&pGVCpu->gvmm.s.u64HaltExpire, 0);
1880 }
1881 else
1882 {
1883 rc = VINF_GVM_NOT_BLOCKED;
1884 pGVM->gvmm.s.StatsSched.cWakeUpNotHalted++;
1885 }
1886
1887 int rc2 = RTSemEventMultiSignal(pGVCpu->gvmm.s.HaltEventMulti);
1888 AssertRC(rc2);
1889
1890 return rc;
1891}
1892
1893
1894/**
1895 * Wakes up the halted EMT thread so it can service a pending request.
1896 *
1897 * @returns VBox status code.
1898 * @retval VINF_SUCCESS if successfully woken up.
1899 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
1900 *
1901 * @param pVM Pointer to the VM.
1902 * @param idCpu The Virtual CPU ID of the EMT to wake up.
1903 * @param fTakeUsedLock Take the used lock or not
1904 * @thread Any but EMT.
1905 */
1906GVMMR0DECL(int) GVMMR0SchedWakeUpEx(PVM pVM, VMCPUID idCpu, bool fTakeUsedLock)
1907{
1908 /*
1909 * Validate input and take the UsedLock.
1910 */
1911 PGVM pGVM;
1912 PGVMM pGVMM;
1913 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, fTakeUsedLock);
1914 if (RT_SUCCESS(rc))
1915 {
1916 if (idCpu < pGVM->cCpus)
1917 {
1918 /*
1919 * Do the actual job.
1920 */
1921 rc = gvmmR0SchedWakeUpOne(pGVM, &pGVM->aCpus[idCpu]);
1922
1923 if (fTakeUsedLock)
1924 {
1925 /*
1926 * While we're here, do a round of scheduling.
1927 */
1928 Assert(ASMGetFlags() & X86_EFL_IF);
1929 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
1930 pGVM->gvmm.s.StatsSched.cWakeUpWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
1931 }
1932 }
1933 else
1934 rc = VERR_INVALID_CPU_ID;
1935
1936 if (fTakeUsedLock)
1937 {
1938 int rc2 = gvmmR0UsedUnlock(pGVMM);
1939 AssertRC(rc2);
1940 }
1941 }
1942
1943 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
1944 return rc;
1945}
1946
1947
1948/**
1949 * Wakes up the halted EMT thread so it can service a pending request.
1950 *
1951 * @returns VBox status code.
1952 * @retval VINF_SUCCESS if successfully woken up.
1953 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
1954 *
1955 * @param pVM Pointer to the VM.
1956 * @param idCpu The Virtual CPU ID of the EMT to wake up.
1957 * @thread Any but EMT.
1958 */
1959GVMMR0DECL(int) GVMMR0SchedWakeUp(PVM pVM, VMCPUID idCpu)
1960{
1961 return GVMMR0SchedWakeUpEx(pVM, idCpu, true /* fTakeUsedLock */);
1962}
1963
1964/**
1965 * Worker common to GVMMR0SchedPoke and GVMMR0SchedWakeUpAndPokeCpus that pokes
1966 * the Virtual CPU if it's still busy executing guest code.
1967 *
1968 * @returns VBox status code.
1969 * @retval VINF_SUCCESS if poked successfully.
1970 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
1971 *
1972 * @param pGVM The global (ring-0) VM structure.
1973 * @param pVCpu Pointer to the VMCPU.
1974 */
1975DECLINLINE(int) gvmmR0SchedPokeOne(PGVM pGVM, PVMCPU pVCpu)
1976{
1977 pGVM->gvmm.s.StatsSched.cPokeCalls++;
1978
1979 RTCPUID idHostCpu = pVCpu->idHostCpu;
1980 if ( idHostCpu == NIL_RTCPUID
1981 || VMCPU_GET_STATE(pVCpu) != VMCPUSTATE_STARTED_EXEC)
1982 {
1983 pGVM->gvmm.s.StatsSched.cPokeNotBusy++;
1984 return VINF_GVM_NOT_BUSY_IN_GC;
1985 }
1986
1987 /* Note: this function is not implemented on Darwin and Linux (kernel < 2.6.19) */
1988 RTMpPokeCpu(idHostCpu);
1989 return VINF_SUCCESS;
1990}
1991
1992/**
1993 * Pokes an EMT if it's still busy running guest code.
1994 *
1995 * @returns VBox status code.
1996 * @retval VINF_SUCCESS if poked successfully.
1997 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
1998 *
1999 * @param pVM Pointer to the VM.
2000 * @param idCpu The ID of the virtual CPU to poke.
2001 * @param fTakeUsedLock Take the used lock or not
2002 */
2003GVMMR0DECL(int) GVMMR0SchedPokeEx(PVM pVM, VMCPUID idCpu, bool fTakeUsedLock)
2004{
2005 /*
2006 * Validate input and take the UsedLock.
2007 */
2008 PGVM pGVM;
2009 PGVMM pGVMM;
2010 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, fTakeUsedLock);
2011 if (RT_SUCCESS(rc))
2012 {
2013 if (idCpu < pGVM->cCpus)
2014 rc = gvmmR0SchedPokeOne(pGVM, &pVM->aCpus[idCpu]);
2015 else
2016 rc = VERR_INVALID_CPU_ID;
2017
2018 if (fTakeUsedLock)
2019 {
2020 int rc2 = gvmmR0UsedUnlock(pGVMM);
2021 AssertRC(rc2);
2022 }
2023 }
2024
2025 LogFlow(("GVMMR0SchedWakeUpAndPokeCpus: returns %Rrc\n", rc));
2026 return rc;
2027}
2028
2029
2030/**
2031 * Pokes an EMT if it's still busy running guest code.
2032 *
2033 * @returns VBox status code.
2034 * @retval VINF_SUCCESS if poked successfully.
2035 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
2036 *
2037 * @param pVM Pointer to the VM.
2038 * @param idCpu The ID of the virtual CPU to poke.
2039 */
2040GVMMR0DECL(int) GVMMR0SchedPoke(PVM pVM, VMCPUID idCpu)
2041{
2042 return GVMMR0SchedPokeEx(pVM, idCpu, true /* fTakeUsedLock */);
2043}
2044
2045
2046/**
2047 * Wakes up a set of halted EMT threads so they can service pending request.
2048 *
2049 * @returns VBox status code, no informational stuff.
2050 *
2051 * @param pVM Pointer to the VM.
2052 * @param pSleepSet The set of sleepers to wake up.
2053 * @param pPokeSet The set of CPUs to poke.
2054 */
2055GVMMR0DECL(int) GVMMR0SchedWakeUpAndPokeCpus(PVM pVM, PCVMCPUSET pSleepSet, PCVMCPUSET pPokeSet)
2056{
2057 AssertPtrReturn(pSleepSet, VERR_INVALID_POINTER);
2058 AssertPtrReturn(pPokeSet, VERR_INVALID_POINTER);
2059 RTNATIVETHREAD hSelf = RTThreadNativeSelf();
2060
2061 /*
2062 * Validate input and take the UsedLock.
2063 */
2064 PGVM pGVM;
2065 PGVMM pGVMM;
2066 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
2067 if (RT_SUCCESS(rc))
2068 {
2069 rc = VINF_SUCCESS;
2070 VMCPUID idCpu = pGVM->cCpus;
2071 while (idCpu-- > 0)
2072 {
2073 /* Don't try poke or wake up ourselves. */
2074 if (pGVM->aCpus[idCpu].hEMT == hSelf)
2075 continue;
2076
2077 /* just ignore errors for now. */
2078 if (VMCPUSET_IS_PRESENT(pSleepSet, idCpu))
2079 gvmmR0SchedWakeUpOne(pGVM, &pGVM->aCpus[idCpu]);
2080 else if (VMCPUSET_IS_PRESENT(pPokeSet, idCpu))
2081 gvmmR0SchedPokeOne(pGVM, &pVM->aCpus[idCpu]);
2082 }
2083
2084 int rc2 = gvmmR0UsedUnlock(pGVMM);
2085 AssertRC(rc2);
2086 }
2087
2088 LogFlow(("GVMMR0SchedWakeUpAndPokeCpus: returns %Rrc\n", rc));
2089 return rc;
2090}
2091
2092
2093/**
2094 * VMMR0 request wrapper for GVMMR0SchedWakeUpAndPokeCpus.
2095 *
2096 * @returns see GVMMR0SchedWakeUpAndPokeCpus.
2097 * @param pVM Pointer to the VM.
2098 * @param pReq Pointer to the request packet.
2099 */
2100GVMMR0DECL(int) GVMMR0SchedWakeUpAndPokeCpusReq(PVM pVM, PGVMMSCHEDWAKEUPANDPOKECPUSREQ pReq)
2101{
2102 /*
2103 * Validate input and pass it on.
2104 */
2105 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
2106 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
2107
2108 return GVMMR0SchedWakeUpAndPokeCpus(pVM, &pReq->SleepSet, &pReq->PokeSet);
2109}
2110
2111
2112
2113/**
2114 * Poll the schedule to see if someone else should get a chance to run.
2115 *
2116 * This is a bit hackish and will not work too well if the machine is
2117 * under heavy load from non-VM processes.
2118 *
2119 * @returns VINF_SUCCESS if not yielded.
2120 * VINF_GVM_YIELDED if an attempt to switch to a different VM task was made.
2121 * @param pVM Pointer to the VM.
2122 * @param idCpu The Virtual CPU ID of the calling EMT.
2123 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
2124 * @param fYield Whether to yield or not.
2125 * This is for when we're spinning in the halt loop.
2126 * @thread EMT(idCpu).
2127 */
2128GVMMR0DECL(int) GVMMR0SchedPoll(PVM pVM, VMCPUID idCpu, bool fYield)
2129{
2130 /*
2131 * Validate input.
2132 */
2133 PGVM pGVM;
2134 PGVMM pGVMM;
2135 int rc = gvmmR0ByVMAndEMT(pVM, idCpu, &pGVM, &pGVMM);
2136 if (RT_SUCCESS(rc))
2137 {
2138 rc = gvmmR0UsedLock(pGVMM);
2139 AssertRC(rc);
2140 pGVM->gvmm.s.StatsSched.cPollCalls++;
2141
2142 Assert(ASMGetFlags() & X86_EFL_IF);
2143 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
2144
2145 if (!fYield)
2146 pGVM->gvmm.s.StatsSched.cPollWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
2147 else
2148 {
2149 /** @todo implement this... */
2150 rc = VERR_NOT_IMPLEMENTED;
2151 }
2152
2153 gvmmR0UsedUnlock(pGVMM);
2154 }
2155
2156 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
2157 return rc;
2158}
2159
2160
2161#ifdef GVMM_SCHED_WITH_PPT
2162/**
2163 * Timer callback for the periodic preemption timer.
2164 *
2165 * @param pTimer The timer handle.
2166 * @param pvUser Pointer to the per cpu structure.
2167 * @param iTick The current tick.
2168 */
2169static DECLCALLBACK(void) gvmmR0SchedPeriodicPreemptionTimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
2170{
2171 PGVMMHOSTCPU pCpu = (PGVMMHOSTCPU)pvUser;
2172 NOREF(pTimer); NOREF(iTick);
2173
2174 /*
2175 * Termination check
2176 */
2177 if (pCpu->u32Magic != GVMMHOSTCPU_MAGIC)
2178 return;
2179
2180 /*
2181 * Do the house keeping.
2182 */
2183 RTSpinlockAcquire(pCpu->Ppt.hSpinlock);
2184
2185 if (++pCpu->Ppt.iTickHistorization >= pCpu->Ppt.cTicksHistoriziationInterval)
2186 {
2187 /*
2188 * Historicize the max frequency.
2189 */
2190 uint32_t iHzHistory = ++pCpu->Ppt.iHzHistory % RT_ELEMENTS(pCpu->Ppt.aHzHistory);
2191 pCpu->Ppt.aHzHistory[iHzHistory] = pCpu->Ppt.uDesiredHz;
2192 pCpu->Ppt.iTickHistorization = 0;
2193 pCpu->Ppt.uDesiredHz = 0;
2194
2195 /*
2196 * Check if the current timer frequency.
2197 */
2198 uint32_t uHistMaxHz = 0;
2199 for (uint32_t i = 0; i < RT_ELEMENTS(pCpu->Ppt.aHzHistory); i++)
2200 if (pCpu->Ppt.aHzHistory[i] > uHistMaxHz)
2201 uHistMaxHz = pCpu->Ppt.aHzHistory[i];
2202 if (uHistMaxHz == pCpu->Ppt.uTimerHz)
2203 RTSpinlockReleaseNoInts(pCpu->Ppt.hSpinlock);
2204 else if (uHistMaxHz)
2205 {
2206 /*
2207 * Reprogram it.
2208 */
2209 pCpu->Ppt.cChanges++;
2210 pCpu->Ppt.iTickHistorization = 0;
2211 pCpu->Ppt.uTimerHz = uHistMaxHz;
2212 uint32_t const cNsInterval = RT_NS_1SEC / uHistMaxHz;
2213 pCpu->Ppt.cNsInterval = cNsInterval;
2214 if (cNsInterval < GVMMHOSTCPU_PPT_HIST_INTERVAL_NS)
2215 pCpu->Ppt.cTicksHistoriziationInterval = ( GVMMHOSTCPU_PPT_HIST_INTERVAL_NS
2216 + GVMMHOSTCPU_PPT_HIST_INTERVAL_NS / 2 - 1)
2217 / cNsInterval;
2218 else
2219 pCpu->Ppt.cTicksHistoriziationInterval = 1;
2220 RTSpinlockReleaseNoInts(pCpu->Ppt.hSpinlock);
2221
2222 /*SUPR0Printf("Cpu%u: change to %u Hz / %u ns\n", pCpu->idxCpuSet, uHistMaxHz, cNsInterval);*/
2223 RTTimerChangeInterval(pTimer, cNsInterval);
2224 }
2225 else
2226 {
2227 /*
2228 * Stop it.
2229 */
2230 pCpu->Ppt.fStarted = false;
2231 pCpu->Ppt.uTimerHz = 0;
2232 pCpu->Ppt.cNsInterval = 0;
2233 RTSpinlockReleaseNoInts(pCpu->Ppt.hSpinlock);
2234
2235 /*SUPR0Printf("Cpu%u: stopping (%u Hz)\n", pCpu->idxCpuSet, uHistMaxHz);*/
2236 RTTimerStop(pTimer);
2237 }
2238 }
2239 else
2240 RTSpinlockReleaseNoInts(pCpu->Ppt.hSpinlock);
2241}
2242#endif /* GVMM_SCHED_WITH_PPT */
2243
2244
2245/**
2246 * Updates the periodic preemption timer for the calling CPU.
2247 *
2248 * The caller must have disabled preemption!
2249 * The caller must check that the host can do high resolution timers.
2250 *
2251 * @param pVM Pointer to the VM.
2252 * @param idHostCpu The current host CPU id.
2253 * @param uHz The desired frequency.
2254 */
2255GVMMR0DECL(void) GVMMR0SchedUpdatePeriodicPreemptionTimer(PVM pVM, RTCPUID idHostCpu, uint32_t uHz)
2256{
2257 NOREF(pVM);
2258#ifdef GVMM_SCHED_WITH_PPT
2259 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2260 Assert(RTTimerCanDoHighResolution());
2261
2262 /*
2263 * Resolve the per CPU data.
2264 */
2265 uint32_t iCpu = RTMpCpuIdToSetIndex(idHostCpu);
2266 PGVMM pGVMM = g_pGVMM;
2267 if ( !VALID_PTR(pGVMM)
2268 || pGVMM->u32Magic != GVMM_MAGIC)
2269 return;
2270 AssertMsgReturnVoid(iCpu < pGVMM->cHostCpus, ("iCpu=%d cHostCpus=%d\n", iCpu, pGVMM->cHostCpus));
2271 PGVMMHOSTCPU pCpu = &pGVMM->aHostCpus[iCpu];
2272 AssertMsgReturnVoid( pCpu->u32Magic == GVMMHOSTCPU_MAGIC
2273 && pCpu->idCpu == idHostCpu,
2274 ("u32Magic=%#x idCpu=% idHostCpu=%d\n", pCpu->u32Magic, pCpu->idCpu, idHostCpu));
2275
2276 /*
2277 * Check whether we need to do anything about the timer.
2278 * We have to be a little bit careful since we might be race the timer
2279 * callback here.
2280 */
2281 if (uHz > 16384)
2282 uHz = 16384; /** @todo add a query method for this! */
2283 if (RT_UNLIKELY( uHz > ASMAtomicReadU32(&pCpu->Ppt.uDesiredHz)
2284 && uHz >= pCpu->Ppt.uMinHz
2285 && !pCpu->Ppt.fStarting /* solaris paranoia */))
2286 {
2287 RTSpinlockAcquire(pCpu->Ppt.hSpinlock);
2288
2289 pCpu->Ppt.uDesiredHz = uHz;
2290 uint32_t cNsInterval = 0;
2291 if (!pCpu->Ppt.fStarted)
2292 {
2293 pCpu->Ppt.cStarts++;
2294 pCpu->Ppt.fStarted = true;
2295 pCpu->Ppt.fStarting = true;
2296 pCpu->Ppt.iTickHistorization = 0;
2297 pCpu->Ppt.uTimerHz = uHz;
2298 pCpu->Ppt.cNsInterval = cNsInterval = RT_NS_1SEC / uHz;
2299 if (cNsInterval < GVMMHOSTCPU_PPT_HIST_INTERVAL_NS)
2300 pCpu->Ppt.cTicksHistoriziationInterval = ( GVMMHOSTCPU_PPT_HIST_INTERVAL_NS
2301 + GVMMHOSTCPU_PPT_HIST_INTERVAL_NS / 2 - 1)
2302 / cNsInterval;
2303 else
2304 pCpu->Ppt.cTicksHistoriziationInterval = 1;
2305 }
2306
2307 RTSpinlockReleaseNoInts(pCpu->Ppt.hSpinlock);
2308
2309 if (cNsInterval)
2310 {
2311 RTTimerChangeInterval(pCpu->Ppt.pTimer, cNsInterval);
2312 int rc = RTTimerStart(pCpu->Ppt.pTimer, cNsInterval);
2313 AssertRC(rc);
2314
2315 RTSpinlockAcquire(pCpu->Ppt.hSpinlock);
2316 if (RT_FAILURE(rc))
2317 pCpu->Ppt.fStarted = false;
2318 pCpu->Ppt.fStarting = false;
2319 RTSpinlockReleaseNoInts(pCpu->Ppt.hSpinlock);
2320 }
2321 }
2322#else /* !GVMM_SCHED_WITH_PPT */
2323 NOREF(idHostCpu); NOREF(uHz);
2324#endif /* !GVMM_SCHED_WITH_PPT */
2325}
2326
2327
2328/**
2329 * Retrieves the GVMM statistics visible to the caller.
2330 *
2331 * @returns VBox status code.
2332 *
2333 * @param pStats Where to put the statistics.
2334 * @param pSession The current session.
2335 * @param pVM The VM to obtain statistics for. Optional.
2336 */
2337GVMMR0DECL(int) GVMMR0QueryStatistics(PGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
2338{
2339 LogFlow(("GVMMR0QueryStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
2340
2341 /*
2342 * Validate input.
2343 */
2344 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
2345 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
2346 pStats->cVMs = 0; /* (crash before taking the sem...) */
2347
2348 /*
2349 * Take the lock and get the VM statistics.
2350 */
2351 PGVMM pGVMM;
2352 if (pVM)
2353 {
2354 PGVM pGVM;
2355 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
2356 if (RT_FAILURE(rc))
2357 return rc;
2358 pStats->SchedVM = pGVM->gvmm.s.StatsSched;
2359 }
2360 else
2361 {
2362 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
2363 memset(&pStats->SchedVM, 0, sizeof(pStats->SchedVM));
2364
2365 int rc = gvmmR0UsedLock(pGVMM);
2366 AssertRCReturn(rc, rc);
2367 }
2368
2369 /*
2370 * Enumerate the VMs and add the ones visible to the statistics.
2371 */
2372 pStats->cVMs = 0;
2373 pStats->cEMTs = 0;
2374 memset(&pStats->SchedSum, 0, sizeof(pStats->SchedSum));
2375
2376 for (unsigned i = pGVMM->iUsedHead;
2377 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
2378 i = pGVMM->aHandles[i].iNext)
2379 {
2380 PGVM pGVM = pGVMM->aHandles[i].pGVM;
2381 void *pvObj = pGVMM->aHandles[i].pvObj;
2382 if ( VALID_PTR(pvObj)
2383 && VALID_PTR(pGVM)
2384 && pGVM->u32Magic == GVM_MAGIC
2385 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
2386 {
2387 pStats->cVMs++;
2388 pStats->cEMTs += pGVM->cCpus;
2389
2390 pStats->SchedSum.cHaltCalls += pGVM->gvmm.s.StatsSched.cHaltCalls;
2391 pStats->SchedSum.cHaltBlocking += pGVM->gvmm.s.StatsSched.cHaltBlocking;
2392 pStats->SchedSum.cHaltTimeouts += pGVM->gvmm.s.StatsSched.cHaltTimeouts;
2393 pStats->SchedSum.cHaltNotBlocking += pGVM->gvmm.s.StatsSched.cHaltNotBlocking;
2394 pStats->SchedSum.cHaltWakeUps += pGVM->gvmm.s.StatsSched.cHaltWakeUps;
2395
2396 pStats->SchedSum.cWakeUpCalls += pGVM->gvmm.s.StatsSched.cWakeUpCalls;
2397 pStats->SchedSum.cWakeUpNotHalted += pGVM->gvmm.s.StatsSched.cWakeUpNotHalted;
2398 pStats->SchedSum.cWakeUpWakeUps += pGVM->gvmm.s.StatsSched.cWakeUpWakeUps;
2399
2400 pStats->SchedSum.cPokeCalls += pGVM->gvmm.s.StatsSched.cPokeCalls;
2401 pStats->SchedSum.cPokeNotBusy += pGVM->gvmm.s.StatsSched.cPokeNotBusy;
2402
2403 pStats->SchedSum.cPollCalls += pGVM->gvmm.s.StatsSched.cPollCalls;
2404 pStats->SchedSum.cPollHalts += pGVM->gvmm.s.StatsSched.cPollHalts;
2405 pStats->SchedSum.cPollWakeUps += pGVM->gvmm.s.StatsSched.cPollWakeUps;
2406 }
2407 }
2408
2409 /*
2410 * Copy out the per host CPU statistics.
2411 */
2412 uint32_t iDstCpu = 0;
2413 uint32_t cSrcCpus = pGVMM->cHostCpus;
2414 for (uint32_t iSrcCpu = 0; iSrcCpu < cSrcCpus; iSrcCpu++)
2415 {
2416 if (pGVMM->aHostCpus[iSrcCpu].idCpu != NIL_RTCPUID)
2417 {
2418 pStats->aHostCpus[iDstCpu].idCpu = pGVMM->aHostCpus[iSrcCpu].idCpu;
2419 pStats->aHostCpus[iDstCpu].idxCpuSet = pGVMM->aHostCpus[iSrcCpu].idxCpuSet;
2420#ifdef GVMM_SCHED_WITH_PPT
2421 pStats->aHostCpus[iDstCpu].uDesiredHz = pGVMM->aHostCpus[iSrcCpu].Ppt.uDesiredHz;
2422 pStats->aHostCpus[iDstCpu].uTimerHz = pGVMM->aHostCpus[iSrcCpu].Ppt.uTimerHz;
2423 pStats->aHostCpus[iDstCpu].cChanges = pGVMM->aHostCpus[iSrcCpu].Ppt.cChanges;
2424 pStats->aHostCpus[iDstCpu].cStarts = pGVMM->aHostCpus[iSrcCpu].Ppt.cStarts;
2425#else
2426 pStats->aHostCpus[iDstCpu].uDesiredHz = 0;
2427 pStats->aHostCpus[iDstCpu].uTimerHz = 0;
2428 pStats->aHostCpus[iDstCpu].cChanges = 0;
2429 pStats->aHostCpus[iDstCpu].cStarts = 0;
2430#endif
2431 iDstCpu++;
2432 if (iDstCpu >= RT_ELEMENTS(pStats->aHostCpus))
2433 break;
2434 }
2435 }
2436 pStats->cHostCpus = iDstCpu;
2437
2438 gvmmR0UsedUnlock(pGVMM);
2439
2440 return VINF_SUCCESS;
2441}
2442
2443
2444/**
2445 * VMMR0 request wrapper for GVMMR0QueryStatistics.
2446 *
2447 * @returns see GVMMR0QueryStatistics.
2448 * @param pVM Pointer to the VM. Optional.
2449 * @param pReq Pointer to the request packet.
2450 */
2451GVMMR0DECL(int) GVMMR0QueryStatisticsReq(PVM pVM, PGVMMQUERYSTATISTICSSREQ pReq)
2452{
2453 /*
2454 * Validate input and pass it on.
2455 */
2456 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
2457 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
2458
2459 return GVMMR0QueryStatistics(&pReq->Stats, pReq->pSession, pVM);
2460}
2461
2462
2463/**
2464 * Resets the specified GVMM statistics.
2465 *
2466 * @returns VBox status code.
2467 *
2468 * @param pStats Which statistics to reset, that is, non-zero fields indicates which to reset.
2469 * @param pSession The current session.
2470 * @param pVM The VM to reset statistics for. Optional.
2471 */
2472GVMMR0DECL(int) GVMMR0ResetStatistics(PCGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
2473{
2474 LogFlow(("GVMMR0ResetStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
2475
2476 /*
2477 * Validate input.
2478 */
2479 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
2480 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
2481
2482 /*
2483 * Take the lock and get the VM statistics.
2484 */
2485 PGVMM pGVMM;
2486 if (pVM)
2487 {
2488 PGVM pGVM;
2489 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
2490 if (RT_FAILURE(rc))
2491 return rc;
2492# define MAYBE_RESET_FIELD(field) \
2493 do { if (pStats->SchedVM. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
2494 MAYBE_RESET_FIELD(cHaltCalls);
2495 MAYBE_RESET_FIELD(cHaltBlocking);
2496 MAYBE_RESET_FIELD(cHaltTimeouts);
2497 MAYBE_RESET_FIELD(cHaltNotBlocking);
2498 MAYBE_RESET_FIELD(cHaltWakeUps);
2499 MAYBE_RESET_FIELD(cWakeUpCalls);
2500 MAYBE_RESET_FIELD(cWakeUpNotHalted);
2501 MAYBE_RESET_FIELD(cWakeUpWakeUps);
2502 MAYBE_RESET_FIELD(cPokeCalls);
2503 MAYBE_RESET_FIELD(cPokeNotBusy);
2504 MAYBE_RESET_FIELD(cPollCalls);
2505 MAYBE_RESET_FIELD(cPollHalts);
2506 MAYBE_RESET_FIELD(cPollWakeUps);
2507# undef MAYBE_RESET_FIELD
2508 }
2509 else
2510 {
2511 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
2512
2513 int rc = gvmmR0UsedLock(pGVMM);
2514 AssertRCReturn(rc, rc);
2515 }
2516
2517 /*
2518 * Enumerate the VMs and add the ones visible to the statistics.
2519 */
2520 if (ASMMemIsAll8(&pStats->SchedSum, sizeof(pStats->SchedSum), 0))
2521 {
2522 for (unsigned i = pGVMM->iUsedHead;
2523 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
2524 i = pGVMM->aHandles[i].iNext)
2525 {
2526 PGVM pGVM = pGVMM->aHandles[i].pGVM;
2527 void *pvObj = pGVMM->aHandles[i].pvObj;
2528 if ( VALID_PTR(pvObj)
2529 && VALID_PTR(pGVM)
2530 && pGVM->u32Magic == GVM_MAGIC
2531 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
2532 {
2533# define MAYBE_RESET_FIELD(field) \
2534 do { if (pStats->SchedSum. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
2535 MAYBE_RESET_FIELD(cHaltCalls);
2536 MAYBE_RESET_FIELD(cHaltBlocking);
2537 MAYBE_RESET_FIELD(cHaltTimeouts);
2538 MAYBE_RESET_FIELD(cHaltNotBlocking);
2539 MAYBE_RESET_FIELD(cHaltWakeUps);
2540 MAYBE_RESET_FIELD(cWakeUpCalls);
2541 MAYBE_RESET_FIELD(cWakeUpNotHalted);
2542 MAYBE_RESET_FIELD(cWakeUpWakeUps);
2543 MAYBE_RESET_FIELD(cPokeCalls);
2544 MAYBE_RESET_FIELD(cPokeNotBusy);
2545 MAYBE_RESET_FIELD(cPollCalls);
2546 MAYBE_RESET_FIELD(cPollHalts);
2547 MAYBE_RESET_FIELD(cPollWakeUps);
2548# undef MAYBE_RESET_FIELD
2549 }
2550 }
2551 }
2552
2553 gvmmR0UsedUnlock(pGVMM);
2554
2555 return VINF_SUCCESS;
2556}
2557
2558
2559/**
2560 * VMMR0 request wrapper for GVMMR0ResetStatistics.
2561 *
2562 * @returns see GVMMR0ResetStatistics.
2563 * @param pVM Pointer to the VM. Optional.
2564 * @param pReq Pointer to the request packet.
2565 */
2566GVMMR0DECL(int) GVMMR0ResetStatisticsReq(PVM pVM, PGVMMRESETSTATISTICSSREQ pReq)
2567{
2568 /*
2569 * Validate input and pass it on.
2570 */
2571 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
2572 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
2573
2574 return GVMMR0ResetStatistics(&pReq->Stats, pReq->pSession, pVM);
2575}
2576
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