PDMCritSect.cpp revision c647bac94d46aebf0bcf05d87cea81d048dc2675
/* $Id$ */
/** @file
* PDM - Critical Sections, Ring-3.
*/
/*
* Copyright (C) 2006-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "PDMInternal.h"
#include <iprt/lockvalidator.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int pdmR3CritSectDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal);
static int pdmR3CritSectRwDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTRWINT pCritSect, PPDMCRITSECTRWINT pPrev, bool fFinal);
/**
* Register statistics related to the critical sections.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
*/
{
STAM_REG(pVM, &pVM->pdm.s.StatQueuedCritSectLeaves, STAMTYPE_COUNTER, "/PDM/QueuedCritSectLeaves", STAMUNIT_OCCURENCES,
"Number of times a critical section leave request needed to be queued for ring-3 execution.");
return VINF_SUCCESS;
}
/**
* Relocates all the critical sections.
*
* @param pVM Pointer to the VM.
*/
{
pCur;
pCur;
}
/**
* Deletes all remaining critical sections.
*
* This is called at the very end of the termination process. It is also called
* at the end of vmR3CreateU failure cleanup, which may cause it to be called
* twice depending on where vmR3CreateU actually failed. We have to do the
* latter call because other components expect the critical sections to be
* automatically deleted.
*
* @returns VBox status.
* First error code, rest is lost.
* @param pVMU The user mode VM handle.
* @remark Don't confuse this with PDMR3CritSectDelete.
*/
{
int rc = VINF_SUCCESS;
{
}
{
}
return rc;
}
/**
* Initializes a critical section and inserts it into the list.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pCritSect The critical section.
* @param pvKey The owner key.
* @param RT_SRC_POS_DECL The source position.
* @param pszName The name of the critical section (for statistics).
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param va Arguments for the format string.
*/
{
/*
* Allocate the semaphore.
*/
if (RT_SUCCESS(rc))
{
/* Only format the name once. */
if (pszName)
{
#ifndef PDMCRITSECT_STRICT
#else
# ifdef RT_LOCK_STRICT_ORDER
# else
# endif
#endif
if (RT_SUCCESS(rc))
{
/*
* Initialize the structure (first bit is c&p from RTCritSectInitEx).
*/
pCritSect->fAutomaticDefaultCritsect = false;
pCritSect->fUsedByTimerOrSimilar = false;
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLock, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZLock", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZUnlock,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionRZUnlock", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionR3, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSects/%s/ContentionR3", pCritSect->pszName);
#ifdef VBOX_WITH_STATISTICS
STAMR3RegisterF(pVM, &pCritSect->StatLocked, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, NULL, "/PDM/CritSects/%s/Locked", pCritSect->pszName);
#endif
return VINF_SUCCESS;
}
}
else
}
return rc;
}
/**
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pvKey The owner key.
* @param RT_SRC_POS_DECL The source position.
* @param pszName The name of the critical section (for statistics).
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param va Arguments for the format string.
*/
static int pdmR3CritSectRwInitOne(PVM pVM, PPDMCRITSECTRWINT pCritSect, void *pvKey, RT_SRC_POS_DECL,
{
/*
* Allocate the semaphores.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/* Only format the name once. */
if (pszName)
{
#ifdef PDMCRITSECTRW_STRICT
# ifdef RT_LOCK_STRICT_ORDER
# else
# endif
rc = RTLockValidatorRecExclCreate(&pCritSect->Core.pValidatorWrite, hClass, RTLOCKVAL_SUB_CLASS_NONE,
if (RT_SUCCESS(rc))
rc = RTLockValidatorRecSharedCreate(&pCritSect->Core.pValidatorRead, hClass, RTLOCKVAL_SUB_CLASS_NONE,
#endif
if (RT_SUCCESS(rc))
{
/*
* Initialize the structure (first bit is c&p from RTCritSectRwInitEx).
*/
#if HC_ARCH_BITS == 32
#endif
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZEnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZEnterExcl", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLeaveExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZLeaveExcl", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZEnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZEnterShared", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionRZLeaveShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionRZLeaveShared", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionR3EnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionR3EnterExcl", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatContentionR3EnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/ContentionR3EnterShared", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatRZEnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/RZEnterExcl", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatRZEnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/RZEnterShared", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatR3EnterExcl, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/R3EnterExcl", pCritSect->pszName);
STAMR3RegisterF(pVM, &pCritSect->StatR3EnterShared, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, NULL, "/PDM/CritSectsRw/%s/R3EnterShared", pCritSect->pszName);
#ifdef VBOX_WITH_STATISTICS
STAMR3RegisterF(pVM, &pCritSect->StatWriteLocked, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, NULL, "/PDM/CritSectsRw/%s/WriteLocked", pCritSect->pszName);
#endif
return VINF_SUCCESS;
}
}
else
}
}
return rc;
}
/**
* Initializes a PDM critical section for internal use.
*
* The PDM critical sections are derived from the IPRT critical sections, but
* works in ring-0 and raw-mode context as well.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDevIns Device instance.
* @param pCritSect Pointer to the critical section.
* @param RT_SRC_POS_DECL Use RT_SRC_POS.
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param ... Arguments for the format string.
* @thread EMT
*/
VMMR3DECL(int) PDMR3CritSectInit(PVM pVM, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL, const char *pszNameFmt, ...)
{
#endif
return rc;
}
/**
*
* critical sections, but works in ring-0 and raw-mode context as well.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDevIns Device instance.
* @param RT_SRC_POS_DECL Use RT_SRC_POS.
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param ... Arguments for the format string.
* @thread EMT
*/
VMMR3DECL(int) PDMR3CritSectRwInit(PVM pVM, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL, const char *pszNameFmt, ...)
{
#endif
return rc;
}
/**
* Initializes a PDM critical section for a device.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDevIns Device instance.
* @param pCritSect Pointer to the critical section.
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param va Arguments for the format string.
*/
{
}
/**
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDevIns Device instance.
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param va Arguments for the format string.
*/
int pdmR3CritSectRwInitDevice(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL,
{
}
/**
* Initializes the automatic default PDM critical section for a device.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDevIns Device instance.
* @param pCritSect Pointer to the critical section.
*/
int pdmR3CritSectInitDeviceAuto(PVM pVM, PPDMDEVINS pDevIns, PPDMCRITSECT pCritSect, RT_SRC_POS_DECL,
const char *pszNameFmt, ...)
{
if (RT_SUCCESS(rc))
pCritSect->s.fAutomaticDefaultCritsect = true;
return rc;
}
/**
* Initializes a PDM critical section for a driver.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDrvIns Driver instance.
* @param pCritSect Pointer to the critical section.
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param ... Arguments for the format string.
*/
const char *pszNameFmt, ...)
{
return rc;
}
/**
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDrvIns Driver instance.
* @param pszNameFmt Format string for naming the critical section. For
* statistics and lock validation.
* @param ... Arguments for the format string.
*/
int pdmR3CritSectRwInitDriver(PVM pVM, PPDMDRVINS pDrvIns, PPDMCRITSECTRW pCritSect, RT_SRC_POS_DECL,
const char *pszNameFmt, ...)
{
return rc;
}
/**
* Deletes one critical section.
*
* @returns Return code from RTCritSectDelete.
*
* @param pVM Pointer to the VM.
* @param pCritSect The critical section.
* @param pPrev The previous critical section in the list.
* @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
*
* @remarks Caller must have entered the ListCritSect.
*/
static int pdmR3CritSectDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTINT pCritSect, PPDMCRITSECTINT pPrev, bool fFinal)
{
/*
* Assert free waiters and so on (c&p from RTCritSectDelete).
*/
/*
* Unlink it.
*/
if (pPrev)
else
/*
* Delete it (parts taken from RTCritSectDelete).
* In case someone is waiting we'll signal the semaphore cLockers + 1 times.
*/
if (!fFinal)
return rc;
}
/**
*
* @returns VBox status code.
*
* @param pVM Pointer to the VM.
* @param pPrev The previous critical section in the list.
* @param fFinal Set if this is the final call and statistics shouldn't be deregistered.
*
* @remarks Caller must have entered the ListCritSect.
*/
static int pdmR3CritSectRwDeleteOne(PVM pVM, PUVM pUVM, PPDMCRITSECTRWINT pCritSect, PPDMCRITSECTRWINT pPrev, bool fFinal)
{
/*
* Assert free waiters and so on (c&p from RTCritSectRwDelete).
*/
//Assert(pCritSect->Core.cNestings == 0);
//Assert(pCritSect->Core.cLockers == -1);
/*
* Invalidate the structure and free the semaphores.
*/
AssertFailed();
/*
* Unlink it.
*/
if (pPrev)
else
/*
* Delete it (parts taken from RTCritSectRwDelete).
* In case someone is waiting we'll signal the semaphore cLockers + 1 times.
*/
if (!fFinal)
}
/**
* Deletes all critical sections with a give initializer key.
*
* @returns VBox status code.
* The entire list is processed on failure, so we'll only
* return the first error code. This shouldn't be a problem
* since errors really shouldn't happen here.
* @param pVM Pointer to the VM.
* @param pvKey The initializer key.
*/
{
/*
* Iterate the list and match key.
*/
int rc = VINF_SUCCESS;
while (pCur)
{
{
}
/* next */
}
return rc;
}
/**
*
* @returns VBox status code.
* The entire list is processed on failure, so we'll only
* return the first error code. This shouldn't be a problem
* since errors really shouldn't happen here.
* @param pVM Pointer to the VM.
* @param pvKey The initializer key.
*/
{
/*
* Iterate the list and match key.
*/
int rc = VINF_SUCCESS;
while (pCur)
{
{
}
/* next */
}
return rc;
}
/**
* Deletes all undeleted critical sections (both types) initialized by a given
* device.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDevIns The device handle.
*/
{
}
/**
* Deletes all undeleted critical sections (both types) initialized by a given
* driver.
*
* @returns VBox status code.
* @param pVM Pointer to the VM.
* @param pDrvIns The driver handle.
*/
{
}
/**
* Deletes the critical section.
*
* @returns VBox status code.
* @param pCritSect The PDM critical section to destroy.
*/
{
return VINF_SUCCESS;
/*
* Find and unlink it.
*/
while (pCur)
{
{
return rc;
}
/* next */
}
return VERR_PDM_CRITSECT_NOT_FOUND;
}
/**
*
* @returns VBox status code.
*/
{
return VINF_SUCCESS;
/*
* Find and unlink it.
*/
while (pCur)
{
{
return rc;
}
/* next */
}
return VERR_PDM_CRITSECT_NOT_FOUND;
}
/**
* Gets the name of the critical section.
*
*
* @returns Pointer to the critical section name (read only) on success,
* NULL on failure (invalid critical section).
* @param pCritSect The critical section.
*/
{
}
/**
*
*
* @returns Pointer to the critical section name (read only) on success,
* NULL on failure (invalid critical section).
*/
{
}
/**
* Yield the critical section if someone is waiting on it.
*
* When yielding, we'll leave the critical section and try to make sure the
* other waiting threads get a chance of entering before we reclaim it.
*
* @retval true if yielded.
* @retval false if not yielded.
* @param pCritSect The critical section.
*/
{
AssertPtrReturn(pCritSect, false);
/* No recursion allowed here. */
return false;
#ifdef PDMCRITSECT_STRICT
#endif
/*
* If we're lucky, then one of the waiters has entered the lock already.
* We spin a little bit in hope for this to happen so we can avoid the
* yield detour.
*/
{
int cLoops = 20;
while ( cLoops > 0
{
ASMNopPause();
cLoops--;
}
if (cLoops == 0)
}
#ifdef PDMCRITSECT_STRICT
#else
#endif
return true;
}
/**
* Schedule a event semaphore for signalling upon critsect exit.
*
* @returns VINF_SUCCESS on success.
* @returns VERR_TOO_MANY_SEMAPHORES if an event was already scheduled.
* @returns VERR_NOT_OWNER if we're not the critsect owner.
* @returns VERR_SEM_DESTROYED if RTCritSectDelete was called while waiting.
*
* @param pCritSect The critical section.
* @param EventToSignal The semaphore that should be signalled.
*/
{
return VERR_NOT_OWNER;
{
return VINF_SUCCESS;
}
return VERR_TOO_MANY_SEMAPHORES;
}
/**
* PDMR3CritSectBothCountOwned worker.
*
* @param pszName The critical section name.
* @param ppszNames Pointer to the pszNames variable.
* @param pcchLeft Pointer to the cchLeft variable.
* @param fFirst Whether this is the first name or not.
*/
static void pdmR3CritSectAppendNameToList(char const *pszName, char **ppszNames, size_t *pcchLeft, bool fFirst)
{
if (cchLeft)
{
/* try add comma. */
if (fFirst)
{
*pszNames++ = ',';
if (--cchLeft)
{
*pszNames++ = ' ';
cchLeft--;
}
}
/* try copy the name. */
if (cchLeft)
{
{
}
else
{
if (cchLeft > 2)
{
cchLeft = 2;
}
while (cchLeft-- > 0)
*pszNames++ = '+';
}
}
*pszNames = '\0';
}
}
/**
* Counts the critical sections (both type) owned by the calling thread,
* optionally returning a comma separated list naming them.
*
* Read ownerships are not included in non-strict builds.
*
* This is for diagnostic purposes only.
*
* @returns Lock count.
*
* @param pVM Pointer to the VM.
* @param pszNames Where to return the critical section names.
* @param cbNames The size of the buffer.
*/
{
/*
* Init the name buffer.
*/
if (cchLeft)
{
cchLeft--;
}
/*
* Iterate the critical sections.
*/
uint32_t cCritSects = 0;
/* This is unsafe, but wtf. */
pCur;
{
/* Same as RTCritSectIsOwner(). */
{
cCritSects++;
}
}
/* This is unsafe, but wtf. */
pCur;
{
{
cCritSects++;
}
}
return cCritSects;
}
/**
* Leave all critical sections the calling thread owns.
*
* This is only used when entering guru meditation in order to prevent other
* EMTs and I/O threads from deadlocking.
*
* @param pVM Pointer to the VM.
*/
{
pCur;
{
}
}
/**
* Gets the address of the NOP critical section.
*
* The NOP critical section will not perform any thread serialization but let
* all enter immediately and concurrently.
*
* @returns The address of the NOP critical section.
* @param pVM Pointer to the VM.
*/
{
}
/**
* Gets the ring-0 address of the NOP critical section.
*
* @returns The ring-0 address of the NOP critical section.
* @param pVM Pointer to the VM.
*/
{
}
/**
* Gets the raw-mode context address of the NOP critical section.
*
* @returns The raw-mode context address of the NOP critical section.
* @param pVM Pointer to the VM.
*/
{
}