#include <libkern/OSDebug.h>
#include <IOKit/IOCommandGate.h>
#include <IOKit/IOWorkLoop.h>
#include <IOKit/IOReturn.h>
#include <IOKit/IOTimeStamp.h>
#include <IOKit/IOKitDebug.h>
#define super IOEventSource
OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource)
#if __LP64__
OSMetaClassDefineReservedUnused(IOCommandGate, 0);
#else
OSMetaClassDefineReservedUsed(IOCommandGate, 0);
#endif
OSMetaClassDefineReservedUnused(IOCommandGate, 1);
OSMetaClassDefineReservedUnused(IOCommandGate, 2);
OSMetaClassDefineReservedUnused(IOCommandGate, 3);
OSMetaClassDefineReservedUnused(IOCommandGate, 4);
OSMetaClassDefineReservedUnused(IOCommandGate, 5);
OSMetaClassDefineReservedUnused(IOCommandGate, 6);
OSMetaClassDefineReservedUnused(IOCommandGate, 7);
#if IOKITSTATS
#define IOStatisticsInitializeCounter() \
do { \
IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsCommandGateCounter); \
} while (0)
#define IOStatisticsActionCall() \
do { \
IOStatistics::countCommandGateActionCall(IOEventSource::reserved->counter); \
} while (0)
#else
#define IOStatisticsInitializeCounter()
#define IOStatisticsActionCall()
#endif
bool IOCommandGate::init(OSObject *inOwner, Action inAction)
{
bool res = super::init(inOwner, (IOEventSource::Action) inAction);
if (res) {
IOStatisticsInitializeCounter();
}
return res;
}
IOCommandGate *
IOCommandGate::commandGate(OSObject *inOwner, Action inAction)
{
IOCommandGate *me = new IOCommandGate;
if (me && !me->init(inOwner, inAction)) {
me->release();
return 0;
}
return me;
}
void IOCommandGate::disable()
{
if (workLoop && !workLoop->inGate())
OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
super::disable();
}
void IOCommandGate::enable()
{
if (workLoop) {
closeGate();
super::enable();
wakeupGate(&enabled, false); openGate();
}
}
void IOCommandGate::free()
{
if (workLoop) setWorkLoop(0);
super::free();
}
enum
{
kSleepersRemoved = 0x00000001,
kSleepersWaitEnabled = 0x00000002,
kSleepersActions = 0x00000100,
kSleepersActionsMask = 0xffffff00,
};
void IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop)
{
IOWorkLoop * wl;
uintptr_t * sleepersP = (uintptr_t *) &reserved;
bool defer;
if (!inWorkLoop && (wl = workLoop)) { wl->closeGate();
*sleepersP |= kSleepersRemoved;
while (*sleepersP & kSleepersWaitEnabled) {
thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED);
sleepGate(sleepersP, THREAD_UNINT);
}
*sleepersP &= ~kSleepersWaitEnabled;
defer = (0 != (kSleepersActionsMask & *sleepersP));
if (!defer)
{
super::setWorkLoop(0);
*sleepersP &= ~kSleepersRemoved;
}
wl->openGate();
return;
}
super::setWorkLoop(inWorkLoop);
}
IOReturn IOCommandGate::runCommand(void *arg0, void *arg1,
void *arg2, void *arg3)
{
return runAction((Action) action, arg0, arg1, arg2, arg3);
}
IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1,
void *arg2, void *arg3)
{
return attemptAction((Action) action, arg0, arg1, arg2, arg3);
}
IOReturn IOCommandGate::runAction(Action inAction,
void *arg0, void *arg1,
void *arg2, void *arg3)
{
IOWorkLoop * wl;
uintptr_t * sleepersP;
if (!inAction)
return kIOReturnBadArgument;
if (!(wl = workLoop))
return kIOReturnNotReady;
wl->closeGate();
sleepersP = (uintptr_t *) &reserved;
IOReturn res;
if (!wl->onThread())
{
while (!enabled)
{
IOReturn sleepResult = kIOReturnSuccess;
if (workLoop)
{
*sleepersP |= kSleepersWaitEnabled;
sleepResult = wl->sleepGate(&enabled, THREAD_ABORTSAFE);
*sleepersP &= ~kSleepersWaitEnabled;
}
bool wakeupTearDown = (!workLoop || (0 != (*sleepersP & kSleepersRemoved)));
if ((kIOReturnSuccess != sleepResult) || wakeupTearDown) {
wl->openGate();
if (wakeupTearDown)
wl->wakeupGate(sleepersP, false);
return kIOReturnAborted;
}
}
}
bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false;
if (trace) IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
IOStatisticsActionCall();
*sleepersP += kSleepersActions;
res = (*inAction)(owner, arg0, arg1, arg2, arg3);
*sleepersP -= kSleepersActions;
if (trace) IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
if (kSleepersRemoved == ((kSleepersActionsMask|kSleepersRemoved) & *sleepersP))
{
*sleepersP &= ~kSleepersRemoved;
super::setWorkLoop(0);
}
wl->openGate();
return res;
}
IOReturn IOCommandGate::attemptAction(Action inAction,
void *arg0, void *arg1,
void *arg2, void *arg3)
{
IOReturn res;
IOWorkLoop * wl;
if (!inAction)
return kIOReturnBadArgument;
if (!(wl = workLoop))
return kIOReturnNotReady;
if (!wl->tryCloseGate())
return kIOReturnCannotLock;
if (!wl->onThread() && !enabled)
res = kIOReturnNotPermitted;
else {
bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false;
if (trace)
IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
IOStatisticsActionCall();
res = (*inAction)(owner, arg0, arg1, arg2, arg3);
if (trace)
IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
}
wl->openGate();
return res;
}
IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible)
{
if (!workLoop->inGate())
return kIOReturnNotPermitted;
return sleepGate(event, interruptible);
}
IOReturn IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible)
{
if (!workLoop->inGate())
return kIOReturnNotPermitted;
return sleepGate(event, deadline, interruptible);
}
void IOCommandGate::commandWakeup(void *event, bool oneThread)
{
wakeupGate(event, oneThread);
}