xref: /xnu-12377.1.9/iokit/IOKit/IOPolledInterface.h (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
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                            fileSizeMin;
161 	uint64_t                            fileSizeMax;
162 	uint64_t                            fileSize;
163 	uint64_t                            position;
164 	uint64_t                            extentPosition;
165 	uint64_t                            encryptStart;
166 	uint64_t                            encryptEnd;
167 	uint64_t                            cryptBytes;
168 	AbsoluteTime                        cryptTime;
169 	IOPolledFileExtent *                extentMap;
170 	IOPolledFileExtent *                currentExtent;
171 	bool                                allocated;
172 };
173 
174 typedef struct IOPolledFileIOVars IOPolledFileIOVars;
175 
176 struct IOPolledFileCryptVars {
177 	uint8_t aes_iv[AES_BLOCK_SIZE];
178 	aes_ctx ctx;
179 };
180 typedef struct IOPolledFileCryptVars IOPolledFileCryptVars;
181 
182 #if defined(__cplusplus)
183 
184 // IOPolledFileOpen setFileMinSize/setFileMaxSize:
185 // passing zero for set_file_size_min (coredumps)
186 // means caller only accepts set_file_size_max and disk free space is checked.
187 // if set_file_size_min & set_file_size_max are passed (hibernation),
188 // a file up to length set_file_size_max is created leaving fsFreeSize bytes free,
189 // but length no smaller than set_file_size_min regardless of disk free space.
190 
191 IOReturn IOPolledFileOpen(const char * filename,
192     uint32_t flags,
193     uint64_t setFileMinSize, uint64_t setFileMaxSize, uint64_t fsFreeSize,
194     void * write_file_addr, size_t write_file_len,
195     IOPolledFileIOVars ** fileVars,
196     LIBKERN_RETURNS_RETAINED OSData ** imagePath,
197     uint8_t * volumeCryptKey, size_t * keySize);
198 
199 IOReturn IOPolledFileClose(IOPolledFileIOVars ** pVars,
200     off_t write_offset, void * addr, size_t write_length,
201     off_t discard_offset, off_t discard_end, bool unlink);
202 
203 IOReturn IOPolledFilePollersSetup(IOPolledFileIOVars * vars, uint32_t openState);
204 
205 LIBKERN_RETURNS_NOT_RETAINED IOMemoryDescriptor * IOPolledFileGetIOBuffer(IOPolledFileIOVars * vars);
206 
207 #endif /* defined(__cplusplus) */
208 
209 #if defined(__cplusplus)
210 #define __C     "C"
211 #else
212 #define __C
213 #endif
214 
215 extern __C IOReturn IOPolledFileSeek(IOPolledFileIOVars * vars, uint64_t position);
216 
217 extern __C IOReturn IOPolledFileWrite(IOPolledFileIOVars * vars,
218     const uint8_t * bytes, IOByteCount size,
219     IOPolledFileCryptVars * cryptvars);
220 extern __C IOReturn IOPolledFileRead(IOPolledFileIOVars * vars,
221     uint8_t * bytes, IOByteCount size,
222     IOPolledFileCryptVars * cryptvars);
223 
224 extern __C IOReturn IOPolledFileFlush(IOPolledFileIOVars * vars);
225 
226 extern __C IOReturn IOPolledFilePollersOpen(IOPolledFileIOVars * vars, uint32_t state, bool abortable);
227 
228 extern __C IOReturn IOPolledFilePollersClose(IOPolledFileIOVars * vars, uint32_t state);
229 
230 extern __C IOReturn IOPolledFilePollersSetEncryptionKey(IOPolledFileIOVars * vars,
231     const uint8_t * key, size_t keySize);
232 
233 extern __C IOPolledFileIOVars * gCoreFileVars;
234 
235 #ifdef _SYS_CONF_H_
236 
237 __BEGIN_DECLS
238 
239 typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size);
240 
241 struct kern_direct_file_io_ref_t *
242 kern_open_file_for_direct_io(const char * name,
243     uint32_t flags,
244     kern_get_file_extents_callback_t callback,
245     void * callback_ref,
246     off_t set_file_size_min,
247     off_t set_file_size_max,
248     off_t fs_free_size,
249     off_t write_file_offset,
250     void * write_file_addr,
251     size_t write_file_len,
252     dev_t * partition_device_result,
253     dev_t * image_device_result,
254     uint64_t * partitionbase_result,
255     uint64_t * maxiocount_result,
256     uint32_t * oflags);
257 void
258 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
259     off_t write_offset, void * addr, size_t write_length,
260     off_t discard_offset, off_t discard_end, off_t set_file_size, bool unlink);
261 int
262 kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
263 int
264 kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
265 
266 struct mount *
267 kern_file_mount(struct kern_direct_file_io_ref_t * ref);
268 
269 enum{
270 	kIOPolledFileMountChangeMount = 0x00000101,
271 	kIOPolledFileMountChangeUnmount = 0x00000102,
272 	kIOPolledFileMountChangeWillResize = 0x00000201,
273 	kIOPolledFileMountChangeDidResize = 0x00000202,
274 };
275 extern void IOPolledFileMountChange(struct mount * mp, uint32_t op);
276 
277 __END_DECLS
278 
279 #endif /* _SYS_CONF_H_ */
280 
281 #endif /* XNU_KERNEL_PRIVATE */
282 
283 #endif /* _IOPOLLEDINTERFACE_H_ */
284