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