2 * Copyright (c) 1998-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <libkern/c++/OSKext.h>
31 #include <IOKit/IOKitServer.h>
32 #include <IOKit/IOKitKeysPrivate.h>
33 #include <IOKit/IOUserClient.h>
34 #include <IOKit/IOService.h>
35 #include <IOKit/IORegistryEntry.h>
36 #include <IOKit/IOCatalogue.h>
37 #include <IOKit/IOMemoryDescriptor.h>
38 #include <IOKit/IOBufferMemoryDescriptor.h>
39 #include <IOKit/IOLib.h>
40 #include <IOKit/IOStatisticsPrivate.h>
41 #include <IOKit/IOTimeStamp.h>
42 #include <IOKit/system.h>
43 #include <libkern/OSDebug.h>
45 #include <sys/kauth.h>
46 #include <sys/codesign.h>
53 #include <security/mac_framework.h>
55 #include <sys/kauth.h>
59 #endif /* CONFIG_MACF */
61 #include <IOKit/assert.h>
63 #include "IOServicePrivate.h"
64 #include "IOKitKernelInternal.h"
66 #define SCALAR64(x) ((io_user_scalar_t)((unsigned int)x))
67 #define SCALAR32(x) ((uint32_t )x)
68 #define ARG32(x) ((void *)(uintptr_t)SCALAR32(x))
69 #define REF64(x) ((io_user_reference_t)((UInt64)(x)))
70 #define REF32(x) ((int)(x))
74 kIOUCAsync0Flags
= 3ULL,
75 kIOUCAsync64Flag
= 1ULL,
76 kIOUCAsyncErrorLoggedFlag
= 2ULL
81 #define IOStatisticsRegisterCounter() \
83 reserved->counter = IOStatistics::registerUserClient(this); \
86 #define IOStatisticsUnregisterCounter() \
89 IOStatistics::unregisterUserClient(reserved->counter); \
92 #define IOStatisticsClientCall() \
94 IOStatistics::countUserClientCall(client); \
99 #define IOStatisticsRegisterCounter()
100 #define IOStatisticsUnregisterCounter()
101 #define IOStatisticsClientCall()
103 #endif /* IOKITSTATS */
105 #if DEVELOPMENT || DEBUG
107 #define FAKE_STACK_FRAME(a) \
108 const void ** __frameptr; \
109 const void * __retaddr; \
110 __frameptr = (typeof(__frameptr)) __builtin_frame_address(0); \
111 __retaddr = __frameptr[1]; \
114 #define FAKE_STACK_FRAME_END() \
115 __frameptr[1] = __retaddr;
117 #else /* DEVELOPMENT || DEBUG */
119 #define FAKE_STACK_FRAME(a)
120 #define FAKE_STACK_FRAME_END()
122 #endif /* DEVELOPMENT || DEBUG */
124 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
126 // definitions we should get from osfmk
128 //typedef struct ipc_port * ipc_port_t;
129 typedef natural_t ipc_kobject_type_t
;
131 #define IKOT_IOKIT_SPARE 27
132 #define IKOT_IOKIT_CONNECT 29
133 #define IKOT_IOKIT_OBJECT 30
137 extern ipc_port_t
iokit_alloc_object_port( io_object_t obj
,
138 ipc_kobject_type_t type
);
140 extern kern_return_t
iokit_destroy_object_port( ipc_port_t port
);
142 extern mach_port_name_t
iokit_make_send_right( task_t task
,
143 io_object_t obj
, ipc_kobject_type_t type
);
145 extern kern_return_t
iokit_mod_send_right( task_t task
, mach_port_name_t name
, mach_port_delta_t delta
);
147 extern io_object_t
iokit_lookup_connect_ref(io_object_t clientRef
, ipc_space_t task
);
149 extern io_object_t
iokit_lookup_connect_ref_current_task(io_object_t clientRef
);
151 extern ipc_port_t master_device_port
;
153 extern void iokit_retain_port( ipc_port_t port
);
154 extern void iokit_release_port( ipc_port_t port
);
155 extern void iokit_release_port_send( ipc_port_t port
);
157 extern kern_return_t
iokit_switch_object_port( ipc_port_t port
, io_object_t obj
, ipc_kobject_type_t type
);
159 #include <mach/mach_traps.h>
160 #include <vm/vm_map.h>
165 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
167 // IOMachPort maps OSObjects to ports, avoiding adding an ivar to OSObject.
169 class IOMachPort
: public OSObject
171 OSDeclareDefaultStructors(IOMachPort
)
178 static IOMachPort
* portForObject( OSObject
* obj
,
179 ipc_kobject_type_t type
);
180 static bool noMoreSendersForObject( OSObject
* obj
,
181 ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
);
182 static void releasePortForObject( OSObject
* obj
,
183 ipc_kobject_type_t type
);
184 static void setHoldDestroy( OSObject
* obj
, ipc_kobject_type_t type
);
186 static OSDictionary
* dictForType( ipc_kobject_type_t type
);
188 static mach_port_name_t
makeSendRightForTask( task_t task
,
189 io_object_t obj
, ipc_kobject_type_t type
);
191 virtual void free() APPLE_KEXT_OVERRIDE
;
194 #define super OSObject
195 OSDefineMetaClassAndStructors(IOMachPort
, OSObject
)
197 static IOLock
* gIOObjectPortLock
;
199 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
201 // not in dictForType() for debugging ease
202 static OSDictionary
* gIOObjectPorts
;
203 static OSDictionary
* gIOConnectPorts
;
205 OSDictionary
* IOMachPort::dictForType( ipc_kobject_type_t type
)
207 OSDictionary
** dict
;
209 if( IKOT_IOKIT_OBJECT
== type
)
210 dict
= &gIOObjectPorts
;
211 else if( IKOT_IOKIT_CONNECT
== type
)
212 dict
= &gIOConnectPorts
;
217 *dict
= OSDictionary::withCapacity( 1 );
222 IOMachPort
* IOMachPort::portForObject ( OSObject
* obj
,
223 ipc_kobject_type_t type
)
225 IOMachPort
* inst
= 0;
228 IOTakeLock( gIOObjectPortLock
);
232 dict
= dictForType( type
);
236 if( (inst
= (IOMachPort
*)
237 dict
->getObject( (const OSSymbol
*) obj
))) {
243 inst
= new IOMachPort
;
244 if( inst
&& !inst
->init()) {
249 inst
->port
= iokit_alloc_object_port( obj
, type
);
252 dict
->setObject( (const OSSymbol
*) obj
, inst
);
262 IOUnlock( gIOObjectPortLock
);
267 bool IOMachPort::noMoreSendersForObject( OSObject
* obj
,
268 ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
)
271 IOMachPort
* machPort
;
273 bool destroyed
= true;
275 IOTakeLock( gIOObjectPortLock
);
277 if( (dict
= dictForType( type
))) {
280 machPort
= (IOMachPort
*) dict
->getObject( (const OSSymbol
*) obj
);
282 destroyed
= (machPort
->mscount
<= *mscount
);
283 if (!destroyed
) *mscount
= machPort
->mscount
;
286 if ((IKOT_IOKIT_CONNECT
== type
) && (uc
= OSDynamicCast(IOUserClient
, obj
)))
290 dict
->removeObject( (const OSSymbol
*) obj
);
296 IOUnlock( gIOObjectPortLock
);
301 void IOMachPort::releasePortForObject( OSObject
* obj
,
302 ipc_kobject_type_t type
)
305 IOMachPort
* machPort
;
307 assert(IKOT_IOKIT_CONNECT
!= type
);
309 IOTakeLock( gIOObjectPortLock
);
311 if( (dict
= dictForType( type
))) {
313 machPort
= (IOMachPort
*) dict
->getObject( (const OSSymbol
*) obj
);
314 if( machPort
&& !machPort
->holdDestroy
)
315 dict
->removeObject( (const OSSymbol
*) obj
);
319 IOUnlock( gIOObjectPortLock
);
322 void IOMachPort::setHoldDestroy( OSObject
* obj
, ipc_kobject_type_t type
)
325 IOMachPort
* machPort
;
327 IOLockLock( gIOObjectPortLock
);
329 if( (dict
= dictForType( type
))) {
330 machPort
= (IOMachPort
*) dict
->getObject( (const OSSymbol
*) obj
);
332 machPort
->holdDestroy
= true;
335 IOLockUnlock( gIOObjectPortLock
);
338 void IOUserClient::destroyUserReferences( OSObject
* obj
)
340 IOMachPort::releasePortForObject( obj
, IKOT_IOKIT_OBJECT
);
343 // IOMachPort::releasePortForObject( obj, IKOT_IOKIT_CONNECT );
347 IOTakeLock( gIOObjectPortLock
);
350 if( (dict
= IOMachPort::dictForType( IKOT_IOKIT_CONNECT
)))
353 port
= (IOMachPort
*) dict
->getObject( (const OSSymbol
*) obj
);
357 if ((uc
= OSDynamicCast(IOUserClient
, obj
)))
362 dict
->setObject((const OSSymbol
*) uc
->mappings
, port
);
363 iokit_switch_object_port(port
->port
, uc
->mappings
, IKOT_IOKIT_CONNECT
);
365 uc
->mappings
->release();
369 dict
->removeObject( (const OSSymbol
*) obj
);
373 IOUnlock( gIOObjectPortLock
);
376 mach_port_name_t
IOMachPort::makeSendRightForTask( task_t task
,
377 io_object_t obj
, ipc_kobject_type_t type
)
379 return( iokit_make_send_right( task
, obj
, type
));
382 void IOMachPort::free( void )
385 iokit_destroy_object_port( port
);
389 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
391 class IOUserIterator
: public OSIterator
393 OSDeclareDefaultStructors(IOUserIterator
)
395 OSObject
* userIteratorObject
;
398 static IOUserIterator
* withIterator(OSIterator
* iter
);
399 virtual bool init( void ) APPLE_KEXT_OVERRIDE
;
400 virtual void free() APPLE_KEXT_OVERRIDE
;
402 virtual void reset() APPLE_KEXT_OVERRIDE
;
403 virtual bool isValid() APPLE_KEXT_OVERRIDE
;
404 virtual OSObject
* getNextObject() APPLE_KEXT_OVERRIDE
;
407 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
409 class IOUserNotification
: public IOUserIterator
411 OSDeclareDefaultStructors(IOUserNotification
)
413 #define holdNotify userIteratorObject
417 virtual void free() APPLE_KEXT_OVERRIDE
;
419 virtual void setNotification( IONotifier
* obj
);
421 virtual void reset() APPLE_KEXT_OVERRIDE
;
422 virtual bool isValid() APPLE_KEXT_OVERRIDE
;
425 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
427 OSDefineMetaClassAndStructors( IOUserIterator
, OSIterator
)
430 IOUserIterator::withIterator(OSIterator
* iter
)
434 if (!iter
) return (0);
436 me
= new IOUserIterator
;
437 if (me
&& !me
->init())
443 me
->userIteratorObject
= iter
;
449 IOUserIterator::init( void )
451 if (!OSObject::init()) return (false);
453 lock
= IOLockAlloc();
461 IOUserIterator::free()
463 if (userIteratorObject
) userIteratorObject
->release();
464 if (lock
) IOLockFree(lock
);
469 IOUserIterator::reset()
472 assert(OSDynamicCast(OSIterator
, userIteratorObject
));
473 ((OSIterator
*)userIteratorObject
)->reset();
478 IOUserIterator::isValid()
483 assert(OSDynamicCast(OSIterator
, userIteratorObject
));
484 ret
= ((OSIterator
*)userIteratorObject
)->isValid();
491 IOUserIterator::getNextObject()
496 assert(OSDynamicCast(OSIterator
, userIteratorObject
));
497 ret
= ((OSIterator
*)userIteratorObject
)->getNextObject();
503 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
506 // functions called from osfmk/device/iokit_rpc.c
509 iokit_add_reference( io_object_t obj
)
516 iokit_remove_reference( io_object_t obj
)
523 iokit_add_connect_reference( io_object_t obj
)
529 if ((uc
= OSDynamicCast(IOUserClient
, obj
))) OSIncrementAtomic(&uc
->__ipc
);
535 iokit_remove_connect_reference( io_object_t obj
)
538 bool finalize
= false;
542 if ((uc
= OSDynamicCast(IOUserClient
, obj
)))
544 if (1 == OSDecrementAtomic(&uc
->__ipc
) && uc
->isInactive())
546 IOLockLock(gIOObjectPortLock
);
547 if ((finalize
= uc
->__ipcFinal
)) uc
->__ipcFinal
= false;
548 IOLockUnlock(gIOObjectPortLock
);
550 if (finalize
) uc
->scheduleFinalize(true);
557 IOUserClient::finalizeUserReferences(OSObject
* obj
)
562 if ((uc
= OSDynamicCast(IOUserClient
, obj
)))
564 IOLockLock(gIOObjectPortLock
);
565 if ((uc
->__ipcFinal
= (0 != uc
->__ipc
))) ok
= false;
566 IOLockUnlock(gIOObjectPortLock
);
572 iokit_port_for_object( io_object_t obj
, ipc_kobject_type_t type
)
574 IOMachPort
* machPort
;
577 if( (machPort
= IOMachPort::portForObject( obj
, type
))) {
579 port
= machPort
->port
;
581 iokit_retain_port( port
);
592 iokit_client_died( io_object_t obj
, ipc_port_t
/* port */,
593 ipc_kobject_type_t type
, mach_port_mscount_t
* mscount
)
595 IOUserClient
* client
;
597 IOUserNotification
* notify
;
599 if( !IOMachPort::noMoreSendersForObject( obj
, type
, mscount
))
600 return( kIOReturnNotReady
);
602 if( IKOT_IOKIT_CONNECT
== type
)
604 if( (client
= OSDynamicCast( IOUserClient
, obj
)))
606 IOStatisticsClientCall();
607 client
->clientDied();
610 else if( IKOT_IOKIT_OBJECT
== type
)
612 if( (map
= OSDynamicCast( IOMemoryMap
, obj
)))
614 else if( (notify
= OSDynamicCast( IOUserNotification
, obj
)))
615 notify
->setNotification( 0 );
618 return( kIOReturnSuccess
);
623 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
625 class IOServiceUserNotification
: public IOUserNotification
627 OSDeclareDefaultStructors(IOServiceUserNotification
)
630 mach_msg_header_t msgHdr
;
631 OSNotificationHeader64 notifyHeader
;
634 enum { kMaxOutstanding
= 1024 };
639 OSObject
* lastEntry
;
645 virtual bool init( mach_port_t port
, natural_t type
,
646 void * reference
, vm_size_t referenceSize
,
648 virtual void free() APPLE_KEXT_OVERRIDE
;
650 static bool _handler( void * target
,
651 void * ref
, IOService
* newService
, IONotifier
* notifier
);
652 virtual bool handler( void * ref
, IOService
* newService
);
654 virtual OSObject
* getNextObject() APPLE_KEXT_OVERRIDE
;
657 class IOServiceMessageUserNotification
: public IOUserNotification
659 OSDeclareDefaultStructors(IOServiceMessageUserNotification
)
662 mach_msg_header_t msgHdr
;
663 mach_msg_body_t msgBody
;
664 mach_msg_port_descriptor_t ports
[1];
665 OSNotificationHeader64 notifyHeader
__attribute__ ((packed
));
676 virtual bool init( mach_port_t port
, natural_t type
,
677 void * reference
, vm_size_t referenceSize
,
681 virtual void free() APPLE_KEXT_OVERRIDE
;
683 static IOReturn
_handler( void * target
, void * ref
,
684 UInt32 messageType
, IOService
* provider
,
685 void * messageArgument
, vm_size_t argSize
);
686 virtual IOReturn
handler( void * ref
,
687 UInt32 messageType
, IOService
* provider
,
688 void * messageArgument
, vm_size_t argSize
);
690 virtual OSObject
* getNextObject() APPLE_KEXT_OVERRIDE
;
693 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
696 #define super IOUserIterator
697 OSDefineMetaClass( IOUserNotification
, IOUserIterator
)
698 OSDefineAbstractStructors( IOUserNotification
, IOUserIterator
)
700 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
702 void IOUserNotification::free( void )
706 assert(OSDynamicCast(IONotifier
, holdNotify
));
707 ((IONotifier
*)holdNotify
)->remove();
710 // can't be in handler now
716 void IOUserNotification::setNotification( IONotifier
* notify
)
718 OSObject
* previousNotify
;
720 IOLockLock( gIOObjectPortLock
);
722 previousNotify
= holdNotify
;
725 IOLockUnlock( gIOObjectPortLock
);
729 assert(OSDynamicCast(IONotifier
, previousNotify
));
730 ((IONotifier
*)previousNotify
)->remove();
734 void IOUserNotification::reset()
739 bool IOUserNotification::isValid()
744 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
747 #define super IOUserNotification
748 OSDefineMetaClassAndStructors(IOServiceUserNotification
, IOUserNotification
)
750 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
752 bool IOServiceUserNotification::init( mach_port_t port
, natural_t type
,
753 void * reference
, vm_size_t referenceSize
,
759 newSet
= OSArray::withCapacity( 1 );
763 if (referenceSize
> sizeof(OSAsyncReference64
))
766 msgSize
= sizeof(PingMsg
) - sizeof(OSAsyncReference64
) + referenceSize
;
767 pingMsg
= (PingMsg
*) IOMalloc( msgSize
);
771 bzero( pingMsg
, msgSize
);
773 pingMsg
->msgHdr
.msgh_remote_port
= port
;
774 pingMsg
->msgHdr
.msgh_bits
= MACH_MSGH_BITS(
775 MACH_MSG_TYPE_COPY_SEND
/*remote*/,
776 MACH_MSG_TYPE_MAKE_SEND
/*local*/);
777 pingMsg
->msgHdr
.msgh_size
= msgSize
;
778 pingMsg
->msgHdr
.msgh_id
= kOSNotificationMessageID
;
780 pingMsg
->notifyHeader
.size
= 0;
781 pingMsg
->notifyHeader
.type
= type
;
782 bcopy( reference
, pingMsg
->notifyHeader
.reference
, referenceSize
);
787 void IOServiceUserNotification::free( void )
792 OSObject
* _lastEntry
;
796 _lastEntry
= lastEntry
;
801 if( _pingMsg
&& _msgSize
) {
802 if (_pingMsg
->msgHdr
.msgh_remote_port
) {
803 iokit_release_port_send(_pingMsg
->msgHdr
.msgh_remote_port
);
805 IOFree(_pingMsg
, _msgSize
);
809 _lastEntry
->release();
815 bool IOServiceUserNotification::_handler( void * target
,
816 void * ref
, IOService
* newService
, IONotifier
* notifier
)
818 return( ((IOServiceUserNotification
*) target
)->handler( ref
, newService
));
821 bool IOServiceUserNotification::handler( void * ref
,
822 IOService
* newService
)
826 ipc_port_t port
= NULL
;
827 bool sendPing
= false;
831 count
= newSet
->getCount();
832 if( count
< kMaxOutstanding
) {
834 newSet
->setObject( newService
);
835 if( (sendPing
= (armed
&& (0 == count
))))
841 if( kIOServiceTerminatedNotificationType
== pingMsg
->notifyHeader
.type
)
842 IOMachPort::setHoldDestroy( newService
, IKOT_IOKIT_OBJECT
);
845 if( (port
= iokit_port_for_object( this, IKOT_IOKIT_OBJECT
) ))
846 pingMsg
->msgHdr
.msgh_local_port
= port
;
848 pingMsg
->msgHdr
.msgh_local_port
= NULL
;
850 kr
= mach_msg_send_from_kernel_with_options( &pingMsg
->msgHdr
,
851 pingMsg
->msgHdr
.msgh_size
,
852 (MACH_SEND_MSG
| MACH_SEND_ALWAYS
| MACH_SEND_IMPORTANCE
),
855 iokit_release_port( port
);
857 if( (KERN_SUCCESS
!= kr
) && !ipcLogged
)
860 IOLog("%s: mach_msg_send_from_kernel_proper(0x%x)\n", __PRETTY_FUNCTION__
, kr
);
867 OSObject
* IOServiceUserNotification::getNextObject()
871 OSObject
* releaseEntry
;
875 releaseEntry
= lastEntry
;
876 count
= newSet
->getCount();
878 result
= newSet
->getObject( count
- 1 );
880 newSet
->removeObject( count
- 1);
889 if (releaseEntry
) releaseEntry
->release();
894 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
896 OSDefineMetaClassAndStructors(IOServiceMessageUserNotification
, IOUserNotification
)
898 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
900 bool IOServiceMessageUserNotification::init( mach_port_t port
, natural_t type
,
901 void * reference
, vm_size_t referenceSize
, vm_size_t extraSize
,
907 if (referenceSize
> sizeof(OSAsyncReference64
))
910 clientIs64
= client64
;
912 owningPID
= proc_selfpid();
914 extraSize
+= sizeof(IOServiceInterestContent64
);
915 msgSize
= sizeof(PingMsg
) - sizeof(OSAsyncReference64
) + referenceSize
;
916 pingMsg
= (PingMsg
*) IOMalloc( msgSize
);
920 bzero( pingMsg
, msgSize
);
922 pingMsg
->msgHdr
.msgh_remote_port
= port
;
923 pingMsg
->msgHdr
.msgh_bits
= MACH_MSGH_BITS_COMPLEX
925 MACH_MSG_TYPE_COPY_SEND
/*remote*/,
926 MACH_MSG_TYPE_MAKE_SEND
/*local*/);
927 pingMsg
->msgHdr
.msgh_size
= msgSize
;
928 pingMsg
->msgHdr
.msgh_id
= kOSNotificationMessageID
;
930 pingMsg
->msgBody
.msgh_descriptor_count
= 1;
932 pingMsg
->ports
[0].name
= 0;
933 pingMsg
->ports
[0].disposition
= MACH_MSG_TYPE_MAKE_SEND
;
934 pingMsg
->ports
[0].type
= MACH_MSG_PORT_DESCRIPTOR
;
936 pingMsg
->notifyHeader
.size
= extraSize
;
937 pingMsg
->notifyHeader
.type
= type
;
938 bcopy( reference
, pingMsg
->notifyHeader
.reference
, referenceSize
);
943 void IOServiceMessageUserNotification::free( void )
953 if( _pingMsg
&& _msgSize
) {
954 if (_pingMsg
->msgHdr
.msgh_remote_port
) {
955 iokit_release_port_send(_pingMsg
->msgHdr
.msgh_remote_port
);
957 IOFree( _pingMsg
, _msgSize
);
961 IOReturn
IOServiceMessageUserNotification::_handler( void * target
, void * ref
,
962 UInt32 messageType
, IOService
* provider
,
963 void * argument
, vm_size_t argSize
)
965 return( ((IOServiceMessageUserNotification
*) target
)->handler(
966 ref
, messageType
, provider
, argument
, argSize
));
969 IOReturn
IOServiceMessageUserNotification::handler( void * ref
,
970 UInt32 messageType
, IOService
* provider
,
971 void * messageArgument
, vm_size_t callerArgSize
)
973 enum { kLocalMsgSize
= 0x100 };
974 uint64_t stackMsg
[kLocalMsgSize
/ sizeof(uint64_t)];
978 vm_size_t thisMsgSize
;
979 ipc_port_t thisPort
, providerPort
;
980 struct PingMsg
* thisMsg
;
981 IOServiceInterestContent64
* data
;
983 if (kIOMessageCopyClientID
== messageType
)
985 *((void **) messageArgument
) = OSNumber::withNumber(owningPID
, 32);
986 return (kIOReturnSuccess
);
989 if (callerArgSize
== 0)
991 if (clientIs64
) argSize
= sizeof(data
->messageArgument
[0]);
992 else argSize
= sizeof(uint32_t);
996 argSize
= callerArgSize
;
997 if( argSize
> kIOUserNotifyMaxMessageSize
)
998 argSize
= kIOUserNotifyMaxMessageSize
;
1001 // adjust message size for ipc restrictions
1003 type
= pingMsg
->notifyHeader
.type
;
1004 type
&= ~(kIOKitNoticationMsgSizeMask
<< kIOKitNoticationTypeSizeAdjShift
);
1005 type
|= ((argSize
& kIOKitNoticationMsgSizeMask
) << kIOKitNoticationTypeSizeAdjShift
);
1006 argSize
= (argSize
+ kIOKitNoticationMsgSizeMask
) & ~kIOKitNoticationMsgSizeMask
;
1008 thisMsgSize
= msgSize
1009 + sizeof( IOServiceInterestContent64
)
1010 - sizeof( data
->messageArgument
)
1013 if (thisMsgSize
> sizeof(stackMsg
))
1015 allocMsg
= IOMalloc(thisMsgSize
);
1016 if (!allocMsg
) return (kIOReturnNoMemory
);
1017 thisMsg
= (typeof(thisMsg
)) allocMsg
;
1022 thisMsg
= (typeof(thisMsg
)) stackMsg
;
1025 bcopy(pingMsg
, thisMsg
, msgSize
);
1026 thisMsg
->notifyHeader
.type
= type
;
1027 data
= (IOServiceInterestContent64
*) (((uint8_t *) thisMsg
) + msgSize
);
1028 // == pingMsg->notifyHeader.content;
1029 data
->messageType
= messageType
;
1031 if (callerArgSize
== 0)
1033 data
->messageArgument
[0] = (io_user_reference_t
) messageArgument
;
1036 data
->messageArgument
[0] |= (data
->messageArgument
[0] << 32);
1041 bcopy( messageArgument
, data
->messageArgument
, callerArgSize
);
1042 bzero((void *)(((uintptr_t) &data
->messageArgument
[0]) + callerArgSize
), argSize
- callerArgSize
);
1045 thisMsg
->notifyHeader
.type
= type
;
1046 thisMsg
->msgHdr
.msgh_size
= thisMsgSize
;
1048 providerPort
= iokit_port_for_object( provider
, IKOT_IOKIT_OBJECT
);
1049 thisMsg
->ports
[0].name
= providerPort
;
1050 thisPort
= iokit_port_for_object( this, IKOT_IOKIT_OBJECT
);
1051 thisMsg
->msgHdr
.msgh_local_port
= thisPort
;
1053 kr
= mach_msg_send_from_kernel_with_options( &thisMsg
->msgHdr
,
1054 thisMsg
->msgHdr
.msgh_size
,
1055 (MACH_SEND_MSG
| MACH_SEND_ALWAYS
| MACH_SEND_IMPORTANCE
),
1058 iokit_release_port( thisPort
);
1060 iokit_release_port( providerPort
);
1063 IOFree(allocMsg
, thisMsgSize
);
1065 if((KERN_SUCCESS
!= kr
) && !ipcLogged
)
1068 IOLog("%s: mach_msg_send_from_kernel_proper (0x%x)\n", __PRETTY_FUNCTION__
, kr
);
1071 return( kIOReturnSuccess
);
1074 OSObject
* IOServiceMessageUserNotification::getNextObject()
1079 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1082 #define super IOService
1083 OSDefineMetaClassAndAbstractStructors( IOUserClient
, IOService
)
1085 IOLock
* gIOUserClientOwnersLock
;
1087 void IOUserClient::initialize( void )
1089 gIOObjectPortLock
= IOLockAlloc();
1090 gIOUserClientOwnersLock
= IOLockAlloc();
1091 assert(gIOObjectPortLock
&& gIOUserClientOwnersLock
);
1094 void IOUserClient::setAsyncReference(OSAsyncReference asyncRef
,
1095 mach_port_t wakePort
,
1096 void *callback
, void *refcon
)
1098 asyncRef
[kIOAsyncReservedIndex
] = ((uintptr_t) wakePort
)
1099 | (kIOUCAsync0Flags
& asyncRef
[kIOAsyncReservedIndex
]);
1100 asyncRef
[kIOAsyncCalloutFuncIndex
] = (uintptr_t) callback
;
1101 asyncRef
[kIOAsyncCalloutRefconIndex
] = (uintptr_t) refcon
;
1104 void IOUserClient::setAsyncReference64(OSAsyncReference64 asyncRef
,
1105 mach_port_t wakePort
,
1106 mach_vm_address_t callback
, io_user_reference_t refcon
)
1108 asyncRef
[kIOAsyncReservedIndex
] = ((io_user_reference_t
) wakePort
)
1109 | (kIOUCAsync0Flags
& asyncRef
[kIOAsyncReservedIndex
]);
1110 asyncRef
[kIOAsyncCalloutFuncIndex
] = (io_user_reference_t
) callback
;
1111 asyncRef
[kIOAsyncCalloutRefconIndex
] = refcon
;
1114 void IOUserClient::setAsyncReference64(OSAsyncReference64 asyncRef
,
1115 mach_port_t wakePort
,
1116 mach_vm_address_t callback
, io_user_reference_t refcon
, task_t task
)
1118 setAsyncReference64(asyncRef
, wakePort
, callback
, refcon
);
1119 if (vm_map_is_64bit(get_task_map(task
))) {
1120 asyncRef
[kIOAsyncReservedIndex
] |= kIOUCAsync64Flag
;
1124 static OSDictionary
* CopyConsoleUser(UInt32 uid
)
1127 OSDictionary
* user
= 0;
1129 if ((array
= OSDynamicCast(OSArray
,
1130 IORegistryEntry::getRegistryRoot()->copyProperty(gIOConsoleUsersKey
))))
1132 for (unsigned int idx
= 0;
1133 (user
= OSDynamicCast(OSDictionary
, array
->getObject(idx
)));
1137 if ((num
= OSDynamicCast(OSNumber
, user
->getObject(gIOConsoleSessionUIDKey
)))
1138 && (uid
== num
->unsigned32BitValue())) {
1148 static OSDictionary
* CopyUserOnConsole(void)
1151 OSDictionary
* user
= 0;
1153 if ((array
= OSDynamicCast(OSArray
,
1154 IORegistryEntry::getRegistryRoot()->copyProperty(gIOConsoleUsersKey
))))
1156 for (unsigned int idx
= 0;
1157 (user
= OSDynamicCast(OSDictionary
, array
->getObject(idx
)));
1160 if (kOSBooleanTrue
== user
->getObject(gIOConsoleSessionOnConsoleKey
))
1171 IOReturn
IOUserClient::clientHasAuthorization( task_t task
,
1172 IOService
* service
)
1176 p
= (proc_t
) get_bsdtask_info(task
);
1179 uint64_t authorizationID
;
1181 authorizationID
= proc_uniqueid(p
);
1182 if (authorizationID
)
1184 if (service
->getAuthorizationID() == authorizationID
)
1186 return (kIOReturnSuccess
);
1191 return (kIOReturnNotPermitted
);
1194 IOReturn
IOUserClient::clientHasPrivilege( void * securityToken
,
1195 const char * privilegeName
)
1198 security_token_t token
;
1199 mach_msg_type_number_t count
;
1201 OSDictionary
* user
;
1205 if (!strncmp(privilegeName
, kIOClientPrivilegeForeground
,
1206 sizeof(kIOClientPrivilegeForeground
)))
1208 if (task_is_gpu_denied(current_task()))
1209 return (kIOReturnNotPrivileged
);
1211 return (kIOReturnSuccess
);
1214 if (!strncmp(privilegeName
, kIOClientPrivilegeConsoleSession
,
1215 sizeof(kIOClientPrivilegeConsoleSession
)))
1220 task
= (task_t
) securityToken
;
1222 task
= current_task();
1223 p
= (proc_t
) get_bsdtask_info(task
);
1224 kr
= kIOReturnNotPrivileged
;
1226 if (p
&& (cred
= kauth_cred_proc_ref(p
)))
1228 user
= CopyUserOnConsole();
1232 if ((num
= OSDynamicCast(OSNumber
, user
->getObject(gIOConsoleSessionAuditIDKey
)))
1233 && (cred
->cr_audit
.as_aia_p
->ai_asid
== (au_asid_t
) num
->unsigned32BitValue()))
1235 kr
= kIOReturnSuccess
;
1239 kauth_cred_unref(&cred
);
1244 if ((secureConsole
= !strncmp(privilegeName
, kIOClientPrivilegeSecureConsoleProcess
,
1245 sizeof(kIOClientPrivilegeSecureConsoleProcess
))))
1246 task
= (task_t
)((IOUCProcessToken
*)securityToken
)->token
;
1248 task
= (task_t
)securityToken
;
1250 count
= TASK_SECURITY_TOKEN_COUNT
;
1251 kr
= task_info( task
, TASK_SECURITY_TOKEN
, (task_info_t
) &token
, &count
);
1253 if (KERN_SUCCESS
!= kr
)
1255 else if (!strncmp(privilegeName
, kIOClientPrivilegeAdministrator
,
1256 sizeof(kIOClientPrivilegeAdministrator
))) {
1257 if (0 != token
.val
[0])
1258 kr
= kIOReturnNotPrivileged
;
1259 } else if (!strncmp(privilegeName
, kIOClientPrivilegeLocalUser
,
1260 sizeof(kIOClientPrivilegeLocalUser
))) {
1261 user
= CopyConsoleUser(token
.val
[0]);
1265 kr
= kIOReturnNotPrivileged
;
1266 } else if (secureConsole
|| !strncmp(privilegeName
, kIOClientPrivilegeConsoleUser
,
1267 sizeof(kIOClientPrivilegeConsoleUser
))) {
1268 user
= CopyConsoleUser(token
.val
[0]);
1270 if (user
->getObject(gIOConsoleSessionOnConsoleKey
) != kOSBooleanTrue
)
1271 kr
= kIOReturnNotPrivileged
;
1272 else if ( secureConsole
) {
1273 OSNumber
* pid
= OSDynamicCast(OSNumber
, user
->getObject(gIOConsoleSessionSecureInputPIDKey
));
1274 if ( pid
&& pid
->unsigned32BitValue() != ((IOUCProcessToken
*)securityToken
)->pid
)
1275 kr
= kIOReturnNotPrivileged
;
1280 kr
= kIOReturnNotPrivileged
;
1282 kr
= kIOReturnUnsupported
;
1287 OSObject
* IOUserClient::copyClientEntitlement( task_t task
,
1288 const char * entitlement
)
1290 #define MAX_ENTITLEMENTS_LEN (128 * 1024)
1294 char procname
[MAXCOMLEN
+ 1] = "";
1296 void *entitlements_blob
= NULL
;
1297 char *entitlements_data
= NULL
;
1298 OSObject
*entitlements_obj
= NULL
;
1299 OSDictionary
*entitlements
= NULL
;
1300 OSString
*errorString
= NULL
;
1301 OSObject
*value
= NULL
;
1303 p
= (proc_t
)get_bsdtask_info(task
);
1307 proc_name(pid
, procname
, (int)sizeof(procname
));
1309 if (cs_entitlements_blob_get(p
, &entitlements_blob
, &len
) != 0)
1312 if (len
<= offsetof(CS_GenericBlob
, data
))
1316 * Per <rdar://problem/11593877>, enforce a limit on the amount of XML
1317 * we'll try to parse in the kernel.
1319 len
-= offsetof(CS_GenericBlob
, data
);
1320 if (len
> MAX_ENTITLEMENTS_LEN
) {
1321 IOLog("failed to parse entitlements for %s[%u]: %lu bytes of entitlements exceeds maximum of %u\n", procname
, pid
, len
, MAX_ENTITLEMENTS_LEN
);
1326 * OSUnserializeXML() expects a nul-terminated string, but that isn't
1327 * what is stored in the entitlements blob. Copy the string and
1330 entitlements_data
= (char *)IOMalloc(len
+ 1);
1331 if (entitlements_data
== NULL
)
1333 memcpy(entitlements_data
, ((CS_GenericBlob
*)entitlements_blob
)->data
, len
);
1334 entitlements_data
[len
] = '\0';
1336 entitlements_obj
= OSUnserializeXML(entitlements_data
, len
+ 1, &errorString
);
1337 if (errorString
!= NULL
) {
1338 IOLog("failed to parse entitlements for %s[%u]: %s\n", procname
, pid
, errorString
->getCStringNoCopy());
1341 if (entitlements_obj
== NULL
)
1344 entitlements
= OSDynamicCast(OSDictionary
, entitlements_obj
);
1345 if (entitlements
== NULL
)
1348 /* Fetch the entitlement value from the dictionary. */
1349 value
= entitlements
->getObject(entitlement
);
1354 if (entitlements_data
!= NULL
)
1355 IOFree(entitlements_data
, len
+ 1);
1356 if (entitlements_obj
!= NULL
)
1357 entitlements_obj
->release();
1358 if (errorString
!= NULL
)
1359 errorString
->release();
1363 bool IOUserClient::init()
1365 if (getPropertyTable() || super::init())
1371 bool IOUserClient::init(OSDictionary
* dictionary
)
1373 if (getPropertyTable() || super::init(dictionary
))
1379 bool IOUserClient::initWithTask(task_t owningTask
,
1383 if (getPropertyTable() || super::init())
1389 bool IOUserClient::initWithTask(task_t owningTask
,
1392 OSDictionary
* properties
)
1396 ok
= super::init( properties
);
1397 ok
&= initWithTask( owningTask
, securityID
, type
);
1402 bool IOUserClient::reserve()
1405 reserved
= IONew(ExpansionData
, 1);
1410 setTerminateDefer(NULL
, true);
1411 IOStatisticsRegisterCounter();
1416 struct IOUserClientOwner
1419 queue_chain_t taskLink
;
1421 queue_chain_t ucLink
;
1425 IOUserClient::registerOwner(task_t task
)
1427 IOUserClientOwner
* owner
;
1431 IOLockLock(gIOUserClientOwnersLock
);
1434 ret
= kIOReturnSuccess
;
1436 if (!owners
.next
) queue_init(&owners
);
1439 queue_iterate(&owners
, owner
, IOUserClientOwner
*, ucLink
)
1441 if (task
!= owner
->task
) continue;
1448 owner
= IONew(IOUserClientOwner
, 1);
1449 if (!newOwner
) ret
= kIOReturnNoMemory
;
1454 queue_enter_first(&owners
, owner
, IOUserClientOwner
*, ucLink
);
1455 queue_enter_first(task_io_user_clients(task
), owner
, IOUserClientOwner
*, taskLink
);
1459 IOLockUnlock(gIOUserClientOwnersLock
);
1465 IOUserClient::noMoreSenders(void)
1467 IOUserClientOwner
* owner
;
1469 IOLockLock(gIOUserClientOwnersLock
);
1473 while (!queue_empty(&owners
))
1475 owner
= (IOUserClientOwner
*)(void *) queue_first(&owners
);
1476 queue_remove(task_io_user_clients(owner
->task
), owner
, IOUserClientOwner
*, taskLink
);
1477 queue_remove(&owners
, owner
, IOUserClientOwner
*, ucLink
);
1478 IODelete(owner
, IOUserClientOwner
, 1);
1480 owners
.next
= owners
.prev
= NULL
;
1483 IOLockUnlock(gIOUserClientOwnersLock
);
1486 extern "C" kern_return_t
1487 iokit_task_terminate(task_t task
)
1489 IOUserClientOwner
* owner
;
1490 IOUserClient
* dead
;
1492 queue_head_t
* taskque
;
1494 IOLockLock(gIOUserClientOwnersLock
);
1496 taskque
= task_io_user_clients(task
);
1498 while (!queue_empty(taskque
))
1500 owner
= (IOUserClientOwner
*)(void *) queue_first(taskque
);
1502 queue_remove(taskque
, owner
, IOUserClientOwner
*, taskLink
);
1503 queue_remove(&uc
->owners
, owner
, IOUserClientOwner
*, ucLink
);
1504 if (queue_empty(&uc
->owners
))
1507 IOLog("destroying out of band connect for %s\n", uc
->getName());
1508 // now using the uc queue head as a singly linked queue,
1509 // leaving .next as NULL to mark it empty
1510 uc
->owners
.next
= NULL
;
1511 uc
->owners
.prev
= (queue_entry_t
) dead
;
1514 IODelete(owner
, IOUserClientOwner
, 1);
1517 IOLockUnlock(gIOUserClientOwnersLock
);
1522 dead
= (IOUserClient
*)(void *) dead
->owners
.prev
;
1523 uc
->owners
.prev
= NULL
;
1524 if (uc
->sharedInstance
|| !uc
->closed
) uc
->clientDied();
1528 return (KERN_SUCCESS
);
1531 void IOUserClient::free()
1533 if( mappings
) mappings
->release();
1535 IOStatisticsUnregisterCounter();
1537 assert(!owners
.next
);
1538 assert(!owners
.prev
);
1540 if (reserved
) IODelete(reserved
, ExpansionData
, 1);
1545 IOReturn
IOUserClient::clientDied( void )
1547 IOReturn ret
= kIOReturnNotReady
;
1549 if (sharedInstance
|| OSCompareAndSwap8(0, 1, &closed
))
1551 ret
= clientClose();
1557 IOReturn
IOUserClient::clientClose( void )
1559 return( kIOReturnUnsupported
);
1562 IOService
* IOUserClient::getService( void )
1567 IOReturn
IOUserClient::registerNotificationPort(
1568 mach_port_t
/* port */,
1570 UInt32
/* refCon */)
1572 return( kIOReturnUnsupported
);
1575 IOReturn
IOUserClient::registerNotificationPort(
1578 io_user_reference_t refCon
)
1580 return (registerNotificationPort(port
, type
, (UInt32
) refCon
));
1583 IOReturn
IOUserClient::getNotificationSemaphore( UInt32 notification_type
,
1584 semaphore_t
* semaphore
)
1586 return( kIOReturnUnsupported
);
1589 IOReturn
IOUserClient::connectClient( IOUserClient
* /* client */ )
1591 return( kIOReturnUnsupported
);
1594 IOReturn
IOUserClient::clientMemoryForType( UInt32 type
,
1595 IOOptionBits
* options
,
1596 IOMemoryDescriptor
** memory
)
1598 return( kIOReturnUnsupported
);
1602 IOMemoryMap
* IOUserClient::mapClientMemory(
1605 IOOptionBits mapFlags
,
1606 IOVirtualAddress atAddress
)
1612 IOMemoryMap
* IOUserClient::mapClientMemory64(
1615 IOOptionBits mapFlags
,
1616 mach_vm_address_t atAddress
)
1619 IOOptionBits options
= 0;
1620 IOMemoryDescriptor
* memory
;
1621 IOMemoryMap
* map
= 0;
1623 err
= clientMemoryForType( (UInt32
) type
, &options
, &memory
);
1625 if( memory
&& (kIOReturnSuccess
== err
)) {
1627 FAKE_STACK_FRAME(getMetaClass());
1629 options
= (options
& ~kIOMapUserOptionsMask
)
1630 | (mapFlags
& kIOMapUserOptionsMask
);
1631 map
= memory
->createMappingInTask( task
, atAddress
, options
);
1634 FAKE_STACK_FRAME_END();
1640 IOReturn
IOUserClient::exportObjectToClient(task_t task
,
1641 OSObject
*obj
, io_object_t
*clientObj
)
1643 mach_port_name_t name
;
1645 name
= IOMachPort::makeSendRightForTask( task
, obj
, IKOT_IOKIT_OBJECT
);
1647 *(mach_port_name_t
*)clientObj
= name
;
1648 return kIOReturnSuccess
;
1651 IOExternalMethod
* IOUserClient::getExternalMethodForIndex( UInt32
/* index */)
1656 IOExternalAsyncMethod
* IOUserClient::getExternalAsyncMethodForIndex( UInt32
/* index */)
1661 IOExternalTrap
* IOUserClient::
1662 getExternalTrapForIndex(UInt32 index
)
1667 #pragma clang diagnostic push
1668 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1670 // Suppressing the deprecated-declarations warning. Avoiding the use of deprecated
1671 // functions can break clients of kexts implementing getExternalMethodForIndex()
1672 IOExternalMethod
* IOUserClient::
1673 getTargetAndMethodForIndex(IOService
**targetP
, UInt32 index
)
1675 IOExternalMethod
*method
= getExternalMethodForIndex(index
);
1678 *targetP
= (IOService
*) method
->object
;
1683 IOExternalAsyncMethod
* IOUserClient::
1684 getAsyncTargetAndMethodForIndex(IOService
** targetP
, UInt32 index
)
1686 IOExternalAsyncMethod
*method
= getExternalAsyncMethodForIndex(index
);
1689 *targetP
= (IOService
*) method
->object
;
1694 IOExternalTrap
* IOUserClient::
1695 getTargetAndTrapForIndex(IOService
** targetP
, UInt32 index
)
1697 IOExternalTrap
*trap
= getExternalTrapForIndex(index
);
1700 *targetP
= trap
->object
;
1705 #pragma clang diagnostic pop
1707 IOReturn
IOUserClient::releaseAsyncReference64(OSAsyncReference64 reference
)
1710 port
= (mach_port_t
) (reference
[0] & ~kIOUCAsync0Flags
);
1712 if (MACH_PORT_NULL
!= port
)
1713 iokit_release_port_send(port
);
1715 return (kIOReturnSuccess
);
1718 IOReturn
IOUserClient::releaseNotificationPort(mach_port_t port
)
1720 if (MACH_PORT_NULL
!= port
)
1721 iokit_release_port_send(port
);
1723 return (kIOReturnSuccess
);
1726 IOReturn
IOUserClient::sendAsyncResult(OSAsyncReference reference
,
1727 IOReturn result
, void *args
[], UInt32 numArgs
)
1729 OSAsyncReference64 reference64
;
1730 io_user_reference_t args64
[kMaxAsyncArgs
];
1733 if (numArgs
> kMaxAsyncArgs
)
1734 return kIOReturnMessageTooLarge
;
1736 for (idx
= 0; idx
< kOSAsyncRef64Count
; idx
++)
1737 reference64
[idx
] = REF64(reference
[idx
]);
1739 for (idx
= 0; idx
< numArgs
; idx
++)
1740 args64
[idx
] = REF64(args
[idx
]);
1742 return (sendAsyncResult64(reference64
, result
, args64
, numArgs
));
1745 IOReturn
IOUserClient::sendAsyncResult64WithOptions(OSAsyncReference64 reference
,
1746 IOReturn result
, io_user_reference_t args
[], UInt32 numArgs
, IOOptionBits options
)
1748 return _sendAsyncResult64(reference
, result
, args
, numArgs
, options
);
1751 IOReturn
IOUserClient::sendAsyncResult64(OSAsyncReference64 reference
,
1752 IOReturn result
, io_user_reference_t args
[], UInt32 numArgs
)
1754 return _sendAsyncResult64(reference
, result
, args
, numArgs
, 0);
1757 IOReturn
IOUserClient::_sendAsyncResult64(OSAsyncReference64 reference
,
1758 IOReturn result
, io_user_reference_t args
[], UInt32 numArgs
, IOOptionBits options
)
1762 mach_msg_header_t msgHdr
;
1767 OSNotificationHeader notifyHdr
;
1768 IOAsyncCompletionContent asyncContent
;
1769 uint32_t args
[kMaxAsyncArgs
];
1773 OSNotificationHeader64 notifyHdr
;
1774 IOAsyncCompletionContent asyncContent
;
1775 io_user_reference_t args
[kMaxAsyncArgs
] __attribute__ ((packed
));
1780 mach_port_t replyPort
;
1783 // If no reply port, do nothing.
1784 replyPort
= (mach_port_t
) (reference
[0] & ~kIOUCAsync0Flags
);
1785 if (replyPort
== MACH_PORT_NULL
)
1786 return kIOReturnSuccess
;
1788 if (numArgs
> kMaxAsyncArgs
)
1789 return kIOReturnMessageTooLarge
;
1791 bzero(&replyMsg
, sizeof(replyMsg
));
1792 replyMsg
.msgHdr
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
/*remote*/,
1794 replyMsg
.msgHdr
.msgh_remote_port
= replyPort
;
1795 replyMsg
.msgHdr
.msgh_local_port
= 0;
1796 replyMsg
.msgHdr
.msgh_id
= kOSNotificationMessageID
;
1797 if (kIOUCAsync64Flag
& reference
[0])
1799 replyMsg
.msgHdr
.msgh_size
=
1800 sizeof(replyMsg
.msgHdr
) + sizeof(replyMsg
.m
.msg64
)
1801 - (kMaxAsyncArgs
- numArgs
) * sizeof(io_user_reference_t
);
1802 replyMsg
.m
.msg64
.notifyHdr
.size
= sizeof(IOAsyncCompletionContent
)
1803 + numArgs
* sizeof(io_user_reference_t
);
1804 replyMsg
.m
.msg64
.notifyHdr
.type
= kIOAsyncCompletionNotificationType
;
1805 bcopy(reference
, replyMsg
.m
.msg64
.notifyHdr
.reference
, sizeof(OSAsyncReference64
));
1807 replyMsg
.m
.msg64
.asyncContent
.result
= result
;
1809 bcopy(args
, replyMsg
.m
.msg64
.args
, numArgs
* sizeof(io_user_reference_t
));
1815 replyMsg
.msgHdr
.msgh_size
=
1816 sizeof(replyMsg
.msgHdr
) + sizeof(replyMsg
.m
.msg32
)
1817 - (kMaxAsyncArgs
- numArgs
) * sizeof(uint32_t);
1819 replyMsg
.m
.msg32
.notifyHdr
.size
= sizeof(IOAsyncCompletionContent
)
1820 + numArgs
* sizeof(uint32_t);
1821 replyMsg
.m
.msg32
.notifyHdr
.type
= kIOAsyncCompletionNotificationType
;
1823 for (idx
= 0; idx
< kOSAsyncRefCount
; idx
++)
1824 replyMsg
.m
.msg32
.notifyHdr
.reference
[idx
] = REF32(reference
[idx
]);
1826 replyMsg
.m
.msg32
.asyncContent
.result
= result
;
1828 for (idx
= 0; idx
< numArgs
; idx
++)
1829 replyMsg
.m
.msg32
.args
[idx
] = REF32(args
[idx
]);
1832 if ((options
& kIOUserNotifyOptionCanDrop
) != 0) {
1833 kr
= mach_msg_send_from_kernel_with_options( &replyMsg
.msgHdr
,
1834 replyMsg
.msgHdr
.msgh_size
, MACH_SEND_TIMEOUT
, MACH_MSG_TIMEOUT_NONE
);
1836 /* Fail on full queue. */
1837 kr
= mach_msg_send_from_kernel_proper( &replyMsg
.msgHdr
,
1838 replyMsg
.msgHdr
.msgh_size
);
1840 if ((KERN_SUCCESS
!= kr
) && (MACH_SEND_TIMED_OUT
!= kr
) && !(kIOUCAsyncErrorLoggedFlag
& reference
[0]))
1842 reference
[0] |= kIOUCAsyncErrorLoggedFlag
;
1843 IOLog("%s: mach_msg_send_from_kernel_proper(0x%x)\n", __PRETTY_FUNCTION__
, kr
);
1849 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1853 #define CHECK(cls,obj,out) \
1855 if( !(out = OSDynamicCast( cls, obj))) \
1856 return( kIOReturnBadArgument )
1858 #define CHECKLOCKED(cls,obj,out) \
1859 IOUserIterator * oIter; \
1861 if( !(oIter = OSDynamicCast(IOUserIterator, obj))) \
1862 return (kIOReturnBadArgument); \
1863 if( !(out = OSDynamicCast(cls, oIter->userIteratorObject))) \
1864 return (kIOReturnBadArgument)
1866 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1868 // Create a vm_map_copy_t or kalloc'ed data for memory
1869 // to be copied out. ipc will free after the copyout.
1871 static kern_return_t
copyoutkdata( const void * data
, vm_size_t len
,
1872 io_buf_ptr_t
* buf
)
1877 err
= vm_map_copyin( kernel_map
, CAST_USER_ADDR_T(data
), len
,
1878 false /* src_destroy */, ©
);
1880 assert( err
== KERN_SUCCESS
);
1881 if( err
== KERN_SUCCESS
)
1882 *buf
= (char *) copy
;
1887 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1889 /* Routine io_server_version */
1890 kern_return_t
is_io_server_version(
1891 mach_port_t master_port
,
1894 *version
= IOKIT_SERVER_VERSION
;
1895 return (kIOReturnSuccess
);
1898 /* Routine io_object_get_class */
1899 kern_return_t
is_io_object_get_class(
1901 io_name_t className
)
1903 const OSMetaClass
* my_obj
= NULL
;
1906 return( kIOReturnBadArgument
);
1908 my_obj
= object
->getMetaClass();
1910 return (kIOReturnNotFound
);
1913 strlcpy( className
, my_obj
->getClassName(), sizeof(io_name_t
));
1915 return( kIOReturnSuccess
);
1918 /* Routine io_object_get_superclass */
1919 kern_return_t
is_io_object_get_superclass(
1920 mach_port_t master_port
,
1922 io_name_t class_name
)
1924 const OSMetaClass
* my_obj
= NULL
;
1925 const OSMetaClass
* superclass
= NULL
;
1926 const OSSymbol
*my_name
= NULL
;
1927 const char *my_cstr
= NULL
;
1929 if (!obj_name
|| !class_name
)
1930 return (kIOReturnBadArgument
);
1932 if( master_port
!= master_device_port
)
1933 return( kIOReturnNotPrivileged
);
1935 my_name
= OSSymbol::withCString(obj_name
);
1938 my_obj
= OSMetaClass::getMetaClassWithName(my_name
);
1942 superclass
= my_obj
->getSuperClass();
1946 return( kIOReturnNotFound
);
1949 my_cstr
= superclass
->getClassName();
1952 strlcpy(class_name
, my_cstr
, sizeof(io_name_t
));
1953 return( kIOReturnSuccess
);
1955 return (kIOReturnNotFound
);
1958 /* Routine io_object_get_bundle_identifier */
1959 kern_return_t
is_io_object_get_bundle_identifier(
1960 mach_port_t master_port
,
1962 io_name_t bundle_name
)
1964 const OSMetaClass
* my_obj
= NULL
;
1965 const OSSymbol
*my_name
= NULL
;
1966 const OSSymbol
*identifier
= NULL
;
1967 const char *my_cstr
= NULL
;
1969 if (!obj_name
|| !bundle_name
)
1970 return (kIOReturnBadArgument
);
1972 if( master_port
!= master_device_port
)
1973 return( kIOReturnNotPrivileged
);
1975 my_name
= OSSymbol::withCString(obj_name
);
1978 my_obj
= OSMetaClass::getMetaClassWithName(my_name
);
1983 identifier
= my_obj
->getKmodName();
1986 return( kIOReturnNotFound
);
1989 my_cstr
= identifier
->getCStringNoCopy();
1991 strlcpy(bundle_name
, identifier
->getCStringNoCopy(), sizeof(io_name_t
));
1992 return( kIOReturnSuccess
);
1995 return (kIOReturnBadArgument
);
1998 /* Routine io_object_conforms_to */
1999 kern_return_t
is_io_object_conforms_to(
2001 io_name_t className
,
2002 boolean_t
*conforms
)
2005 return( kIOReturnBadArgument
);
2007 *conforms
= (0 != object
->metaCast( className
));
2009 return( kIOReturnSuccess
);
2012 /* Routine io_object_get_retain_count */
2013 kern_return_t
is_io_object_get_retain_count(
2015 uint32_t *retainCount
)
2018 return( kIOReturnBadArgument
);
2020 *retainCount
= object
->getRetainCount();
2021 return( kIOReturnSuccess
);
2024 /* Routine io_iterator_next */
2025 kern_return_t
is_io_iterator_next(
2026 io_object_t iterator
,
2027 io_object_t
*object
)
2032 CHECK( OSIterator
, iterator
, iter
);
2034 obj
= iter
->getNextObject();
2038 ret
= kIOReturnSuccess
;
2040 ret
= kIOReturnNoDevice
;
2045 /* Routine io_iterator_reset */
2046 kern_return_t
is_io_iterator_reset(
2047 io_object_t iterator
)
2049 CHECK( OSIterator
, iterator
, iter
);
2053 return( kIOReturnSuccess
);
2056 /* Routine io_iterator_is_valid */
2057 kern_return_t
is_io_iterator_is_valid(
2058 io_object_t iterator
,
2059 boolean_t
*is_valid
)
2061 CHECK( OSIterator
, iterator
, iter
);
2063 *is_valid
= iter
->isValid();
2065 return( kIOReturnSuccess
);
2069 static kern_return_t
internal_io_service_match_property_table(
2070 io_service_t _service
,
2071 const char * matching
,
2072 mach_msg_type_number_t matching_size
,
2075 CHECK( IOService
, _service
, service
);
2079 OSDictionary
* dict
;
2081 assert(matching_size
);
2082 obj
= OSUnserializeXML(matching
, matching_size
);
2084 if( (dict
= OSDynamicCast( OSDictionary
, obj
))) {
2085 *matches
= service
->passiveMatch( dict
);
2086 kr
= kIOReturnSuccess
;
2088 kr
= kIOReturnBadArgument
;
2096 /* Routine io_service_match_property_table */
2097 kern_return_t
is_io_service_match_property_table(
2098 io_service_t service
,
2099 io_string_t matching
,
2100 boolean_t
*matches
)
2102 return (kIOReturnUnsupported
);
2106 /* Routine io_service_match_property_table_ool */
2107 kern_return_t
is_io_service_match_property_table_ool(
2108 io_object_t service
,
2109 io_buf_ptr_t matching
,
2110 mach_msg_type_number_t matchingCnt
,
2111 kern_return_t
*result
,
2112 boolean_t
*matches
)
2116 vm_map_offset_t map_data
;
2118 kr
= vm_map_copyout( kernel_map
, &map_data
, (vm_map_copy_t
) matching
);
2119 data
= CAST_DOWN(vm_offset_t
, map_data
);
2121 if( KERN_SUCCESS
== kr
) {
2122 // must return success after vm_map_copyout() succeeds
2123 *result
= internal_io_service_match_property_table(service
,
2124 (const char *)data
, matchingCnt
, matches
);
2125 vm_deallocate( kernel_map
, data
, matchingCnt
);
2131 /* Routine io_service_match_property_table_bin */
2132 kern_return_t
is_io_service_match_property_table_bin(
2133 io_object_t service
,
2134 io_struct_inband_t matching
,
2135 mach_msg_type_number_t matchingCnt
,
2138 return (internal_io_service_match_property_table(service
, matching
, matchingCnt
, matches
));
2141 static kern_return_t
internal_io_service_get_matching_services(
2142 mach_port_t master_port
,
2143 const char * matching
,
2144 mach_msg_type_number_t matching_size
,
2145 io_iterator_t
*existing
)
2149 OSDictionary
* dict
;
2151 if( master_port
!= master_device_port
)
2152 return( kIOReturnNotPrivileged
);
2154 assert(matching_size
);
2155 obj
= OSUnserializeXML(matching
, matching_size
);
2157 if( (dict
= OSDynamicCast( OSDictionary
, obj
))) {
2158 *existing
= IOUserIterator::withIterator(IOService::getMatchingServices( dict
));
2159 kr
= kIOReturnSuccess
;
2161 kr
= kIOReturnBadArgument
;
2169 /* Routine io_service_get_matching_services */
2170 kern_return_t
is_io_service_get_matching_services(
2171 mach_port_t master_port
,
2172 io_string_t matching
,
2173 io_iterator_t
*existing
)
2175 return (kIOReturnUnsupported
);
2178 /* Routine io_service_get_matching_services_ool */
2179 kern_return_t
is_io_service_get_matching_services_ool(
2180 mach_port_t master_port
,
2181 io_buf_ptr_t matching
,
2182 mach_msg_type_number_t matchingCnt
,
2183 kern_return_t
*result
,
2184 io_object_t
*existing
)
2188 vm_map_offset_t map_data
;
2190 kr
= vm_map_copyout( kernel_map
, &map_data
, (vm_map_copy_t
) matching
);
2191 data
= CAST_DOWN(vm_offset_t
, map_data
);
2193 if( KERN_SUCCESS
== kr
) {
2194 // must return success after vm_map_copyout() succeeds
2195 // and mig will copy out objects on success
2197 *result
= internal_io_service_get_matching_services(master_port
,
2198 (const char *) data
, matchingCnt
, existing
);
2199 vm_deallocate( kernel_map
, data
, matchingCnt
);
2205 /* Routine io_service_get_matching_services_bin */
2206 kern_return_t
is_io_service_get_matching_services_bin(
2207 mach_port_t master_port
,
2208 io_struct_inband_t matching
,
2209 mach_msg_type_number_t matchingCnt
,
2210 io_object_t
*existing
)
2212 return (internal_io_service_get_matching_services(master_port
, matching
, matchingCnt
, existing
));
2216 static kern_return_t
internal_io_service_get_matching_service(
2217 mach_port_t master_port
,
2218 const char * matching
,
2219 mach_msg_type_number_t matching_size
,
2220 io_service_t
*service
)
2224 OSDictionary
* dict
;
2226 if( master_port
!= master_device_port
)
2227 return( kIOReturnNotPrivileged
);
2229 assert(matching_size
);
2230 obj
= OSUnserializeXML(matching
, matching_size
);
2232 if( (dict
= OSDynamicCast( OSDictionary
, obj
))) {
2233 *service
= IOService::copyMatchingService( dict
);
2234 kr
= *service
? kIOReturnSuccess
: kIOReturnNotFound
;
2236 kr
= kIOReturnBadArgument
;
2244 /* Routine io_service_get_matching_service */
2245 kern_return_t
is_io_service_get_matching_service(
2246 mach_port_t master_port
,
2247 io_string_t matching
,
2248 io_service_t
*service
)
2250 return (kIOReturnUnsupported
);
2253 /* Routine io_service_get_matching_services_ool */
2254 kern_return_t
is_io_service_get_matching_service_ool(
2255 mach_port_t master_port
,
2256 io_buf_ptr_t matching
,
2257 mach_msg_type_number_t matchingCnt
,
2258 kern_return_t
*result
,
2259 io_object_t
*service
)
2263 vm_map_offset_t map_data
;
2265 kr
= vm_map_copyout( kernel_map
, &map_data
, (vm_map_copy_t
) matching
);
2266 data
= CAST_DOWN(vm_offset_t
, map_data
);
2268 if( KERN_SUCCESS
== kr
) {
2269 // must return success after vm_map_copyout() succeeds
2270 // and mig will copy out objects on success
2272 *result
= internal_io_service_get_matching_service(master_port
,
2273 (const char *) data
, matchingCnt
, service
);
2274 vm_deallocate( kernel_map
, data
, matchingCnt
);
2280 /* Routine io_service_get_matching_service_bin */
2281 kern_return_t
is_io_service_get_matching_service_bin(
2282 mach_port_t master_port
,
2283 io_struct_inband_t matching
,
2284 mach_msg_type_number_t matchingCnt
,
2285 io_object_t
*service
)
2287 return (internal_io_service_get_matching_service(master_port
, matching
, matchingCnt
, service
));
2290 static kern_return_t
internal_io_service_add_notification(
2291 mach_port_t master_port
,
2292 io_name_t notification_type
,
2293 const char * matching
,
2294 size_t matching_size
,
2297 vm_size_t referenceSize
,
2299 io_object_t
* notification
)
2301 IOServiceUserNotification
* userNotify
= 0;
2302 IONotifier
* notify
= 0;
2303 const OSSymbol
* sym
;
2304 OSDictionary
* dict
;
2306 unsigned long int userMsgType
;
2308 if( master_port
!= master_device_port
)
2309 return( kIOReturnNotPrivileged
);
2312 err
= kIOReturnNoResources
;
2314 if( !(sym
= OSSymbol::withCString( notification_type
)))
2315 err
= kIOReturnNoResources
;
2317 assert(matching_size
);
2318 dict
= OSDynamicCast(OSDictionary
, OSUnserializeXML(matching
, matching_size
));
2320 err
= kIOReturnBadArgument
;
2324 if( (sym
== gIOPublishNotification
)
2325 || (sym
== gIOFirstPublishNotification
))
2326 userMsgType
= kIOServicePublishNotificationType
;
2327 else if( (sym
== gIOMatchedNotification
)
2328 || (sym
== gIOFirstMatchNotification
))
2329 userMsgType
= kIOServiceMatchedNotificationType
;
2330 else if( sym
== gIOTerminatedNotification
)
2331 userMsgType
= kIOServiceTerminatedNotificationType
;
2333 userMsgType
= kLastIOKitNotificationType
;
2335 userNotify
= new IOServiceUserNotification
;
2337 if( userNotify
&& !userNotify
->init( port
, userMsgType
,
2338 reference
, referenceSize
, client64
)) {
2339 iokit_release_port_send(port
);
2340 userNotify
->release();
2346 notify
= IOService::addMatchingNotification( sym
, dict
,
2347 &userNotify
->_handler
, userNotify
);
2349 *notification
= userNotify
;
2350 userNotify
->setNotification( notify
);
2351 err
= kIOReturnSuccess
;
2353 err
= kIOReturnUnsupported
;
2366 /* Routine io_service_add_notification */
2367 kern_return_t
is_io_service_add_notification(
2368 mach_port_t master_port
,
2369 io_name_t notification_type
,
2370 io_string_t matching
,
2372 io_async_ref_t reference
,
2373 mach_msg_type_number_t referenceCnt
,
2374 io_object_t
* notification
)
2376 return (kIOReturnUnsupported
);
2379 /* Routine io_service_add_notification_64 */
2380 kern_return_t
is_io_service_add_notification_64(
2381 mach_port_t master_port
,
2382 io_name_t notification_type
,
2383 io_string_t matching
,
2384 mach_port_t wake_port
,
2385 io_async_ref64_t reference
,
2386 mach_msg_type_number_t referenceCnt
,
2387 io_object_t
*notification
)
2389 return (kIOReturnUnsupported
);
2392 /* Routine io_service_add_notification_bin */
2393 kern_return_t is_io_service_add_notification_bin
2395 mach_port_t master_port
,
2396 io_name_t notification_type
,
2397 io_struct_inband_t matching
,
2398 mach_msg_type_number_t matchingCnt
,
2399 mach_port_t wake_port
,
2400 io_async_ref_t reference
,
2401 mach_msg_type_number_t referenceCnt
,
2402 io_object_t
*notification
)
2404 return (internal_io_service_add_notification(master_port
, notification_type
,
2405 matching
, matchingCnt
, wake_port
, &reference
[0], sizeof(io_async_ref_t
),
2406 false, notification
));
2409 /* Routine io_service_add_notification_bin_64 */
2410 kern_return_t is_io_service_add_notification_bin_64
2412 mach_port_t master_port
,
2413 io_name_t notification_type
,
2414 io_struct_inband_t matching
,
2415 mach_msg_type_number_t matchingCnt
,
2416 mach_port_t wake_port
,
2417 io_async_ref64_t reference
,
2418 mach_msg_type_number_t referenceCnt
,
2419 io_object_t
*notification
)
2421 return (internal_io_service_add_notification(master_port
, notification_type
,
2422 matching
, matchingCnt
, wake_port
, &reference
[0], sizeof(io_async_ref64_t
),
2423 true, notification
));
2426 static kern_return_t
internal_io_service_add_notification_ool(
2427 mach_port_t master_port
,
2428 io_name_t notification_type
,
2429 io_buf_ptr_t matching
,
2430 mach_msg_type_number_t matchingCnt
,
2431 mach_port_t wake_port
,
2433 vm_size_t referenceSize
,
2435 kern_return_t
*result
,
2436 io_object_t
*notification
)
2440 vm_map_offset_t map_data
;
2442 kr
= vm_map_copyout( kernel_map
, &map_data
, (vm_map_copy_t
) matching
);
2443 data
= CAST_DOWN(vm_offset_t
, map_data
);
2445 if( KERN_SUCCESS
== kr
) {
2446 // must return success after vm_map_copyout() succeeds
2447 // and mig will copy out objects on success
2449 *result
= internal_io_service_add_notification( master_port
, notification_type
,
2450 (char *) data
, matchingCnt
, wake_port
, reference
, referenceSize
, client64
, notification
);
2451 vm_deallocate( kernel_map
, data
, matchingCnt
);
2457 /* Routine io_service_add_notification_ool */
2458 kern_return_t
is_io_service_add_notification_ool(
2459 mach_port_t master_port
,
2460 io_name_t notification_type
,
2461 io_buf_ptr_t matching
,
2462 mach_msg_type_number_t matchingCnt
,
2463 mach_port_t wake_port
,
2464 io_async_ref_t reference
,
2465 mach_msg_type_number_t referenceCnt
,
2466 kern_return_t
*result
,
2467 io_object_t
*notification
)
2469 return (internal_io_service_add_notification_ool(master_port
, notification_type
,
2470 matching
, matchingCnt
, wake_port
, &reference
[0], sizeof(io_async_ref_t
),
2471 false, result
, notification
));
2474 /* Routine io_service_add_notification_ool_64 */
2475 kern_return_t
is_io_service_add_notification_ool_64(
2476 mach_port_t master_port
,
2477 io_name_t notification_type
,
2478 io_buf_ptr_t matching
,
2479 mach_msg_type_number_t matchingCnt
,
2480 mach_port_t wake_port
,
2481 io_async_ref64_t reference
,
2482 mach_msg_type_number_t referenceCnt
,
2483 kern_return_t
*result
,
2484 io_object_t
*notification
)
2486 return (internal_io_service_add_notification_ool(master_port
, notification_type
,
2487 matching
, matchingCnt
, wake_port
, &reference
[0], sizeof(io_async_ref64_t
),
2488 true, result
, notification
));
2491 /* Routine io_service_add_notification_old */
2492 kern_return_t
is_io_service_add_notification_old(
2493 mach_port_t master_port
,
2494 io_name_t notification_type
,
2495 io_string_t matching
,
2497 // for binary compatibility reasons, this must be natural_t for ILP32
2499 io_object_t
* notification
)
2501 return( is_io_service_add_notification( master_port
, notification_type
,
2502 matching
, port
, &ref
, 1, notification
));
2506 static kern_return_t
internal_io_service_add_interest_notification(
2507 io_object_t _service
,
2508 io_name_t type_of_interest
,
2511 vm_size_t referenceSize
,
2513 io_object_t
* notification
)
2516 IOServiceMessageUserNotification
* userNotify
= 0;
2517 IONotifier
* notify
= 0;
2518 const OSSymbol
* sym
;
2521 CHECK( IOService
, _service
, service
);
2523 err
= kIOReturnNoResources
;
2524 if( (sym
= OSSymbol::withCString( type_of_interest
))) do {
2526 userNotify
= new IOServiceMessageUserNotification
;
2528 if( userNotify
&& !userNotify
->init( port
, kIOServiceMessageNotificationType
,
2529 reference
, referenceSize
,
2530 kIOUserNotifyMaxMessageSize
,
2532 iokit_release_port_send(port
);
2533 userNotify
->release();
2539 notify
= service
->registerInterest( sym
,
2540 &userNotify
->_handler
, userNotify
);
2542 *notification
= userNotify
;
2543 userNotify
->setNotification( notify
);
2544 err
= kIOReturnSuccess
;
2546 err
= kIOReturnUnsupported
;
2555 /* Routine io_service_add_message_notification */
2556 kern_return_t
is_io_service_add_interest_notification(
2557 io_object_t service
,
2558 io_name_t type_of_interest
,
2560 io_async_ref_t reference
,
2561 mach_msg_type_number_t referenceCnt
,
2562 io_object_t
* notification
)
2564 return (internal_io_service_add_interest_notification(service
, type_of_interest
,
2565 port
, &reference
[0], sizeof(io_async_ref_t
), false, notification
));
2568 /* Routine io_service_add_interest_notification_64 */
2569 kern_return_t
is_io_service_add_interest_notification_64(
2570 io_object_t service
,
2571 io_name_t type_of_interest
,
2572 mach_port_t wake_port
,
2573 io_async_ref64_t reference
,
2574 mach_msg_type_number_t referenceCnt
,
2575 io_object_t
*notification
)
2577 return (internal_io_service_add_interest_notification(service
, type_of_interest
,
2578 wake_port
, &reference
[0], sizeof(io_async_ref64_t
), true, notification
));
2582 /* Routine io_service_acknowledge_notification */
2583 kern_return_t
is_io_service_acknowledge_notification(
2584 io_object_t _service
,
2585 natural_t notify_ref
,
2586 natural_t response
)
2588 CHECK( IOService
, _service
, service
);
2590 return( service
->acknowledgeNotification( (IONotificationRef
)(uintptr_t) notify_ref
,
2591 (IOOptionBits
) response
));
2595 /* Routine io_connect_get_semaphore */
2596 kern_return_t
is_io_connect_get_notification_semaphore(
2597 io_connect_t connection
,
2598 natural_t notification_type
,
2599 semaphore_t
*semaphore
)
2601 CHECK( IOUserClient
, connection
, client
);
2603 IOStatisticsClientCall();
2604 return( client
->getNotificationSemaphore( (UInt32
) notification_type
,
2608 /* Routine io_registry_get_root_entry */
2609 kern_return_t
is_io_registry_get_root_entry(
2610 mach_port_t master_port
,
2613 IORegistryEntry
* entry
;
2615 if( master_port
!= master_device_port
)
2616 return( kIOReturnNotPrivileged
);
2618 entry
= IORegistryEntry::getRegistryRoot();
2623 return( kIOReturnSuccess
);
2626 /* Routine io_registry_create_iterator */
2627 kern_return_t
is_io_registry_create_iterator(
2628 mach_port_t master_port
,
2631 io_object_t
*iterator
)
2633 if( master_port
!= master_device_port
)
2634 return( kIOReturnNotPrivileged
);
2636 *iterator
= IOUserIterator::withIterator(
2637 IORegistryIterator::iterateOver(
2638 IORegistryEntry::getPlane( plane
), options
));
2640 return( *iterator
? kIOReturnSuccess
: kIOReturnBadArgument
);
2643 /* Routine io_registry_entry_create_iterator */
2644 kern_return_t
is_io_registry_entry_create_iterator(
2645 io_object_t registry_entry
,
2648 io_object_t
*iterator
)
2650 CHECK( IORegistryEntry
, registry_entry
, entry
);
2652 *iterator
= IOUserIterator::withIterator(
2653 IORegistryIterator::iterateOver( entry
,
2654 IORegistryEntry::getPlane( plane
), options
));
2656 return( *iterator
? kIOReturnSuccess
: kIOReturnBadArgument
);
2659 /* Routine io_registry_iterator_enter */
2660 kern_return_t
is_io_registry_iterator_enter_entry(
2661 io_object_t iterator
)
2663 CHECKLOCKED( IORegistryIterator
, iterator
, iter
);
2665 IOLockLock(oIter
->lock
);
2667 IOLockUnlock(oIter
->lock
);
2669 return( kIOReturnSuccess
);
2672 /* Routine io_registry_iterator_exit */
2673 kern_return_t
is_io_registry_iterator_exit_entry(
2674 io_object_t iterator
)
2678 CHECKLOCKED( IORegistryIterator
, iterator
, iter
);
2680 IOLockLock(oIter
->lock
);
2681 didIt
= iter
->exitEntry();
2682 IOLockUnlock(oIter
->lock
);
2684 return( didIt
? kIOReturnSuccess
: kIOReturnNoDevice
);
2687 /* Routine io_registry_entry_from_path */
2688 kern_return_t
is_io_registry_entry_from_path(
2689 mach_port_t master_port
,
2691 io_object_t
*registry_entry
)
2693 IORegistryEntry
* entry
;
2695 if( master_port
!= master_device_port
)
2696 return( kIOReturnNotPrivileged
);
2698 entry
= IORegistryEntry::fromPath( path
);
2700 *registry_entry
= entry
;
2702 return( kIOReturnSuccess
);
2706 /* Routine io_registry_entry_from_path */
2707 kern_return_t
is_io_registry_entry_from_path_ool(
2708 mach_port_t master_port
,
2709 io_string_inband_t path
,
2710 io_buf_ptr_t path_ool
,
2711 mach_msg_type_number_t path_oolCnt
,
2712 kern_return_t
*result
,
2713 io_object_t
*registry_entry
)
2715 IORegistryEntry
* entry
;
2716 vm_map_offset_t map_data
;
2721 if (master_port
!= master_device_port
) return(kIOReturnNotPrivileged
);
2725 res
= err
= KERN_SUCCESS
;
2726 if (path
[0]) cpath
= path
;
2729 if (!path_oolCnt
) return(kIOReturnBadArgument
);
2730 if (path_oolCnt
> (sizeof(io_struct_inband_t
) * 1024)) return(kIOReturnMessageTooLarge
);
2732 err
= vm_map_copyout(kernel_map
, &map_data
, (vm_map_copy_t
) path_ool
);
2733 if (KERN_SUCCESS
== err
)
2735 // must return success to mig after vm_map_copyout() succeeds, so result is actual
2736 cpath
= CAST_DOWN(const char *, map_data
);
2737 if (cpath
[path_oolCnt
- 1]) res
= kIOReturnBadArgument
;
2741 if ((KERN_SUCCESS
== err
) && (KERN_SUCCESS
== res
))
2743 entry
= IORegistryEntry::fromPath(cpath
);
2744 res
= entry
? kIOReturnSuccess
: kIOReturnNotFound
;
2747 if (map_data
) vm_deallocate(kernel_map
, map_data
, path_oolCnt
);
2749 if (KERN_SUCCESS
!= err
) res
= err
;
2750 *registry_entry
= entry
;
2757 /* Routine io_registry_entry_in_plane */
2758 kern_return_t
is_io_registry_entry_in_plane(
2759 io_object_t registry_entry
,
2761 boolean_t
*inPlane
)
2763 CHECK( IORegistryEntry
, registry_entry
, entry
);
2765 *inPlane
= entry
->inPlane( IORegistryEntry::getPlane( plane
));
2767 return( kIOReturnSuccess
);
2771 /* Routine io_registry_entry_get_path */
2772 kern_return_t
is_io_registry_entry_get_path(
2773 io_object_t registry_entry
,
2778 CHECK( IORegistryEntry
, registry_entry
, entry
);
2780 length
= sizeof( io_string_t
);
2781 if( entry
->getPath( path
, &length
, IORegistryEntry::getPlane( plane
)))
2782 return( kIOReturnSuccess
);
2784 return( kIOReturnBadArgument
);
2787 /* Routine io_registry_entry_get_path */
2788 kern_return_t
is_io_registry_entry_get_path_ool(
2789 io_object_t registry_entry
,
2791 io_string_inband_t path
,
2792 io_buf_ptr_t
*path_ool
,
2793 mach_msg_type_number_t
*path_oolCnt
)
2795 enum { kMaxPath
= 16384 };
2800 CHECK( IORegistryEntry
, registry_entry
, entry
);
2804 length
= sizeof(io_string_inband_t
);
2805 if (entry
->getPath(path
, &length
, IORegistryEntry::getPlane(plane
))) err
= kIOReturnSuccess
;
2809 buf
= IONew(char, length
);
2810 if (!buf
) err
= kIOReturnNoMemory
;
2811 else if (!entry
->getPath(buf
, &length
, IORegistryEntry::getPlane(plane
))) err
= kIOReturnError
;
2814 *path_oolCnt
= length
;
2815 err
= copyoutkdata(buf
, length
, path_ool
);
2817 if (buf
) IODelete(buf
, char, kMaxPath
);
2824 /* Routine io_registry_entry_get_name */
2825 kern_return_t
is_io_registry_entry_get_name(
2826 io_object_t registry_entry
,
2829 CHECK( IORegistryEntry
, registry_entry
, entry
);
2831 strncpy( name
, entry
->getName(), sizeof( io_name_t
));
2833 return( kIOReturnSuccess
);
2836 /* Routine io_registry_entry_get_name_in_plane */
2837 kern_return_t
is_io_registry_entry_get_name_in_plane(
2838 io_object_t registry_entry
,
2839 io_name_t planeName
,
2842 const IORegistryPlane
* plane
;
2843 CHECK( IORegistryEntry
, registry_entry
, entry
);
2846 plane
= IORegistryEntry::getPlane( planeName
);
2850 strncpy( name
, entry
->getName( plane
), sizeof( io_name_t
));
2852 return( kIOReturnSuccess
);
2855 /* Routine io_registry_entry_get_location_in_plane */
2856 kern_return_t
is_io_registry_entry_get_location_in_plane(
2857 io_object_t registry_entry
,
2858 io_name_t planeName
,
2859 io_name_t location
)
2861 const IORegistryPlane
* plane
;
2862 CHECK( IORegistryEntry
, registry_entry
, entry
);
2865 plane
= IORegistryEntry::getPlane( planeName
);
2869 const char * cstr
= entry
->getLocation( plane
);
2872 strncpy( location
, cstr
, sizeof( io_name_t
));
2873 return( kIOReturnSuccess
);
2875 return( kIOReturnNotFound
);
2878 /* Routine io_registry_entry_get_registry_entry_id */
2879 kern_return_t
is_io_registry_entry_get_registry_entry_id(
2880 io_object_t registry_entry
,
2881 uint64_t *entry_id
)
2883 CHECK( IORegistryEntry
, registry_entry
, entry
);
2885 *entry_id
= entry
->getRegistryEntryID();
2887 return (kIOReturnSuccess
);
2890 /* Routine io_registry_entry_get_property */
2891 kern_return_t
is_io_registry_entry_get_property_bytes(
2892 io_object_t registry_entry
,
2893 io_name_t property_name
,
2894 io_struct_inband_t buf
,
2895 mach_msg_type_number_t
*dataCnt
)
2903 unsigned int len
= 0;
2904 const void * bytes
= 0;
2905 IOReturn ret
= kIOReturnSuccess
;
2907 CHECK( IORegistryEntry
, registry_entry
, entry
);
2910 if (0 != mac_iokit_check_get_property(kauth_cred_get(), entry
, property_name
))
2911 return kIOReturnNotPermitted
;
2914 obj
= entry
->copyProperty(property_name
);
2916 return( kIOReturnNoResources
);
2918 // One day OSData will be a common container base class
2920 if( (data
= OSDynamicCast( OSData
, obj
))) {
2921 len
= data
->getLength();
2922 bytes
= data
->getBytesNoCopy();
2924 } else if( (str
= OSDynamicCast( OSString
, obj
))) {
2925 len
= str
->getLength() + 1;
2926 bytes
= str
->getCStringNoCopy();
2928 } else if( (boo
= OSDynamicCast( OSBoolean
, obj
))) {
2929 len
= boo
->isTrue() ? sizeof("Yes") : sizeof("No");
2930 bytes
= boo
->isTrue() ? "Yes" : "No";
2932 } else if( (off
= OSDynamicCast( OSNumber
, obj
))) {
2933 offsetBytes
= off
->unsigned64BitValue();
2934 len
= off
->numberOfBytes();
2935 if (len
> sizeof(offsetBytes
)) len
= sizeof(offsetBytes
);
2936 bytes
= &offsetBytes
;
2937 #ifdef __BIG_ENDIAN__
2938 bytes
= (const void *)
2939 (((UInt32
) bytes
) + (sizeof( UInt64
) - len
));
2943 ret
= kIOReturnBadArgument
;
2947 ret
= kIOReturnIPCError
;
2950 bcopy( bytes
, buf
, len
);
2959 /* Routine io_registry_entry_get_property */
2960 kern_return_t
is_io_registry_entry_get_property(
2961 io_object_t registry_entry
,
2962 io_name_t property_name
,
2963 io_buf_ptr_t
*properties
,
2964 mach_msg_type_number_t
*propertiesCnt
)
2970 CHECK( IORegistryEntry
, registry_entry
, entry
);
2973 if (0 != mac_iokit_check_get_property(kauth_cred_get(), entry
, property_name
))
2974 return kIOReturnNotPermitted
;
2977 obj
= entry
->copyProperty(property_name
);
2979 return( kIOReturnNotFound
);
2981 OSSerialize
* s
= OSSerialize::withCapacity(4096);
2984 return( kIOReturnNoMemory
);
2987 if( obj
->serialize( s
)) {
2988 len
= s
->getLength();
2989 *propertiesCnt
= len
;
2990 err
= copyoutkdata( s
->text(), len
, properties
);
2993 err
= kIOReturnUnsupported
;
3001 /* Routine io_registry_entry_get_property_recursively */
3002 kern_return_t
is_io_registry_entry_get_property_recursively(
3003 io_object_t registry_entry
,
3005 io_name_t property_name
,
3007 io_buf_ptr_t
*properties
,
3008 mach_msg_type_number_t
*propertiesCnt
)
3014 CHECK( IORegistryEntry
, registry_entry
, entry
);
3017 if (0 != mac_iokit_check_get_property(kauth_cred_get(), entry
, property_name
))
3018 return kIOReturnNotPermitted
;
3021 obj
= entry
->copyProperty( property_name
,
3022 IORegistryEntry::getPlane( plane
), options
);
3024 return( kIOReturnNotFound
);
3026 OSSerialize
* s
= OSSerialize::withCapacity(4096);
3029 return( kIOReturnNoMemory
);
3032 if( obj
->serialize( s
)) {
3033 len
= s
->getLength();
3034 *propertiesCnt
= len
;
3035 err
= copyoutkdata( s
->text(), len
, properties
);
3038 err
= kIOReturnUnsupported
;
3046 /* Routine io_registry_entry_get_properties */
3047 kern_return_t
is_io_registry_entry_get_properties(
3048 io_object_t registry_entry
,
3049 io_buf_ptr_t
*properties
,
3050 mach_msg_type_number_t
*propertiesCnt
)
3052 return (kIOReturnUnsupported
);
3057 struct GetPropertiesEditorRef
3060 IORegistryEntry
* entry
;
3061 OSCollection
* root
;
3064 static const OSMetaClassBase
*
3065 GetPropertiesEditor(void * reference
,
3067 OSCollection
* container
,
3068 const OSSymbol
* name
,
3069 const OSMetaClassBase
* value
)
3071 GetPropertiesEditorRef
* ref
= (typeof(ref
)) reference
;
3073 if (!ref
->root
) ref
->root
= container
;
3074 if (ref
->root
== container
)
3076 if (0 != mac_iokit_check_get_property(ref
->cred
, ref
->entry
, name
->getCStringNoCopy()))
3081 if (value
) value
->retain();
3085 #endif /* CONFIG_MACF */
3087 /* Routine io_registry_entry_get_properties */
3088 kern_return_t
is_io_registry_entry_get_properties_bin(
3089 io_object_t registry_entry
,
3090 io_buf_ptr_t
*properties
,
3091 mach_msg_type_number_t
*propertiesCnt
)
3093 kern_return_t err
= kIOReturnSuccess
;
3096 OSSerialize::Editor editor
= 0;
3099 CHECK(IORegistryEntry
, registry_entry
, entry
);
3102 GetPropertiesEditorRef ref
;
3103 if (mac_iokit_check_filter_properties(kauth_cred_get(), entry
))
3105 editor
= &GetPropertiesEditor
;
3107 ref
.cred
= kauth_cred_get();
3113 s
= OSSerialize::binaryWithCapacity(4096, editor
, editRef
);
3114 if (!s
) return (kIOReturnNoMemory
);
3116 if (!entry
->serializeProperties(s
)) err
= kIOReturnUnsupported
;
3118 if (kIOReturnSuccess
== err
)
3120 len
= s
->getLength();
3121 *propertiesCnt
= len
;
3122 err
= copyoutkdata(s
->text(), len
, properties
);
3129 /* Routine io_registry_entry_get_property_bin */
3130 kern_return_t
is_io_registry_entry_get_property_bin(
3131 io_object_t registry_entry
,
3133 io_name_t property_name
,
3135 io_buf_ptr_t
*properties
,
3136 mach_msg_type_number_t
*propertiesCnt
)
3141 const OSSymbol
* sym
;
3143 CHECK( IORegistryEntry
, registry_entry
, entry
);
3146 if (0 != mac_iokit_check_get_property(kauth_cred_get(), entry
, property_name
))
3147 return kIOReturnNotPermitted
;
3150 sym
= OSSymbol::withCString(property_name
);
3151 if (!sym
) return (kIOReturnNoMemory
);
3153 if (gIORegistryEntryPropertyKeysKey
== sym
)
3155 obj
= entry
->copyPropertyKeys();
3159 if ((kIORegistryIterateRecursively
& options
) && plane
[0])
3161 obj
= entry
->copyProperty(property_name
,
3162 IORegistryEntry::getPlane(plane
), options
);
3166 obj
= entry
->copyProperty(property_name
);
3168 if (obj
&& gIORemoveOnReadProperties
->containsObject(sym
)) entry
->removeProperty(sym
);
3172 if (!obj
) return (kIOReturnNotFound
);
3174 OSSerialize
* s
= OSSerialize::binaryWithCapacity(4096);
3177 return( kIOReturnNoMemory
);
3180 if( obj
->serialize( s
)) {
3181 len
= s
->getLength();
3182 *propertiesCnt
= len
;
3183 err
= copyoutkdata( s
->text(), len
, properties
);
3185 } else err
= kIOReturnUnsupported
;
3194 /* Routine io_registry_entry_set_properties */
3195 kern_return_t is_io_registry_entry_set_properties
3197 io_object_t registry_entry
,
3198 io_buf_ptr_t properties
,
3199 mach_msg_type_number_t propertiesCnt
,
3200 kern_return_t
* result
)
3206 vm_map_offset_t map_data
;
3208 CHECK( IORegistryEntry
, registry_entry
, entry
);
3210 if( propertiesCnt
> sizeof(io_struct_inband_t
) * 1024)
3211 return( kIOReturnMessageTooLarge
);
3213 err
= vm_map_copyout( kernel_map
, &map_data
, (vm_map_copy_t
) properties
);
3214 data
= CAST_DOWN(vm_offset_t
, map_data
);
3216 if( KERN_SUCCESS
== err
) {
3218 FAKE_STACK_FRAME(entry
->getMetaClass());
3220 // must return success after vm_map_copyout() succeeds
3221 obj
= OSUnserializeXML( (const char *) data
, propertiesCnt
);
3222 vm_deallocate( kernel_map
, data
, propertiesCnt
);
3225 res
= kIOReturnBadArgument
;
3227 else if (0 != mac_iokit_check_set_properties(kauth_cred_get(),
3228 registry_entry
, obj
))
3230 res
= kIOReturnNotPermitted
;
3235 res
= entry
->setProperties( obj
);
3241 FAKE_STACK_FRAME_END();
3250 /* Routine io_registry_entry_get_child_iterator */
3251 kern_return_t
is_io_registry_entry_get_child_iterator(
3252 io_object_t registry_entry
,
3254 io_object_t
*iterator
)
3256 CHECK( IORegistryEntry
, registry_entry
, entry
);
3258 *iterator
= entry
->getChildIterator(
3259 IORegistryEntry::getPlane( plane
));
3261 return( kIOReturnSuccess
);
3264 /* Routine io_registry_entry_get_parent_iterator */
3265 kern_return_t
is_io_registry_entry_get_parent_iterator(
3266 io_object_t registry_entry
,
3268 io_object_t
*iterator
)
3270 CHECK( IORegistryEntry
, registry_entry
, entry
);
3272 *iterator
= entry
->getParentIterator(
3273 IORegistryEntry::getPlane( plane
));
3275 return( kIOReturnSuccess
);
3278 /* Routine io_service_get_busy_state */
3279 kern_return_t
is_io_service_get_busy_state(
3280 io_object_t _service
,
3281 uint32_t *busyState
)
3283 CHECK( IOService
, _service
, service
);
3285 *busyState
= service
->getBusyState();
3287 return( kIOReturnSuccess
);
3290 /* Routine io_service_get_state */
3291 kern_return_t
is_io_service_get_state(
3292 io_object_t _service
,
3294 uint32_t *busy_state
,
3295 uint64_t *accumulated_busy_time
)
3297 CHECK( IOService
, _service
, service
);
3299 *state
= service
->getState();
3300 *busy_state
= service
->getBusyState();
3301 *accumulated_busy_time
= service
->getAccumulatedBusyTime();
3303 return( kIOReturnSuccess
);
3306 /* Routine io_service_wait_quiet */
3307 kern_return_t
is_io_service_wait_quiet(
3308 io_object_t _service
,
3309 mach_timespec_t wait_time
)
3313 CHECK( IOService
, _service
, service
);
3315 timeoutNS
= wait_time
.tv_sec
;
3316 timeoutNS
*= kSecondScale
;
3317 timeoutNS
+= wait_time
.tv_nsec
;
3319 return( service
->waitQuiet(timeoutNS
) );
3322 /* Routine io_service_request_probe */
3323 kern_return_t
is_io_service_request_probe(
3324 io_object_t _service
,
3327 CHECK( IOService
, _service
, service
);
3329 return( service
->requestProbe( options
));
3332 /* Routine io_service_get_authorization_id */
3333 kern_return_t
is_io_service_get_authorization_id(
3334 io_object_t _service
,
3335 uint64_t *authorization_id
)
3339 CHECK( IOService
, _service
, service
);
3341 kr
= IOUserClient::clientHasPrivilege( (void *) current_task(),
3342 kIOClientPrivilegeAdministrator
);
3343 if( kIOReturnSuccess
!= kr
)
3346 *authorization_id
= service
->getAuthorizationID();
3351 /* Routine io_service_set_authorization_id */
3352 kern_return_t
is_io_service_set_authorization_id(
3353 io_object_t _service
,
3354 uint64_t authorization_id
)
3356 CHECK( IOService
, _service
, service
);
3358 return( service
->setAuthorizationID( authorization_id
) );
3361 /* Routine io_service_open_ndr */
3362 kern_return_t
is_io_service_open_extended(
3363 io_object_t _service
,
3365 uint32_t connect_type
,
3367 io_buf_ptr_t properties
,
3368 mach_msg_type_number_t propertiesCnt
,
3369 kern_return_t
* result
,
3370 io_object_t
*connection
)
3372 IOUserClient
* client
= 0;
3373 kern_return_t err
= KERN_SUCCESS
;
3374 IOReturn res
= kIOReturnSuccess
;
3375 OSDictionary
* propertiesDict
= 0;
3377 bool disallowAccess
;
3379 CHECK( IOService
, _service
, service
);
3381 if (!owningTask
) return (kIOReturnBadArgument
);
3382 assert(owningTask
== current_task());
3383 if (owningTask
!= current_task()) return (kIOReturnBadArgument
);
3391 vm_map_offset_t map_data
;
3393 if( propertiesCnt
> sizeof(io_struct_inband_t
))
3394 return( kIOReturnMessageTooLarge
);
3396 err
= vm_map_copyout( kernel_map
, &map_data
, (vm_map_copy_t
) properties
);
3398 data
= CAST_DOWN(vm_offset_t
, map_data
);
3399 if (KERN_SUCCESS
== err
)
3401 // must return success after vm_map_copyout() succeeds
3402 obj
= OSUnserializeXML( (const char *) data
, propertiesCnt
);
3403 vm_deallocate( kernel_map
, data
, propertiesCnt
);
3404 propertiesDict
= OSDynamicCast(OSDictionary
, obj
);
3405 if (!propertiesDict
)
3407 res
= kIOReturnBadArgument
;
3412 if (kIOReturnSuccess
!= res
)
3416 crossEndian
= (ndr
.int_rep
!= NDR_record
.int_rep
);
3419 if (!propertiesDict
)
3420 propertiesDict
= OSDictionary::withCapacity(4);
3421 OSData
* data
= OSData::withBytes(&ndr
, sizeof(ndr
));
3425 propertiesDict
->setObject(kIOUserClientCrossEndianKey
, data
);
3430 res
= service
->newUserClient( owningTask
, (void *) owningTask
,
3431 connect_type
, propertiesDict
, &client
);
3434 propertiesDict
->release();
3436 if (res
== kIOReturnSuccess
)
3438 assert( OSDynamicCast(IOUserClient
, client
) );
3440 client
->sharedInstance
= (0 != client
->getProperty(kIOUserClientSharedInstanceKey
));
3441 client
->closed
= false;
3443 disallowAccess
= (crossEndian
3444 && (kOSBooleanTrue
!= service
->getProperty(kIOUserClientCrossEndianCompatibleKey
))
3445 && (kOSBooleanTrue
!= client
->getProperty(kIOUserClientCrossEndianCompatibleKey
)));
3446 if (disallowAccess
) res
= kIOReturnUnsupported
;
3448 else if (0 != mac_iokit_check_open(kauth_cred_get(), client
, connect_type
))
3449 res
= kIOReturnNotPermitted
;
3452 if (kIOReturnSuccess
== res
) res
= client
->registerOwner(owningTask
);
3454 if (kIOReturnSuccess
!= res
)
3456 IOStatisticsClientCall();
3457 client
->clientClose();
3462 OSString
* creatorName
= IOCopyLogNameForPID(proc_selfpid());
3465 client
->setProperty(kIOUserClientCreatorKey
, creatorName
);
3466 creatorName
->release();
3468 client
->setTerminateDefer(service
, false);
3473 *connection
= client
;
3479 /* Routine io_service_close */
3480 kern_return_t
is_io_service_close(
3481 io_object_t connection
)
3484 if ((mappings
= OSDynamicCast(OSSet
, connection
)))
3485 return( kIOReturnSuccess
);
3487 CHECK( IOUserClient
, connection
, client
);
3489 IOStatisticsClientCall();
3491 if (client
->sharedInstance
|| OSCompareAndSwap8(0, 1, &client
->closed
))
3493 client
->clientClose();
3497 IOLog("ignored is_io_service_close(0x%qx,%s)\n",
3498 client
->getRegistryEntryID(), client
->getName());
3501 return( kIOReturnSuccess
);
3504 /* Routine io_connect_get_service */
3505 kern_return_t
is_io_connect_get_service(
3506 io_object_t connection
,
3507 io_object_t
*service
)
3509 IOService
* theService
;
3511 CHECK( IOUserClient
, connection
, client
);
3513 theService
= client
->getService();
3515 theService
->retain();
3517 *service
= theService
;
3519 return( theService
? kIOReturnSuccess
: kIOReturnUnsupported
);
3522 /* Routine io_connect_set_notification_port */
3523 kern_return_t
is_io_connect_set_notification_port(
3524 io_object_t connection
,
3525 uint32_t notification_type
,
3529 CHECK( IOUserClient
, connection
, client
);
3531 IOStatisticsClientCall();
3532 return( client
->registerNotificationPort( port
, notification_type
,
3533 (io_user_reference_t
) reference
));
3536 /* Routine io_connect_set_notification_port */
3537 kern_return_t
is_io_connect_set_notification_port_64(
3538 io_object_t connection
,
3539 uint32_t notification_type
,
3541 io_user_reference_t reference
)
3543 CHECK( IOUserClient
, connection
, client
);
3545 IOStatisticsClientCall();
3546 return( client
->registerNotificationPort( port
, notification_type
,
3550 /* Routine io_connect_map_memory_into_task */
3551 kern_return_t is_io_connect_map_memory_into_task
3553 io_connect_t connection
,
3554 uint32_t memory_type
,
3556 mach_vm_address_t
*address
,
3557 mach_vm_size_t
*size
,
3564 CHECK( IOUserClient
, connection
, client
);
3566 if (!into_task
) return (kIOReturnBadArgument
);
3568 IOStatisticsClientCall();
3569 map
= client
->mapClientMemory64( memory_type
, into_task
, flags
, *address
);
3572 *address
= map
->getAddress();
3574 *size
= map
->getSize();
3576 if( client
->sharedInstance
3577 || (into_task
!= current_task())) {
3578 // push a name out to the task owning the map,
3579 // so we can clean up maps
3580 mach_port_name_t name __unused
=
3581 IOMachPort::makeSendRightForTask(
3582 into_task
, map
, IKOT_IOKIT_OBJECT
);
3585 // keep it with the user client
3586 IOLockLock( gIOObjectPortLock
);
3587 if( 0 == client
->mappings
)
3588 client
->mappings
= OSSet::withCapacity(2);
3589 if( client
->mappings
)
3590 client
->mappings
->setObject( map
);
3591 IOLockUnlock( gIOObjectPortLock
);
3594 err
= kIOReturnSuccess
;
3597 err
= kIOReturnBadArgument
;
3602 /* Routine is_io_connect_map_memory */
3603 kern_return_t
is_io_connect_map_memory(
3604 io_object_t connect
,
3612 mach_vm_address_t address
;
3613 mach_vm_size_t size
;
3615 address
= SCALAR64(*mapAddr
);
3616 size
= SCALAR64(*mapSize
);
3618 err
= is_io_connect_map_memory_into_task(connect
, type
, task
, &address
, &size
, flags
);
3620 *mapAddr
= SCALAR32(address
);
3621 *mapSize
= SCALAR32(size
);
3628 IOMemoryMap
* IOUserClient::removeMappingForDescriptor(IOMemoryDescriptor
* mem
)
3631 IOMemoryMap
* map
= 0;
3633 IOLockLock(gIOObjectPortLock
);
3635 iter
= OSCollectionIterator::withCollection(mappings
);
3638 while ((map
= OSDynamicCast(IOMemoryMap
, iter
->getNextObject())))
3640 if(mem
== map
->getMemoryDescriptor())
3643 mappings
->removeObject(map
);
3650 IOLockUnlock(gIOObjectPortLock
);
3657 /* Routine io_connect_unmap_memory_from_task */
3658 kern_return_t is_io_connect_unmap_memory_from_task
3660 io_connect_t connection
,
3661 uint32_t memory_type
,
3663 mach_vm_address_t address
)
3666 IOOptionBits options
= 0;
3667 IOMemoryDescriptor
* memory
;
3670 CHECK( IOUserClient
, connection
, client
);
3672 if (!from_task
) return (kIOReturnBadArgument
);
3674 IOStatisticsClientCall();
3675 err
= client
->clientMemoryForType( (UInt32
) memory_type
, &options
, &memory
);
3677 if( memory
&& (kIOReturnSuccess
== err
)) {
3679 options
= (options
& ~kIOMapUserOptionsMask
)
3680 | kIOMapAnywhere
| kIOMapReference
;
3682 map
= memory
->createMappingInTask( from_task
, address
, options
);
3686 IOLockLock( gIOObjectPortLock
);
3687 if( client
->mappings
)
3688 client
->mappings
->removeObject( map
);
3689 IOLockUnlock( gIOObjectPortLock
);
3691 mach_port_name_t name
= 0;
3692 if (from_task
!= current_task())
3693 name
= IOMachPort::makeSendRightForTask( from_task
, map
, IKOT_IOKIT_OBJECT
);
3696 map
->userClientUnmap();
3697 err
= iokit_mod_send_right( from_task
, name
, -2 );
3698 err
= kIOReturnSuccess
;
3701 IOMachPort::releasePortForObject( map
, IKOT_IOKIT_OBJECT
);
3702 if (from_task
== current_task())
3706 err
= kIOReturnBadArgument
;
3712 kern_return_t
is_io_connect_unmap_memory(
3713 io_object_t connect
,
3719 mach_vm_address_t address
;
3721 address
= SCALAR64(mapAddr
);
3723 err
= is_io_connect_unmap_memory_from_task(connect
, type
, task
, mapAddr
);
3729 /* Routine io_connect_add_client */
3730 kern_return_t
is_io_connect_add_client(
3731 io_object_t connection
,
3732 io_object_t connect_to
)
3734 CHECK( IOUserClient
, connection
, client
);
3735 CHECK( IOUserClient
, connect_to
, to
);
3737 IOStatisticsClientCall();
3738 return( client
->connectClient( to
) );
3742 /* Routine io_connect_set_properties */
3743 kern_return_t
is_io_connect_set_properties(
3744 io_object_t connection
,
3745 io_buf_ptr_t properties
,
3746 mach_msg_type_number_t propertiesCnt
,
3747 kern_return_t
* result
)
3749 return( is_io_registry_entry_set_properties( connection
, properties
, propertiesCnt
, result
));
3752 /* Routine io_user_client_method */
3753 kern_return_t is_io_connect_method_var_output
3755 io_connect_t connection
,
3757 io_scalar_inband64_t scalar_input
,
3758 mach_msg_type_number_t scalar_inputCnt
,
3759 io_struct_inband_t inband_input
,
3760 mach_msg_type_number_t inband_inputCnt
,
3761 mach_vm_address_t ool_input
,
3762 mach_vm_size_t ool_input_size
,
3763 io_struct_inband_t inband_output
,
3764 mach_msg_type_number_t
*inband_outputCnt
,
3765 io_scalar_inband64_t scalar_output
,
3766 mach_msg_type_number_t
*scalar_outputCnt
,
3767 io_buf_ptr_t
*var_output
,
3768 mach_msg_type_number_t
*var_outputCnt
3771 CHECK( IOUserClient
, connection
, client
);
3773 IOExternalMethodArguments args
;
3775 IOMemoryDescriptor
* inputMD
= 0;
3776 OSObject
* structureVariableOutputData
= 0;
3778 bzero(&args
.__reserved
[0], sizeof(args
.__reserved
));
3779 args
.__reservedA
= 0;
3780 args
.version
= kIOExternalMethodArgumentsCurrentVersion
;
3782 args
.selector
= selector
;
3784 args
.asyncWakePort
= MACH_PORT_NULL
;
3785 args
.asyncReference
= 0;
3786 args
.asyncReferenceCount
= 0;
3787 args
.structureVariableOutputData
= &structureVariableOutputData
;
3789 args
.scalarInput
= scalar_input
;
3790 args
.scalarInputCount
= scalar_inputCnt
;
3791 args
.structureInput
= inband_input
;
3792 args
.structureInputSize
= inband_inputCnt
;
3795 inputMD
= IOMemoryDescriptor::withAddressRange(ool_input
, ool_input_size
,
3796 kIODirectionOut
, current_task());
3798 args
.structureInputDescriptor
= inputMD
;
3800 args
.scalarOutput
= scalar_output
;
3801 args
.scalarOutputCount
= *scalar_outputCnt
;
3802 bzero(&scalar_output
[0], *scalar_outputCnt
* sizeof(scalar_output
[0]));
3803 args
.structureOutput
= inband_output
;
3804 args
.structureOutputSize
= *inband_outputCnt
;
3805 args
.structureOutputDescriptor
= NULL
;
3806 args
.structureOutputDescriptorSize
= 0;
3808 IOStatisticsClientCall();
3809 ret
= client
->externalMethod( selector
, &args
);
3811 *scalar_outputCnt
= args
.scalarOutputCount
;
3812 *inband_outputCnt
= args
.structureOutputSize
;
3814 if (var_outputCnt
&& var_output
&& (kIOReturnSuccess
== ret
))
3816 OSSerialize
* serialize
;
3820 if ((serialize
= OSDynamicCast(OSSerialize
, structureVariableOutputData
)))
3822 len
= serialize
->getLength();
3823 *var_outputCnt
= len
;
3824 ret
= copyoutkdata(serialize
->text(), len
, var_output
);
3826 else if ((data
= OSDynamicCast(OSData
, structureVariableOutputData
)))
3828 len
= data
->getLength();
3829 *var_outputCnt
= len
;
3830 ret
= copyoutkdata(data
->getBytesNoCopy(), len
, var_output
);
3834 ret
= kIOReturnUnderrun
;
3840 if (structureVariableOutputData
)
3841 structureVariableOutputData
->release();
3846 /* Routine io_user_client_method */
3847 kern_return_t is_io_connect_method
3849 io_connect_t connection
,
3851 io_scalar_inband64_t scalar_input
,
3852 mach_msg_type_number_t scalar_inputCnt
,
3853 io_struct_inband_t inband_input
,
3854 mach_msg_type_number_t inband_inputCnt
,
3855 mach_vm_address_t ool_input
,
3856 mach_vm_size_t ool_input_size
,
3857 io_struct_inband_t inband_output
,
3858 mach_msg_type_number_t
*inband_outputCnt
,
3859 io_scalar_inband64_t scalar_output
,
3860 mach_msg_type_number_t
*scalar_outputCnt
,
3861 mach_vm_address_t ool_output
,
3862 mach_vm_size_t
*ool_output_size
3865 CHECK( IOUserClient
, connection
, client
);
3867 IOExternalMethodArguments args
;
3869 IOMemoryDescriptor
* inputMD
= 0;
3870 IOMemoryDescriptor
* outputMD
= 0;
3872 bzero(&args
.__reserved
[0], sizeof(args
.__reserved
));
3873 args
.__reservedA
= 0;
3874 args
.version
= kIOExternalMethodArgumentsCurrentVersion
;
3876 args
.selector
= selector
;
3878 args
.asyncWakePort
= MACH_PORT_NULL
;
3879 args
.asyncReference
= 0;
3880 args
.asyncReferenceCount
= 0;
3881 args
.structureVariableOutputData
= 0;
3883 args
.scalarInput
= scalar_input
;
3884 args
.scalarInputCount
= scalar_inputCnt
;
3885 args
.structureInput
= inband_input
;
3886 args
.structureInputSize
= inband_inputCnt
;
3889 inputMD
= IOMemoryDescriptor::withAddressRange(ool_input
, ool_input_size
,
3890 kIODirectionOut
, current_task());
3892 args
.structureInputDescriptor
= inputMD
;
3894 args
.scalarOutput
= scalar_output
;
3895 args
.scalarOutputCount
= *scalar_outputCnt
;
3896 bzero(&scalar_output
[0], *scalar_outputCnt
* sizeof(scalar_output
[0]));
3897 args
.structureOutput
= inband_output
;
3898 args
.structureOutputSize
= *inband_outputCnt
;
3900 if (ool_output
&& ool_output_size
)
3902 outputMD
= IOMemoryDescriptor::withAddressRange(ool_output
, *ool_output_size
,
3903 kIODirectionIn
, current_task());
3906 args
.structureOutputDescriptor
= outputMD
;
3907 args
.structureOutputDescriptorSize
= ool_output_size
? *ool_output_size
: 0;
3909 IOStatisticsClientCall();
3910 ret
= client
->externalMethod( selector
, &args
);
3912 *scalar_outputCnt
= args
.scalarOutputCount
;
3913 *inband_outputCnt
= args
.structureOutputSize
;
3914 *ool_output_size
= args
.structureOutputDescriptorSize
;
3919 outputMD
->release();
3924 /* Routine io_async_user_client_method */
3925 kern_return_t is_io_connect_async_method
3927 io_connect_t connection
,
3928 mach_port_t wake_port
,
3929 io_async_ref64_t reference
,
3930 mach_msg_type_number_t referenceCnt
,
3932 io_scalar_inband64_t scalar_input
,
3933 mach_msg_type_number_t scalar_inputCnt
,
3934 io_struct_inband_t inband_input
,
3935 mach_msg_type_number_t inband_inputCnt
,
3936 mach_vm_address_t ool_input
,
3937 mach_vm_size_t ool_input_size
,
3938 io_struct_inband_t inband_output
,
3939 mach_msg_type_number_t
*inband_outputCnt
,
3940 io_scalar_inband64_t scalar_output
,
3941 mach_msg_type_number_t
*scalar_outputCnt
,
3942 mach_vm_address_t ool_output
,
3943 mach_vm_size_t
* ool_output_size
3946 CHECK( IOUserClient
, connection
, client
);
3948 IOExternalMethodArguments args
;
3950 IOMemoryDescriptor
* inputMD
= 0;
3951 IOMemoryDescriptor
* outputMD
= 0;
3953 bzero(&args
.__reserved
[0], sizeof(args
.__reserved
));
3954 args
.__reservedA
= 0;
3955 args
.version
= kIOExternalMethodArgumentsCurrentVersion
;
3957 reference
[0] = (io_user_reference_t
) wake_port
;
3958 if (vm_map_is_64bit(get_task_map(current_task())))
3959 reference
[0] |= kIOUCAsync64Flag
;
3961 args
.selector
= selector
;
3963 args
.asyncWakePort
= wake_port
;
3964 args
.asyncReference
= reference
;
3965 args
.asyncReferenceCount
= referenceCnt
;
3967 args
.scalarInput
= scalar_input
;
3968 args
.scalarInputCount
= scalar_inputCnt
;
3969 args
.structureInput
= inband_input
;
3970 args
.structureInputSize
= inband_inputCnt
;
3973 inputMD
= IOMemoryDescriptor::withAddressRange(ool_input
, ool_input_size
,
3974 kIODirectionOut
, current_task());
3976 args
.structureInputDescriptor
= inputMD
;
3978 args
.scalarOutput
= scalar_output
;
3979 args
.scalarOutputCount
= *scalar_outputCnt
;
3980 bzero(&scalar_output
[0], *scalar_outputCnt
* sizeof(scalar_output
[0]));
3981 args
.structureOutput
= inband_output
;
3982 args
.structureOutputSize
= *inband_outputCnt
;
3986 outputMD
= IOMemoryDescriptor::withAddressRange(ool_output
, *ool_output_size
,
3987 kIODirectionIn
, current_task());
3990 args
.structureOutputDescriptor
= outputMD
;
3991 args
.structureOutputDescriptorSize
= *ool_output_size
;
3993 IOStatisticsClientCall();
3994 ret
= client
->externalMethod( selector
, &args
);
3996 *inband_outputCnt
= args
.structureOutputSize
;
3997 *ool_output_size
= args
.structureOutputDescriptorSize
;
4002 outputMD
->release();
4007 /* Routine io_connect_method_scalarI_scalarO */
4008 kern_return_t
is_io_connect_method_scalarI_scalarO(
4009 io_object_t connect
,
4011 io_scalar_inband_t input
,
4012 mach_msg_type_number_t inputCount
,
4013 io_scalar_inband_t output
,
4014 mach_msg_type_number_t
* outputCount
)
4018 io_scalar_inband64_t _input
;
4019 io_scalar_inband64_t _output
;
4021 mach_msg_type_number_t struct_outputCnt
= 0;
4022 mach_vm_size_t ool_output_size
= 0;
4024 bzero(&_output
[0], sizeof(_output
));
4025 for (i
= 0; i
< inputCount
; i
++)
4026 _input
[i
] = SCALAR64(input
[i
]);
4028 err
= is_io_connect_method(connect
, index
,
4032 NULL
, &struct_outputCnt
,
4033 _output
, outputCount
,
4034 0, &ool_output_size
);
4036 for (i
= 0; i
< *outputCount
; i
++)
4037 output
[i
] = SCALAR32(_output
[i
]);
4042 kern_return_t
shim_io_connect_method_scalarI_scalarO(
4043 IOExternalMethod
* method
,
4045 const io_user_scalar_t
* input
,
4046 mach_msg_type_number_t inputCount
,
4047 io_user_scalar_t
* output
,
4048 mach_msg_type_number_t
* outputCount
)
4051 io_scalar_inband_t _output
;
4053 err
= kIOReturnBadArgument
;
4055 bzero(&_output
[0], sizeof(_output
));
4058 if( inputCount
!= method
->count0
)
4060 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
);
4061 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4064 if( *outputCount
!= method
->count1
)
4066 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)*outputCount
, (uint64_t)method
->count1
);
4067 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)*outputCount
, uint64_t, (uint64_t)method
->count1
);
4071 func
= method
->func
;
4073 switch( inputCount
) {
4076 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4077 ARG32(input
[3]), ARG32(input
[4]), ARG32(input
[5]) );
4080 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4081 ARG32(input
[3]), ARG32(input
[4]),
4085 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4087 &_output
[0], &_output
[1] );
4090 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4091 &_output
[0], &_output
[1], &_output
[2] );
4094 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]),
4095 &_output
[0], &_output
[1], &_output
[2],
4099 err
= (object
->*func
)( ARG32(input
[0]),
4100 &_output
[0], &_output
[1], &_output
[2],
4101 &_output
[3], &_output
[4] );
4104 err
= (object
->*func
)( &_output
[0], &_output
[1], &_output
[2],
4105 &_output
[3], &_output
[4], &_output
[5] );
4109 IOLog("%s: Bad method table\n", object
->getName());
4115 for (i
= 0; i
< *outputCount
; i
++)
4116 output
[i
] = SCALAR32(_output
[i
]);
4121 /* Routine io_async_method_scalarI_scalarO */
4122 kern_return_t
is_io_async_method_scalarI_scalarO(
4123 io_object_t connect
,
4124 mach_port_t wake_port
,
4125 io_async_ref_t reference
,
4126 mach_msg_type_number_t referenceCnt
,
4128 io_scalar_inband_t input
,
4129 mach_msg_type_number_t inputCount
,
4130 io_scalar_inband_t output
,
4131 mach_msg_type_number_t
* outputCount
)
4135 io_scalar_inband64_t _input
;
4136 io_scalar_inband64_t _output
;
4137 io_async_ref64_t _reference
;
4139 bzero(&_output
[0], sizeof(_output
));
4140 for (i
= 0; i
< referenceCnt
; i
++)
4141 _reference
[i
] = REF64(reference
[i
]);
4143 mach_msg_type_number_t struct_outputCnt
= 0;
4144 mach_vm_size_t ool_output_size
= 0;
4146 for (i
= 0; i
< inputCount
; i
++)
4147 _input
[i
] = SCALAR64(input
[i
]);
4149 err
= is_io_connect_async_method(connect
,
4150 wake_port
, _reference
, referenceCnt
,
4155 NULL
, &struct_outputCnt
,
4156 _output
, outputCount
,
4157 0, &ool_output_size
);
4159 for (i
= 0; i
< *outputCount
; i
++)
4160 output
[i
] = SCALAR32(_output
[i
]);
4164 /* Routine io_async_method_scalarI_structureO */
4165 kern_return_t
is_io_async_method_scalarI_structureO(
4166 io_object_t connect
,
4167 mach_port_t wake_port
,
4168 io_async_ref_t reference
,
4169 mach_msg_type_number_t referenceCnt
,
4171 io_scalar_inband_t input
,
4172 mach_msg_type_number_t inputCount
,
4173 io_struct_inband_t output
,
4174 mach_msg_type_number_t
* outputCount
)
4177 io_scalar_inband64_t _input
;
4178 io_async_ref64_t _reference
;
4180 for (i
= 0; i
< referenceCnt
; i
++)
4181 _reference
[i
] = REF64(reference
[i
]);
4183 mach_msg_type_number_t scalar_outputCnt
= 0;
4184 mach_vm_size_t ool_output_size
= 0;
4186 for (i
= 0; i
< inputCount
; i
++)
4187 _input
[i
] = SCALAR64(input
[i
]);
4189 return (is_io_connect_async_method(connect
,
4190 wake_port
, _reference
, referenceCnt
,
4195 output
, outputCount
,
4196 NULL
, &scalar_outputCnt
,
4197 0, &ool_output_size
));
4200 /* Routine io_async_method_scalarI_structureI */
4201 kern_return_t
is_io_async_method_scalarI_structureI(
4202 io_connect_t connect
,
4203 mach_port_t wake_port
,
4204 io_async_ref_t reference
,
4205 mach_msg_type_number_t referenceCnt
,
4207 io_scalar_inband_t input
,
4208 mach_msg_type_number_t inputCount
,
4209 io_struct_inband_t inputStruct
,
4210 mach_msg_type_number_t inputStructCount
)
4213 io_scalar_inband64_t _input
;
4214 io_async_ref64_t _reference
;
4216 for (i
= 0; i
< referenceCnt
; i
++)
4217 _reference
[i
] = REF64(reference
[i
]);
4219 mach_msg_type_number_t scalar_outputCnt
= 0;
4220 mach_msg_type_number_t inband_outputCnt
= 0;
4221 mach_vm_size_t ool_output_size
= 0;
4223 for (i
= 0; i
< inputCount
; i
++)
4224 _input
[i
] = SCALAR64(input
[i
]);
4226 return (is_io_connect_async_method(connect
,
4227 wake_port
, _reference
, referenceCnt
,
4230 inputStruct
, inputStructCount
,
4232 NULL
, &inband_outputCnt
,
4233 NULL
, &scalar_outputCnt
,
4234 0, &ool_output_size
));
4237 /* Routine io_async_method_structureI_structureO */
4238 kern_return_t
is_io_async_method_structureI_structureO(
4239 io_object_t connect
,
4240 mach_port_t wake_port
,
4241 io_async_ref_t reference
,
4242 mach_msg_type_number_t referenceCnt
,
4244 io_struct_inband_t input
,
4245 mach_msg_type_number_t inputCount
,
4246 io_struct_inband_t output
,
4247 mach_msg_type_number_t
* outputCount
)
4250 mach_msg_type_number_t scalar_outputCnt
= 0;
4251 mach_vm_size_t ool_output_size
= 0;
4252 io_async_ref64_t _reference
;
4254 for (i
= 0; i
< referenceCnt
; i
++)
4255 _reference
[i
] = REF64(reference
[i
]);
4257 return (is_io_connect_async_method(connect
,
4258 wake_port
, _reference
, referenceCnt
,
4263 output
, outputCount
,
4264 NULL
, &scalar_outputCnt
,
4265 0, &ool_output_size
));
4269 kern_return_t
shim_io_async_method_scalarI_scalarO(
4270 IOExternalAsyncMethod
* method
,
4272 mach_port_t asyncWakePort
,
4273 io_user_reference_t
* asyncReference
,
4274 uint32_t asyncReferenceCount
,
4275 const io_user_scalar_t
* input
,
4276 mach_msg_type_number_t inputCount
,
4277 io_user_scalar_t
* output
,
4278 mach_msg_type_number_t
* outputCount
)
4282 io_scalar_inband_t _output
;
4284 io_async_ref_t reference
;
4286 bzero(&_output
[0], sizeof(_output
));
4287 for (i
= 0; i
< asyncReferenceCount
; i
++)
4288 reference
[i
] = REF32(asyncReference
[i
]);
4290 err
= kIOReturnBadArgument
;
4294 if( inputCount
!= method
->count0
)
4296 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
);
4297 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4300 if( *outputCount
!= method
->count1
)
4302 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)*outputCount
, (uint64_t)method
->count1
);
4303 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)*outputCount
, uint64_t, (uint64_t)method
->count1
);
4307 func
= method
->func
;
4309 switch( inputCount
) {
4312 err
= (object
->*func
)( reference
,
4313 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4314 ARG32(input
[3]), ARG32(input
[4]), ARG32(input
[5]) );
4317 err
= (object
->*func
)( reference
,
4318 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4319 ARG32(input
[3]), ARG32(input
[4]),
4323 err
= (object
->*func
)( reference
,
4324 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4326 &_output
[0], &_output
[1] );
4329 err
= (object
->*func
)( reference
,
4330 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4331 &_output
[0], &_output
[1], &_output
[2] );
4334 err
= (object
->*func
)( reference
,
4335 ARG32(input
[0]), ARG32(input
[1]),
4336 &_output
[0], &_output
[1], &_output
[2],
4340 err
= (object
->*func
)( reference
,
4342 &_output
[0], &_output
[1], &_output
[2],
4343 &_output
[3], &_output
[4] );
4346 err
= (object
->*func
)( reference
,
4347 &_output
[0], &_output
[1], &_output
[2],
4348 &_output
[3], &_output
[4], &_output
[5] );
4352 IOLog("%s: Bad method table\n", object
->getName());
4357 for (i
= 0; i
< *outputCount
; i
++)
4358 output
[i
] = SCALAR32(_output
[i
]);
4364 /* Routine io_connect_method_scalarI_structureO */
4365 kern_return_t
is_io_connect_method_scalarI_structureO(
4366 io_object_t connect
,
4368 io_scalar_inband_t input
,
4369 mach_msg_type_number_t inputCount
,
4370 io_struct_inband_t output
,
4371 mach_msg_type_number_t
* outputCount
)
4374 io_scalar_inband64_t _input
;
4376 mach_msg_type_number_t scalar_outputCnt
= 0;
4377 mach_vm_size_t ool_output_size
= 0;
4379 for (i
= 0; i
< inputCount
; i
++)
4380 _input
[i
] = SCALAR64(input
[i
]);
4382 return (is_io_connect_method(connect
, index
,
4386 output
, outputCount
,
4387 NULL
, &scalar_outputCnt
,
4388 0, &ool_output_size
));
4391 kern_return_t
shim_io_connect_method_scalarI_structureO(
4393 IOExternalMethod
* method
,
4395 const io_user_scalar_t
* input
,
4396 mach_msg_type_number_t inputCount
,
4397 io_struct_inband_t output
,
4398 IOByteCount
* outputCount
)
4403 err
= kIOReturnBadArgument
;
4406 if( inputCount
!= method
->count0
)
4408 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
);
4409 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4412 if( (kIOUCVariableStructureSize
!= method
->count1
)
4413 && (*outputCount
!= method
->count1
))
4415 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)*outputCount
, (uint64_t)method
->count1
, (uint64_t)kIOUCVariableStructureSize
);
4416 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)*outputCount
, uint64_t, (uint64_t)method
->count1
);
4420 func
= method
->func
;
4422 switch( inputCount
) {
4425 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4426 ARG32(input
[3]), ARG32(input
[4]),
4430 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4432 output
, (void *)outputCount
);
4435 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4436 output
, (void *)outputCount
, 0 );
4439 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]),
4440 output
, (void *)outputCount
, 0, 0 );
4443 err
= (object
->*func
)( ARG32(input
[0]),
4444 output
, (void *)outputCount
, 0, 0, 0 );
4447 err
= (object
->*func
)( output
, (void *)outputCount
, 0, 0, 0, 0 );
4451 IOLog("%s: Bad method table\n", object
->getName());
4460 kern_return_t
shim_io_async_method_scalarI_structureO(
4461 IOExternalAsyncMethod
* method
,
4463 mach_port_t asyncWakePort
,
4464 io_user_reference_t
* asyncReference
,
4465 uint32_t asyncReferenceCount
,
4466 const io_user_scalar_t
* input
,
4467 mach_msg_type_number_t inputCount
,
4468 io_struct_inband_t output
,
4469 mach_msg_type_number_t
* outputCount
)
4474 io_async_ref_t reference
;
4476 for (i
= 0; i
< asyncReferenceCount
; i
++)
4477 reference
[i
] = REF32(asyncReference
[i
]);
4479 err
= kIOReturnBadArgument
;
4481 if( inputCount
!= method
->count0
)
4483 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
);
4484 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4487 if( (kIOUCVariableStructureSize
!= method
->count1
)
4488 && (*outputCount
!= method
->count1
))
4490 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)*outputCount
, (uint64_t)method
->count1
, (uint64_t)kIOUCVariableStructureSize
);
4491 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)*outputCount
, uint64_t, (uint64_t)method
->count1
);
4495 func
= method
->func
;
4497 switch( inputCount
) {
4500 err
= (object
->*func
)( reference
,
4501 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4502 ARG32(input
[3]), ARG32(input
[4]),
4506 err
= (object
->*func
)( reference
,
4507 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4509 output
, (void *)outputCount
);
4512 err
= (object
->*func
)( reference
,
4513 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4514 output
, (void *)outputCount
, 0 );
4517 err
= (object
->*func
)( reference
,
4518 ARG32(input
[0]), ARG32(input
[1]),
4519 output
, (void *)outputCount
, 0, 0 );
4522 err
= (object
->*func
)( reference
,
4524 output
, (void *)outputCount
, 0, 0, 0 );
4527 err
= (object
->*func
)( reference
,
4528 output
, (void *)outputCount
, 0, 0, 0, 0 );
4532 IOLog("%s: Bad method table\n", object
->getName());
4540 /* Routine io_connect_method_scalarI_structureI */
4541 kern_return_t
is_io_connect_method_scalarI_structureI(
4542 io_connect_t connect
,
4544 io_scalar_inband_t input
,
4545 mach_msg_type_number_t inputCount
,
4546 io_struct_inband_t inputStruct
,
4547 mach_msg_type_number_t inputStructCount
)
4550 io_scalar_inband64_t _input
;
4552 mach_msg_type_number_t scalar_outputCnt
= 0;
4553 mach_msg_type_number_t inband_outputCnt
= 0;
4554 mach_vm_size_t ool_output_size
= 0;
4556 for (i
= 0; i
< inputCount
; i
++)
4557 _input
[i
] = SCALAR64(input
[i
]);
4559 return (is_io_connect_method(connect
, index
,
4561 inputStruct
, inputStructCount
,
4563 NULL
, &inband_outputCnt
,
4564 NULL
, &scalar_outputCnt
,
4565 0, &ool_output_size
));
4568 kern_return_t
shim_io_connect_method_scalarI_structureI(
4569 IOExternalMethod
* method
,
4571 const io_user_scalar_t
* input
,
4572 mach_msg_type_number_t inputCount
,
4573 io_struct_inband_t inputStruct
,
4574 mach_msg_type_number_t inputStructCount
)
4577 IOReturn err
= kIOReturnBadArgument
;
4581 if (inputCount
!= method
->count0
)
4583 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
);
4584 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4587 if( (kIOUCVariableStructureSize
!= method
->count1
)
4588 && (inputStructCount
!= method
->count1
))
4590 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputStructCount
, (uint64_t)method
->count1
, (uint64_t)kIOUCVariableStructureSize
);
4591 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputStructCount
, uint64_t, (uint64_t)method
->count1
);
4595 func
= method
->func
;
4597 switch( inputCount
) {
4600 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4601 ARG32(input
[3]), ARG32(input
[4]),
4605 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), (void *) input
[2],
4607 inputStruct
, (void *)(uintptr_t)inputStructCount
);
4610 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4611 inputStruct
, (void *)(uintptr_t)inputStructCount
,
4615 err
= (object
->*func
)( ARG32(input
[0]), ARG32(input
[1]),
4616 inputStruct
, (void *)(uintptr_t)inputStructCount
,
4620 err
= (object
->*func
)( ARG32(input
[0]),
4621 inputStruct
, (void *)(uintptr_t)inputStructCount
,
4625 err
= (object
->*func
)( inputStruct
, (void *)(uintptr_t)inputStructCount
,
4630 IOLog("%s: Bad method table\n", object
->getName());
4638 kern_return_t
shim_io_async_method_scalarI_structureI(
4639 IOExternalAsyncMethod
* method
,
4641 mach_port_t asyncWakePort
,
4642 io_user_reference_t
* asyncReference
,
4643 uint32_t asyncReferenceCount
,
4644 const io_user_scalar_t
* input
,
4645 mach_msg_type_number_t inputCount
,
4646 io_struct_inband_t inputStruct
,
4647 mach_msg_type_number_t inputStructCount
)
4651 IOReturn err
= kIOReturnBadArgument
;
4652 io_async_ref_t reference
;
4654 for (i
= 0; i
< asyncReferenceCount
; i
++)
4655 reference
[i
] = REF32(asyncReference
[i
]);
4659 if (inputCount
!= method
->count0
)
4661 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
);
4662 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4665 if( (kIOUCVariableStructureSize
!= method
->count1
)
4666 && (inputStructCount
!= method
->count1
))
4668 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputStructCount
, (uint64_t)method
->count1
, (uint64_t)kIOUCVariableStructureSize
);
4669 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputStructCount
, uint64_t, (uint64_t)method
->count1
);
4673 func
= method
->func
;
4675 switch( inputCount
) {
4678 err
= (object
->*func
)( reference
,
4679 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4680 ARG32(input
[3]), ARG32(input
[4]),
4684 err
= (object
->*func
)( reference
,
4685 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4687 inputStruct
, (void *)(uintptr_t)inputStructCount
);
4690 err
= (object
->*func
)( reference
,
4691 ARG32(input
[0]), ARG32(input
[1]), ARG32(input
[2]),
4692 inputStruct
, (void *)(uintptr_t)inputStructCount
,
4696 err
= (object
->*func
)( reference
,
4697 ARG32(input
[0]), ARG32(input
[1]),
4698 inputStruct
, (void *)(uintptr_t)inputStructCount
,
4702 err
= (object
->*func
)( reference
,
4704 inputStruct
, (void *)(uintptr_t)inputStructCount
,
4708 err
= (object
->*func
)( reference
,
4709 inputStruct
, (void *)(uintptr_t)inputStructCount
,
4714 IOLog("%s: Bad method table\n", object
->getName());
4722 /* Routine io_connect_method_structureI_structureO */
4723 kern_return_t
is_io_connect_method_structureI_structureO(
4724 io_object_t connect
,
4726 io_struct_inband_t input
,
4727 mach_msg_type_number_t inputCount
,
4728 io_struct_inband_t output
,
4729 mach_msg_type_number_t
* outputCount
)
4731 mach_msg_type_number_t scalar_outputCnt
= 0;
4732 mach_vm_size_t ool_output_size
= 0;
4734 return (is_io_connect_method(connect
, index
,
4738 output
, outputCount
,
4739 NULL
, &scalar_outputCnt
,
4740 0, &ool_output_size
));
4743 kern_return_t
shim_io_connect_method_structureI_structureO(
4744 IOExternalMethod
* method
,
4746 io_struct_inband_t input
,
4747 mach_msg_type_number_t inputCount
,
4748 io_struct_inband_t output
,
4749 IOByteCount
* outputCount
)
4752 IOReturn err
= kIOReturnBadArgument
;
4756 if( (kIOUCVariableStructureSize
!= method
->count0
)
4757 && (inputCount
!= method
->count0
))
4759 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
, (uint64_t)kIOUCVariableStructureSize
);
4760 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4763 if( (kIOUCVariableStructureSize
!= method
->count1
)
4764 && (*outputCount
!= method
->count1
))
4766 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)*outputCount
, (uint64_t)method
->count1
, (uint64_t)kIOUCVariableStructureSize
);
4767 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)*outputCount
, uint64_t, (uint64_t)method
->count1
);
4771 func
= method
->func
;
4773 if( method
->count1
) {
4774 if( method
->count0
) {
4775 err
= (object
->*func
)( input
, output
,
4776 (void *)(uintptr_t)inputCount
, outputCount
, 0, 0 );
4778 err
= (object
->*func
)( output
, outputCount
, 0, 0, 0, 0 );
4781 err
= (object
->*func
)( input
, (void *)(uintptr_t)inputCount
, 0, 0, 0, 0 );
4790 kern_return_t
shim_io_async_method_structureI_structureO(
4791 IOExternalAsyncMethod
* method
,
4793 mach_port_t asyncWakePort
,
4794 io_user_reference_t
* asyncReference
,
4795 uint32_t asyncReferenceCount
,
4796 io_struct_inband_t input
,
4797 mach_msg_type_number_t inputCount
,
4798 io_struct_inband_t output
,
4799 mach_msg_type_number_t
* outputCount
)
4804 io_async_ref_t reference
;
4806 for (i
= 0; i
< asyncReferenceCount
; i
++)
4807 reference
[i
] = REF32(asyncReference
[i
]);
4809 err
= kIOReturnBadArgument
;
4812 if( (kIOUCVariableStructureSize
!= method
->count0
)
4813 && (inputCount
!= method
->count0
))
4815 IOLog("%s:%d %s: IOUserClient inputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)inputCount
, (uint64_t)method
->count0
, (uint64_t)kIOUCVariableStructureSize
);
4816 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)inputCount
, uint64_t, (uint64_t)method
->count0
);
4819 if( (kIOUCVariableStructureSize
!= method
->count1
)
4820 && (*outputCount
!= method
->count1
))
4822 IOLog("%s:%d %s: IOUserClient outputCount count mismatch 0x%llx 0x%llx 0x%llx\n", __FUNCTION__
, __LINE__
, object
->getName(), (uint64_t)*outputCount
, (uint64_t)method
->count1
, (uint64_t)kIOUCVariableStructureSize
);
4823 DTRACE_IO2(iokit_count_mismatch
, uint64_t, (uint64_t)*outputCount
, uint64_t, (uint64_t)method
->count1
);
4827 func
= method
->func
;
4829 if( method
->count1
) {
4830 if( method
->count0
) {
4831 err
= (object
->*func
)( reference
,
4833 (void *)(uintptr_t)inputCount
, outputCount
, 0, 0 );
4835 err
= (object
->*func
)( reference
,
4836 output
, outputCount
, 0, 0, 0, 0 );
4839 err
= (object
->*func
)( reference
,
4840 input
, (void *)(uintptr_t)inputCount
, 0, 0, 0, 0 );
4849 bool gIOKextdClearedBusy
= false;
4852 /* Routine io_catalog_send_data */
4853 kern_return_t
is_io_catalog_send_data(
4854 mach_port_t master_port
,
4856 io_buf_ptr_t inData
,
4857 mach_msg_type_number_t inDataCount
,
4858 kern_return_t
* result
)
4862 kern_return_t kr
= kIOReturnError
;
4864 //printf("io_catalog_send_data called. flag: %d\n", flag);
4866 if( master_port
!= master_device_port
)
4867 return kIOReturnNotPrivileged
;
4869 if( (flag
!= kIOCatalogRemoveKernelLinker
&&
4870 flag
!= kIOCatalogKextdActive
&&
4871 flag
!= kIOCatalogKextdFinishedLaunching
) &&
4872 ( !inData
|| !inDataCount
) )
4874 return kIOReturnBadArgument
;
4878 vm_map_offset_t map_data
;
4880 if( inDataCount
> sizeof(io_struct_inband_t
) * 1024)
4881 return( kIOReturnMessageTooLarge
);
4883 kr
= vm_map_copyout( kernel_map
, &map_data
, (vm_map_copy_t
)inData
);
4884 data
= CAST_DOWN(vm_offset_t
, map_data
);
4886 if( kr
!= KERN_SUCCESS
)
4889 // must return success after vm_map_copyout() succeeds
4892 obj
= (OSObject
*)OSUnserializeXML((const char *)data
, inDataCount
);
4893 vm_deallocate( kernel_map
, data
, inDataCount
);
4895 *result
= kIOReturnNoMemory
;
4896 return( KERN_SUCCESS
);
4902 case kIOCatalogResetDrivers
:
4903 case kIOCatalogResetDriversNoMatch
: {
4906 array
= OSDynamicCast(OSArray
, obj
);
4908 if ( !gIOCatalogue
->resetAndAddDrivers(array
,
4909 flag
== kIOCatalogResetDrivers
) ) {
4911 kr
= kIOReturnError
;
4914 kr
= kIOReturnBadArgument
;
4919 case kIOCatalogAddDrivers
:
4920 case kIOCatalogAddDriversNoMatch
: {
4923 array
= OSDynamicCast(OSArray
, obj
);
4925 if ( !gIOCatalogue
->addDrivers( array
,
4926 flag
== kIOCatalogAddDrivers
) ) {
4927 kr
= kIOReturnError
;
4931 kr
= kIOReturnBadArgument
;
4936 case kIOCatalogRemoveDrivers
:
4937 case kIOCatalogRemoveDriversNoMatch
: {
4938 OSDictionary
* dict
;
4940 dict
= OSDynamicCast(OSDictionary
, obj
);
4942 if ( !gIOCatalogue
->removeDrivers( dict
,
4943 flag
== kIOCatalogRemoveDrivers
) ) {
4944 kr
= kIOReturnError
;
4948 kr
= kIOReturnBadArgument
;
4953 case kIOCatalogStartMatching
: {
4954 OSDictionary
* dict
;
4956 dict
= OSDynamicCast(OSDictionary
, obj
);
4958 if ( !gIOCatalogue
->startMatching( dict
) ) {
4959 kr
= kIOReturnError
;
4963 kr
= kIOReturnBadArgument
;
4968 case kIOCatalogRemoveKernelLinker
:
4969 kr
= KERN_NOT_SUPPORTED
;
4972 case kIOCatalogKextdActive
:
4974 IOServiceTrace(IOSERVICE_KEXTD_ALIVE
, 0, 0, 0, 0);
4975 OSKext::setKextdActive();
4977 /* Dump all nonloaded startup extensions; kextd will now send them
4980 OSKext::flushNonloadedKexts( /* flushPrelinkedKexts */ false);
4982 kr
= kIOReturnSuccess
;
4985 case kIOCatalogKextdFinishedLaunching
: {
4987 if (!gIOKextdClearedBusy
) {
4988 IOService
* serviceRoot
= IOService::getServiceRoot();
4990 IOServiceTrace(IOSERVICE_KEXTD_READY
, 0, 0, 0, 0);
4991 serviceRoot
->adjustBusy(-1);
4992 gIOKextdClearedBusy
= true;
4996 kr
= kIOReturnSuccess
;
5001 kr
= kIOReturnBadArgument
;
5005 if (obj
) obj
->release();
5008 return( KERN_SUCCESS
);
5011 /* Routine io_catalog_terminate */
5012 kern_return_t
is_io_catalog_terminate(
5013 mach_port_t master_port
,
5019 if( master_port
!= master_device_port
)
5020 return kIOReturnNotPrivileged
;
5022 kr
= IOUserClient::clientHasPrivilege( (void *) current_task(),
5023 kIOClientPrivilegeAdministrator
);
5024 if( kIOReturnSuccess
!= kr
)
5028 #if !defined(SECURE_KERNEL)
5029 case kIOCatalogServiceTerminate
:
5031 IOService
* service
;
5033 iter
= IORegistryIterator::iterateOver(gIOServicePlane
,
5034 kIORegistryIterateRecursively
);
5036 return kIOReturnNoMemory
;
5040 while( (service
= (IOService
*)iter
->getNextObject()) ) {
5041 if( service
->metaCast(name
)) {
5042 if ( !service
->terminate( kIOServiceRequired
5043 | kIOServiceSynchronous
) ) {
5044 kr
= kIOReturnUnsupported
;
5049 } while( !service
&& !iter
->isValid());
5053 case kIOCatalogModuleUnload
:
5054 case kIOCatalogModuleTerminate
:
5055 kr
= gIOCatalogue
->terminateDriversForModule(name
,
5056 flag
== kIOCatalogModuleUnload
);
5061 kr
= kIOReturnBadArgument
;
5068 /* Routine io_catalog_get_data */
5069 kern_return_t
is_io_catalog_get_data(
5070 mach_port_t master_port
,
5072 io_buf_ptr_t
*outData
,
5073 mach_msg_type_number_t
*outDataCount
)
5075 kern_return_t kr
= kIOReturnSuccess
;
5078 if( master_port
!= master_device_port
)
5079 return kIOReturnNotPrivileged
;
5081 //printf("io_catalog_get_data called. flag: %d\n", flag);
5083 s
= OSSerialize::withCapacity(4096);
5085 return kIOReturnNoMemory
;
5087 kr
= gIOCatalogue
->serializeData(flag
, s
);
5089 if ( kr
== kIOReturnSuccess
) {
5094 size
= s
->getLength();
5095 kr
= vm_allocate(kernel_map
, &data
, size
, VM_FLAGS_ANYWHERE
);
5096 if ( kr
== kIOReturnSuccess
) {
5097 bcopy(s
->text(), (void *)data
, size
);
5098 kr
= vm_map_copyin(kernel_map
, (vm_map_address_t
)data
,
5099 (vm_map_size_t
)size
, true, ©
);
5100 *outData
= (char *)copy
;
5101 *outDataCount
= size
;
5110 /* Routine io_catalog_get_gen_count */
5111 kern_return_t
is_io_catalog_get_gen_count(
5112 mach_port_t master_port
,
5115 if( master_port
!= master_device_port
)
5116 return kIOReturnNotPrivileged
;
5118 //printf("io_catalog_get_gen_count called.\n");
5121 return kIOReturnBadArgument
;
5123 *genCount
= gIOCatalogue
->getGenerationCount();
5125 return kIOReturnSuccess
;
5128 /* Routine io_catalog_module_loaded.
5129 * Is invoked from IOKitLib's IOCatalogueModuleLoaded(). Doesn't seem to be used.
5131 kern_return_t
is_io_catalog_module_loaded(
5132 mach_port_t master_port
,
5135 if( master_port
!= master_device_port
)
5136 return kIOReturnNotPrivileged
;
5138 //printf("io_catalog_module_loaded called. name %s\n", name);
5141 return kIOReturnBadArgument
;
5143 gIOCatalogue
->moduleHasLoaded(name
);
5145 return kIOReturnSuccess
;
5148 kern_return_t
is_io_catalog_reset(
5149 mach_port_t master_port
,
5152 if( master_port
!= master_device_port
)
5153 return kIOReturnNotPrivileged
;
5156 case kIOCatalogResetDefault
:
5157 gIOCatalogue
->reset();
5161 return kIOReturnBadArgument
;
5164 return kIOReturnSuccess
;
5167 kern_return_t
iokit_user_client_trap(struct iokit_user_client_trap_args
*args
)
5169 kern_return_t result
= kIOReturnBadArgument
;
5170 IOUserClient
*userClient
;
5172 if ((userClient
= OSDynamicCast(IOUserClient
,
5173 iokit_lookup_connect_ref_current_task((OSObject
*)(args
->userClientRef
))))) {
5174 IOExternalTrap
*trap
;
5175 IOService
*target
= NULL
;
5177 trap
= userClient
->getTargetAndTrapForIndex(&target
, args
->index
);
5179 if (trap
&& target
) {
5185 result
= (target
->*func
)(args
->p1
, args
->p2
, args
->p3
, args
->p4
, args
->p5
, args
->p6
);
5189 iokit_remove_connect_reference(userClient
);
5197 IOReturn
IOUserClient::externalMethod( uint32_t selector
, IOExternalMethodArguments
* args
,
5198 IOExternalMethodDispatch
* dispatch
, OSObject
* target
, void * reference
)
5202 IOByteCount structureOutputSize
;
5207 count
= dispatch
->checkScalarInputCount
;
5208 if ((kIOUCVariableStructureSize
!= count
) && (count
!= args
->scalarInputCount
))
5210 return (kIOReturnBadArgument
);
5213 count
= dispatch
->checkStructureInputSize
;
5214 if ((kIOUCVariableStructureSize
!= count
)
5215 && (count
!= ((args
->structureInputDescriptor
)
5216 ? args
->structureInputDescriptor
->getLength() : args
->structureInputSize
)))
5218 return (kIOReturnBadArgument
);
5221 count
= dispatch
->checkScalarOutputCount
;
5222 if ((kIOUCVariableStructureSize
!= count
) && (count
!= args
->scalarOutputCount
))
5224 return (kIOReturnBadArgument
);
5227 count
= dispatch
->checkStructureOutputSize
;
5228 if ((kIOUCVariableStructureSize
!= count
)
5229 && (count
!= ((args
->structureOutputDescriptor
)
5230 ? args
->structureOutputDescriptor
->getLength() : args
->structureOutputSize
)))
5232 return (kIOReturnBadArgument
);
5235 if (dispatch
->function
)
5236 err
= (*dispatch
->function
)(target
, reference
, args
);
5238 err
= kIOReturnNoCompletion
; /* implementator can dispatch */
5244 // pre-Leopard API's don't do ool structs
5245 if (args
->structureInputDescriptor
|| args
->structureOutputDescriptor
)
5247 err
= kIOReturnIPCError
;
5251 structureOutputSize
= args
->structureOutputSize
;
5253 if (args
->asyncWakePort
)
5255 IOExternalAsyncMethod
* method
;
5257 if( !(method
= getAsyncTargetAndMethodForIndex(&object
, selector
)) || !object
)
5258 return (kIOReturnUnsupported
);
5260 if (kIOUCForegroundOnly
& method
->flags
)
5262 if (task_is_gpu_denied(current_task()))
5263 return (kIOReturnNotPermitted
);
5266 switch (method
->flags
& kIOUCTypeMask
)
5268 case kIOUCScalarIStructI
:
5269 err
= shim_io_async_method_scalarI_structureI( method
, object
,
5270 args
->asyncWakePort
, args
->asyncReference
, args
->asyncReferenceCount
,
5271 args
->scalarInput
, args
->scalarInputCount
,
5272 (char *)args
->structureInput
, args
->structureInputSize
);
5275 case kIOUCScalarIScalarO
:
5276 err
= shim_io_async_method_scalarI_scalarO( method
, object
,
5277 args
->asyncWakePort
, args
->asyncReference
, args
->asyncReferenceCount
,
5278 args
->scalarInput
, args
->scalarInputCount
,
5279 args
->scalarOutput
, &args
->scalarOutputCount
);
5282 case kIOUCScalarIStructO
:
5283 err
= shim_io_async_method_scalarI_structureO( method
, object
,
5284 args
->asyncWakePort
, args
->asyncReference
, args
->asyncReferenceCount
,
5285 args
->scalarInput
, args
->scalarInputCount
,
5286 (char *) args
->structureOutput
, &args
->structureOutputSize
);
5290 case kIOUCStructIStructO
:
5291 err
= shim_io_async_method_structureI_structureO( method
, object
,
5292 args
->asyncWakePort
, args
->asyncReference
, args
->asyncReferenceCount
,
5293 (char *)args
->structureInput
, args
->structureInputSize
,
5294 (char *) args
->structureOutput
, &args
->structureOutputSize
);
5298 err
= kIOReturnBadArgument
;
5304 IOExternalMethod
* method
;
5306 if( !(method
= getTargetAndMethodForIndex(&object
, selector
)) || !object
)
5307 return (kIOReturnUnsupported
);
5309 if (kIOUCForegroundOnly
& method
->flags
)
5311 if (task_is_gpu_denied(current_task()))
5312 return (kIOReturnNotPermitted
);
5315 switch (method
->flags
& kIOUCTypeMask
)
5317 case kIOUCScalarIStructI
:
5318 err
= shim_io_connect_method_scalarI_structureI( method
, object
,
5319 args
->scalarInput
, args
->scalarInputCount
,
5320 (char *) args
->structureInput
, args
->structureInputSize
);
5323 case kIOUCScalarIScalarO
:
5324 err
= shim_io_connect_method_scalarI_scalarO( method
, object
,
5325 args
->scalarInput
, args
->scalarInputCount
,
5326 args
->scalarOutput
, &args
->scalarOutputCount
);
5329 case kIOUCScalarIStructO
:
5330 err
= shim_io_connect_method_scalarI_structureO( method
, object
,
5331 args
->scalarInput
, args
->scalarInputCount
,
5332 (char *) args
->structureOutput
, &structureOutputSize
);
5336 case kIOUCStructIStructO
:
5337 err
= shim_io_connect_method_structureI_structureO( method
, object
,
5338 (char *) args
->structureInput
, args
->structureInputSize
,
5339 (char *) args
->structureOutput
, &structureOutputSize
);
5343 err
= kIOReturnBadArgument
;
5348 args
->structureOutputSize
= structureOutputSize
;
5354 OSMetaClassDefineReservedUnused(IOUserClient
, 0);
5355 OSMetaClassDefineReservedUnused(IOUserClient
, 1);
5357 OSMetaClassDefineReservedUsed(IOUserClient
, 0);
5358 OSMetaClassDefineReservedUsed(IOUserClient
, 1);
5360 OSMetaClassDefineReservedUnused(IOUserClient
, 2);
5361 OSMetaClassDefineReservedUnused(IOUserClient
, 3);
5362 OSMetaClassDefineReservedUnused(IOUserClient
, 4);
5363 OSMetaClassDefineReservedUnused(IOUserClient
, 5);
5364 OSMetaClassDefineReservedUnused(IOUserClient
, 6);
5365 OSMetaClassDefineReservedUnused(IOUserClient
, 7);
5366 OSMetaClassDefineReservedUnused(IOUserClient
, 8);
5367 OSMetaClassDefineReservedUnused(IOUserClient
, 9);
5368 OSMetaClassDefineReservedUnused(IOUserClient
, 10);
5369 OSMetaClassDefineReservedUnused(IOUserClient
, 11);
5370 OSMetaClassDefineReservedUnused(IOUserClient
, 12);
5371 OSMetaClassDefineReservedUnused(IOUserClient
, 13);
5372 OSMetaClassDefineReservedUnused(IOUserClient
, 14);
5373 OSMetaClassDefineReservedUnused(IOUserClient
, 15);