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