xref: /xnu-8796.121.2/iokit/IOKit/IOPolledInterface.h (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions #ifndef _IOPOLLEDINTERFACE_H_
30*c54f35caSApple OSS Distributions #define _IOPOLLEDINTERFACE_H_
31*c54f35caSApple OSS Distributions 
32*c54f35caSApple OSS Distributions enum{
33*c54f35caSApple OSS Distributions 	kIOPolledPreflightState           = 1,
34*c54f35caSApple OSS Distributions 	kIOPolledBeforeSleepState         = 2,
35*c54f35caSApple OSS Distributions 	kIOPolledAfterSleepState          = 3,
36*c54f35caSApple OSS Distributions 	kIOPolledPostflightState          = 4,
37*c54f35caSApple OSS Distributions 
38*c54f35caSApple OSS Distributions 	kIOPolledPreflightCoreDumpState   = 5,
39*c54f35caSApple OSS Distributions 	kIOPolledPostflightCoreDumpState  = 6,
40*c54f35caSApple OSS Distributions 
41*c54f35caSApple OSS Distributions 	kIOPolledBeforeSleepStateAborted  = 7,
42*c54f35caSApple OSS Distributions };
43*c54f35caSApple OSS Distributions 
44*c54f35caSApple OSS Distributions #if defined(__cplusplus)
45*c54f35caSApple OSS Distributions 
46*c54f35caSApple OSS Distributions #include <libkern/c++/OSObject.h>
47*c54f35caSApple OSS Distributions #include <libkern/c++/OSPtr.h>
48*c54f35caSApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
49*c54f35caSApple OSS Distributions 
50*c54f35caSApple OSS Distributions #define kIOPolledInterfaceSupportKey "IOPolledInterface"
51*c54f35caSApple OSS Distributions #define kIOPolledInterfaceActiveKey  "IOPolledInterfaceActive"
52*c54f35caSApple OSS Distributions #define kIOPolledInterfaceStackKey   "IOPolledInterfaceStack"
53*c54f35caSApple OSS Distributions 
54*c54f35caSApple OSS Distributions enum{
55*c54f35caSApple OSS Distributions 	kIOPolledWrite = 1,
56*c54f35caSApple OSS Distributions 	kIOPolledRead  = 2,
57*c54f35caSApple OSS Distributions 	kIOPolledFlush = 3
58*c54f35caSApple OSS Distributions };
59*c54f35caSApple OSS Distributions 
60*c54f35caSApple OSS Distributions typedef void (*IOPolledCompletionAction)( void *   target,
61*c54f35caSApple OSS Distributions     void *   parameter,
62*c54f35caSApple OSS Distributions     IOReturn status,
63*c54f35caSApple OSS Distributions     uint64_t actualByteCount);
64*c54f35caSApple OSS Distributions struct IOPolledCompletion {
65*c54f35caSApple OSS Distributions 	void *                    target;
66*c54f35caSApple OSS Distributions 	IOPolledCompletionAction  action;
67*c54f35caSApple OSS Distributions 	void *                    parameter;
68*c54f35caSApple OSS Distributions };
69*c54f35caSApple OSS Distributions 
70*c54f35caSApple OSS Distributions class IOPolledInterface : public OSObject
71*c54f35caSApple OSS Distributions {
72*c54f35caSApple OSS Distributions 	OSDeclareAbstractStructors(IOPolledInterface);
73*c54f35caSApple OSS Distributions 
74*c54f35caSApple OSS Distributions protected:
75*c54f35caSApple OSS Distributions 	struct ExpansionData { };
76*c54f35caSApple OSS Distributions 	ExpansionData * reserved;
77*c54f35caSApple OSS Distributions 
78*c54f35caSApple OSS Distributions public:
79*c54f35caSApple OSS Distributions 	virtual IOReturn probe(IOService * target) = 0;
80*c54f35caSApple OSS Distributions 
81*c54f35caSApple OSS Distributions 	virtual IOReturn open( IOOptionBits state, IOMemoryDescriptor * buffer) = 0;
82*c54f35caSApple OSS Distributions 	virtual IOReturn close(IOOptionBits state) = 0;
83*c54f35caSApple OSS Distributions 
84*c54f35caSApple OSS Distributions 	virtual IOReturn startIO(uint32_t           operation,
85*c54f35caSApple OSS Distributions 	    uint32_t           bufferOffset,
86*c54f35caSApple OSS Distributions 	    uint64_t           deviceOffset,
87*c54f35caSApple OSS Distributions 	    uint64_t           length,
88*c54f35caSApple OSS Distributions 	    IOPolledCompletion completion) = 0;
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions 	virtual IOReturn checkForWork(void) = 0;
91*c54f35caSApple OSS Distributions 
92*c54f35caSApple OSS Distributions 	virtual IOReturn setEncryptionKey(const uint8_t * key, size_t keySize);
93*c54f35caSApple OSS Distributions 
94*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUsedX86(IOPolledInterface, 0);
95*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 1);
96*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 2);
97*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 3);
98*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 4);
99*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 5);
100*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 6);
101*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 7);
102*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 8);
103*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 9);
104*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 10);
105*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 11);
106*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 12);
107*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 13);
108*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 14);
109*c54f35caSApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOPolledInterface, 15);
110*c54f35caSApple OSS Distributions };
111*c54f35caSApple OSS Distributions 
112*c54f35caSApple OSS Distributions #endif /* defined(__cplusplus) */
113*c54f35caSApple OSS Distributions 
114*c54f35caSApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions #include <libkern/crypto/aes.h>
117*c54f35caSApple OSS Distributions #include <IOKit/IOTypes.h>
118*c54f35caSApple OSS Distributions #include <IOKit/IOHibernatePrivate.h>
119*c54f35caSApple OSS Distributions 
120*c54f35caSApple OSS Distributions // kern_open_file_for_direct_io() flags
121*c54f35caSApple OSS Distributions enum{
122*c54f35caSApple OSS Distributions 	kIOPolledFileCreate    = 0x00000001,
123*c54f35caSApple OSS Distributions 	kIOPolledFileHibernate = 0x00000002,
124*c54f35caSApple OSS Distributions };
125*c54f35caSApple OSS Distributions 
126*c54f35caSApple OSS Distributions // kern_open_file_for_direct_io() oflags
127*c54f35caSApple OSS Distributions enum{
128*c54f35caSApple OSS Distributions 	kIOPolledFileSSD    = 0x00000001
129*c54f35caSApple OSS Distributions };
130*c54f35caSApple OSS Distributions 
131*c54f35caSApple OSS Distributions #if !defined(__cplusplus)
132*c54f35caSApple OSS Distributions typedef struct IORegistryEntry IORegistryEntry;
133*c54f35caSApple OSS Distributions typedef struct OSData OSData;
134*c54f35caSApple OSS Distributions typedef struct OSArray OSArray;
135*c54f35caSApple OSS Distributions typedef struct IOMemoryDescriptor IOMemoryDescriptor;
136*c54f35caSApple OSS Distributions typedef struct IOPolledFilePollers IOPolledFilePollers;
137*c54f35caSApple OSS Distributions #else
138*c54f35caSApple OSS Distributions class IOPolledFilePollers;
139*c54f35caSApple OSS Distributions #endif
140*c54f35caSApple OSS Distributions 
141*c54f35caSApple OSS Distributions struct IOPolledFileIOVars {
142*c54f35caSApple OSS Distributions 	IOPolledFilePollers              *  pollers;
143*c54f35caSApple OSS Distributions 	struct kern_direct_file_io_ref_t *  fileRef;
144*c54f35caSApple OSS Distributions 	OSData *                            fileExtents;
145*c54f35caSApple OSS Distributions 	uint64_t                            block0;
146*c54f35caSApple OSS Distributions 	uint32_t                         blockSize;
147*c54f35caSApple OSS Distributions 	uint64_t                            maxiobytes;
148*c54f35caSApple OSS Distributions 	uint32_t                         bufferLimit;
149*c54f35caSApple OSS Distributions 	uint8_t *                           buffer;
150*c54f35caSApple OSS Distributions 	uint32_t                         bufferSize;
151*c54f35caSApple OSS Distributions 	uint32_t                         bufferOffset;
152*c54f35caSApple OSS Distributions 	uint32_t                         bufferHalf;
153*c54f35caSApple OSS Distributions 	uint64_t                         extentRemaining;
154*c54f35caSApple OSS Distributions 	uint32_t                         lastRead;
155*c54f35caSApple OSS Distributions 	uint64_t                         readEnd;
156*c54f35caSApple OSS Distributions 	uint32_t                            flags;
157*c54f35caSApple OSS Distributions 	uint64_t                            fileSize;
158*c54f35caSApple OSS Distributions 	uint64_t                            position;
159*c54f35caSApple OSS Distributions 	uint64_t                            extentPosition;
160*c54f35caSApple OSS Distributions 	uint64_t                            encryptStart;
161*c54f35caSApple OSS Distributions 	uint64_t                            encryptEnd;
162*c54f35caSApple OSS Distributions 	uint64_t                            cryptBytes;
163*c54f35caSApple OSS Distributions 	AbsoluteTime                        cryptTime;
164*c54f35caSApple OSS Distributions 	IOPolledFileExtent *                extentMap;
165*c54f35caSApple OSS Distributions 	IOPolledFileExtent *                currentExtent;
166*c54f35caSApple OSS Distributions 	bool                                allocated;
167*c54f35caSApple OSS Distributions };
168*c54f35caSApple OSS Distributions 
169*c54f35caSApple OSS Distributions typedef struct IOPolledFileIOVars IOPolledFileIOVars;
170*c54f35caSApple OSS Distributions 
171*c54f35caSApple OSS Distributions struct IOPolledFileCryptVars {
172*c54f35caSApple OSS Distributions 	uint8_t aes_iv[AES_BLOCK_SIZE];
173*c54f35caSApple OSS Distributions 	aes_ctx ctx;
174*c54f35caSApple OSS Distributions };
175*c54f35caSApple OSS Distributions typedef struct IOPolledFileCryptVars IOPolledFileCryptVars;
176*c54f35caSApple OSS Distributions 
177*c54f35caSApple OSS Distributions #if defined(__cplusplus)
178*c54f35caSApple OSS Distributions 
179*c54f35caSApple OSS Distributions IOReturn IOPolledFileOpen(const char * filename,
180*c54f35caSApple OSS Distributions     uint32_t flags,
181*c54f35caSApple OSS Distributions     uint64_t setFileSize, uint64_t fsFreeSize,
182*c54f35caSApple OSS Distributions     void * write_file_addr, size_t write_file_len,
183*c54f35caSApple OSS Distributions     IOPolledFileIOVars ** fileVars,
184*c54f35caSApple OSS Distributions     LIBKERN_RETURNS_RETAINED OSData ** imagePath,
185*c54f35caSApple OSS Distributions     uint8_t * volumeCryptKey, size_t * keySize);
186*c54f35caSApple OSS Distributions 
187*c54f35caSApple OSS Distributions IOReturn IOPolledFileOpen(const char * filename,
188*c54f35caSApple OSS Distributions     uint32_t flags,
189*c54f35caSApple OSS Distributions     uint64_t setFileSize, uint64_t fsFreeSize,
190*c54f35caSApple OSS Distributions     void * write_file_addr, size_t write_file_len,
191*c54f35caSApple OSS Distributions     IOPolledFileIOVars ** fileVars,
192*c54f35caSApple OSS Distributions     OSSharedPtr<OSData>& imagePath,
193*c54f35caSApple OSS Distributions     uint8_t * volumeCryptKey, size_t * keySize);
194*c54f35caSApple OSS Distributions 
195*c54f35caSApple OSS Distributions IOReturn IOPolledFileClose(IOPolledFileIOVars ** pVars,
196*c54f35caSApple OSS Distributions     off_t write_offset, void * addr, size_t write_length,
197*c54f35caSApple OSS Distributions     off_t discard_offset, off_t discard_end, bool unlink);
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions IOReturn IOPolledFilePollersSetup(IOPolledFileIOVars * vars, uint32_t openState);
200*c54f35caSApple OSS Distributions 
201*c54f35caSApple OSS Distributions LIBKERN_RETURNS_NOT_RETAINED IOMemoryDescriptor * IOPolledFileGetIOBuffer(IOPolledFileIOVars * vars);
202*c54f35caSApple OSS Distributions 
203*c54f35caSApple OSS Distributions #endif /* defined(__cplusplus) */
204*c54f35caSApple OSS Distributions 
205*c54f35caSApple OSS Distributions #if defined(__cplusplus)
206*c54f35caSApple OSS Distributions #define __C     "C"
207*c54f35caSApple OSS Distributions #else
208*c54f35caSApple OSS Distributions #define __C
209*c54f35caSApple OSS Distributions #endif
210*c54f35caSApple OSS Distributions 
211*c54f35caSApple OSS Distributions extern __C IOReturn IOPolledFileSeek(IOPolledFileIOVars * vars, uint64_t position);
212*c54f35caSApple OSS Distributions 
213*c54f35caSApple OSS Distributions extern __C IOReturn IOPolledFileWrite(IOPolledFileIOVars * vars,
214*c54f35caSApple OSS Distributions     const uint8_t * bytes, IOByteCount size,
215*c54f35caSApple OSS Distributions     IOPolledFileCryptVars * cryptvars);
216*c54f35caSApple OSS Distributions extern __C IOReturn IOPolledFileRead(IOPolledFileIOVars * vars,
217*c54f35caSApple OSS Distributions     uint8_t * bytes, IOByteCount size,
218*c54f35caSApple OSS Distributions     IOPolledFileCryptVars * cryptvars);
219*c54f35caSApple OSS Distributions 
220*c54f35caSApple OSS Distributions extern __C IOReturn IOPolledFileFlush(IOPolledFileIOVars * vars);
221*c54f35caSApple OSS Distributions 
222*c54f35caSApple OSS Distributions extern __C IOReturn IOPolledFilePollersOpen(IOPolledFileIOVars * vars, uint32_t state, bool abortable);
223*c54f35caSApple OSS Distributions 
224*c54f35caSApple OSS Distributions extern __C IOReturn IOPolledFilePollersClose(IOPolledFileIOVars * vars, uint32_t state);
225*c54f35caSApple OSS Distributions 
226*c54f35caSApple OSS Distributions extern __C IOReturn IOPolledFilePollersSetEncryptionKey(IOPolledFileIOVars * vars,
227*c54f35caSApple OSS Distributions     const uint8_t * key, size_t keySize);
228*c54f35caSApple OSS Distributions 
229*c54f35caSApple OSS Distributions extern __C IOPolledFileIOVars * gCoreFileVars;
230*c54f35caSApple OSS Distributions 
231*c54f35caSApple OSS Distributions #ifdef _SYS_CONF_H_
232*c54f35caSApple OSS Distributions 
233*c54f35caSApple OSS Distributions __BEGIN_DECLS
234*c54f35caSApple OSS Distributions 
235*c54f35caSApple OSS Distributions typedef void (*kern_get_file_extents_callback_t)(void * ref, uint64_t start, uint64_t size);
236*c54f35caSApple OSS Distributions 
237*c54f35caSApple OSS Distributions struct kern_direct_file_io_ref_t *
238*c54f35caSApple OSS Distributions kern_open_file_for_direct_io(const char * name,
239*c54f35caSApple OSS Distributions     uint32_t flags,
240*c54f35caSApple OSS Distributions     kern_get_file_extents_callback_t callback,
241*c54f35caSApple OSS Distributions     void * callback_ref,
242*c54f35caSApple OSS Distributions     off_t set_file_size,
243*c54f35caSApple OSS Distributions     off_t fs_free_size,
244*c54f35caSApple OSS Distributions     off_t write_file_offset,
245*c54f35caSApple OSS Distributions     void * write_file_addr,
246*c54f35caSApple OSS Distributions     size_t write_file_len,
247*c54f35caSApple OSS Distributions     dev_t * partition_device_result,
248*c54f35caSApple OSS Distributions     dev_t * image_device_result,
249*c54f35caSApple OSS Distributions     uint64_t * partitionbase_result,
250*c54f35caSApple OSS Distributions     uint64_t * maxiocount_result,
251*c54f35caSApple OSS Distributions     uint32_t * oflags);
252*c54f35caSApple OSS Distributions void
253*c54f35caSApple OSS Distributions kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
254*c54f35caSApple OSS Distributions     off_t write_offset, void * addr, size_t write_length,
255*c54f35caSApple OSS Distributions     off_t discard_offset, off_t discard_end, bool unlink);
256*c54f35caSApple OSS Distributions int
257*c54f35caSApple OSS Distributions kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
258*c54f35caSApple OSS Distributions int
259*c54f35caSApple OSS Distributions kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag);
260*c54f35caSApple OSS Distributions 
261*c54f35caSApple OSS Distributions struct mount *
262*c54f35caSApple OSS Distributions kern_file_mount(struct kern_direct_file_io_ref_t * ref);
263*c54f35caSApple OSS Distributions 
264*c54f35caSApple OSS Distributions enum{
265*c54f35caSApple OSS Distributions 	kIOPolledFileMountChangeMount = 0x00000101,
266*c54f35caSApple OSS Distributions 	kIOPolledFileMountChangeUnmount = 0x00000102,
267*c54f35caSApple OSS Distributions 	kIOPolledFileMountChangeWillResize = 0x00000201,
268*c54f35caSApple OSS Distributions 	kIOPolledFileMountChangeDidResize = 0x00000202,
269*c54f35caSApple OSS Distributions };
270*c54f35caSApple OSS Distributions extern void IOPolledFileMountChange(struct mount * mp, uint32_t op);
271*c54f35caSApple OSS Distributions 
272*c54f35caSApple OSS Distributions __END_DECLS
273*c54f35caSApple OSS Distributions 
274*c54f35caSApple OSS Distributions #endif /* _SYS_CONF_H_ */
275*c54f35caSApple OSS Distributions 
276*c54f35caSApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
277*c54f35caSApple OSS Distributions 
278*c54f35caSApple OSS Distributions #endif /* _IOPOLLEDINTERFACE_H_ */
279