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