]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOService.cpp
xnu-3247.1.106.tar.gz
[apple/xnu.git] / iokit / Kernel / IOService.cpp
1 /*
2 * Copyright (c) 1998-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <IOKit/system.h>
30
31 #include <IOKit/IOService.h>
32 #include <libkern/OSDebug.h>
33 #include <libkern/c++/OSContainers.h>
34 #include <libkern/c++/OSKext.h>
35 #include <libkern/c++/OSUnserialize.h>
36 #include <IOKit/IOCatalogue.h>
37 #include <IOKit/IOCommand.h>
38 #include <IOKit/IODeviceTreeSupport.h>
39 #include <IOKit/IODeviceMemory.h>
40 #include <IOKit/IOInterrupts.h>
41 #include <IOKit/IOInterruptController.h>
42 #include <IOKit/IOPlatformExpert.h>
43 #include <IOKit/IOMessage.h>
44 #include <IOKit/IOLib.h>
45 #include <IOKit/IOKitKeysPrivate.h>
46 #include <IOKit/IOBSD.h>
47 #include <IOKit/IOUserClient.h>
48 #include <IOKit/IOWorkLoop.h>
49 #include <IOKit/IOTimeStamp.h>
50 #include <IOKit/IOHibernatePrivate.h>
51 #include <IOKit/IOInterruptAccountingPrivate.h>
52 #include <IOKit/IOKernelReporters.h>
53 #include <IOKit/AppleKeyStoreInterface.h>
54 #include <IOKit/IOCPU.h>
55 #include <mach/sync_policy.h>
56 #include <IOKit/assert.h>
57 #include <sys/errno.h>
58 #include <sys/kdebug.h>
59 #include <string.h>
60
61 #include <machine/pal_routines.h>
62
63 #define LOG kprintf
64 //#define LOG IOLog
65 #define MATCH_DEBUG 0
66 #define OBFUSCATE(x) ((void *)(VM_KERNEL_ADDRPERM(x)))
67
68 #include "IOServicePrivate.h"
69 #include "IOKitKernelInternal.h"
70
71 // take lockForArbitration before LOCKNOTIFY
72
73 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
74
75 #define super IORegistryEntry
76
77 OSDefineMetaClassAndStructors(IOService, IORegistryEntry)
78
79 OSDefineMetaClassAndStructors(_IOServiceNotifier, IONotifier)
80
81 OSDefineMetaClassAndStructors(_IOServiceInterestNotifier, IONotifier)
82
83 OSDefineMetaClassAndStructors(_IOConfigThread, OSObject)
84
85 OSDefineMetaClassAndStructors(_IOServiceJob, OSObject)
86
87 OSDefineMetaClassAndStructors(IOResources, IOService)
88
89 OSDefineMetaClassAndStructors(_IOOpenServiceIterator, OSIterator)
90
91 OSDefineMetaClassAndAbstractStructors(IONotifier, OSObject)
92
93 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
94
95 static IOPlatformExpert * gIOPlatform;
96 static class IOPMrootDomain * gIOPMRootDomain;
97 const IORegistryPlane * gIOServicePlane;
98 const IORegistryPlane * gIOPowerPlane;
99 const OSSymbol * gIODeviceMemoryKey;
100 const OSSymbol * gIOInterruptControllersKey;
101 const OSSymbol * gIOInterruptSpecifiersKey;
102
103 const OSSymbol * gIOResourcesKey;
104 const OSSymbol * gIOResourceMatchKey;
105 const OSSymbol * gIOProviderClassKey;
106 const OSSymbol * gIONameMatchKey;
107 const OSSymbol * gIONameMatchedKey;
108 const OSSymbol * gIOPropertyMatchKey;
109 const OSSymbol * gIOLocationMatchKey;
110 const OSSymbol * gIOParentMatchKey;
111 const OSSymbol * gIOPathMatchKey;
112 const OSSymbol * gIOMatchCategoryKey;
113 const OSSymbol * gIODefaultMatchCategoryKey;
114 const OSSymbol * gIOMatchedServiceCountKey;
115
116 const OSSymbol * gIOMapperIDKey;
117 const OSSymbol * gIOUserClientClassKey;
118 const OSSymbol * gIOKitDebugKey;
119
120 const OSSymbol * gIOCommandPoolSizeKey;
121
122 const OSSymbol * gIOConsoleLockedKey;
123 const OSSymbol * gIOConsoleUsersKey;
124 const OSSymbol * gIOConsoleSessionUIDKey;
125 const OSSymbol * gIOConsoleSessionAuditIDKey;
126 const OSSymbol * gIOConsoleUsersSeedKey;
127 const OSSymbol * gIOConsoleSessionOnConsoleKey;
128 const OSSymbol * gIOConsoleSessionLoginDoneKey;
129 const OSSymbol * gIOConsoleSessionSecureInputPIDKey;
130 const OSSymbol * gIOConsoleSessionScreenLockedTimeKey;
131
132 clock_sec_t gIOConsoleLockTime;
133 static bool gIOConsoleLoggedIn;
134 #if HIBERNATION
135 static uint32_t gIOScreenLockState;
136 #endif
137 static IORegistryEntry * gIOChosenEntry;
138
139 static int gIOResourceGenerationCount;
140
141 const OSSymbol * gIOServiceKey;
142 const OSSymbol * gIOPublishNotification;
143 const OSSymbol * gIOFirstPublishNotification;
144 const OSSymbol * gIOMatchedNotification;
145 const OSSymbol * gIOFirstMatchNotification;
146 const OSSymbol * gIOTerminatedNotification;
147
148 const OSSymbol * gIOGeneralInterest;
149 const OSSymbol * gIOBusyInterest;
150 const OSSymbol * gIOAppPowerStateInterest;
151 const OSSymbol * gIOPriorityPowerStateInterest;
152 const OSSymbol * gIOConsoleSecurityInterest;
153
154 const OSSymbol * gAKSGetKey;
155 #if defined(__i386__) || defined(__x86_64__)
156 const OSSymbol * gIOCreateEFIDevicePathSymbol;
157 #endif
158
159 static OSDictionary * gNotifications;
160 static IORecursiveLock * gNotificationLock;
161
162 static IOService * gIOResources;
163 static IOService * gIOServiceRoot;
164
165 static OSOrderedSet * gJobs;
166 static semaphore_port_t gJobsSemaphore;
167 static IOLock * gJobsLock;
168 static int gOutstandingJobs;
169 static int gNumConfigThreads;
170 static int gNumWaitingThreads;
171 static IOLock * gIOServiceBusyLock;
172 static bool gCPUsRunning;
173
174 static thread_t gIOTerminateThread;
175 static UInt32 gIOTerminateWork;
176 static OSArray * gIOTerminatePhase2List;
177 static OSArray * gIOStopList;
178 static OSArray * gIOStopProviderList;
179 static OSArray * gIOFinalizeList;
180
181 static SInt32 gIOConsoleUsersSeed;
182 static OSData * gIOConsoleUsersSeedValue;
183
184 extern const OSSymbol * gIODTPHandleKey;
185
186 const OSSymbol * gIOPlatformFunctionHandlerSet;
187
188 static IOLock * gIOConsoleUsersLock;
189 static thread_call_t gIOConsoleLockCallout;
190
191 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
192
193 #define LOCKREADNOTIFY() \
194 IORecursiveLockLock( gNotificationLock )
195 #define LOCKWRITENOTIFY() \
196 IORecursiveLockLock( gNotificationLock )
197 #define LOCKWRITE2READNOTIFY()
198 #define UNLOCKNOTIFY() \
199 IORecursiveLockUnlock( gNotificationLock )
200 #define SLEEPNOTIFY(event) \
201 IORecursiveLockSleep( gNotificationLock, (void *)(event), THREAD_UNINT )
202 #define SLEEPNOTIFYTO(event, deadline) \
203 IORecursiveLockSleepDeadline( gNotificationLock, (void *)(event), deadline, THREAD_UNINT )
204 #define WAKEUPNOTIFY(event) \
205 IORecursiveLockWakeup( gNotificationLock, (void *)(event), /* wake one */ false )
206
207 #define randomDelay() \
208 int del = read_processor_clock(); \
209 del = (((int)IOThreadSelf()) ^ del ^ (del >> 10)) & 0x3ff; \
210 IOSleep( del );
211
212 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
213
214 #define queue_element(entry, element, type, field) do { \
215 vm_address_t __ele = (vm_address_t) (entry); \
216 __ele -= -4 + ((size_t)(&((type) 4)->field)); \
217 (element) = (type) __ele; \
218 } while(0)
219
220 #define iterqueue(que, elt) \
221 for (queue_entry_t elt = queue_first(que); \
222 !queue_end(que, elt); \
223 elt = queue_next(elt))
224
225 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
226
227 struct IOInterruptAccountingReporter {
228 IOSimpleReporter * reporter; /* Reporter responsible for communicating the statistics */
229 IOInterruptAccountingData * statistics; /* The live statistics values, if any */
230 };
231
232 struct ArbitrationLockQueueElement {
233 queue_chain_t link;
234 IOThread thread;
235 IOService * service;
236 unsigned count;
237 bool required;
238 bool aborted;
239 };
240
241 static queue_head_t gArbitrationLockQueueActive;
242 static queue_head_t gArbitrationLockQueueWaiting;
243 static queue_head_t gArbitrationLockQueueFree;
244 static IOLock * gArbitrationLockQueueLock;
245
246 bool IOService::isInactive( void ) const
247 { return( 0 != (kIOServiceInactiveState & getState())); }
248
249 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
250
251 #if defined(__i386__) || defined(__x86_64__)
252
253 // Only used by the intel implementation of
254 // IOService::requireMaxBusStall(UInt32 ns)
255 // IOService::requireMaxInterruptDelay(uint32_t ns)
256 struct CpuDelayEntry
257 {
258 IOService * fService;
259 UInt32 fMaxDelay;
260 UInt32 fDelayType;
261 };
262
263 enum {
264 kCpuDelayBusStall, kCpuDelayInterrupt,
265 kCpuNumDelayTypes
266 };
267
268 static OSData *sCpuDelayData = OSData::withCapacity(8 * sizeof(CpuDelayEntry));
269 static IORecursiveLock *sCpuDelayLock = IORecursiveLockAlloc();
270 static OSArray *sCpuLatencyHandlers[kCpuNumDelayTypes];
271 const OSSymbol *sCPULatencyFunctionName[kCpuNumDelayTypes];
272 static OSNumber * sCPULatencyHolder[kCpuNumDelayTypes];
273 static char sCPULatencyHolderName[kCpuNumDelayTypes][128];
274 static OSNumber * sCPULatencySet[kCpuNumDelayTypes];
275
276 static void
277 requireMaxCpuDelay(IOService * service, UInt32 ns, UInt32 delayType);
278 static IOReturn
279 setLatencyHandler(UInt32 delayType, IOService * target, bool enable);
280
281 #endif /* defined(__i386__) || defined(__x86_64__) */
282
283 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
284
285 void IOService::initialize( void )
286 {
287 kern_return_t err;
288
289 gIOServicePlane = IORegistryEntry::makePlane( kIOServicePlane );
290 gIOPowerPlane = IORegistryEntry::makePlane( kIOPowerPlane );
291
292 gIOProviderClassKey = OSSymbol::withCStringNoCopy( kIOProviderClassKey );
293 gIONameMatchKey = OSSymbol::withCStringNoCopy( kIONameMatchKey );
294 gIONameMatchedKey = OSSymbol::withCStringNoCopy( kIONameMatchedKey );
295 gIOPropertyMatchKey = OSSymbol::withCStringNoCopy( kIOPropertyMatchKey );
296 gIOPathMatchKey = OSSymbol::withCStringNoCopy( kIOPathMatchKey );
297 gIOLocationMatchKey = OSSymbol::withCStringNoCopy( kIOLocationMatchKey );
298 gIOParentMatchKey = OSSymbol::withCStringNoCopy( kIOParentMatchKey );
299
300 gIOMatchCategoryKey = OSSymbol::withCStringNoCopy( kIOMatchCategoryKey );
301 gIODefaultMatchCategoryKey = OSSymbol::withCStringNoCopy(
302 kIODefaultMatchCategoryKey );
303 gIOMatchedServiceCountKey = OSSymbol::withCStringNoCopy(
304 kIOMatchedServiceCountKey );
305
306 gIOUserClientClassKey = OSSymbol::withCStringNoCopy( kIOUserClientClassKey );
307
308 gIOResourcesKey = OSSymbol::withCStringNoCopy( kIOResourcesClass );
309 gIOResourceMatchKey = OSSymbol::withCStringNoCopy( kIOResourceMatchKey );
310
311 gIODeviceMemoryKey = OSSymbol::withCStringNoCopy( "IODeviceMemory" );
312 gIOInterruptControllersKey
313 = OSSymbol::withCStringNoCopy("IOInterruptControllers");
314 gIOInterruptSpecifiersKey
315 = OSSymbol::withCStringNoCopy("IOInterruptSpecifiers");
316
317 gIOMapperIDKey = OSSymbol::withCStringNoCopy(kIOMapperIDKey);
318
319 gIOKitDebugKey = OSSymbol::withCStringNoCopy( kIOKitDebugKey );
320
321 gIOCommandPoolSizeKey = OSSymbol::withCStringNoCopy( kIOCommandPoolSizeKey );
322
323 gIOGeneralInterest = OSSymbol::withCStringNoCopy( kIOGeneralInterest );
324 gIOBusyInterest = OSSymbol::withCStringNoCopy( kIOBusyInterest );
325 gIOAppPowerStateInterest = OSSymbol::withCStringNoCopy( kIOAppPowerStateInterest );
326 gIOPriorityPowerStateInterest = OSSymbol::withCStringNoCopy( kIOPriorityPowerStateInterest );
327 gIOConsoleSecurityInterest = OSSymbol::withCStringNoCopy( kIOConsoleSecurityInterest );
328
329 gNotifications = OSDictionary::withCapacity( 1 );
330 gIOPublishNotification = OSSymbol::withCStringNoCopy(
331 kIOPublishNotification );
332 gIOFirstPublishNotification = OSSymbol::withCStringNoCopy(
333 kIOFirstPublishNotification );
334 gIOMatchedNotification = OSSymbol::withCStringNoCopy(
335 kIOMatchedNotification );
336 gIOFirstMatchNotification = OSSymbol::withCStringNoCopy(
337 kIOFirstMatchNotification );
338 gIOTerminatedNotification = OSSymbol::withCStringNoCopy(
339 kIOTerminatedNotification );
340 gIOServiceKey = OSSymbol::withCStringNoCopy( kIOServiceClass);
341
342 gIOConsoleLockedKey = OSSymbol::withCStringNoCopy( kIOConsoleLockedKey);
343 gIOConsoleUsersKey = OSSymbol::withCStringNoCopy( kIOConsoleUsersKey);
344 gIOConsoleSessionUIDKey = OSSymbol::withCStringNoCopy( kIOConsoleSessionUIDKey);
345 gIOConsoleSessionAuditIDKey = OSSymbol::withCStringNoCopy( kIOConsoleSessionAuditIDKey);
346
347 gIOConsoleUsersSeedKey = OSSymbol::withCStringNoCopy(kIOConsoleUsersSeedKey);
348 gIOConsoleSessionOnConsoleKey = OSSymbol::withCStringNoCopy(kIOConsoleSessionOnConsoleKey);
349 gIOConsoleSessionLoginDoneKey = OSSymbol::withCStringNoCopy(kIOConsoleSessionLoginDoneKey);
350 gIOConsoleSessionSecureInputPIDKey = OSSymbol::withCStringNoCopy(kIOConsoleSessionSecureInputPIDKey);
351 gIOConsoleSessionScreenLockedTimeKey = OSSymbol::withCStringNoCopy(kIOConsoleSessionScreenLockedTimeKey);
352
353 gIOConsoleUsersSeedValue = OSData::withBytesNoCopy(&gIOConsoleUsersSeed, sizeof(gIOConsoleUsersSeed));
354
355 gIOPlatformFunctionHandlerSet = OSSymbol::withCStringNoCopy(kIOPlatformFunctionHandlerSet);
356 #if defined(__i386__) || defined(__x86_64__)
357 sCPULatencyFunctionName[kCpuDelayBusStall] = OSSymbol::withCStringNoCopy(kIOPlatformFunctionHandlerMaxBusDelay);
358 sCPULatencyFunctionName[kCpuDelayInterrupt] = OSSymbol::withCStringNoCopy(kIOPlatformFunctionHandlerMaxInterruptDelay);
359 uint32_t idx;
360 for (idx = 0; idx < kCpuNumDelayTypes; idx++)
361 {
362 sCPULatencySet[idx] = OSNumber::withNumber(-1U, 32);
363 sCPULatencyHolder[idx] = OSNumber::withNumber(0ULL, 64);
364 assert(sCPULatencySet[idx] && sCPULatencyHolder[idx]);
365 }
366 gIOCreateEFIDevicePathSymbol = OSSymbol::withCString("CreateEFIDevicePath");
367 #endif
368 gNotificationLock = IORecursiveLockAlloc();
369
370 gAKSGetKey = OSSymbol::withCStringNoCopy(AKS_PLATFORM_FUNCTION_GETKEY);
371
372 assert( gIOServicePlane && gIODeviceMemoryKey
373 && gIOInterruptControllersKey && gIOInterruptSpecifiersKey
374 && gIOResourcesKey && gNotifications && gNotificationLock
375 && gIOProviderClassKey && gIONameMatchKey && gIONameMatchedKey
376 && gIOMatchCategoryKey && gIODefaultMatchCategoryKey
377 && gIOPublishNotification && gIOMatchedNotification
378 && gIOTerminatedNotification && gIOServiceKey
379 && gIOConsoleUsersKey && gIOConsoleSessionUIDKey
380 && gIOConsoleSessionOnConsoleKey && gIOConsoleSessionSecureInputPIDKey
381 && gIOConsoleUsersSeedKey && gIOConsoleUsersSeedValue);
382
383 gJobsLock = IOLockAlloc();
384 gJobs = OSOrderedSet::withCapacity( 10 );
385
386 gIOServiceBusyLock = IOLockAlloc();
387
388 gIOConsoleUsersLock = IOLockAlloc();
389
390 err = semaphore_create(kernel_task, &gJobsSemaphore, SYNC_POLICY_FIFO, 0);
391
392 gIOConsoleLockCallout = thread_call_allocate(&IOService::consoleLockTimer, NULL);
393
394 IORegistryEntry::getRegistryRoot()->setProperty(gIOConsoleLockedKey, kOSBooleanTrue);
395
396 assert( gIOServiceBusyLock && gJobs && gJobsLock && gIOConsoleUsersLock
397 && gIOConsoleLockCallout && (err == KERN_SUCCESS) );
398
399 gIOResources = IOResources::resources();
400 assert( gIOResources );
401
402 gArbitrationLockQueueLock = IOLockAlloc();
403 queue_init(&gArbitrationLockQueueActive);
404 queue_init(&gArbitrationLockQueueWaiting);
405 queue_init(&gArbitrationLockQueueFree);
406
407 assert( gArbitrationLockQueueLock );
408
409 gIOTerminatePhase2List = OSArray::withCapacity( 2 );
410 gIOStopList = OSArray::withCapacity( 16 );
411 gIOStopProviderList = OSArray::withCapacity( 16 );
412 gIOFinalizeList = OSArray::withCapacity( 16 );
413 assert( gIOTerminatePhase2List && gIOStopList && gIOStopProviderList && gIOFinalizeList );
414 }
415
416 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
417
418 #if defined(__i386__) || defined(__x86_64__)
419 extern "C" {
420
421 const char *getCpuDelayBusStallHolderName(void) {
422 return sCPULatencyHolderName[kCpuDelayBusStall];
423 }
424
425 }
426 #endif
427
428 #if IOMATCHDEBUG
429 static UInt64 getDebugFlags( OSDictionary * props )
430 {
431 OSNumber * debugProp;
432 UInt64 debugFlags;
433
434 debugProp = OSDynamicCast( OSNumber,
435 props->getObject( gIOKitDebugKey ));
436 if( debugProp)
437 debugFlags = debugProp->unsigned64BitValue();
438 else
439 debugFlags = gIOKitDebug;
440
441 return( debugFlags );
442 }
443 #endif
444
445 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
446
447 // Probe a matched service and return an instance to be started.
448 // The default score is from the property table, & may be altered
449 // during probe to change the start order.
450
451 IOService * IOService::probe( IOService * provider,
452 SInt32 * score )
453 {
454 return( this );
455 }
456
457 bool IOService::start( IOService * provider )
458 {
459 return( true );
460 }
461
462 void IOService::stop( IOService * provider )
463 {
464 }
465
466 bool IOService::init( OSDictionary * dictionary )
467 {
468 bool ret;
469
470 ret = super::init(dictionary);
471 if (!ret) return (false);
472 if (reserved) return (true);
473
474 reserved = IONew(ExpansionData, 1);
475 if (!reserved) return (false);
476 bzero(reserved, sizeof(*reserved));
477
478 /*
479 * TODO: Improve on this. Previous efforts to more lazily allocate this
480 * lock based on the presence of specifiers ran into issues as some
481 * platforms set up the specifiers after IOService initialization.
482 *
483 * We may be able to get away with a global lock, as this should only be
484 * contended by IOReporting clients and driver start/stop (unless a
485 * driver wants to remove/add handlers in the course of normal operation,
486 * which should be unlikely).
487 */
488 reserved->interruptStatisticsLock = IOLockAlloc();
489 if (!reserved->interruptStatisticsLock) return (false);
490
491 return (true);
492 }
493
494 bool IOService::init( IORegistryEntry * from,
495 const IORegistryPlane * inPlane )
496 {
497 bool ret;
498
499 ret = super::init(from, inPlane);
500 if (!ret) return (false);
501 if (reserved) return (true);
502
503 reserved = IONew(ExpansionData, 1);
504 if (!reserved) return (false);
505 bzero(reserved, sizeof(*reserved));
506
507 /*
508 * TODO: Improve on this. Previous efforts to more lazily allocate this
509 * lock based on the presence of specifiers ran into issues as some
510 * platforms set up the specifiers after IOService initialization.
511 *
512 * We may be able to get away with a global lock, as this should only be
513 * contended by IOReporting clients and driver start/stop (unless a
514 * driver wants to remove/add handlers in the course of normal operation,
515 * which should be unlikely).
516 */
517 reserved->interruptStatisticsLock = IOLockAlloc();
518 if (!reserved->interruptStatisticsLock) return (false);
519
520 return (true);
521 }
522
523 void IOService::free( void )
524 {
525 int i = 0;
526 requireMaxBusStall(0);
527 requireMaxInterruptDelay(0);
528 if( getPropertyTable())
529 unregisterAllInterest();
530 PMfree();
531
532 if (reserved) {
533 if (reserved->interruptStatisticsArray) {
534 for (i = 0; i < reserved->interruptStatisticsArrayCount; i++) {
535 if (reserved->interruptStatisticsArray[i].reporter)
536 reserved->interruptStatisticsArray[i].reporter->release();
537 }
538
539 IODelete(reserved->interruptStatisticsArray, IOInterruptAccountingReporter, reserved->interruptStatisticsArrayCount);
540 }
541
542 if (reserved->interruptStatisticsLock)
543 IOLockFree(reserved->interruptStatisticsLock);
544 IODelete(reserved, ExpansionData, 1);
545 }
546
547 if (_numInterruptSources && _interruptSources)
548 {
549 IOFree(_interruptSources, _numInterruptSources * sizeof(IOInterruptSource));
550 _interruptSources = 0;
551 }
552
553 super::free();
554 }
555
556 /*
557 * Attach in service plane
558 */
559 bool IOService::attach( IOService * provider )
560 {
561 bool ok;
562
563 if( provider) {
564
565 if( gIOKitDebug & kIOLogAttach)
566 LOG( "%s::attach(%s)\n", getName(),
567 provider->getName());
568
569 provider->lockForArbitration();
570 if( provider->__state[0] & kIOServiceInactiveState)
571 ok = false;
572 else
573 ok = attachToParent( provider, gIOServicePlane);
574 provider->unlockForArbitration();
575
576 } else {
577 gIOServiceRoot = this;
578 ok = attachToParent( getRegistryRoot(), gIOServicePlane);
579 }
580
581 if (ok && !__provider) (void) getProvider();
582
583 return( ok );
584 }
585
586 IOService * IOService::getServiceRoot( void )
587 {
588 return( gIOServiceRoot );
589 }
590
591 void IOService::detach( IOService * provider )
592 {
593 IOService * newProvider = 0;
594 SInt32 busy;
595 bool adjParent;
596
597 if( gIOKitDebug & kIOLogAttach)
598 LOG("%s::detach(%s)\n", getName(), provider->getName());
599
600 lockForArbitration();
601
602 uint64_t regID1 = provider->getRegistryEntryID();
603 uint64_t regID2 = getRegistryEntryID();
604 IOServiceTrace(
605 IOSERVICE_DETACH,
606 (uintptr_t) regID1,
607 (uintptr_t) (regID1 >> 32),
608 (uintptr_t) regID2,
609 (uintptr_t) (regID2 >> 32));
610
611 adjParent = ((busy = (__state[1] & kIOServiceBusyStateMask))
612 && (provider == getProvider()));
613
614 detachFromParent( provider, gIOServicePlane );
615
616 if( busy) {
617 newProvider = getProvider();
618 if( busy && (__state[1] & kIOServiceTermPhase3State) && (0 == newProvider))
619 _adjustBusy( -busy );
620 }
621
622 if (kIOServiceInactiveState & __state[0]) {
623 getMetaClass()->removeInstance(this);
624 IORemoveServicePlatformActions(this);
625 }
626
627 unlockForArbitration();
628
629 if( newProvider) {
630 newProvider->lockForArbitration();
631 newProvider->_adjustBusy(1);
632 newProvider->unlockForArbitration();
633 }
634
635 // check for last client detach from a terminated service
636 if( provider->lockForArbitration( true ))
637 {
638 if (kIOServiceStartState & __state[1])
639 {
640 provider->scheduleTerminatePhase2();
641 }
642 if( adjParent) provider->_adjustBusy( -1 );
643 if( (provider->__state[1] & kIOServiceTermPhase3State)
644 && (0 == provider->getClient())) {
645 provider->scheduleFinalize();
646 }
647 provider->unlockForArbitration();
648 }
649 }
650
651 /*
652 * Register instance - publish it for matching
653 */
654
655 void IOService::registerService( IOOptionBits options )
656 {
657 char * pathBuf;
658 const char * path;
659 char * skip;
660 int len;
661 enum { kMaxPathLen = 256 };
662 enum { kMaxChars = 63 };
663
664 IORegistryEntry * parent = this;
665 IORegistryEntry * root = getRegistryRoot();
666 while( parent && (parent != root))
667 parent = parent->getParentEntry( gIOServicePlane);
668
669 if( parent != root) {
670 IOLog("%s: not registry member at registerService()\n", getName());
671 return;
672 }
673
674 // Allow the Platform Expert to adjust this node.
675 if( gIOPlatform && (!gIOPlatform->platformAdjustService(this)))
676 return;
677
678 IOInstallServicePlatformActions(this);
679
680 if( (this != gIOResources)
681 && (kIOLogRegister & gIOKitDebug)) {
682
683 pathBuf = (char *) IOMalloc( kMaxPathLen );
684
685 IOLog( "Registering: " );
686
687 len = kMaxPathLen;
688 if( pathBuf && getPath( pathBuf, &len, gIOServicePlane)) {
689
690 path = pathBuf;
691 if( len > kMaxChars) {
692 IOLog("..");
693 len -= kMaxChars;
694 path += len;
695 if( (skip = strchr( path, '/')))
696 path = skip;
697 }
698 } else
699 path = getName();
700
701 IOLog( "%s\n", path );
702
703 if( pathBuf)
704 IOFree( pathBuf, kMaxPathLen );
705 }
706
707 startMatching( options );
708 }
709
710 void IOService::startMatching( IOOptionBits options )
711 {
712 IOService * provider;
713 UInt32 prevBusy = 0;
714 bool needConfig;
715 bool needWake = false;
716 bool ok;
717 bool sync;
718 bool waitAgain;
719
720 lockForArbitration();
721
722 sync = (options & kIOServiceSynchronous)
723 || ((provider = getProvider())
724 && (provider->__state[1] & kIOServiceSynchronousState));
725
726 if ( options & kIOServiceAsynchronous )
727 sync = false;
728
729 needConfig = (0 == (__state[1] & (kIOServiceNeedConfigState | kIOServiceConfigState)))
730 && (0 == (__state[0] & kIOServiceInactiveState));
731
732 __state[1] |= kIOServiceNeedConfigState;
733
734 // __state[0] &= ~kIOServiceInactiveState;
735
736 // if( sync) LOG("OSKernelStackRemaining = %08x @ %s\n",
737 // OSKernelStackRemaining(), getName());
738
739 if( needConfig) {
740 needWake = (0 != (kIOServiceSyncPubState & __state[1]));
741 }
742
743 if( sync)
744 __state[1] |= kIOServiceSynchronousState;
745 else
746 __state[1] &= ~kIOServiceSynchronousState;
747
748 if( needConfig) prevBusy = _adjustBusy( 1 );
749
750 unlockForArbitration();
751
752 if( needConfig) {
753
754 if( needWake) {
755 IOLockLock( gIOServiceBusyLock );
756 thread_wakeup( (event_t) this/*&__state[1]*/ );
757 IOLockUnlock( gIOServiceBusyLock );
758
759 } else if( !sync || (kIOServiceAsynchronous & options)) {
760
761 ok = (0 != _IOServiceJob::startJob( this, kMatchNubJob, options ));
762
763 } else do {
764
765 if( (__state[1] & kIOServiceNeedConfigState))
766 doServiceMatch( options );
767
768 lockForArbitration();
769 IOLockLock( gIOServiceBusyLock );
770
771 waitAgain = ((prevBusy < (__state[1] & kIOServiceBusyStateMask))
772 && (0 == (__state[0] & kIOServiceInactiveState)));
773
774 if( waitAgain)
775 __state[1] |= kIOServiceSyncPubState | kIOServiceBusyWaiterState;
776 else
777 __state[1] &= ~kIOServiceSyncPubState;
778
779 unlockForArbitration();
780
781 if( waitAgain)
782 assert_wait( (event_t) this/*&__state[1]*/, THREAD_UNINT);
783
784 IOLockUnlock( gIOServiceBusyLock );
785 if( waitAgain)
786 thread_block(THREAD_CONTINUE_NULL);
787
788 } while( waitAgain );
789 }
790 }
791
792 IOReturn IOService::catalogNewDrivers( OSOrderedSet * newTables )
793 {
794 OSDictionary * table;
795 OSSet * set;
796 OSSet * allSet = 0;
797 IOService * service;
798 #if IOMATCHDEBUG
799 SInt32 count = 0;
800 #endif
801
802 newTables->retain();
803
804 while( (table = (OSDictionary *) newTables->getFirstObject())) {
805
806 LOCKWRITENOTIFY();
807 set = (OSSet *) copyExistingServices( table,
808 kIOServiceRegisteredState,
809 kIOServiceExistingSet);
810 UNLOCKNOTIFY();
811 if( set) {
812
813 #if IOMATCHDEBUG
814 count += set->getCount();
815 #endif
816 if (allSet) {
817 allSet->merge((const OSSet *) set);
818 set->release();
819 }
820 else
821 allSet = set;
822 }
823
824 #if IOMATCHDEBUG
825 if( getDebugFlags( table ) & kIOLogMatch)
826 LOG("Matching service count = %ld\n", (long)count);
827 #endif
828 newTables->removeObject(table);
829 }
830
831 if (allSet) {
832 while( (service = (IOService *) allSet->getAnyObject())) {
833 service->startMatching(kIOServiceAsynchronous);
834 allSet->removeObject(service);
835 }
836 allSet->release();
837 }
838
839 newTables->release();
840
841 return( kIOReturnSuccess );
842 }
843
844 _IOServiceJob * _IOServiceJob::startJob( IOService * nub, int type,
845 IOOptionBits options )
846 {
847 _IOServiceJob * job;
848
849 job = new _IOServiceJob;
850 if( job && !job->init()) {
851 job->release();
852 job = 0;
853 }
854
855 if( job) {
856 job->type = type;
857 job->nub = nub;
858 job->options = options;
859 nub->retain(); // thread will release()
860 pingConfig( job );
861 }
862
863 return( job );
864 }
865
866 /*
867 * Called on a registered service to see if it matches
868 * a property table.
869 */
870
871 bool IOService::matchPropertyTable( OSDictionary * table, SInt32 * score )
872 {
873 return( matchPropertyTable(table) );
874 }
875
876 bool IOService::matchPropertyTable( OSDictionary * table )
877 {
878 return( true );
879 }
880
881 /*
882 * Called on a matched service to allocate resources
883 * before first driver is attached.
884 */
885
886 IOReturn IOService::getResources( void )
887 {
888 return( kIOReturnSuccess);
889 }
890
891 /*
892 * Client/provider accessors
893 */
894
895 IOService * IOService::getProvider( void ) const
896 {
897 IOService * self = (IOService *) this;
898 IOService * parent;
899 SInt32 generation;
900
901 generation = getGenerationCount();
902 if( __providerGeneration == generation)
903 return( __provider );
904
905 parent = (IOService *) getParentEntry( gIOServicePlane);
906 if( parent == IORegistryEntry::getRegistryRoot())
907 /* root is not an IOService */
908 parent = 0;
909
910 self->__provider = parent;
911 OSMemoryBarrier();
912 // save the count from before call to getParentEntry()
913 self->__providerGeneration = generation;
914
915 return( parent );
916 }
917
918 IOWorkLoop * IOService::getWorkLoop() const
919 {
920 IOService *provider = getProvider();
921
922 if (provider)
923 return provider->getWorkLoop();
924 else
925 return 0;
926 }
927
928 OSIterator * IOService::getProviderIterator( void ) const
929 {
930 return( getParentIterator( gIOServicePlane));
931 }
932
933 IOService * IOService::getClient( void ) const
934 {
935 return( (IOService *) getChildEntry( gIOServicePlane));
936 }
937
938 OSIterator * IOService::getClientIterator( void ) const
939 {
940 return( getChildIterator( gIOServicePlane));
941 }
942
943 OSIterator * _IOOpenServiceIterator::iterator( OSIterator * _iter,
944 const IOService * client,
945 const IOService * provider )
946 {
947 _IOOpenServiceIterator * inst;
948
949 if( !_iter)
950 return( 0 );
951
952 inst = new _IOOpenServiceIterator;
953
954 if( inst && !inst->init()) {
955 inst->release();
956 inst = 0;
957 }
958 if( inst) {
959 inst->iter = _iter;
960 inst->client = client;
961 inst->provider = provider;
962 }
963
964 return( inst );
965 }
966
967 void _IOOpenServiceIterator::free()
968 {
969 iter->release();
970 if( last)
971 last->unlockForArbitration();
972 OSIterator::free();
973 }
974
975 OSObject * _IOOpenServiceIterator::getNextObject()
976 {
977 IOService * next;
978
979 if( last)
980 last->unlockForArbitration();
981
982 while( (next = (IOService *) iter->getNextObject())) {
983
984 next->lockForArbitration();
985 if( (client && (next->isOpen( client )))
986 || (provider && (provider->isOpen( next ))) )
987 break;
988 next->unlockForArbitration();
989 }
990
991 last = next;
992
993 return( next );
994 }
995
996 bool _IOOpenServiceIterator::isValid()
997 {
998 return( iter->isValid() );
999 }
1000
1001 void _IOOpenServiceIterator::reset()
1002 {
1003 if( last) {
1004 last->unlockForArbitration();
1005 last = 0;
1006 }
1007 iter->reset();
1008 }
1009
1010 OSIterator * IOService::getOpenProviderIterator( void ) const
1011 {
1012 return( _IOOpenServiceIterator::iterator( getProviderIterator(), this, 0 ));
1013 }
1014
1015 OSIterator * IOService::getOpenClientIterator( void ) const
1016 {
1017 return( _IOOpenServiceIterator::iterator( getClientIterator(), 0, this ));
1018 }
1019
1020
1021 IOReturn IOService::callPlatformFunction( const OSSymbol * functionName,
1022 bool waitForFunction,
1023 void *param1, void *param2,
1024 void *param3, void *param4 )
1025 {
1026 IOReturn result = kIOReturnUnsupported;
1027 IOService *provider;
1028
1029 if (gIOPlatformFunctionHandlerSet == functionName)
1030 {
1031 #if defined(__i386__) || defined(__x86_64__)
1032 const OSSymbol * functionHandlerName = (const OSSymbol *) param1;
1033 IOService * target = (IOService *) param2;
1034 bool enable = (param3 != 0);
1035
1036 if (sCPULatencyFunctionName[kCpuDelayBusStall] == functionHandlerName)
1037 result = setLatencyHandler(kCpuDelayBusStall, target, enable);
1038 else if (sCPULatencyFunctionName[kCpuDelayInterrupt] == param1)
1039 result = setLatencyHandler(kCpuDelayInterrupt, target, enable);
1040 #endif /* defined(__i386__) || defined(__x86_64__) */
1041 }
1042
1043 if ((kIOReturnUnsupported == result) && (provider = getProvider())) {
1044 result = provider->callPlatformFunction(functionName, waitForFunction,
1045 param1, param2, param3, param4);
1046 }
1047
1048 return result;
1049 }
1050
1051 IOReturn IOService::callPlatformFunction( const char * functionName,
1052 bool waitForFunction,
1053 void *param1, void *param2,
1054 void *param3, void *param4 )
1055 {
1056 IOReturn result = kIOReturnNoMemory;
1057 const OSSymbol *functionSymbol = OSSymbol::withCString(functionName);
1058
1059 if (functionSymbol != 0) {
1060 result = callPlatformFunction(functionSymbol, waitForFunction,
1061 param1, param2, param3, param4);
1062 functionSymbol->release();
1063 }
1064
1065 return result;
1066 }
1067
1068
1069 /*
1070 * Accessors for global services
1071 */
1072
1073 IOPlatformExpert * IOService::getPlatform( void )
1074 {
1075 return( gIOPlatform);
1076 }
1077
1078 class IOPMrootDomain * IOService::getPMRootDomain( void )
1079 {
1080 return( gIOPMRootDomain);
1081 }
1082
1083 IOService * IOService::getResourceService( void )
1084 {
1085 return( gIOResources );
1086 }
1087
1088 void IOService::setPlatform( IOPlatformExpert * platform)
1089 {
1090 gIOPlatform = platform;
1091 gIOResources->attachToParent( gIOServiceRoot, gIOServicePlane );
1092
1093 #if defined(__i386__) || defined(__x86_64__)
1094
1095 static const char * keys[kCpuNumDelayTypes] = {
1096 kIOPlatformMaxBusDelay, kIOPlatformMaxInterruptDelay };
1097 const OSObject * objs[2];
1098 OSArray * array;
1099 uint32_t idx;
1100
1101 for (idx = 0; idx < kCpuNumDelayTypes; idx++)
1102 {
1103 objs[0] = sCPULatencySet[idx];
1104 objs[1] = sCPULatencyHolder[idx];
1105 array = OSArray::withObjects(objs, 2);
1106 if (!array) break;
1107 platform->setProperty(keys[idx], array);
1108 array->release();
1109 }
1110 #endif /* defined(__i386__) || defined(__x86_64__) */
1111 }
1112
1113 void IOService::setPMRootDomain( class IOPMrootDomain * rootDomain)
1114 {
1115 gIOPMRootDomain = rootDomain;
1116 publishResource("IOKit");
1117 }
1118
1119 /*
1120 * Stacking change
1121 */
1122
1123 bool IOService::lockForArbitration( bool isSuccessRequired )
1124 {
1125 bool found;
1126 bool success;
1127 ArbitrationLockQueueElement * element;
1128 ArbitrationLockQueueElement * active;
1129 ArbitrationLockQueueElement * waiting;
1130
1131 enum { kPutOnFreeQueue, kPutOnActiveQueue, kPutOnWaitingQueue } action;
1132
1133 // lock global access
1134 IOTakeLock( gArbitrationLockQueueLock );
1135
1136 // obtain an unused queue element
1137 if( !queue_empty( &gArbitrationLockQueueFree )) {
1138 queue_remove_first( &gArbitrationLockQueueFree,
1139 element,
1140 ArbitrationLockQueueElement *,
1141 link );
1142 } else {
1143 element = IONew( ArbitrationLockQueueElement, 1 );
1144 assert( element );
1145 }
1146
1147 // prepare the queue element
1148 element->thread = IOThreadSelf();
1149 element->service = this;
1150 element->count = 1;
1151 element->required = isSuccessRequired;
1152 element->aborted = false;
1153
1154 // determine whether this object is already locked (ie. on active queue)
1155 found = false;
1156 queue_iterate( &gArbitrationLockQueueActive,
1157 active,
1158 ArbitrationLockQueueElement *,
1159 link )
1160 {
1161 if( active->service == element->service ) {
1162 found = true;
1163 break;
1164 }
1165 }
1166
1167 if( found ) { // this object is already locked
1168
1169 // determine whether it is the same or a different thread trying to lock
1170 if( active->thread != element->thread ) { // it is a different thread
1171
1172 ArbitrationLockQueueElement * victim = 0;
1173
1174 // before placing this new thread on the waiting queue, we look for
1175 // a deadlock cycle...
1176
1177 while( 1 ) {
1178 // determine whether the active thread holding the object we
1179 // want is waiting for another object to be unlocked
1180 found = false;
1181 queue_iterate( &gArbitrationLockQueueWaiting,
1182 waiting,
1183 ArbitrationLockQueueElement *,
1184 link )
1185 {
1186 if( waiting->thread == active->thread ) {
1187 assert( false == waiting->aborted );
1188 found = true;
1189 break;
1190 }
1191 }
1192
1193 if( found ) { // yes, active thread waiting for another object
1194
1195 // this may be a candidate for rejection if the required
1196 // flag is not set, should we detect a deadlock later on
1197 if( false == waiting->required )
1198 victim = waiting;
1199
1200 // find the thread that is holding this other object, that
1201 // is blocking the active thread from proceeding (fun :-)
1202 found = false;
1203 queue_iterate( &gArbitrationLockQueueActive,
1204 active, // (reuse active queue element)
1205 ArbitrationLockQueueElement *,
1206 link )
1207 {
1208 if( active->service == waiting->service ) {
1209 found = true;
1210 break;
1211 }
1212 }
1213
1214 // someone must be holding it or it wouldn't be waiting
1215 assert( found );
1216
1217 if( active->thread == element->thread ) {
1218
1219 // doh, it's waiting for the thread that originated
1220 // this whole lock (ie. current thread) -> deadlock
1221 if( false == element->required ) { // willing to fail?
1222
1223 // the originating thread doesn't have the required
1224 // flag, so it can fail
1225 success = false; // (fail originating lock request)
1226 break; // (out of while)
1227
1228 } else { // originating thread is not willing to fail
1229
1230 // see if we came across a waiting thread that did
1231 // not have the 'required' flag set: we'll fail it
1232 if( victim ) {
1233
1234 // we do have a willing victim, fail it's lock
1235 victim->aborted = true;
1236
1237 // take the victim off the waiting queue
1238 queue_remove( &gArbitrationLockQueueWaiting,
1239 victim,
1240 ArbitrationLockQueueElement *,
1241 link );
1242
1243 // wake the victim
1244 IOLockWakeup( gArbitrationLockQueueLock,
1245 victim,
1246 /* one thread */ true );
1247
1248 // allow this thread to proceed (ie. wait)
1249 success = true; // (put request on wait queue)
1250 break; // (out of while)
1251 } else {
1252
1253 // all the waiting threads we came across in
1254 // finding this loop had the 'required' flag
1255 // set, so we've got a deadlock we can't avoid
1256 panic("I/O Kit: Unrecoverable deadlock.");
1257 }
1258 }
1259 } else {
1260 // repeat while loop, redefining active thread to be the
1261 // thread holding "this other object" (see above), and
1262 // looking for threads waiting on it; note the active
1263 // variable points to "this other object" already... so
1264 // there nothing to do in this else clause.
1265 }
1266 } else { // no, active thread is not waiting for another object
1267
1268 success = true; // (put request on wait queue)
1269 break; // (out of while)
1270 }
1271 } // while forever
1272
1273 if( success ) { // put the request on the waiting queue?
1274 kern_return_t wait_result;
1275
1276 // place this thread on the waiting queue and put it to sleep;
1277 // we place it at the tail of the queue...
1278 queue_enter( &gArbitrationLockQueueWaiting,
1279 element,
1280 ArbitrationLockQueueElement *,
1281 link );
1282
1283 // declare that this thread will wait for a given event
1284 restart_sleep: wait_result = assert_wait( element,
1285 element->required ? THREAD_UNINT
1286 : THREAD_INTERRUPTIBLE );
1287
1288 // unlock global access
1289 IOUnlock( gArbitrationLockQueueLock );
1290
1291 // put thread to sleep, waiting for our event to fire...
1292 if (wait_result == THREAD_WAITING)
1293 wait_result = thread_block(THREAD_CONTINUE_NULL);
1294
1295
1296 // ...and we've been woken up; we might be in one of two states:
1297 // (a) we've been aborted and our queue element is not on
1298 // any of the three queues, but is floating around
1299 // (b) we're allowed to proceed with the lock and we have
1300 // already been moved from the waiting queue to the
1301 // active queue.
1302 // ...plus a 3rd state, should the thread have been interrupted:
1303 // (c) we're still on the waiting queue
1304
1305 // determine whether we were interrupted out of our sleep
1306 if( THREAD_INTERRUPTED == wait_result ) {
1307
1308 // re-lock global access
1309 IOTakeLock( gArbitrationLockQueueLock );
1310
1311 // determine whether we're still on the waiting queue
1312 found = false;
1313 queue_iterate( &gArbitrationLockQueueWaiting,
1314 waiting, // (reuse waiting queue element)
1315 ArbitrationLockQueueElement *,
1316 link )
1317 {
1318 if( waiting == element ) {
1319 found = true;
1320 break;
1321 }
1322 }
1323
1324 if( found ) { // yes, we're still on the waiting queue
1325
1326 // determine whether we're willing to fail
1327 if( false == element->required ) {
1328
1329 // mark us as aborted
1330 element->aborted = true;
1331
1332 // take us off the waiting queue
1333 queue_remove( &gArbitrationLockQueueWaiting,
1334 element,
1335 ArbitrationLockQueueElement *,
1336 link );
1337 } else { // we are not willing to fail
1338
1339 // ignore interruption, go back to sleep
1340 goto restart_sleep;
1341 }
1342 }
1343
1344 // unlock global access
1345 IOUnlock( gArbitrationLockQueueLock );
1346
1347 // proceed as though this were a normal wake up
1348 wait_result = THREAD_AWAKENED;
1349 }
1350
1351 assert( THREAD_AWAKENED == wait_result );
1352
1353 // determine whether we've been aborted while we were asleep
1354 if( element->aborted ) {
1355 assert( false == element->required );
1356
1357 // re-lock global access
1358 IOTakeLock( gArbitrationLockQueueLock );
1359
1360 action = kPutOnFreeQueue;
1361 success = false;
1362 } else { // we weren't aborted, so we must be ready to go :-)
1363
1364 // we've already been moved from waiting to active queue
1365 return true;
1366 }
1367
1368 } else { // the lock request is to be failed
1369
1370 // return unused queue element to queue
1371 action = kPutOnFreeQueue;
1372 }
1373 } else { // it is the same thread, recursive access is allowed
1374
1375 // add one level of recursion
1376 active->count++;
1377
1378 // return unused queue element to queue
1379 action = kPutOnFreeQueue;
1380 success = true;
1381 }
1382 } else { // this object is not already locked, so let this thread through
1383 action = kPutOnActiveQueue;
1384 success = true;
1385 }
1386
1387 // put the new element on a queue
1388 if( kPutOnActiveQueue == action ) {
1389 queue_enter( &gArbitrationLockQueueActive,
1390 element,
1391 ArbitrationLockQueueElement *,
1392 link );
1393 } else if( kPutOnFreeQueue == action ) {
1394 queue_enter( &gArbitrationLockQueueFree,
1395 element,
1396 ArbitrationLockQueueElement *,
1397 link );
1398 } else {
1399 assert( 0 ); // kPutOnWaitingQueue never occurs, handled specially above
1400 }
1401
1402 // unlock global access
1403 IOUnlock( gArbitrationLockQueueLock );
1404
1405 return( success );
1406 }
1407
1408 void IOService::unlockForArbitration( void )
1409 {
1410 bool found;
1411 ArbitrationLockQueueElement * element;
1412
1413 // lock global access
1414 IOTakeLock( gArbitrationLockQueueLock );
1415
1416 // find the lock element for this object (ie. on active queue)
1417 found = false;
1418 queue_iterate( &gArbitrationLockQueueActive,
1419 element,
1420 ArbitrationLockQueueElement *,
1421 link )
1422 {
1423 if( element->service == this ) {
1424 found = true;
1425 break;
1426 }
1427 }
1428
1429 assert( found );
1430
1431 // determine whether the lock has been taken recursively
1432 if( element->count > 1 ) {
1433 // undo one level of recursion
1434 element->count--;
1435
1436 } else {
1437
1438 // remove it from the active queue
1439 queue_remove( &gArbitrationLockQueueActive,
1440 element,
1441 ArbitrationLockQueueElement *,
1442 link );
1443
1444 // put it on the free queue
1445 queue_enter( &gArbitrationLockQueueFree,
1446 element,
1447 ArbitrationLockQueueElement *,
1448 link );
1449
1450 // determine whether a thread is waiting for object (head to tail scan)
1451 found = false;
1452 queue_iterate( &gArbitrationLockQueueWaiting,
1453 element,
1454 ArbitrationLockQueueElement *,
1455 link )
1456 {
1457 if( element->service == this ) {
1458 found = true;
1459 break;
1460 }
1461 }
1462
1463 if ( found ) { // we found an interested thread on waiting queue
1464
1465 // remove it from the waiting queue
1466 queue_remove( &gArbitrationLockQueueWaiting,
1467 element,
1468 ArbitrationLockQueueElement *,
1469 link );
1470
1471 // put it on the active queue
1472 queue_enter( &gArbitrationLockQueueActive,
1473 element,
1474 ArbitrationLockQueueElement *,
1475 link );
1476
1477 // wake the waiting thread
1478 IOLockWakeup( gArbitrationLockQueueLock,
1479 element,
1480 /* one thread */ true );
1481 }
1482 }
1483
1484 // unlock global access
1485 IOUnlock( gArbitrationLockQueueLock );
1486 }
1487
1488 void IOService::applyToProviders( IOServiceApplierFunction applier,
1489 void * context )
1490 {
1491 applyToParents( (IORegistryEntryApplierFunction) applier,
1492 context, gIOServicePlane );
1493 }
1494
1495 void IOService::applyToClients( IOServiceApplierFunction applier,
1496 void * context )
1497 {
1498 applyToChildren( (IORegistryEntryApplierFunction) applier,
1499 context, gIOServicePlane );
1500 }
1501
1502
1503 /*
1504 * Client messages
1505 */
1506
1507
1508 // send a message to a client or interested party of this service
1509 IOReturn IOService::messageClient( UInt32 type, OSObject * client,
1510 void * argument, vm_size_t argSize )
1511 {
1512 IOReturn ret;
1513 IOService * service;
1514 _IOServiceInterestNotifier * notify;
1515
1516 if( (service = OSDynamicCast( IOService, client)))
1517 ret = service->message( type, this, argument );
1518
1519 else if( (notify = OSDynamicCast( _IOServiceInterestNotifier, client))) {
1520
1521 _IOServiceNotifierInvocation invocation;
1522 bool willNotify;
1523
1524 invocation.thread = current_thread();
1525
1526 LOCKWRITENOTIFY();
1527 willNotify = (0 != (kIOServiceNotifyEnable & notify->state));
1528
1529 if( willNotify) {
1530 queue_enter( &notify->handlerInvocations, &invocation,
1531 _IOServiceNotifierInvocation *, link );
1532 }
1533 UNLOCKNOTIFY();
1534
1535 if( willNotify) {
1536
1537 ret = (*notify->handler)( notify->target, notify->ref,
1538 type, this, argument, argSize );
1539
1540 LOCKWRITENOTIFY();
1541 queue_remove( &notify->handlerInvocations, &invocation,
1542 _IOServiceNotifierInvocation *, link );
1543 if( kIOServiceNotifyWaiter & notify->state) {
1544 notify->state &= ~kIOServiceNotifyWaiter;
1545 WAKEUPNOTIFY( notify );
1546 }
1547 UNLOCKNOTIFY();
1548
1549 } else
1550 ret = kIOReturnSuccess;
1551
1552 } else
1553 ret = kIOReturnBadArgument;
1554
1555 return( ret );
1556 }
1557
1558 static void
1559 applyToInterestNotifiers(const IORegistryEntry *target,
1560 const OSSymbol * typeOfInterest,
1561 OSObjectApplierFunction applier,
1562 void * context )
1563 {
1564 OSArray * copyArray = 0;
1565
1566 LOCKREADNOTIFY();
1567
1568 IOCommand *notifyList =
1569 OSDynamicCast( IOCommand, target->getProperty( typeOfInterest ));
1570
1571 if( notifyList) {
1572 copyArray = OSArray::withCapacity(1);
1573
1574 // iterate over queue, entry is set to each element in the list
1575 iterqueue(&notifyList->fCommandChain, entry) {
1576 _IOServiceInterestNotifier * notify;
1577
1578 queue_element(entry, notify, _IOServiceInterestNotifier *, chain);
1579 copyArray->setObject(notify);
1580 }
1581 }
1582 UNLOCKNOTIFY();
1583
1584 if( copyArray) {
1585 unsigned int index;
1586 OSObject * next;
1587
1588 for( index = 0; (next = copyArray->getObject( index )); index++)
1589 (*applier)(next, context);
1590 copyArray->release();
1591 }
1592 }
1593
1594 void IOService::applyToInterested( const OSSymbol * typeOfInterest,
1595 OSObjectApplierFunction applier,
1596 void * context )
1597 {
1598 if (gIOGeneralInterest == typeOfInterest)
1599 applyToClients( (IOServiceApplierFunction) applier, context );
1600 applyToInterestNotifiers(this, typeOfInterest, applier, context);
1601 }
1602
1603 struct MessageClientsContext {
1604 IOService * service;
1605 UInt32 type;
1606 void * argument;
1607 vm_size_t argSize;
1608 IOReturn ret;
1609 };
1610
1611 static void messageClientsApplier( OSObject * object, void * ctx )
1612 {
1613 IOReturn ret;
1614 MessageClientsContext * context = (MessageClientsContext *) ctx;
1615
1616 ret = context->service->messageClient( context->type,
1617 object, context->argument, context->argSize );
1618 if( kIOReturnSuccess != ret)
1619 context->ret = ret;
1620 }
1621
1622 // send a message to all clients
1623 IOReturn IOService::messageClients( UInt32 type,
1624 void * argument, vm_size_t argSize )
1625 {
1626 MessageClientsContext context;
1627
1628 context.service = this;
1629 context.type = type;
1630 context.argument = argument;
1631 context.argSize = argSize;
1632 context.ret = kIOReturnSuccess;
1633
1634 applyToInterested( gIOGeneralInterest,
1635 &messageClientsApplier, &context );
1636
1637 return( context.ret );
1638 }
1639
1640 IOReturn IOService::acknowledgeNotification( IONotificationRef notification,
1641 IOOptionBits response )
1642 {
1643 return( kIOReturnUnsupported );
1644 }
1645
1646 IONotifier * IOService::registerInterest( const OSSymbol * typeOfInterest,
1647 IOServiceInterestHandler handler, void * target, void * ref )
1648 {
1649 _IOServiceInterestNotifier * notify = 0;
1650 IOReturn rc = kIOReturnError;
1651
1652 notify = new _IOServiceInterestNotifier;
1653 if (!notify) return NULL;
1654
1655 if(notify->init()) {
1656 rc = registerInterestForNotifer(notify, typeOfInterest,
1657 handler, target, ref);
1658 }
1659
1660 if (rc != kIOReturnSuccess) {
1661 notify->release();
1662 notify = 0;
1663 }
1664
1665 return( notify );
1666 }
1667
1668 IOReturn IOService::registerInterestForNotifer( IONotifier *svcNotify, const OSSymbol * typeOfInterest,
1669 IOServiceInterestHandler handler, void * target, void * ref )
1670 {
1671 IOReturn rc = kIOReturnSuccess;
1672 _IOServiceInterestNotifier *notify = 0;
1673
1674 if( (typeOfInterest != gIOGeneralInterest)
1675 && (typeOfInterest != gIOBusyInterest)
1676 && (typeOfInterest != gIOAppPowerStateInterest)
1677 && (typeOfInterest != gIOConsoleSecurityInterest)
1678 && (typeOfInterest != gIOPriorityPowerStateInterest))
1679 return( kIOReturnBadArgument );
1680
1681 if (!svcNotify || !(notify = OSDynamicCast(_IOServiceInterestNotifier, svcNotify)))
1682 return( kIOReturnBadArgument );
1683
1684 lockForArbitration();
1685 if( 0 == (__state[0] & kIOServiceInactiveState)) {
1686
1687 notify->handler = handler;
1688 notify->target = target;
1689 notify->ref = ref;
1690 notify->state = kIOServiceNotifyEnable;
1691
1692 ////// queue
1693
1694 LOCKWRITENOTIFY();
1695
1696 // Get the head of the notifier linked list
1697 IOCommand * notifyList;
1698 OSObject * obj = copyProperty( typeOfInterest );
1699 if (!(notifyList = OSDynamicCast(IOCommand, obj))) {
1700 notifyList = OSTypeAlloc(IOCommand);
1701 if (notifyList) {
1702 notifyList->init();
1703 bool ok = setProperty( typeOfInterest, notifyList);
1704 notifyList->release();
1705 if (!ok) notifyList = 0;
1706 }
1707 }
1708 if (obj) obj->release();
1709
1710 if (notifyList) {
1711 enqueue(&notifyList->fCommandChain, &notify->chain);
1712 notify->retain(); // ref'ed while in list
1713 }
1714
1715 UNLOCKNOTIFY();
1716 }
1717 else {
1718 rc = kIOReturnNotReady;
1719 }
1720 unlockForArbitration();
1721
1722 return rc;
1723 }
1724
1725 static void cleanInterestList( OSObject * head )
1726 {
1727 IOCommand *notifyHead = OSDynamicCast(IOCommand, head);
1728 if (!notifyHead)
1729 return;
1730
1731 LOCKWRITENOTIFY();
1732 while ( queue_entry_t entry = dequeue(&notifyHead->fCommandChain) ) {
1733 queue_next(entry) = queue_prev(entry) = 0;
1734
1735 _IOServiceInterestNotifier * notify;
1736
1737 queue_element(entry, notify, _IOServiceInterestNotifier *, chain);
1738 notify->release();
1739 }
1740 UNLOCKNOTIFY();
1741 }
1742
1743 void IOService::unregisterAllInterest( void )
1744 {
1745 cleanInterestList( getProperty( gIOGeneralInterest ));
1746 cleanInterestList( getProperty( gIOBusyInterest ));
1747 cleanInterestList( getProperty( gIOAppPowerStateInterest ));
1748 cleanInterestList( getProperty( gIOPriorityPowerStateInterest ));
1749 cleanInterestList( getProperty( gIOConsoleSecurityInterest ));
1750 }
1751
1752 /*
1753 * _IOServiceInterestNotifier
1754 */
1755
1756 // wait for all threads, other than the current one,
1757 // to exit the handler
1758
1759 void _IOServiceInterestNotifier::wait()
1760 {
1761 _IOServiceNotifierInvocation * next;
1762 bool doWait;
1763
1764 do {
1765 doWait = false;
1766 queue_iterate( &handlerInvocations, next,
1767 _IOServiceNotifierInvocation *, link) {
1768 if( next->thread != current_thread() ) {
1769 doWait = true;
1770 break;
1771 }
1772 }
1773 if( doWait) {
1774 state |= kIOServiceNotifyWaiter;
1775 SLEEPNOTIFY(this);
1776 }
1777
1778 } while( doWait );
1779 }
1780
1781 void _IOServiceInterestNotifier::free()
1782 {
1783 assert( queue_empty( &handlerInvocations ));
1784 OSObject::free();
1785 }
1786
1787 void _IOServiceInterestNotifier::remove()
1788 {
1789 LOCKWRITENOTIFY();
1790
1791 if( queue_next( &chain )) {
1792 remqueue(&chain);
1793 queue_next( &chain) = queue_prev( &chain) = 0;
1794 release();
1795 }
1796
1797 state &= ~kIOServiceNotifyEnable;
1798
1799 wait();
1800
1801 UNLOCKNOTIFY();
1802
1803 release();
1804 }
1805
1806 bool _IOServiceInterestNotifier::disable()
1807 {
1808 bool ret;
1809
1810 LOCKWRITENOTIFY();
1811
1812 ret = (0 != (kIOServiceNotifyEnable & state));
1813 state &= ~kIOServiceNotifyEnable;
1814 if( ret)
1815 wait();
1816
1817 UNLOCKNOTIFY();
1818
1819 return( ret );
1820 }
1821
1822 void _IOServiceInterestNotifier::enable( bool was )
1823 {
1824 LOCKWRITENOTIFY();
1825 if( was)
1826 state |= kIOServiceNotifyEnable;
1827 else
1828 state &= ~kIOServiceNotifyEnable;
1829 UNLOCKNOTIFY();
1830 }
1831
1832 bool _IOServiceInterestNotifier::init()
1833 {
1834 queue_init( &handlerInvocations );
1835 return (OSObject::init());
1836 }
1837 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1838
1839 /*
1840 * Termination
1841 */
1842
1843 #define tailQ(o) setObject(o)
1844 #define headQ(o) setObject(0, o)
1845 #define TLOG(fmt, args...) { if(kIOLogYield & gIOKitDebug) { IOLog("[%llx] ", thread_tid(current_thread())); IOLog(fmt, ## args); }}
1846
1847 static void _workLoopAction( IOWorkLoop::Action action,
1848 IOService * service,
1849 void * p0 = 0, void * p1 = 0,
1850 void * p2 = 0, void * p3 = 0 )
1851 {
1852 IOWorkLoop * wl;
1853
1854 if( (wl = service->getWorkLoop())) {
1855 wl->retain();
1856 wl->runAction( action, service, p0, p1, p2, p3 );
1857 wl->release();
1858 } else
1859 (*action)( service, p0, p1, p2, p3 );
1860 }
1861
1862 bool IOService::requestTerminate( IOService * provider, IOOptionBits options )
1863 {
1864 bool ok;
1865
1866 // if its our only provider
1867 ok = isParent( provider, gIOServicePlane, true);
1868
1869 // -- compat
1870 if( ok) {
1871 provider->terminateClient( this, options | kIOServiceRecursing );
1872 ok = (0 != (kIOServiceInactiveState & __state[0]));
1873 }
1874 // --
1875
1876 return( ok );
1877 }
1878
1879 bool IOService::terminatePhase1( IOOptionBits options )
1880 {
1881 IOService * victim;
1882 IOService * client;
1883 OSIterator * iter;
1884 OSArray * makeInactive;
1885 OSArray * waitingInactive;
1886 int waitResult = THREAD_AWAKENED;
1887 bool wait;
1888 bool ok;
1889 bool didInactive;
1890 bool startPhase2 = false;
1891
1892 TLOG("%s[0x%qx]::terminatePhase1(%08llx)\n", getName(), getRegistryEntryID(), (long long)options);
1893
1894 uint64_t regID = getRegistryEntryID();
1895 IOServiceTrace(
1896 IOSERVICE_TERMINATE_PHASE1,
1897 (uintptr_t) regID,
1898 (uintptr_t) (regID >> 32),
1899 (uintptr_t) this,
1900 (uintptr_t) options);
1901
1902 // -- compat
1903 if( options & kIOServiceRecursing) {
1904 lockForArbitration();
1905 if (0 == (kIOServiceInactiveState & __state[0]))
1906 {
1907 __state[0] |= kIOServiceInactiveState;
1908 __state[1] |= kIOServiceRecursing | kIOServiceTermPhase1State;
1909 }
1910 unlockForArbitration();
1911
1912 return( true );
1913 }
1914 // --
1915
1916 makeInactive = OSArray::withCapacity( 16 );
1917 waitingInactive = OSArray::withCapacity( 16 );
1918 if(!makeInactive || !waitingInactive) return( false );
1919
1920 victim = this;
1921 victim->retain();
1922
1923 while( victim )
1924 {
1925 didInactive = victim->lockForArbitration( true );
1926 if( didInactive)
1927 {
1928 uint64_t regID1 = victim->getRegistryEntryID();
1929 IOServiceTrace(IOSERVICE_TERM_SET_INACTIVE,
1930 (uintptr_t) regID1,
1931 (uintptr_t) (regID1 >> 32),
1932 (uintptr_t) victim->__state[1],
1933 (uintptr_t) 0);
1934
1935 enum { kRP1 = kIOServiceRecursing | kIOServiceTermPhase1State };
1936 didInactive = (kRP1 == (victim->__state[1] & kRP1))
1937 || (0 == (victim->__state[0] & kIOServiceInactiveState));
1938
1939 if (!didInactive)
1940 {
1941 // a multiply attached IOService can be visited twice
1942 if (-1U == waitingInactive->getNextIndexOfObject(victim, 0)) do
1943 {
1944 IOLockLock(gIOServiceBusyLock);
1945 wait = (victim->__state[1] & kIOServiceTermPhase1State);
1946 if( wait) {
1947 TLOG("%s[0x%qx]::waitPhase1(%s[0x%qx])\n",
1948 getName(), getRegistryEntryID(), victim->getName(), victim->getRegistryEntryID());
1949 victim->__state[1] |= kIOServiceTerm1WaiterState;
1950 victim->unlockForArbitration();
1951 assert_wait((event_t)&victim->__state[1], THREAD_UNINT);
1952 }
1953 IOLockUnlock(gIOServiceBusyLock);
1954 if( wait) {
1955 waitResult = thread_block(THREAD_CONTINUE_NULL);
1956 TLOG("%s[0x%qx]::did waitPhase1(%s[0x%qx])\n",
1957 getName(), getRegistryEntryID(), victim->getName(), victim->getRegistryEntryID());
1958 victim->lockForArbitration();
1959 }
1960 }
1961 while (wait && (waitResult != THREAD_TIMED_OUT));
1962 }
1963 else
1964 {
1965 victim->__state[0] |= kIOServiceInactiveState;
1966 victim->__state[0] &= ~(kIOServiceRegisteredState | kIOServiceMatchedState
1967 | kIOServiceFirstPublishState | kIOServiceFirstMatchState);
1968 victim->__state[1] &= ~kIOServiceRecursing;
1969 victim->__state[1] |= kIOServiceTermPhase1State;
1970 waitingInactive->headQ(victim);
1971 if (victim == this)
1972 {
1973 if (kIOServiceTerminateNeedWillTerminate & options)
1974 {
1975 victim->__state[1] |= kIOServiceNeedWillTerminate;
1976 }
1977 }
1978 victim->_adjustBusy( 1 );
1979 }
1980 victim->unlockForArbitration();
1981 }
1982 if( victim == this) startPhase2 = didInactive;
1983 if (didInactive)
1984 {
1985 victim->deliverNotification( gIOTerminatedNotification, 0, 0xffffffff );
1986 IOUserClient::destroyUserReferences( victim );
1987
1988 iter = victim->getClientIterator();
1989 if( iter) {
1990 while( (client = (IOService *) iter->getNextObject())) {
1991 TLOG("%s[0x%qx]::requestTerminate(%s[0x%qx], %08llx)\n",
1992 client->getName(), client->getRegistryEntryID(),
1993 victim->getName(), victim->getRegistryEntryID(), (long long)options);
1994 ok = client->requestTerminate( victim, options );
1995 TLOG("%s[0x%qx]::requestTerminate(%s[0x%qx], ok = %d)\n",
1996 client->getName(), client->getRegistryEntryID(),
1997 victim->getName(), victim->getRegistryEntryID(), ok);
1998
1999 uint64_t regID1 = client->getRegistryEntryID();
2000 uint64_t regID2 = victim->getRegistryEntryID();
2001 IOServiceTrace(
2002 (ok ? IOSERVICE_TERMINATE_REQUEST_OK
2003 : IOSERVICE_TERMINATE_REQUEST_FAIL),
2004 (uintptr_t) regID1,
2005 (uintptr_t) (regID1 >> 32),
2006 (uintptr_t) regID2,
2007 (uintptr_t) (regID2 >> 32));
2008
2009 if( ok)
2010 makeInactive->setObject( client );
2011 }
2012 iter->release();
2013 }
2014 }
2015 victim->release();
2016 victim = (IOService *) makeInactive->getObject(0);
2017 if( victim) {
2018 victim->retain();
2019 makeInactive->removeObject(0);
2020 }
2021 }
2022 makeInactive->release();
2023
2024 while ((victim = (IOService *) waitingInactive->getObject(0)))
2025 {
2026 victim->retain();
2027 waitingInactive->removeObject(0);
2028
2029 victim->lockForArbitration();
2030 victim->__state[1] &= ~kIOServiceTermPhase1State;
2031 if (kIOServiceTerm1WaiterState & victim->__state[1])
2032 {
2033 victim->__state[1] &= ~kIOServiceTerm1WaiterState;
2034 TLOG("%s[0x%qx]::wakePhase1\n", victim->getName(), victim->getRegistryEntryID());
2035 IOLockLock( gIOServiceBusyLock );
2036 thread_wakeup( (event_t) &victim->__state[1]);
2037 IOLockUnlock( gIOServiceBusyLock );
2038 }
2039 victim->unlockForArbitration();
2040 victim->release();
2041 }
2042 waitingInactive->release();
2043
2044 if( startPhase2)
2045 {
2046 retain();
2047 lockForArbitration();
2048 scheduleTerminatePhase2(options);
2049 unlockForArbitration();
2050 release();
2051 }
2052
2053 return( true );
2054 }
2055
2056 void IOService::setTerminateDefer(IOService * provider, bool defer)
2057 {
2058 lockForArbitration();
2059 if (defer) __state[1] |= kIOServiceStartState;
2060 else __state[1] &= ~kIOServiceStartState;
2061 unlockForArbitration();
2062
2063 if (provider && !defer)
2064 {
2065 provider->lockForArbitration();
2066 provider->scheduleTerminatePhase2();
2067 provider->unlockForArbitration();
2068 }
2069 }
2070
2071 // call with lockForArbitration
2072 void IOService::scheduleTerminatePhase2( IOOptionBits options )
2073 {
2074 AbsoluteTime deadline;
2075 uint64_t regID1;
2076 int waitResult = THREAD_AWAKENED;
2077 bool wait, haveDeadline = false;
2078
2079 if (!(__state[0] & kIOServiceInactiveState)) return;
2080
2081 regID1 = getRegistryEntryID();
2082 IOServiceTrace(
2083 IOSERVICE_TERM_SCHED_PHASE2,
2084 (uintptr_t) regID1,
2085 (uintptr_t) (regID1 >> 32),
2086 (uintptr_t) __state[1],
2087 (uintptr_t) options);
2088
2089 if (__state[1] & kIOServiceTermPhase1State) return;
2090
2091 retain();
2092 unlockForArbitration();
2093 options |= kIOServiceRequired;
2094 IOLockLock( gJobsLock );
2095
2096 if( (options & kIOServiceSynchronous)
2097 && (current_thread() != gIOTerminateThread)) {
2098
2099 do {
2100 wait = (gIOTerminateThread != 0);
2101 if( wait) {
2102 // wait to become the terminate thread
2103 IOLockSleep( gJobsLock, &gIOTerminateThread, THREAD_UNINT);
2104 }
2105 } while( wait );
2106
2107 gIOTerminateThread = current_thread();
2108 gIOTerminatePhase2List->setObject( this );
2109 gIOTerminateWork++;
2110
2111 do {
2112 while( gIOTerminateWork )
2113 terminateWorker( options );
2114 wait = (0 != (__state[1] & kIOServiceBusyStateMask));
2115 if( wait) {
2116 // wait for the victim to go non-busy
2117 if( !haveDeadline) {
2118 clock_interval_to_deadline( 15, kSecondScale, &deadline );
2119 haveDeadline = true;
2120 }
2121 waitResult = IOLockSleepDeadline( gJobsLock, &gIOTerminateWork,
2122 deadline, THREAD_UNINT );
2123 if( waitResult == THREAD_TIMED_OUT) {
2124 IOLog("%s[0x%qx]::terminate(kIOServiceSynchronous) timeout\n", getName(), getRegistryEntryID());
2125 }
2126 }
2127 } while(gIOTerminateWork || (wait && (waitResult != THREAD_TIMED_OUT)));
2128
2129 gIOTerminateThread = 0;
2130 IOLockWakeup( gJobsLock, (event_t) &gIOTerminateThread, /* one-thread */ false);
2131
2132 } else {
2133 // ! kIOServiceSynchronous
2134
2135 gIOTerminatePhase2List->setObject( this );
2136 if( 0 == gIOTerminateWork++) {
2137 if( !gIOTerminateThread)
2138 kernel_thread_start(&terminateThread, (void *)(uintptr_t) options, &gIOTerminateThread);
2139 else
2140 IOLockWakeup(gJobsLock, (event_t) &gIOTerminateWork, /* one-thread */ false );
2141 }
2142 }
2143
2144 IOLockUnlock( gJobsLock );
2145 lockForArbitration();
2146 release();
2147 }
2148
2149 void IOService::terminateThread( void * arg, wait_result_t waitResult )
2150 {
2151 IOLockLock( gJobsLock );
2152
2153 while (gIOTerminateWork)
2154 terminateWorker( (uintptr_t) arg );
2155
2156 thread_deallocate(gIOTerminateThread);
2157 gIOTerminateThread = 0;
2158 IOLockWakeup( gJobsLock, (event_t) &gIOTerminateThread, /* one-thread */ false);
2159
2160 IOLockUnlock( gJobsLock );
2161 }
2162
2163 void IOService::scheduleStop( IOService * provider )
2164 {
2165 uint64_t regID1 = getRegistryEntryID();
2166 uint64_t regID2 = provider->getRegistryEntryID();
2167
2168 TLOG("%s[0x%qx]::scheduleStop(%s[0x%qx])\n", getName(), regID1, provider->getName(), regID2);
2169 IOServiceTrace(
2170 IOSERVICE_TERMINATE_SCHEDULE_STOP,
2171 (uintptr_t) regID1,
2172 (uintptr_t) (regID1 >> 32),
2173 (uintptr_t) regID2,
2174 (uintptr_t) (regID2 >> 32));
2175
2176 IOLockLock( gJobsLock );
2177 gIOStopList->tailQ( this );
2178 gIOStopProviderList->tailQ( provider );
2179
2180 if( 0 == gIOTerminateWork++) {
2181 if( !gIOTerminateThread)
2182 kernel_thread_start(&terminateThread, (void *) 0, &gIOTerminateThread);
2183 else
2184 IOLockWakeup(gJobsLock, (event_t) &gIOTerminateWork, /* one-thread */ false );
2185 }
2186
2187 IOLockUnlock( gJobsLock );
2188 }
2189
2190 void IOService::scheduleFinalize( void )
2191 {
2192 uint64_t regID1 = getRegistryEntryID();
2193
2194 TLOG("%s[0x%qx]::scheduleFinalize\n", getName(), regID1);
2195 IOServiceTrace(
2196 IOSERVICE_TERMINATE_SCHEDULE_FINALIZE,
2197 (uintptr_t) regID1,
2198 (uintptr_t) (regID1 >> 32),
2199 0, 0);
2200
2201 IOLockLock( gJobsLock );
2202 gIOFinalizeList->tailQ( this );
2203
2204 if( 0 == gIOTerminateWork++) {
2205 if( !gIOTerminateThread)
2206 kernel_thread_start(&terminateThread, (void *) 0, &gIOTerminateThread);
2207 else
2208 IOLockWakeup(gJobsLock, (event_t) &gIOTerminateWork, /* one-thread */ false );
2209 }
2210
2211 IOLockUnlock( gJobsLock );
2212 }
2213
2214 bool IOService::willTerminate( IOService * provider, IOOptionBits options )
2215 {
2216 return( true );
2217 }
2218
2219 bool IOService::didTerminate( IOService * provider, IOOptionBits options, bool * defer )
2220 {
2221 if( false == *defer) {
2222
2223 if( lockForArbitration( true )) {
2224 if( false == provider->handleIsOpen( this ))
2225 scheduleStop( provider );
2226 // -- compat
2227 else {
2228 message( kIOMessageServiceIsRequestingClose, provider, (void *)(uintptr_t) options );
2229 if( false == provider->handleIsOpen( this ))
2230 scheduleStop( provider );
2231 }
2232 // --
2233 unlockForArbitration();
2234 }
2235 }
2236
2237 return( true );
2238 }
2239
2240 void IOService::actionWillTerminate( IOService * victim, IOOptionBits options,
2241 OSArray * doPhase2List,
2242 void *unused2 __unused,
2243 void *unused3 __unused )
2244 {
2245 OSIterator * iter;
2246 IOService * client;
2247 bool ok;
2248 uint64_t regID1, regID2 = victim->getRegistryEntryID();
2249
2250 iter = victim->getClientIterator();
2251 if( iter) {
2252 while( (client = (IOService *) iter->getNextObject())) {
2253
2254 regID1 = client->getRegistryEntryID();
2255 TLOG("%s[0x%qx]::willTerminate(%s[0x%qx], %08llx)\n",
2256 client->getName(), regID1,
2257 victim->getName(), regID2, (long long)options);
2258 IOServiceTrace(
2259 IOSERVICE_TERMINATE_WILL,
2260 (uintptr_t) regID1,
2261 (uintptr_t) (regID1 >> 32),
2262 (uintptr_t) regID2,
2263 (uintptr_t) (regID2 >> 32));
2264
2265 ok = client->willTerminate( victim, options );
2266 doPhase2List->tailQ( client );
2267 }
2268 iter->release();
2269 }
2270 }
2271
2272 void IOService::actionDidTerminate( IOService * victim, IOOptionBits options,
2273 void *unused1 __unused, void *unused2 __unused,
2274 void *unused3 __unused )
2275 {
2276 OSIterator * iter;
2277 IOService * client;
2278 bool defer;
2279 uint64_t regID1, regID2 = victim->getRegistryEntryID();
2280
2281 victim->messageClients( kIOMessageServiceIsTerminated, (void *)(uintptr_t) options );
2282
2283 iter = victim->getClientIterator();
2284 if( iter) {
2285 while( (client = (IOService *) iter->getNextObject())) {
2286
2287 regID1 = client->getRegistryEntryID();
2288 TLOG("%s[0x%qx]::didTerminate(%s[0x%qx], %08llx)\n",
2289 client->getName(), regID1,
2290 victim->getName(), regID2, (long long)options);
2291 defer = false;
2292 client->didTerminate( victim, options, &defer );
2293
2294 IOServiceTrace(
2295 (defer ? IOSERVICE_TERMINATE_DID_DEFER
2296 : IOSERVICE_TERMINATE_DID),
2297 (uintptr_t) regID1,
2298 (uintptr_t) (regID1 >> 32),
2299 (uintptr_t) regID2,
2300 (uintptr_t) (regID2 >> 32));
2301
2302 TLOG("%s[0x%qx]::didTerminate(%s[0x%qx], defer %d)\n",
2303 client->getName(), regID1,
2304 victim->getName(), regID2, defer);
2305 }
2306 iter->release();
2307 }
2308 }
2309
2310
2311 void IOService::actionWillStop( IOService * victim, IOOptionBits options,
2312 void *unused1 __unused, void *unused2 __unused,
2313 void *unused3 __unused )
2314 {
2315 OSIterator * iter;
2316 IOService * provider;
2317 bool ok;
2318 uint64_t regID1, regID2 = victim->getRegistryEntryID();
2319
2320 iter = victim->getProviderIterator();
2321 if( iter) {
2322 while( (provider = (IOService *) iter->getNextObject())) {
2323
2324 regID1 = provider->getRegistryEntryID();
2325 TLOG("%s[0x%qx]::willTerminate(%s[0x%qx], %08llx)\n",
2326 victim->getName(), regID2,
2327 provider->getName(), regID1, (long long)options);
2328 IOServiceTrace(
2329 IOSERVICE_TERMINATE_WILL,
2330 (uintptr_t) regID2,
2331 (uintptr_t) (regID2 >> 32),
2332 (uintptr_t) regID1,
2333 (uintptr_t) (regID1 >> 32));
2334
2335 ok = victim->willTerminate( provider, options );
2336 }
2337 iter->release();
2338 }
2339 }
2340
2341 void IOService::actionDidStop( IOService * victim, IOOptionBits options,
2342 void *unused1 __unused, void *unused2 __unused,
2343 void *unused3 __unused )
2344 {
2345 OSIterator * iter;
2346 IOService * provider;
2347 bool defer = false;
2348 uint64_t regID1, regID2 = victim->getRegistryEntryID();
2349
2350 iter = victim->getProviderIterator();
2351 if( iter) {
2352 while( (provider = (IOService *) iter->getNextObject())) {
2353
2354 regID1 = provider->getRegistryEntryID();
2355 TLOG("%s[0x%qx]::didTerminate(%s[0x%qx], %08llx)\n",
2356 victim->getName(), regID2,
2357 provider->getName(), regID1, (long long)options);
2358 victim->didTerminate( provider, options, &defer );
2359
2360 IOServiceTrace(
2361 (defer ? IOSERVICE_TERMINATE_DID_DEFER
2362 : IOSERVICE_TERMINATE_DID),
2363 (uintptr_t) regID2,
2364 (uintptr_t) (regID2 >> 32),
2365 (uintptr_t) regID1,
2366 (uintptr_t) (regID1 >> 32));
2367
2368 TLOG("%s[0x%qx]::didTerminate(%s[0x%qx], defer %d)\n",
2369 victim->getName(), regID2,
2370 provider->getName(), regID1, defer);
2371 }
2372 iter->release();
2373 }
2374 }
2375
2376
2377 void IOService::actionFinalize( IOService * victim, IOOptionBits options,
2378 void *unused1 __unused, void *unused2 __unused,
2379 void *unused3 __unused )
2380 {
2381 uint64_t regID1 = victim->getRegistryEntryID();
2382 TLOG("%s[0x%qx]::finalize(%08llx)\n", victim->getName(), regID1, (long long)options);
2383 IOServiceTrace(
2384 IOSERVICE_TERMINATE_FINALIZE,
2385 (uintptr_t) regID1,
2386 (uintptr_t) (regID1 >> 32),
2387 0, 0);
2388
2389 victim->finalize( options );
2390 }
2391
2392 void IOService::actionStop( IOService * provider, IOService * client,
2393 void *unused1 __unused, void *unused2 __unused,
2394 void *unused3 __unused )
2395 {
2396 uint64_t regID1 = provider->getRegistryEntryID();
2397 uint64_t regID2 = client->getRegistryEntryID();
2398
2399 TLOG("%s[0x%qx]::stop(%s[0x%qx])\n", client->getName(), regID2, provider->getName(), regID1);
2400 IOServiceTrace(
2401 IOSERVICE_TERMINATE_STOP,
2402 (uintptr_t) regID1,
2403 (uintptr_t) (regID1 >> 32),
2404 (uintptr_t) regID2,
2405 (uintptr_t) (regID2 >> 32));
2406
2407 client->stop( provider );
2408 if( provider->isOpen( client ))
2409 provider->close( client );
2410
2411 TLOG("%s[0x%qx]::detach(%s[0x%qx])\n", client->getName(), regID2, provider->getName(), regID1);
2412 client->detach( provider );
2413 }
2414
2415 void IOService::terminateWorker( IOOptionBits options )
2416 {
2417 OSArray * doPhase2List;
2418 OSArray * didPhase2List;
2419 OSSet * freeList;
2420 OSIterator * iter;
2421 UInt32 workDone;
2422 IOService * victim;
2423 IOService * client;
2424 IOService * provider;
2425 unsigned int idx;
2426 bool moreToDo;
2427 bool doPhase2;
2428 bool doPhase3;
2429
2430 options |= kIOServiceRequired;
2431
2432 doPhase2List = OSArray::withCapacity( 16 );
2433 didPhase2List = OSArray::withCapacity( 16 );
2434 freeList = OSSet::withCapacity( 16 );
2435 if( (0 == doPhase2List) || (0 == didPhase2List) || (0 == freeList))
2436 return;
2437
2438 do {
2439 workDone = gIOTerminateWork;
2440
2441 while( (victim = (IOService *) gIOTerminatePhase2List->getObject(0) )) {
2442
2443 victim->retain();
2444 gIOTerminatePhase2List->removeObject(0);
2445 IOLockUnlock( gJobsLock );
2446
2447 uint64_t regID1 = victim->getRegistryEntryID();
2448 IOServiceTrace(
2449 IOSERVICE_TERM_START_PHASE2,
2450 (uintptr_t) regID1,
2451 (uintptr_t) (regID1 >> 32),
2452 (uintptr_t) 0,
2453 (uintptr_t) 0);
2454
2455 while( victim ) {
2456
2457 doPhase2 = victim->lockForArbitration( true );
2458 if( doPhase2) {
2459 doPhase2 = (0 != (kIOServiceInactiveState & victim->__state[0]));
2460 if( doPhase2) {
2461
2462 uint64_t regID1 = victim->getRegistryEntryID();
2463 IOServiceTrace(
2464 IOSERVICE_TERM_TRY_PHASE2,
2465 (uintptr_t) regID1,
2466 (uintptr_t) (regID1 >> 32),
2467 (uintptr_t) victim->__state[1],
2468 (uintptr_t) 0);
2469
2470 doPhase2 = (0 == (victim->__state[1] & kIOServiceTermPhase2State))
2471 && (0 == (victim->__state[1] & kIOServiceConfigState));
2472
2473 if (doPhase2 && (iter = victim->getClientIterator())) {
2474 while (doPhase2 && (client = (IOService *) iter->getNextObject())) {
2475 doPhase2 = (0 == (client->__state[1] & kIOServiceStartState));
2476 if (!doPhase2)
2477 {
2478 uint64_t regID1 = client->getRegistryEntryID();
2479 IOServiceTrace(
2480 IOSERVICE_TERM_UC_DEFER,
2481 (uintptr_t) regID1,
2482 (uintptr_t) (regID1 >> 32),
2483 (uintptr_t) client->__state[1],
2484 (uintptr_t) 0);
2485 TLOG("%s[0x%qx]::defer phase2(%s[0x%qx])\n",
2486 victim->getName(), victim->getRegistryEntryID(),
2487 client->getName(), client->getRegistryEntryID());
2488 }
2489 }
2490 iter->release();
2491 }
2492 if( doPhase2)
2493 victim->__state[1] |= kIOServiceTermPhase2State;
2494 }
2495 victim->unlockForArbitration();
2496 }
2497 if( doPhase2) {
2498
2499 if (kIOServiceNeedWillTerminate & victim->__state[1]) {
2500 _workLoopAction( (IOWorkLoop::Action) &actionWillStop,
2501 victim, (void *)(uintptr_t) options, NULL );
2502 }
2503
2504 if( 0 == victim->getClient()) {
2505 // no clients - will go to finalize
2506 IOLockLock( gJobsLock );
2507 gIOFinalizeList->tailQ( victim );
2508 IOLockUnlock( gJobsLock );
2509 } else {
2510 _workLoopAction( (IOWorkLoop::Action) &actionWillTerminate,
2511 victim, (void *)(uintptr_t) options, (void *)(uintptr_t) doPhase2List );
2512 }
2513 didPhase2List->headQ( victim );
2514 }
2515 victim->release();
2516 victim = (IOService *) doPhase2List->getObject(0);
2517 if( victim) {
2518 victim->retain();
2519 doPhase2List->removeObject(0);
2520 }
2521 }
2522
2523 while( (victim = (IOService *) didPhase2List->getObject(0)) ) {
2524
2525 if( victim->lockForArbitration( true )) {
2526 victim->__state[1] |= kIOServiceTermPhase3State;
2527 victim->unlockForArbitration();
2528 }
2529 _workLoopAction( (IOWorkLoop::Action) &actionDidTerminate,
2530 victim, (void *)(uintptr_t) options );
2531 if (kIOServiceNeedWillTerminate & victim->__state[1]) {
2532 _workLoopAction( (IOWorkLoop::Action) &actionDidStop,
2533 victim, (void *)(uintptr_t) options, NULL );
2534 }
2535 didPhase2List->removeObject(0);
2536 }
2537 IOLockLock( gJobsLock );
2538 }
2539
2540 // phase 3
2541 do {
2542 doPhase3 = false;
2543 // finalize leaves
2544 while( (victim = (IOService *) gIOFinalizeList->getObject(0))) {
2545
2546 IOLockUnlock( gJobsLock );
2547 _workLoopAction( (IOWorkLoop::Action) &actionFinalize,
2548 victim, (void *)(uintptr_t) options );
2549 IOLockLock( gJobsLock );
2550 // hold off free
2551 freeList->setObject( victim );
2552 // safe if finalize list is append only
2553 gIOFinalizeList->removeObject(0);
2554 }
2555
2556 for( idx = 0;
2557 (!doPhase3) && (client = (IOService *) gIOStopList->getObject(idx)); ) {
2558
2559 provider = (IOService *) gIOStopProviderList->getObject(idx);
2560 assert( provider );
2561
2562 uint64_t regID1 = provider->getRegistryEntryID();
2563 uint64_t regID2 = client->getRegistryEntryID();
2564
2565 if( !provider->isChild( client, gIOServicePlane )) {
2566 // may be multiply queued - nop it
2567 TLOG("%s[0x%qx]::nop stop(%s[0x%qx])\n", client->getName(), regID2, provider->getName(), regID1);
2568 IOServiceTrace(
2569 IOSERVICE_TERMINATE_STOP_NOP,
2570 (uintptr_t) regID1,
2571 (uintptr_t) (regID1 >> 32),
2572 (uintptr_t) regID2,
2573 (uintptr_t) (regID2 >> 32));
2574
2575 } else {
2576 // a terminated client is not ready for stop if it has clients, skip it
2577 if( (kIOServiceInactiveState & client->__state[0]) && client->getClient()) {
2578 TLOG("%s[0x%qx]::defer stop(%s[0x%qx])\n",
2579 client->getName(), regID2,
2580 client->getClient()->getName(), client->getClient()->getRegistryEntryID());
2581 IOServiceTrace(
2582 IOSERVICE_TERMINATE_STOP_DEFER,
2583 (uintptr_t) regID1,
2584 (uintptr_t) (regID1 >> 32),
2585 (uintptr_t) regID2,
2586 (uintptr_t) (regID2 >> 32));
2587
2588 idx++;
2589 continue;
2590 }
2591
2592 IOLockUnlock( gJobsLock );
2593 _workLoopAction( (IOWorkLoop::Action) &actionStop,
2594 provider, (void *) client );
2595 IOLockLock( gJobsLock );
2596 // check the finalize list now
2597 doPhase3 = true;
2598 }
2599 // hold off free
2600 freeList->setObject( client );
2601 freeList->setObject( provider );
2602
2603 // safe if stop list is append only
2604 gIOStopList->removeObject( idx );
2605 gIOStopProviderList->removeObject( idx );
2606 idx = 0;
2607 }
2608
2609 } while( doPhase3 );
2610
2611 gIOTerminateWork -= workDone;
2612 moreToDo = (gIOTerminateWork != 0);
2613
2614 if( !moreToDo) {
2615 TLOG("iokit terminate done, %d stops remain\n", gIOStopList->getCount());
2616 IOServiceTrace(
2617 IOSERVICE_TERMINATE_DONE,
2618 (uintptr_t) gIOStopList->getCount(), 0, 0, 0);
2619 }
2620
2621 } while( moreToDo );
2622
2623 IOLockUnlock( gJobsLock );
2624
2625 freeList->release();
2626 doPhase2List->release();
2627 didPhase2List->release();
2628
2629 IOLockLock( gJobsLock );
2630 }
2631
2632 bool IOService::finalize( IOOptionBits options )
2633 {
2634 OSIterator * iter;
2635 IOService * provider;
2636 uint64_t regID1, regID2 = getRegistryEntryID();
2637
2638 iter = getProviderIterator();
2639 assert( iter );
2640
2641 if( iter) {
2642 while( (provider = (IOService *) iter->getNextObject())) {
2643
2644 // -- compat
2645 if( 0 == (__state[1] & kIOServiceTermPhase3State)) {
2646 /* we come down here on programmatic terminate */
2647
2648 regID1 = provider->getRegistryEntryID();
2649 TLOG("%s[0x%qx]::stop1(%s[0x%qx])\n", getName(), regID2, provider->getName(), regID1);
2650 IOServiceTrace(
2651 IOSERVICE_TERMINATE_STOP,
2652 (uintptr_t) regID1,
2653 (uintptr_t) (regID1 >> 32),
2654 (uintptr_t) regID2,
2655 (uintptr_t) (regID2 >> 32));
2656
2657 stop( provider );
2658 if( provider->isOpen( this ))
2659 provider->close( this );
2660 detach( provider );
2661 } else {
2662 //--
2663 if( provider->lockForArbitration( true )) {
2664 if( 0 == (provider->__state[1] & kIOServiceTermPhase3State))
2665 scheduleStop( provider );
2666 provider->unlockForArbitration();
2667 }
2668 }
2669 }
2670 iter->release();
2671 }
2672
2673 return( true );
2674 }
2675
2676 #undef tailQ
2677 #undef headQ
2678
2679 /*
2680 * Terminate
2681 */
2682
2683 void IOService::doServiceTerminate( IOOptionBits options )
2684 {
2685 }
2686
2687 // a method in case someone needs to override it
2688 bool IOService::terminateClient( IOService * client, IOOptionBits options )
2689 {
2690 bool ok;
2691
2692 if( client->isParent( this, gIOServicePlane, true))
2693 // we are the clients only provider
2694 ok = client->terminate( options );
2695 else
2696 ok = true;
2697
2698 return( ok );
2699 }
2700
2701 bool IOService::terminate( IOOptionBits options )
2702 {
2703 options |= kIOServiceTerminate;
2704
2705 return( terminatePhase1( options ));
2706 }
2707
2708 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2709
2710 /*
2711 * Open & close
2712 */
2713
2714 struct ServiceOpenMessageContext
2715 {
2716 IOService * service;
2717 UInt32 type;
2718 IOService * excludeClient;
2719 IOOptionBits options;
2720 };
2721
2722 static void serviceOpenMessageApplier( OSObject * object, void * ctx )
2723 {
2724 ServiceOpenMessageContext * context = (ServiceOpenMessageContext *) ctx;
2725
2726 if( object != context->excludeClient)
2727 context->service->messageClient( context->type, object, (void *)(uintptr_t) context->options );
2728 }
2729
2730 bool IOService::open( IOService * forClient,
2731 IOOptionBits options,
2732 void * arg )
2733 {
2734 bool ok;
2735 ServiceOpenMessageContext context;
2736
2737 context.service = this;
2738 context.type = kIOMessageServiceIsAttemptingOpen;
2739 context.excludeClient = forClient;
2740 context.options = options;
2741
2742 applyToInterested( gIOGeneralInterest,
2743 &serviceOpenMessageApplier, &context );
2744
2745 if( false == lockForArbitration(false) )
2746 return false;
2747
2748 ok = (0 == (__state[0] & kIOServiceInactiveState));
2749 if( ok)
2750 ok = handleOpen( forClient, options, arg );
2751
2752 unlockForArbitration();
2753
2754 return( ok );
2755 }
2756
2757 void IOService::close( IOService * forClient,
2758 IOOptionBits options )
2759 {
2760 bool wasClosed;
2761 bool last = false;
2762
2763 lockForArbitration();
2764
2765 wasClosed = handleIsOpen( forClient );
2766 if( wasClosed) {
2767 handleClose( forClient, options );
2768 last = (__state[1] & kIOServiceTermPhase3State);
2769 }
2770
2771 unlockForArbitration();
2772
2773 if( last)
2774 forClient->scheduleStop( this );
2775
2776 else if( wasClosed) {
2777
2778 ServiceOpenMessageContext context;
2779
2780 context.service = this;
2781 context.type = kIOMessageServiceWasClosed;
2782 context.excludeClient = forClient;
2783 context.options = options;
2784
2785 applyToInterested( gIOGeneralInterest,
2786 &serviceOpenMessageApplier, &context );
2787 }
2788 }
2789
2790 bool IOService::isOpen( const IOService * forClient ) const
2791 {
2792 IOService * self = (IOService *) this;
2793 bool ok;
2794
2795 self->lockForArbitration();
2796
2797 ok = handleIsOpen( forClient );
2798
2799 self->unlockForArbitration();
2800
2801 return( ok );
2802 }
2803
2804 bool IOService::handleOpen( IOService * forClient,
2805 IOOptionBits options,
2806 void * arg )
2807 {
2808 bool ok;
2809
2810 ok = (0 == __owner);
2811 if( ok )
2812 __owner = forClient;
2813
2814 else if( options & kIOServiceSeize ) {
2815 ok = (kIOReturnSuccess == messageClient( kIOMessageServiceIsRequestingClose,
2816 __owner, (void *)(uintptr_t) options ));
2817 if( ok && (0 == __owner ))
2818 __owner = forClient;
2819 else
2820 ok = false;
2821 }
2822 return( ok );
2823 }
2824
2825 void IOService::handleClose( IOService * forClient,
2826 IOOptionBits options )
2827 {
2828 if( __owner == forClient)
2829 __owner = 0;
2830 }
2831
2832 bool IOService::handleIsOpen( const IOService * forClient ) const
2833 {
2834 if( forClient)
2835 return( __owner == forClient );
2836 else
2837 return( __owner != forClient );
2838 }
2839
2840 /*
2841 * Probing & starting
2842 */
2843 static SInt32 IONotifyOrdering( const OSMetaClassBase * inObj1, const OSMetaClassBase * inObj2, void * ref )
2844 {
2845 const _IOServiceNotifier * obj1 = (const _IOServiceNotifier *) inObj1;
2846 const _IOServiceNotifier * obj2 = (const _IOServiceNotifier *) inObj2;
2847 SInt32 val1;
2848 SInt32 val2;
2849
2850 val1 = 0;
2851 val2 = 0;
2852
2853 if ( obj1 )
2854 val1 = obj1->priority;
2855
2856 if ( obj2 )
2857 val2 = obj2->priority;
2858
2859 return ( val1 - val2 );
2860 }
2861
2862 static SInt32 IOServiceObjectOrder( const OSObject * entry, void * ref)
2863 {
2864 OSDictionary * dict;
2865 IOService * service;
2866 _IOServiceNotifier * notify;
2867 OSSymbol * key = (OSSymbol *) ref;
2868 OSNumber * offset;
2869
2870 if( (dict = OSDynamicCast( OSDictionary, entry)))
2871 offset = OSDynamicCast(OSNumber, dict->getObject( key ));
2872 else if( (notify = OSDynamicCast( _IOServiceNotifier, entry)))
2873 return( notify->priority );
2874
2875 else if( (service = OSDynamicCast( IOService, entry)))
2876 offset = OSDynamicCast(OSNumber, service->getProperty( key ));
2877 else {
2878 assert( false );
2879 offset = 0;
2880 }
2881
2882 if( offset)
2883 return( (SInt32) offset->unsigned32BitValue());
2884 else
2885 return( kIODefaultProbeScore );
2886 }
2887
2888 SInt32 IOServiceOrdering( const OSMetaClassBase * inObj1, const OSMetaClassBase * inObj2, void * ref )
2889 {
2890 const OSObject * obj1 = (const OSObject *) inObj1;
2891 const OSObject * obj2 = (const OSObject *) inObj2;
2892 SInt32 val1;
2893 SInt32 val2;
2894
2895 val1 = 0;
2896 val2 = 0;
2897
2898 if ( obj1 )
2899 val1 = IOServiceObjectOrder( obj1, ref );
2900
2901 if ( obj2 )
2902 val2 = IOServiceObjectOrder( obj2, ref );
2903
2904 return ( val1 - val2 );
2905 }
2906
2907 IOService * IOService::copyClientWithCategory( const OSSymbol * category )
2908 {
2909 IOService * service = 0;
2910 OSIterator * iter;
2911 const OSSymbol * nextCat;
2912
2913 iter = getClientIterator();
2914 if( iter) {
2915 while( (service = (IOService *) iter->getNextObject())) {
2916 if( kIOServiceInactiveState & service->__state[0])
2917 continue;
2918 nextCat = (const OSSymbol *) OSDynamicCast( OSSymbol,
2919 service->getProperty( gIOMatchCategoryKey ));
2920 if( category == nextCat)
2921 {
2922 service->retain();
2923 break;
2924 }
2925 }
2926 iter->release();
2927 }
2928 return( service );
2929 }
2930
2931 IOService * IOService::getClientWithCategory( const OSSymbol * category )
2932 {
2933 IOService *
2934 service = copyClientWithCategory(category);
2935 if (service)
2936 service->release();
2937 return (service);
2938 }
2939
2940 bool IOService::invokeNotifer( _IOServiceNotifier * notify )
2941 {
2942 _IOServiceNotifierInvocation invocation;
2943 bool willNotify;
2944 bool ret = true;
2945
2946 invocation.thread = current_thread();
2947
2948 LOCKWRITENOTIFY();
2949 willNotify = (0 != (kIOServiceNotifyEnable & notify->state));
2950
2951 if( willNotify) {
2952 queue_enter( &notify->handlerInvocations, &invocation,
2953 _IOServiceNotifierInvocation *, link );
2954 }
2955 UNLOCKNOTIFY();
2956
2957 if( willNotify) {
2958
2959 ret = (*notify->handler)(notify->target, notify->ref, this, notify);
2960
2961 LOCKWRITENOTIFY();
2962 queue_remove( &notify->handlerInvocations, &invocation,
2963 _IOServiceNotifierInvocation *, link );
2964 if( kIOServiceNotifyWaiter & notify->state) {
2965 notify->state &= ~kIOServiceNotifyWaiter;
2966 WAKEUPNOTIFY( notify );
2967 }
2968 UNLOCKNOTIFY();
2969 }
2970
2971 return( ret );
2972 }
2973
2974 /*
2975 * Alloc and probe matching classes,
2976 * called on the provider instance
2977 */
2978
2979 void IOService::probeCandidates( OSOrderedSet * matches )
2980 {
2981 OSDictionary * match = 0;
2982 OSSymbol * symbol;
2983 IOService * inst;
2984 IOService * newInst;
2985 OSDictionary * props;
2986 SInt32 score;
2987 OSNumber * newPri;
2988 OSOrderedSet * familyMatches = 0;
2989 OSOrderedSet * startList;
2990 OSDictionary * startDict = 0;
2991 const OSSymbol * category;
2992 OSIterator * iter;
2993 _IOServiceNotifier * notify;
2994 OSObject * nextMatch = 0;
2995 bool started;
2996 bool needReloc = false;
2997 #if IOMATCHDEBUG
2998 SInt64 debugFlags;
2999 #endif
3000 IOService * client = NULL;
3001
3002
3003 assert( matches );
3004 while( !needReloc && (nextMatch = matches->getFirstObject())) {
3005
3006 nextMatch->retain();
3007 matches->removeObject(nextMatch);
3008
3009 if( (notify = OSDynamicCast( _IOServiceNotifier, nextMatch ))) {
3010
3011 lockForArbitration();
3012 if( 0 == (__state[0] & kIOServiceInactiveState))
3013 invokeNotifer( notify );
3014 unlockForArbitration();
3015 nextMatch->release();
3016 nextMatch = 0;
3017 continue;
3018
3019 } else if( !(match = OSDynamicCast( OSDictionary, nextMatch ))) {
3020 nextMatch->release();
3021 nextMatch = 0;
3022 continue;
3023 }
3024
3025 props = 0;
3026 #if IOMATCHDEBUG
3027 debugFlags = getDebugFlags( match );
3028 #endif
3029
3030 do {
3031 category = OSDynamicCast( OSSymbol,
3032 match->getObject( gIOMatchCategoryKey ));
3033 if( 0 == category)
3034 category = gIODefaultMatchCategoryKey;
3035
3036 if( (client = copyClientWithCategory(category)) ) {
3037 #if IOMATCHDEBUG
3038 if( (debugFlags & kIOLogMatch) && (this != gIOResources))
3039 LOG("%s: match category %s exists\n", getName(),
3040 category->getCStringNoCopy());
3041 #endif
3042 nextMatch->release();
3043 nextMatch = 0;
3044
3045 client->release();
3046 client = NULL;
3047
3048 continue;
3049 }
3050
3051 // create a copy now in case its modified during matching
3052 props = OSDictionary::withDictionary( match, match->getCount());
3053 if( 0 == props)
3054 continue;
3055 props->setCapacityIncrement(1);
3056
3057 // check the nub matches
3058 if( false == matchPassive(props, kIOServiceChangesOK | kIOServiceClassDone))
3059 continue;
3060
3061 // Check to see if driver reloc has been loaded.
3062 needReloc = (false == gIOCatalogue->isModuleLoaded( match ));
3063 if( needReloc) {
3064 #if IOMATCHDEBUG
3065 if( debugFlags & kIOLogCatalogue)
3066 LOG("%s: stalling for module\n", getName());
3067 #endif
3068 // If reloc hasn't been loaded, exit;
3069 // reprobing will occur after reloc has been loaded.
3070 continue;
3071 }
3072
3073 // reorder on family matchPropertyTable score.
3074 if( 0 == familyMatches)
3075 familyMatches = OSOrderedSet::withCapacity( 1,
3076 IOServiceOrdering, (void *) gIOProbeScoreKey );
3077 if( familyMatches)
3078 familyMatches->setObject( props );
3079
3080 } while( false );
3081
3082 if (nextMatch) {
3083 nextMatch->release();
3084 nextMatch = 0;
3085 }
3086 if( props)
3087 props->release();
3088 }
3089 matches->release();
3090 matches = 0;
3091
3092 if( familyMatches) {
3093
3094 while( !needReloc
3095 && (props = (OSDictionary *) familyMatches->getFirstObject())) {
3096
3097 props->retain();
3098 familyMatches->removeObject( props );
3099
3100 inst = 0;
3101 newInst = 0;
3102 #if IOMATCHDEBUG
3103 debugFlags = getDebugFlags( props );
3104 #endif
3105 do {
3106 symbol = OSDynamicCast( OSSymbol,
3107 props->getObject( gIOClassKey));
3108 if( !symbol)
3109 continue;
3110
3111 //IOLog("%s alloc (symbol %p props %p)\n", symbol->getCStringNoCopy(), OBFUSCATE(symbol), OBFUSCATE(props));
3112
3113 // alloc the driver instance
3114 inst = (IOService *) OSMetaClass::allocClassWithName( symbol);
3115
3116 if( !inst) {
3117 IOLog("Couldn't alloc class \"%s\"\n",
3118 symbol->getCStringNoCopy());
3119 continue;
3120 }
3121
3122 // init driver instance
3123 if( !(inst->init( props ))) {
3124 #if IOMATCHDEBUG
3125 if( debugFlags & kIOLogStart)
3126 IOLog("%s::init fails\n", symbol->getCStringNoCopy());
3127 #endif
3128 continue;
3129 }
3130 if( __state[1] & kIOServiceSynchronousState)
3131 inst->__state[1] |= kIOServiceSynchronousState;
3132
3133 // give the driver the default match category if not specified
3134 category = OSDynamicCast( OSSymbol,
3135 props->getObject( gIOMatchCategoryKey ));
3136 if( 0 == category)
3137 category = gIODefaultMatchCategoryKey;
3138 inst->setProperty( gIOMatchCategoryKey, (OSObject *) category );
3139 // attach driver instance
3140 if( !(inst->attach( this )))
3141 continue;
3142
3143 // pass in score from property table
3144 score = familyMatches->orderObject( props );
3145
3146 // & probe the new driver instance
3147 #if IOMATCHDEBUG
3148 if( debugFlags & kIOLogProbe)
3149 LOG("%s::probe(%s)\n",
3150 inst->getMetaClass()->getClassName(), getName());
3151 #endif
3152
3153 newInst = inst->probe( this, &score );
3154 inst->detach( this );
3155 if( 0 == newInst) {
3156 #if IOMATCHDEBUG
3157 if( debugFlags & kIOLogProbe)
3158 IOLog("%s::probe fails\n", symbol->getCStringNoCopy());
3159 #endif
3160 continue;
3161 }
3162
3163 // save the score
3164 newPri = OSNumber::withNumber( score, 32 );
3165 if( newPri) {
3166 newInst->setProperty( gIOProbeScoreKey, newPri );
3167 newPri->release();
3168 }
3169
3170 // add to start list for the match category
3171 if( 0 == startDict)
3172 startDict = OSDictionary::withCapacity( 1 );
3173 assert( startDict );
3174 startList = (OSOrderedSet *)
3175 startDict->getObject( category );
3176 if( 0 == startList) {
3177 startList = OSOrderedSet::withCapacity( 1,
3178 IOServiceOrdering, (void *) gIOProbeScoreKey );
3179 if( startDict && startList) {
3180 startDict->setObject( category, startList );
3181 startList->release();
3182 }
3183 }
3184 assert( startList );
3185 if( startList)
3186 startList->setObject( newInst );
3187
3188 } while( false );
3189
3190 props->release();
3191 if( inst)
3192 inst->release();
3193 }
3194 familyMatches->release();
3195 familyMatches = 0;
3196 }
3197
3198 // start the best (until success) of each category
3199
3200 iter = OSCollectionIterator::withCollection( startDict );
3201 if( iter) {
3202 while( (category = (const OSSymbol *) iter->getNextObject())) {
3203
3204 startList = (OSOrderedSet *) startDict->getObject( category );
3205 assert( startList );
3206 if( !startList)
3207 continue;
3208
3209 started = false;
3210 while( true // (!started)
3211 && (inst = (IOService *)startList->getFirstObject())) {
3212
3213 inst->retain();
3214 startList->removeObject(inst);
3215
3216 #if IOMATCHDEBUG
3217 debugFlags = getDebugFlags( inst->getPropertyTable() );
3218
3219 if( debugFlags & kIOLogStart) {
3220 if( started)
3221 LOG( "match category exists, skipping " );
3222 LOG( "%s::start(%s) <%d>\n", inst->getName(),
3223 getName(), inst->getRetainCount());
3224 }
3225 #endif
3226 if( false == started)
3227 started = startCandidate( inst );
3228 #if IOMATCHDEBUG
3229 if( (debugFlags & kIOLogStart) && (false == started))
3230 LOG( "%s::start(%s) <%d> failed\n", inst->getName(), getName(),
3231 inst->getRetainCount());
3232 #endif
3233 inst->release();
3234 }
3235 }
3236 iter->release();
3237 }
3238
3239
3240 // adjust the busy count by +1 if matching is stalled for a module,
3241 // or -1 if a previously stalled matching is complete.
3242 lockForArbitration();
3243 SInt32 adjBusy = 0;
3244 uint64_t regID = getRegistryEntryID();
3245
3246 if( needReloc) {
3247 adjBusy = (__state[1] & kIOServiceModuleStallState) ? 0 : 1;
3248 if( adjBusy) {
3249
3250 IOServiceTrace(
3251 IOSERVICE_MODULESTALL,
3252 (uintptr_t) regID,
3253 (uintptr_t) (regID >> 32),
3254 (uintptr_t) this,
3255 0);
3256
3257 __state[1] |= kIOServiceModuleStallState;
3258 }
3259
3260 } else if( __state[1] & kIOServiceModuleStallState) {
3261
3262 IOServiceTrace(
3263 IOSERVICE_MODULEUNSTALL,
3264 (uintptr_t) regID,
3265 (uintptr_t) (regID >> 32),
3266 (uintptr_t) this,
3267 0);
3268
3269 __state[1] &= ~kIOServiceModuleStallState;
3270 adjBusy = -1;
3271 }
3272 if( adjBusy)
3273 _adjustBusy( adjBusy );
3274 unlockForArbitration();
3275
3276 if( startDict)
3277 startDict->release();
3278 }
3279
3280 /*
3281 * Start a previously attached & probed instance,
3282 * called on exporting object instance
3283 */
3284
3285 bool IOService::startCandidate( IOService * service )
3286 {
3287 bool ok;
3288
3289 ok = service->attach( this );
3290
3291 if( ok)
3292 {
3293 if (this != gIOResources)
3294 {
3295 // stall for any nub resources
3296 checkResources();
3297 // stall for any driver resources
3298 service->checkResources();
3299 }
3300
3301 AbsoluteTime startTime;
3302 AbsoluteTime endTime;
3303 UInt64 nano;
3304
3305 if (kIOLogStart & gIOKitDebug)
3306 clock_get_uptime(&startTime);
3307
3308 ok = service->start(this);
3309
3310 if (kIOLogStart & gIOKitDebug)
3311 {
3312 clock_get_uptime(&endTime);
3313
3314 if (CMP_ABSOLUTETIME(&endTime, &startTime) > 0)
3315 {
3316 SUB_ABSOLUTETIME(&endTime, &startTime);
3317 absolutetime_to_nanoseconds(endTime, &nano);
3318 if (nano > 500000000ULL)
3319 IOLog("%s::start took %ld ms\n", service->getName(), (long)(UInt32)(nano / 1000000ULL));
3320 }
3321 }
3322 if( !ok)
3323 service->detach( this );
3324 }
3325 return( ok );
3326 }
3327
3328 void IOService::publishResource( const char * key, OSObject * value )
3329 {
3330 const OSSymbol * sym;
3331
3332 if( (sym = OSSymbol::withCString( key))) {
3333 publishResource( sym, value);
3334 sym->release();
3335 }
3336 }
3337
3338 void IOService::publishResource( const OSSymbol * key, OSObject * value )
3339 {
3340 if( 0 == value)
3341 value = (OSObject *) gIOServiceKey;
3342
3343 gIOResources->setProperty( key, value);
3344
3345 if( IORecursiveLockHaveLock( gNotificationLock))
3346 return;
3347
3348 gIOResourceGenerationCount++;
3349 gIOResources->registerService();
3350 }
3351
3352 bool IOService::addNeededResource( const char * key )
3353 {
3354 OSObject * resourcesProp;
3355 OSSet * set;
3356 OSString * newKey;
3357 bool ret;
3358
3359 resourcesProp = getProperty( gIOResourceMatchKey );
3360
3361 newKey = OSString::withCString( key );
3362 if( (0 == resourcesProp) || (0 == newKey))
3363 return( false);
3364
3365 set = OSDynamicCast( OSSet, resourcesProp );
3366 if( !set) {
3367 set = OSSet::withCapacity( 1 );
3368 if( set)
3369 set->setObject( resourcesProp );
3370 }
3371 else
3372 set->retain();
3373
3374 set->setObject( newKey );
3375 newKey->release();
3376 ret = setProperty( gIOResourceMatchKey, set );
3377 set->release();
3378
3379 return( ret );
3380 }
3381
3382 bool IOService::checkResource( OSObject * matching )
3383 {
3384 OSString * str;
3385 OSDictionary * table;
3386
3387 if( (str = OSDynamicCast( OSString, matching ))) {
3388 if( gIOResources->getProperty( str ))
3389 return( true );
3390 }
3391
3392 if( str)
3393 table = resourceMatching( str );
3394 else if( (table = OSDynamicCast( OSDictionary, matching )))
3395 table->retain();
3396 else {
3397 IOLog("%s: Can't match using: %s\n", getName(),
3398 matching->getMetaClass()->getClassName());
3399 /* false would stall forever */
3400 return( true );
3401 }
3402
3403 if( gIOKitDebug & kIOLogConfig)
3404 LOG("config(%p): stalling %s\n", OBFUSCATE(IOThreadSelf()), getName());
3405
3406 waitForService( table );
3407
3408 if( gIOKitDebug & kIOLogConfig)
3409 LOG("config(%p): waking\n", OBFUSCATE(IOThreadSelf()) );
3410
3411 return( true );
3412 }
3413
3414 bool IOService::checkResources( void )
3415 {
3416 OSObject * resourcesProp;
3417 OSSet * set;
3418 OSIterator * iter;
3419 bool ok;
3420
3421 resourcesProp = getProperty( gIOResourceMatchKey );
3422 if( 0 == resourcesProp)
3423 return( true );
3424
3425 if( (set = OSDynamicCast( OSSet, resourcesProp ))) {
3426
3427 iter = OSCollectionIterator::withCollection( set );
3428 ok = (0 != iter);
3429 while( ok && (resourcesProp = iter->getNextObject()) )
3430 ok = checkResource( resourcesProp );
3431 if( iter)
3432 iter->release();
3433
3434 } else
3435 ok = checkResource( resourcesProp );
3436
3437 return( ok );
3438 }
3439
3440
3441 void _IOConfigThread::configThread( void )
3442 {
3443 _IOConfigThread * inst;
3444
3445 do {
3446 if( !(inst = new _IOConfigThread))
3447 continue;
3448 if( !inst->init())
3449 continue;
3450 thread_t unused;
3451 if (KERN_SUCCESS != kernel_thread_start(&_IOConfigThread::main, inst, &unused))
3452 continue;
3453
3454 return;
3455
3456 } while( false);
3457
3458 if( inst)
3459 inst->release();
3460
3461 return;
3462 }
3463
3464 void _IOConfigThread::free( void )
3465 {
3466 thread_deallocate(current_thread());
3467 OSObject::free();
3468 }
3469
3470 void IOService::doServiceMatch( IOOptionBits options )
3471 {
3472 _IOServiceNotifier * notify;
3473 OSIterator * iter;
3474 OSOrderedSet * matches;
3475 SInt32 catalogGeneration;
3476 bool keepGuessing = true;
3477 bool reRegistered = true;
3478 bool didRegister;
3479
3480 // job->nub->deliverNotification( gIOPublishNotification,
3481 // kIOServiceRegisteredState, 0xffffffff );
3482
3483 while( keepGuessing ) {
3484
3485 matches = gIOCatalogue->findDrivers( this, &catalogGeneration );
3486 // the matches list should always be created by findDrivers()
3487 if( matches) {
3488
3489 lockForArbitration();
3490 if( 0 == (__state[0] & kIOServiceFirstPublishState)) {
3491 getMetaClass()->addInstance(this);
3492 deliverNotification( gIOFirstPublishNotification,
3493 kIOServiceFirstPublishState, 0xffffffff );
3494 }
3495 LOCKREADNOTIFY();
3496 __state[1] &= ~kIOServiceNeedConfigState;
3497 __state[1] |= kIOServiceConfigState;
3498 didRegister = (0 == (kIOServiceRegisteredState & __state[0]));
3499 __state[0] |= kIOServiceRegisteredState;
3500
3501 keepGuessing &= (0 == (__state[0] & kIOServiceInactiveState));
3502 if (reRegistered && keepGuessing) {
3503 iter = OSCollectionIterator::withCollection( (OSOrderedSet *)
3504 gNotifications->getObject( gIOPublishNotification ) );
3505 if( iter) {
3506 while((notify = (_IOServiceNotifier *)
3507 iter->getNextObject())) {
3508
3509 if( matchPassive(notify->matching, 0)
3510 && (kIOServiceNotifyEnable & notify->state))
3511 matches->setObject( notify );
3512 }
3513 iter->release();
3514 }
3515 }
3516
3517 UNLOCKNOTIFY();
3518 unlockForArbitration();
3519
3520 if (keepGuessing && matches->getCount() && (kIOReturnSuccess == getResources()))
3521 probeCandidates( matches );
3522 else
3523 matches->release();
3524 }
3525
3526 lockForArbitration();
3527 reRegistered = (0 != (__state[1] & kIOServiceNeedConfigState));
3528 keepGuessing =
3529 (reRegistered || (catalogGeneration !=
3530 gIOCatalogue->getGenerationCount()))
3531 && (0 == (__state[0] & kIOServiceInactiveState));
3532
3533 if( keepGuessing)
3534 unlockForArbitration();
3535 }
3536
3537 if( (0 == (__state[0] & kIOServiceInactiveState))
3538 && (0 == (__state[1] & kIOServiceModuleStallState)) ) {
3539 deliverNotification( gIOMatchedNotification,
3540 kIOServiceMatchedState, 0xffffffff );
3541 if( 0 == (__state[0] & kIOServiceFirstMatchState))
3542 deliverNotification( gIOFirstMatchNotification,
3543 kIOServiceFirstMatchState, 0xffffffff );
3544 }
3545
3546 __state[1] &= ~kIOServiceConfigState;
3547 scheduleTerminatePhase2();
3548
3549 _adjustBusy( -1 );
3550 unlockForArbitration();
3551 }
3552
3553 UInt32 IOService::_adjustBusy( SInt32 delta )
3554 {
3555 IOService * next;
3556 UInt32 count;
3557 UInt32 result;
3558 bool wasQuiet, nowQuiet, needWake;
3559
3560 next = this;
3561 result = __state[1] & kIOServiceBusyStateMask;
3562
3563 if( delta) do {
3564 if( next != this)
3565 next->lockForArbitration();
3566 count = next->__state[1] & kIOServiceBusyStateMask;
3567 wasQuiet = (0 == count);
3568 if (((delta < 0) && wasQuiet) || ((delta > 0) && (kIOServiceBusyMax == count)))
3569 OSReportWithBacktrace("%s: bad busy count (%d,%d)\n", next->getName(), count, delta);
3570 else
3571 count += delta;
3572 next->__state[1] = (next->__state[1] & ~kIOServiceBusyStateMask) | count;
3573 nowQuiet = (0 == count);
3574 needWake = (0 != (kIOServiceBusyWaiterState & next->__state[1]));
3575
3576 if( needWake) {
3577 next->__state[1] &= ~kIOServiceBusyWaiterState;
3578 IOLockLock( gIOServiceBusyLock );
3579 thread_wakeup( (event_t) next);
3580 IOLockUnlock( gIOServiceBusyLock );
3581 }
3582 if( next != this)
3583 next->unlockForArbitration();
3584
3585 if( (wasQuiet || nowQuiet) ) {
3586
3587 uint64_t regID = next->getRegistryEntryID();
3588 IOServiceTrace(
3589 ((wasQuiet/*nowBusy*/) ? IOSERVICE_BUSY : IOSERVICE_NONBUSY),
3590 (uintptr_t) regID,
3591 (uintptr_t) (regID >> 32),
3592 (uintptr_t) next,
3593 0);
3594
3595 if (wasQuiet)
3596 {
3597 next->__timeBusy = mach_absolute_time();
3598 }
3599 else
3600 {
3601 next->__accumBusy += mach_absolute_time() - next->__timeBusy;
3602 next->__timeBusy = 0;
3603 }
3604
3605 MessageClientsContext context;
3606
3607 context.service = next;
3608 context.type = kIOMessageServiceBusyStateChange;
3609 context.argument = (void *) wasQuiet; /*nowBusy*/
3610 context.argSize = 0;
3611
3612 applyToInterestNotifiers( next, gIOBusyInterest,
3613 &messageClientsApplier, &context );
3614
3615 #if !NO_KEXTD
3616 if( nowQuiet && (next == gIOServiceRoot)) {
3617 OSKext::considerUnloads();
3618 IOServiceTrace(IOSERVICE_REGISTRY_QUIET, 0, 0, 0, 0);
3619 }
3620 #endif
3621 }
3622
3623 delta = nowQuiet ? -1 : +1;
3624
3625 } while( (wasQuiet || nowQuiet) && (next = next->getProvider()));
3626
3627 return( result );
3628 }
3629
3630 void IOService::adjustBusy( SInt32 delta )
3631 {
3632 lockForArbitration();
3633 _adjustBusy( delta );
3634 unlockForArbitration();
3635 }
3636
3637 uint64_t IOService::getAccumulatedBusyTime( void )
3638 {
3639 uint64_t accumBusy = __accumBusy;
3640 uint64_t timeBusy = __timeBusy;
3641 uint64_t nano;
3642
3643 do
3644 {
3645 accumBusy = __accumBusy;
3646 timeBusy = __timeBusy;
3647 if (timeBusy)
3648 accumBusy += mach_absolute_time() - timeBusy;
3649 }
3650 while (timeBusy != __timeBusy);
3651
3652 absolutetime_to_nanoseconds(*(AbsoluteTime *)&accumBusy, &nano);
3653
3654 return (nano);
3655 }
3656
3657 UInt32 IOService::getBusyState( void )
3658 {
3659 return( __state[1] & kIOServiceBusyStateMask );
3660 }
3661
3662 IOReturn IOService::waitForState( UInt32 mask, UInt32 value,
3663 mach_timespec_t * timeout )
3664 {
3665 panic("waitForState");
3666 return (kIOReturnUnsupported);
3667 }
3668
3669 IOReturn IOService::waitForState( UInt32 mask, UInt32 value,
3670 uint64_t timeout )
3671 {
3672 bool wait;
3673 int waitResult = THREAD_AWAKENED;
3674 bool computeDeadline = true;
3675 AbsoluteTime abstime;
3676
3677 do {
3678 lockForArbitration();
3679 IOLockLock( gIOServiceBusyLock );
3680 wait = (value != (__state[1] & mask));
3681 if( wait) {
3682 __state[1] |= kIOServiceBusyWaiterState;
3683 unlockForArbitration();
3684 if( timeout != UINT64_MAX ) {
3685 if( computeDeadline ) {
3686 AbsoluteTime nsinterval;
3687 nanoseconds_to_absolutetime(timeout, &nsinterval );
3688 clock_absolutetime_interval_to_deadline(nsinterval, &abstime);
3689 computeDeadline = false;
3690 }
3691 assert_wait_deadline((event_t)this, THREAD_UNINT, __OSAbsoluteTime(abstime));
3692 }
3693 else
3694 assert_wait((event_t)this, THREAD_UNINT );
3695 } else
3696 unlockForArbitration();
3697 IOLockUnlock( gIOServiceBusyLock );
3698 if( wait)
3699 waitResult = thread_block(THREAD_CONTINUE_NULL);
3700
3701 } while( wait && (waitResult != THREAD_TIMED_OUT));
3702
3703 if( waitResult == THREAD_TIMED_OUT)
3704 return( kIOReturnTimeout );
3705 else
3706 return( kIOReturnSuccess );
3707 }
3708
3709 IOReturn IOService::waitQuiet( uint64_t timeout )
3710 {
3711 IOReturn ret;
3712 ret = waitForState( kIOServiceBusyStateMask, 0, timeout );
3713 if ((kIOReturnTimeout == ret) && (timeout >= 41000000000) && (kIOWaitQuietPanics & gIOKitDebug))
3714 {
3715 IORegistryIterator * iter;
3716 OSOrderedSet * set;
3717 OSOrderedSet * leaves;
3718 IOService * next;
3719 IOService * nextParent;
3720 char * string;
3721 char * s;
3722 size_t len, l;
3723
3724 len = 256;
3725 string = IONew(char, len);
3726 set = NULL;
3727 iter = IORegistryIterator::iterateOver(this, gIOServicePlane, kIORegistryIterateRecursively);
3728 leaves = OSOrderedSet::withCapacity(4);
3729 if (iter) set = iter->iterateAll();
3730 if (string && leaves && set)
3731 {
3732 while ((next = (IOService *) set->getLastObject()))
3733 {
3734 if (next->getBusyState())
3735 {
3736 leaves->setObject(next);
3737 nextParent = next;
3738 while ((nextParent = nextParent->getProvider()))
3739 {
3740 set->removeObject(nextParent);
3741 leaves->removeObject(nextParent);
3742 }
3743 }
3744 set->removeObject(next);
3745 }
3746 s = string;
3747 while ((next = (IOService *) leaves->getLastObject()))
3748 {
3749 l = snprintf(s, len, "%s'%s'", ((s == string) ? "" : ", "), next->getName());
3750 if (l >= len) break;
3751 s += l;
3752 len -= l;
3753 leaves->removeObject(next);
3754 }
3755 }
3756 panic("busy timeout(%llds): %s", timeout / 1000000000ULL, string ? string : "");
3757 }
3758 return (ret);
3759 }
3760
3761 IOReturn IOService::waitQuiet( mach_timespec_t * timeout )
3762 {
3763 uint64_t timeoutNS;
3764
3765 if (timeout)
3766 {
3767 timeoutNS = timeout->tv_sec;
3768 timeoutNS *= kSecondScale;
3769 timeoutNS += timeout->tv_nsec;
3770 }
3771 else
3772 timeoutNS = UINT64_MAX;
3773
3774 return (waitQuiet(timeoutNS));
3775 }
3776
3777 bool IOService::serializeProperties( OSSerialize * s ) const
3778 {
3779 #if 0
3780 ((IOService *)this)->setProperty( ((IOService *)this)->__state,
3781 sizeof( __state), "__state");
3782 #endif
3783 return( super::serializeProperties(s) );
3784 }
3785
3786
3787 void _IOConfigThread::main(void * arg, wait_result_t result)
3788 {
3789 _IOConfigThread * self = (_IOConfigThread *) arg;
3790 _IOServiceJob * job;
3791 IOService * nub;
3792 bool alive = true;
3793 kern_return_t kr;
3794 thread_precedence_policy_data_t precedence = { -1 };
3795
3796 kr = thread_policy_set(current_thread(),
3797 THREAD_PRECEDENCE_POLICY,
3798 (thread_policy_t) &precedence,
3799 THREAD_PRECEDENCE_POLICY_COUNT);
3800 if (KERN_SUCCESS != kr)
3801 IOLog("thread_policy_set(%d)\n", kr);
3802
3803 do {
3804
3805 // randomDelay();
3806
3807 semaphore_wait( gJobsSemaphore );
3808
3809 IOTakeLock( gJobsLock );
3810 job = (_IOServiceJob *) gJobs->getFirstObject();
3811 job->retain();
3812 gJobs->removeObject(job);
3813 if( job) {
3814 gOutstandingJobs--;
3815 // gNumConfigThreads--; // we're out of service
3816 gNumWaitingThreads--; // we're out of service
3817 }
3818 IOUnlock( gJobsLock );
3819
3820 if( job) {
3821
3822 nub = job->nub;
3823
3824 if( gIOKitDebug & kIOLogConfig)
3825 LOG("config(%p): starting on %s, %d\n",
3826 OBFUSCATE(IOThreadSelf()), job->nub->getName(), job->type);
3827
3828 switch( job->type) {
3829
3830 case kMatchNubJob:
3831 nub->doServiceMatch( job->options );
3832 break;
3833
3834 default:
3835 LOG("config(%p): strange type (%d)\n",
3836 OBFUSCATE(IOThreadSelf()), job->type );
3837 break;
3838 }
3839
3840 nub->release();
3841 job->release();
3842
3843 IOTakeLock( gJobsLock );
3844 alive = (gOutstandingJobs > gNumWaitingThreads);
3845 if( alive)
3846 gNumWaitingThreads++; // back in service
3847 // gNumConfigThreads++;
3848 else {
3849 if( 0 == --gNumConfigThreads) {
3850 // IOLog("MATCH IDLE\n");
3851 IOLockWakeup( gJobsLock, (event_t) &gNumConfigThreads, /* one-thread */ false );
3852 }
3853 }
3854 IOUnlock( gJobsLock );
3855 }
3856
3857 } while( alive );
3858
3859 if( gIOKitDebug & kIOLogConfig)
3860 LOG("config(%p): terminating\n", OBFUSCATE(IOThreadSelf()) );
3861
3862 self->release();
3863 }
3864
3865 IOReturn IOService::waitMatchIdle( UInt32 msToWait )
3866 {
3867 bool wait;
3868 int waitResult = THREAD_AWAKENED;
3869 bool computeDeadline = true;
3870 AbsoluteTime deadline;
3871
3872 IOLockLock( gJobsLock );
3873 do {
3874 wait = (0 != gNumConfigThreads);
3875 if( wait) {
3876 if( msToWait) {
3877 if( computeDeadline ) {
3878 clock_interval_to_deadline(
3879 msToWait, kMillisecondScale, &deadline );
3880 computeDeadline = false;
3881 }
3882 waitResult = IOLockSleepDeadline( gJobsLock, &gNumConfigThreads,
3883 deadline, THREAD_UNINT );
3884 } else {
3885 waitResult = IOLockSleep( gJobsLock, &gNumConfigThreads,
3886 THREAD_UNINT );
3887 }
3888 }
3889 } while( wait && (waitResult != THREAD_TIMED_OUT));
3890 IOLockUnlock( gJobsLock );
3891
3892 if( waitResult == THREAD_TIMED_OUT)
3893 return( kIOReturnTimeout );
3894 else
3895 return( kIOReturnSuccess );
3896 }
3897
3898 void IOService::cpusRunning(void)
3899 {
3900 gCPUsRunning = true;
3901 }
3902
3903 void _IOServiceJob::pingConfig( _IOServiceJob * job )
3904 {
3905 int count;
3906 bool create;
3907
3908 assert( job );
3909
3910 IOTakeLock( gJobsLock );
3911
3912 gOutstandingJobs++;
3913 gJobs->setLastObject( job );
3914
3915 count = gNumWaitingThreads;
3916 // if( gNumConfigThreads) count++;// assume we're called from a config thread
3917
3918 create = ( (gOutstandingJobs > count)
3919 && ((gNumConfigThreads < kMaxConfigThreads)
3920 || (job->nub == gIOResources)
3921 || !gCPUsRunning));
3922 if( create) {
3923 gNumConfigThreads++;
3924 gNumWaitingThreads++;
3925 }
3926
3927 IOUnlock( gJobsLock );
3928
3929 job->release();
3930
3931 if( create) {
3932 if( gIOKitDebug & kIOLogConfig)
3933 LOG("config(%d): creating\n", gNumConfigThreads - 1);
3934 _IOConfigThread::configThread();
3935 }
3936
3937 semaphore_signal( gJobsSemaphore );
3938 }
3939
3940 struct IOServiceMatchContext
3941 {
3942 OSDictionary * table;
3943 OSObject * result;
3944 uint32_t options;
3945 uint32_t state;
3946 uint32_t count;
3947 uint32_t done;
3948 };
3949
3950 bool IOService::instanceMatch(const OSObject * entry, void * context)
3951 {
3952 IOServiceMatchContext * ctx = (typeof(ctx)) context;
3953 IOService * service = (typeof(service)) entry;
3954 OSDictionary * table = ctx->table;
3955 uint32_t options = ctx->options;
3956 uint32_t state = ctx->state;
3957 uint32_t done;
3958 bool match;
3959
3960 done = 0;
3961 do
3962 {
3963 match = ((state == (state & service->__state[0]))
3964 && (0 == (service->__state[0] & kIOServiceInactiveState)));
3965 if (!match) break;
3966 ctx->count += table->getCount();
3967 match = service->matchInternal(table, options, &done);
3968 ctx->done += done;
3969 }
3970 while (false);
3971 if (!match)
3972 return (false);
3973
3974 if ((kIONotifyOnce & options) && (ctx->done == ctx->count))
3975 {
3976 service->retain();
3977 ctx->result = service;
3978 return (true);
3979 }
3980 else if (!ctx->result)
3981 {
3982 ctx->result = OSSet::withObjects((const OSObject **) &service, 1, 1);
3983 }
3984 else
3985 {
3986 ((OSSet *)ctx->result)->setObject(service);
3987 }
3988 return (false);
3989 }
3990
3991 // internal - call with gNotificationLock
3992 OSObject * IOService::copyExistingServices( OSDictionary * matching,
3993 IOOptionBits inState, IOOptionBits options )
3994 {
3995 OSObject * current = 0;
3996 OSIterator * iter;
3997 IOService * service;
3998 OSObject * obj;
3999 OSString * str;
4000
4001 if( !matching)
4002 return( 0 );
4003
4004 #if MATCH_DEBUG
4005 OSSerialize * s = OSSerialize::withCapacity(128);
4006 matching->serialize(s);
4007 #endif
4008
4009 if((obj = matching->getObject(gIOProviderClassKey))
4010 && gIOResourcesKey
4011 && gIOResourcesKey->isEqualTo(obj)
4012 && (service = gIOResources))
4013 {
4014 if( (inState == (service->__state[0] & inState))
4015 && (0 == (service->__state[0] & kIOServiceInactiveState))
4016 && service->matchPassive(matching, options))
4017 {
4018 if( options & kIONotifyOnce)
4019 {
4020 service->retain();
4021 current = service;
4022 }
4023 else
4024 current = OSSet::withObjects((const OSObject **) &service, 1, 1 );
4025 }
4026 }
4027 else
4028 {
4029 IOServiceMatchContext ctx;
4030 ctx.table = matching;
4031 ctx.state = inState;
4032 ctx.count = 0;
4033 ctx.done = 0;
4034 ctx.options = options;
4035 ctx.result = 0;
4036
4037 if ((str = OSDynamicCast(OSString, obj)))
4038 {
4039 const OSSymbol * sym = OSSymbol::withString(str);
4040 OSMetaClass::applyToInstancesOfClassName(sym, instanceMatch, &ctx);
4041 sym->release();
4042 }
4043 else
4044 {
4045 IOService::gMetaClass.applyToInstances(instanceMatch, &ctx);
4046 }
4047
4048
4049 current = ctx.result;
4050
4051 options |= kIOServiceInternalDone | kIOServiceClassDone;
4052 if (current && (ctx.done != ctx.count))
4053 {
4054 OSSet *
4055 source = OSDynamicCast(OSSet, current);
4056 current = 0;
4057 while ((service = (IOService *) source->getAnyObject()))
4058 {
4059 if (service->matchPassive(matching, options))
4060 {
4061 if( options & kIONotifyOnce)
4062 {
4063 service->retain();
4064 current = service;
4065 break;
4066 }
4067 if( current)
4068 {
4069 ((OSSet *)current)->setObject( service );
4070 }
4071 else
4072 {
4073 current = OSSet::withObjects(
4074 (const OSObject **) &service, 1, 1 );
4075 }
4076 }
4077 source->removeObject(service);
4078 }
4079 source->release();
4080 }
4081 }
4082
4083 #if MATCH_DEBUG
4084 {
4085 OSObject * _current = 0;
4086
4087 iter = IORegistryIterator::iterateOver( gIOServicePlane,
4088 kIORegistryIterateRecursively );
4089 if( iter) {
4090 do {
4091 iter->reset();
4092 while( (service = (IOService *) iter->getNextObject())) {
4093 if( (inState == (service->__state[0] & inState))
4094 && (0 == (service->__state[0] & kIOServiceInactiveState))
4095 && service->matchPassive(matching, 0)) {
4096
4097 if( options & kIONotifyOnce) {
4098 service->retain();
4099 _current = service;
4100 break;
4101 }
4102 if( _current)
4103 ((OSSet *)_current)->setObject( service );
4104 else
4105 _current = OSSet::withObjects(
4106 (const OSObject **) &service, 1, 1 );
4107 }
4108 }
4109 } while( !service && !iter->isValid());
4110 iter->release();
4111 }
4112
4113
4114 if ( ((current != 0) != (_current != 0))
4115 || (current && _current && !current->isEqualTo(_current)))
4116 {
4117 OSSerialize * s1 = OSSerialize::withCapacity(128);
4118 OSSerialize * s2 = OSSerialize::withCapacity(128);
4119 current->serialize(s1);
4120 _current->serialize(s2);
4121 kprintf("**mismatch** %p %p\n%s\n%s\n%s\n", OBFUSCATE(current),
4122 OBFUSCATE(_current), s->text(), s1->text(), s2->text());
4123 s1->release();
4124 s2->release();
4125 }
4126
4127 if (_current) _current->release();
4128 }
4129
4130 s->release();
4131 #endif
4132
4133 if( current && (0 == (options & (kIONotifyOnce | kIOServiceExistingSet)))) {
4134 iter = OSCollectionIterator::withCollection( (OSSet *)current );
4135 current->release();
4136 current = iter;
4137 }
4138
4139 return( current );
4140 }
4141
4142 // public version
4143 OSIterator * IOService::getMatchingServices( OSDictionary * matching )
4144 {
4145 OSIterator * iter;
4146
4147 // is a lock even needed?
4148 LOCKWRITENOTIFY();
4149
4150 iter = (OSIterator *) copyExistingServices( matching,
4151 kIOServiceMatchedState );
4152
4153 UNLOCKNOTIFY();
4154
4155 return( iter );
4156 }
4157
4158 IOService * IOService::copyMatchingService( OSDictionary * matching )
4159 {
4160 IOService * service;
4161
4162 // is a lock even needed?
4163 LOCKWRITENOTIFY();
4164
4165 service = (IOService *) copyExistingServices( matching,
4166 kIOServiceMatchedState, kIONotifyOnce );
4167
4168 UNLOCKNOTIFY();
4169
4170 return( service );
4171 }
4172
4173 struct _IOServiceMatchingNotificationHandlerRef
4174 {
4175 IOServiceNotificationHandler handler;
4176 void * ref;
4177 };
4178
4179 static bool _IOServiceMatchingNotificationHandler( void * target, void * refCon,
4180 IOService * newService,
4181 IONotifier * notifier )
4182 {
4183 return ((*((_IOServiceNotifier *) notifier)->compatHandler)(target, refCon, newService));
4184 }
4185
4186 // internal - call with gNotificationLock
4187 IONotifier * IOService::setNotification(
4188 const OSSymbol * type, OSDictionary * matching,
4189 IOServiceMatchingNotificationHandler handler, void * target, void * ref,
4190 SInt32 priority )
4191 {
4192 _IOServiceNotifier * notify = 0;
4193 OSOrderedSet * set;
4194
4195 if( !matching)
4196 return( 0 );
4197
4198 notify = new _IOServiceNotifier;
4199 if( notify && !notify->init()) {
4200 notify->release();
4201 notify = 0;
4202 }
4203
4204 if( notify) {
4205 notify->handler = handler;
4206 notify->target = target;
4207 notify->matching = matching;
4208 matching->retain();
4209 if (handler == &_IOServiceMatchingNotificationHandler)
4210 {
4211 notify->compatHandler = ((_IOServiceMatchingNotificationHandlerRef *)ref)->handler;
4212 notify->ref = ((_IOServiceMatchingNotificationHandlerRef *)ref)->ref;
4213 }
4214 else
4215 notify->ref = ref;
4216 notify->priority = priority;
4217 notify->state = kIOServiceNotifyEnable;
4218 queue_init( &notify->handlerInvocations );
4219
4220 ////// queue
4221
4222 if( 0 == (set = (OSOrderedSet *) gNotifications->getObject( type ))) {
4223 set = OSOrderedSet::withCapacity( 1,
4224 IONotifyOrdering, 0 );
4225 if( set) {
4226 gNotifications->setObject( type, set );
4227 set->release();
4228 }
4229 }
4230 notify->whence = set;
4231 if( set)
4232 set->setObject( notify );
4233 }
4234
4235 return( notify );
4236 }
4237
4238 // internal - call with gNotificationLock
4239 IONotifier * IOService::doInstallNotification(
4240 const OSSymbol * type, OSDictionary * matching,
4241 IOServiceMatchingNotificationHandler handler,
4242 void * target, void * ref,
4243 SInt32 priority, OSIterator ** existing )
4244 {
4245 OSIterator * exist;
4246 IONotifier * notify;
4247 IOOptionBits inState;
4248
4249 if( !matching)
4250 return( 0 );
4251
4252 if( type == gIOPublishNotification)
4253 inState = kIOServiceRegisteredState;
4254
4255 else if( type == gIOFirstPublishNotification)
4256 inState = kIOServiceFirstPublishState;
4257
4258 else if( (type == gIOMatchedNotification)
4259 || (type == gIOFirstMatchNotification))
4260 inState = kIOServiceMatchedState;
4261 else if( type == gIOTerminatedNotification)
4262 inState = 0;
4263 else
4264 return( 0 );
4265
4266 notify = setNotification( type, matching, handler, target, ref, priority );
4267
4268 if( inState)
4269 // get the current set
4270 exist = (OSIterator *) copyExistingServices( matching, inState );
4271 else
4272 exist = 0;
4273
4274 *existing = exist;
4275
4276 return( notify );
4277 }
4278
4279 #if !defined(__LP64__)
4280 IONotifier * IOService::installNotification(const OSSymbol * type, OSDictionary * matching,
4281 IOServiceNotificationHandler handler,
4282 void * target, void * refCon,
4283 SInt32 priority, OSIterator ** existing )
4284 {
4285 IONotifier * result;
4286 _IOServiceMatchingNotificationHandlerRef ref;
4287 ref.handler = handler;
4288 ref.ref = refCon;
4289
4290 result = (_IOServiceNotifier *) installNotification( type, matching,
4291 &_IOServiceMatchingNotificationHandler,
4292 target, &ref, priority, existing );
4293 if (result)
4294 matching->release();
4295
4296 return (result);
4297 }
4298 #endif /* !defined(__LP64__) */
4299
4300
4301 IONotifier * IOService::installNotification(
4302 const OSSymbol * type, OSDictionary * matching,
4303 IOServiceMatchingNotificationHandler handler,
4304 void * target, void * ref,
4305 SInt32 priority, OSIterator ** existing )
4306 {
4307 IONotifier * notify;
4308
4309 LOCKWRITENOTIFY();
4310
4311 notify = doInstallNotification( type, matching, handler, target, ref,
4312 priority, existing );
4313
4314 UNLOCKNOTIFY();
4315
4316 return( notify );
4317 }
4318
4319 IONotifier * IOService::addNotification(
4320 const OSSymbol * type, OSDictionary * matching,
4321 IOServiceNotificationHandler handler,
4322 void * target, void * refCon,
4323 SInt32 priority )
4324 {
4325 IONotifier * result;
4326 _IOServiceMatchingNotificationHandlerRef ref;
4327
4328 ref.handler = handler;
4329 ref.ref = refCon;
4330
4331 result = addMatchingNotification(type, matching, &_IOServiceMatchingNotificationHandler,
4332 target, &ref, priority);
4333
4334 if (result)
4335 matching->release();
4336
4337 return (result);
4338 }
4339
4340 IONotifier * IOService::addMatchingNotification(
4341 const OSSymbol * type, OSDictionary * matching,
4342 IOServiceMatchingNotificationHandler handler,
4343 void * target, void * ref,
4344 SInt32 priority )
4345 {
4346 OSIterator * existing = NULL;
4347 _IOServiceNotifier * notify;
4348 IOService * next;
4349
4350 notify = (_IOServiceNotifier *) installNotification( type, matching,
4351 handler, target, ref, priority, &existing );
4352
4353 // send notifications for existing set
4354 if( existing) {
4355
4356 notify->retain(); // in case handler remove()s
4357 while( (next = (IOService *) existing->getNextObject())) {
4358
4359 next->lockForArbitration();
4360 if( 0 == (next->__state[0] & kIOServiceInactiveState))
4361 next->invokeNotifer( notify );
4362 next->unlockForArbitration();
4363 }
4364 notify->release();
4365 existing->release();
4366 }
4367
4368 return( notify );
4369 }
4370
4371 bool IOService::syncNotificationHandler(
4372 void * /* target */, void * ref,
4373 IOService * newService,
4374 IONotifier * notifier )
4375 {
4376
4377 LOCKWRITENOTIFY();
4378 if (!*((IOService **) ref))
4379 {
4380 newService->retain();
4381 (*(IOService **) ref) = newService;
4382 WAKEUPNOTIFY(ref);
4383 }
4384 UNLOCKNOTIFY();
4385
4386 return( false );
4387 }
4388
4389 IOService * IOService::waitForMatchingService( OSDictionary * matching,
4390 uint64_t timeout)
4391 {
4392 IONotifier * notify = 0;
4393 // priority doesn't help us much since we need a thread wakeup
4394 SInt32 priority = 0;
4395 IOService * result;
4396
4397 if (!matching)
4398 return( 0 );
4399
4400 result = NULL;
4401
4402 LOCKWRITENOTIFY();
4403 do
4404 {
4405 result = (IOService *) copyExistingServices( matching,
4406 kIOServiceMatchedState, kIONotifyOnce );
4407 if (result)
4408 break;
4409 notify = IOService::setNotification( gIOMatchedNotification, matching,
4410 &IOService::syncNotificationHandler, (void *) 0,
4411 &result, priority );
4412 if (!notify)
4413 break;
4414 if (UINT64_MAX != timeout)
4415 {
4416 AbsoluteTime deadline;
4417 nanoseconds_to_absolutetime(timeout, &deadline);
4418 clock_absolutetime_interval_to_deadline(deadline, &deadline);
4419 SLEEPNOTIFYTO(&result, deadline);
4420 }
4421 else
4422 {
4423 SLEEPNOTIFY(&result);
4424 }
4425 }
4426 while( false );
4427
4428 UNLOCKNOTIFY();
4429
4430 if (notify)
4431 notify->remove(); // dequeues
4432
4433 return( result );
4434 }
4435
4436 IOService * IOService::waitForService( OSDictionary * matching,
4437 mach_timespec_t * timeout )
4438 {
4439 IOService * result;
4440 uint64_t timeoutNS;
4441
4442 if (timeout)
4443 {
4444 timeoutNS = timeout->tv_sec;
4445 timeoutNS *= kSecondScale;
4446 timeoutNS += timeout->tv_nsec;
4447 }
4448 else
4449 timeoutNS = UINT64_MAX;
4450
4451 result = waitForMatchingService(matching, timeoutNS);
4452
4453 matching->release();
4454 if (result)
4455 result->release();
4456
4457 return (result);
4458 }
4459
4460 void IOService::deliverNotification( const OSSymbol * type,
4461 IOOptionBits orNewState, IOOptionBits andNewState )
4462 {
4463 _IOServiceNotifier * notify;
4464 OSIterator * iter;
4465 OSArray * willSend = 0;
4466
4467 lockForArbitration();
4468
4469 if( (0 == (__state[0] & kIOServiceInactiveState))
4470 || (type == gIOTerminatedNotification)) {
4471
4472 LOCKREADNOTIFY();
4473
4474 iter = OSCollectionIterator::withCollection( (OSOrderedSet *)
4475 gNotifications->getObject( type ) );
4476
4477 if( iter) {
4478 while( (notify = (_IOServiceNotifier *) iter->getNextObject())) {
4479
4480 if( matchPassive(notify->matching, 0)
4481 && (kIOServiceNotifyEnable & notify->state)) {
4482 if( 0 == willSend)
4483 willSend = OSArray::withCapacity(8);
4484 if( willSend)
4485 willSend->setObject( notify );
4486 }
4487 }
4488 iter->release();
4489 }
4490
4491 __state[0] = (__state[0] | orNewState) & andNewState;
4492
4493 UNLOCKNOTIFY();
4494 }
4495
4496 if( willSend) {
4497 for( unsigned int idx = 0;
4498 (notify = (_IOServiceNotifier *) willSend->getObject(idx));
4499 idx++) {
4500 invokeNotifer( notify );
4501 }
4502 willSend->release();
4503 }
4504 unlockForArbitration();
4505 }
4506
4507 IOOptionBits IOService::getState( void ) const
4508 {
4509 return( __state[0] );
4510 }
4511
4512 /*
4513 * Helpers to make matching objects for simple cases
4514 */
4515
4516 OSDictionary * IOService::serviceMatching( const OSString * name,
4517 OSDictionary * table )
4518 {
4519
4520 const OSString * str;
4521
4522 str = OSSymbol::withString(name);
4523 if( !str)
4524 return( 0 );
4525
4526 if( !table)
4527 table = OSDictionary::withCapacity( 2 );
4528 if( table)
4529 table->setObject(gIOProviderClassKey, (OSObject *)str );
4530 str->release();
4531
4532 return( table );
4533 }
4534
4535 OSDictionary * IOService::serviceMatching( const char * name,
4536 OSDictionary * table )
4537 {
4538 const OSString * str;
4539
4540 str = OSSymbol::withCString( name );
4541 if( !str)
4542 return( 0 );
4543
4544 table = serviceMatching( str, table );
4545 str->release();
4546 return( table );
4547 }
4548
4549 OSDictionary * IOService::nameMatching( const OSString * name,
4550 OSDictionary * table )
4551 {
4552 if( !table)
4553 table = OSDictionary::withCapacity( 2 );
4554 if( table)
4555 table->setObject( gIONameMatchKey, (OSObject *)name );
4556
4557 return( table );
4558 }
4559
4560 OSDictionary * IOService::nameMatching( const char * name,
4561 OSDictionary * table )
4562 {
4563 const OSString * str;
4564
4565 str = OSSymbol::withCString( name );
4566 if( !str)
4567 return( 0 );
4568
4569 table = nameMatching( str, table );
4570 str->release();
4571 return( table );
4572 }
4573
4574 OSDictionary * IOService::resourceMatching( const OSString * str,
4575 OSDictionary * table )
4576 {
4577 table = serviceMatching( gIOResourcesKey, table );
4578 if( table)
4579 table->setObject( gIOResourceMatchKey, (OSObject *) str );
4580
4581 return( table );
4582 }
4583
4584 OSDictionary * IOService::resourceMatching( const char * name,
4585 OSDictionary * table )
4586 {
4587 const OSSymbol * str;
4588
4589 str = OSSymbol::withCString( name );
4590 if( !str)
4591 return( 0 );
4592
4593 table = resourceMatching( str, table );
4594 str->release();
4595
4596 return( table );
4597 }
4598
4599 OSDictionary * IOService::propertyMatching( const OSSymbol * key, const OSObject * value,
4600 OSDictionary * table )
4601 {
4602 OSDictionary * properties;
4603
4604 properties = OSDictionary::withCapacity( 2 );
4605 if( !properties)
4606 return( 0 );
4607 properties->setObject( key, value );
4608
4609 if( !table)
4610 table = OSDictionary::withCapacity( 2 );
4611 if( table)
4612 table->setObject( gIOPropertyMatchKey, properties );
4613
4614 properties->release();
4615
4616 return( table );
4617 }
4618
4619 OSDictionary * IOService::registryEntryIDMatching( uint64_t entryID,
4620 OSDictionary * table )
4621 {
4622 OSNumber * num;
4623
4624 num = OSNumber::withNumber( entryID, 64 );
4625 if( !num)
4626 return( 0 );
4627
4628 if( !table)
4629 table = OSDictionary::withCapacity( 2 );
4630 if( table)
4631 table->setObject( gIORegistryEntryIDKey, num );
4632
4633 if (num)
4634 num->release();
4635
4636 return( table );
4637 }
4638
4639
4640 /*
4641 * _IOServiceNotifier
4642 */
4643
4644 // wait for all threads, other than the current one,
4645 // to exit the handler
4646
4647 void _IOServiceNotifier::wait()
4648 {
4649 _IOServiceNotifierInvocation * next;
4650 bool doWait;
4651
4652 do {
4653 doWait = false;
4654 queue_iterate( &handlerInvocations, next,
4655 _IOServiceNotifierInvocation *, link) {
4656 if( next->thread != current_thread() ) {
4657 doWait = true;
4658 break;
4659 }
4660 }
4661 if( doWait) {
4662 state |= kIOServiceNotifyWaiter;
4663 SLEEPNOTIFY(this);
4664 }
4665
4666 } while( doWait );
4667 }
4668
4669 void _IOServiceNotifier::free()
4670 {
4671 assert( queue_empty( &handlerInvocations ));
4672 OSObject::free();
4673 }
4674
4675 void _IOServiceNotifier::remove()
4676 {
4677 LOCKWRITENOTIFY();
4678
4679 if( whence) {
4680 whence->removeObject( (OSObject *) this );
4681 whence = 0;
4682 }
4683 if( matching) {
4684 matching->release();
4685 matching = 0;
4686 }
4687
4688 state &= ~kIOServiceNotifyEnable;
4689
4690 wait();
4691
4692 UNLOCKNOTIFY();
4693
4694 release();
4695 }
4696
4697 bool _IOServiceNotifier::disable()
4698 {
4699 bool ret;
4700
4701 LOCKWRITENOTIFY();
4702
4703 ret = (0 != (kIOServiceNotifyEnable & state));
4704 state &= ~kIOServiceNotifyEnable;
4705 if( ret)
4706 wait();
4707
4708 UNLOCKNOTIFY();
4709
4710 return( ret );
4711 }
4712
4713 void _IOServiceNotifier::enable( bool was )
4714 {
4715 LOCKWRITENOTIFY();
4716 if( was)
4717 state |= kIOServiceNotifyEnable;
4718 else
4719 state &= ~kIOServiceNotifyEnable;
4720 UNLOCKNOTIFY();
4721 }
4722
4723 /*
4724 * IOResources
4725 */
4726
4727 IOService * IOResources::resources( void )
4728 {
4729 IOResources * inst;
4730
4731 inst = new IOResources;
4732 if( inst && !inst->init()) {
4733 inst->release();
4734 inst = 0;
4735 }
4736
4737 return( inst );
4738 }
4739
4740 bool IOResources::init( OSDictionary * dictionary )
4741 {
4742 // Do super init first
4743 if ( !super::init() )
4744 return false;
4745
4746 // Allow PAL layer to publish a value
4747 const char *property_name;
4748 int property_value;
4749
4750 pal_get_resource_property( &property_name, &property_value );
4751
4752 if( property_name ) {
4753 OSNumber *num;
4754 const OSSymbol * sym;
4755
4756 if( (num = OSNumber::withNumber(property_value, 32)) != 0 ) {
4757 if( (sym = OSSymbol::withCString( property_name)) != 0 ) {
4758 this->setProperty( sym, num );
4759 sym->release();
4760 }
4761 num->release();
4762 }
4763 }
4764
4765 return true;
4766 }
4767
4768 IOReturn IOResources::newUserClient(task_t owningTask, void * securityID,
4769 UInt32 type, OSDictionary * properties,
4770 IOUserClient ** handler)
4771 {
4772 return( kIOReturnUnsupported );
4773 }
4774
4775 IOWorkLoop * IOResources::getWorkLoop() const
4776 {
4777 // If we are the resource root
4778 // then use the platform's workloop
4779 if (this == (IOResources *) gIOResources)
4780 return getPlatform()->getWorkLoop();
4781 else
4782 return IOService::getWorkLoop();
4783 }
4784
4785 bool IOResources::matchPropertyTable( OSDictionary * table )
4786 {
4787 OSObject * prop;
4788 OSString * str;
4789 OSSet * set;
4790 OSIterator * iter;
4791 bool ok = true;
4792
4793 prop = table->getObject( gIOResourceMatchKey );
4794 str = OSDynamicCast( OSString, prop );
4795 if( str)
4796 ok = (0 != getProperty( str ));
4797
4798 else if( (set = OSDynamicCast( OSSet, prop))) {
4799
4800 iter = OSCollectionIterator::withCollection( set );
4801 ok = (iter != 0);
4802 while( ok && (str = OSDynamicCast( OSString, iter->getNextObject()) ))
4803 ok = (0 != getProperty( str ));
4804
4805 if( iter)
4806 iter->release();
4807 }
4808
4809 return( ok );
4810 }
4811
4812 void IOService::consoleLockTimer(thread_call_param_t p0, thread_call_param_t p1)
4813 {
4814 IOService::updateConsoleUsers(NULL, 0);
4815 }
4816
4817 void IOService::updateConsoleUsers(OSArray * consoleUsers, IOMessage systemMessage)
4818 {
4819 IORegistryEntry * regEntry;
4820 OSObject * locked = kOSBooleanFalse;
4821 uint32_t idx;
4822 bool publish;
4823 OSDictionary * user;
4824 static IOMessage sSystemPower;
4825
4826 regEntry = IORegistryEntry::getRegistryRoot();
4827
4828 if (!gIOChosenEntry)
4829 gIOChosenEntry = IORegistryEntry::fromPath("/chosen", gIODTPlane);
4830
4831 IOLockLock(gIOConsoleUsersLock);
4832
4833 if (systemMessage)
4834 {
4835 sSystemPower = systemMessage;
4836 #if HIBERNATION
4837 if ((kIOMessageSystemHasPoweredOn == systemMessage) && IOHibernateWasScreenLocked())
4838 {
4839 locked = kOSBooleanTrue;
4840 }
4841 #endif /* HIBERNATION */
4842 }
4843
4844 if (consoleUsers)
4845 {
4846 OSNumber * num = 0;
4847 gIOConsoleLoggedIn = false;
4848 for (idx = 0;
4849 (user = OSDynamicCast(OSDictionary, consoleUsers->getObject(idx)));
4850 idx++)
4851 {
4852 gIOConsoleLoggedIn |= ((kOSBooleanTrue == user->getObject(gIOConsoleSessionOnConsoleKey))
4853 && (kOSBooleanTrue == user->getObject(gIOConsoleSessionLoginDoneKey)));
4854 if (!num)
4855 {
4856 num = OSDynamicCast(OSNumber, user->getObject(gIOConsoleSessionScreenLockedTimeKey));
4857 }
4858 }
4859 gIOConsoleLockTime = num ? num->unsigned32BitValue() : 0;
4860 }
4861
4862 if (!gIOConsoleLoggedIn
4863 || (kIOMessageSystemWillSleep == sSystemPower)
4864 || (kIOMessageSystemPagingOff == sSystemPower))
4865 {
4866 locked = kOSBooleanTrue;
4867 }
4868 else if (gIOConsoleLockTime)
4869 {
4870 clock_sec_t now;
4871 clock_usec_t microsecs;
4872
4873 clock_get_calendar_microtime(&now, &microsecs);
4874 if (gIOConsoleLockTime > now)
4875 {
4876 AbsoluteTime deadline;
4877 clock_interval_to_deadline(gIOConsoleLockTime - now, kSecondScale, &deadline);
4878 thread_call_enter_delayed(gIOConsoleLockCallout, deadline);
4879 }
4880 else
4881 {
4882 locked = kOSBooleanTrue;
4883 }
4884 }
4885
4886 publish = (consoleUsers || (locked != regEntry->getProperty(gIOConsoleLockedKey)));
4887 if (publish)
4888 {
4889 regEntry->setProperty(gIOConsoleLockedKey, locked);
4890 if (consoleUsers)
4891 {
4892 regEntry->setProperty(gIOConsoleUsersKey, consoleUsers);
4893 }
4894 OSIncrementAtomic( &gIOConsoleUsersSeed );
4895 }
4896
4897 #if HIBERNATION
4898 if (gIOChosenEntry)
4899 {
4900 if (locked == kOSBooleanTrue) gIOScreenLockState = kIOScreenLockLocked;
4901 else if (gIOConsoleLockTime) gIOScreenLockState = kIOScreenLockUnlocked;
4902 else gIOScreenLockState = kIOScreenLockNoLock;
4903 gIOChosenEntry->setProperty(kIOScreenLockStateKey, &gIOScreenLockState, sizeof(gIOScreenLockState));
4904 }
4905 #endif /* HIBERNATION */
4906
4907 IOLockUnlock(gIOConsoleUsersLock);
4908
4909 if (publish)
4910 {
4911 publishResource( gIOConsoleUsersSeedKey, gIOConsoleUsersSeedValue );
4912
4913 MessageClientsContext context;
4914
4915 context.service = getServiceRoot();
4916 context.type = kIOMessageConsoleSecurityChange;
4917 context.argument = (void *) regEntry;
4918 context.argSize = 0;
4919
4920 applyToInterestNotifiers(getServiceRoot(), gIOConsoleSecurityInterest,
4921 &messageClientsApplier, &context );
4922 }
4923 }
4924
4925 IOReturn IOResources::setProperties( OSObject * properties )
4926 {
4927 IOReturn err;
4928 const OSSymbol * key;
4929 OSDictionary * dict;
4930 OSCollectionIterator * iter;
4931
4932 err = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
4933 if ( kIOReturnSuccess != err)
4934 return( err );
4935
4936 dict = OSDynamicCast(OSDictionary, properties);
4937 if( 0 == dict)
4938 return( kIOReturnBadArgument);
4939
4940 iter = OSCollectionIterator::withCollection( dict);
4941 if( 0 == iter)
4942 return( kIOReturnBadArgument);
4943
4944 while( (key = OSDynamicCast(OSSymbol, iter->getNextObject())))
4945 {
4946 if (gIOConsoleUsersKey == key) do
4947 {
4948 OSArray * consoleUsers;
4949 consoleUsers = OSDynamicCast(OSArray, dict->getObject(key));
4950 if (!consoleUsers)
4951 continue;
4952 IOService::updateConsoleUsers(consoleUsers, 0);
4953 }
4954 while (false);
4955
4956 publishResource( key, dict->getObject(key) );
4957 }
4958
4959 iter->release();
4960
4961 return( kIOReturnSuccess );
4962 }
4963
4964 /*
4965 * Helpers for matching dictionaries.
4966 * Keys existing in matching are checked in properties.
4967 * Keys may be a string or OSCollection of IOStrings
4968 */
4969
4970 bool IOService::compareProperty( OSDictionary * matching,
4971 const char * key )
4972 {
4973 OSObject * value;
4974 bool ok;
4975
4976 value = matching->getObject( key );
4977 if( value)
4978 ok = value->isEqualTo( getProperty( key ));
4979 else
4980 ok = true;
4981
4982 return( ok );
4983 }
4984
4985
4986 bool IOService::compareProperty( OSDictionary * matching,
4987 const OSString * key )
4988 {
4989 OSObject * value;
4990 bool ok;
4991
4992 value = matching->getObject( key );
4993 if( value)
4994 ok = value->isEqualTo( getProperty( key ));
4995 else
4996 ok = true;
4997
4998 return( ok );
4999 }
5000
5001 bool IOService::compareProperties( OSDictionary * matching,
5002 OSCollection * keys )
5003 {
5004 OSCollectionIterator * iter;
5005 const OSString * key;
5006 bool ok = true;
5007
5008 if( !matching || !keys)
5009 return( false );
5010
5011 iter = OSCollectionIterator::withCollection( keys );
5012
5013 if( iter) {
5014 while( ok && (key = OSDynamicCast( OSString, iter->getNextObject())))
5015 ok = compareProperty( matching, key );
5016
5017 iter->release();
5018 }
5019 keys->release(); // !! consume a ref !!
5020
5021 return( ok );
5022 }
5023
5024 /* Helper to add a location matching dict to the table */
5025
5026 OSDictionary * IOService::addLocation( OSDictionary * table )
5027 {
5028 OSDictionary * dict;
5029
5030 if( !table)
5031 return( 0 );
5032
5033 dict = OSDictionary::withCapacity( 1 );
5034 if( dict) {
5035 table->setObject( gIOLocationMatchKey, dict );
5036 dict->release();
5037 }
5038
5039 return( dict );
5040 }
5041
5042 /*
5043 * Go looking for a provider to match a location dict.
5044 */
5045
5046 IOService * IOService::matchLocation( IOService * /* client */ )
5047 {
5048 IOService * parent;
5049
5050 parent = getProvider();
5051
5052 if( parent)
5053 parent = parent->matchLocation( this );
5054
5055 return( parent );
5056 }
5057
5058 bool IOService::matchInternal(OSDictionary * table, uint32_t options, uint32_t * did)
5059 {
5060 OSString * matched;
5061 OSObject * obj;
5062 OSString * str;
5063 IORegistryEntry * entry;
5064 OSNumber * num;
5065 bool match = true;
5066 bool changesOK = (0 != (kIOServiceChangesOK & options));
5067 uint32_t count;
5068 uint32_t done;
5069
5070 do
5071 {
5072 count = table->getCount();
5073 done = 0;
5074 str = OSDynamicCast(OSString, table->getObject(gIOProviderClassKey));
5075 if (str) {
5076 done++;
5077 match = ((kIOServiceClassDone & options) || (0 != metaCast(str)));
5078 #if MATCH_DEBUG
5079 match = (0 != metaCast( str ));
5080 if ((kIOServiceClassDone & options) && !match) panic("classDone");
5081 #endif
5082 if ((!match) || (done == count)) break;
5083 }
5084
5085 obj = table->getObject( gIONameMatchKey );
5086 if( obj) {
5087 done++;
5088 match = compareNames( obj, changesOK ? &matched : 0 );
5089 if (!match) break;
5090 if( changesOK && matched) {
5091 // leave a hint as to which name matched
5092 table->setObject( gIONameMatchedKey, matched );
5093 matched->release();
5094 }
5095 if (done == count) break;
5096 }
5097
5098 str = OSDynamicCast( OSString, table->getObject( gIOLocationMatchKey ));
5099 if (str)
5100 {
5101 const OSSymbol * sym;
5102 done++;
5103 match = false;
5104 sym = copyLocation();
5105 if (sym) {
5106 match = sym->isEqualTo( str );
5107 sym->release();
5108 }
5109 if ((!match) || (done == count)) break;
5110 }
5111
5112 obj = table->getObject( gIOPropertyMatchKey );
5113 if( obj)
5114 {
5115 OSDictionary * dict;
5116 OSDictionary * nextDict;
5117 OSIterator * iter;
5118 done++;
5119 match = false;
5120 dict = dictionaryWithProperties();
5121 if( dict) {
5122 nextDict = OSDynamicCast( OSDictionary, obj);
5123 if( nextDict)
5124 iter = 0;
5125 else
5126 iter = OSCollectionIterator::withCollection(
5127 OSDynamicCast(OSCollection, obj));
5128
5129 while( nextDict
5130 || (iter && (0 != (nextDict = OSDynamicCast(OSDictionary,
5131 iter->getNextObject()))))) {
5132 match = dict->isEqualTo( nextDict, nextDict);
5133 if( match)
5134 break;
5135 nextDict = 0;
5136 }
5137 dict->release();
5138 if( iter)
5139 iter->release();
5140 }
5141 if ((!match) || (done == count)) break;
5142 }
5143
5144 str = OSDynamicCast( OSString, table->getObject( gIOPathMatchKey ));
5145 if( str) {
5146 done++;
5147 entry = IORegistryEntry::fromPath( str->getCStringNoCopy() );
5148 match = (this == entry);
5149 if( entry)
5150 entry->release();
5151 if ((!match) || (done == count)) break;
5152 }
5153
5154 num = OSDynamicCast( OSNumber, table->getObject( gIORegistryEntryIDKey ));
5155 if (num) {
5156 done++;
5157 match = (getRegistryEntryID() == num->unsigned64BitValue());
5158 if ((!match) || (done == count)) break;
5159 }
5160
5161 num = OSDynamicCast( OSNumber, table->getObject( gIOMatchedServiceCountKey ));
5162 if( num)
5163 {
5164 OSIterator * iter;
5165 IOService * service = 0;
5166 UInt32 serviceCount = 0;
5167
5168 done++;
5169 iter = getClientIterator();
5170 if( iter) {
5171 while( (service = (IOService *) iter->getNextObject())) {
5172 if( kIOServiceInactiveState & service->__state[0])
5173 continue;
5174 if( 0 == service->getProperty( gIOMatchCategoryKey ))
5175 continue;
5176 ++serviceCount;
5177 }
5178 iter->release();
5179 }
5180 match = (serviceCount == num->unsigned32BitValue());
5181 if ((!match) || (done == count)) break;
5182 }
5183
5184 #define propMatch(key) \
5185 obj = table->getObject(key); \
5186 if (obj) \
5187 { \
5188 OSObject * prop; \
5189 done++; \
5190 prop = copyProperty(key); \
5191 match = obj->isEqualTo(prop); \
5192 if (prop) prop->release(); \
5193 if ((!match) || (done == count)) break; \
5194 }
5195 propMatch(kIOBSDNameKey)
5196 propMatch(kIOBSDMajorKey)
5197 propMatch(kIOBSDMinorKey)
5198 propMatch(kIOBSDUnitKey)
5199 #undef propMatch
5200 }
5201 while (false);
5202
5203 if (did) *did = done;
5204 return (match);
5205 }
5206
5207 bool IOService::passiveMatch( OSDictionary * table, bool changesOK )
5208 {
5209 return (matchPassive(table, changesOK ? kIOServiceChangesOK : 0));
5210 }
5211
5212 bool IOService::matchPassive(OSDictionary * table, uint32_t options)
5213 {
5214 IOService * where;
5215 OSDictionary * nextTable;
5216 SInt32 score;
5217 OSNumber * newPri;
5218 bool match = true;
5219 bool matchParent = false;
5220 uint32_t count;
5221 uint32_t done;
5222
5223 assert( table );
5224
5225 #if MATCH_DEBUG
5226 OSDictionary * root = table;
5227 #endif
5228
5229 where = this;
5230 do
5231 {
5232 do
5233 {
5234 count = table->getCount();
5235 if (!(kIOServiceInternalDone & options))
5236 {
5237 match = where->matchInternal(table, options, &done);
5238 // don't call family if we've done all the entries in the table
5239 if ((!match) || (done == count)) break;
5240 }
5241
5242 // pass in score from property table
5243 score = IOServiceObjectOrder( table, (void *) gIOProbeScoreKey);
5244
5245 // do family specific matching
5246 match = where->matchPropertyTable( table, &score );
5247
5248 if( !match) {
5249 #if IOMATCHDEBUG
5250 if( kIOLogMatch & getDebugFlags( table ))
5251 LOG("%s: family specific matching fails\n", where->getName());
5252 #endif
5253 break;
5254 }
5255
5256 if (kIOServiceChangesOK & options) {
5257 // save the score
5258 newPri = OSNumber::withNumber( score, 32 );
5259 if( newPri) {
5260 table->setObject( gIOProbeScoreKey, newPri );
5261 newPri->release();
5262 }
5263 }
5264
5265 options = 0;
5266 matchParent = false;
5267
5268 nextTable = OSDynamicCast(OSDictionary,
5269 table->getObject( gIOParentMatchKey ));
5270 if( nextTable) {
5271 // look for a matching entry anywhere up to root
5272 match = false;
5273 matchParent = true;
5274 table = nextTable;
5275 break;
5276 }
5277
5278 table = OSDynamicCast(OSDictionary,
5279 table->getObject( gIOLocationMatchKey ));
5280 if (table) {
5281 // look for a matching entry at matchLocation()
5282 match = false;
5283 where = where->getProvider();
5284 if (where && (where = where->matchLocation(where))) continue;
5285 }
5286 break;
5287 }
5288 while (true);
5289 }
5290 while( matchParent && (!match) && (where = where->getProvider()) );
5291
5292 #if MATCH_DEBUG
5293 if (where != this)
5294 {
5295 OSSerialize * s = OSSerialize::withCapacity(128);
5296 root->serialize(s);
5297 kprintf("parent match 0x%llx, %d,\n%s\n", getRegistryEntryID(), match, s->text());
5298 s->release();
5299 }
5300 #endif
5301
5302 return( match );
5303 }
5304
5305
5306 IOReturn IOService::newUserClient( task_t owningTask, void * securityID,
5307 UInt32 type, OSDictionary * properties,
5308 IOUserClient ** handler )
5309 {
5310 const OSSymbol *userClientClass = 0;
5311 IOUserClient *client;
5312 OSObject *temp;
5313
5314 if (kIOReturnSuccess == newUserClient( owningTask, securityID, type, handler ))
5315 return kIOReturnSuccess;
5316
5317 // First try my own properties for a user client class name
5318 temp = getProperty(gIOUserClientClassKey);
5319 if (temp) {
5320 if (OSDynamicCast(OSSymbol, temp))
5321 userClientClass = (const OSSymbol *) temp;
5322 else if (OSDynamicCast(OSString, temp)) {
5323 userClientClass = OSSymbol::withString((OSString *) temp);
5324 if (userClientClass)
5325 setProperty(kIOUserClientClassKey,
5326 (OSObject *) userClientClass);
5327 }
5328 }
5329
5330 // Didn't find one so lets just bomb out now without further ado.
5331 if (!userClientClass)
5332 return kIOReturnUnsupported;
5333
5334 // This reference is consumed by the IOServiceOpen call
5335 temp = OSMetaClass::allocClassWithName(userClientClass);
5336 if (!temp)
5337 return kIOReturnNoMemory;
5338
5339 if (OSDynamicCast(IOUserClient, temp))
5340 client = (IOUserClient *) temp;
5341 else {
5342 temp->release();
5343 return kIOReturnUnsupported;
5344 }
5345
5346 if ( !client->initWithTask(owningTask, securityID, type, properties) ) {
5347 client->release();
5348 return kIOReturnBadArgument;
5349 }
5350
5351 if ( !client->attach(this) ) {
5352 client->release();
5353 return kIOReturnUnsupported;
5354 }
5355
5356 if ( !client->start(this) ) {
5357 client->detach(this);
5358 client->release();
5359 return kIOReturnUnsupported;
5360 }
5361
5362 *handler = client;
5363 return kIOReturnSuccess;
5364 }
5365
5366 IOReturn IOService::newUserClient( task_t owningTask, void * securityID,
5367 UInt32 type, IOUserClient ** handler )
5368 {
5369 return( kIOReturnUnsupported );
5370 }
5371
5372 IOReturn IOService::requestProbe( IOOptionBits options )
5373 {
5374 return( kIOReturnUnsupported);
5375 }
5376
5377 /*
5378 * Convert an IOReturn to text. Subclasses which add additional
5379 * IOReturn's should override this method and call
5380 * super::stringFromReturn if the desired value is not found.
5381 */
5382
5383 const char * IOService::stringFromReturn( IOReturn rtn )
5384 {
5385 static const IONamedValue IOReturn_values[] = {
5386 {kIOReturnSuccess, "success" },
5387 {kIOReturnError, "general error" },
5388 {kIOReturnNoMemory, "memory allocation error" },
5389 {kIOReturnNoResources, "resource shortage" },
5390 {kIOReturnIPCError, "Mach IPC failure" },
5391 {kIOReturnNoDevice, "no such device" },
5392 {kIOReturnNotPrivileged, "privilege violation" },
5393 {kIOReturnBadArgument, "invalid argument" },
5394 {kIOReturnLockedRead, "device is read locked" },
5395 {kIOReturnLockedWrite, "device is write locked" },
5396 {kIOReturnExclusiveAccess, "device is exclusive access" },
5397 {kIOReturnBadMessageID, "bad IPC message ID" },
5398 {kIOReturnUnsupported, "unsupported function" },
5399 {kIOReturnVMError, "virtual memory error" },
5400 {kIOReturnInternalError, "internal driver error" },
5401 {kIOReturnIOError, "I/O error" },
5402 {kIOReturnCannotLock, "cannot acquire lock" },
5403 {kIOReturnNotOpen, "device is not open" },
5404 {kIOReturnNotReadable, "device is not readable" },
5405 {kIOReturnNotWritable, "device is not writeable" },
5406 {kIOReturnNotAligned, "alignment error" },
5407 {kIOReturnBadMedia, "media error" },
5408 {kIOReturnStillOpen, "device is still open" },
5409 {kIOReturnRLDError, "rld failure" },
5410 {kIOReturnDMAError, "DMA failure" },
5411 {kIOReturnBusy, "device is busy" },
5412 {kIOReturnTimeout, "I/O timeout" },
5413 {kIOReturnOffline, "device is offline" },
5414 {kIOReturnNotReady, "device is not ready" },
5415 {kIOReturnNotAttached, "device/channel is not attached" },
5416 {kIOReturnNoChannels, "no DMA channels available" },
5417 {kIOReturnNoSpace, "no space for data" },
5418 {kIOReturnPortExists, "device port already exists" },
5419 {kIOReturnCannotWire, "cannot wire physical memory" },
5420 {kIOReturnNoInterrupt, "no interrupt attached" },
5421 {kIOReturnNoFrames, "no DMA frames enqueued" },
5422 {kIOReturnMessageTooLarge, "message is too large" },
5423 {kIOReturnNotPermitted, "operation is not permitted" },
5424 {kIOReturnNoPower, "device is without power" },
5425 {kIOReturnNoMedia, "media is not present" },
5426 {kIOReturnUnformattedMedia, "media is not formatted" },
5427 {kIOReturnUnsupportedMode, "unsupported mode" },
5428 {kIOReturnUnderrun, "data underrun" },
5429 {kIOReturnOverrun, "data overrun" },
5430 {kIOReturnDeviceError, "device error" },
5431 {kIOReturnNoCompletion, "no completion routine" },
5432 {kIOReturnAborted, "operation was aborted" },
5433 {kIOReturnNoBandwidth, "bus bandwidth would be exceeded" },
5434 {kIOReturnNotResponding, "device is not responding" },
5435 {kIOReturnInvalid, "unanticipated driver error" },
5436 {0, NULL }
5437 };
5438
5439 return IOFindNameForValue(rtn, IOReturn_values);
5440 }
5441
5442 /*
5443 * Convert an IOReturn to an errno.
5444 */
5445 int IOService::errnoFromReturn( IOReturn rtn )
5446 {
5447 if (unix_err(err_get_code(rtn)) == rtn)
5448 return err_get_code(rtn);
5449
5450 switch(rtn) {
5451 // (obvious match)
5452 case kIOReturnSuccess:
5453 return(0);
5454 case kIOReturnNoMemory:
5455 return(ENOMEM);
5456 case kIOReturnNoDevice:
5457 return(ENXIO);
5458 case kIOReturnVMError:
5459 return(EFAULT);
5460 case kIOReturnNotPermitted:
5461 return(EPERM);
5462 case kIOReturnNotPrivileged:
5463 return(EACCES);
5464 case kIOReturnIOError:
5465 return(EIO);
5466 case kIOReturnNotWritable:
5467 return(EROFS);
5468 case kIOReturnBadArgument:
5469 return(EINVAL);
5470 case kIOReturnUnsupported:
5471 return(ENOTSUP);
5472 case kIOReturnBusy:
5473 return(EBUSY);
5474 case kIOReturnNoPower:
5475 return(EPWROFF);
5476 case kIOReturnDeviceError:
5477 return(EDEVERR);
5478 case kIOReturnTimeout:
5479 return(ETIMEDOUT);
5480 case kIOReturnMessageTooLarge:
5481 return(EMSGSIZE);
5482 case kIOReturnNoSpace:
5483 return(ENOSPC);
5484 case kIOReturnCannotLock:
5485 return(ENOLCK);
5486
5487 // (best match)
5488 case kIOReturnBadMessageID:
5489 case kIOReturnNoCompletion:
5490 case kIOReturnNotAligned:
5491 return(EINVAL);
5492 case kIOReturnNotReady:
5493 return(EBUSY);
5494 case kIOReturnRLDError:
5495 return(EBADMACHO);
5496 case kIOReturnPortExists:
5497 case kIOReturnStillOpen:
5498 return(EEXIST);
5499 case kIOReturnExclusiveAccess:
5500 case kIOReturnLockedRead:
5501 case kIOReturnLockedWrite:
5502 case kIOReturnNotOpen:
5503 case kIOReturnNotReadable:
5504 return(EACCES);
5505 case kIOReturnCannotWire:
5506 case kIOReturnNoResources:
5507 return(ENOMEM);
5508 case kIOReturnAborted:
5509 case kIOReturnOffline:
5510 case kIOReturnNotResponding:
5511 return(EBUSY);
5512 case kIOReturnBadMedia:
5513 case kIOReturnNoMedia:
5514 case kIOReturnNotAttached:
5515 case kIOReturnUnformattedMedia:
5516 return(ENXIO); // (media error)
5517 case kIOReturnDMAError:
5518 case kIOReturnOverrun:
5519 case kIOReturnUnderrun:
5520 return(EIO); // (transfer error)
5521 case kIOReturnNoBandwidth:
5522 case kIOReturnNoChannels:
5523 case kIOReturnNoFrames:
5524 case kIOReturnNoInterrupt:
5525 return(EIO); // (hardware error)
5526 case kIOReturnError:
5527 case kIOReturnInternalError:
5528 case kIOReturnInvalid:
5529 return(EIO); // (generic error)
5530 case kIOReturnIPCError:
5531 return(EIO); // (ipc error)
5532 default:
5533 return(EIO); // (all other errors)
5534 }
5535 }
5536
5537 IOReturn IOService::message( UInt32 type, IOService * provider,
5538 void * argument )
5539 {
5540 /*
5541 * Generic entry point for calls from the provider. A return value of
5542 * kIOReturnSuccess indicates that the message was received, and where
5543 * applicable, that it was successful.
5544 */
5545
5546 return kIOReturnUnsupported;
5547 }
5548
5549 /*
5550 * Device memory
5551 */
5552
5553 IOItemCount IOService::getDeviceMemoryCount( void )
5554 {
5555 OSArray * array;
5556 IOItemCount count;
5557
5558 array = OSDynamicCast( OSArray, getProperty( gIODeviceMemoryKey));
5559 if( array)
5560 count = array->getCount();
5561 else
5562 count = 0;
5563
5564 return( count);
5565 }
5566
5567 IODeviceMemory * IOService::getDeviceMemoryWithIndex( unsigned int index )
5568 {
5569 OSArray * array;
5570 IODeviceMemory * range;
5571
5572 array = OSDynamicCast( OSArray, getProperty( gIODeviceMemoryKey));
5573 if( array)
5574 range = (IODeviceMemory *) array->getObject( index );
5575 else
5576 range = 0;
5577
5578 return( range);
5579 }
5580
5581 IOMemoryMap * IOService::mapDeviceMemoryWithIndex( unsigned int index,
5582 IOOptionBits options )
5583 {
5584 IODeviceMemory * range;
5585 IOMemoryMap * map;
5586
5587 range = getDeviceMemoryWithIndex( index );
5588 if( range)
5589 map = range->map( options );
5590 else
5591 map = 0;
5592
5593 return( map );
5594 }
5595
5596 OSArray * IOService::getDeviceMemory( void )
5597 {
5598 return( OSDynamicCast( OSArray, getProperty( gIODeviceMemoryKey)));
5599 }
5600
5601
5602 void IOService::setDeviceMemory( OSArray * array )
5603 {
5604 setProperty( gIODeviceMemoryKey, array);
5605 }
5606
5607 /*
5608 * For machines where the transfers on an I/O bus can stall because
5609 * the CPU is in an idle mode, These APIs allow a driver to specify
5610 * the maximum bus stall that they can handle. 0 indicates no limit.
5611 */
5612 void IOService::
5613 setCPUSnoopDelay(UInt32 __unused ns)
5614 {
5615 #if defined(__i386__) || defined(__x86_64__)
5616 ml_set_maxsnoop(ns);
5617 #endif /* defined(__i386__) || defined(__x86_64__) */
5618 }
5619
5620 UInt32 IOService::
5621 getCPUSnoopDelay()
5622 {
5623 #if defined(__i386__) || defined(__x86_64__)
5624 return ml_get_maxsnoop();
5625 #else
5626 return 0;
5627 #endif /* defined(__i386__) || defined(__x86_64__) */
5628 }
5629
5630 #if defined(__i386__) || defined(__x86_64__)
5631 static void
5632 requireMaxCpuDelay(IOService * service, UInt32 ns, UInt32 delayType)
5633 {
5634 static const UInt kNoReplace = -1U; // Must be an illegal index
5635 UInt replace = kNoReplace;
5636 bool setCpuDelay = false;
5637
5638 IORecursiveLockLock(sCpuDelayLock);
5639
5640 UInt count = sCpuDelayData->getLength() / sizeof(CpuDelayEntry);
5641 CpuDelayEntry *entries = (CpuDelayEntry *) sCpuDelayData->getBytesNoCopy();
5642 IOService * holder = NULL;
5643
5644 if (ns) {
5645 const CpuDelayEntry ne = {service, ns, delayType};
5646 holder = service;
5647 // Set maximum delay.
5648 for (UInt i = 0; i < count; i++) {
5649 IOService *thisService = entries[i].fService;
5650 bool sameType = (delayType == entries[i].fDelayType);
5651 if ((service == thisService) && sameType)
5652 replace = i;
5653 else if (!thisService) {
5654 if (kNoReplace == replace)
5655 replace = i;
5656 }
5657 else if (sameType) {
5658 const UInt32 thisMax = entries[i].fMaxDelay;
5659 if (thisMax < ns)
5660 {
5661 ns = thisMax;
5662 holder = thisService;
5663 }
5664 }
5665 }
5666
5667 setCpuDelay = true;
5668 if (kNoReplace == replace)
5669 sCpuDelayData->appendBytes(&ne, sizeof(ne));
5670 else
5671 entries[replace] = ne;
5672 }
5673 else {
5674 ns = -1U; // Set to max unsigned, i.e. no restriction
5675
5676 for (UInt i = 0; i < count; i++) {
5677 // Clear a maximum delay.
5678 IOService *thisService = entries[i].fService;
5679 if (thisService && (delayType == entries[i].fDelayType)) {
5680 UInt32 thisMax = entries[i].fMaxDelay;
5681 if (service == thisService)
5682 replace = i;
5683 else if (thisMax < ns) {
5684 ns = thisMax;
5685 holder = thisService;
5686 }
5687 }
5688 }
5689
5690 // Check if entry found
5691 if (kNoReplace != replace) {
5692 entries[replace].fService = 0; // Null the entry
5693 setCpuDelay = true;
5694 }
5695 }
5696
5697 if (setCpuDelay)
5698 {
5699 if (holder && debug_boot_arg) {
5700 strlcpy(sCPULatencyHolderName[delayType], holder->getName(), sizeof(sCPULatencyHolderName[delayType]));
5701 }
5702
5703 // Must be safe to call from locked context
5704 if (delayType == kCpuDelayBusStall)
5705 {
5706 ml_set_maxbusdelay(ns);
5707 }
5708 else if (delayType == kCpuDelayInterrupt)
5709 {
5710 ml_set_maxintdelay(ns);
5711 }
5712 sCPULatencyHolder[delayType]->setValue(holder ? holder->getRegistryEntryID() : 0);
5713 sCPULatencySet [delayType]->setValue(ns);
5714
5715 OSArray * handlers = sCpuLatencyHandlers[delayType];
5716 IOService * target;
5717 if (handlers) for (unsigned int idx = 0;
5718 (target = (IOService *) handlers->getObject(idx));
5719 idx++)
5720 {
5721 target->callPlatformFunction(sCPULatencyFunctionName[delayType], false,
5722 (void *) (uintptr_t) ns, holder,
5723 NULL, NULL);
5724 }
5725 }
5726
5727 IORecursiveLockUnlock(sCpuDelayLock);
5728 }
5729
5730 static IOReturn
5731 setLatencyHandler(UInt32 delayType, IOService * target, bool enable)
5732 {
5733 IOReturn result = kIOReturnNotFound;
5734 OSArray * array;
5735 unsigned int idx;
5736
5737 IORecursiveLockLock(sCpuDelayLock);
5738
5739 do
5740 {
5741 if (enable && !sCpuLatencyHandlers[delayType])
5742 sCpuLatencyHandlers[delayType] = OSArray::withCapacity(4);
5743 array = sCpuLatencyHandlers[delayType];
5744 if (!array)
5745 break;
5746 idx = array->getNextIndexOfObject(target, 0);
5747 if (!enable)
5748 {
5749 if (-1U != idx)
5750 {
5751 array->removeObject(idx);
5752 result = kIOReturnSuccess;
5753 }
5754 }
5755 else
5756 {
5757 if (-1U != idx) {
5758 result = kIOReturnExclusiveAccess;
5759 break;
5760 }
5761 array->setObject(target);
5762
5763 UInt count = sCpuDelayData->getLength() / sizeof(CpuDelayEntry);
5764 CpuDelayEntry *entries = (CpuDelayEntry *) sCpuDelayData->getBytesNoCopy();
5765 UInt32 ns = -1U; // Set to max unsigned, i.e. no restriction
5766 IOService * holder = NULL;
5767
5768 for (UInt i = 0; i < count; i++) {
5769 if (entries[i].fService
5770 && (delayType == entries[i].fDelayType)
5771 && (entries[i].fMaxDelay < ns)) {
5772 ns = entries[i].fMaxDelay;
5773 holder = entries[i].fService;
5774 }
5775 }
5776 target->callPlatformFunction(sCPULatencyFunctionName[delayType], false,
5777 (void *) (uintptr_t) ns, holder,
5778 NULL, NULL);
5779 result = kIOReturnSuccess;
5780 }
5781 }
5782 while (false);
5783
5784 IORecursiveLockUnlock(sCpuDelayLock);
5785
5786 return (result);
5787 }
5788
5789 #endif /* defined(__i386__) || defined(__x86_64__) */
5790
5791 void IOService::
5792 requireMaxBusStall(UInt32 __unused ns)
5793 {
5794 #if defined(__i386__) || defined(__x86_64__)
5795 requireMaxCpuDelay(this, ns, kCpuDelayBusStall);
5796 #endif
5797 }
5798
5799 void IOService::
5800 requireMaxInterruptDelay(uint32_t __unused ns)
5801 {
5802 #if defined(__i386__) || defined(__x86_64__)
5803 requireMaxCpuDelay(this, ns, kCpuDelayInterrupt);
5804 #endif
5805 }
5806
5807 /*
5808 * Device interrupts
5809 */
5810
5811 IOReturn IOService::resolveInterrupt(IOService *nub, int source)
5812 {
5813 IOInterruptController *interruptController;
5814 OSArray *array;
5815 OSData *data;
5816 OSSymbol *interruptControllerName;
5817 long numSources;
5818 IOInterruptSource *interruptSources;
5819
5820 // Get the parents list from the nub.
5821 array = OSDynamicCast(OSArray, nub->getProperty(gIOInterruptControllersKey));
5822 if (array == 0) return kIOReturnNoResources;
5823
5824 // Allocate space for the IOInterruptSources if needed... then return early.
5825 if (nub->_interruptSources == 0) {
5826 numSources = array->getCount();
5827 interruptSources = (IOInterruptSource *)IOMalloc(numSources * sizeof(IOInterruptSource));
5828 if (interruptSources == 0) return kIOReturnNoMemory;
5829
5830 bzero(interruptSources, numSources * sizeof(IOInterruptSource));
5831
5832 nub->_numInterruptSources = numSources;
5833 nub->_interruptSources = interruptSources;
5834 return kIOReturnSuccess;
5835 }
5836
5837 interruptControllerName = OSDynamicCast(OSSymbol,array->getObject(source));
5838 if (interruptControllerName == 0) return kIOReturnNoResources;
5839
5840 interruptController = getPlatform()->lookUpInterruptController(interruptControllerName);
5841 if (interruptController == 0) return kIOReturnNoResources;
5842
5843 // Get the interrupt numbers from the nub.
5844 array = OSDynamicCast(OSArray, nub->getProperty(gIOInterruptSpecifiersKey));
5845 if (array == 0) return kIOReturnNoResources;
5846 data = OSDynamicCast(OSData, array->getObject(source));
5847 if (data == 0) return kIOReturnNoResources;
5848
5849 // Set the interruptController and interruptSource in the nub's table.
5850 interruptSources = nub->_interruptSources;
5851 interruptSources[source].interruptController = interruptController;
5852 interruptSources[source].vectorData = data;
5853
5854 return kIOReturnSuccess;
5855 }
5856
5857 IOReturn IOService::lookupInterrupt(int source, bool resolve, IOInterruptController **interruptController)
5858 {
5859 IOReturn ret;
5860
5861 /* Make sure the _interruptSources are set */
5862 if (_interruptSources == 0) {
5863 ret = resolveInterrupt(this, source);
5864 if (ret != kIOReturnSuccess) return ret;
5865 }
5866
5867 /* Make sure the local source number is valid */
5868 if ((source < 0) || (source >= _numInterruptSources))
5869 return kIOReturnNoInterrupt;
5870
5871 /* Look up the contoller for the local source */
5872 *interruptController = _interruptSources[source].interruptController;
5873
5874 if (*interruptController == NULL) {
5875 if (!resolve) return kIOReturnNoInterrupt;
5876
5877 /* Try to reslove the interrupt */
5878 ret = resolveInterrupt(this, source);
5879 if (ret != kIOReturnSuccess) return ret;
5880
5881 *interruptController = _interruptSources[source].interruptController;
5882 }
5883
5884 return kIOReturnSuccess;
5885 }
5886
5887 IOReturn IOService::registerInterrupt(int source, OSObject *target,
5888 IOInterruptAction handler,
5889 void *refCon)
5890 {
5891 IOInterruptController *interruptController;
5892 IOReturn ret;
5893
5894 ret = lookupInterrupt(source, true, &interruptController);
5895 if (ret != kIOReturnSuccess) return ret;
5896
5897 /* Register the source */
5898 return interruptController->registerInterrupt(this, source, target,
5899 (IOInterruptHandler)handler,
5900 refCon);
5901 }
5902
5903 IOReturn IOService::unregisterInterrupt(int source)
5904 {
5905 IOInterruptController *interruptController;
5906 IOReturn ret;
5907
5908 ret = lookupInterrupt(source, false, &interruptController);
5909 if (ret != kIOReturnSuccess) return ret;
5910
5911 /* Unregister the source */
5912 return interruptController->unregisterInterrupt(this, source);
5913 }
5914
5915 IOReturn IOService::addInterruptStatistics(IOInterruptAccountingData * statistics, int source)
5916 {
5917 IOReportLegend * legend = NULL;
5918 IOInterruptAccountingData * oldValue = NULL;
5919 IOInterruptAccountingReporter * newArray = NULL;
5920 char subgroupName[64];
5921 int newArraySize = 0;
5922 int i = 0;
5923
5924 if (source < 0) {
5925 return kIOReturnBadArgument;
5926 }
5927
5928 /*
5929 * We support statistics on a maximum of 256 interrupts per nub; if a nub
5930 * has more than 256 interrupt specifiers associated with it, and tries
5931 * to register a high interrupt index with interrupt accounting, panic.
5932 * Having more than 256 interrupts associated with a single nub is
5933 * probably a sign that something fishy is going on.
5934 */
5935 if (source > IA_INDEX_MAX) {
5936 panic("addInterruptStatistics called for an excessively large index (%d)", source);
5937 }
5938
5939 /*
5940 * TODO: This is ugly (wrapping a lock around an allocation). I'm only
5941 * leaving it as is because the likelihood of contention where we are
5942 * actually growing the array is minimal (we would realistically need
5943 * to be starting a driver for the first time, with an IOReporting
5944 * client already in place). Nonetheless, cleanup that can be done
5945 * to adhere to best practices; it'll make the code more complicated,
5946 * unfortunately.
5947 */
5948 IOLockLock(reserved->interruptStatisticsLock);
5949
5950 /*
5951 * Lazily allocate the statistics array.
5952 */
5953 if (!reserved->interruptStatisticsArray) {
5954 reserved->interruptStatisticsArray = IONew(IOInterruptAccountingReporter, 1);
5955 assert(reserved->interruptStatisticsArray);
5956 reserved->interruptStatisticsArrayCount = 1;
5957 bzero(reserved->interruptStatisticsArray, sizeof(*reserved->interruptStatisticsArray));
5958 }
5959
5960 if (source >= reserved->interruptStatisticsArrayCount) {
5961 /*
5962 * We're still within the range of supported indices, but we are out
5963 * of space in the current array. Do a nasty realloc (because
5964 * IORealloc isn't a thing) here. We'll double the size with each
5965 * reallocation.
5966 *
5967 * Yes, the "next power of 2" could be more efficient; but this will
5968 * be invoked incredibly rarely. Who cares.
5969 */
5970 newArraySize = (reserved->interruptStatisticsArrayCount << 1);
5971
5972 while (newArraySize <= source)
5973 newArraySize = (newArraySize << 1);
5974 newArray = IONew(IOInterruptAccountingReporter, newArraySize);
5975
5976 assert(newArray);
5977
5978 /*
5979 * TODO: This even zeroes the memory it is about to overwrite.
5980 * Shameful; fix it. Not particularly high impact, however.
5981 */
5982 bzero(newArray, newArraySize * sizeof(*newArray));
5983 memcpy(newArray, reserved->interruptStatisticsArray, reserved->interruptStatisticsArrayCount * sizeof(*newArray));
5984 IODelete(reserved->interruptStatisticsArray, IOInterruptAccountingReporter, reserved->interruptStatisticsArrayCount);
5985 reserved->interruptStatisticsArray = newArray;
5986 reserved->interruptStatisticsArrayCount = newArraySize;
5987 }
5988
5989 if (!reserved->interruptStatisticsArray[source].reporter) {
5990 /*
5991 * We don't have a reporter associated with this index yet, so we
5992 * need to create one.
5993 */
5994 /*
5995 * TODO: Some statistics do in fact have common units (time); should this be
5996 * split into separate reporters to communicate this?
5997 */
5998 reserved->interruptStatisticsArray[source].reporter = IOSimpleReporter::with(this, kIOReportCategoryPower, kIOReportUnitNone);
5999
6000 /*
6001 * Each statistic is given an identifier based on the interrupt index (which
6002 * should be unique relative to any single nub) and the statistic involved.
6003 * We should now have a sane (small and positive) index, so start
6004 * constructing the channels for statistics.
6005 */
6006 for (i = 0; i < IA_NUM_INTERRUPT_ACCOUNTING_STATISTICS; i++) {
6007 /*
6008 * TODO: Currently, this does not add channels for disabled statistics.
6009 * Will this be confusing for clients? If so, we should just add the
6010 * channels; we can avoid updating the channels even if they exist.
6011 */
6012 if (IA_GET_STATISTIC_ENABLED(i))
6013 reserved->interruptStatisticsArray[source].reporter->addChannel(IA_GET_CHANNEL_ID(source, i), kInterruptAccountingStatisticNameArray[i]);
6014 }
6015
6016 /*
6017 * We now need to add the legend for this reporter to the registry.
6018 */
6019 legend = IOReportLegend::with(OSDynamicCast(OSArray, getProperty(kIOReportLegendKey)));
6020
6021 /*
6022 * Note that while we compose the subgroup name, we do not need to
6023 * manage its lifecycle (the reporter will handle this).
6024 */
6025 snprintf(subgroupName, sizeof(subgroupName), "%s %d", getName(), source);
6026 subgroupName[sizeof(subgroupName) - 1] = 0;
6027 legend->addReporterLegend(reserved->interruptStatisticsArray[source].reporter, kInterruptAccountingGroupName, subgroupName);
6028 setProperty(kIOReportLegendKey, legend->getLegend());
6029 legend->release();
6030
6031 /*
6032 * TODO: Is this a good idea? Probably not; my assumption is it opts
6033 * all entities who register interrupts into public disclosure of all
6034 * IOReporting channels. Unfortunately, this appears to be as fine
6035 * grain as it gets.
6036 */
6037 setProperty(kIOReportLegendPublicKey, true);
6038 }
6039
6040 /*
6041 * Don't stomp existing entries. If we are about to, panic; this
6042 * probably means we failed to tear down our old interrupt source
6043 * correctly.
6044 */
6045 oldValue = reserved->interruptStatisticsArray[source].statistics;
6046
6047 if (oldValue) {
6048 panic("addInterruptStatistics call for index %d would have clobbered existing statistics", source);
6049 }
6050
6051 reserved->interruptStatisticsArray[source].statistics = statistics;
6052
6053 /*
6054 * Inherit the reporter values for each statistic. The target may
6055 * be torn down as part of the runtime of the service (especially
6056 * for sleep/wake), so we inherit in order to avoid having values
6057 * reset for no apparent reason. Our statistics are ultimately
6058 * tied to the index and the sevice, not to an individual target,
6059 * so we should maintain them accordingly.
6060 */
6061 interruptAccountingDataInheritChannels(reserved->interruptStatisticsArray[source].statistics, reserved->interruptStatisticsArray[source].reporter);
6062
6063 IOLockUnlock(reserved->interruptStatisticsLock);
6064
6065 return kIOReturnSuccess;
6066 }
6067
6068 IOReturn IOService::removeInterruptStatistics(int source)
6069 {
6070 IOInterruptAccountingData * value = NULL;
6071
6072 if (source < 0) {
6073 return kIOReturnBadArgument;
6074 }
6075
6076 IOLockLock(reserved->interruptStatisticsLock);
6077
6078 /*
6079 * We dynamically grow the statistics array, so an excessively
6080 * large index value has NEVER been registered. This either
6081 * means our cap on the array size is too small (unlikely), or
6082 * that we have been passed a corrupt index (this must be passed
6083 * the plain index into the interrupt specifier list).
6084 */
6085 if (source >= reserved->interruptStatisticsArrayCount) {
6086 panic("removeInterruptStatistics called for index %d, which was never registered", source);
6087 }
6088
6089 assert(reserved->interruptStatisticsArray);
6090
6091 /*
6092 * If there is no existing entry, we are most likely trying to
6093 * free an interrupt owner twice, or we have corrupted the
6094 * index value.
6095 */
6096 value = reserved->interruptStatisticsArray[source].statistics;
6097
6098 if (!value) {
6099 panic("removeInterruptStatistics called for empty index %d", source);
6100 }
6101
6102 /*
6103 * We update the statistics, so that any delta with the reporter
6104 * state is not lost.
6105 */
6106 interruptAccountingDataUpdateChannels(reserved->interruptStatisticsArray[source].statistics, reserved->interruptStatisticsArray[source].reporter);
6107 reserved->interruptStatisticsArray[source].statistics = NULL;
6108 IOLockUnlock(reserved->interruptStatisticsLock);
6109
6110 return kIOReturnSuccess;
6111 }
6112
6113 IOReturn IOService::getInterruptType(int source, int *interruptType)
6114 {
6115 IOInterruptController *interruptController;
6116 IOReturn ret;
6117
6118 ret = lookupInterrupt(source, true, &interruptController);
6119 if (ret != kIOReturnSuccess) return ret;
6120
6121 /* Return the type */
6122 return interruptController->getInterruptType(this, source, interruptType);
6123 }
6124
6125 IOReturn IOService::enableInterrupt(int source)
6126 {
6127 IOInterruptController *interruptController;
6128 IOReturn ret;
6129
6130 ret = lookupInterrupt(source, false, &interruptController);
6131 if (ret != kIOReturnSuccess) return ret;
6132
6133 /* Enable the source */
6134 return interruptController->enableInterrupt(this, source);
6135 }
6136
6137 IOReturn IOService::disableInterrupt(int source)
6138 {
6139 IOInterruptController *interruptController;
6140 IOReturn ret;
6141
6142 ret = lookupInterrupt(source, false, &interruptController);
6143 if (ret != kIOReturnSuccess) return ret;
6144
6145 /* Disable the source */
6146 return interruptController->disableInterrupt(this, source);
6147 }
6148
6149 IOReturn IOService::causeInterrupt(int source)
6150 {
6151 IOInterruptController *interruptController;
6152 IOReturn ret;
6153
6154 ret = lookupInterrupt(source, false, &interruptController);
6155 if (ret != kIOReturnSuccess) return ret;
6156
6157 /* Cause an interrupt for the source */
6158 return interruptController->causeInterrupt(this, source);
6159 }
6160
6161 IOReturn IOService::configureReport(IOReportChannelList *channelList,
6162 IOReportConfigureAction action,
6163 void *result,
6164 void *destination)
6165 {
6166 unsigned cnt;
6167
6168 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
6169 if ( channelList->channels[cnt].channel_id == kPMPowerStatesChID ) {
6170 if (pwrMgt) configurePowerStatesReport(action, result);
6171 else return kIOReturnUnsupported;
6172 }
6173 else if ( channelList->channels[cnt].channel_id == kPMCurrStateChID ) {
6174 if (pwrMgt) configureSimplePowerReport(action, result);
6175 else return kIOReturnUnsupported;
6176 }
6177 }
6178
6179 IOLockLock(reserved->interruptStatisticsLock);
6180
6181 /* The array count is signed (because the interrupt indices are signed), hence the cast */
6182 for (cnt = 0; cnt < (unsigned) reserved->interruptStatisticsArrayCount; cnt++) {
6183 if (reserved->interruptStatisticsArray[cnt].reporter) {
6184 /*
6185 * If the reporter is currently associated with the statistics
6186 * for an event source, we may need to update the reporter.
6187 */
6188 if (reserved->interruptStatisticsArray[cnt].statistics)
6189 interruptAccountingDataUpdateChannels(reserved->interruptStatisticsArray[cnt].statistics, reserved->interruptStatisticsArray[cnt].reporter);
6190
6191 reserved->interruptStatisticsArray[cnt].reporter->configureReport(channelList, action, result, destination);
6192 }
6193 }
6194
6195 IOLockUnlock(reserved->interruptStatisticsLock);
6196
6197 return kIOReturnSuccess;
6198 }
6199
6200 IOReturn IOService::updateReport(IOReportChannelList *channelList,
6201 IOReportUpdateAction action,
6202 void *result,
6203 void *destination)
6204 {
6205 unsigned cnt;
6206
6207 for (cnt = 0; cnt < channelList->nchannels; cnt++) {
6208 if ( channelList->channels[cnt].channel_id == kPMPowerStatesChID ) {
6209 if (pwrMgt) updatePowerStatesReport(action, result, destination);
6210 else return kIOReturnUnsupported;
6211 }
6212 else if ( channelList->channels[cnt].channel_id == kPMCurrStateChID ) {
6213 if (pwrMgt) updateSimplePowerReport(action, result, destination);
6214 else return kIOReturnUnsupported;
6215 }
6216 }
6217
6218 IOLockLock(reserved->interruptStatisticsLock);
6219
6220 /* The array count is signed (because the interrupt indices are signed), hence the cast */
6221 for (cnt = 0; cnt < (unsigned) reserved->interruptStatisticsArrayCount; cnt++) {
6222 if (reserved->interruptStatisticsArray[cnt].reporter) {
6223 /*
6224 * If the reporter is currently associated with the statistics
6225 * for an event source, we need to update the reporter.
6226 */
6227 if (reserved->interruptStatisticsArray[cnt].statistics)
6228 interruptAccountingDataUpdateChannels(reserved->interruptStatisticsArray[cnt].statistics, reserved->interruptStatisticsArray[cnt].reporter);
6229
6230 reserved->interruptStatisticsArray[cnt].reporter->updateReport(channelList, action, result, destination);
6231 }
6232 }
6233
6234 IOLockUnlock(reserved->interruptStatisticsLock);
6235
6236 return kIOReturnSuccess;
6237 }
6238
6239 uint64_t IOService::getAuthorizationID( void )
6240 {
6241 return reserved->authorizationID;
6242 }
6243
6244 IOReturn IOService::setAuthorizationID( uint64_t authorizationID )
6245 {
6246 OSObject * entitlement;
6247 IOReturn status;
6248
6249 entitlement = IOUserClient::copyClientEntitlement( current_task( ), "com.apple.private.iokit.IOServiceSetAuthorizationID" );
6250
6251 if ( entitlement )
6252 {
6253 if ( entitlement == kOSBooleanTrue )
6254 {
6255 reserved->authorizationID = authorizationID;
6256
6257 status = kIOReturnSuccess;
6258 }
6259 else
6260 {
6261 status = kIOReturnNotPrivileged;
6262 }
6263
6264 entitlement->release( );
6265 }
6266 else
6267 {
6268 status = kIOReturnNotPrivileged;
6269 }
6270
6271 return status;
6272 }
6273
6274 #if __LP64__
6275 OSMetaClassDefineReservedUsed(IOService, 0);
6276 OSMetaClassDefineReservedUsed(IOService, 1);
6277 OSMetaClassDefineReservedUnused(IOService, 2);
6278 OSMetaClassDefineReservedUnused(IOService, 3);
6279 OSMetaClassDefineReservedUnused(IOService, 4);
6280 OSMetaClassDefineReservedUnused(IOService, 5);
6281 OSMetaClassDefineReservedUnused(IOService, 6);
6282 OSMetaClassDefineReservedUnused(IOService, 7);
6283 #else
6284 OSMetaClassDefineReservedUsed(IOService, 0);
6285 OSMetaClassDefineReservedUsed(IOService, 1);
6286 OSMetaClassDefineReservedUsed(IOService, 2);
6287 OSMetaClassDefineReservedUsed(IOService, 3);
6288 OSMetaClassDefineReservedUsed(IOService, 4);
6289 OSMetaClassDefineReservedUsed(IOService, 5);
6290 OSMetaClassDefineReservedUsed(IOService, 6);
6291 OSMetaClassDefineReservedUsed(IOService, 7);
6292 #endif
6293 OSMetaClassDefineReservedUnused(IOService, 8);
6294 OSMetaClassDefineReservedUnused(IOService, 9);
6295 OSMetaClassDefineReservedUnused(IOService, 10);
6296 OSMetaClassDefineReservedUnused(IOService, 11);
6297 OSMetaClassDefineReservedUnused(IOService, 12);
6298 OSMetaClassDefineReservedUnused(IOService, 13);
6299 OSMetaClassDefineReservedUnused(IOService, 14);
6300 OSMetaClassDefineReservedUnused(IOService, 15);
6301 OSMetaClassDefineReservedUnused(IOService, 16);
6302 OSMetaClassDefineReservedUnused(IOService, 17);
6303 OSMetaClassDefineReservedUnused(IOService, 18);
6304 OSMetaClassDefineReservedUnused(IOService, 19);
6305 OSMetaClassDefineReservedUnused(IOService, 20);
6306 OSMetaClassDefineReservedUnused(IOService, 21);
6307 OSMetaClassDefineReservedUnused(IOService, 22);
6308 OSMetaClassDefineReservedUnused(IOService, 23);
6309 OSMetaClassDefineReservedUnused(IOService, 24);
6310 OSMetaClassDefineReservedUnused(IOService, 25);
6311 OSMetaClassDefineReservedUnused(IOService, 26);
6312 OSMetaClassDefineReservedUnused(IOService, 27);
6313 OSMetaClassDefineReservedUnused(IOService, 28);
6314 OSMetaClassDefineReservedUnused(IOService, 29);
6315 OSMetaClassDefineReservedUnused(IOService, 30);
6316 OSMetaClassDefineReservedUnused(IOService, 31);
6317 OSMetaClassDefineReservedUnused(IOService, 32);
6318 OSMetaClassDefineReservedUnused(IOService, 33);
6319 OSMetaClassDefineReservedUnused(IOService, 34);
6320 OSMetaClassDefineReservedUnused(IOService, 35);
6321 OSMetaClassDefineReservedUnused(IOService, 36);
6322 OSMetaClassDefineReservedUnused(IOService, 37);
6323 OSMetaClassDefineReservedUnused(IOService, 38);
6324 OSMetaClassDefineReservedUnused(IOService, 39);
6325 OSMetaClassDefineReservedUnused(IOService, 40);
6326 OSMetaClassDefineReservedUnused(IOService, 41);
6327 OSMetaClassDefineReservedUnused(IOService, 42);
6328 OSMetaClassDefineReservedUnused(IOService, 43);
6329 OSMetaClassDefineReservedUnused(IOService, 44);
6330 OSMetaClassDefineReservedUnused(IOService, 45);
6331 OSMetaClassDefineReservedUnused(IOService, 46);
6332 OSMetaClassDefineReservedUnused(IOService, 47);