1*1b191cb5SApple OSS Distributions /* 2*1b191cb5SApple OSS Distributions * Copyright (c) 1998-2019 Apple Inc. All rights reserved. 3*1b191cb5SApple OSS Distributions * 4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*1b191cb5SApple OSS Distributions * 6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*1b191cb5SApple OSS Distributions * 15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at 16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*1b191cb5SApple OSS Distributions * 18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are 19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and 24*1b191cb5SApple OSS Distributions * limitations under the License. 25*1b191cb5SApple OSS Distributions * 26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*1b191cb5SApple OSS Distributions */ 28*1b191cb5SApple OSS Distributions /*[ 29*1b191cb5SApple OSS Distributions * 1999-8-10 Godfrey van der Linden(gvdl) 30*1b191cb5SApple OSS Distributions * Created. 31*1b191cb5SApple OSS Distributions * ]*/ 32*1b191cb5SApple OSS Distributions /*! @language embedded-c++ */ 33*1b191cb5SApple OSS Distributions 34*1b191cb5SApple OSS Distributions #ifndef _IOKIT_IOCOMMANDGATE_H 35*1b191cb5SApple OSS Distributions #define _IOKIT_IOCOMMANDGATE_H 36*1b191cb5SApple OSS Distributions 37*1b191cb5SApple OSS Distributions #include <IOKit/IOEventSource.h> 38*1b191cb5SApple OSS Distributions #include <libkern/c++/OSPtr.h> 39*1b191cb5SApple OSS Distributions 40*1b191cb5SApple OSS Distributions /*! 41*1b191cb5SApple OSS Distributions * @class IOCommandGate : public IOEventSource 42*1b191cb5SApple OSS Distributions * @abstract Single-threaded work loop client request mechanism. 43*1b191cb5SApple OSS Distributions * @discussion An IOCommandGate instance is an extremely lightweight mechanism 44*1b191cb5SApple OSS Distributions * that executes an action on the driver's work loop. Although the code does not 45*1b191cb5SApple OSS Distributions * technically execute on the work loop itself, a single-threaded work loop semantic 46*1b191cb5SApple OSS Distributions * is maintained for this event source using the work loop gate. The command gate 47*1b191cb5SApple OSS Distributions * tests for a potential self dead lock by checking if the runCommand request is 48*1b191cb5SApple OSS Distributions * made from the work loop's thread, it doesn't check for a mutual dead lock though 49*1b191cb5SApple OSS Distributions * where a pair of work loop's dead lock each other. 50*1b191cb5SApple OSS Distributions * <br><br> 51*1b191cb5SApple OSS Distributions * The IOCommandGate is a lighter weight version of the IOCommandQueue and 52*1b191cb5SApple OSS Distributions * should be used in preference. Generally use a command queue whenever you need a 53*1b191cb5SApple OSS Distributions * client to submit a request to a work loop. A typical command gate action would 54*1b191cb5SApple OSS Distributions * check if the hardware is active, if so it will add the request to a pending 55*1b191cb5SApple OSS Distributions * queue internal to the device or the device's family. Otherwise if the hardware 56*1b191cb5SApple OSS Distributions * is inactive then this request can be acted upon immediately. 57*1b191cb5SApple OSS Distributions * <br><br> 58*1b191cb5SApple OSS Distributions * CAUTION: The runAction, runCommand, and attemptCommand functions cannot be called from an interrupt context. 59*1b191cb5SApple OSS Distributions * 60*1b191cb5SApple OSS Distributions */ 61*1b191cb5SApple OSS Distributions class IOCommandGate : public IOEventSource 62*1b191cb5SApple OSS Distributions { 63*1b191cb5SApple OSS Distributions OSDeclareDefaultStructors(IOCommandGate); 64*1b191cb5SApple OSS Distributions 65*1b191cb5SApple OSS Distributions public: 66*1b191cb5SApple OSS Distributions /*! 67*1b191cb5SApple OSS Distributions * @typedef Action 68*1b191cb5SApple OSS Distributions * @discussion Type and arguments of callout C function that is used when 69*1b191cb5SApple OSS Distributions * a runCommand is executed by a client. Cast to this type when you want a C++ 70*1b191cb5SApple OSS Distributions * member function to be used. Note the arg1 - arg3 parameters are straight pass 71*1b191cb5SApple OSS Distributions * through from the runCommand to the action callout. 72*1b191cb5SApple OSS Distributions * @param owner 73*1b191cb5SApple OSS Distributions * Target of the function, can be used as a refcon. The owner is set 74*1b191cb5SApple OSS Distributions * during initialisation of the IOCommandGate instance. Note if a C++ function 75*1b191cb5SApple OSS Distributions * was specified this parameter is implicitly the first paramter in the target 76*1b191cb5SApple OSS Distributions * member function's parameter list. 77*1b191cb5SApple OSS Distributions * @param arg0 Argument to action from run operation. 78*1b191cb5SApple OSS Distributions * @param arg1 Argument to action from run operation. 79*1b191cb5SApple OSS Distributions * @param arg2 Argument to action from run operation. 80*1b191cb5SApple OSS Distributions * @param arg3 Argument to action from run operation. 81*1b191cb5SApple OSS Distributions */ 82*1b191cb5SApple OSS Distributions typedef IOReturn (*Action)(OSObject *owner, 83*1b191cb5SApple OSS Distributions void *arg0, void *arg1, 84*1b191cb5SApple OSS Distributions void *arg2, void *arg3); 85*1b191cb5SApple OSS Distributions 86*1b191cb5SApple OSS Distributions protected: 87*1b191cb5SApple OSS Distributions 88*1b191cb5SApple OSS Distributions /*! @struct ExpansionData 89*1b191cb5SApple OSS Distributions * @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future. 90*1b191cb5SApple OSS Distributions */ 91*1b191cb5SApple OSS Distributions struct ExpansionData { }; 92*1b191cb5SApple OSS Distributions 93*1b191cb5SApple OSS Distributions /*! @var reserved 94*1b191cb5SApple OSS Distributions * Reserved for future use. (Internal use only) */ 95*1b191cb5SApple OSS Distributions APPLE_KEXT_WSHADOW_PUSH; 96*1b191cb5SApple OSS Distributions ExpansionData *reserved; 97*1b191cb5SApple OSS Distributions APPLE_KEXT_WSHADOW_POP; 98*1b191cb5SApple OSS Distributions 99*1b191cb5SApple OSS Distributions public: 100*1b191cb5SApple OSS Distributions /*! @function commandGate 101*1b191cb5SApple OSS Distributions * @abstract Factory method to create and initialise an IOCommandGate, See $link init. 102*1b191cb5SApple OSS Distributions * @result Returns a pointer to the new command gate if sucessful, 0 otherwise. */ 103*1b191cb5SApple OSS Distributions static OSPtr<IOCommandGate> commandGate(OSObject *owner, Action action = NULL); 104*1b191cb5SApple OSS Distributions 105*1b191cb5SApple OSS Distributions /*! @function init 106*1b191cb5SApple OSS Distributions * @abstract Class initialiser. 107*1b191cb5SApple OSS Distributions * @discussion Initialiser for IOCommandGate operates only on newly 'newed' 108*1b191cb5SApple OSS Distributions * objects. Shouldn't be used to re-init an existing instance. 109*1b191cb5SApple OSS Distributions * @param owner Owner of this, newly created, instance of the IOCommandGate. This argument will be used as the first parameter in the action callout. 110*1b191cb5SApple OSS Distributions * @param action 111*1b191cb5SApple OSS Distributions * Pointer to a C function that is called whenever a client of the 112*1b191cb5SApple OSS Distributions * IOCommandGate calls runCommand. NB Can be a C++ member function but caller 113*1b191cb5SApple OSS Distributions * must cast the member function to $link IOCommandGate::Action and they will get a 114*1b191cb5SApple OSS Distributions * compiler warning. Defaults to zero, see $link IOEventSource::setAction. 115*1b191cb5SApple OSS Distributions * @result True if inherited classes initialise successfully. */ 116*1b191cb5SApple OSS Distributions virtual bool init(OSObject *owner, Action action = NULL); 117*1b191cb5SApple OSS Distributions 118*1b191cb5SApple OSS Distributions // Superclass overrides 119*1b191cb5SApple OSS Distributions virtual void free() APPLE_KEXT_OVERRIDE; 120*1b191cb5SApple OSS Distributions virtual void setWorkLoop(IOWorkLoop *inWorkLoop) APPLE_KEXT_OVERRIDE; 121*1b191cb5SApple OSS Distributions 122*1b191cb5SApple OSS Distributions /*! @function runCommand 123*1b191cb5SApple OSS Distributions * @abstract Single thread a command with the target work loop. 124*1b191cb5SApple OSS Distributions * @discussion Client function that causes the current action to be called in 125*1b191cb5SApple OSS Distributions * a single threaded manner. Beware the work loop's gate is recursive and command 126*1b191cb5SApple OSS Distributions * gates can cause direct or indirect re-entrancy. When the executing on a 127*1b191cb5SApple OSS Distributions * client's thread runCommand will sleep until the work loop's gate opens for 128*1b191cb5SApple OSS Distributions * execution of client actions, the action is single threaded against all other 129*1b191cb5SApple OSS Distributions * work loop event sources. If the command is disabled the attempt to run a command will be stalled until enable is called. 130*1b191cb5SApple OSS Distributions * @param arg0 Parameter for action of command gate, defaults to 0. 131*1b191cb5SApple OSS Distributions * @param arg1 Parameter for action of command gate, defaults to 0. 132*1b191cb5SApple OSS Distributions * @param arg2 Parameter for action of command gate, defaults to 0. 133*1b191cb5SApple OSS Distributions * @param arg3 Parameter for action of command gate, defaults to 0. 134*1b191cb5SApple OSS Distributions * @result kIOReturnSuccess if successful. kIOReturnAborted if a disabled command gate is free()ed before being reenabled, kIOReturnNoResources if no action available. 135*1b191cb5SApple OSS Distributions */ 136*1b191cb5SApple OSS Distributions virtual IOReturn runCommand(void *arg0 = NULL, void *arg1 = NULL, 137*1b191cb5SApple OSS Distributions void *arg2 = NULL, void *arg3 = NULL); 138*1b191cb5SApple OSS Distributions 139*1b191cb5SApple OSS Distributions /*! @function runAction 140*1b191cb5SApple OSS Distributions * @abstract Single thread a call to an action with the target work loop. 141*1b191cb5SApple OSS Distributions * @discussion Client function that causes the given action to be called in 142*1b191cb5SApple OSS Distributions * a single threaded manner. Beware the work loop's gate is recursive and command 143*1b191cb5SApple OSS Distributions * gates can cause direct or indirect re-entrancy. When the executing on a 144*1b191cb5SApple OSS Distributions * client's thread runAction will sleep until the work loop's gate opens for 145*1b191cb5SApple OSS Distributions * execution of client actions, the action is single threaded against all other 146*1b191cb5SApple OSS Distributions * work loop event sources. If the command is disabled the attempt to run a command will be stalled until enable is called. 147*1b191cb5SApple OSS Distributions * @param action Pointer to function to be executed in the context of the work loop. 148*1b191cb5SApple OSS Distributions * @param arg0 Parameter for action parameter, defaults to 0. 149*1b191cb5SApple OSS Distributions * @param arg1 Parameter for action parameter, defaults to 0. 150*1b191cb5SApple OSS Distributions * @param arg2 Parameter for action parameter, defaults to 0. 151*1b191cb5SApple OSS Distributions * @param arg3 Parameter for action parameter, defaults to 0. 152*1b191cb5SApple OSS Distributions * @result The return value of action if it was called, kIOReturnBadArgument if action is not defined, kIOReturnAborted if a disabled command gate is free()ed before being reenabled. 153*1b191cb5SApple OSS Distributions */ 154*1b191cb5SApple OSS Distributions virtual IOReturn runAction(Action action, 155*1b191cb5SApple OSS Distributions void *arg0 = NULL, void *arg1 = NULL, 156*1b191cb5SApple OSS Distributions void *arg2 = NULL, void *arg3 = NULL); 157*1b191cb5SApple OSS Distributions 158*1b191cb5SApple OSS Distributions #ifdef __BLOCKS__ 159*1b191cb5SApple OSS Distributions /*! @function runActionBlock 160*1b191cb5SApple OSS Distributions * @abstract Single thread a call to an action with the target work loop. 161*1b191cb5SApple OSS Distributions * @discussion Client function that causes the given action to be called in 162*1b191cb5SApple OSS Distributions * a single threaded manner. Beware the work loop's gate is recursive and command 163*1b191cb5SApple OSS Distributions * gates can cause direct or indirect re-entrancy. When the executing on a 164*1b191cb5SApple OSS Distributions * client's thread runAction will sleep until the work loop's gate opens for 165*1b191cb5SApple OSS Distributions * execution of client actions, the action is single threaded against all other 166*1b191cb5SApple OSS Distributions * work loop event sources. If the command is disabled the attempt to run a command will be stalled until enable is called. 167*1b191cb5SApple OSS Distributions * @param action Block to be executed in the context of the work loop. 168*1b191cb5SApple OSS Distributions * @result The return value of action if it was called, kIOReturnBadArgument if action is not defined, kIOReturnAborted if a disabled command gate is free()ed before being reenabled. 169*1b191cb5SApple OSS Distributions */ 170*1b191cb5SApple OSS Distributions IOReturn runActionBlock(ActionBlock action); 171*1b191cb5SApple OSS Distributions #endif /* __BLOCKS__ */ 172*1b191cb5SApple OSS Distributions 173*1b191cb5SApple OSS Distributions /*! @function attemptCommand 174*1b191cb5SApple OSS Distributions * @abstract Single thread a command with the target work loop. 175*1b191cb5SApple OSS Distributions * @discussion Client function that causes the current action to be called in 176*1b191cb5SApple OSS Distributions * a single threaded manner. When the executing on a client's thread attemptCommand will fail if the work loop's gate is closed. 177*1b191cb5SApple OSS Distributions * @param arg0 Parameter for action of command gate, defaults to 0. 178*1b191cb5SApple OSS Distributions * @param arg1 Parameter for action of command gate, defaults to 0. 179*1b191cb5SApple OSS Distributions * @param arg2 Parameter for action of command gate, defaults to 0. 180*1b191cb5SApple OSS Distributions * @param arg3 Parameter for action of command gate, defaults to 0. 181*1b191cb5SApple OSS Distributions * @result kIOReturnSuccess if successful. kIOReturnNotPermitted if this event source is currently disabled, kIOReturnNoResources if no action available, kIOReturnCannotLock if lock attempt fails. 182*1b191cb5SApple OSS Distributions */ 183*1b191cb5SApple OSS Distributions virtual IOReturn attemptCommand(void *arg0 = NULL, void *arg1 = NULL, 184*1b191cb5SApple OSS Distributions void *arg2 = NULL, void *arg3 = NULL); 185*1b191cb5SApple OSS Distributions 186*1b191cb5SApple OSS Distributions /*! @function attemptAction 187*1b191cb5SApple OSS Distributions * @abstract Single thread a call to an action with the target work loop. 188*1b191cb5SApple OSS Distributions * @discussion Client function that causes the given action to be called in 189*1b191cb5SApple OSS Distributions * a single threaded manner. Beware the work loop's gate is recursive and command 190*1b191cb5SApple OSS Distributions * gates can cause direct or indirect re-entrancy. When the executing on a 191*1b191cb5SApple OSS Distributions * client's thread attemptCommand will fail if the work loop's gate is closed. 192*1b191cb5SApple OSS Distributions * @param action Pointer to function to be executed in context of the work loop. 193*1b191cb5SApple OSS Distributions * @param arg0 Parameter for action parameter, defaults to 0. 194*1b191cb5SApple OSS Distributions * @param arg1 Parameter for action parameter, defaults to 0. 195*1b191cb5SApple OSS Distributions * @param arg2 Parameter for action parameter, defaults to 0. 196*1b191cb5SApple OSS Distributions * @param arg3 Parameter for action parameter, defaults to 0. 197*1b191cb5SApple OSS Distributions * @result kIOReturnSuccess if successful. kIOReturnBadArgument if action is not defined, kIOReturnNotPermitted if this event source is currently disabled, kIOReturnCannotLock if lock attempt fails. 198*1b191cb5SApple OSS Distributions * 199*1b191cb5SApple OSS Distributions */ 200*1b191cb5SApple OSS Distributions virtual IOReturn attemptAction(Action action, 201*1b191cb5SApple OSS Distributions void *arg0 = NULL, void *arg1 = NULL, 202*1b191cb5SApple OSS Distributions void *arg2 = NULL, void *arg3 = NULL); 203*1b191cb5SApple OSS Distributions 204*1b191cb5SApple OSS Distributions /*! @function commandSleep 205*1b191cb5SApple OSS Distributions * @abstract Put a thread that is currently holding the command gate to sleep. 206*1b191cb5SApple OSS Distributions * @discussion Put a thread to sleep waiting for an event but release the gate first. If the event occurs then the commandGate is closed before the function returns. If the thread does not hold the gate, panic. 207*1b191cb5SApple OSS Distributions * @param event Pointer to an address. 208*1b191cb5SApple OSS Distributions * @param interruptible THREAD_UNINT, THREAD_INTERRUPTIBLE or THREAD_ABORTSAFE. THREAD_UNINT specifies that the sleep cannot be interrupted by a signal. THREAD_INTERRUPTIBLE specifies that the sleep may be interrupted by a "kill -9" signal. THREAD_ABORTSAFE (the default value) specifies that the sleep may be interrupted by any user signal. 209*1b191cb5SApple OSS Distributions * @result THREAD_AWAKENED - normal wakeup, THREAD_TIMED_OUT - timeout expired, THREAD_INTERRUPTED - interrupted, THREAD_RESTART - restart operation entirely. */ 210*1b191cb5SApple OSS Distributions virtual IOReturn commandSleep(void *event, 211*1b191cb5SApple OSS Distributions UInt32 interruptible = THREAD_ABORTSAFE); 212*1b191cb5SApple OSS Distributions 213*1b191cb5SApple OSS Distributions /*! @function commandWakeup 214*1b191cb5SApple OSS Distributions * @abstract Wakeup one or more threads that are asleep on an event. 215*1b191cb5SApple OSS Distributions * @param event Pointer to an address. 216*1b191cb5SApple OSS Distributions * @param oneThread true to only wake up at most one thread, false otherwise. */ 217*1b191cb5SApple OSS Distributions virtual void commandWakeup(void *event, bool oneThread = false); 218*1b191cb5SApple OSS Distributions 219*1b191cb5SApple OSS Distributions /*! @function disable 220*1b191cb5SApple OSS Distributions * @abstract Disable the command gate 221*1b191cb5SApple OSS Distributions * @discussion When a command gate is disabled all future calls to runAction and runCommand will stall until the gate is enable()d later. This can be used to block client threads when a system sleep is requested. The IOWorkLoop thread itself will never stall, even when making runAction/runCommand calls. This call must be made from a gated context, to clear potential race conditions. */ 222*1b191cb5SApple OSS Distributions virtual void disable() APPLE_KEXT_OVERRIDE; 223*1b191cb5SApple OSS Distributions 224*1b191cb5SApple OSS Distributions /*! @function enable 225*1b191cb5SApple OSS Distributions * @abstract Enable command gate, this will unblock any blocked Commands and Actions. 226*1b191cb5SApple OSS Distributions * @discussion Enable the command gate. The attemptAction/attemptCommand calls will now be enabled and can succeeed. Stalled runCommand/runAction calls will be woken up. */ 227*1b191cb5SApple OSS Distributions virtual void enable() APPLE_KEXT_OVERRIDE; 228*1b191cb5SApple OSS Distributions 229*1b191cb5SApple OSS Distributions /*! @function commandSleep 230*1b191cb5SApple OSS Distributions * @abstract Put a thread that is currently holding the command gate to sleep. 231*1b191cb5SApple OSS Distributions * @discussion Put a thread to sleep waiting for an event but release the gate first. If the event occurs or timeout occurs then the commandGate is closed before the function returns. If the thread does not hold the gate, panic. 232*1b191cb5SApple OSS Distributions * @param event Pointer to an address. 233*1b191cb5SApple OSS Distributions * @param deadline Clock deadline to timeout the sleep. 234*1b191cb5SApple OSS Distributions * @param interruptible THREAD_UNINT, THREAD_INTERRUPTIBLE or THREAD_ABORTSAFE. THREAD_UNINT specifies that the sleep cannot be interrupted by a signal. THREAD_INTERRUPTIBLE specifies that the sleep may be interrupted by a "kill -9" signal. THREAD_ABORTSAFE specifies that the sleep may be interrupted by any user signal. 235*1b191cb5SApple OSS Distributions * @result THREAD_AWAKENED - normal wakeup, THREAD_TIMED_OUT - timeout expired, THREAD_INTERRUPTED - interrupted, THREAD_RESTART - restart operation entirely. */ 236*1b191cb5SApple OSS Distributions virtual IOReturn commandSleep(void *event, 237*1b191cb5SApple OSS Distributions AbsoluteTime deadline, 238*1b191cb5SApple OSS Distributions UInt32 interruptible); 239*1b191cb5SApple OSS Distributions 240*1b191cb5SApple OSS Distributions private: 241*1b191cb5SApple OSS Distributions #if __LP64__ 242*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 0); 243*1b191cb5SApple OSS Distributions #else 244*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUsedX86(IOCommandGate, 0); 245*1b191cb5SApple OSS Distributions #endif 246*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 1); 247*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 2); 248*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 3); 249*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 4); 250*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 5); 251*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 6); 252*1b191cb5SApple OSS Distributions OSMetaClassDeclareReservedUnused(IOCommandGate, 7); 253*1b191cb5SApple OSS Distributions }; 254*1b191cb5SApple OSS Distributions 255*1b191cb5SApple OSS Distributions #endif /* !_IOKIT_IOCOMMANDGATE_H */ 256