*
* @APPLE_LICENSE_HEADER_START@
*
- * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
+ * The contents of this file constitute Original Code as defined in and
+ * are subject to the Apple Public Source License Version 1.1 (the
+ * "License"). You may not use this file except in compliance with the
+ * License. Please obtain a copy of the License at
+ * http://www.apple.com/publicsource and read it before using this file.
*
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This Original Code and all software distributed under the License are
+ * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
+ * License for the specific language governing rights and limitations
+ * under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#ifndef _IOMEMORYDESCRIPTOR_H
#define _IOMEMORYDESCRIPTOR_H
+#include <sys/cdefs.h>
+
#include <IOKit/IOTypes.h>
#include <libkern/c++/OSContainers.h>
+__BEGIN_DECLS
+#include <mach/memory_object_types.h>
+__END_DECLS
+
struct IOPhysicalRange
{
IOPhysicalAddress address;
};
class IOMemoryMap;
+class IOMapper;
/*
* Direction of transfer, with respect to the described memory.
kIODirectionNone = 0x0, // same as VM_PROT_NONE
kIODirectionIn = 0x1, // User land 'read', same as VM_PROT_READ
kIODirectionOut = 0x2, // User land 'write', same as VM_PROT_WRITE
- kIODirectionOutIn = kIODirectionIn | kIODirectionOut,
+ kIODirectionOutIn = kIODirectionOut | kIODirectionIn,
+ kIODirectionInOut = kIODirectionIn | kIODirectionOut
+};
+
+/*
+ * IOOptionBits used in the withOptions variant
+ */
+enum {
+ kIOMemoryDirectionMask = 0x00000007,
+ kIOMemoryAutoPrepare = 0x00000008, // Shared with Buffer MD
+
+ kIOMemoryTypeVirtual = 0x00000010,
+ kIOMemoryTypePhysical = 0x00000020,
+ kIOMemoryTypeUPL = 0x00000030,
+ kIOMemoryTypePersistentMD = 0x00000040, // Persistent Memory Descriptor
+ kIOMemoryTypeUIO = 0x00000050,
+ kIOMemoryTypeMask = 0x000000f0,
+
+ kIOMemoryAsReference = 0x00000100,
+ kIOMemoryBufferPageable = 0x00000400,
+ kIOMemoryDontMap = 0x00000800,
+ kIOMemoryPersistent = 0x00010000
+};
+
+#define kIOMapperNone ((IOMapper *) -1)
+#define kIOMapperSystem ((IOMapper *) 0)
+
+enum
+{
+ kIOMemoryPurgeableKeepCurrent = 1,
+ kIOMemoryPurgeableNonVolatile = 2,
+ kIOMemoryPurgeableVolatile = 3,
+ kIOMemoryPurgeableEmpty = 4
+};
+enum
+{
+ kIOMemoryIncoherentIOFlush = 1,
+ kIOMemoryIncoherentIOStore = 2,
};
/*! @class IOMemoryDescriptor : public OSObject
IOOptionBits _flags;
void * _memEntry;
- IODirection _direction; /* direction of transfer */
+ IODirection _direction; /* DEPRECATED: use _flags instead. direction of transfer */
IOByteCount _length; /* length of all ranges */
IOOptionBits _tag;
virtual IOPhysicalAddress getSourceSegment( IOByteCount offset,
IOByteCount * length );
+ OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 0);
+
+/*! @function initWithOptions
+ @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
+ @discussion Note this function can be used to re-init a previously created memory descriptor.
+ @result true on success, false on failure. */
+ virtual bool initWithOptions(void * buffers,
+ UInt32 count,
+ UInt32 offset,
+ task_t task,
+ IOOptionBits options,
+ IOMapper * mapper = 0);
+ OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 1);
+
+ virtual addr64_t getPhysicalSegment64( IOByteCount offset,
+ IOByteCount * length );
+ OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 2);
+
+
+/*! @function setPurgeable
+ @abstract Control the purgeable status of a memory descriptors memory.
+ @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.
+ @param newState - the desired new purgeable state of the memory:<br>
+ kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
+ kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
+ kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
+ kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
+ @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
+ kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
+ kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
+ kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
+ @result An IOReturn code. */
+
+ virtual IOReturn setPurgeable( IOOptionBits newState,
+ IOOptionBits * oldState );
+ OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 3);
+
+/*! @function performOperation
+ @abstract Perform an operation on the memory descriptor's memory.
+ @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.
+ @param options The operation to perform on the memory:<br>
+ 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>
+ 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.
+ @param offset A byte offset into the memory descriptor's memory.
+ @param length The length of the data range.
+ @result An IOReturn code. */
+
+ virtual IOReturn performOperation( IOOptionBits options,
+ IOByteCount offset, IOByteCount length );
+ OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 4);
private:
- OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 0);
- OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 1);
- OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 2);
- OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 3);
- OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 4);
+
OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 5);
OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 6);
OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 7);
@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.
@result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
- static IOMemoryDescriptor * withRanges(IOVirtualRange * ranges,
- UInt32 withCount,
- IODirection withDirection,
- task_t withTask,
- bool asReference = false);
+ static IOMemoryDescriptor * withRanges(IOVirtualRange * ranges,
+ UInt32 withCount,
+ IODirection withDirection,
+ task_t withTask,
+ bool asReference = false);
+
+/*! @function withOptions
+ @abstract Master initialiser for all variants of memory descriptors.
+ @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.
+
+
+ @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.
+
+ @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.
+
+ @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
+
+ @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
+
+ @param 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) 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.
+ 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.
+ kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
+
+ @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.
+
+ @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
+
+ static IOMemoryDescriptor *withOptions(void * buffers,
+ UInt32 count,
+ UInt32 offset,
+ task_t task,
+ IOOptionBits options,
+ IOMapper * mapper = 0);
/*! @function withPhysicalRanges
@abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
static IOMemoryDescriptor * withPhysicalRanges(
IOPhysicalRange * ranges,
UInt32 withCount,
- IODirection withDirection,
+ IODirection withDirection,
bool asReference = false);
/*! @function withSubRange
@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.
@result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
- static IOMemoryDescriptor * withSubRange(IOMemoryDescriptor * of,
- IOByteCount offset,
- IOByteCount length,
- IODirection withDirection);
+ static IOMemoryDescriptor * withSubRange(IOMemoryDescriptor *of,
+ IOByteCount offset,
+ IOByteCount length,
+ IODirection withDirection);
+
+/*! @function withPersistentMemoryDescriptor
+ @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
+ @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.
+ @param originalMD The memory descriptor to be duplicated.
+ @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. */
+ static IOMemoryDescriptor *
+ withPersistentMemoryDescriptor(IOMemoryDescriptor *originalMD);
/*! @function initWithAddress
@abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
@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.
@result true on success, false on failure. */
- virtual bool initWithRanges( IOVirtualRange * ranges,
- UInt32 withCount,
- IODirection withDirection,
- task_t withTask,
- bool asReference = false) = 0;
+ virtual bool initWithRanges(IOVirtualRange * ranges,
+ UInt32 withCount,
+ IODirection withDirection,
+ task_t withTask,
+ bool asReference = false) = 0;
/*! @function initWithPhysicalRanges
@abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
/*! @function prepare
@abstract Prepare the memory for an I/O transfer.
- @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. This method needn't called for non-pageable memory.
+ @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.
@param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
@result An IOReturn code. */
/*! @function complete
@abstract Complete processing of the memory after an I/O transfer finishes.
- @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.
- @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
+ @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.
+ @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
@result An IOReturn code. */
virtual IOReturn complete(IODirection forDirection = kIODirectionNone) = 0;
kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
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>
+ 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>
@param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
@param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
@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. */
virtual IOReturn unmap() = 0;
- virtual void taskDied() = 0;
+ virtual void taskDied() = 0;
+
+/*! @function redirect
+ @abstract Replace the memory mapped in a process with new backing memory.
+ @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.
+ @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.
+ @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
+ @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
+ @result An IOReturn code. */
+
+ virtual IOReturn redirect(IOMemoryDescriptor * newBackingMemory,
+ IOOptionBits options,
+ IOByteCount offset = 0) = 0;
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// might be created by IOMemoryDescriptor::withAddress(), but there should be
// no need to reference as anything but a generic IOMemoryDescriptor *.
+// Also these flags should not overlap with the options to
+// IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
+
enum {
- kIOMemoryRequiresWire = 0x00000001
+ kIOMemoryPreparedReadOnly = 0x00008000,
};
class IOGeneralMemoryDescriptor : public IOMemoryDescriptor
{
OSDeclareDefaultStructors(IOGeneralMemoryDescriptor);
+public:
+ union Ranges {
+ IOVirtualRange * v;
+ IOPhysicalRange * p;
+ void *uio;
+ };
protected:
- union {
- IOVirtualRange * v;
- IOPhysicalRange * p;
- } _ranges; /* list of address ranges */
+ Ranges _ranges;
unsigned _rangesCount; /* number of address ranges in list */
bool _rangesIsAllocated; /* is list allocated by us? */
task_t _task; /* task where all ranges are mapped to */
union {
- IOVirtualRange v;
- IOPhysicalRange p;
+ IOVirtualRange v;
+ IOPhysicalRange p;
} _singleRange; /* storage space for a single range */
unsigned _wireCount; /* number of outstanding wires */
- vm_address_t _cachedVirtualAddress; /* a cached virtual-to-physical */
- IOPhysicalAddress _cachedPhysicalAddress; /* mapping, for optimization */
+ /* DEPRECATED */ vm_address_t _cachedVirtualAddress; /* a cached virtual-to-physical */
+
+ /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress;
bool _initialized; /* has superclass been initialized? */
virtual void free();
-protected:
+
+private:
+ // Internal APIs may be made virtual at some time in the future.
+ IOReturn wireVirtual(IODirection forDirection);
+ void *createNamedEntry();
+
+
/* DEPRECATED */ IOByteCount _position; /* absolute position over all ranges */
/* DEPRECATED */ virtual void setPosition(IOByteCount position);
-private:
- /* DEPRECATED */ unsigned _positionAtIndex; /* range #n in which position is now */
- /* DEPRECATED */ IOByteCount _positionAtOffset; /* relative position within range #n */
+/*
+ * DEPRECATED IOByteCount _positionAtIndex; // relative position within range #n
+ *
+ * Re-use the _positionAtIndex as a count of the number of pages in
+ * this memory descriptor. Convieniently vm_address_t is an unsigned integer
+ * type so I can get away without having to change the type.
+ */
+ unsigned int _pages;
+
+/* DEPRECATED */ unsigned _positionAtOffset; //range #n in which position is now
+
OSData *_memoryEntries;
/* DEPRECATED */ vm_offset_t _kernPtrAligned;
/* DEPRECATED */ unsigned _kernPtrAtIndex;
/* DEPRECATED */ IOByteCount _kernSize;
+
/* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex);
/* DEPRECATED */ virtual void unmapFromKernel();
- inline vm_map_t getMapForTask( task_t task, vm_address_t address );
public:
/*
* IOMemoryDescriptor required methods
*/
- virtual bool initWithAddress(void * address,
- IOByteCount withLength,
- IODirection withDirection);
+ // Master initaliser
+ virtual bool initWithOptions(void * buffers,
+ UInt32 count,
+ UInt32 offset,
+ task_t task,
+ IOOptionBits options,
+ IOMapper * mapper = 0);
- virtual bool initWithAddress(vm_address_t address,
+ // Secondary initialisers
+ virtual bool initWithAddress(void * address,
+ IOByteCount withLength,
+ IODirection withDirection);
+
+ virtual bool initWithAddress(vm_address_t address,
IOByteCount withLength,
- IODirection withDirection,
- task_t withTask);
+ IODirection withDirection,
+ task_t withTask);
virtual bool initWithPhysicalAddress(
IOPhysicalAddress address,
IOVirtualAddress logical,
IOByteCount length );
virtual bool serialize(OSSerialize *s) const;
+
+ // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
+ static IOMemoryDescriptor *
+ withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor *originalMD);
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
virtual void free();
- virtual bool initSubRange( IOMemoryDescriptor * parent,
- IOByteCount offset, IOByteCount length,
- IODirection withDirection );
-
virtual bool initWithAddress(void * address,
IOByteCount withLength,
IODirection withDirection);
IOMemoryDescriptor::withSubRange;
public:
+ /*
+ * Initialize or reinitialize an IOSubMemoryDescriptor to describe
+ * a subrange of an existing descriptor.
+ *
+ * An IOSubMemoryDescriptor can be re-used by calling initSubRange
+ * again on an existing instance -- note that this behavior is not
+ * commonly supported in other IOKit classes, although it is here.
+ */
+ virtual bool initSubRange( IOMemoryDescriptor * parent,
+ IOByteCount offset, IOByteCount length,
+ IODirection withDirection );
+
/*
* IOMemoryDescriptor required methods
*/
virtual bool serialize(OSSerialize *s) const;
+ virtual IOReturn setPurgeable( IOOptionBits newState,
+ IOOptionBits * oldState );
+ virtual IOReturn performOperation( IOOptionBits options,
+ IOByteCount offset, IOByteCount length );
+
protected:
virtual IOMemoryMap * makeMapping(
IOMemoryDescriptor * owner,
IOOptionBits options,
IOByteCount offset,
IOByteCount length );
+
+ virtual IOReturn doMap(
+ vm_map_t addressMap,
+ IOVirtualAddress * atAddress,
+ IOOptionBits options,
+ IOByteCount sourceOffset = 0,
+ IOByteCount length = 0 );
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */