xref: /xnu-11215/iokit/IOKit/IOPolledInterface.h (revision 8d741a5d)
1 /*
2  * Copyright (c) 2004 Apple Computer, 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 #ifndef _IOPOLLEDINTERFACE_H_
30 #define _IOPOLLEDINTERFACE_H_
31 
32 enum{
33 	kIOPolledPreflightState           = 1,
34 	kIOPolledBeforeSleepState         = 2,
35 	kIOPolledAfterSleepState          = 3,
36 	kIOPolledPostflightState          = 4,
37 
38 	kIOPolledPreflightCoreDumpState   = 5,
39 	kIOPolledPostflightCoreDumpState  = 6,
40 
41 	kIOPolledBeforeSleepStateAborted  = 7,
42 };
43 
44 #if defined(__cplusplus)
45 
46 #include <libkern/c++/OSObject.h>
47 #include <libkern/c++/OSPtr.h>
48 #include <IOKit/IOMemoryDescriptor.h>
49 
50 #define kIOPolledInterfaceSupportKey "IOPolledInterface"
51 #define kIOPolledInterfaceActiveKey  "IOPolledInterfaceActive"
52 #define kIOPolledInterfaceStackKey   "IOPolledInterfaceStack"
53 
54 enum{
55 	kIOPolledWrite = 1,
56 	kIOPolledRead  = 2,
57 	kIOPolledFlush = 3
58 };
59 
60 typedef void (*IOPolledCompletionAction)( void *   target,
61     void *   parameter,
62     IOReturn status,
63     uint64_t actualByteCount);
64 struct IOPolledCompletion {
65 	void *                    target;
66 	IOPolledCompletionAction  action;
67 	void *                    parameter;
68 };
69 
70 class IOPolledInterface : public OSObject
71 {
72 	OSDeclareAbstractStructors(IOPolledInterface);
73 
74 protected:
75 	struct ExpansionData { };
76 	ExpansionData * reserved;
77 
78 public:
79 	virtual IOReturn probe(IOService * target) = 0;
80 
81 	virtual IOReturn open( IOOptionBits state, IOMemoryDescriptor * buffer) = 0;
82 	virtual IOReturn close(IOOptionBits state) = 0;
83 
84 	virtual IOReturn startIO(uint32_t           operation,
85 	    uint32_t           bufferOffset,
86 	    uint64_t           deviceOffset,
87 	    uint64_t           length,
88 	    IOPolledCompletion completion) = 0;
89 
90 	virtual IOReturn checkForWork(void) = 0;
91 
92 	virtual IOReturn setEncryptionKey(const uint8_t * key, size_t keySize);
93 
94 	OSMetaClassDeclareReservedUsedX86(IOPolledInterface, 0);
95 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 1);
96 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 2);
97 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 3);
98 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 4);
99 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 5);
100 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 6);
101 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 7);
102 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 8);
103 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 9);
104 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 10);
105 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 11);
106 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 12);
107 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 13);
108 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 14);
109 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 15);
110 };
111 
112 #endif /* defined(__cplusplus) */
113 
114 #ifdef XNU_KERNEL_PRIVATE
115 
116 #include <libkern/crypto/aes.h>
117 #include <IOKit/IOTypes.h>
118 #include <IOKit/IOHibernatePrivate.h>
119 
120 // kern_open_file_for_direct_io() flags
121 enum{
122 	kIOPolledFileCreate    = 0x00000001,
123 	kIOPolledFileHibernate = 0x00000002,
124 };
125 
126 // kern_open_file_for_direct_io() oflags
127 enum{
128 	kIOPolledFileSSD    = 0x00000001
129 };
130 
131 enum { kDefaultIOSize = 128 * 1024 };
132 enum { kDefaultIONumBuffers = 2 };
133 
134 #if !defined(__cplusplus)
135 typedef struct IORegistryEntry IORegistryEntry;
136 typedef struct OSData OSData;
137 typedef struct OSArray OSArray;
138 typedef struct IOMemoryDescriptor IOMemoryDescriptor;
139 typedef struct IOPolledFilePollers IOPolledFilePollers;
140 #else
141 class IOPolledFilePollers;
142 #endif
143 
144 struct IOPolledFileIOVars {
145 	IOPolledFilePollers              *  pollers;
146 	struct kern_direct_file_io_ref_t *  fileRef;
147 	OSData *                            fileExtents;
148 	uint64_t                            block0;
149 	uint32_t                         blockSize;
150 	uint64_t                            maxiobytes;
151 	uint32_t                         bufferLimit;
152 	uint8_t *                           buffer;
153 	uint32_t                         bufferSize;
154 	uint32_t                         bufferOffset;
155 	uint32_t                         bufferHalf;
156 	uint64_t                         extentRemaining;
157 	uint32_t                         lastRead;
158 	uint64_t                         readEnd;
159 	uint32_t                            flags;
160 	uint64_t                            fileSize;
161 	uint64_t                            position;
162 	uint64_t                            extentPosition;
163 	uint64_t                            encryptStart;
164 	uint64_t                            encryptEnd;
165 	uint64_t                            cryptBytes;
166 	AbsoluteTime                        cryptTime;
167 	IOPolledFileExtent *                extentMap;
168 	IOPolledFileExtent *                currentExtent;
169 	bool                                allocated;
170 };
171 
172 typedef struct IOPolledFileIOVars IOPolledFileIOVars;
173 
174 struct IOPolledFileCryptVars {
175 	uint8_t aes_iv[AES_BLOCK_SIZE];
176 	aes_ctx ctx;
177 };
178 typedef struct IOPolledFileCryptVars IOPolledFileCryptVars;
179 
180 #if defined(__cplusplus)
181 
182 IOReturn IOPolledFileOpen(const char * filename,
183     uint32_t flags,
184     uint64_t setFileSize, uint64_t fsFreeSize,
185     void * write_file_addr, size_t write_file_len,
186     IOPolledFileIOVars ** fileVars,
187     LIBKERN_RETURNS_RETAINED OSData ** imagePath,
188     uint8_t * volumeCryptKey, size_t * keySize);
189 
190 IOReturn IOPolledFileOpen(const char * filename,
191     uint32_t flags,
192     uint64_t setFileSize, uint64_t fsFreeSize,
193     void * write_file_addr, size_t write_file_len,
194     IOPolledFileIOVars ** fileVars,
195     OSSharedPtr<OSData>& imagePath,
196     uint8_t * volumeCryptKey, size_t * keySize);
197 
198 IOReturn IOPolledFileClose(IOPolledFileIOVars ** pVars,
199     off_t write_offset, void * addr, size_t write_length,
200     off_t discard_offset, off_t discard_end, bool unlink);
201 
202 IOReturn IOPolledFilePollersSetup(IOPolledFileIOVars * vars, uint32_t openState);
203 
204 LIBKERN_RETURNS_NOT_RETAINED IOMemoryDescriptor * IOPolledFileGetIOBuffer(IOPolledFileIOVars * vars);
205 
206 #endif /* defined(__cplusplus) */
207 
208 #if defined(__cplusplus)
209 #define __C     "C"
210 #else
211 #define __C
212 #endif
213 
214 extern __C IOReturn IOPolledFileSeek(IOPolledFileIOVars * vars, uint64_t position);
215 
216 extern __C IOReturn IOPolledFileWrite(IOPolledFileIOVars * vars,
217     const uint8_t * bytes, IOByteCount size,
218     IOPolledFileCryptVars * cryptvars);
219 extern __C IOReturn IOPolledFileRead(IOPolledFileIOVars * vars,
220     uint8_t * bytes, IOByteCount size,
221     IOPolledFileCryptVars * cryptvars);
222 
223 extern __C IOReturn IOPolledFileFlush(IOPolledFileIOVars * vars);
224 
225 extern __C IOReturn IOPolledFilePollersOpen(IOPolledFileIOVars * vars, uint32_t state, bool abortable);
226 
227 extern __C IOReturn IOPolledFilePollersClose(IOPolledFileIOVars * vars, uint32_t state);
228 
229 extern __C IOReturn IOPolledFilePollersSetEncryptionKey(IOPolledFileIOVars * vars,
230     const uint8_t * key, size_t keySize);
231 
232 extern __C IOPolledFileIOVars * gCoreFileVars;
233 
234 #ifdef _SYS_CONF_H_
235 
236 __BEGIN_DECLS
237 
238 typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size);
239 
240 struct kern_direct_file_io_ref_t *
241 kern_open_file_for_direct_io(const char * name,
242     uint32_t flags,
243     kern_get_file_extents_callback_t callback,
244     void * callback_ref,
245     off_t set_file_size,
246     off_t fs_free_size,
247     off_t write_file_offset,
248     void * write_file_addr,
249     size_t write_file_len,
250     dev_t * partition_device_result,
251     dev_t * image_device_result,
252     uint64_t * partitionbase_result,
253     uint64_t * maxiocount_result,
254     uint32_t * oflags);
255 void
256 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
257     off_t write_offset, void * addr, size_t write_length,
258     off_t discard_offset, off_t discard_end, bool unlink);
259 int
260 kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
261 int
262 kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
263 
264 struct mount *
265 kern_file_mount(struct kern_direct_file_io_ref_t * ref);
266 
267 enum{
268 	kIOPolledFileMountChangeMount = 0x00000101,
269 	kIOPolledFileMountChangeUnmount = 0x00000102,
270 	kIOPolledFileMountChangeWillResize = 0x00000201,
271 	kIOPolledFileMountChangeDidResize = 0x00000202,
272 };
273 extern void IOPolledFileMountChange(struct mount * mp, uint32_t op);
274 
275 __END_DECLS
276 
277 #endif /* _SYS_CONF_H_ */
278 
279 #endif /* XNU_KERNEL_PRIVATE */
280 
281 #endif /* _IOPOLLEDINTERFACE_H_ */
282