]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOWorkLoop.cpp
xnu-1699.22.73.tar.gz
[apple/xnu.git] / iokit / Kernel / IOWorkLoop.cpp
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 1998-2010 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
17 *
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
b0d623f7
A
28
29#include <pexpert/pexpert.h>
1c79356b
A
30#include <IOKit/IOWorkLoop.h>
31#include <IOKit/IOEventSource.h>
32#include <IOKit/IOInterruptEventSource.h>
33#include <IOKit/IOCommandGate.h>
34#include <IOKit/IOTimeStamp.h>
060df5ea 35#include <IOKit/IOKitDebug.h>
2d21ac55 36#include <libkern/OSDebug.h>
1c79356b
A
37
38#define super OSObject
39
40OSDefineMetaClassAndStructors(IOWorkLoop, OSObject);
41
42// Block of unused functions intended for future use
b0d623f7
A
43#if __LP64__
44OSMetaClassDefineReservedUnused(IOWorkLoop, 0);
45OSMetaClassDefineReservedUnused(IOWorkLoop, 1);
46OSMetaClassDefineReservedUnused(IOWorkLoop, 2);
47#else
0b4e3aa0 48OSMetaClassDefineReservedUsed(IOWorkLoop, 0);
0c530ab8 49OSMetaClassDefineReservedUsed(IOWorkLoop, 1);
b0d623f7
A
50OSMetaClassDefineReservedUsed(IOWorkLoop, 2);
51#endif
1c79356b
A
52OSMetaClassDefineReservedUnused(IOWorkLoop, 3);
53OSMetaClassDefineReservedUnused(IOWorkLoop, 4);
54OSMetaClassDefineReservedUnused(IOWorkLoop, 5);
55OSMetaClassDefineReservedUnused(IOWorkLoop, 6);
56OSMetaClassDefineReservedUnused(IOWorkLoop, 7);
57
58enum IOWorkLoopState { kLoopRestart = 0x1, kLoopTerminate = 0x2 };
2d21ac55
A
59static inline void SETP(void *addr, unsigned int flag)
60 { unsigned char *num = (unsigned char *) addr; *num |= flag; }
61static inline void CLRP(void *addr, unsigned int flag)
62 { unsigned char *num = (unsigned char *) addr; *num &= ~flag; }
63static inline bool ISSETP(void *addr, unsigned int flag)
64 { unsigned char *num = (unsigned char *) addr; return (*num & flag) != 0; }
1c79356b
A
65
66#define fFlags loopRestart
67
6d2010ae
A
68#define passiveEventChain reserved->passiveEventChain
69
70#if IOKITSTATS
71
72#define IOStatisticsRegisterCounter() \
73do { \
74 reserved->counter = IOStatistics::registerWorkLoop(this); \
75} while(0)
76
77#define IOStatisticsUnregisterCounter() \
78do { \
79 if (reserved) \
80 IOStatistics::unregisterWorkLoop(reserved->counter); \
81} while(0)
82
83#define IOStatisticsOpenGate() \
84do { \
85 IOStatistics::countWorkLoopOpenGate(reserved->counter); \
86} while(0)
87
88#define IOStatisticsCloseGate() \
89do { \
90 IOStatistics::countWorkLoopCloseGate(reserved->counter); \
91} while(0)
92
93#define IOStatisticsAttachEventSource() \
94do { \
95 IOStatistics::attachWorkLoopEventSource(reserved->counter, inEvent->reserved->counter); \
96} while(0)
97
98#define IOStatisticsDetachEventSource() \
99do { \
100 IOStatistics::detachWorkLoopEventSource(reserved->counter, inEvent->reserved->counter); \
101} while(0)
102
103#else
104
105#define IOStatisticsRegisterCounter()
106#define IOStatisticsUnregisterCounter()
107#define IOStatisticsOpenGate()
108#define IOStatisticsCloseGate()
109#define IOStatisticsAttachEventSource()
110#define IOStatisticsDetachEventSource()
111
112#endif /* IOKITSTATS */
b0d623f7 113
1c79356b
A
114bool IOWorkLoop::init()
115{
6d2010ae 116 // The super init and gateLock allocation MUST be done first.
1c79356b
A
117 if ( !super::init() )
118 return false;
2d21ac55 119
6d2010ae
A
120 // Allocate our ExpansionData if it hasn't been allocated already.
121 if ( !reserved )
122 {
123 reserved = IONew(ExpansionData,1);
124 if ( !reserved )
125 return false;
126
127 bzero(reserved,sizeof(ExpansionData));
128 }
129
130#if DEBUG
131 OSBacktrace ( reserved->allocationBacktrace, sizeof ( reserved->allocationBacktrace ) / sizeof ( reserved->allocationBacktrace[0] ) );
132#endif
133
2d21ac55
A
134 if ( gateLock == NULL ) {
135 if ( !( gateLock = IORecursiveLockAlloc()) )
136 return false;
137 }
138
139 if ( workToDoLock == NULL ) {
140 if ( !(workToDoLock = IOSimpleLockAlloc()) )
141 return false;
142 IOSimpleLockInit(workToDoLock);
143 workToDo = false;
144 }
1c79356b 145
6d2010ae
A
146 if (!reserved) {
147 reserved = IONew(ExpansionData, 1);
148 reserved->options = 0;
149 }
150
151 IOStatisticsRegisterCounter();
152
2d21ac55
A
153 if ( controlG == NULL ) {
154 controlG = IOCommandGate::commandGate(
155 this,
156 OSMemberFunctionCast(
157 IOCommandGate::Action,
158 this,
159 &IOWorkLoop::_maintRequest));
160
161 if ( !controlG )
162 return false;
163 // Point the controlGate at the workLoop. Usually addEventSource
164 // does this automatically. The problem is in this case addEventSource
165 // uses the control gate and it has to be bootstrapped.
166 controlG->setWorkLoop(this);
167 if (addEventSource(controlG) != kIOReturnSuccess)
168 return false;
169 }
1c79356b 170
2d21ac55 171 if ( workThread == NULL ) {
b0d623f7
A
172 thread_continue_t cptr = OSMemberFunctionCast(
173 thread_continue_t,
2d21ac55
A
174 this,
175 &IOWorkLoop::threadMain);
b0d623f7 176 if (KERN_SUCCESS != kernel_thread_start(cptr, this, &workThread))
2d21ac55
A
177 return false;
178 }
1c79356b
A
179
180 return true;
181}
182
183IOWorkLoop *
184IOWorkLoop::workLoop()
2d21ac55
A
185{
186 return IOWorkLoop::workLoopWithOptions(0);
187}
188
189IOWorkLoop *
190IOWorkLoop::workLoopWithOptions(IOOptionBits options)
1c79356b 191{
6d2010ae
A
192 IOWorkLoop *me = new IOWorkLoop;
193
194 if (me && options) {
195 me->reserved = IONew(ExpansionData,1);
196 if (!me->reserved) {
197 me->release();
198 return 0;
199 }
200 bzero(me->reserved,sizeof(ExpansionData));
201 me->reserved->options = options;
2d21ac55 202 }
6d2010ae
A
203
204 if (me && !me->init()) {
205 me->release();
206 return 0;
207 }
208
209 return me;
1c79356b
A
210}
211
212// Free is called twice:
213// First when the atomic retainCount transitions from 1 -> 0
214// Secondly when the work loop itself is commiting hari kari
215// Hence the each leg of the free must be single threaded.
216void IOWorkLoop::free()
217{
218 if (workThread) {
219 IOInterruptState is;
220
221 // If we are here then we must be trying to shut down this work loop
2d21ac55 222 // in this case disable all of the event source, mark the loop
1c79356b
A
223 // as terminating and wakeup the work thread itself and return
224 // Note: we hold the gate across the entire operation mainly for the
225 // benefit of our event sources so we can disable them cleanly.
226 closeGate();
227
228 disableAllEventSources();
229
230 is = IOSimpleLockLockDisableInterrupt(workToDoLock);
231 SETP(&fFlags, kLoopTerminate);
232 thread_wakeup_one((void *) &workToDo);
233 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
234
235 openGate();
236 }
237 else /* !workThread */ {
238 IOEventSource *event, *next;
239
240 for (event = eventChain; event; event = next) {
241 next = event->getNext();
242 event->setWorkLoop(0);
243 event->setNext(0);
244 event->release();
245 }
246 eventChain = 0;
247
6d2010ae
A
248 for (event = passiveEventChain; event; event = next) {
249 next = event->getNext();
250 event->setWorkLoop(0);
251 event->setNext(0);
252 event->release();
253 }
254 passiveEventChain = 0;
255
2d21ac55
A
256 // Either we have a partial initialization to clean up
257 // or the workThread itself is performing hari-kari.
258 // Either way clean up all of our resources and return.
1c79356b
A
259
260 if (controlG) {
261 controlG->release();
262 controlG = 0;
263 }
264
265 if (workToDoLock) {
266 IOSimpleLockFree(workToDoLock);
267 workToDoLock = 0;
268 }
269
270 if (gateLock) {
271 IORecursiveLockFree(gateLock);
272 gateLock = 0;
273 }
6d2010ae
A
274
275 IOStatisticsUnregisterCounter();
276
2d21ac55
A
277 if (reserved) {
278 IODelete(reserved, ExpansionData, 1);
279 reserved = 0;
280 }
1c79356b
A
281
282 super::free();
283 }
284}
285
286IOReturn IOWorkLoop::addEventSource(IOEventSource *newEvent)
287{
288 return controlG->runCommand((void *) mAddEvent, (void *) newEvent);
289}
290
291IOReturn IOWorkLoop::removeEventSource(IOEventSource *toRemove)
292{
293 return controlG->runCommand((void *) mRemoveEvent, (void *) toRemove);
294}
295
296void IOWorkLoop::enableAllEventSources() const
297{
298 IOEventSource *event;
299
300 for (event = eventChain; event; event = event->getNext())
301 event->enable();
6d2010ae
A
302
303 for (event = passiveEventChain; event; event = event->getNext())
304 event->enable();
1c79356b
A
305}
306
307void IOWorkLoop::disableAllEventSources() const
308{
309 IOEventSource *event;
310
311 for (event = eventChain; event; event = event->getNext())
6d2010ae
A
312 event->disable();
313
314 /* NOTE: controlG is in passiveEventChain since it's an IOCommandGate */
315 for (event = passiveEventChain; event; event = event->getNext())
1c79356b
A
316 if (event != controlG) // Don't disable the control gate
317 event->disable();
318}
319
320void IOWorkLoop::enableAllInterrupts() const
321{
322 IOEventSource *event;
6d2010ae 323
1c79356b
A
324 for (event = eventChain; event; event = event->getNext())
325 if (OSDynamicCast(IOInterruptEventSource, event))
326 event->enable();
327}
328
329void IOWorkLoop::disableAllInterrupts() const
330{
331 IOEventSource *event;
6d2010ae 332
1c79356b
A
333 for (event = eventChain; event; event = event->getNext())
334 if (OSDynamicCast(IOInterruptEventSource, event))
335 event->disable();
336}
337
1c79356b 338
0c530ab8 339/* virtual */ bool IOWorkLoop::runEventSources()
1c79356b 340{
0c530ab8 341 bool res = false;
060df5ea
A
342 bool traceWL = (gIOKitTrace & kIOTraceWorkLoops) ? true : false;
343 bool traceES = (gIOKitTrace & kIOTraceEventSources) ? true : false;
344
0c530ab8
A
345 closeGate();
346 if (ISSETP(&fFlags, kLoopTerminate))
6d2010ae
A
347 goto abort;
348
060df5ea
A
349 if (traceWL)
350 IOTimeStampStartConstant(IODBG_WORKLOOP(IOWL_WORK), (uintptr_t) this);
351
0c530ab8
A
352 bool more;
353 do {
6d2010ae
A
354 CLRP(&fFlags, kLoopRestart);
355 more = false;
356 IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock);
357 workToDo = false;
358 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
359 /* NOTE: only loop over event sources in eventChain. Bypass "passive" event sources for performance */
360 for (IOEventSource *evnt = eventChain; evnt; evnt = evnt->getNext()) {
060df5ea 361
6d2010ae
A
362 if (traceES)
363 IOTimeStampStartConstant(IODBG_WORKLOOP(IOWL_CLIENT), (uintptr_t) this, (uintptr_t) evnt);
060df5ea 364
6d2010ae
A
365 more |= evnt->checkForWork();
366
367 if (traceES)
368 IOTimeStampEndConstant(IODBG_WORKLOOP(IOWL_CLIENT), (uintptr_t) this, (uintptr_t) evnt);
369
370 if (ISSETP(&fFlags, kLoopTerminate))
371 goto abort;
372 else if (fFlags & kLoopRestart) {
373 more = true;
374 break;
375 }
376 }
0c530ab8 377 } while (more);
6d2010ae 378
0c530ab8 379 res = true;
060df5ea
A
380
381 if (traceWL)
382 IOTimeStampEndConstant(IODBG_WORKLOOP(IOWL_WORK), (uintptr_t) this);
6d2010ae 383
0c530ab8
A
384abort:
385 openGate();
386 return res;
387}
388
389/* virtual */ void IOWorkLoop::threadMain()
390{
2d21ac55 391restartThread:
0c530ab8
A
392 do {
393 if ( !runEventSources() )
394 goto exitThread;
6601e61a 395
0c530ab8 396 IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock);
1c79356b
A
397 if ( !ISSETP(&fFlags, kLoopTerminate) && !workToDo) {
398 assert_wait((void *) &workToDo, false);
399 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
2d21ac55
A
400 thread_continue_t cptr = NULL;
401 if (!reserved || !(kPreciousStack & reserved->options))
402 cptr = OSMemberFunctionCast(
403 thread_continue_t, this, &IOWorkLoop::threadMain);
0c530ab8 404 thread_block_parameter(cptr, this);
2d21ac55 405 goto restartThread;
1c79356b
A
406 /* NOTREACHED */
407 }
408
409 // At this point we either have work to do or we need
410 // to commit suicide. But no matter
411 // Clear the simple lock and retore the interrupt state
412 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
0c530ab8 413 } while(workToDo);
1c79356b
A
414
415exitThread:
b0d623f7 416 thread_t thread = workThread;
1c79356b
A
417 workThread = 0; // Say we don't have a loop and free ourselves
418 free();
b0d623f7
A
419
420 thread_deallocate(thread);
421 (void) thread_terminate(thread);
1c79356b
A
422}
423
424IOThread IOWorkLoop::getThread() const
425{
426 return workThread;
427}
428
429bool IOWorkLoop::onThread() const
430{
431 return (IOThreadSelf() == workThread);
432}
433
434bool IOWorkLoop::inGate() const
435{
436 return IORecursiveLockHaveLock(gateLock);
437}
438
439// Internal APIs used by event sources to control the thread
440void IOWorkLoop::signalWorkAvailable()
441{
442 if (workToDoLock) {
443 IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock);
444 workToDo = true;
445 thread_wakeup_one((void *) &workToDo);
446 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
447 }
448}
449
450void IOWorkLoop::openGate()
451{
6d2010ae 452 IOStatisticsOpenGate();
1c79356b
A
453 IORecursiveLockUnlock(gateLock);
454}
455
456void IOWorkLoop::closeGate()
457{
458 IORecursiveLockLock(gateLock);
6d2010ae 459 IOStatisticsCloseGate();
1c79356b
A
460}
461
462bool IOWorkLoop::tryCloseGate()
463{
6d2010ae
A
464 bool res = (IORecursiveLockTryLock(gateLock) != 0);
465 if (res) {
466 IOStatisticsCloseGate();
467 }
468 return res;
1c79356b
A
469}
470
471int IOWorkLoop::sleepGate(void *event, UInt32 interuptibleType)
472{
6d2010ae
A
473 int res;
474 IOStatisticsOpenGate();
475 res = IORecursiveLockSleep(gateLock, event, interuptibleType);
476 IOStatisticsCloseGate();
477 return res;
1c79356b
A
478}
479
b0d623f7
A
480int IOWorkLoop::sleepGate(void *event, AbsoluteTime deadline, UInt32 interuptibleType)
481{
6d2010ae
A
482 int res;
483 IOStatisticsOpenGate();
484 res = IORecursiveLockSleepDeadline(gateLock, event, deadline, interuptibleType);
485 IOStatisticsCloseGate();
486 return res;
b0d623f7
A
487}
488
1c79356b
A
489void IOWorkLoop::wakeupGate(void *event, bool oneThread)
490{
491 IORecursiveLockWakeup(gateLock, event, oneThread);
492}
493
0b4e3aa0 494IOReturn IOWorkLoop::runAction(Action inAction, OSObject *target,
55e303ae
A
495 void *arg0, void *arg1,
496 void *arg2, void *arg3)
0b4e3aa0
A
497{
498 IOReturn res;
499
500 // closeGate is recursive so don't worry if we already hold the lock.
501 closeGate();
502 res = (*inAction)(target, arg0, arg1, arg2, arg3);
503 openGate();
504
505 return res;
506}
507
1c79356b
A
508IOReturn IOWorkLoop::_maintRequest(void *inC, void *inD, void *, void *)
509{
b0d623f7 510 maintCommandEnum command = (maintCommandEnum) (uintptr_t) inC;
1c79356b
A
511 IOEventSource *inEvent = (IOEventSource *) inD;
512 IOReturn res = kIOReturnSuccess;
513
514 switch (command)
515 {
516 case mAddEvent:
9bccf70c
A
517 if (!inEvent->getWorkLoop()) {
518 SETP(&fFlags, kLoopRestart);
519
520 inEvent->retain();
521 inEvent->setWorkLoop(this);
522 inEvent->setNext(0);
6d2010ae
A
523
524 /* Check if this is a passive or active event source being added */
525 if (eventSourcePerformsWork(inEvent)) {
526
527 if (!eventChain)
528 eventChain = inEvent;
529 else {
530 IOEventSource *event, *next;
9bccf70c 531
6d2010ae
A
532 for (event = eventChain; (next = event->getNext()); event = next)
533 ;
534 event->setNext(inEvent);
535
536 }
537
538 }
9bccf70c 539 else {
6d2010ae
A
540
541 if (!passiveEventChain)
542 passiveEventChain = inEvent;
543 else {
544 IOEventSource *event, *next;
9bccf70c 545
6d2010ae
A
546 for (event = passiveEventChain; (next = event->getNext()); event = next)
547 ;
548 event->setNext(inEvent);
549
550 }
551
9bccf70c 552 }
6d2010ae 553 IOStatisticsAttachEventSource();
1c79356b
A
554 }
555 break;
556
557 case mRemoveEvent:
9bccf70c 558 if (inEvent->getWorkLoop()) {
6d2010ae
A
559 if (eventSourcePerformsWork(inEvent)) {
560 if (eventChain == inEvent)
561 eventChain = inEvent->getNext();
562 else {
563 IOEventSource *event, *next;
564
565 event = eventChain;
566 while ((next = event->getNext()) && next != inEvent)
567 event = next;
568
569 if (!next) {
570 res = kIOReturnBadArgument;
571 break;
572 }
573 event->setNext(inEvent->getNext());
574 }
575 }
576 else {
577 if (passiveEventChain == inEvent)
578 passiveEventChain = inEvent->getNext();
579 else {
580 IOEventSource *event, *next;
581
582 event = passiveEventChain;
583 while ((next = event->getNext()) && next != inEvent)
584 event = next;
585
586 if (!next) {
587 res = kIOReturnBadArgument;
588 break;
589 }
590 event->setNext(inEvent->getNext());
591 }
592 }
593
9bccf70c
A
594 inEvent->setWorkLoop(0);
595 inEvent->setNext(0);
596 inEvent->release();
597 SETP(&fFlags, kLoopRestart);
6d2010ae 598 IOStatisticsDetachEventSource();
1c79356b 599 }
1c79356b
A
600 break;
601
602 default:
603 return kIOReturnUnsupported;
604 }
605
606 return res;
607}
6d2010ae
A
608
609bool
610IOWorkLoop::eventSourcePerformsWork(IOEventSource *inEventSource)
611{
612 bool result = true;
613
614 /*
615 * The idea here is to see if the subclass of IOEventSource has overridden checkForWork().
616 * The assumption is that if you override checkForWork(), you need to be
617 * active and not passive.
618 *
619 * We picked a known quantity controlG that does not override
620 * IOEventSource::checkForWork(), namely the IOCommandGate associated with
621 * the workloop to which this event source is getting attached.
622 *
623 * We do a pointer comparison on the offset in the vtable for inNewEvent against
624 * the offset in the vtable for inReferenceEvent. This works because
625 * IOCommandGate's slot for checkForWork() has the address of
626 * IOEventSource::checkForWork() in it.
627 *
628 * Think of OSMemberFunctionCast yielding the value at the vtable offset for
629 * checkForWork() here. We're just testing to see if it's the same or not.
630 *
631 */
632 if (controlG) {
633 void * ptr1;
634 void * ptr2;
635
636 ptr1 = OSMemberFunctionCast(void*, inEventSource, &IOEventSource::checkForWork);
637 ptr2 = OSMemberFunctionCast(void*, controlG, &IOEventSource::checkForWork);
638
639 if (ptr1 == ptr2)
640 result = false;
641 }
642
643 return result;
644}