IOMemoryDescriptor

Inherits from
OSObject
Availability
Available in OS X v10.0 and later.
Declared in
IOMemoryDescriptor.h

Overview

An abstract base class defining common methods for describing physical or virtual memory.

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.

Tasks

Miscellaneous

Instance Methods

complete

Complete processing of the memory after an I/O transfer finishes.

virtual IOReturn complete( IODirection forDirection = kIODirectionNone) = 0;
Parameters
forDirection

DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.

Return Value

An IOReturn code.

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.

createMappingInTask

Maps a IOMemoryDescriptor into a task.

IOMemoryMap * createMappingInTask( task_t intoTask, mach_vm_address_t atAddress, IOOptionBits options, mach_vm_size_t offset = 0, mach_vm_size_t length = 0 );
Parameters
intoTask

Sets the target task for the mapping. Pass kernel_task for the kernel address space.

atAddress

If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.

options

Mapping options are defined in IOTypes.h,

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.

kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.

kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.

kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.

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.

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.

offset

Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.

length

Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.

Return Value

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.

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.

getDirection

Accessor to get the direction the memory descriptor was created with.

virtual IODirection getDirection() const;
Return Value

The direction.

Discussion

This method returns the direction the memory descriptor was created with.

getLength

Accessor to get the length of the memory descriptor (over all its ranges).

virtual IOByteCount getLength() const;
Return Value

The byte count.

Discussion

This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.

getPageCounts

Retrieve the number of resident and/or dirty pages encompassed by an IOMemoryDescriptor.

IOReturn getPageCounts( IOByteCount *residentPageCount, IOByteCount *dirtyPageCount);
Parameters
residentPageCount

- If non-null, a pointer to a byte count that will return the number of resident pages encompassed by this IOMemoryDescriptor.

dirtyPageCount

- If non-null, a pointer to a byte count that will return the number of dirty pages encompassed by this IOMemoryDescriptor.

Return Value

An IOReturn code.

Discussion

This method returns the number of resident and/or dirty pages encompassed by an IOMemoryDescriptor.

getPhysicalAddress

Return the physical address of the first byte in the memory.

IOPhysicalAddress getPhysicalAddress();
Return Value

A physical address.

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.

getPhysicalSegment

Break a memory descriptor into its physically contiguous segments.

#ifdef __LP64__ virtual addr64_t getPhysicalSegment( IOByteCount offset, IOByteCount *length, IOOptionBits options = 0 ) = 0; #else /* !__LP64__ */ virtual addr64_t getPhysicalSegment( IOByteCount offset, IOByteCount *length, IOOptionBits options ); #endif /* !__LP64__ */
Parameters
offset

A byte offset into the memory whose physical address to return.

length

If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.

Return Value

A physical address, or zero if the offset is beyond the length of the memory.

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.

getTag

Accessor to the retrieve the tag for the memory descriptor.

virtual IOOptionBits getTag( void );
Return Value

The tag.

Discussion

This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.

initWithOptions

Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.

virtual bool initWithOptions( void *buffers, UInt32 count, UInt32 offset, task_t task, IOOptionBits options, IOMapper *mapper = kIOMapperSystem);
Return Value

true on success, false on failure.

Discussion

Note this function can be used to re-init a previously created memory descriptor.

map

Maps a IOMemoryDescriptor into the kernel map.

virtual IOMemoryMap * map( IOOptionBits options = 0 );
Parameters
options

Mapping options as in the full version of the createMappingInTask method, with kIOMapAnywhere assumed.

Return Value

See the full version of the createMappingInTask method.

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 createMappingInTask method for further details.

performOperation

Perform an operation on the memory descriptor's memory.

virtual IOReturn performOperation( IOOptionBits options, IOByteCount offset, IOByteCount length );
Parameters
options

The operation to perform on the memory:

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.

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.

offset

A byte offset into the memory descriptor's memory.

length

The length of the data range.

Return Value

An IOReturn code.

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.

prepare

Prepare the memory for an I/O transfer.

virtual IOReturn prepare( IODirection forDirection = kIODirectionNone) = 0;
Parameters
forDirection

The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.

Return Value

An IOReturn code.

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.

readBytes

Copy data from the memory descriptor's buffer to the specified buffer.

virtual IOByteCount readBytes( IOByteCount offset, void *bytes, IOByteCount withLength);
Parameters
offset

A byte offset into the memory descriptor's memory.

bytes

The caller supplied buffer to copy the data to.

withLength

The length of the data to copy.

Return Value

The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. Development/debug kernel builds will assert if the offset is beyond the length of the descriptor.

Discussion

This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer. The memory descriptor MUST have the kIODirectionOut direcction bit set and be prepared. kIODirectionOut means that this memory descriptor will be output to an external device, so readBytes is used to get memory into a local buffer for a PIO transfer to the device.

setMapping

Establishes an already existing mapping.

virtual IOMemoryMap * setMapping( task_t task, IOVirtualAddress mapAddress, IOOptionBits options = 0 );
Parameters
task

Address space in which the mapping exists.

mapAddress

Virtual address of the mapping.

options

Caching and read-only attributes of the mapping.

Return Value

A IOMemoryMap object created to represent the mapping.

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.

setPurgeable

Control the purgeable status of a memory descriptors memory.

virtual IOReturn setPurgeable( IOOptionBits newState, IOOptionBits *oldState );
Parameters
newState

- the desired new purgeable state of the memory:

kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.

kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.

kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.

kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.

oldState

- if non-NULL, the previous purgeable state of the memory is returned here:

kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.

kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.

kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.

Return Value

An IOReturn code.

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.

setTag

Set the tag for the memory descriptor.

virtual void setTag( IOOptionBits tag );
Parameters
tag

The tag.

Discussion

This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.

withAddress

Create an IOMemoryDescriptor to describe one virtual range of the kernel task.

static IOMemoryDescriptor * withAddress( void *address, IOByteCount withLength, IODirection withDirection);
Parameters
address

The virtual address of the first byte in the memory.

withLength

The length of memory.

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.

Return Value

The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure.

Discussion

This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map. This memory descriptor needs to be prepared before it can be used to extract data from the memory described.

withAddressRange

Create an IOMemoryDescriptor to describe one virtual range of the specified map.

static IOMemoryDescriptor * withAddressRange( mach_vm_address_t address, mach_vm_size_t length, IOOptionBits options, task_t task);
Parameters
address

The virtual address of the first byte in the memory.

withLength

The length of memory.

options

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.

task

The task the virtual ranges are mapped into. Note that unlike IOMemoryDescriptor::withAddress(), kernel_task memory must be explicitly prepared when passed to this api. The task argument may be NULL to specify memory by physical address.

Return Value

The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure.

Discussion

This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map. This memory descriptor needs to be prepared before it can be used to extract data from the memory described.

withAddressRanges

Create an IOMemoryDescriptor to describe one or more virtual ranges.

static IOMemoryDescriptor * withAddressRanges( IOAddressRange *ranges, UInt32 rangeCount, IOOptionBits options, task_t task);
Parameters
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.

rangeCount

The member count of the ranges array.

options

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. 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.

task

The task each of the virtual ranges are mapped into. Note that unlike IOMemoryDescriptor::withAddress(), kernel_task memory must be explicitly prepared when passed to this api. The task argument may be NULL to specify memory by physical address.

Return Value

The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure.

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. This memory descriptor needs to be prepared before it can be used to extract data from the memory described.

withOptions

Master initialiser for all variants of memory descriptors.

static IOMemoryDescriptor *withOptions( void *buffers, UInt32 count, UInt32 offset, task_t task, IOOptionBits options, IOMapper *mapper = kIOMapperSystem);
Parameters
buffers

A pointer to an array of IOAddressRange when options:type is kIOMemoryTypeVirtual64 or kIOMemoryTypePhysical64 or a 64bit kernel. For type UPL it is a upl_t returned by the mach/memory_object_types.h apis, primarily used internally by the UBC. IOVirtualRanges or IOPhysicalRanges are 32 bit only types for use when options:type is kIOMemoryTypeVirtual or kIOMemoryTypePhysical on 32bit kernels.

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.

offset

Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.

task

Only used options:type = Virtual, The task each of the virtual ranges are mapped into.

options

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. kIOMemoryTypeMask (options:type) kIOMemoryTypeVirtual64, kIOMemoryTypeVirtual, kIOMemoryTypePhysical64, 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. 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. 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.

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.

Return Value

The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure.

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.

withPersistentMemoryDescriptor

Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.

static IOMemoryDescriptor * withPersistentMemoryDescriptor( IOMemoryDescriptor *originalMD);
Parameters
originalMD

The memory descriptor to be duplicated.

Return Value

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.

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.

withPhysicalAddress

Create an IOMemoryDescriptor to describe one physical range.

static IOMemoryDescriptor * withPhysicalAddress( IOPhysicalAddress address, IOByteCount withLength, IODirection withDirection );
Parameters
address

The physical address of the first byte in the memory.

withLength

The length of memory.

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.

Return Value

The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure.

Discussion

This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.

writeBytes

Copy data to the memory descriptor's buffer from the specified buffer.

virtual IOByteCount writeBytes( IOByteCount offset, const void *bytes, IOByteCount withLength);
Parameters
offset

A byte offset into the memory descriptor's memory.

bytes

The caller supplied buffer to copy the data from.

withLength

The length of the data to copy.

Return Value

The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. Development/debug kernel builds will assert if the offset is beyond the length of the descriptor.

Discussion

This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer. The memory descriptor MUST have the kIODirectionIn direcction bit set and be prepared. kIODirectionIn means that this memory descriptor will be input from an external device, so writeBytes is used to write memory into the descriptor for PIO drivers.

Instance Variables

reserved

struct IOMemoryDescriptorReserved * reserved;

Reserved for future use. (Internal use only)