]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOWorkLoop.h
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / iokit / IOKit / IOWorkLoop.h
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 1998-2019 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
28
29#ifndef __IOKIT_IOWORKLOOP_H
30#define __IOKIT_IOWORKLOOP_H
31
32#include <libkern/c++/OSObject.h>
33#include <IOKit/IOReturn.h>
34#include <IOKit/IOLib.h>
35#include <IOKit/IOLocks.h>
f427ee49 36#include <libkern/c++/OSPtr.h>
1c79356b
A
37
38#include <IOKit/system.h>
39
6d2010ae
A
40#if IOKITSTATS
41#include <IOKit/IOStatisticsPrivate.h>
42#endif
43
1c79356b 44class IOEventSource;
91447636 45class IOTimerEventSource;
1c79356b
A
46class IOCommandGate;
47
91447636 48/*! @class IOWorkLoop
0a7de745
A
49 * @discussion An IOWorkLoop is a thread of control that is intended to be used to provide single threaded access to hardware. This class has no knowledge of the nature and type of the events that it marshals and forwards. When a device driver successfully starts (see IOService::start), it is expected to create the event sources it will need to receive events. Then a work loop is initialized and the events are added to the work loop for monitoring. In general this set up will be automated by the family superclass of the specific device.
50 * <br><br>
51 * The thread main method walks the event source linked list and messages each one requesting a work check. At this point each event source is expected to notify its registered owner that the event has occurred. After each event has been walked and each indicates that another loop isn't required (by setting the 'more' flag to false) the thread will go to sleep on a signaling semaphore.
52 * <br><br>
53 * When an event source is registered with a work loop it is informed of the semaphore to use to wake up the loop.
54 */
1c79356b
A
55class IOWorkLoop : public OSObject
56{
cb323159 57 OSDeclareDefaultStructors(IOWorkLoop);
1c79356b 58
0b4e3aa0
A
59public:
60/*!
0a7de745
A
61 * @typedef Action
62 * @discussion Type and arguments of callout C function that is used when
63 * a runCommand is executed by a client. Cast to this type when you want a C++
64 * member function to be used. Note the arg1 - arg3 parameters are straight pass
65 * through from the runCommand to the action callout.
66 * @param target
67 * Target of the function, can be used as a refcon. Note if a C++ function
68 * was specified, this parameter is implicitly the first parameter in the target
69 * member function's parameter list.
70 * @param arg0 Argument to action from run operation.
71 * @param arg1 Argument to action from run operation.
72 * @param arg2 Argument to action from run operation.
73 * @param arg3 Argument to action from run operation.
74 */
75 typedef IOReturn (*Action)(OSObject *target,
76 void *arg0, void *arg1,
77 void *arg2, void *arg3);
d9a64523
A
78
79#ifdef __BLOCKS__
0a7de745 80 typedef IOReturn (^ActionBlock)();
d9a64523
A
81#endif /* __BLOCKS__ */
82
0a7de745
A
83 enum {
84 kPreciousStack = 0x00000001,
85 kTimeLockPanics = 0x00000002,
86 };
0b4e3aa0 87
1c79356b 88private:
1c79356b 89/*! @function threadMainContinuation
0a7de745
A
90 * @abstract Static function that calls the threadMain function.
91 */
92 static void threadMainContinuation(IOWorkLoop *self);
93
6d2010ae 94/*! @function eventSourcePerformsWork
0a7de745
A
95 * @abstract Checks if the event source passed in overrides checkForWork() to perform any work.
96 * IOWorkLoop uses this to determine if the event source should be polled in runEventSources() or not.
97 * @param inEventSource The event source to check.
98 */
6d2010ae 99 bool eventSourcePerformsWork(IOEventSource *inEventSource);
0a7de745 100
f427ee49
A
101/*! @function releaseEventChain
102 * @abstract Static function that releases the events in a chain and sets
103 * their work loops to NULL.
104 */
105 static void releaseEventChain(LIBKERN_CONSUMED IOEventSource *eventChain);
106
1c79356b
A
107protected:
108
109/*! @typedef maintCommandEnum
0a7de745
A
110 * @discussion Enumeration of commands that _maintCommand can deal with.
111 * @constant mAddEvent Used to tag a Remove event source command.
112 * @constant mRemoveEvent Used to tag a Remove event source command.
113 */
114 typedef enum { mAddEvent, mRemoveEvent } maintCommandEnum;
1c79356b
A
115
116/*! @var gateLock
0a7de745
A
117 * Mutual exclusion lock that is used by close and open Gate functions.
118 * This is a recursive lock, which allows multiple layers of code to share a single IOWorkLoop without deadlock. This is common in IOKit since threads of execution tend to follow the service plane in the IORegistry, and multiple objects along the call path may acquire the gate for the same (shared) workloop.
119 */
120 IORecursiveLock *gateLock;
1c79356b 121
0a7de745
A
122/*! @var eventChain
123 * Pointer to first event source in linked list.
124 */
125 IOEventSource *eventChain;
1c79356b 126
0a7de745
A
127/*! @var controlG
128 * Internal control gate to maintain event system.
129 */
130 IOCommandGate *controlG;
1c79356b 131
2d21ac55 132/*! @var workToDoLock
0a7de745
A
133 * The spin lock that is used to guard the 'workToDo' variable.
134 */
135 IOSimpleLock *workToDoLock;
1c79356b 136
0a7de745
A
137/*! @var workThread
138 * Work loop thread.
139 */
140 IOThread workThread;
1c79356b
A
141
142/*! @var workToDo
0a7de745
A
143 * Used to to indicate that an interrupt has fired and needs to be processed.
144 */
145 volatile bool workToDo;
1c79356b
A
146
147/*! @var loopRestart
0a7de745
A
148 * Set if an event chain has been changed and the system has to be rechecked from start. (Internal use only)
149 */
150 bool loopRestart;
1c79356b
A
151
152/*! @struct ExpansionData
0a7de745
A
153 * @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
154 */
155 struct ExpansionData {
156 IOOptionBits options;
157 IOEventSource *passiveEventChain;
6d2010ae 158#if IOKITSTATS
0a7de745 159 struct IOWorkLoopCounter *counter;
6d2010ae 160#else
0a7de745 161 void *iokitstatsReserved;
6d2010ae 162#endif
0a7de745
A
163 uint64_t lockInterval;
164 uint64_t lockTime;
165 };
1c79356b
A
166
167/*! @var reserved
0a7de745
A
168 * Reserved for future use. (Internal use only)
169 */
170 ExpansionData *reserved;
1c79356b
A
171
172/*! @function _maintRequest
0a7de745
A
173 * @abstract Synchronous implementation of addEventSource and removeEventSource functions.
174 * @discussion This function implements the commands as defined in the maintCommandEnum. It can be subclassed but it isn't an external API in the usual sense. A subclass implementation of _maintRequest would be called synchronously with respect to the work loop and it should be implemented in the usual way that an ioctl would be.
175 * @return kIOReturnUnsupported if the command given is not implemented, kIOReturnSuccess otherwise.
176 */
177 virtual IOReturn _maintRequest(void *command, void *data, void *, void *);
1c79356b
A
178
179/*! @function free
0a7de745
A
180 * @discussion Mandatory free of the object independent of the current retain count. If the work loop is running, this method will not return until the thread has successfully terminated. Each event source in the chain will be released and the working semaphore will be destroyed.
181 * <br><br>
182 * If the client has some outstanding requests on an event they will never be informed of completion. If an external thread is blocked on any of the event sources they will be awakened with a KERN_INTERUPTED status.
183 */
184 virtual void free() APPLE_KEXT_OVERRIDE;
1c79356b
A
185
186/*! @function threadMain
0a7de745
A
187 * @discussion Work loop threads main function. This function consists of 3
188 * loops: the outermost loop is the semaphore clear and wait loop, the middle
189 * loop terminates when there is no more work, and the inside loop walks the
190 * event list calling the checkForWork method in each event source. If an
191 * event source has more work to do, it can set the more flag and the middle
192 * loop will repeat. When no more work is outstanding the outermost will
193 * sleep until an event is signalled.
194 */
195 virtual void threadMain();
1c79356b
A
196
197public:
198
199/*! @function workLoop
0a7de745
A
200 * @abstract Factory member function to construct and intialize a work loop.
201 * @result Returns a workLoop instance if constructed successfully, 0 otherwise.
202 */
f427ee49 203 static OSPtr<IOWorkLoop> workLoop();
1c79356b 204
2d21ac55 205/*! @function workLoopWithOptions(IOOptionBits options)
0a7de745
A
206 * @abstract Factory member function to constuct and intialize a work loop.
207 * @param options Options - kPreciousStack to avoid stack deallocation on paging path.
208 * @result Returns a workLoop instance if constructed successfully, 0 otherwise.
209 */
f427ee49 210 static OSPtr<IOWorkLoop> workLoopWithOptions(IOOptionBits options);
2d21ac55 211
1c79356b 212/*! @function init
0a7de745
A
213 * @discussion Initializes an instance of the workloop. This method creates and initializes the signaling semaphore, the controller gate lock, and spawns the thread that will continue executing.
214 * @result Returns true if initialized successfully, false otherwise.
215 */
216 virtual bool init() APPLE_KEXT_OVERRIDE;
1c79356b
A
217
218/*! @function getThread
0a7de745
A
219 * @abstract Gets the workThread.
220 * @result Returns workThread.
221 */
222 virtual IOThread getThread() const;
1c79356b
A
223
224/*! @function onThread
0a7de745
A
225 * @abstract Is the current execution context on the work thread?
226 * @result Returns true if IOThreadSelf() == workThread.
227 */
228 virtual bool onThread() const;
1c79356b
A
229
230/*! @function inGate
0a7de745
A
231 * @abstract Is the current execution context holding the work-loop's gate?
232 * @result Returns true if IOThreadSelf() is gate holder.
233 */
234 virtual bool inGate() const;
235
1c79356b 236/*! @function addEventSource
0a7de745
A
237 * @discussion Add an event source to be monitored by the work loop. This function does not return until the work loop has acknowledged the arrival of the new event source. When a new event has been added the threadMain will always restart its loop and check all outstanding events. The event source is retained by the work loop.
238 * @param newEvent Pointer to IOEventSource subclass to add.
239 * @result Always returns kIOReturnSuccess.
240 */
241 virtual IOReturn addEventSource(IOEventSource *newEvent);
1c79356b
A
242
243/*! @function removeEventSource
0a7de745
A
244 * @discussion Remove an event source from the work loop. This function does not return until the work loop has acknowledged the removal of the event source. When an event has been removed the threadMain will always restart its loop and check all outstanding events. The event source will be released before return.
245 * @param toRemove Pointer to IOEventSource subclass to remove.
246 * @result Returns kIOReturnSuccess if successful, kIOReturnBadArgument if toRemove couldn't be found.
247 */
248 virtual IOReturn removeEventSource(IOEventSource *toRemove);
1c79356b
A
249
250/*! @function enableAllEventSources
0a7de745
A
251 * @abstract Calls enable() in all event sources.
252 * @discussion For all event sources in eventChain, call enable() function. See IOEventSource::enable().
253 */
254 virtual void enableAllEventSources() const;
1c79356b
A
255
256/*! @function disableAllEventSources
0a7de745
A
257 * @abstract Calls disable() in all event sources.
258 * @discussion For all event sources in eventChain, call disable() function. See IOEventSource::disable().
259 */
260 virtual void disableAllEventSources() const;
1c79356b
A
261
262/*! @function enableAllInterrupts
0a7de745
A
263 * @abstract Calls enable() in all interrupt event sources.
264 * @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call enable() function. See IOEventSource::enable().
265 */
266 virtual void enableAllInterrupts() const;
1c79356b
A
267
268/*! @function disableAllInterrupts
0a7de745
A
269 * @abstract Calls disable() in all interrupt event sources.
270 * @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call disable() function. See IOEventSource::disable().
271 */
272 virtual void disableAllInterrupts() const;
1c79356b 273
0b4e3aa0 274
1c79356b 275protected:
0a7de745
A
276// Internal APIs used by event sources to control the thread
277 friend class IOEventSource;
278 friend class IOTimerEventSource;
279 friend class IOCommandGate;
6d2010ae 280#if IOKITSTATS
0a7de745 281 friend class IOStatistics;
6d2010ae 282#endif
0a7de745
A
283 virtual void signalWorkAvailable();
284 virtual void openGate();
285 virtual void closeGate();
286 virtual bool tryCloseGate();
287 virtual int sleepGate(void *event, UInt32 interuptibleType);
288 virtual void wakeupGate(void *event, bool oneThread);
1c79356b 289
0b4e3aa0 290public:
0a7de745 291/* methods available in Mac OS X 10.1 or later */
0b4e3aa0
A
292
293/*! @function runAction
0a7de745
A
294 * @abstract Single thread a call to an action with the work-loop.
295 * @discussion Client function that causes the given action to be called in a single threaded manner. Beware: the work-loop's gate is recursive and runAction can cause direct or indirect re-entrancy. When executing on a client's thread, runAction will sleep until the work-loop's gate opens for execution of client actions, the action is single threaded against all other work-loop event sources.
296 * @param action Pointer to function to be executed in work-loop context.
297 * @param arg0 Parameter for action parameter, defaults to 0.
298 * @param arg1 Parameter for action parameter, defaults to 0.
299 * @param arg2 Parameter for action parameter, defaults to 0.
300 * @param arg3 Parameter for action parameter, defaults to 0.
301 * @result Returns the value of the Action callout.
302 */
303 virtual IOReturn runAction(Action action, OSObject *target,
cb323159
A
304 void *arg0 = NULL, void *arg1 = NULL,
305 void *arg2 = NULL, void *arg3 = NULL);
0b4e3aa0 306
d9a64523
A
307#ifdef __BLOCKS__
308/*! @function runAction
0a7de745
A
309 * @abstract Single thread a call to an action with the work-loop.
310 * @discussion Client function that causes the given action to be called in a single threaded manner. Beware: the work-loop's gate is recursive and runAction can cause direct or indirect re-entrancy. When executing on a client's thread, runAction will sleep until the work-loop's gate opens for execution of client actions, the action is single threaded against all other work-loop event sources.
311 * @param action Block to be executed in work-loop context.
312 * @result Returns the result of the action block.
313 */
314 IOReturn runActionBlock(ActionBlock action);
d9a64523
A
315#endif /* __BLOCKS__ */
316
0c530ab8 317/*! @function runEventSources
0a7de745
A
318 * @discussion Consists of the inner 2 loops of the threadMain function(qv).
319 * The outer loop terminates when there is no more work, and the inside loop
320 * walks the event list calling the checkForWork method in each event source.
321 * If an event source has more work to do, it can set the more flag and the
322 * outer loop will repeat.
323 * <br><br>
324 * This function can be used to clear a priority inversion between the normal
325 * workloop thread and multimedia's real time threads. The problem is that
326 * the interrupt action routine is often held off by high priority threads.
327 * So if they want to get their data now they will have to call us and ask if
328 * any data is available. The multi-media user client will arrange for this
329 * function to be called, which causes any pending interrupts to be processed
330 * and the completion routines called. By the time the function returns all
331 * outstanding work will have been completed at the real time threads
332 * priority.
333 *
334 * @result Return false if the work loop is shutting down, true otherwise.
335 */
336 virtual bool runEventSources();
0c530ab8 337
39037602 338/*! @function setMaximumLockTime
0a7de745
A
339 * @discussion For diagnostics use in DEVELOPMENT kernels, set a time interval which if the work loop lock is held for this time or greater, IOWorkLoop will panic or log a backtrace.
340 * @param interval An absolute time interval, eg. created with clock_interval_to_absolutetime_interval().
341 * @param options Pass IOWorkLoop::kTimeLockPanics to panic when the time is exceeded, otherwise a log will be generated with OSReportWithBacktrace().
342 */
343 void setMaximumLockTime(uint64_t interval, uint32_t options);
39037602 344
0b4e3aa0 345protected:
0a7de745
A
346// Internal APIs used by event sources to control the thread
347 virtual int sleepGate(void *event, AbsoluteTime deadline, UInt32 interuptibleType);
0b4e3aa0 348
39037602 349#if XNU_KERNEL_PRIVATE
0a7de745 350 void lockTime(void);
39037602
A
351#endif /* XNU_KERNEL_PRIVATE */
352
b0d623f7
A
353protected:
354#if __LP64__
0a7de745
A
355 OSMetaClassDeclareReservedUnused(IOWorkLoop, 0);
356 OSMetaClassDeclareReservedUnused(IOWorkLoop, 1);
357 OSMetaClassDeclareReservedUnused(IOWorkLoop, 2);
b0d623f7 358#else
f427ee49
A
359 OSMetaClassDeclareReservedUsedX86(IOWorkLoop, 0);
360 OSMetaClassDeclareReservedUsedX86(IOWorkLoop, 1);
361 OSMetaClassDeclareReservedUsedX86(IOWorkLoop, 2);
b0d623f7 362#endif
0a7de745
A
363 OSMetaClassDeclareReservedUnused(IOWorkLoop, 3);
364 OSMetaClassDeclareReservedUnused(IOWorkLoop, 4);
365 OSMetaClassDeclareReservedUnused(IOWorkLoop, 5);
366 OSMetaClassDeclareReservedUnused(IOWorkLoop, 6);
367 OSMetaClassDeclareReservedUnused(IOWorkLoop, 7);
1c79356b
A
368};
369
370#endif /* !__IOKIT_IOWORKLOOP_H */