2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 #ifndef _IOMEMORYDESCRIPTOR_H
31 #define _IOMEMORYDESCRIPTOR_H
33 #include <sys/cdefs.h>
35 #include <IOKit/IOTypes.h>
36 #include <libkern/c++/OSContainers.h>
39 #include <mach/memory_object_types.h>
42 struct IOPhysicalRange
44 IOPhysicalAddress address
;
52 * Direction of transfer, with respect to the described memory.
56 kIODirectionNone
= 0x0, // same as VM_PROT_NONE
57 kIODirectionIn
= 0x1, // User land 'read', same as VM_PROT_READ
58 kIODirectionOut
= 0x2, // User land 'write', same as VM_PROT_WRITE
59 kIODirectionOutIn
= kIODirectionOut
| kIODirectionIn
,
60 kIODirectionInOut
= kIODirectionIn
| kIODirectionOut
64 * IOOptionBits used in the withOptions variant
67 kIOMemoryDirectionMask
= 0x00000007,
68 kIOMemoryAutoPrepare
= 0x00000008, // Shared with Buffer MD
70 kIOMemoryTypeVirtual
= 0x00000010,
71 kIOMemoryTypePhysical
= 0x00000020,
72 kIOMemoryTypeUPL
= 0x00000030,
73 kIOMemoryTypePersistentMD
= 0x00000040, // Persistent Memory Descriptor
74 kIOMemoryTypeUIO
= 0x00000050,
75 kIOMemoryTypeVirtual64
= 0x00000060,
76 kIOMemoryTypePhysical64
= 0x00000070,
77 kIOMemoryTypeMask
= 0x000000f0,
79 kIOMemoryAsReference
= 0x00000100,
80 kIOMemoryBufferPageable
= 0x00000400,
81 kIOMemoryDontMap
= 0x00000800,
82 kIOMemoryPersistent
= 0x00010000
85 #define kIOMapperNone ((IOMapper *) -1)
86 #define kIOMapperSystem ((IOMapper *) 0)
90 kIOMemoryPurgeableKeepCurrent
= 1,
91 kIOMemoryPurgeableNonVolatile
= 2,
92 kIOMemoryPurgeableVolatile
= 3,
93 kIOMemoryPurgeableEmpty
= 4
97 kIOMemoryIncoherentIOFlush
= 1,
98 kIOMemoryIncoherentIOStore
= 2,
101 #define IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND 1
104 /*! @class IOMemoryDescriptor : public OSObject
105 @abstract An abstract base class defining common methods for describing physical or virtual memory.
106 @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. */
108 class IOMemoryDescriptor
: public OSObject
110 friend class _IOMemoryMap
;
111 friend class IOSubMemoryDescriptor
;
113 OSDeclareDefaultStructors(IOMemoryDescriptor
);
116 /*! @struct ExpansionData
117 @discussion This structure will be used to expand the capablilties of this class in the future.
119 struct ExpansionData
{
121 unsigned int pagerContig
:1;
122 unsigned int unused
:31;
123 IOMemoryDescriptor
* memory
;
127 Reserved for future use. (Internal use only) */
128 ExpansionData
* reserved
;
135 IODirection _direction
; /* DEPRECATED: use _flags instead. direction of transfer */
136 IOByteCount _length
; /* length of all ranges */
140 typedef IOOptionBits DMACommandOps
;
141 virtual IOPhysicalAddress
getSourceSegment( IOByteCount offset
,
142 IOByteCount
* length
);
143 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 0);
145 /*! @function initWithOptions
146 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
147 @discussion Note this function can be used to re-init a previously created memory descriptor.
148 @result true on success, false on failure. */
149 virtual bool initWithOptions(void * buffers
,
153 IOOptionBits options
,
154 IOMapper
* mapper
= kIOMapperSystem
);
155 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 1);
157 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
158 IOByteCount
* length
);
159 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 2);
162 /*! @function setPurgeable
163 @abstract Control the purgeable status of a memory descriptors memory.
164 @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.
165 @param newState - the desired new purgeable state of the memory:<br>
166 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
167 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
168 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
169 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
170 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
171 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
172 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
173 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
174 @result An IOReturn code. */
176 virtual IOReturn
setPurgeable( IOOptionBits newState
,
177 IOOptionBits
* oldState
);
178 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 3);
180 /*! @function performOperation
181 @abstract Perform an operation on the memory descriptor's memory.
182 @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.
183 @param options The operation to perform on the memory:<br>
184 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>
185 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.
186 @param offset A byte offset into the memory descriptor's memory.
187 @param length The length of the data range.
188 @result An IOReturn code. */
190 virtual IOReturn
performOperation( IOOptionBits options
,
191 IOByteCount offset
, IOByteCount length
);
192 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 4);
194 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
195 // Used for dedicated communications for IODMACommand
196 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
197 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 5);
201 #if (defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
202 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 5);
204 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 6);
205 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 7);
206 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 8);
207 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 9);
208 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 10);
209 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 11);
210 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 12);
211 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 13);
212 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 14);
213 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 15);
218 static void initialize( void );
221 /*! @function withAddress
222 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
223 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map.
224 @param address The virtual address of the first byte in the memory.
225 @param withLength The length of memory.
226 @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.
227 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
229 static IOMemoryDescriptor
* withAddress(void * address
,
230 IOByteCount withLength
,
231 IODirection withDirection
);
233 /*! @function withAddress
234 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
235 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
236 @param address The virtual 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 @param withTask The task the virtual ranges are mapped into.
240 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
242 static IOMemoryDescriptor
* withAddress(vm_address_t address
,
243 IOByteCount withLength
,
244 IODirection withDirection
,
247 /*! @function withPhysicalAddress
248 @abstract Create an IOMemoryDescriptor to describe one physical range.
249 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
250 @param address The physical address of the first byte in the memory.
251 @param withLength The length of memory.
252 @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.
253 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
255 static IOMemoryDescriptor
* withPhysicalAddress(
256 IOPhysicalAddress address
,
257 IOByteCount withLength
,
258 IODirection withDirection
);
260 /*! @function withRanges
261 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
262 @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.
263 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
264 @param withCount The member count of the ranges array.
265 @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.
266 @param withTask The task each of the virtual ranges are mapped into.
267 @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.
268 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
270 static IOMemoryDescriptor
* withRanges(IOVirtualRange
* ranges
,
272 IODirection withDirection
,
274 bool asReference
= false);
276 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
277 /*! @function withAddressRange
278 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
279 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
280 @param address The virtual address of the first byte in the memory.
281 @param withLength The length of memory.
283 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.
284 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
285 @param task The task the virtual ranges are mapped into.
286 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
288 static IOMemoryDescriptor
* withAddressRange(
289 mach_vm_address_t address
,
290 mach_vm_size_t length
,
291 IOOptionBits options
,
294 /*! @function withAddressRanges
295 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
296 @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.
297 @param ranges An array of IOAddressRange structures which specify the virtual ranges in the specified map which make up the memory to be described. IOAddressRange is the 64bit version of IOVirtualRange.
298 @param rangeCount The member count of the ranges array.
300 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.
301 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.
302 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
303 @param task The task each of the virtual ranges are mapped into.
304 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
306 static IOMemoryDescriptor
* withAddressRanges(
307 IOAddressRange
* ranges
,
309 IOOptionBits options
,
313 /*! @function withOptions
314 @abstract Master initialiser for all variants of memory descriptors.
315 @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.
318 @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.
320 @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.
322 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
324 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
327 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.
328 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.
329 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.
330 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.
331 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
333 @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.
335 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
337 static IOMemoryDescriptor
*withOptions(void * buffers
,
341 IOOptionBits options
,
342 IOMapper
* mapper
= kIOMapperSystem
);
344 /*! @function withPhysicalRanges
345 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
346 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
347 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
348 @param withCount The member count of the ranges array.
349 @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.
350 @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.
351 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
353 static IOMemoryDescriptor
* withPhysicalRanges(
354 IOPhysicalRange
* ranges
,
356 IODirection withDirection
,
357 bool asReference
= false);
359 /*! @function withSubRange
360 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
361 @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.
362 @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.
363 @param offset A byte offset into the parent memory descriptor's memory.
364 @param length The length of the subrange.
365 @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.
366 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
368 static IOMemoryDescriptor
* withSubRange(IOMemoryDescriptor
*of
,
371 IODirection withDirection
);
373 /*! @function withPersistentMemoryDescriptor
374 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
375 @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.
376 @param originalMD The memory descriptor to be duplicated.
377 @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. */
378 static IOMemoryDescriptor
*
379 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
);
381 /*! @function initWithAddress
382 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
383 @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.
384 @param address The virtual address of the first byte in the memory.
385 @param withLength The length of memory.
386 @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.
387 @result true on success, false on failure. */
389 virtual bool initWithAddress(void * address
,
390 IOByteCount withLength
,
391 IODirection withDirection
) = 0;
393 /*! @function initWithAddress
394 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
395 @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.
396 @param address The virtual address of the first byte in the memory.
397 @param withLength The length of memory.
398 @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.
399 @param withTask The task the virtual ranges are mapped into.
400 @result true on success, false on failure. */
402 virtual bool initWithAddress(vm_address_t address
,
403 IOByteCount withLength
,
404 IODirection withDirection
,
405 task_t withTask
) = 0;
407 /*! @function initWithPhysicalAddress
408 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
409 @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.
410 @param address The physical address of the first byte in the memory.
411 @param withLength The length of memory.
412 @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.
413 @result true on success, false on failure. */
415 virtual bool initWithPhysicalAddress(
416 IOPhysicalAddress address
,
417 IOByteCount withLength
,
418 IODirection withDirection
) = 0;
420 /*! @function initWithRanges
421 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
422 @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.
423 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
424 @param withCount The member count of the ranges array.
425 @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.
426 @param withTask The task each of the virtual ranges are mapped into.
427 @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.
428 @result true on success, false on failure. */
430 virtual bool initWithRanges(IOVirtualRange
* ranges
,
432 IODirection withDirection
,
434 bool asReference
= false) = 0;
436 /*! @function initWithPhysicalRanges
437 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
438 @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.
439 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
440 @param withCount The member count of the ranges array.
441 @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.
442 @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.
443 @result true on success, false on failure. */
445 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
447 IODirection withDirection
,
448 bool asReference
= false) = 0;
450 /*! @function getDirection
451 @abstract Accessor to get the direction the memory descriptor was created with.
452 @discussion This method returns the direction the memory descriptor was created with.
453 @result The direction. */
455 virtual IODirection
getDirection() const;
457 /*! @function getLength
458 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
459 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
460 @result The byte count. */
462 virtual IOByteCount
getLength() const;
465 @abstract Set the tag for the memory descriptor.
466 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
467 @param tag The tag. */
469 virtual void setTag( IOOptionBits tag
);
472 @abstract Accessor to the retrieve the tag for the memory descriptor.
473 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
476 virtual IOOptionBits
getTag( void );
478 /*! @function readBytes
479 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
480 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
481 @param offset A byte offset into the memory descriptor's memory.
482 @param bytes The caller supplied buffer to copy the data to.
483 @param withLength The length of the data to copy.
484 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
486 virtual IOByteCount
readBytes(IOByteCount offset
,
487 void * bytes
, IOByteCount withLength
);
489 /*! @function writeBytes
490 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
491 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
492 @param offset A byte offset into the memory descriptor's memory.
493 @param bytes The caller supplied buffer to copy the data from.
494 @param withLength The length of the data to copy.
495 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
497 virtual IOByteCount
writeBytes(IOByteCount offset
,
498 const void * bytes
, IOByteCount withLength
);
500 /*! @function getPhysicalSegment
501 @abstract Break a memory descriptor into its physically contiguous segments.
502 @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.
503 @param offset A byte offset into the memory whose physical address to return.
504 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
505 @result A physical address, or zero if the offset is beyond the length of the memory. */
507 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
508 IOByteCount
* length
) = 0;
510 /*! @function getPhysicalAddress
511 @abstract Return the physical address of the first byte in the memory.
512 @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.
513 @result A physical address. */
515 /* inline */ IOPhysicalAddress
getPhysicalAddress();
516 /* { return( getPhysicalSegment( 0, 0 )); } */
518 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
519 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
520 /* DEPRECATED */ IOByteCount
* length
) = 0;
521 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
523 /*! @function prepare
524 @abstract Prepare the memory for an I/O transfer.
525 @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.
526 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
527 @result An IOReturn code. */
529 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
) = 0;
531 /*! @function complete
532 @abstract Complete processing of the memory after an I/O transfer finishes.
533 @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.
534 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
535 @result An IOReturn code. */
537 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
) = 0;
544 @abstract Maps a IOMemoryDescriptor into a task.
545 @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.
546 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
547 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
548 @param options Mapping options are defined in IOTypes.h,<br>
549 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>
550 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
551 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
552 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
553 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>
554 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>
555 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
556 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
557 @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. */
559 virtual IOMemoryMap
* map(
561 IOVirtualAddress atAddress
,
562 IOOptionBits options
,
563 IOByteCount offset
= 0,
564 IOByteCount length
= 0 );
567 @abstract Maps a IOMemoryDescriptor into the kernel map.
568 @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.
569 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
570 @result See the full version of the map method. */
572 virtual IOMemoryMap
* map(
573 IOOptionBits options
= 0 );
575 /*! @function setMapping
576 @abstract Establishes an already existing mapping.
577 @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.
578 @param task Address space in which the mapping exists.
579 @param mapAddress Virtual address of the mapping.
580 @param options Caching and read-only attributes of the mapping.
581 @result A IOMemoryMap object created to represent the mapping. */
583 virtual IOMemoryMap
* setMapping(
585 IOVirtualAddress mapAddress
,
586 IOOptionBits options
= 0 );
588 // Following methods are private implementation
591 IOReturn
redirect( task_t safeTask
, bool redirect
);
593 IOReturn
handleFault(
596 IOVirtualAddress address
,
597 IOByteCount sourceOffset
,
599 IOOptionBits options
);
602 virtual IOMemoryMap
* makeMapping(
603 IOMemoryDescriptor
* owner
,
605 IOVirtualAddress atAddress
,
606 IOOptionBits options
,
608 IOByteCount length
);
610 virtual void addMapping(
611 IOMemoryMap
* mapping
);
613 virtual void removeMapping(
614 IOMemoryMap
* mapping
);
616 virtual IOReturn
doMap(
618 IOVirtualAddress
* atAddress
,
619 IOOptionBits options
,
620 IOByteCount sourceOffset
= 0,
621 IOByteCount length
= 0 );
623 virtual IOReturn
doUnmap(
625 IOVirtualAddress logical
,
626 IOByteCount length
);
629 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
631 /*! @class IOMemoryMap : public OSObject
632 @abstract An abstract base class defining common methods for describing a memory mapping.
633 @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. */
635 class IOMemoryMap
: public OSObject
637 OSDeclareAbstractStructors(IOMemoryMap
)
640 /*! @function getVirtualAddress
641 @abstract Accessor to the virtual address of the first byte in the mapping.
642 @discussion This method returns the virtual address of the first byte in the mapping.
643 @result A virtual address. */
645 virtual IOVirtualAddress
getVirtualAddress() = 0;
647 /*! @function getPhysicalSegment
648 @abstract Break a mapping into its physically contiguous segments.
649 @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.
650 @param offset A byte offset into the mapping whose physical address to return.
651 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
652 @result A physical address, or zero if the offset is beyond the length of the mapping. */
654 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
655 IOByteCount
* length
) = 0;
657 /*! @function getPhysicalAddress
658 @abstract Return the physical address of the first byte in the mapping.
659 @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.
660 @result A physical address. */
662 /* inline */ IOPhysicalAddress
getPhysicalAddress();
663 /* { return( getPhysicalSegment( 0, 0 )); } */
665 /*! @function getLength
666 @abstract Accessor to the length of the mapping.
667 @discussion This method returns the length of the mapping.
668 @result A byte count. */
670 virtual IOByteCount
getLength() = 0;
672 /*! @function getAddressTask
673 @abstract Accessor to the task of the mapping.
674 @discussion This method returns the mach task the mapping exists in.
675 @result A mach task_t. */
677 virtual task_t
getAddressTask() = 0;
679 /*! @function getMemoryDescriptor
680 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
681 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
682 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
684 virtual IOMemoryDescriptor
* getMemoryDescriptor() = 0;
686 /*! @function getMapOptions
687 @abstract Accessor to the options the mapping was created with.
688 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
689 @result Options for the mapping, including cache settings. */
691 virtual IOOptionBits
getMapOptions() = 0;
694 @abstract Force the IOMemoryMap to unmap, without destroying the object.
695 @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.
696 @result An IOReturn code. */
698 virtual IOReturn
unmap() = 0;
700 virtual void taskDied() = 0;
702 /*! @function redirect
703 @abstract Replace the memory mapped in a process with new backing memory.
704 @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.
705 @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.
706 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
707 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
708 @result An IOReturn code. */
710 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
711 IOOptionBits options
,
712 IOByteCount offset
= 0) = 0;
715 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
717 // The following classes are private implementation of IOMemoryDescriptor - they
718 // should not be referenced directly, just through the public API's in the
719 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
720 // might be created by IOMemoryDescriptor::withAddress(), but there should be
721 // no need to reference as anything but a generic IOMemoryDescriptor *.
723 // Also these flags should not overlap with the options to
724 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
727 kIOMemoryPreparedReadOnly
= 0x00008000,
730 class IOGeneralMemoryDescriptor
: public IOMemoryDescriptor
732 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor
);
743 unsigned _rangesCount
; /* number of address ranges in list */
744 bool _rangesIsAllocated
; /* is list allocated by us? */
746 task_t _task
; /* task where all ranges are mapped to */
751 } _singleRange
; /* storage space for a single range */
753 unsigned _wireCount
; /* number of outstanding wires */
755 /* DEPRECATED */ vm_address_t _cachedVirtualAddress
; /* a cached virtual-to-physical */
757 /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress
;
759 bool _initialized
; /* has superclass been initialized? */
763 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
764 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
769 /* DEPRECATED */ virtual void setPosition(IOByteCount position
);
770 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex
);
771 /* DEPRECATED */ virtual void unmapFromKernel();
773 // Internal APIs may be made virtual at some time in the future.
774 IOReturn
wireVirtual(IODirection forDirection
);
775 void *createNamedEntry();
778 OSData
* _memoryEntries
;
780 ppnum_t _highestPage
;
781 uint32_t __iomd_reservedA
;
782 uint32_t __iomd_reservedB
;
783 uint32_t __iomd_reservedC
;
787 * IOMemoryDescriptor required methods
791 virtual bool initWithOptions(void * buffers
,
795 IOOptionBits options
,
796 IOMapper
* mapper
= kIOMapperSystem
);
798 // Secondary initialisers
799 virtual bool initWithAddress(void * address
,
800 IOByteCount withLength
,
801 IODirection withDirection
);
803 virtual bool initWithAddress(vm_address_t address
,
804 IOByteCount withLength
,
805 IODirection withDirection
,
808 virtual bool initWithPhysicalAddress(
809 IOPhysicalAddress address
,
810 IOByteCount withLength
,
811 IODirection withDirection
);
813 virtual bool initWithRanges( IOVirtualRange
* ranges
,
815 IODirection withDirection
,
817 bool asReference
= false);
819 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
821 IODirection withDirection
,
822 bool asReference
= false);
824 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
825 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
826 IOByteCount
* length
);
829 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
830 IOByteCount
* length
);
832 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
833 IOByteCount
* length
);
835 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
836 /* DEPRECATED */ IOByteCount
* length
);
838 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
840 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
842 virtual IOReturn
doMap(
844 IOVirtualAddress
* atAddress
,
845 IOOptionBits options
,
846 IOByteCount sourceOffset
= 0,
847 IOByteCount length
= 0 );
849 virtual IOReturn
doUnmap(
851 IOVirtualAddress logical
,
852 IOByteCount length
);
853 virtual bool serialize(OSSerialize
*s
) const;
855 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
856 static IOMemoryDescriptor
*
857 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
);
860 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
862 class IOSubMemoryDescriptor
: public IOMemoryDescriptor
864 friend class IOMemoryDescriptor
;
866 OSDeclareDefaultStructors(IOSubMemoryDescriptor
);
869 IOMemoryDescriptor
* _parent
;
874 virtual bool initWithAddress(void * address
,
875 IOByteCount withLength
,
876 IODirection withDirection
);
878 virtual bool initWithAddress(vm_address_t address
,
879 IOByteCount withLength
,
880 IODirection withDirection
,
883 virtual bool initWithPhysicalAddress(
884 IOPhysicalAddress address
,
885 IOByteCount withLength
,
886 IODirection withDirection
);
888 virtual bool initWithRanges( IOVirtualRange
* ranges
,
890 IODirection withDirection
,
892 bool asReference
= false);
894 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
896 IODirection withDirection
,
897 bool asReference
= false);
899 IOMemoryDescriptor::withAddress
;
900 IOMemoryDescriptor::withPhysicalAddress
;
901 IOMemoryDescriptor::withPhysicalRanges
;
902 IOMemoryDescriptor::withRanges
;
903 IOMemoryDescriptor::withSubRange
;
905 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
906 // used by IODMACommand
907 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
912 * Initialize or reinitialize an IOSubMemoryDescriptor to describe
913 * a subrange of an existing descriptor.
915 * An IOSubMemoryDescriptor can be re-used by calling initSubRange
916 * again on an existing instance -- note that this behavior is not
917 * commonly supported in other IOKit classes, although it is here.
919 virtual bool initSubRange( IOMemoryDescriptor
* parent
,
920 IOByteCount offset
, IOByteCount length
,
921 IODirection withDirection
);
924 * IOMemoryDescriptor required methods
927 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
928 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
929 IOByteCount
* length
);
932 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
933 IOByteCount
* length
);
935 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
936 IOByteCount
* length
);
938 virtual IOByteCount
readBytes(IOByteCount offset
,
939 void * bytes
, IOByteCount withLength
);
941 virtual IOByteCount
writeBytes(IOByteCount offset
,
942 const void * bytes
, IOByteCount withLength
);
944 virtual void * getVirtualSegment(IOByteCount offset
,
945 IOByteCount
* length
);
947 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
949 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
952 IOReturn
redirect( task_t safeTask
, bool redirect
);
954 virtual bool serialize(OSSerialize
*s
) const;
956 virtual IOReturn
setPurgeable( IOOptionBits newState
,
957 IOOptionBits
* oldState
);
958 virtual IOReturn
performOperation( IOOptionBits options
,
959 IOByteCount offset
, IOByteCount length
);
962 virtual IOMemoryMap
* makeMapping(
963 IOMemoryDescriptor
* owner
,
965 IOVirtualAddress atAddress
,
966 IOOptionBits options
,
968 IOByteCount length
);
970 virtual IOReturn
doMap(
972 IOVirtualAddress
* atAddress
,
973 IOOptionBits options
,
974 IOByteCount sourceOffset
= 0,
975 IOByteCount length
= 0 );
978 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
980 #endif /* !_IOMEMORYDESCRIPTOR_H */