VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMQueue.cpp@ 93896

最後變更 在這個檔案從93896是 93896,由 vboxsync 提交於 3 年 前

VMM/PDMQueue: Free memory when the queue count drops to 0. Addresses the failure of tstPDMQueue in ASAN builds. bugref:10093

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 31.3 KB
 
1/* $Id: PDMQueue.cpp 93896 2022-02-23 12:48:07Z vboxsync $ */
2/** @file
3 * PDM Queue - Transport data and tasks to EMT and R3.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PDM_QUEUE
23#include "PDMInternal.h"
24#include <VBox/vmm/pdm.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/vm.h>
27#include <VBox/vmm/uvm.h>
28#include <iprt/errcore.h>
29
30#include <VBox/log.h>
31#include <iprt/asm.h>
32#include <iprt/assert.h>
33#include <iprt/mem.h>
34#include <iprt/thread.h>
35
36
37/*********************************************************************************************************************************
38* Internal Functions *
39*********************************************************************************************************************************/
40static DECLCALLBACK(void) pdmR3QueueTimer(PVM pVM, TMTIMERHANDLE hTimer, void *pvUser);
41
42
43
44/**
45 * Internal worker for the queue creation apis.
46 *
47 * @returns VBox status code.
48 * @param pVM The cross context VM structure.
49 * @param cbItem Item size.
50 * @param cItems Number of items.
51 * @param cMilliesInterval Number of milliseconds between polling the queue.
52 * If 0 then the emulation thread will be notified whenever an item arrives.
53 * @param fRZEnabled Set if the queue will be used from RC/R0 and need to be allocated from the hyper heap.
54 * @param pszName The queue name. Unique. Not copied.
55 * @param enmType Owner type.
56 * @param pvOwner The queue owner pointer.
57 * @param uCallback Callback function.
58 * @param phQueue Where to store the queue handle.
59 */
60static int pdmR3QueueCreate(PVM pVM, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval, bool fRZEnabled,
61 const char *pszName, PDMQUEUETYPE enmType, void *pvOwner, uintptr_t uCallback,
62 PDMQUEUEHANDLE *phQueue)
63{
64 /*
65 * Validate and adjust the input.
66 */
67 VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT); /* serialization by exclusivity */
68
69 cbItem = RT_ALIGN(cbItem, sizeof(uint64_t));
70 AssertMsgReturn(cbItem >= sizeof(PDMQUEUEITEMCORE) && cbItem < PDMQUEUE_MAX_ITEM_SIZE, ("cbItem=%zu\n", cbItem),
71 VERR_OUT_OF_RANGE);
72 AssertMsgReturn(cItems >= 1 && cItems <= PDMQUEUE_MAX_ITEMS, ("cItems=%u\n", cItems), VERR_OUT_OF_RANGE);
73 AssertMsgReturn((uint64_t)cbItem * cItems <= (fRZEnabled ? PDMQUEUE_MAX_TOTAL_SIZE_R0 : PDMQUEUE_MAX_TOTAL_SIZE_R3),
74 ("cItems=%u cbItem=%#x -> %#RX64, max %'u\n", cItems, cbItem, (uint64_t)cbItem * cItems,
75 fRZEnabled ? PDMQUEUE_MAX_TOTAL_SIZE_R0 : PDMQUEUE_MAX_TOTAL_SIZE_R3),
76 VERR_OUT_OF_RANGE);
77 AssertReturn(!fRZEnabled || enmType == PDMQUEUETYPE_INTERNAL || enmType == PDMQUEUETYPE_DEV, VERR_INVALID_PARAMETER);
78 if (SUPR3IsDriverless())
79 fRZEnabled = false;
80
81 /* Unqiue name that fits within the szName field: */
82 size_t cchName = strlen(pszName);
83 AssertReturn(cchName > 0, VERR_INVALID_NAME);
84 AssertMsgReturn(cchName < RT_SIZEOFMEMB(PDMQUEUE, szName), ("'%s' is too long\n", pszName), VERR_INVALID_NAME);
85 size_t i = pVM->pdm.s.cRing3Queues;
86 while (i-- > 0 )
87 AssertMsgReturn(strcmp(pVM->pdm.s.papRing3Queues[i]->szName, pszName) != 0, ("%s\n", pszName), VERR_DUPLICATE);
88 i = pVM->pdm.s.cRing0Queues;
89 while (i-- > 0 )
90 AssertMsgReturn(strcmp(pVM->pdm.s.apRing0Queues[i]->szName, pszName) != 0, ("%s\n", pszName), VERR_DUPLICATE);
91
92 /*
93 * Align the item size and calculate the structure size.
94 */
95 PPDMQUEUE pQueue;
96 PDMQUEUEHANDLE hQueue;
97 if (fRZEnabled)
98 {
99 /* Call ring-0 to allocate and create the queue: */
100 PDMQUEUECREATEREQ Req;
101 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
102 Req.Hdr.cbReq = sizeof(Req);
103 Req.cItems = cItems;
104 Req.cbItem = (uint32_t)cbItem;
105 Req.enmType = enmType;
106 Req.pvOwner = pvOwner;
107 Req.pfnCallback = (RTR3PTR)uCallback;
108 RTStrCopy(Req.szName, sizeof(Req.szName), pszName);
109 AssertCompileMembersSameSize(PDMQUEUECREATEREQ, szName, PDMQUEUE, szName);
110 Req.hQueue = NIL_PDMQUEUEHANDLE;
111
112 int rc = VMMR3CallR0(pVM, VMMR0_DO_PDM_QUEUE_CREATE, 0, &Req.Hdr);
113 if (RT_FAILURE(rc))
114 return rc;
115 hQueue = Req.hQueue;
116 AssertReturn(hQueue < RT_ELEMENTS(pVM->pdm.s.apRing0Queues), VERR_INTERNAL_ERROR_2);
117 pQueue = pVM->pdm.s.apRing0Queues[hQueue];
118 AssertPtrReturn(pQueue, VERR_INTERNAL_ERROR_3);
119 AssertReturn(pQueue->u32Magic == PDMQUEUE_MAGIC, VERR_INTERNAL_ERROR_4);
120 AssertReturn(pQueue->cbItem == cbItem, VERR_INTERNAL_ERROR_4);
121 AssertReturn(pQueue->cItems == cItems, VERR_INTERNAL_ERROR_4);
122 AssertReturn(pQueue->enmType == enmType, VERR_INTERNAL_ERROR_4);
123 AssertReturn(pQueue->u.Gen.pvOwner == pvOwner, VERR_INTERNAL_ERROR_4);
124 AssertReturn(pQueue->u.Gen.pfnCallback == (RTR3PTR)uCallback, VERR_INTERNAL_ERROR_4);
125 }
126 else
127 {
128 /* Do it here using the paged heap: */
129 uint32_t const cbBitmap = RT_ALIGN_32(RT_ALIGN_32(cItems, 64) / 8, 64); /* keep bitmap in it's own cacheline */
130 uint32_t const cbQueue = RT_OFFSETOF(PDMQUEUE, bmAlloc)
131 + cbBitmap
132 + (uint32_t)cbItem * cItems;
133 pQueue = (PPDMQUEUE)RTMemPageAllocZ(cbQueue);
134 if (!pQueue)
135 return VERR_NO_PAGE_MEMORY;
136 pdmQueueInit(pQueue, cbBitmap, (uint32_t)cbItem, cItems, pszName, enmType, (RTR3PTR)uCallback, pvOwner);
137
138 uint32_t iQueue = pVM->pdm.s.cRing3Queues;
139 if (iQueue >= pVM->pdm.s.cRing3QueuesAlloc)
140 {
141 AssertLogRelMsgReturnStmt(iQueue < _16K, ("%#x\n", iQueue), RTMemPageFree(pQueue, cbQueue), VERR_TOO_MANY_OPENS);
142
143 uint32_t const cNewAlloc = RT_ALIGN_32(iQueue, 64) + 64;
144 PPDMQUEUE *papQueuesNew = (PPDMQUEUE *)RTMemAllocZ(cNewAlloc * sizeof(papQueuesNew[0]));
145 AssertLogRelMsgReturnStmt(papQueuesNew, ("cNewAlloc=%u\n", cNewAlloc), RTMemPageFree(pQueue, cbQueue), VERR_NO_MEMORY);
146
147 if (iQueue)
148 memcpy(papQueuesNew, pVM->pdm.s.papRing3Queues, iQueue * sizeof(papQueuesNew[0]));
149 PPDMQUEUE *papQueuesOld = ASMAtomicXchgPtrT(&pVM->pdm.s.papRing3Queues, papQueuesNew, PPDMQUEUE *);
150 pVM->pdm.s.cRing3QueuesAlloc = cNewAlloc;
151 RTMemFree(papQueuesOld);
152 }
153
154 pVM->pdm.s.papRing3Queues[iQueue] = pQueue;
155 pVM->pdm.s.cRing3Queues = iQueue + 1;
156 hQueue = iQueue + RT_ELEMENTS(pVM->pdm.s.apRing0Queues);
157 }
158
159 /*
160 * Create timer?
161 */
162 if (cMilliesInterval)
163 {
164 char szName[48+6];
165 RTStrPrintf(szName, sizeof(szName), "Que/%s", pQueue->szName);
166 int rc = TMR3TimerCreate(pVM, TMCLOCK_REAL, pdmR3QueueTimer, pQueue, TMTIMER_FLAGS_NO_RING0, szName, &pQueue->hTimer);
167 if (RT_SUCCESS(rc))
168 {
169 rc = TMTimerSetMillies(pVM, pQueue->hTimer, cMilliesInterval);
170 if (RT_SUCCESS(rc))
171 pQueue->cMilliesInterval = cMilliesInterval;
172 else
173 {
174 AssertMsgFailed(("TMTimerSetMillies failed rc=%Rrc\n", rc));
175 int rc2 = TMR3TimerDestroy(pVM, pQueue->hTimer); AssertRC(rc2);
176 pQueue->hTimer = NIL_TMTIMERHANDLE;
177 }
178 }
179 else
180 AssertMsgFailed(("TMR3TimerCreateInternal failed rc=%Rrc\n", rc));
181 if (RT_FAILURE(rc))
182 {
183 if (!fRZEnabled)
184 PDMR3QueueDestroy(pVM, hQueue, pvOwner);
185 /* else: will clean up queue when VM is destroyed */
186 return rc;
187 }
188 }
189
190 /*
191 * Register the statistics.
192 */
193 STAMR3RegisterF(pVM, &pQueue->cbItem, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
194 "Item size.", "/PDM/Queue/%s/cbItem", pQueue->szName);
195 STAMR3RegisterF(pVM, &pQueue->cItems, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
196 "Queue size.", "/PDM/Queue/%s/cItems", pQueue->szName);
197 STAMR3RegisterF(pVM, &pQueue->rcOkay, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_NONE,
198 "Non-zero means queue is busted.", "/PDM/Queue/%s/rcOkay", pQueue->szName);
199 STAMR3RegisterF(pVM, &pQueue->StatAllocFailures, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
200 "PDMQueueAlloc failures.", "/PDM/Queue/%s/AllocFailures", pQueue->szName);
201 STAMR3RegisterF(pVM, &pQueue->StatInsert, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS,
202 "Calls to PDMQueueInsert.", "/PDM/Queue/%s/Insert", pQueue->szName);
203 STAMR3RegisterF(pVM, &pQueue->StatFlush, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS,
204 "Calls to pdmR3QueueFlush.", "/PDM/Queue/%s/Flush", pQueue->szName);
205 STAMR3RegisterF(pVM, &pQueue->StatFlushLeftovers, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
206 "Left over items after flush.", "/PDM/Queue/%s/FlushLeftovers", pQueue->szName);
207#ifdef VBOX_WITH_STATISTICS
208 STAMR3RegisterF(pVM, &pQueue->StatFlushPrf, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL,
209 "Profiling pdmR3QueueFlush.", "/PDM/Queue/%s/FlushPrf", pQueue->szName);
210 STAMR3RegisterF(pVM, (void *)&pQueue->cStatPending, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
211 "Pending items.", "/PDM/Queue/%s/Pending", pQueue->szName);
212#endif
213
214 *phQueue = hQueue;
215 return VINF_SUCCESS;
216}
217
218
219/**
220 * Create a queue with a device owner.
221 *
222 * @returns VBox status code.
223 * @param pVM The cross context VM structure.
224 * @param pDevIns Device instance.
225 * @param cbItem Size a queue item.
226 * @param cItems Number of items in the queue.
227 * @param cMilliesInterval Number of milliseconds between polling the queue.
228 * If 0 then the emulation thread will be notified whenever an item arrives.
229 * @param pfnCallback The consumer function.
230 * @param fRZEnabled Set if the queue must be usable from RC/R0.
231 * @param pszName The queue name. Unique. Copied.
232 * @param phQueue Where to store the queue handle on success.
233 * @thread Emulation thread only.
234 */
235VMMR3_INT_DECL(int) PDMR3QueueCreateDevice(PVM pVM, PPDMDEVINS pDevIns, size_t cbItem, uint32_t cItems,
236 uint32_t cMilliesInterval, PFNPDMQUEUEDEV pfnCallback,
237 bool fRZEnabled, const char *pszName, PDMQUEUEHANDLE *phQueue)
238{
239 LogFlow(("PDMR3QueueCreateDevice: pDevIns=%p cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p fRZEnabled=%RTbool pszName=%s\n",
240 pDevIns, cbItem, cItems, cMilliesInterval, pfnCallback, fRZEnabled, pszName));
241
242 /*
243 * Validate input.
244 */
245 VM_ASSERT_EMT0(pVM);
246 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
247 AssertPtrReturn(pDevIns, VERR_INVALID_POINTER);
248
249 if (!(pDevIns->Internal.s.fIntFlags & PDMDEVINSINT_FLAGS_R0_ENABLED))
250 fRZEnabled = false;
251
252 /*
253 * Create the queue.
254 */
255 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, fRZEnabled, pszName,
256 PDMQUEUETYPE_DEV, pDevIns, (uintptr_t)pfnCallback, phQueue);
257 if (RT_SUCCESS(rc))
258 Log(("PDM: Created device queue %#RX64; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p pDevIns=%p\n",
259 *phQueue, cbItem, cItems, cMilliesInterval, pfnCallback, pDevIns));
260 return rc;
261}
262
263
264/**
265 * Create a queue with a driver owner.
266 *
267 * @returns VBox status code.
268 * @param pVM The cross context VM structure.
269 * @param pDrvIns Driver instance.
270 * @param cbItem Size a queue item.
271 * @param cItems Number of items in the queue.
272 * @param cMilliesInterval Number of milliseconds between polling the queue.
273 * If 0 then the emulation thread will be notified whenever an item arrives.
274 * @param pfnCallback The consumer function.
275 * @param pszName The queue name. Unique. Copied.
276 * @param phQueue Where to store the queue handle on success.
277 * @thread Emulation thread only.
278 */
279VMMR3_INT_DECL(int) PDMR3QueueCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
280 PFNPDMQUEUEDRV pfnCallback, const char *pszName, PDMQUEUEHANDLE *phQueue)
281{
282 LogFlow(("PDMR3QueueCreateDriver: pDrvIns=%p cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p pszName=%s\n",
283 pDrvIns, cbItem, cItems, cMilliesInterval, pfnCallback, pszName));
284
285 /*
286 * Validate input.
287 */
288 VM_ASSERT_EMT0(pVM);
289 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
290 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
291
292 /*
293 * Create the queue.
294 */
295 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, false /*fRZEnabled*/, pszName,
296 PDMQUEUETYPE_DRV, pDrvIns, (uintptr_t)pfnCallback, phQueue);
297 if (RT_SUCCESS(rc))
298 Log(("PDM: Created driver queue %#RX64; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p pDrvIns=%p\n",
299 *phQueue, cbItem, cItems, cMilliesInterval, pfnCallback, pDrvIns));
300 return rc;
301}
302
303
304/**
305 * Create a queue with an internal owner.
306 *
307 * @returns VBox status code.
308 * @param pVM The cross context VM structure.
309 * @param cbItem Size a queue item.
310 * @param cItems Number of items in the queue.
311 * @param cMilliesInterval Number of milliseconds between polling the queue.
312 * If 0 then the emulation thread will be notified whenever an item arrives.
313 * @param pfnCallback The consumer function.
314 * @param fRZEnabled Set if the queue must be usable from RC/R0.
315 * @param pszName The queue name. Unique. Copied.
316 * @param phQueue Where to store the queue handle on success.
317 * @thread Emulation thread only.
318 */
319VMMR3_INT_DECL(int) PDMR3QueueCreateInternal(PVM pVM, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
320 PFNPDMQUEUEINT pfnCallback, bool fRZEnabled,
321 const char *pszName, PDMQUEUEHANDLE *phQueue)
322{
323 LogFlow(("PDMR3QueueCreateInternal: cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p fRZEnabled=%RTbool pszName=%s\n",
324 cbItem, cItems, cMilliesInterval, pfnCallback, fRZEnabled, pszName));
325
326 /*
327 * Validate input.
328 */
329 VM_ASSERT_EMT0(pVM);
330 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
331
332 /*
333 * Create the queue.
334 */
335 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, fRZEnabled, pszName,
336 PDMQUEUETYPE_INTERNAL, pVM, (uintptr_t)pfnCallback, phQueue);
337 if (RT_SUCCESS(rc))
338 Log(("PDM: Created internal queue %p; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p\n",
339 *phQueue, cbItem, cItems, cMilliesInterval, pfnCallback));
340 return rc;
341}
342
343
344/**
345 * Create a queue with an external owner.
346 *
347 * @returns VBox status code.
348 * @param pVM The cross context VM structure.
349 * @param cbItem Size a queue item.
350 * @param cItems Number of items in the queue.
351 * @param cMilliesInterval Number of milliseconds between polling the queue.
352 * If 0 then the emulation thread will be notified whenever an item arrives.
353 * @param pfnCallback The consumer function.
354 * @param pvUser The user argument to the consumer function.
355 * @param pszName The queue name. Unique. Not copied.
356 * @param phQueue Where to store the queue handle on success.
357 * @thread Emulation thread only.
358 */
359VMMR3DECL(int) PDMR3QueueCreateExternal(PVM pVM, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
360 PFNPDMQUEUEEXT pfnCallback, void *pvUser,
361 const char *pszName, PDMQUEUEHANDLE *phQueue)
362{
363 LogFlow(("PDMR3QueueCreateExternal: cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p pszName=%s\n",
364 cbItem, cItems, cMilliesInterval, pfnCallback, pszName));
365
366 /*
367 * Validate input.
368 */
369 VM_ASSERT_EMT0(pVM);
370 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
371
372 /*
373 * Create the queue.
374 */
375 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, false /*fRZEnabled*/, pszName,
376 PDMQUEUETYPE_EXTERNAL, pvUser, (uintptr_t)pfnCallback, phQueue);
377 if (RT_SUCCESS(rc))
378 Log(("PDM: Created external queue %p; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p pvUser=%p\n",
379 *phQueue, cbItem, cItems, cMilliesInterval, pfnCallback, pvUser));
380 return rc;
381}
382
383
384/**
385 * Destroy a queue.
386 *
387 * @returns VBox status code.
388 * @param pVM Pointer to the cross context VM structure.
389 * @param hQueue Handle to the queue that should be destroyed.
390 * @param pvOwner The owner address.
391 * @thread EMT(0)
392 * @note Externally visible mainly for testing purposes.
393 */
394VMMR3DECL(int) PDMR3QueueDestroy(PVM pVM, PDMQUEUEHANDLE hQueue, void *pvOwner)
395{
396 LogFlow(("PDMR3QueueDestroy: hQueue=%p pvOwner=%p\n", hQueue, pvOwner));
397
398 /*
399 * Validate input.
400 */
401 VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT); /* serialization */
402 if (hQueue == NIL_PDMQUEUEHANDLE)
403 return VINF_SUCCESS;
404
405 PPDMQUEUE pQueue;
406 bool fRZEnabled = false;
407 if (hQueue < RT_ELEMENTS(pVM->pdm.s.apRing0Queues))
408 {
409 AssertReturn(hQueue < pVM->pdm.s.cRing0Queues, VERR_INVALID_HANDLE);
410 pQueue = pVM->pdm.s.apRing0Queues[hQueue];
411 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
412 AssertReturn(pQueue->u32Magic == PDMQUEUE_MAGIC, VERR_INVALID_HANDLE);
413 AssertReturn(pQueue->u.Gen.pvOwner == pvOwner, VERR_INVALID_HANDLE);
414
415 /* Lazy bird: Cannot dynamically delete ring-0 capable queues. */
416 AssertFailedReturn(VERR_NOT_SUPPORTED);
417 }
418 else
419 {
420 hQueue -= RT_ELEMENTS(pVM->pdm.s.apRing0Queues);
421 AssertReturn(hQueue < pVM->pdm.s.cRing3Queues, VERR_INVALID_HANDLE);
422 pQueue = pVM->pdm.s.papRing3Queues[hQueue];
423 AssertPtrReturn(pQueue, VERR_INVALID_HANDLE);
424 AssertReturn(pQueue->u32Magic == PDMQUEUE_MAGIC, VERR_INVALID_HANDLE);
425 AssertReturn(pQueue->u.Gen.pvOwner == pvOwner, VERR_INVALID_HANDLE);
426
427 /* Enter the lock here to serialize with other EMTs traversing the handles. */
428 pdmLock(pVM);
429 pVM->pdm.s.papRing3Queues[hQueue] = NULL;
430 if (hQueue + 1 == pVM->pdm.s.cRing3Queues)
431 {
432 while (hQueue > 0 && pVM->pdm.s.papRing3Queues[hQueue - 1] == NULL)
433 hQueue--;
434 pVM->pdm.s.cRing3Queues = hQueue;
435 if (!hQueue)
436 {
437 pVM->pdm.s.cRing3QueuesAlloc = 0;
438 PPDMQUEUE *papQueuesOld = ASMAtomicXchgPtrT(&pVM->pdm.s.papRing3Queues, NULL, PPDMQUEUE *);
439 RTMemFree(papQueuesOld);
440 }
441 }
442 pQueue->u32Magic = PDMQUEUE_MAGIC_DEAD;
443 pdmUnlock(pVM);
444 }
445
446 /*
447 * Deregister statistics.
448 */
449 STAMR3DeregisterF(pVM->pUVM, "/PDM/Queue/%s/*", pQueue->szName);
450
451 /*
452 * Destroy the timer and free it.
453 */
454 if (pQueue->hTimer != NIL_TMTIMERHANDLE)
455 {
456 TMR3TimerDestroy(pVM, pQueue->hTimer);
457 pQueue->hTimer = NIL_TMTIMERHANDLE;
458 }
459 if (!fRZEnabled)
460 RTMemPageFree(pQueue, pQueue->offItems + pQueue->cbItem * pQueue->cItems);
461
462 return VINF_SUCCESS;
463}
464
465
466/**
467 * Destroy a all queues with a given owner.
468 *
469 * @returns VBox status code.
470 * @param pVM The cross context VM structure.
471 * @param pvOwner The owner pointer.
472 * @param enmType Owner type.
473 * @thread EMT(0)
474 */
475static int pdmR3QueueDestroyByOwner(PVM pVM, void *pvOwner, PDMQUEUETYPE enmType)
476{
477 LogFlow(("pdmR3QueueDestroyByOwner: pvOwner=%p enmType=%d\n", pvOwner, enmType));
478
479 /*
480 * Validate input.
481 */
482 AssertPtrReturn(pvOwner, VERR_INVALID_PARAMETER);
483 AssertReturn(pvOwner != pVM, VERR_INVALID_PARAMETER);
484 VM_ASSERT_EMT0_RETURN(pVM, VERR_VM_THREAD_NOT_EMT); /* serialization */
485
486 /*
487 * Scan and destroy.
488 */
489 uint32_t i = pVM->pdm.s.cRing0Queues;
490 while (i-- > 0)
491 {
492 PPDMQUEUE pQueue = pVM->pdm.s.apRing0Queues[i];
493 if ( pQueue
494 && pQueue->u.Gen.pvOwner == pvOwner
495 && pQueue->enmType == enmType)
496 {
497 /* Not supported at runtime. */
498 VM_ASSERT_STATE_RETURN(pVM, VMSTATE_DESTROYING, VERR_WRONG_ORDER);
499 }
500 }
501
502 i = pVM->pdm.s.cRing3Queues;
503 while (i-- > 0)
504 {
505 PPDMQUEUE pQueue = pVM->pdm.s.papRing3Queues[i];
506 if ( pQueue
507 && pQueue->u.Gen.pvOwner == pvOwner
508 && pQueue->enmType == enmType)
509 PDMR3QueueDestroy(pVM, i + RT_ELEMENTS(pVM->pdm.s.apRing0Queues), pvOwner);
510 }
511
512 return VINF_SUCCESS;
513}
514
515
516/**
517 * Destroy a all queues owned by the specified device.
518 *
519 * @returns VBox status code.
520 * @param pVM The cross context VM structure.
521 * @param pDevIns Device instance.
522 * @thread EMT(0)
523 */
524VMMR3_INT_DECL(int) PDMR3QueueDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
525{
526 LogFlow(("PDMR3QueueDestroyDevice: pDevIns=%p\n", pDevIns));
527 return pdmR3QueueDestroyByOwner(pVM, pDevIns, PDMQUEUETYPE_DEV);
528}
529
530
531/**
532 * Destroy a all queues owned by the specified driver.
533 *
534 * @returns VBox status code.
535 * @param pVM The cross context VM structure.
536 * @param pDrvIns Driver instance.
537 * @thread EMT(0)
538 */
539VMMR3_INT_DECL(int) PDMR3QueueDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
540{
541 LogFlow(("PDMR3QueueDestroyDriver: pDrvIns=%p\n", pDrvIns));
542 return pdmR3QueueDestroyByOwner(pVM, pDrvIns, PDMQUEUETYPE_DRV);
543}
544
545
546/**
547 * Free an item.
548 *
549 * @param pQueue The queue.
550 * @param pbItems Where the items area starts.
551 * @param cbItem Item size.
552 * @param pItem The item to free.
553 */
554DECLINLINE(void) pdmR3QueueFreeItem(PPDMQUEUE pQueue, uint8_t *pbItems, uint32_t cbItem, PPDMQUEUEITEMCORE pItem)
555{
556 pItem->u64View = UINT64_C(0xfeedfeedfeedfeed);
557
558 uintptr_t const offItem = (uintptr_t)pItem - (uintptr_t)pbItems;
559 uintptr_t const iItem = offItem / cbItem;
560 Assert(!(offItem % cbItem));
561 Assert(iItem < pQueue->cItems);
562 AssertReturnVoidStmt(ASMAtomicBitTestAndSet(pQueue->bmAlloc, iItem) == false, pQueue->rcOkay = VERR_INTERNAL_ERROR_4);
563 STAM_STATS({ ASMAtomicDecU32(&pQueue->cStatPending); });
564}
565
566
567
568/**
569 * Process pending items in one queue.
570 *
571 * @returns VBox status code.
572 * @param pVM The cross context VM structure.
573 * @param pQueue The queue needing flushing.
574 */
575static int pdmR3QueueFlush(PVM pVM, PPDMQUEUE pQueue)
576{
577 STAM_PROFILE_START(&pQueue->StatFlushPrf,p);
578
579 uint32_t const cbItem = pQueue->cbItem;
580 uint32_t const cItems = pQueue->cItems;
581 uint8_t * const pbItems = (uint8_t *)pQueue + pQueue->offItems;
582
583 /*
584 * Get the list and reverse it into a pointer list (inserted in LIFO order to avoid locking).
585 */
586 uint32_t cPending = 0;
587 PPDMQUEUEITEMCORE pHead = NULL;
588 {
589 uint32_t iCur = ASMAtomicXchgU32(&pQueue->iPending, UINT32_MAX);
590 do
591 {
592 AssertMsgReturn(iCur < cItems, ("%#x vs %#x\n", iCur, cItems), pQueue->rcOkay = VERR_INTERNAL_ERROR_5);
593 AssertReturn(ASMBitTest(pQueue->bmAlloc, iCur) == false, pQueue->rcOkay = VERR_INTERNAL_ERROR_3);
594 PPDMQUEUEITEMCORE pCur = (PPDMQUEUEITEMCORE)&pbItems[iCur * cbItem];
595
596 iCur = pCur->iNext;
597 ASMCompilerBarrier(); /* paranoia */
598 pCur->pNext = pHead;
599 pHead = pCur;
600 cPending++;
601 } while (iCur != UINT32_MAX);
602 }
603 RT_NOREF(cPending);
604
605 /*
606 * Feed the items to the consumer function.
607 */
608 Log2(("pdmR3QueueFlush: pQueue=%p enmType=%d pHead=%p cItems=%u\n", pQueue, pQueue->enmType, pHead, cPending));
609 switch (pQueue->enmType)
610 {
611 case PDMQUEUETYPE_DEV:
612 while (pHead)
613 {
614 if (!pQueue->u.Dev.pfnCallback(pQueue->u.Dev.pDevIns, pHead))
615 break;
616 PPDMQUEUEITEMCORE pFree = pHead;
617 pHead = pHead->pNext;
618 ASMCompilerBarrier(); /* paranoia */
619 pdmR3QueueFreeItem(pQueue, pbItems, cbItem, pFree);
620 }
621 break;
622
623 case PDMQUEUETYPE_DRV:
624 while (pHead)
625 {
626 if (!pQueue->u.Drv.pfnCallback(pQueue->u.Drv.pDrvIns, pHead))
627 break;
628 PPDMQUEUEITEMCORE pFree = pHead;
629 pHead = pHead->pNext;
630 ASMCompilerBarrier(); /* paranoia */
631 pdmR3QueueFreeItem(pQueue, pbItems, cbItem, pFree);
632 }
633 break;
634
635 case PDMQUEUETYPE_INTERNAL:
636 while (pHead)
637 {
638 if (!pQueue->u.Int.pfnCallback(pVM, pHead))
639 break;
640 PPDMQUEUEITEMCORE pFree = pHead;
641 pHead = pHead->pNext;
642 ASMCompilerBarrier(); /* paranoia */
643 pdmR3QueueFreeItem(pQueue, pbItems, cbItem, pFree);
644 }
645 break;
646
647 case PDMQUEUETYPE_EXTERNAL:
648 while (pHead)
649 {
650 if (!pQueue->u.Ext.pfnCallback(pQueue->u.Ext.pvUser, pHead))
651 break;
652 PPDMQUEUEITEMCORE pFree = pHead;
653 pHead = pHead->pNext;
654 ASMCompilerBarrier(); /* paranoia */
655 pdmR3QueueFreeItem(pQueue, pbItems, cbItem, pFree);
656 }
657 break;
658
659 default:
660 AssertMsgFailed(("Invalid queue type %d\n", pQueue->enmType));
661 break;
662 }
663
664 /*
665 * Success?
666 */
667 if (!pHead)
668 { /* likely */ }
669 else
670 {
671 /*
672 * Reverse the list and turn it back into index chain.
673 */
674 uint32_t iPendingHead = UINT32_MAX;
675 do
676 {
677 PPDMQUEUEITEMCORE pInsert = pHead;
678 pHead = pHead->pNext;
679 ASMCompilerBarrier(); /* paranoia */
680 pInsert->iNext = iPendingHead;
681 iPendingHead = ((uintptr_t)pInsert - (uintptr_t)pbItems) / cbItem;
682 } while (pHead);
683
684 /*
685 * Insert the list at the tail of the pending list. If someone races
686 * us there, we have to join the new LIFO with the old.
687 */
688 for (;;)
689 {
690 if (ASMAtomicCmpXchgU32(&pQueue->iPending, iPendingHead, UINT32_MAX))
691 break;
692
693 uint32_t const iNewPending = ASMAtomicXchgU32(&pQueue->iPending, UINT32_MAX);
694 if (iNewPending != UINT32_MAX)
695 {
696 /* Find the last entry and chain iPendingHead onto it. */
697 uint32_t iCur = iNewPending;
698 for (;;)
699 {
700 AssertReturn(iCur < cItems, pQueue->rcOkay = VERR_INTERNAL_ERROR_2);
701 AssertReturn(ASMBitTest(pQueue->bmAlloc, iCur) == false, pQueue->rcOkay = VERR_INTERNAL_ERROR_3);
702 PPDMQUEUEITEMCORE pCur = (PPDMQUEUEITEMCORE)&pbItems[iCur * cbItem];
703 iCur = pCur->iNext;
704 if (iCur == UINT32_MAX)
705 {
706 pCur->iNext = iPendingHead;
707 break;
708 }
709 }
710
711 iPendingHead = iNewPending;
712 }
713 }
714
715 STAM_REL_COUNTER_INC(&pQueue->StatFlushLeftovers);
716 }
717
718 STAM_PROFILE_STOP(&pQueue->StatFlushPrf,p);
719 return VINF_SUCCESS;
720}
721
722
723/**
724 * Flush pending queues.
725 * This is a forced action callback.
726 *
727 * @param pVM The cross context VM structure.
728 * @thread Emulation thread only.
729 * @note Internal, but exported for use in the testcase.
730 */
731VMMR3DECL(void) PDMR3QueueFlushAll(PVM pVM)
732{
733 VM_ASSERT_EMT(pVM);
734 LogFlow(("PDMR3QueuesFlush:\n"));
735
736 /*
737 * Only let one EMT flushing queues at any one time to preserve the order
738 * and to avoid wasting time. The FF is always cleared here, because it's
739 * only used to get someones attention. Queue inserts occurring during the
740 * flush are caught using the pending bit.
741 *
742 * Note! We must check the force action and pending flags after clearing
743 * the active bit!
744 */
745 VM_FF_CLEAR(pVM, VM_FF_PDM_QUEUES);
746 while (!ASMAtomicBitTestAndSet(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_ACTIVE_BIT))
747 {
748 ASMAtomicBitClear(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_PENDING_BIT);
749
750 /* Scan the ring-0 queues: */
751 size_t i = pVM->pdm.s.cRing0Queues;
752 while (i-- > 0)
753 {
754 PPDMQUEUE pQueue = pVM->pdm.s.apRing0Queues[i];
755 if ( pQueue
756 && pQueue->iPending != UINT32_MAX
757 && pQueue->hTimer == NIL_TMTIMERHANDLE
758 && pQueue->rcOkay == VINF_SUCCESS)
759 pdmR3QueueFlush(pVM, pQueue);
760 }
761
762 /* Scan the ring-3 queues: */
763/** @todo Deal with destroy concurrency issues. */
764 i = pVM->pdm.s.cRing3Queues;
765 while (i-- > 0)
766 {
767 PPDMQUEUE pQueue = pVM->pdm.s.papRing3Queues[i];
768 if ( pQueue
769 && pQueue->iPending != UINT32_MAX
770 && pQueue->hTimer == NIL_TMTIMERHANDLE
771 && pQueue->rcOkay == VINF_SUCCESS)
772 pdmR3QueueFlush(pVM, pQueue);
773 }
774
775 ASMAtomicBitClear(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_ACTIVE_BIT);
776
777 /* We're done if there were no inserts while we were busy. */
778 if ( !ASMBitTest(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_PENDING_BIT)
779 && !VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES))
780 break;
781 VM_FF_CLEAR(pVM, VM_FF_PDM_QUEUES);
782 }
783}
784
785
786
787/**
788 * @callback_method_impl{FNTMTIMERINT, Timer handler for one PDM queue.}
789 */
790static DECLCALLBACK(void) pdmR3QueueTimer(PVM pVM, TMTIMERHANDLE hTimer, void *pvUser)
791{
792 PPDMQUEUE pQueue = (PPDMQUEUE)pvUser;
793 Assert(hTimer == pQueue->hTimer);
794
795 if (pQueue->iPending != UINT32_MAX)
796 pdmR3QueueFlush(pVM, pQueue);
797
798 int rc = TMTimerSetMillies(pVM, hTimer, pQueue->cMilliesInterval);
799 AssertRC(rc);
800}
801
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette