]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOUserServer.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / iokit / IOKit / IOUserServer.h
1 /*
2 * Copyright (c) 2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29
30 #ifndef _IOUSERSERVER_H
31 #define _IOUSERSERVER_H
32
33 #include <IOKit/IORPC.h>
34
35 #define kIOUserClassKey "IOUserClass"
36 #define kIOUserServerClassKey "IOUserServer"
37 #define kIOUserServerNameKey "IOUserServerName"
38 #define kIOUserServerTagKey "IOUserServerTag"
39 // the expected cdhash value of the userspace driver executable
40 #define kIOUserServerCDHashKey "IOUserServerCDHash"
41
42 #if DRIVERKIT_PRIVATE
43
44 enum{
45 kIOKitUserServerClientType = 0x99000003,
46 };
47
48 enum{
49 kIOUserServerMethodRegisterClass = 0x0001000,
50 kIOUserServerMethodStart = 0x0001001,
51 kIOUserServerMethodRegister = 0x0001002,
52 };
53
54
55 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
56
57 class OSObject;
58
59 #define OSObject_Instantiate_ID 0x0000000100000001ULL
60
61 enum {
62 kOSObjectRPCRemote = 0x00000001,
63 kOSObjectRPCKernel = 0x00000002,
64 };
65
66 struct OSObject_Instantiate_Msg_Content {
67 IORPCMessage __hdr;
68 OSObjectRef __object;
69 };
70
71 struct OSObject_Instantiate_Rpl_Content {
72 IORPCMessage __hdr;
73 kern_return_t __result;
74 uint32_t __pad;
75 uint64_t flags;
76 char classname[128];
77 uint64_t methods[0];
78 };
79
80 #pragma pack(4)
81 struct OSObject_Instantiate_Msg {
82 IORPCMessageMach mach;
83 mach_msg_port_descriptor_t __object__descriptor;
84 OSObject_Instantiate_Msg_Content content;
85 };
86 struct OSObject_Instantiate_Rpl {
87 IORPCMessageMach mach;
88 OSObject_Instantiate_Rpl_Content content;
89 };
90 #pragma pack()
91
92 typedef uint64_t IOTrapMessageBuffer[256];
93
94 #endif /* DRIVERKIT_PRIVATE */
95
96 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
97 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
98
99 #ifdef XNU_KERNEL_PRIVATE
100
101 #include <IOKit/IOService.h>
102 #include <IOKit/IOUserClient.h>
103 #include <DriverKit/IOUserServer.h>
104 #include <libkern/c++/OSPtr.h>
105 #include <libkern/c++/OSKext.h>
106
107 class IOUserServer;
108 class OSUserMetaClass;
109 class IODispatchQueue;
110 class IODispatchSource;
111 class IOInterruptDispatchSource;
112 class IOTimerDispatchSource;
113 class IOUserServerCheckInToken;
114 struct IOPStrings;
115
116 struct OSObjectUserVars {
117 IOUserServer * userServer;
118 IODispatchQueue ** queueArray;
119 OSUserMetaClass * userMeta;
120 OSArray * openProviders;
121 IOService * controllingDriver;
122 unsigned long willPowerState;
123 bool willTerminate;
124 bool didTerminate;
125 bool serverDied;
126 bool started;
127 bool stopped;
128 bool userServerPM;
129 bool willPower;
130 uint32_t powerOverride;
131 };
132
133 extern IOLock * gIOUserServerLock;
134
135 typedef struct ipc_kmsg * ipc_kmsg_t;
136
137 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
138
139 namespace IOServicePH
140 {
141 void serverAdd(IOUserServer * server);
142 void serverRemove(IOUserServer * server);
143 void serverAck(IOUserServer * server);
144 bool serverSlept(void);
145 void systemHalt(void);
146 };
147
148 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
149
150 class IOUserServer : public IOUserClient
151 {
152 OSDeclareDefaultStructorsWithDispatch(IOUserServer);
153
154 IOLock * fLock;
155 IOSimpleLock * fInterruptLock;
156 task_t fOwningTask;
157 OSDictionary * fEntitlements;
158 OSDictionary * fClasses;
159 IODispatchQueue * fRootQueue;
160 OSArray * fServices;
161
162 uint64_t fPowerStates;
163 uint8_t fRootNotifier;
164 uint8_t fSystemPowerAck;
165 uint8_t fSystemOff;
166 IOUserServerCheckInToken * fCheckInToken;
167
168 public:
169
170 static IOUserClient * withTask(task_t owningTask);
171 virtual IOReturn clientClose(void) APPLE_KEXT_OVERRIDE;
172 virtual bool finalize(IOOptionBits options) APPLE_KEXT_OVERRIDE;
173 virtual void stop(IOService * provider) APPLE_KEXT_OVERRIDE;
174 virtual void free() APPLE_KEXT_OVERRIDE;
175
176 virtual IOReturn setProperties(OSObject * properties) APPLE_KEXT_OVERRIDE;
177 virtual IOReturn externalMethod(uint32_t selector, IOExternalMethodArguments * args,
178 IOExternalMethodDispatch * dispatch,
179 OSObject * target, void * reference) APPLE_KEXT_OVERRIDE;
180
181 virtual IOExternalTrap * getTargetAndTrapForIndex(IOService ** targetP, UInt32 index) APPLE_KEXT_OVERRIDE;
182
183 IOReturn serviceAttach(IOService * service, IOService * provider);
184 IOReturn serviceStop(IOService * service, IOService * provider);
185 void serviceFree(IOService * service);
186 IOReturn serviceStarted(IOService * service, IOService * provider, bool result);
187 static void serviceWillTerminate(IOService * client, IOService * provider, IOOptionBits options);
188 static void serviceDidTerminate(IOService * client, IOService * provider, IOOptionBits options, bool * defer);
189 static void serviceDidStop(IOService * client, IOService * provider);
190 IOReturn serviceOpen(IOService * provider, IOService * client);
191 IOReturn serviceClose(IOService * provider, IOService * client);
192 IOReturn serviceSetPowerState(IOService * controllingDriver, IOService * service, IOPMPowerFlags flags, IOPMPowerStateIndex powerState);
193 IOReturn serviceNewUserClient(IOService * service, task_t owningTask, void * securityID,
194 uint32_t type, OSDictionary * properties, IOUserClient ** handler);
195 IOReturn serviceNewUserClient(IOService * service, task_t owningTask, void * securityID,
196 uint32_t type, OSDictionary * properties, OSSharedPtr<IOUserClient>& handler);
197 IOReturn exit(const char * reason);
198
199 bool serviceMatchesCheckInToken(IOUserServerCheckInToken *token);
200 bool checkEntitlements(IOService * provider, IOService * dext);
201 bool checkEntitlements(OSDictionary * entitlements, OSObject * prop,
202 IOService * provider, IOService * dext);
203
204 void setTaskLoadTag(OSKext *kext);
205 void setDriverKitUUID(OSKext *kext);
206 void setCheckInToken(IOUserServerCheckInToken *token);
207 void systemPower(bool powerOff);
208 void systemHalt(void);
209 IOReturn setPowerState(unsigned long state, IOService * service) APPLE_KEXT_OVERRIDE;
210 IOReturn powerStateWillChangeTo(IOPMPowerFlags flags, unsigned long state, IOService * service) APPLE_KEXT_OVERRIDE;
211 IOReturn powerStateDidChangeTo(IOPMPowerFlags flags, unsigned long state, IOService * service) APPLE_KEXT_OVERRIDE;
212
213 IOPStrings * copyInStringArray(const char * string, uint32_t userSize);
214 uint32_t stringArrayIndex(IOPStrings * array, const char * look);
215 IOReturn registerClass(OSClassDescription * desc, uint32_t size, OSUserMetaClass ** cls);
216 IOReturn registerClass(OSClassDescription * desc, uint32_t size, OSSharedPtr<OSUserMetaClass>& cls);
217 IOReturn setRootQueue(IODispatchQueue * queue);
218
219 OSObjectUserVars * varsForObject(OSObject * obj);
220 LIBKERN_RETURNS_NOT_RETAINED IODispatchQueue * queueForObject(OSObject * obj, uint64_t msgid);
221
222 static ipc_port_t copySendRightForObject(OSObject * object, natural_t /* ipc_kobject_type_t */ type);
223 static OSObject * copyObjectForSendRight(ipc_port_t port, natural_t /* ipc_kobject_type_t */ type);
224
225 IOReturn copyOutObjects(IORPCMessageMach * mach, IORPCMessage * message,
226 size_t size, bool consume);
227 IOReturn copyInObjects(IORPCMessageMach * mach, IORPCMessage * message,
228 size_t size, bool copyObjects, bool consumePorts);
229
230 IOReturn consumeObjects(IORPCMessage * message, size_t messageSize);
231
232 IOReturn objectInstantiate(OSObject * obj, IORPC rpc, IORPCMessage * message);
233 IOReturn kernelDispatch(OSObject * obj, IORPC rpc);
234 static OSObject * target(OSAction * action, IORPCMessage * message);
235
236 IOReturn rpc(IORPC rpc);
237 IOReturn server(ipc_kmsg_t requestkmsg, ipc_kmsg_t * preply);
238 kern_return_t waitInterruptTrap(void * p1, void * p2, void * p3, void * p4, void * p5, void * p6);
239 };
240
241 typedef void (*IOUserServerCheckInNotificationHandler)(class IOUserServerCheckInToken*, void*);
242
243 class IOUserServerCheckInToken : public OSObject
244 {
245 OSDeclareDefaultStructors(IOUserServerCheckInToken);
246 public:
247 static IOUserServerCheckInToken * create();
248 void setNoSendersNotification(IOUserServerCheckInNotificationHandler handler, void *handlerArgs);
249 void clearNotification();
250 static void notifyNoSenders(IOUserServerCheckInToken * token);
251 private:
252 IOUserServerCheckInNotificationHandler handler;
253 void *handlerArgs;
254 };
255
256 extern "C" kern_return_t
257 IOUserServerUEXTTrap(OSObject * object, void * p1, void * p2, void * p3, void * p4, void * p5, void * p6);
258
259 #endif /* XNU_KERNEL_PRIVATE */
260 #endif /* _IOUSERSERVER_H */