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