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