]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOBufferMemoryDescriptor.h
xnu-6153.121.1.tar.gz
[apple/xnu.git] / iokit / IOKit / IOBufferMemoryDescriptor.h
1 /*
2 * Copyright (c) 1998-2016 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 #ifndef _IOBUFFERMEMORYDESCRIPTOR_H
29 #define _IOBUFFERMEMORYDESCRIPTOR_H
30
31 #include <IOKit/IOMemoryDescriptor.h>
32 #include <DriverKit/IOBufferMemoryDescriptor.h>
33
34 enum {
35 kIOMemoryPhysicallyContiguous = 0x00000010,
36 kIOMemoryPageable = 0x00000020,
37 kIOMemoryPurgeable = 0x00000040,
38 kIOMemoryHostPhysicallyContiguous = 0x00000080,
39 kIOMemorySharingTypeMask = 0x000f0000,
40 kIOMemoryUnshared = 0x00000000,
41 kIOMemoryKernelUserShared = 0x00010000,
42 // shared IOMemoryDescriptor options for IOBufferMemoryDescriptor:
43 kIOBufferDescriptorMemoryFlags = kIOMemoryDirectionMask
44 #ifdef XNU_KERNEL_PRIVATE
45 | kIOMemoryAutoPrepare
46 #endif
47 | kIOMemoryThreadSafe
48 | kIOMemoryClearEncrypt
49 | kIOMemoryMapperNone
50 | kIOMemoryUseReserve
51 };
52
53 #define _IOBUFFERMEMORYDESCRIPTOR_INTASKWITHOPTIONS_ 1
54 #define _IOBUFFERMEMORYDESCRIPTOR_HOSTPHYSICALLYCONTIGUOUS_ 1
55 #define IOBUFFERMEMORYDESCRIPTOR_SUPPORTS_INTASKWITHOPTIONS_TAGS 1
56 /*!
57 * @class IOBufferMemoryDescriptor
58 * @abstract Provides a simple memory descriptor that allocates its own buffer memory.
59 */
60
61 class IOBufferMemoryDescriptor : public IOGeneralMemoryDescriptor
62 {
63 OSDeclareDefaultStructorsWithDispatch(IOBufferMemoryDescriptor);
64
65 private:
66 /*! @struct ExpansionData
67 * @discussion This structure will be used to expand the capablilties of this class in the future.
68 */
69 struct ExpansionData {
70 IOMemoryMap * map;
71 };
72
73 /*! @var reserved
74 * Reserved for future use. (Internal use only) */
75 APPLE_KEXT_WSHADOW_PUSH;
76 ExpansionData * reserved;
77
78 protected:
79 void * _buffer;
80 vm_size_t _capacity;
81 vm_offset_t _alignment;
82 IOOptionBits _options;
83 private:
84 uintptr_t _internalReserved;
85 unsigned _internalFlags;
86 APPLE_KEXT_WSHADOW_POP;
87
88 private:
89 #ifndef __LP64__
90 virtual bool initWithOptions(
91 IOOptionBits options,
92 vm_size_t capacity,
93 vm_offset_t alignment,
94 task_t inTask) APPLE_KEXT_DEPRECATED; /* use withOptions() instead */
95 #endif /* !__LP64__ */
96
97 public:
98 virtual bool initWithPhysicalMask(
99 task_t inTask,
100 IOOptionBits options,
101 mach_vm_size_t capacity,
102 mach_vm_address_t alignment,
103 mach_vm_address_t physicalMask);
104
105 #ifdef __LP64__
106 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 0);
107 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 1);
108 #else /* !__LP64__ */
109 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 0);
110 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 1);
111 #endif /* !__LP64__ */
112 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 2);
113 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 3);
114 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 4);
115 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 5);
116 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 6);
117 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 7);
118 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 8);
119 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 9);
120 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 10);
121 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 11);
122 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 12);
123 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 13);
124 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 14);
125 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 15);
126
127 protected:
128 virtual void free() APPLE_KEXT_OVERRIDE;
129
130 public:
131
132 /*
133 * withOptions:
134 *
135 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
136 * hold capacity bytes. The descriptor's length is initially set to the
137 * capacity.
138 */
139 #ifndef __LP64__
140 virtual bool initWithOptions( IOOptionBits options,
141 vm_size_t capacity,
142 vm_offset_t alignment) APPLE_KEXT_DEPRECATED; /* use withOptions() instead */
143 #endif /* !__LP64__ */
144
145 static IOBufferMemoryDescriptor * withCopy(
146 task_t inTask,
147 IOOptionBits options,
148 vm_map_t sourceMap,
149 mach_vm_address_t source,
150 mach_vm_size_t size);
151
152 static IOBufferMemoryDescriptor * withOptions( IOOptionBits options,
153 vm_size_t capacity,
154 vm_offset_t alignment = 1);
155
156 /*! @function inTaskWithOptions
157 * @abstract Creates a memory buffer with memory descriptor for that buffer.
158 * @discussion Added in Mac OS X 10.2, this method allocates a memory buffer with a given size and alignment in the task's address space specified, and returns a memory descriptor instance representing the memory. It is recommended that memory allocated for I/O or sharing via mapping be created via IOBufferMemoryDescriptor. Options passed with the request specify the kind of memory to be allocated - pageablity and sharing are specified with option bits. This function may block and so should not be called from interrupt level or while a simple lock is held.
159 * @param inTask The task the buffer will be allocated in.
160 * @param options Options for the allocation:<br>
161 * kIODirectionOut, kIODirectionIn - set the direction of the I/O transfer.<br>
162 * kIOMemoryPhysicallyContiguous - pass to request memory be physically contiguous. This option is heavily discouraged. The request may fail if memory is fragmented, may cause large amounts of paging activity, and may take a very long time to execute.<br>
163 * kIOMemoryPageable - pass to request memory be non-wired - the default for kernel allocated memory is wired.<br>
164 * kIOMemoryPurgeable - pass to request memory that may later have its purgeable state set with IOMemoryDescriptor::setPurgeable. Only supported for kIOMemoryPageable allocations.<br>
165 * kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.<br>
166 * kIOMapInhibitCache - allocate memory with inhibited cache setting. <br>
167 * kIOMapWriteThruCache - allocate memory with writethru cache setting. <br>
168 * kIOMapCopybackCache - allocate memory with copyback cache setting. <br>
169 * kIOMapWriteCombineCache - allocate memory with writecombined cache setting.
170 * @param capacity The number of bytes to allocate.
171 * @param alignment The minimum required alignment of the buffer in bytes - 1 is the default for no required alignment. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
172 * @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
173
174 static IOBufferMemoryDescriptor * inTaskWithOptions(
175 task_t inTask,
176 IOOptionBits options,
177 vm_size_t capacity,
178 vm_offset_t alignment = 1);
179
180 /*! @function inTaskWithOptions
181 * @abstract Creates a memory buffer with memory descriptor for that buffer.
182 * @discussion Added in Mac OS X 10.2, this method allocates a memory buffer with a given size and alignment in the task's address space specified, and returns a memory descriptor instance representing the memory. It is recommended that memory allocated for I/O or sharing via mapping be created via IOBufferMemoryDescriptor. Options passed with the request specify the kind of memory to be allocated - pageablity and sharing are specified with option bits. This function may block and so should not be called from interrupt level or while a simple lock is held.
183 * @param inTask The task the buffer will be allocated in.
184 * @param options Options for the allocation:<br>
185 * kIODirectionOut, kIODirectionIn - set the direction of the I/O transfer.<br>
186 * kIOMemoryPhysicallyContiguous - pass to request memory be physically contiguous. This option is heavily discouraged. The request may fail if memory is fragmented, may cause large amounts of paging activity, and may take a very long time to execute.<br>
187 * kIOMemoryPageable - pass to request memory be non-wired - the default for kernel allocated memory is wired.<br>
188 * kIOMemoryPurgeable - pass to request memory that may later have its purgeable state set with IOMemoryDescriptor::setPurgeable. Only supported for kIOMemoryPageable allocations.<br>
189 * kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.<br>
190 * kIOMapInhibitCache - allocate memory with inhibited cache setting. <br>
191 * kIOMapWriteThruCache - allocate memory with writethru cache setting. <br>
192 * kIOMapCopybackCache - allocate memory with copyback cache setting. <br>
193 * kIOMapWriteCombineCache - allocate memory with writecombined cache setting.
194 * @param capacity The number of bytes to allocate.
195 * @param alignment The minimum required alignment of the buffer in bytes - 1 is the default for no required alignment. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
196 * @param kernTag The kernel memory tag
197 * @param userTag The user memory tag
198 * @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
199
200 static IOBufferMemoryDescriptor * inTaskWithOptions(
201 task_t inTask,
202 IOOptionBits options,
203 vm_size_t capacity,
204 vm_offset_t alignment,
205 uint32_t kernTag,
206 uint32_t userTag);
207
208 /*! @function inTaskWithPhysicalMask
209 * @abstract Creates a memory buffer with memory descriptor for that buffer.
210 * @discussion Added in Mac OS X 10.5, this method allocates a memory buffer with a given size and alignment in the task's address space specified, and returns a memory descriptor instance representing the memory. It is recommended that memory allocated for I/O or sharing via mapping be created via IOBufferMemoryDescriptor. Options passed with the request specify the kind of memory to be allocated - pageablity and sharing are specified with option bits. This function may block and so should not be called from interrupt level or while a simple lock is held.
211 * @param inTask The task the buffer will be mapped in. Pass NULL to create memory unmapped in any task (eg. for use as a DMA buffer).
212 * @param options Options for the allocation:<br>
213 * kIODirectionOut, kIODirectionIn - set the direction of the I/O transfer.<br>
214 * kIOMemoryPhysicallyContiguous - pass to request memory be physically contiguous. This option is heavily discouraged. The request may fail if memory is fragmented, may cause large amounts of paging activity, and may take a very long time to execute.<br>
215 * kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.<br>
216 * kIOMapInhibitCache - allocate memory with inhibited cache setting. <br>
217 * kIOMapWriteThruCache - allocate memory with writethru cache setting. <br>
218 * kIOMapCopybackCache - allocate memory with copyback cache setting. <br>
219 * kIOMapWriteCombineCache - allocate memory with writecombined cache setting.
220 * @param capacity The number of bytes to allocate.
221 * @param physicalMask The buffer will be allocated with pages such that physical addresses will only have bits set present in physicalMask. For example, pass 0x00000000FFFFFFFFULL for a buffer to be accessed by hardware that has 32 address bits.
222 * @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
223
224 static IOBufferMemoryDescriptor * inTaskWithPhysicalMask(
225 task_t inTask,
226 IOOptionBits options,
227 mach_vm_size_t capacity,
228 mach_vm_address_t physicalMask);
229
230 /*
231 * withCapacity:
232 *
233 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
234 * hold capacity bytes. The descriptor's length is initially set to the
235 * capacity.
236 */
237 static IOBufferMemoryDescriptor * withCapacity(
238 vm_size_t capacity,
239 IODirection withDirection,
240 bool withContiguousMemory = false);
241 #ifndef __LP64__
242 virtual bool initWithBytes(const void * bytes,
243 vm_size_t withLength,
244 IODirection withDirection,
245 bool withContiguousMemory = false) APPLE_KEXT_DEPRECATED; /* use withBytes() instead */
246 #endif /* !__LP64__ */
247
248 /*
249 * withBytes:
250 *
251 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
252 * The descriptor's length and capacity are set to the input buffer's size.
253 */
254 static IOBufferMemoryDescriptor * withBytes(
255 const void * bytes,
256 vm_size_t withLength,
257 IODirection withDirection,
258 bool withContiguousMemory = false);
259
260 /*
261 * setLength:
262 *
263 * Change the buffer length of the memory descriptor. When a new buffer
264 * is created, the initial length of the buffer is set to be the same as
265 * the capacity. The length can be adjusted via setLength for a shorter
266 * transfer (there is no need to create more buffer descriptors when you
267 * can reuse an existing one, even for different transfer sizes). Note
268 * that the specified length must not exceed the capacity of the buffer.
269 */
270 virtual void setLength(vm_size_t length);
271
272 /*
273 * setDirection:
274 *
275 * Change the direction of the transfer. This method allows one to redirect
276 * the descriptor's transfer direction. This eliminates the need to destroy
277 * and create new buffers when different transfer directions are needed.
278 */
279 virtual void setDirection(IODirection direction);
280
281 /*
282 * getCapacity:
283 *
284 * Get the buffer capacity
285 */
286 virtual vm_size_t getCapacity() const;
287
288 /*
289 * getBytesNoCopy:
290 *
291 * Return the virtual address of the beginning of the buffer
292 */
293 virtual void *getBytesNoCopy();
294
295 /*
296 * getBytesNoCopy:
297 *
298 * Return the virtual address of an offset from the beginning of the buffer
299 */
300 virtual void *getBytesNoCopy(vm_size_t start, vm_size_t withLength);
301
302 /*
303 * appendBytes:
304 *
305 * Add some data to the end of the buffer. This method automatically
306 * maintains the memory descriptor buffer length. Note that appendBytes
307 * will not copy past the end of the memory descriptor's current capacity.
308 */
309 virtual bool appendBytes(const void *bytes, vm_size_t withLength);
310
311 #ifndef __LP64__
312 virtual void * getVirtualSegment(IOByteCount offset,
313 IOByteCount * length) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED; /* use getBytesNoCopy() instead */
314 #endif /* !__LP64__ */
315 };
316
317 #endif /* !_IOBUFFERMEMORYDESCRIPTOR_H */