]> git.saurik.com Git - apple/xnu.git/blobdiff - iokit/Kernel/IOService.cpp
xnu-1228.9.59.tar.gz
[apple/xnu.git] / iokit / Kernel / IOService.cpp
index b5991ba4e4dcde1777529279a6ad6634a215d69f..f58ea137c7dd02cf388b55b54359bd02ccb70ef7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1998-2007 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
@@ -146,6 +146,15 @@ static OSArray *           gIOFinalizeList;
 static SInt32                  gIOConsoleUsersSeed;
 static OSData *                        gIOConsoleUsersSeedValue;
 
+extern const OSSymbol *                gIODTPHandleKey;
+
+const OSSymbol *               gIOPlatformSleepActionKey;
+const OSSymbol *               gIOPlatformWakeActionKey;
+const OSSymbol *               gIOPlatformQuiesceActionKey;
+const OSSymbol *               gIOPlatformActiveActionKey;
+
+const OSSymbol *               gIOPlatformFunctionHandlerSet;
+
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 #define LOCKREADNOTIFY()       \
@@ -199,19 +208,34 @@ bool IOService::isInactive( void ) const
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
-#if __i386__
+#if defined(__i386__)
 
 // Only used by the intel implementation of
-//     IOService::requireMaxBusStall(UInt32 __unused ns)
-struct BusStallEntry
+//     IOService::requireMaxBusStall(UInt32 ns)
+//     IOService::requireMaxInterruptDelay(uint32_t ns)
+struct CpuDelayEntry
 {
-    const IOService *fService;
-    UInt32 fMaxDelay;
+    IOService * fService;
+    UInt32      fMaxDelay;
+    UInt32      fDelayType;
 };
 
-static OSData *sBusStall     = OSData::withCapacity(8 * sizeof(BusStallEntry));
-static IOLock *sBusStallLock = IOLockAlloc();
-#endif /* __i386__ */
+enum {
+    kCpuDelayBusStall, kCpuDelayInterrupt,
+    kCpuNumDelayTypes
+};
+
+static OSData          *sCpuDelayData = OSData::withCapacity(8 * sizeof(CpuDelayEntry));
+static IORecursiveLock *sCpuDelayLock = IORecursiveLockAlloc();
+static OSArray         *sCpuLatencyHandlers[kCpuNumDelayTypes];
+const OSSymbol         *sCPULatencyFunctionName[kCpuNumDelayTypes];
+
+static void
+requireMaxCpuDelay(IOService * service, UInt32 ns, UInt32 delayType);
+static IOReturn
+setLatencyHandler(UInt32 delayType, IOService * target, bool enable);
+
+#endif /* defined(__i386__) */
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
@@ -276,6 +300,16 @@ void IOService::initialize( void )
     gIOConsoleSessionSecureInputPIDKey = OSSymbol::withCStringNoCopy( kIOConsoleSessionSecureInputPIDKey);
     gIOConsoleUsersSeedValue   = OSData::withBytesNoCopy(&gIOConsoleUsersSeed, sizeof(gIOConsoleUsersSeed));
 
+    gIOPlatformSleepActionKey  = OSSymbol::withCStringNoCopy(kIOPlatformSleepActionKey);
+    gIOPlatformWakeActionKey   = OSSymbol::withCStringNoCopy(kIOPlatformWakeActionKey);
+    gIOPlatformQuiesceActionKey        = OSSymbol::withCStringNoCopy(kIOPlatformQuiesceActionKey);
+    gIOPlatformActiveActionKey = OSSymbol::withCStringNoCopy(kIOPlatformActiveActionKey);
+
+    gIOPlatformFunctionHandlerSet              = OSSymbol::withCStringNoCopy(kIOPlatformFunctionHandlerSet);
+#if defined(__i386__)
+    sCPULatencyFunctionName[kCpuDelayBusStall] = OSSymbol::withCStringNoCopy(kIOPlatformFunctionHandlerMaxBusDelay);
+    sCPULatencyFunctionName[kCpuDelayInterrupt]        = OSSymbol::withCStringNoCopy(kIOPlatformFunctionHandlerMaxInterruptDelay);
+#endif
     gNotificationLock          = IORecursiveLockAlloc();
 
     assert( gIOServicePlane && gIODeviceMemoryKey
@@ -810,9 +844,23 @@ IOReturn IOService::callPlatformFunction( const OSSymbol * functionName,
                                          void *param3, void *param4 )
 {
   IOReturn  result = kIOReturnUnsupported;
-  IOService *provider = getProvider();
-  
-  if (provider != 0) {
+  IOService *provider;
+
+  if (gIOPlatformFunctionHandlerSet == functionName)
+  {
+#if defined(__i386__)
+    const OSSymbol * functionHandlerName = (const OSSymbol *) param1;
+    IOService *             target              = (IOService *) param2;
+    bool            enable              = (param3 != 0);
+
+    if (sCPULatencyFunctionName[kCpuDelayBusStall] == functionHandlerName)
+       result = setLatencyHandler(kCpuDelayBusStall, target, enable);
+    else if (sCPULatencyFunctionName[kCpuDelayInterrupt] == param1)
+       result = setLatencyHandler(kCpuDelayInterrupt, target, enable);
+#endif /* defined(__i386__) */
+  }
+
+  if ((kIOReturnUnsupported == result) && (provider = getProvider())) {
     result = provider->callPlatformFunction(functionName, waitForFunction,
                                            param1, param2, param3, param4);
   }
@@ -1348,7 +1396,8 @@ void IOService::applyToInterested( const OSSymbol * typeOfInterest,
                                    OSObjectApplierFunction applier,
                                    void * context )
 {
-    applyToClients( (IOServiceApplierFunction) applier, context );
+    if (gIOGeneralInterest == typeOfInterest)
+       applyToClients( (IOServiceApplierFunction) applier, context );
     applyToInterestNotifiers(this, typeOfInterest, applier, context);
 }
 
@@ -2358,6 +2407,10 @@ void IOService::probeCandidates( OSOrderedSet * matches )
     OSObject           *       nextMatch = 0;
     bool                       started;
     bool                       needReloc = false;
+#if CONFIG_MACF_KEXT
+    OSBoolean          *       isSandbox = 0;
+    bool                       useSandbox = false;
+#endif
 #if IOMATCHDEBUG
     SInt64                     debugFlags;
 #endif
@@ -2466,6 +2519,8 @@ void IOService::probeCandidates( OSOrderedSet * matches )
                 if( !symbol)
                     continue;
     
+                //IOLog("%s alloc (symbol %p props %p)\n", symbol->getCStringNoCopy(), symbol, props);
+
                 // alloc the driver instance
                 inst = (IOService *) OSMetaClass::allocClassWithName( symbol);
     
@@ -2492,7 +2547,10 @@ void IOService::probeCandidates( OSOrderedSet * matches )
                 if( 0 == category)
                     category = gIODefaultMatchCategoryKey;
                 inst->setProperty( gIOMatchCategoryKey, (OSObject *) category );
-    
+#if CONFIG_MACF_KEXT
+               isSandbox = OSDynamicCast(OSBoolean,
+                            props->getObject("IOKitForceMatch"));
+#endif
                 // attach driver instance
                 if( !(inst->attach( this )))
                         continue;
@@ -2509,6 +2567,21 @@ void IOService::probeCandidates( OSOrderedSet * matches )
     
                 newInst = inst->probe( this, &score );
                 inst->detach( this );
+#if CONFIG_MACF_KEXT
+               /*
+                * If this is the Sandbox driver and it matched, this is a
+                * disallowed device; toss any drivers that were already
+                * matched.
+                */
+               if (isSandbox && isSandbox->isTrue() && newInst != 0) {
+                   if (startDict != 0) {
+                       startDict->flushCollection();
+                       startDict->release();
+                       startDict = 0;
+                   }
+                   useSandbox = true;
+               }
+#endif
                 if( 0 == newInst) {
 #if IOMATCHDEBUG
                     if( debugFlags & kIOLogProbe)
@@ -2547,6 +2620,13 @@ void IOService::probeCandidates( OSOrderedSet * matches )
             props->release();
             if( inst)
                 inst->release();
+#if CONFIG_MACF_KEXT
+           /*
+            * If we're forcing the sandbox, drop out of the loop.
+            */
+           if (isSandbox && isSandbox->isTrue() && useSandbox)
+                   break;
+#endif
         }
         familyMatches->release();
         familyMatches = 0;
@@ -2929,8 +3009,10 @@ UInt32 IOService::_adjustBusy( SInt32 delta )
            applyToInterestNotifiers( next, gIOBusyInterest, 
                                     &messageClientsApplier, &context );
 
+#if !NO_KEXTD
             if( nowQuiet && (next == gIOServiceRoot))
                 OSMetaClass::considerUnloads();
+#endif
         }
 
         delta = nowQuiet ? -1 : +1;
@@ -3015,9 +3097,18 @@ bool IOService::serializeProperties( OSSerialize * s ) const
 
 void _IOConfigThread::main( _IOConfigThread * self )
 {
-    _IOServiceJob *    job;
-    IOService  *       nub;
-    bool               alive = true;
+    _IOServiceJob * job;
+    IOService  *   nub;
+    bool           alive = true;
+    kern_return_t   kr;
+    thread_precedence_policy_data_t precedence = { -1 };
+
+    kr = thread_policy_set(current_thread(), 
+                           THREAD_PRECEDENCE_POLICY, 
+                           (thread_policy_t) &precedence, 
+                           THREAD_PRECEDENCE_POLICY_COUNT);
+    if (KERN_SUCCESS != kr)
+       IOLog("thread_policy_set(%d)\n", kr);
 
     do {
 
@@ -3345,7 +3436,7 @@ IONotifier * IOService::addNotification(
                        void * target, void * ref,
                        SInt32 priority )
 {
-    OSIterator *               existing;
+    OSIterator *               existing = NULL;
     _IOServiceNotifier *       notify;
     IOService *                        next;
 
@@ -3573,6 +3664,26 @@ OSDictionary * IOService::resourceMatching( const char * name,
     return( table );
 }
 
+OSDictionary * IOService::propertyMatching( const OSSymbol * key, const OSObject * value,
+                       OSDictionary * table )
+{
+    OSDictionary * properties;
+
+    properties = OSDictionary::withCapacity( 2 );
+    if( !properties)
+       return( 0 );
+    properties->setObject( key, value );
+
+    if( !table)
+       table = OSDictionary::withCapacity( 2 );
+    if( table)
+        table->setObject( gIOPropertyMatchKey, properties );
+
+    properties->release();
+
+    return( table );
+}
+
 /*
  * _IOServiceNotifier
  */
@@ -4000,6 +4111,12 @@ bool IOService::passiveMatch( OSDictionary * table, bool changesOK )
 
             if( !(match = where->compareProperty( table, kIOBSDNameKey )))
                 break;
+            if( !(match = where->compareProperty( table, kIOBSDMajorKey )))
+                break;
+            if( !(match = where->compareProperty( table, kIOBSDMinorKey )))
+                break;
+            if( !(match = where->compareProperty( table, kIOBSDUnitKey )))
+                break;
 
             matchParent = false;
 
@@ -4340,82 +4457,182 @@ void IOService::setDeviceMemory( OSArray * array )
 void IOService::
 setCPUSnoopDelay(UInt32 __unused ns)
 {
-#if __i386__
+#if defined(__i386__)
     ml_set_maxsnoop(ns); 
-#endif /* __i386__ */
+#endif /* defined(__i386__) */
 }
 
 UInt32 IOService::
 getCPUSnoopDelay()
 {
-#if __i386__
+#if defined(__i386__)
     return ml_get_maxsnoop(); 
 #else
     return 0;
-#endif /* __i386__ */
+#endif /* defined(__i386__) */
 }
 
-void IOService::
-requireMaxBusStall(UInt32 __unused ns)
+#if defined(__i386__)
+static void
+requireMaxCpuDelay(IOService * service, UInt32 ns, UInt32 delayType)
 {
-#if __i386__
     static const UInt kNoReplace = -1U;        // Must be an illegal index
     UInt replace = kNoReplace;
+    bool setCpuDelay = false;
 
-    IOLockLock(sBusStallLock);
+    IORecursiveLockLock(sCpuDelayLock);
 
-    UInt count = sBusStall->getLength() / sizeof(BusStallEntry);
-    BusStallEntry *entries = (BusStallEntry *) sBusStall->getBytesNoCopy();
+    UInt count = sCpuDelayData->getLength() / sizeof(CpuDelayEntry);
+    CpuDelayEntry *entries = (CpuDelayEntry *) sCpuDelayData->getBytesNoCopy();
+    IOService * holder = NULL;
 
     if (ns) {
-       const BusStallEntry ne = {this, ns};
-
-       // Set Maximum bus delay.
-       for (UInt i = 0; i < count; i++) {
-           const IOService *thisService = entries[i].fService;
-           if (this == thisService)
-               replace = i;
-           else if (!thisService) {
-               if (kNoReplace == replace)
-                   replace = i;
-           }
-           else {
-               const UInt32 thisMax = entries[i].fMaxDelay;
-               if (thisMax < ns)
-                   ns = thisMax;
-           }
-       }
-
-       // Must be safe to call from locked context
-       ml_set_maxbusdelay(ns);
-
-       if (kNoReplace == replace)
-           sBusStall->appendBytes(&ne, sizeof(ne));
-       else
-           entries[replace] = ne;
+        const CpuDelayEntry ne = {service, ns, delayType};
+       holder = service;
+        // Set maximum delay.
+        for (UInt i = 0; i < count; i++) {
+            IOService *thisService = entries[i].fService;
+            bool sameType = (delayType == entries[i].fDelayType);            
+            if ((service == thisService) && sameType)
+                replace = i;
+            else if (!thisService) {
+                if (kNoReplace == replace)
+                    replace = i;
+            }
+            else if (sameType) {
+                const UInt32 thisMax = entries[i].fMaxDelay;
+                if (thisMax < ns)
+               {
+                    ns = thisMax;
+                   holder = thisService;
+               }
+            }
+        }
+        
+        setCpuDelay = true;
+        if (kNoReplace == replace)
+            sCpuDelayData->appendBytes(&ne, sizeof(ne));
+        else
+            entries[replace] = ne;
     }
     else {
-       ns = -1U;       // Set to max unsigned, i.e. no restriction
-
-       for (UInt i = 0; i < count; i++) {
-           // Clear a maximum bus delay.
-           const IOService *thisService = entries[i].fService;
-           UInt32 thisMax = entries[i].fMaxDelay;
-           if (this == thisService)
-               replace = i;
-           else if (thisService && thisMax < ns)
-               ns = thisMax;
+        ns = -1U;      // Set to max unsigned, i.e. no restriction
+
+        for (UInt i = 0; i < count; i++) {
+            // Clear a maximum delay.
+            IOService *thisService = entries[i].fService;
+            if (thisService && (delayType == entries[i].fDelayType)) {
+                UInt32 thisMax = entries[i].fMaxDelay;
+                if (service == thisService)
+                    replace = i;
+                else if (thisMax < ns) {
+                    ns = thisMax;
+                   holder = thisService;
+               }
+            }
+        }
+
+        // Check if entry found
+        if (kNoReplace != replace) {
+            entries[replace].fService = 0;     // Null the entry
+            setCpuDelay = true;
+        }
+    }
+
+    if (setCpuDelay)
+    {
+        // Must be safe to call from locked context
+        if (delayType == kCpuDelayBusStall)
+        {
+            ml_set_maxbusdelay(ns);
+        }
+        else if (delayType == kCpuDelayInterrupt)
+        {
+            ml_set_maxintdelay(ns);
+        }
+
+       OSArray * handlers = sCpuLatencyHandlers[delayType];
+       IOService * target;
+       if (handlers) for (unsigned int idx = 0; 
+                           (target = (IOService *) handlers->getObject(idx));
+                           idx++)
+       {
+           target->callPlatformFunction(sCPULatencyFunctionName[delayType], false,
+                                           (void *) (uintptr_t) ns, holder,
+                                           NULL, NULL);
        }
+    }
 
-       // Check if entry found
-       if (kNoReplace != replace) {
-           entries[replace].fService = 0;      // Null the entry
-           ml_set_maxbusdelay(ns);
+    IORecursiveLockUnlock(sCpuDelayLock);
+}
+
+static IOReturn
+setLatencyHandler(UInt32 delayType, IOService * target, bool enable)
+{
+    IOReturn result = kIOReturnNotFound;
+    OSArray * array;
+    unsigned int idx;
+
+    IORecursiveLockLock(sCpuDelayLock);
+
+    do
+    {
+       if (enable && !sCpuLatencyHandlers[delayType])
+           sCpuLatencyHandlers[delayType] = OSArray::withCapacity(4);
+       array = sCpuLatencyHandlers[delayType];
+       if (!array)
+           break;
+       idx = array->getNextIndexOfObject(target, 0);
+       if (!enable)
+       {
+           if (-1U != idx)
+           {
+               array->removeObject(idx);
+               result = kIOReturnSuccess;
+           }
+       }
+       else
+       {
+           if (-1U != idx) {
+               result = kIOReturnExclusiveAccess;
+               break;
+           }
+           array->setObject(target);
+           
+           UInt count = sCpuDelayData->getLength() / sizeof(CpuDelayEntry);
+           CpuDelayEntry *entries = (CpuDelayEntry *) sCpuDelayData->getBytesNoCopy();
+           UInt32 ns = -1U;    // Set to max unsigned, i.e. no restriction
+           IOService * holder = NULL;
+
+           for (UInt i = 0; i < count; i++) {
+               if (entries[i].fService 
+                 && (delayType == entries[i].fDelayType) 
+                 && (entries[i].fMaxDelay < ns)) {
+                   ns = entries[i].fMaxDelay;
+                   holder = entries[i].fService;
+               }
+           }
+           target->callPlatformFunction(sCPULatencyFunctionName[delayType], false,
+                                           (void *) (uintptr_t) ns, holder,
+                                           NULL, NULL);
+           result = kIOReturnSuccess;
        }
     }
+    while (false);
+
+    IORecursiveLockUnlock(sCpuDelayLock);
+
+    return (result);
+}
 
-    IOLockUnlock(sBusStallLock);
-#endif /* __i386__ */
+#endif /* defined(__i386__) */
+
+void IOService::
+requireMaxBusStall(UInt32 __unused ns)
+{
+#if defined(__i386__)
+    requireMaxCpuDelay(this, ns, kCpuDelayBusStall);
+#endif
 }
 
 /*
@@ -4578,8 +4795,8 @@ OSMetaClassDefineReservedUsed(IOService, 0);
 OSMetaClassDefineReservedUsed(IOService, 1);
 OSMetaClassDefineReservedUsed(IOService, 2);
 OSMetaClassDefineReservedUsed(IOService, 3);
+OSMetaClassDefineReservedUsed(IOService, 4);
 
-OSMetaClassDefineReservedUnused(IOService, 4);
 OSMetaClassDefineReservedUnused(IOService, 5);
 OSMetaClassDefineReservedUnused(IOService, 6);
 OSMetaClassDefineReservedUnused(IOService, 7);