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