]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOMemoryDescriptor.h
551ecfff6f28257b1f49111cad01121ef13474c5
[apple/xnu.git] / iokit / IOKit / IOMemoryDescriptor.h
1 /*
2 * Copyright (c) 1998-2000 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 #ifndef _IOMEMORYDESCRIPTOR_H
29 #define _IOMEMORYDESCRIPTOR_H
30
31 #include <sys/cdefs.h>
32
33 #include <IOKit/IOTypes.h>
34 #include <libkern/c++/OSContainers.h>
35
36 __BEGIN_DECLS
37 #include <mach/memory_object_types.h>
38 __END_DECLS
39
40 struct IOPhysicalRange
41 {
42 IOPhysicalAddress address;
43 IOByteCount length;
44 };
45
46 class IOMemoryMap;
47 class IOMapper;
48
49 /*
50 * Direction of transfer, with respect to the described memory.
51 */
52 enum IODirection
53 {
54 kIODirectionNone = 0x0, // same as VM_PROT_NONE
55 kIODirectionIn = 0x1, // User land 'read', same as VM_PROT_READ
56 kIODirectionOut = 0x2, // User land 'write', same as VM_PROT_WRITE
57 kIODirectionOutIn = kIODirectionOut | kIODirectionIn,
58 kIODirectionInOut = kIODirectionIn | kIODirectionOut
59 };
60
61 /*
62 * IOOptionBits used in the withOptions variant
63 */
64 enum {
65 kIOMemoryDirectionMask = 0x00000007,
66 kIOMemoryAutoPrepare = 0x00000008, // Shared with Buffer MD
67
68 kIOMemoryTypeVirtual = 0x00000010,
69 kIOMemoryTypePhysical = 0x00000020,
70 kIOMemoryTypeUPL = 0x00000030,
71 kIOMemoryTypePersistentMD = 0x00000040, // Persistent Memory Descriptor
72 kIOMemoryTypeUIO = 0x00000050,
73 kIOMemoryTypeMask = 0x000000f0,
74
75 kIOMemoryAsReference = 0x00000100,
76 kIOMemoryBufferPageable = 0x00000400,
77 kIOMemoryDontMap = 0x00000800,
78 kIOMemoryPersistent = 0x00010000
79 };
80
81 #define kIOMapperNone ((IOMapper *) -1)
82 #define kIOMapperSystem ((IOMapper *) 0)
83
84 enum
85 {
86 kIOMemoryPurgeableKeepCurrent = 1,
87 kIOMemoryPurgeableNonVolatile = 2,
88 kIOMemoryPurgeableVolatile = 3,
89 kIOMemoryPurgeableEmpty = 4
90 };
91 enum
92 {
93 kIOMemoryIncoherentIOFlush = 1,
94 kIOMemoryIncoherentIOStore = 2,
95 };
96
97 /*! @class IOMemoryDescriptor : public OSObject
98 @abstract An abstract base class defining common methods for describing physical or virtual memory.
99 @discussion The IOMemoryDescriptor object represents a buffer or range of memory, specified as one or more physical or virtual address ranges. It contains methods to return the memory's physically contiguous segments (fragments), for use with the IOMemoryCursor, and methods to map the memory into any address space with caching and placed mapping options. */
100
101 class IOMemoryDescriptor : public OSObject
102 {
103 friend class _IOMemoryMap;
104 friend class IOSubMemoryDescriptor;
105
106 OSDeclareDefaultStructors(IOMemoryDescriptor);
107
108 protected:
109 /*! @struct ExpansionData
110 @discussion This structure will be used to expand the capablilties of this class in the future.
111 */
112 struct ExpansionData {
113 void * devicePager;
114 unsigned int pagerContig:1;
115 unsigned int unused:31;
116 IOMemoryDescriptor * memory;
117 };
118
119 /*! @var reserved
120 Reserved for future use. (Internal use only) */
121 ExpansionData * reserved;
122
123 protected:
124 OSSet * _mappings;
125 IOOptionBits _flags;
126 void * _memEntry;
127
128 IODirection _direction; /* DEPRECATED: use _flags instead. direction of transfer */
129 IOByteCount _length; /* length of all ranges */
130 IOOptionBits _tag;
131
132 public:
133
134 virtual IOPhysicalAddress getSourceSegment( IOByteCount offset,
135 IOByteCount * length );
136 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 0);
137
138 /*! @function initWithOptions
139 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
140 @discussion Note this function can be used to re-init a previously created memory descriptor.
141 @result true on success, false on failure. */
142 virtual bool initWithOptions(void * buffers,
143 UInt32 count,
144 UInt32 offset,
145 task_t task,
146 IOOptionBits options,
147 IOMapper * mapper = 0);
148 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 1);
149
150 virtual addr64_t getPhysicalSegment64( IOByteCount offset,
151 IOByteCount * length );
152 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 2);
153
154
155 /*! @function setPurgeable
156 @abstract Control the purgeable status of a memory descriptors memory.
157 @discussion Buffers may be allocated with the ability to have their purgeable status changed - IOBufferMemoryDescriptor with the kIOMemoryPurgeable option, VM_FLAGS_PURGEABLE may be passed to vm_allocate() in user space to allocate such buffers. The purgeable status of such a buffer may be controlled with setPurgeable(). The process of making a purgeable memory descriptor non-volatile and determining its previous state is atomic - if a purgeable memory descriptor is made nonvolatile and the old state is returned as kIOMemoryPurgeableVolatile, then the memory's previous contents are completely intact and will remain so until the memory is made volatile again. If the old state is returned as kIOMemoryPurgeableEmpty then the memory was reclaimed while it was in a volatile state and its previous contents have been lost.
158 @param newState - the desired new purgeable state of the memory:<br>
159 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
160 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
161 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
162 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
163 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
164 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
165 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
166 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
167 @result An IOReturn code. */
168
169 virtual IOReturn setPurgeable( IOOptionBits newState,
170 IOOptionBits * oldState );
171 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 3);
172
173 /*! @function performOperation
174 @abstract Perform an operation on the memory descriptor's memory.
175 @discussion This method performs some operation on a range of the memory descriptor's memory. When a memory descriptor's memory is not mapped, it should be more efficient to use this method than mapping the memory to perform the operation virtually.
176 @param options The operation to perform on the memory:<br>
177 kIOMemoryIncoherentIOFlush - pass this option to store to memory and flush any data in the processor cache for the memory range, with synchronization to ensure the data has passed through all levels of processor cache. It may not be supported on all architectures. This type of flush may be used for non-coherent I/O such as AGP - it is NOT required for PCI coherent operations. The memory descriptor must have been previously prepared.<br>
178 kIOMemoryIncoherentIOStore - pass this option to store to memory any data in the processor cache for the memory range, with synchronization to ensure the data has passed through all levels of processor cache. It may not be supported on all architectures. This type of flush may be used for non-coherent I/O such as AGP - it is NOT required for PCI coherent operations. The memory descriptor must have been previously prepared.
179 @param offset A byte offset into the memory descriptor's memory.
180 @param length The length of the data range.
181 @result An IOReturn code. */
182
183 virtual IOReturn performOperation( IOOptionBits options,
184 IOByteCount offset, IOByteCount length );
185 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 4);
186
187 private:
188
189 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 5);
190 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 6);
191 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 7);
192 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 8);
193 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 9);
194 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 10);
195 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 11);
196 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 12);
197 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 13);
198 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 14);
199 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 15);
200
201 protected:
202 virtual void free();
203 public:
204 static void initialize( void );
205
206 public:
207 /*! @function withAddress
208 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
209 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map.
210 @param address The virtual address of the first byte in the memory.
211 @param withLength The length of memory.
212 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
213 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
214
215 static IOMemoryDescriptor * withAddress(void * address,
216 IOByteCount withLength,
217 IODirection withDirection);
218
219 /*! @function withAddress
220 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
221 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
222 @param address The virtual address of the first byte in the memory.
223 @param withLength The length of memory.
224 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
225 @param withTask The task the virtual ranges are mapped into.
226 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
227
228 static IOMemoryDescriptor * withAddress(vm_address_t address,
229 IOByteCount withLength,
230 IODirection withDirection,
231 task_t withTask);
232
233 /*! @function withPhysicalAddress
234 @abstract Create an IOMemoryDescriptor to describe one physical range.
235 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
236 @param address The physical address of the first byte in the memory.
237 @param withLength The length of memory.
238 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
239 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
240
241 static IOMemoryDescriptor * withPhysicalAddress(
242 IOPhysicalAddress address,
243 IOByteCount withLength,
244 IODirection withDirection );
245
246 /*! @function withRanges
247 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
248 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of virtual memory ranges each mapped into a specified source task.
249 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
250 @param withCount The member count of the ranges array.
251 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
252 @param withTask The task each of the virtual ranges are mapped into.
253 @param asReference If false, the IOMemoryDescriptor object will make a copy of the ranges array, otherwise, the array will be used in situ, avoiding an extra allocation.
254 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
255
256 static IOMemoryDescriptor * withRanges(IOVirtualRange * ranges,
257 UInt32 withCount,
258 IODirection withDirection,
259 task_t withTask,
260 bool asReference = false);
261
262 /*! @function withOptions
263 @abstract Master initialiser for all variants of memory descriptors.
264 @discussion This method creates and initializes an IOMemoryDescriptor for memory it has three main variants: Virtual, Physical & mach UPL. These variants are selected with the options parameter, see below. This memory descriptor needs to be prepared before it can be used to extract data from the memory described. However we temporarily have setup a mechanism that automatically prepares kernel_task memory descriptors at creation time.
265
266
267 @param buffers A pointer to an array of IOVirtualRanges or IOPhysicalRanges if the options:type is Virtual or Physical. For type UPL it is a upl_t returned by the mach/memory_object_types.h apis, primarily used internally by the UBC.
268
269 @param count options:type = Virtual or Physical count contains a count of the number of entires in the buffers array. For options:type = UPL this field contains a total length.
270
271 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
272
273 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
274
275 @param options
276 kIOMemoryDirectionMask (options:direction) This nibble indicates the I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
277 kIOMemoryTypeMask (options:type) kIOMemoryTypeVirtual, kIOMemoryTypePhysical, kIOMemoryTypeUPL Indicates that what type of memory basic memory descriptor to use. This sub-field also controls the interpretation of the buffers, count, offset & task parameters.
278 kIOMemoryAsReference For options:type = Virtual or Physical this indicate that the memory descriptor need not copy the ranges array into local memory. This is an optimisation to try to minimise unnecessary allocations.
279 kIOMemoryBufferPageable Only used by the IOBufferMemoryDescriptor as an indication that the kernel virtual memory is in fact pageable and we need to use the kernel pageable submap rather than the default map.
280 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
281
282 @param mapper Which IOMapper should be used to map the in-memory physical addresses into I/O space addresses. Defaults to 0 which indicates that the system mapper is to be used, if present.
283
284 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
285
286 static IOMemoryDescriptor *withOptions(void * buffers,
287 UInt32 count,
288 UInt32 offset,
289 task_t task,
290 IOOptionBits options,
291 IOMapper * mapper = 0);
292
293 /*! @function withPhysicalRanges
294 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
295 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
296 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
297 @param withCount The member count of the ranges array.
298 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
299 @param asReference If false, the IOMemoryDescriptor object will make a copy of the ranges array, otherwise, the array will be used in situ, avoiding an extra allocation.
300 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
301
302 static IOMemoryDescriptor * withPhysicalRanges(
303 IOPhysicalRange * ranges,
304 UInt32 withCount,
305 IODirection withDirection,
306 bool asReference = false);
307
308 /*! @function withSubRange
309 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
310 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a subrange of the specified memory descriptor. The parent memory descriptor is retained by the new descriptor.
311 @param of The parent IOMemoryDescriptor of which a subrange is to be used for the new descriptor, which will be retained by the subrange IOMemoryDescriptor.
312 @param offset A byte offset into the parent memory descriptor's memory.
313 @param length The length of the subrange.
314 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures. This is used over the direction of the parent descriptor.
315 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
316
317 static IOMemoryDescriptor * withSubRange(IOMemoryDescriptor *of,
318 IOByteCount offset,
319 IOByteCount length,
320 IODirection withDirection);
321
322 /*! @function withPersistentMemoryDescriptor
323 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
324 @discussion If the original memory descriptor's address and length is still backed by the same real memory, i.e. the user hasn't deallocated and the reallocated memory at the same address then the original memory descriptor is returned with a additional reference. Otherwise we build a totally new memory descriptor with the same characteristics as the previous one but with a new view of the vm. Note not legal to call this function with anything except an IOGeneralMemoryDescriptor that was created with the kIOMemoryPersistent option.
325 @param originalMD The memory descriptor to be duplicated.
326 @result Either the original memory descriptor with an additional retain or a new memory descriptor, 0 for a bad original memory descriptor or some other resource shortage. */
327 static IOMemoryDescriptor *
328 withPersistentMemoryDescriptor(IOMemoryDescriptor *originalMD);
329
330 /*! @function initWithAddress
331 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
332 @discussion This method initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map. An IOMemoryDescriptor can be re-used by calling initWithAddress or initWithRanges again on an existing instance -- note this behavior is not commonly supported in other IOKit classes, although it is supported here.
333 @param address The virtual address of the first byte in the memory.
334 @param withLength The length of memory.
335 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
336 @result true on success, false on failure. */
337
338 virtual bool initWithAddress(void * address,
339 IOByteCount withLength,
340 IODirection withDirection) = 0;
341
342 /*! @function initWithAddress
343 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
344 @discussion This method initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map. An IOMemoryDescriptor can be re-used by calling initWithAddress or initWithRanges again on an existing instance -- note this behavior is not commonly supported in other IOKit classes, although it is supported here.
345 @param address The virtual address of the first byte in the memory.
346 @param withLength The length of memory.
347 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
348 @param withTask The task the virtual ranges are mapped into.
349 @result true on success, false on failure. */
350
351 virtual bool initWithAddress(vm_address_t address,
352 IOByteCount withLength,
353 IODirection withDirection,
354 task_t withTask) = 0;
355
356 /*! @function initWithPhysicalAddress
357 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
358 @discussion This method initializes an IOMemoryDescriptor for memory consisting of a single physical memory range. An IOMemoryDescriptor can be re-used by calling initWithAddress or initWithRanges again on an existing instance -- note this behavior is not commonly supported in other IOKit classes, although it is supported here.
359 @param address The physical address of the first byte in the memory.
360 @param withLength The length of memory.
361 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
362 @result true on success, false on failure. */
363
364 virtual bool initWithPhysicalAddress(
365 IOPhysicalAddress address,
366 IOByteCount withLength,
367 IODirection withDirection ) = 0;
368
369 /*! @function initWithRanges
370 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
371 @discussion This method initializes an IOMemoryDescriptor for memory consisting of an array of virtual memory ranges each mapped into a specified source task. An IOMemoryDescriptor can be re-used by calling initWithAddress or initWithRanges again on an existing instance -- note this behavior is not commonly supported in other IOKit classes, although it is supported here.
372 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
373 @param withCount The member count of the ranges array.
374 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
375 @param withTask The task each of the virtual ranges are mapped into.
376 @param asReference If false, the IOMemoryDescriptor object will make a copy of the ranges array, otherwise, the array will be used in situ, avoiding an extra allocation.
377 @result true on success, false on failure. */
378
379 virtual bool initWithRanges(IOVirtualRange * ranges,
380 UInt32 withCount,
381 IODirection withDirection,
382 task_t withTask,
383 bool asReference = false) = 0;
384
385 /*! @function initWithPhysicalRanges
386 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
387 @discussion This method initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges. An IOMemoryDescriptor can be re-used by calling initWithAddress or initWithRanges again on an existing instance -- note this behavior is not commonly supported in other IOKit classes, although it is supported here.
388 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
389 @param withCount The member count of the ranges array.
390 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
391 @param asReference If false, the IOMemoryDescriptor object will make a copy of the ranges array, otherwise, the array will be used in situ, avoiding an extra allocation.
392 @result true on success, false on failure. */
393
394 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
395 UInt32 withCount,
396 IODirection withDirection,
397 bool asReference = false) = 0;
398
399 /*! @function getDirection
400 @abstract Accessor to get the direction the memory descriptor was created with.
401 @discussion This method returns the direction the memory descriptor was created with.
402 @result The direction. */
403
404 virtual IODirection getDirection() const;
405
406 /*! @function getLength
407 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
408 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
409 @result The byte count. */
410
411 virtual IOByteCount getLength() const;
412
413 /*! @function setTag
414 @abstract Set the tag for the memory descriptor.
415 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
416 @param tag The tag. */
417
418 virtual void setTag( IOOptionBits tag );
419
420 /*! @function getTag
421 @abstract Accessor to the retrieve the tag for the memory descriptor.
422 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
423 @result The tag. */
424
425 virtual IOOptionBits getTag( void );
426
427 /*! @function readBytes
428 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
429 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
430 @param offset A byte offset into the memory descriptor's memory.
431 @param bytes The caller supplied buffer to copy the data to.
432 @param withLength The length of the data to copy.
433 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
434
435 virtual IOByteCount readBytes(IOByteCount offset,
436 void * bytes, IOByteCount withLength);
437
438 /*! @function writeBytes
439 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
440 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
441 @param offset A byte offset into the memory descriptor's memory.
442 @param bytes The caller supplied buffer to copy the data from.
443 @param withLength The length of the data to copy.
444 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
445
446 virtual IOByteCount writeBytes(IOByteCount offset,
447 const void * bytes, IOByteCount withLength);
448
449 /*! @function getPhysicalSegment
450 @abstract Break a memory descriptor into its physically contiguous segments.
451 @discussion This method returns the physical address of the byte at the given offset into the memory, and optionally the length of the physically contiguous segment from that offset.
452 @param offset A byte offset into the memory whose physical address to return.
453 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
454 @result A physical address, or zero if the offset is beyond the length of the memory. */
455
456 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
457 IOByteCount * length) = 0;
458
459 /*! @function getPhysicalAddress
460 @abstract Return the physical address of the first byte in the memory.
461 @discussion This method returns the physical address of the first byte in the memory. It is most useful on memory known to be physically contiguous.
462 @result A physical address. */
463
464 /* inline */ IOPhysicalAddress getPhysicalAddress();
465 /* { return( getPhysicalSegment( 0, 0 )); } */
466
467 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
468 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
469 /* DEPRECATED */ IOByteCount * length) = 0;
470 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
471
472 /*! @function prepare
473 @abstract Prepare the memory for an I/O transfer.
474 @discussion This involves paging in the memory, if necessary, and wiring it down for the duration of the transfer. The complete() method completes the processing of the memory after the I/O transfer finishes. Note that the prepare call is not thread safe and it is expected that the client will more easily be able to guarantee single threading a particular memory descriptor.
475 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
476 @result An IOReturn code. */
477
478 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone) = 0;
479
480 /*! @function complete
481 @abstract Complete processing of the memory after an I/O transfer finishes.
482 @discussion This method should not be called unless a prepare was previously issued; the prepare() and complete() must occur in pairs, before and after an I/O transfer involving pageable memory. In 10.3 or greater systems the direction argument to complete is not longer respected. The direction is totally determined at prepare() time.
483 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
484 @result An IOReturn code. */
485
486 virtual IOReturn complete(IODirection forDirection = kIODirectionNone) = 0;
487
488 /*
489 * Mapping functions.
490 */
491
492 /*! @function map
493 @abstract Maps a IOMemoryDescriptor into a task.
494 @discussion This is the general purpose method to map all or part of the memory described by a memory descriptor into a task at any available address, or at a fixed address if possible. Caching & read-only options may be set for the mapping. The mapping is represented as a returned reference to a IOMemoryMap object, which may be shared if the mapping is compatible with an existing mapping of the IOMemoryDescriptor. The IOMemoryMap object returned should be released only when the caller has finished accessing the mapping, as freeing the object destroys the mapping.
495 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
496 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
497 @param options Mapping options are defined in IOTypes.h,<br>
498 kIOMapAnywhere should be passed if the mapping can be created anywhere. If not set, the atAddress parameter sets the location of the mapping, if it is available in the target map.<br>
499 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
500 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
501 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
502 kIOMapReference will only succeed if the mapping already exists, and the IOMemoryMap object is just an extra reference, ie. no new mapping will be created.<br>
503 kIOMapUnique allows a special kind of mapping to be created that may be used with the IOMemoryMap::redirect() API. These mappings will not be shared as is the default - there will always be a unique mapping created for the caller, not an existing mapping with an extra reference.<br>
504 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
505 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
506 @result A reference to an IOMemoryMap object representing the mapping, which can supply the virtual address of the mapping and other information. The mapping may be shared with multiple callers - multiple maps are avoided if a compatible one exists. The IOMemoryMap object returned should be released only when the caller has finished accessing the mapping, as freeing the object destroys the mapping. The IOMemoryMap instance also retains the IOMemoryDescriptor it maps while it exists. */
507
508 virtual IOMemoryMap * map(
509 task_t intoTask,
510 IOVirtualAddress atAddress,
511 IOOptionBits options,
512 IOByteCount offset = 0,
513 IOByteCount length = 0 );
514
515 /*! @function map
516 @abstract Maps a IOMemoryDescriptor into the kernel map.
517 @discussion This is a shortcut method to map all the memory described by a memory descriptor into the kernel map at any available address. See the full version of the map method for further details.
518 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
519 @result See the full version of the map method. */
520
521 virtual IOMemoryMap * map(
522 IOOptionBits options = 0 );
523
524 /*! @function setMapping
525 @abstract Establishes an already existing mapping.
526 @discussion This method tells the IOMemoryDescriptor about a mapping that exists, but was created elsewhere. It allows later callers of the map method to share this externally created mapping. The IOMemoryMap object returned is created to represent it. This method is not commonly needed.
527 @param task Address space in which the mapping exists.
528 @param mapAddress Virtual address of the mapping.
529 @param options Caching and read-only attributes of the mapping.
530 @result A IOMemoryMap object created to represent the mapping. */
531
532 virtual IOMemoryMap * setMapping(
533 task_t task,
534 IOVirtualAddress mapAddress,
535 IOOptionBits options = 0 );
536
537 // Following methods are private implementation
538
539 // make virtual
540 IOReturn redirect( task_t safeTask, bool redirect );
541
542 IOReturn handleFault(
543 void * pager,
544 vm_map_t addressMap,
545 IOVirtualAddress address,
546 IOByteCount sourceOffset,
547 IOByteCount length,
548 IOOptionBits options );
549
550 protected:
551 virtual IOMemoryMap * makeMapping(
552 IOMemoryDescriptor * owner,
553 task_t intoTask,
554 IOVirtualAddress atAddress,
555 IOOptionBits options,
556 IOByteCount offset,
557 IOByteCount length );
558
559 virtual void addMapping(
560 IOMemoryMap * mapping );
561
562 virtual void removeMapping(
563 IOMemoryMap * mapping );
564
565 virtual IOReturn doMap(
566 vm_map_t addressMap,
567 IOVirtualAddress * atAddress,
568 IOOptionBits options,
569 IOByteCount sourceOffset = 0,
570 IOByteCount length = 0 );
571
572 virtual IOReturn doUnmap(
573 vm_map_t addressMap,
574 IOVirtualAddress logical,
575 IOByteCount length );
576 };
577
578 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
579
580 /*! @class IOMemoryMap : public OSObject
581 @abstract An abstract base class defining common methods for describing a memory mapping.
582 @discussion The IOMemoryMap object represents a mapped range of memory, described by a IOMemoryDescriptor. The mapping may be in the kernel or a non-kernel task and has processor cache mode attributes. IOMemoryMap instances are created by IOMemoryDescriptor when it creates mappings in its map method, and returned to the caller. */
583
584 class IOMemoryMap : public OSObject
585 {
586 OSDeclareAbstractStructors(IOMemoryMap)
587
588 public:
589 /*! @function getVirtualAddress
590 @abstract Accessor to the virtual address of the first byte in the mapping.
591 @discussion This method returns the virtual address of the first byte in the mapping.
592 @result A virtual address. */
593
594 virtual IOVirtualAddress getVirtualAddress() = 0;
595
596 /*! @function getPhysicalSegment
597 @abstract Break a mapping into its physically contiguous segments.
598 @discussion This method returns the physical address of the byte at the given offset into the mapping, and optionally the length of the physically contiguous segment from that offset. It functions similarly to IOMemoryDescriptor::getPhysicalSegment.
599 @param offset A byte offset into the mapping whose physical address to return.
600 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
601 @result A physical address, or zero if the offset is beyond the length of the mapping. */
602
603 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
604 IOByteCount * length) = 0;
605
606 /*! @function getPhysicalAddress
607 @abstract Return the physical address of the first byte in the mapping.
608 @discussion This method returns the physical address of the first byte in the mapping. It is most useful on mappings known to be physically contiguous.
609 @result A physical address. */
610
611 /* inline */ IOPhysicalAddress getPhysicalAddress();
612 /* { return( getPhysicalSegment( 0, 0 )); } */
613
614 /*! @function getLength
615 @abstract Accessor to the length of the mapping.
616 @discussion This method returns the length of the mapping.
617 @result A byte count. */
618
619 virtual IOByteCount getLength() = 0;
620
621 /*! @function getAddressTask
622 @abstract Accessor to the task of the mapping.
623 @discussion This method returns the mach task the mapping exists in.
624 @result A mach task_t. */
625
626 virtual task_t getAddressTask() = 0;
627
628 /*! @function getMemoryDescriptor
629 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
630 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
631 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
632
633 virtual IOMemoryDescriptor * getMemoryDescriptor() = 0;
634
635 /*! @function getMapOptions
636 @abstract Accessor to the options the mapping was created with.
637 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
638 @result Options for the mapping, including cache settings. */
639
640 virtual IOOptionBits getMapOptions() = 0;
641
642 /*! @function unmap
643 @abstract Force the IOMemoryMap to unmap, without destroying the object.
644 @discussion IOMemoryMap instances will unmap themselves upon free, ie. when the last client with a reference calls release. This method forces the IOMemoryMap to destroy the mapping it represents, regardless of the number of clients. It is not generally used.
645 @result An IOReturn code. */
646
647 virtual IOReturn unmap() = 0;
648
649 virtual void taskDied() = 0;
650
651 /*! @function redirect
652 @abstract Replace the memory mapped in a process with new backing memory.
653 @discussion An IOMemoryMap created with the kIOMapUnique option to IOMemoryDescriptor::map() can remapped to a new IOMemoryDescriptor backing object. If the new IOMemoryDescriptor is specified as NULL, client access to the memory map is blocked until a new backing object has been set. By blocking access and copying data, the caller can create atomic copies of the memory while the client is potentially reading or writing the memory.
654 @param newBackingMemory The IOMemoryDescriptor that represents the physical memory that is to be now mapped in the virtual range the IOMemoryMap represents. If newBackingMemory is NULL, any access to the mapping will hang (in vm_fault()) until access has been restored by a new call to redirect() with non-NULL newBackingMemory argument.
655 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
656 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
657 @result An IOReturn code. */
658
659 virtual IOReturn redirect(IOMemoryDescriptor * newBackingMemory,
660 IOOptionBits options,
661 IOByteCount offset = 0) = 0;
662 };
663
664 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
665
666 // The following classes are private implementation of IOMemoryDescriptor - they
667 // should not be referenced directly, just through the public API's in the
668 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
669 // might be created by IOMemoryDescriptor::withAddress(), but there should be
670 // no need to reference as anything but a generic IOMemoryDescriptor *.
671
672 // Also these flags should not overlap with the options to
673 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
674
675 enum {
676 kIOMemoryPreparedReadOnly = 0x00008000,
677 };
678
679 class IOGeneralMemoryDescriptor : public IOMemoryDescriptor
680 {
681 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor);
682
683 public:
684 union Ranges {
685 IOVirtualRange * v;
686 IOPhysicalRange * p;
687 void *uio;
688 };
689 protected:
690 Ranges _ranges;
691 unsigned _rangesCount; /* number of address ranges in list */
692 bool _rangesIsAllocated; /* is list allocated by us? */
693
694 task_t _task; /* task where all ranges are mapped to */
695
696 union {
697 IOVirtualRange v;
698 IOPhysicalRange p;
699 } _singleRange; /* storage space for a single range */
700
701 unsigned _wireCount; /* number of outstanding wires */
702
703 /* DEPRECATED */ vm_address_t _cachedVirtualAddress; /* a cached virtual-to-physical */
704
705 /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress;
706
707 bool _initialized; /* has superclass been initialized? */
708
709 virtual void free();
710
711
712 private:
713 // Internal APIs may be made virtual at some time in the future.
714 IOReturn wireVirtual(IODirection forDirection);
715 void *createNamedEntry();
716
717
718 /* DEPRECATED */ IOByteCount _position; /* absolute position over all ranges */
719 /* DEPRECATED */ virtual void setPosition(IOByteCount position);
720
721 /*
722 * DEPRECATED IOByteCount _positionAtIndex; // relative position within range #n
723 *
724 * Re-use the _positionAtIndex as a count of the number of pages in
725 * this memory descriptor. Convieniently vm_address_t is an unsigned integer
726 * type so I can get away without having to change the type.
727 */
728 unsigned int _pages;
729
730 /* DEPRECATED */ unsigned _positionAtOffset; //range #n in which position is now
731
732 OSData *_memoryEntries;
733
734 /* DEPRECATED */ vm_offset_t _kernPtrAligned;
735 /* DEPRECATED */ unsigned _kernPtrAtIndex;
736 /* DEPRECATED */ IOByteCount _kernSize;
737
738 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex);
739 /* DEPRECATED */ virtual void unmapFromKernel();
740
741 public:
742 /*
743 * IOMemoryDescriptor required methods
744 */
745
746 // Master initaliser
747 virtual bool initWithOptions(void * buffers,
748 UInt32 count,
749 UInt32 offset,
750 task_t task,
751 IOOptionBits options,
752 IOMapper * mapper = 0);
753
754 // Secondary initialisers
755 virtual bool initWithAddress(void * address,
756 IOByteCount withLength,
757 IODirection withDirection);
758
759 virtual bool initWithAddress(vm_address_t address,
760 IOByteCount withLength,
761 IODirection withDirection,
762 task_t withTask);
763
764 virtual bool initWithPhysicalAddress(
765 IOPhysicalAddress address,
766 IOByteCount withLength,
767 IODirection withDirection );
768
769 virtual bool initWithRanges( IOVirtualRange * ranges,
770 UInt32 withCount,
771 IODirection withDirection,
772 task_t withTask,
773 bool asReference = false);
774
775 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
776 UInt32 withCount,
777 IODirection withDirection,
778 bool asReference = false);
779
780 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
781 IOByteCount * length);
782
783 virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
784 IOByteCount * length);
785
786 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
787 /* DEPRECATED */ IOByteCount * length);
788
789 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone);
790
791 virtual IOReturn complete(IODirection forDirection = kIODirectionNone);
792
793 virtual IOReturn doMap(
794 vm_map_t addressMap,
795 IOVirtualAddress * atAddress,
796 IOOptionBits options,
797 IOByteCount sourceOffset = 0,
798 IOByteCount length = 0 );
799
800 virtual IOReturn doUnmap(
801 vm_map_t addressMap,
802 IOVirtualAddress logical,
803 IOByteCount length );
804 virtual bool serialize(OSSerialize *s) const;
805
806 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
807 static IOMemoryDescriptor *
808 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor *originalMD);
809 };
810
811 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
812
813 class IOSubMemoryDescriptor : public IOMemoryDescriptor
814 {
815 friend class IOMemoryDescriptor;
816
817 OSDeclareDefaultStructors(IOSubMemoryDescriptor);
818
819 protected:
820 IOMemoryDescriptor * _parent;
821 IOByteCount _start;
822
823 virtual void free();
824
825 virtual bool initWithAddress(void * address,
826 IOByteCount withLength,
827 IODirection withDirection);
828
829 virtual bool initWithAddress(vm_address_t address,
830 IOByteCount withLength,
831 IODirection withDirection,
832 task_t withTask);
833
834 virtual bool initWithPhysicalAddress(
835 IOPhysicalAddress address,
836 IOByteCount withLength,
837 IODirection withDirection );
838
839 virtual bool initWithRanges( IOVirtualRange * ranges,
840 UInt32 withCount,
841 IODirection withDirection,
842 task_t withTask,
843 bool asReference = false);
844
845 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
846 UInt32 withCount,
847 IODirection withDirection,
848 bool asReference = false);
849
850 IOMemoryDescriptor::withAddress;
851 IOMemoryDescriptor::withPhysicalAddress;
852 IOMemoryDescriptor::withPhysicalRanges;
853 IOMemoryDescriptor::withRanges;
854 IOMemoryDescriptor::withSubRange;
855
856 public:
857 /*
858 * Initialize or reinitialize an IOSubMemoryDescriptor to describe
859 * a subrange of an existing descriptor.
860 *
861 * An IOSubMemoryDescriptor can be re-used by calling initSubRange
862 * again on an existing instance -- note that this behavior is not
863 * commonly supported in other IOKit classes, although it is here.
864 */
865 virtual bool initSubRange( IOMemoryDescriptor * parent,
866 IOByteCount offset, IOByteCount length,
867 IODirection withDirection );
868
869 /*
870 * IOMemoryDescriptor required methods
871 */
872
873 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
874 IOByteCount * length);
875
876 virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
877 IOByteCount * length);
878
879 virtual IOByteCount readBytes(IOByteCount offset,
880 void * bytes, IOByteCount withLength);
881
882 virtual IOByteCount writeBytes(IOByteCount offset,
883 const void * bytes, IOByteCount withLength);
884
885 virtual void * getVirtualSegment(IOByteCount offset,
886 IOByteCount * length);
887
888 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone);
889
890 virtual IOReturn complete(IODirection forDirection = kIODirectionNone);
891
892 // make virtual
893 IOReturn redirect( task_t safeTask, bool redirect );
894
895 virtual bool serialize(OSSerialize *s) const;
896
897 virtual IOReturn setPurgeable( IOOptionBits newState,
898 IOOptionBits * oldState );
899 virtual IOReturn performOperation( IOOptionBits options,
900 IOByteCount offset, IOByteCount length );
901
902 protected:
903 virtual IOMemoryMap * makeMapping(
904 IOMemoryDescriptor * owner,
905 task_t intoTask,
906 IOVirtualAddress atAddress,
907 IOOptionBits options,
908 IOByteCount offset,
909 IOByteCount length );
910
911 virtual IOReturn doMap(
912 vm_map_t addressMap,
913 IOVirtualAddress * atAddress,
914 IOOptionBits options,
915 IOByteCount sourceOffset = 0,
916 IOByteCount length = 0 );
917 };
918
919 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
920
921 #endif /* !_IOMEMORYDESCRIPTOR_H */