xref: /xnu-8796.121.2/iokit/DriverKit/IOEventLink.iig (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions/*
2*c54f35caSApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions#ifndef _IOKIT_UIOEVENTLINK_H
30*c54f35caSApple OSS Distributions#define _IOKIT_UIOEVENTLINK_H
31*c54f35caSApple OSS Distributions
32*c54f35caSApple OSS Distributions#include <DriverKit/IODispatchQueue.iig>
33*c54f35caSApple OSS Distributions#include <DriverKit/IODispatchSource.iig>
34*c54f35caSApple OSS Distributions#include <DriverKit/IOUserClient.iig>
35*c54f35caSApple OSS Distributions
36*c54f35caSApple OSS Distributionsenum {
37*c54f35caSApple OSS Distributions	kIOEventLinkMaxNameLength = 64,
38*c54f35caSApple OSS Distributions};
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributions// IOEventLink::Associate() options
41*c54f35caSApple OSS Distributionsenum {
42*c54f35caSApple OSS Distributions	kIOEventLinkAssociateCurrentThread   = 0x00000000,
43*c54f35caSApple OSS Distributions    kIOEventLinkAssociateOnWait          = 0x00000001,
44*c54f35caSApple OSS Distributions};
45*c54f35caSApple OSS Distributions
46*c54f35caSApple OSS Distributions// Clock options
47*c54f35caSApple OSS Distributionsenum {
48*c54f35caSApple OSS Distributions    kIOEventLinkClockMachAbsoluteTime = 0x0,
49*c54f35caSApple OSS Distributions};
50*c54f35caSApple OSS Distributions
51*c54f35caSApple OSS Distributions/*!
52*c54f35caSApple OSS Distributions * @class IOEventLink
53*c54f35caSApple OSS Distributions *
54*c54f35caSApple OSS Distributions * @abstract
55*c54f35caSApple OSS Distributions * IOEventLink allows for fast IPC, suitable for
56*c54f35caSApple OSS Distributions * realtime applications.
57*c54f35caSApple OSS Distributions *
58*c54f35caSApple OSS Distributions * @discussion
59*c54f35caSApple OSS Distributions *
60*c54f35caSApple OSS Distributions * Applications that open user clients to a DriverKit driver can set up an eventlink
61*c54f35caSApple OSS Distributions * for fast signaling.
62*c54f35caSApple OSS Distributions *
63*c54f35caSApple OSS Distributions * To configure an eventlink, the application will have to first create an eventlink object
64*c54f35caSApple OSS Distributions * with os_eventlink_create() (see <os/eventlink_private.h>). The application then has to extract
65*c54f35caSApple OSS Distributions * the remote eventlink port with os_eventlink_extract_remote_port(). To send the remote eventlink port
66*c54f35caSApple OSS Distributions * to the driver, use:
67*c54f35caSApple OSS Distributions *
68*c54f35caSApple OSS Distributions * const char * name = "Event Link Name"; // This must match the name the driver used in IOEventLink::Create().
69*c54f35caSApple OSS Distributions * kern_return_t ret = IOConnectTrap3(connect, // user client connection (io_connect_t)
70*c54f35caSApple OSS Distributions *                                    0, // specifies event link configuration trap
71*c54f35caSApple OSS Distributions *                                    (uintptr_t)name,
72*c54f35caSApple OSS Distributions *                                    (uintptr_t)strlen(name),
73*c54f35caSApple OSS Distributions *                                    (uintptr_t)remotePort // port from os_eventlink_extract_remote_port
74*c54f35caSApple OSS Distributions *                                    );
75*c54f35caSApple OSS Distributions *
76*c54f35caSApple OSS Distributions * Once the remote eventlink port has been sent to the driver, the driver should be notified with a user-defined external method
77*c54f35caSApple OSS Distributions * or other existing signaling mechanism. The driver should handle this by activating the IOEventLink with Activate().
78*c54f35caSApple OSS Distributions */
79*c54f35caSApple OSS Distributionsclass NATIVE KERNEL IOEventLink : public OSObject
80*c54f35caSApple OSS Distributions{
81*c54f35caSApple OSS Distributionspublic:
82*c54f35caSApple OSS Distributions
83*c54f35caSApple OSS Distributions    virtual bool
84*c54f35caSApple OSS Distributions    init() override;
85*c54f35caSApple OSS Distributions
86*c54f35caSApple OSS Distributions    virtual void
87*c54f35caSApple OSS Distributions    free() override;
88*c54f35caSApple OSS Distributions
89*c54f35caSApple OSS Distributions    /*!
90*c54f35caSApple OSS Distributions     * @brief       Create an IOEventLink.
91*c54f35caSApple OSS Distributions     * @param       name       User-specified name. If an IOEventLink with the same name already exists in the specified
92*c54f35caSApple OSS Distributions     *                         user client, the old IOEventLink will be replaced.
93*c54f35caSApple OSS Distributions     * @param       userClient  Userclient to create the eventlink in. The DriverKit runtime will retain the userclient, and will
94*c54f35caSApple OSS Distributions     *                          release it in Invalidate() or when the IOEventLink is freed.
95*c54f35caSApple OSS Distributions     * @param       eventLink  Created IOEventLink with +1 retain count to be released by the caller.
96*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
97*c54f35caSApple OSS Distributions     */
98*c54f35caSApple OSS Distributions    static kern_return_t
99*c54f35caSApple OSS Distributions    Create(OSString * name, IOUserClient * userClient, IOEventLink ** eventLink) LOCAL;
100*c54f35caSApple OSS Distributions
101*c54f35caSApple OSS Distributions#if DRIVERKIT_PRIVATE
102*c54f35caSApple OSS Distributions
103*c54f35caSApple OSS Distributions    /*!
104*c54f35caSApple OSS Distributions     * @brief       Set the port for this eventlink.
105*c54f35caSApple OSS Distributions     * @discussion  This is not meant to be used directly.
106*c54f35caSApple OSS Distributions     * @param       port Eventlink port
107*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
108*c54f35caSApple OSS Distributions     */
109*c54f35caSApple OSS Distributions    virtual kern_return_t
110*c54f35caSApple OSS Distributions    SetEventlinkPort(mach_port_t port PORTCOPYSEND) LOCAL;
111*c54f35caSApple OSS Distributions
112*c54f35caSApple OSS Distributions#endif /* DRIVERKIT_PRIVATE */
113*c54f35caSApple OSS Distributions
114*c54f35caSApple OSS Distributions    /*
115*c54f35caSApple OSS Distributions     * @brief       Signal the eventlink. If a thread is waiting on the eventlink, this will wake up that thread.
116*c54f35caSApple OSS Distributions     * @discussion  This API call is real time safe.
117*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
118*c54f35caSApple OSS Distributions     */
119*c54f35caSApple OSS Distributions    kern_return_t
120*c54f35caSApple OSS Distributions    Signal() LOCALONLY;
121*c54f35caSApple OSS Distributions
122*c54f35caSApple OSS Distributions    /*
123*c54f35caSApple OSS Distributions     * @brief       Wait on the eventlink.
124*c54f35caSApple OSS Distributions     * @discussion  This API call is real time safe.
125*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
126*c54f35caSApple OSS Distributions     */
127*c54f35caSApple OSS Distributions    kern_return_t
128*c54f35caSApple OSS Distributions    Wait(uint64_t * signalsConsumed) LOCALONLY;
129*c54f35caSApple OSS Distributions
130*c54f35caSApple OSS Distributions    /*
131*c54f35caSApple OSS Distributions     * @brief       Signal the eventlink and wait. If a thread is waiting on the eventlink, this will wake up that thread.
132*c54f35caSApple OSS Distributions     * @discussion  This API call is real time safe.
133*c54f35caSApple OSS Distributions     * @param       signalsConsumed   When the calling thread wakes up, the amount of signals consumed by the wait will be written here.
134*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
135*c54f35caSApple OSS Distributions     */
136*c54f35caSApple OSS Distributions    kern_return_t
137*c54f35caSApple OSS Distributions    SignalAndWait(uint64_t * signalsConsumed) LOCALONLY;
138*c54f35caSApple OSS Distributions
139*c54f35caSApple OSS Distributions    /*
140*c54f35caSApple OSS Distributions     * @brief       Signal the eventlink and wait with a timeout. If a thread is waiting on the eventlink, this will wake up that thread.
141*c54f35caSApple OSS Distributions     * @discussion  This API call is real time safe.
142*c54f35caSApple OSS Distributions     * @param       signalsConsumed   When the calling thread wakes up, the amount of signals consumed by the wait will be written here.
143*c54f35caSApple OSS Distributions     * @param       clockOptions      Options for which clock to use. The only currently supported option is kIOEventLinkClockMachAbsoluteTime.
144*c54f35caSApple OSS Distributions     * @param       timeout           Timeout
145*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
146*c54f35caSApple OSS Distributions     */
147*c54f35caSApple OSS Distributions    kern_return_t
148*c54f35caSApple OSS Distributions    SignalAndWaitUntil(uint64_t clockOptions, uint64_t timeout, uint64_t * signalsConsumed) LOCALONLY;
149*c54f35caSApple OSS Distributions
150*c54f35caSApple OSS Distributions    /*
151*c54f35caSApple OSS Distributions     * @brief       Wait with a timeout. If a thread is waiting on the eventlink, this will wake up that thread.
152*c54f35caSApple OSS Distributions     * @discussion  This API call is real time safe.
153*c54f35caSApple OSS Distributions     * @param       signalsConsumed   When the calling thread wakes up, the amount of signals consumed by the wait will be written here.
154*c54f35caSApple OSS Distributions     * @param       clockOptions      Options for which clock to use. The only currently supported option is kIOEventLinkClockMachAbsoluteTime.
155*c54f35caSApple OSS Distributions     * @param       timeout           Timeout
156*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
157*c54f35caSApple OSS Distributions     */
158*c54f35caSApple OSS Distributions    kern_return_t
159*c54f35caSApple OSS Distributions    WaitUntil(uint64_t clockOptions, uint64_t timeout, uint64_t * signalsConsumed) LOCALONLY;
160*c54f35caSApple OSS Distributions
161*c54f35caSApple OSS Distributions    /*!
162*c54f35caSApple OSS Distributions     * @brief       Cancel the event link.
163*c54f35caSApple OSS Distributions     * @discussion  If a thread is waiting on the eventlink, cancellation will wake up that thread.
164*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
165*c54f35caSApple OSS Distributions     */
166*c54f35caSApple OSS Distributions    kern_return_t
167*c54f35caSApple OSS Distributions    Cancel() LOCALONLY;
168*c54f35caSApple OSS Distributions
169*c54f35caSApple OSS Distributions    /*!
170*c54f35caSApple OSS Distributions     * @brief       Activate the event link.
171*c54f35caSApple OSS Distributions     * @discussion  The event link must be activated before it can be signaled or waited on. This is not real-time safe.
172*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
173*c54f35caSApple OSS Distributions     */
174*c54f35caSApple OSS Distributions    kern_return_t
175*c54f35caSApple OSS Distributions    Activate() LOCALONLY;
176*c54f35caSApple OSS Distributions
177*c54f35caSApple OSS Distributions    /*!
178*c54f35caSApple OSS Distributions     * @brief       Associate a thread with the eventlink.
179*c54f35caSApple OSS Distributions     * @discussion  The eventlink should be activated before this call. This is not real-time safe.
180*c54f35caSApple OSS Distributions     * @param       options  Options for Associate(). Use kIOEventLinkAssociateCurrentThread or kIOEventLinkAssociateOnWait.
181*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
182*c54f35caSApple OSS Distributions     */
183*c54f35caSApple OSS Distributions    kern_return_t
184*c54f35caSApple OSS Distributions    Associate(uint64_t options) LOCALONLY;
185*c54f35caSApple OSS Distributions
186*c54f35caSApple OSS Distributions    /*!
187*c54f35caSApple OSS Distributions     * @brief       Disassociate the current thread from the eventlink. This is not real-time safe.
188*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
189*c54f35caSApple OSS Distributions     */
190*c54f35caSApple OSS Distributions    kern_return_t
191*c54f35caSApple OSS Distributions    Disassociate() LOCALONLY;
192*c54f35caSApple OSS Distributions
193*c54f35caSApple OSS Distributions    /*!
194*c54f35caSApple OSS Distributions     * @brief       Invalidate the IOEventLink.
195*c54f35caSApple OSS Distributions     * @discussion  This releases the kernel reference to the IOEventLink, allowing the name to be used for a different
196*c54f35caSApple OSS Distributions     *              IOEventLink. This method should be called after the client has configured the eventlink with the IOConnectTrap
197*c54f35caSApple OSS Distributions     *              call. After invalidation, the IOEventLink can no longer be configured through the IOConnectTrap call. No other
198*c54f35caSApple OSS Distributions     *              functionality is affected.
199*c54f35caSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
200*c54f35caSApple OSS Distributions     */
201*c54f35caSApple OSS Distributions    kern_return_t
202*c54f35caSApple OSS Distributions    Invalidate() LOCALONLY;
203*c54f35caSApple OSS Distributions
204*c54f35caSApple OSS Distributions#if DRIVERKIT_PRIVATE
205*c54f35caSApple OSS Distributions
206*c54f35caSApple OSS Distributions    virtual kern_return_t
207*c54f35caSApple OSS Distributions    InvalidateKernel(IOUserClient * client);
208*c54f35caSApple OSS Distributions
209*c54f35caSApple OSS Distributions#endif /* DRIVERKIT_PRIVATE */
210*c54f35caSApple OSS Distributions};
211*c54f35caSApple OSS Distributions
212*c54f35caSApple OSS Distributions#endif /* ! _IOKIT_UIOEVENTLINK_H */
213