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