*
* @APPLE_LICENSE_HEADER_START@
*
- * 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.
+ * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
*
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * 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.
*
* @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 second withRanges variant
+ */
+enum {
+ kIOMemoryDirectionMask = 0x00000007,
+ kIOMemoryAutoPrepare = 0x00000008, // Shared with Buffer MD
+
+ kIOMemoryTypeVirtual = 0x00000010,
+ kIOMemoryTypePhysical = 0x00000020,
+ kIOMemoryTypeUPL = 0x00000030,
+ kIOMemoryTypeMask = 0x000000f0,
+
+ kIOMemoryAsReference = 0x00000100,
+ kIOMemoryBufferPageable = 0x00000400,
+ kIOMemoryDontMap = 0x00000800,
+};
+
+#define kIOMapperNone ((IOMapper *) -1)
+#define kIOMapperSystem ((IOMapper *) 0)
+
/*! @class IOMemoryDescriptor : public OSObject
@abstract An abstract base class defining common methods for describing physical or virtual memory.
@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. */
/*! @struct ExpansionData
@discussion This structure will be used to expand the capablilties of this class in the future.
*/
- struct ExpansionData { };
+ struct ExpansionData {
+ void * devicePager;
+ unsigned int pagerContig:1;
+ unsigned int unused:31;
+ IOMemoryDescriptor * memory;
+ };
/*! @var reserved
Reserved for future use. (Internal use only) */
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;
+public:
+
+ 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 IOMemoryDescriptor::getPhysicalSegment64( IOByteCount offset,
+ IOByteCount * length );
+ OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 2);
+
private:
- OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 0);
- OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 1);
- OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 2);
+
OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 3);
OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 4);
OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 5);
@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 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.
@result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
virtual IOByteCount readBytes(IOByteCount offset,
- void * bytes, IOByteCount withLength) = 0;
+ void * bytes, IOByteCount withLength);
/*! @function writeBytes
@abstract Copy data to the memory descriptor's buffer from the specified buffer.
@result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
virtual IOByteCount writeBytes(IOByteCount offset,
- const void * bytes, IOByteCount withLength) = 0;
+ const void * bytes, IOByteCount withLength);
/*! @function getPhysicalSegment
@abstract Break a memory descriptor into its physically contiguous segments.
@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.
@result A physical address. */
- inline IOPhysicalAddress getPhysicalAddress()
- { return( getPhysicalSegment( 0, 0 )); }
+ /* inline */ IOPhysicalAddress getPhysicalAddress();
+ /* { return( getPhysicalSegment( 0, 0 )); } */
- /*
- * getVirtualSegment:
- *
- * Get the virtual address of the buffer, relative to the given offset.
- * If the memory wasn't mapped into the caller's address space, it will be
- * mapped in now. If the current position is at the end of the buffer, a
- * null is returned.
- */
- virtual void * getVirtualSegment(IOByteCount offset,
- IOByteCount * length) = 0;
+ /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
+ /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
+ /* DEPRECATED */ IOByteCount * length) = 0;
+ /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
/*! @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;
IOVirtualAddress mapAddress,
IOOptionBits options = 0 );
+ // Following methods are private implementation
+
+ // make virtual
+ IOReturn redirect( task_t safeTask, bool redirect );
+
+ IOReturn handleFault(
+ void * pager,
+ vm_map_t addressMap,
+ IOVirtualAddress address,
+ IOByteCount sourceOffset,
+ IOByteCount length,
+ IOOptionBits options );
+
protected:
virtual IOMemoryMap * makeMapping(
IOMemoryDescriptor * owner,
@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.
@result A physical address. */
- inline IOPhysicalAddress getPhysicalAddress()
- { return( getPhysicalSegment( 0, 0 )); }
+ /* inline */ IOPhysicalAddress getPhysicalAddress();
+ /* { return( getPhysicalSegment( 0, 0 )); } */
/*! @function getLength
@abstract Accessor to the length of the mapping.
virtual IOReturn unmap() = 0;
- virtual void taskDied() = 0;
+ virtual void taskDied() = 0;
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+// The following classes are private implementation of IOMemoryDescriptor - they
+// should not be referenced directly, just through the public API's in the
+// IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
+// 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
protected:
union {
- IOVirtualRange * v;
- IOPhysicalRange * p;
+ IOVirtualRange * v;
+ IOPhysicalRange * p;
} _ranges; /* list of address 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: /* (to be deprecated) */
- IOByteCount _position; /* absolute position over all ranges */
- virtual void setPosition(IOByteCount position);
private:
- unsigned _positionAtIndex; /* range #n in which position is now */
- IOByteCount _positionAtOffset; /* relative position within range #n */
+ // Internal API may be made virtual at some time in the future.
+ IOReturn wireVirtual(IODirection forDirection);
+
+ /* DEPRECATED */ IOByteCount _position; /* absolute position over all ranges */
+ /* DEPRECATED */ virtual void setPosition(IOByteCount position);
+
+/*
+ * 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;
- vm_offset_t _kernPtrAligned;
- unsigned _kernPtrAtIndex;
- IOByteCount _kernSize;
- virtual void mapIntoKernel(unsigned rangeIndex);
- virtual void unmapFromKernel();
- inline vm_map_t getMapForTask( task_t task, vm_address_t address );
+ /* DEPRECATED */ vm_offset_t _kernPtrAligned;
+ /* DEPRECATED */ unsigned _kernPtrAtIndex;
+ /* DEPRECATED */ IOByteCount _kernSize;
+
+ /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex);
+ /* DEPRECATED */ virtual void unmapFromKernel();
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,
IODirection withDirection,
bool asReference = false);
- virtual IOByteCount readBytes(IOByteCount offset,
- void * bytes, IOByteCount withLength);
-
- virtual IOByteCount writeBytes(IOByteCount offset,
- const void * bytes, IOByteCount withLength);
-
virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
IOByteCount * length);
- virtual void * getVirtualSegment(IOByteCount offset,
- IOByteCount * length);
+ virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
+ IOByteCount * length);
+
+ /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
+ /* DEPRECATED */ IOByteCount * length);
virtual IOReturn prepare(IODirection forDirection = kIODirectionNone);
vm_map_t addressMap,
IOVirtualAddress logical,
IOByteCount length );
+ virtual bool serialize(OSSerialize *s) const;
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
class IOSubMemoryDescriptor : public IOMemoryDescriptor
{
- friend IOMemoryDescriptor;
+ friend class IOMemoryDescriptor;
OSDeclareDefaultStructors(IOSubMemoryDescriptor);
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 IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
IOByteCount * length);
+ virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
+ IOByteCount * length);
+
virtual IOByteCount readBytes(IOByteCount offset,
void * bytes, IOByteCount withLength);
virtual IOReturn complete(IODirection forDirection = kIODirectionNone);
+ // make virtual
+ IOReturn redirect( task_t safeTask, bool redirect );
+
+ virtual bool serialize(OSSerialize *s) const;
+
protected:
virtual IOMemoryMap * makeMapping(
IOMemoryDescriptor * owner,