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