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