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