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