2 * Copyright (c) 1998-2019 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 * Changes to this API are expected.
33 #ifndef _IOKIT_IOUSERCLIENT_H
34 #define _IOKIT_IOUSERCLIENT_H
36 #include <IOKit/IOTypes.h>
37 #include <IOKit/IOService.h>
38 #include <IOKit/OSMessageNotification.h>
39 #include <DriverKit/IOUserClient.h>
40 #include <libkern/c++/OSPtr.h>
43 #include <IOKit/IOStatisticsPrivate.h>
46 #define _IOUSERCLIENT_SENDASYNCRESULT64WITHOPTIONS_ 1
49 kIOUCTypeMask
= 0x0000000f,
50 kIOUCScalarIScalarO
= 0,
51 kIOUCScalarIStructO
= 2,
52 kIOUCStructIStructO
= 3,
53 kIOUCScalarIStructI
= 4,
55 kIOUCForegroundOnly
= 0x00000010,
59 * @abstract Constant to denote a variable length structure argument to IOUserClient.
60 * @constant kIOUCVariableStructureSize Use in the structures IOExternalMethod, IOExternalAsyncMethod, IOExternalMethodDispatch to specify the size of the structure is variable.
63 kIOUCVariableStructureSize
= 0xffffffff
67 typedef IOReturn (IOService::*IOMethod
)(void * p1
, void * p2
, void * p3
,
68 void * p4
, void * p5
, void * p6
);
70 typedef IOReturn (IOService::*IOAsyncMethod
)(OSAsyncReference asyncRef
,
71 void * p1
, void * p2
, void * p3
,
72 void * p4
, void * p5
, void * p6
);
74 typedef IOReturn (IOService::*IOTrap
)(void * p1
, void * p2
, void * p3
,
75 void * p4
, void * p5
, void * p6
);
77 struct IOExternalMethod
{
85 struct IOExternalAsyncMethod
{
93 struct IOExternalTrap
{
99 kIOUserNotifyMaxMessageSize
= 64
103 kIOUserNotifyOptionCanDrop
= 0x1 /* Fail if queue is full, rather than infinitely queuing. */
106 // keys for clientHasPrivilege
107 #define kIOClientPrivilegeAdministrator "root"
108 #define kIOClientPrivilegeLocalUser "local"
109 #define kIOClientPrivilegeForeground "foreground"
112 * @abstract Constants to specify the maximum number of scalar arguments in the IOExternalMethodArguments structure. These constants are documentary since the scalarInputCount, scalarOutputCount fields reflect the actual number passed.
113 * @constant kIOExternalMethodScalarInputCountMax The maximum number of scalars able to passed on input.
114 * @constant kIOExternalMethodScalarOutputCountMax The maximum number of scalars able to passed on output.
117 kIOExternalMethodScalarInputCountMax
= 16,
118 kIOExternalMethodScalarOutputCountMax
= 16,
122 struct IOExternalMethodArguments
{
127 mach_port_t asyncWakePort
;
128 io_user_reference_t
* asyncReference
;
129 uint32_t asyncReferenceCount
;
131 const uint64_t * scalarInput
;
132 uint32_t scalarInputCount
;
134 const void * structureInput
;
135 uint32_t structureInputSize
;
137 IOMemoryDescriptor
* structureInputDescriptor
;
139 uint64_t * scalarOutput
;
140 uint32_t scalarOutputCount
;
142 void * structureOutput
;
143 uint32_t structureOutputSize
;
145 IOMemoryDescriptor
* structureOutputDescriptor
;
146 uint32_t structureOutputDescriptorSize
;
148 uint32_t __reservedA
;
150 OSObject
** structureVariableOutputData
;
152 uint32_t __reserved
[30];
155 typedef IOReturn (*IOExternalMethodAction
)(OSObject
* target
, void * reference
,
156 IOExternalMethodArguments
* arguments
);
157 struct IOExternalMethodDispatch
{
158 IOExternalMethodAction function
;
159 uint32_t checkScalarInputCount
;
160 uint32_t checkStructureInputSize
;
161 uint32_t checkScalarOutputCount
;
162 uint32_t checkStructureOutputSize
;
166 #define IO_EXTERNAL_METHOD_ARGUMENTS_CURRENT_VERSION 2
167 kIOExternalMethodArgumentsCurrentVersion
= IO_EXTERNAL_METHOD_ARGUMENTS_CURRENT_VERSION
171 typedef uintptr_t io_filter_policy_t
;
172 enum io_filter_type_t
{
173 io_filter_type_external_method
= 1,
174 io_filter_type_external_async_method
= 2,
175 io_filter_type_trap
= 3,
178 typedef IOReturn (*io_filter_resolver_t
) (task_t task
, IOUserClient
* client
, uint32_t type
, io_filter_policy_t
*filterp
);
179 typedef IOReturn (*io_filter_applier_t
) (io_filter_policy_t filter
, io_filter_type_t type
, uint32_t selector
);
180 typedef void (*io_filter_release_t
) (io_filter_policy_t filter
);
181 struct io_filter_callbacks
{
182 const io_filter_resolver_t io_filter_resolver
;
183 const io_filter_applier_t io_filter_applier
;
184 const io_filter_release_t io_filter_release
;
186 struct IOUCFilterPolicy
;
190 * @class IOUserClient
191 * @abstract Provides a basis for communication between client applications and I/O Kit objects.
194 class IOUserClient
: public IOService
196 OSDeclareAbstractStructorsWithDispatch(IOUserClient
);
198 friend class IOStatistics
;
201 #if XNU_KERNEL_PRIVATE
203 #else /* XNU_KERNEL_PRIVATE */
205 #endif /* !XNU_KERNEL_PRIVATE */
206 /*! @struct ExpansionData
207 * @discussion This structure will be used to expand the capablilties of this class in the future.
209 struct ExpansionData
{
211 IOUserClientCounter
*counter
;
213 void *iokitstatsReserved
;
216 IOUCFilterPolicy
* filterPolicies
;
218 void *iokitFilterReserved
;
223 * Reserved for future use. (Internal use only)
225 APPLE_KEXT_WSHADOW_PUSH
;
226 ExpansionData
* reserved
;
227 APPLE_KEXT_WSHADOW_POP
;
231 #ifdef XNU_KERNEL_PRIVATE
235 UInt8 sharedInstance
;
238 UInt8 messageAppSuspended
:1,
241 volatile SInt32 __ipc
;
246 void * __reserved
[3];
248 void * __reserved
[2];
251 #else /* XNU_KERNEL_PRIVATE */
253 void * __reserved
[9];
254 #endif /* XNU_KERNEL_PRIVATE */
257 MIG_SERVER_ROUTINE
virtual IOReturn
258 externalMethod(uint32_t selector
, IOExternalMethodArguments
*arguments
,
259 IOExternalMethodDispatch
*dispatch
= NULL
,
260 OSObject
*target
= NULL
, void *reference
= NULL
);
262 MIG_SERVER_ROUTINE
virtual IOReturn
registerNotificationPort(
263 mach_port_t port
, UInt32 type
, io_user_reference_t refCon
);
267 OSMetaClassDeclareReservedUnused(IOUserClient
, 0);
268 OSMetaClassDeclareReservedUnused(IOUserClient
, 1);
270 OSMetaClassDeclareReservedUsedX86(IOUserClient
, 0);
271 OSMetaClassDeclareReservedUsedX86(IOUserClient
, 1);
273 OSMetaClassDeclareReservedUnused(IOUserClient
, 2);
274 OSMetaClassDeclareReservedUnused(IOUserClient
, 3);
275 OSMetaClassDeclareReservedUnused(IOUserClient
, 4);
276 OSMetaClassDeclareReservedUnused(IOUserClient
, 5);
277 OSMetaClassDeclareReservedUnused(IOUserClient
, 6);
278 OSMetaClassDeclareReservedUnused(IOUserClient
, 7);
279 OSMetaClassDeclareReservedUnused(IOUserClient
, 8);
280 OSMetaClassDeclareReservedUnused(IOUserClient
, 9);
281 OSMetaClassDeclareReservedUnused(IOUserClient
, 10);
282 OSMetaClassDeclareReservedUnused(IOUserClient
, 11);
283 OSMetaClassDeclareReservedUnused(IOUserClient
, 12);
284 OSMetaClassDeclareReservedUnused(IOUserClient
, 13);
285 OSMetaClassDeclareReservedUnused(IOUserClient
, 14);
286 OSMetaClassDeclareReservedUnused(IOUserClient
, 15);
288 #ifdef XNU_KERNEL_PRIVATE
290 /* Available within xnu source only */
292 static void initialize( void );
293 static void destroyUserReferences( OSObject
* obj
);
294 static bool finalizeUserReferences( OSObject
* obj
);
295 OSPtr
<IOMemoryMap
> mapClientMemory64( IOOptionBits type
,
297 IOOptionBits mapFlags
= kIOMapAnywhere
,
298 mach_vm_address_t atAddress
= 0 );
299 IOReturn
registerOwner(task_t task
);
300 void noMoreSenders(void);
301 io_filter_policy_t
filterForTask(task_t task
, io_filter_policy_t addFilterPolicy
);
302 #endif /* XNU_KERNEL_PRIVATE */
306 static IOReturn
registerFilterCallbacks(const struct io_filter_callbacks
*callbacks
, size_t size
);
310 static IOReturn
sendAsyncResult(OSAsyncReference reference
,
311 IOReturn result
, void *args
[], UInt32 numArgs
);
312 static void setAsyncReference(OSAsyncReference asyncRef
,
313 mach_port_t wakePort
,
314 void *callback
, void *refcon
);
316 static IOReturn
sendAsyncResult64(OSAsyncReference64 reference
,
317 IOReturn result
, io_user_reference_t args
[], UInt32 numArgs
);
320 * @function sendAsyncResult64WithOptions
321 * @abstract Send a notification as with sendAsyncResult, but with finite queueing.
322 * @discussion IOUserClient::sendAsyncResult64() will infitely queue messages if the client
323 * is not processing them in a timely fashion. This variant will not, for simple
324 * handling of situations where clients may be expected to stop processing messages.
326 static IOReturn
sendAsyncResult64WithOptions(OSAsyncReference64 reference
,
327 IOReturn result
, io_user_reference_t args
[], UInt32 numArgs
,
328 IOOptionBits options
);
330 static void setAsyncReference64(OSAsyncReference64 asyncRef
,
331 mach_port_t wakePort
,
332 mach_vm_address_t callback
, io_user_reference_t refcon
);
334 static void setAsyncReference64(OSAsyncReference64 asyncRef
,
335 mach_port_t wakePort
,
336 mach_vm_address_t callback
, io_user_reference_t refcon
,
341 static IOReturn
clientHasAuthorization( task_t task
,
342 IOService
* service
);
344 static IOReturn
clientHasPrivilege( void * securityToken
,
345 const char * privilegeName
);
347 static OSPtr
<OSObject
> copyClientEntitlement(task_t task
, const char *entitlement
);
348 static OSPtr
<OSObject
> copyClientEntitlementVnode(struct vnode
*vnode
, off_t offset
, const char *entitlement
);
350 static OSPtr
<OSDictionary
> copyClientEntitlements(task_t task
);
351 static OSPtr
<OSDictionary
> copyClientEntitlementsVnode(struct vnode
*vnode
, off_t offset
);
352 static OSPtr
<OSDictionary
> copyEntitlementsFromBlob(void *blob
, size_t len
);
355 * @function releaseAsyncReference64
356 * @abstract Release the mach_port_t reference held within the OSAsyncReference64 structure.
357 * @discussion The OSAsyncReference64 structure passed to async methods holds a reference to the wakeup mach port, which should be released to balance each async method call. Behavior is undefined if these calls are not correctly balanced.
358 * @param reference The reference passed to the subclass IOAsyncMethod, or externalMethod() in the IOExternalMethodArguments.asyncReference field.
359 * @result A return code.
361 static IOReturn
releaseAsyncReference64(OSAsyncReference64 reference
);
363 * @function releaseNotificationPort
364 * @abstract Release the mach_port_t passed to registerNotificationPort().
365 * @discussion The mach_port_t passed to the registerNotificationPort() methods should be released to balance each call to registerNotificationPort(). Behavior is undefined if these calls are not correctly balanced.
366 * @param port The mach_port_t argument previously passed to the subclass implementation of registerNotificationPort().
367 * @result A return code.
369 static IOReturn
releaseNotificationPort(mach_port_t port
);
371 virtual bool init() APPLE_KEXT_OVERRIDE
;
372 virtual bool init( OSDictionary
* dictionary
) APPLE_KEXT_OVERRIDE
;
373 // Currently ignores the all args, just passes up to IOService::init()
374 virtual bool initWithTask(
375 task_t owningTask
, void * securityToken
, UInt32 type
,
376 OSDictionary
* properties
);
378 virtual bool initWithTask(
379 task_t owningTask
, void * securityToken
, UInt32 type
);
381 virtual void free() APPLE_KEXT_OVERRIDE
;
383 virtual IOReturn
clientClose( void );
384 virtual IOReturn
clientDied( void );
386 virtual IOService
* getService( void );
388 MIG_SERVER_ROUTINE
virtual IOReturn
registerNotificationPort(
389 mach_port_t port
, UInt32 type
, UInt32 refCon
);
391 MIG_SERVER_ROUTINE
virtual IOReturn
getNotificationSemaphore( UInt32 notification_type
,
392 semaphore_t
* semaphore
);
394 virtual IOReturn
connectClient( IOUserClient
* client
);
396 // memory will be released by user client when last map is destroyed
397 virtual IOReturn
clientMemoryForType( UInt32 type
,
398 IOOptionBits
* options
,
399 IOMemoryDescriptor
** memory
);
401 IOReturn
clientMemoryForType( UInt32 type
,
402 IOOptionBits
* options
,
403 OSSharedPtr
<IOMemoryDescriptor
>& memory
);
407 APPLE_KEXT_COMPATIBILITY_VIRTUAL
408 OSPtr
<IOMemoryMap
> mapClientMemory( IOOptionBits type
,
410 IOOptionBits mapFlags
= kIOMapAnywhere
,
411 IOVirtualAddress atAddress
= 0 );
414 static IOReturn
_sendAsyncResult64(OSAsyncReference64 reference
,
415 IOReturn result
, io_user_reference_t args
[], UInt32 numArgs
, IOOptionBits options
);
419 * @function removeMappingForDescriptor
420 * Remove the first mapping created from the memory descriptor returned by clientMemoryForType() from IOUserClient's list of mappings. If such a mapping exists, it is retained and the reference currently held by IOUserClient is returned to the caller.
421 * @param memory The memory descriptor instance previously returned by the implementation of clientMemoryForType().
422 * @result A reference to the first IOMemoryMap instance found in the list of mappings created by IOUserClient from that passed memory descriptor is returned, or zero if none exist. The caller should release this reference.
424 OSPtr
<IOMemoryMap
> removeMappingForDescriptor(IOMemoryDescriptor
* memory
);
427 * @function exportObjectToClient
428 * Make an arbitrary OSObject available to the client task.
429 * @param task The task.
430 * @param obj The object we want to export to the client.
431 * @param clientObj Returned value is the client's port name.
433 virtual IOReturn
exportObjectToClient(task_t task
,
434 LIBKERN_CONSUMED OSObject
*obj
, io_object_t
*clientObj
);
439 * @function copyPortNameForObjectInTask
440 * Make an arbitrary OSObject available to the client task as a port name.
441 * The port does not respond to any IOKit IPC calls.
442 * @param task The task.
443 * @param object The object we want to export to the client.
444 * The port holds a reference on the object, this function does not consume any reference on the object.
445 * @param port_name Returned value is the task's port name. It has one send right created by this function.
446 * @result A return code.
448 static IOReturn
copyPortNameForObjectInTask(task_t task
, OSObject
*object
,
449 mach_port_name_t
* port_name
);
452 * @function copyObjectForPortNameInTask
453 * Look up an OSObject given a task's port name created with copyPortNameForObjectInTask().
454 * @param task The task.
455 * @param port_name The task's port name. This function does not consume any reference on the port name.
456 * @param object If the port name is valid, a reference to the object is returned. It should be released by the caller.
457 * @result A return code.
459 static IOReturn
copyObjectForPortNameInTask(task_t task
, mach_port_name_t port_name
,
462 static IOReturn
copyObjectForPortNameInTask(task_t task
, mach_port_name_t port_name
,
463 OSSharedPtr
<OSObject
>& object
);
466 * @function adjustPortNameReferencesInTask
467 * Adjust the send rights for a port name created with copyPortNameForObjectInTask().
468 * @param task The task.
469 * @param port_name The task's port name.
470 * @param delta Signed value change to the number of user references.
471 * @result A return code.
473 static IOReturn
adjustPortNameReferencesInTask(task_t task
, mach_port_name_t port_name
, mach_port_delta_t delta
);
475 #define IOUC_COPYPORTNAMEFOROBJECTINTASK 1
477 #endif /* KERNEL_PRIVATE */
479 // Old methods for accessing method vector backward compatiblility only
480 virtual IOExternalMethod
*
481 getExternalMethodForIndex( UInt32 index
)
482 APPLE_KEXT_DEPRECATED
;
483 virtual IOExternalAsyncMethod
*
484 getExternalAsyncMethodForIndex( UInt32 index
)
485 APPLE_KEXT_DEPRECATED
;
487 // Methods for accessing method vector.
488 virtual IOExternalMethod
*
489 getTargetAndMethodForIndex(
490 LIBKERN_RETURNS_NOT_RETAINED IOService
** targetP
, UInt32 index
);
491 virtual IOExternalAsyncMethod
*
492 getAsyncTargetAndMethodForIndex(
493 LIBKERN_RETURNS_NOT_RETAINED IOService
** targetP
, UInt32 index
);
495 getTargetAndMethodForIndex(
496 OSSharedPtr
<IOService
>& targetP
, UInt32 index
);
497 IOExternalAsyncMethod
*
498 getAsyncTargetAndMethodForIndex(
499 OSSharedPtr
<IOService
>& targetP
, UInt32 index
);
501 // Methods for accessing trap vector - old and new style
502 virtual IOExternalTrap
*
503 getExternalTrapForIndex( UInt32 index
)
504 APPLE_KEXT_DEPRECATED
;
506 virtual IOExternalTrap
*
507 getTargetAndTrapForIndex(
508 LIBKERN_RETURNS_NOT_RETAINED IOService
**targetP
, UInt32 index
);
511 #ifdef XNU_KERNEL_PRIVATE
512 extern "C" void IOMachPortDestroyUserReferences(OSObject
* obj
, natural_t type
);
513 #endif /* XNU_KERNEL_PRIVATE */
515 #endif /* ! _IOKIT_IOUSERCLIENT_H */