2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #ifndef _IOMEMORYDESCRIPTOR_H
23 #define _IOMEMORYDESCRIPTOR_H
25 #include <sys/cdefs.h>
27 #include <IOKit/IOTypes.h>
28 #include <libkern/c++/OSContainers.h>
31 #include <mach/memory_object_types.h>
34 struct IOPhysicalRange
36 IOPhysicalAddress address
;
44 * Direction of transfer, with respect to the described memory.
48 kIODirectionNone
= 0x0, // same as VM_PROT_NONE
49 kIODirectionIn
= 0x1, // User land 'read', same as VM_PROT_READ
50 kIODirectionOut
= 0x2, // User land 'write', same as VM_PROT_WRITE
51 kIODirectionOutIn
= kIODirectionOut
| kIODirectionIn
,
52 kIODirectionInOut
= kIODirectionIn
| kIODirectionOut
56 * IOOptionBits used in the withOptions variant
59 kIOMemoryDirectionMask
= 0x00000007,
60 kIOMemoryAutoPrepare
= 0x00000008, // Shared with Buffer MD
62 kIOMemoryTypeVirtual
= 0x00000010,
63 kIOMemoryTypePhysical
= 0x00000020,
64 kIOMemoryTypeUPL
= 0x00000030,
65 kIOMemoryTypePersistentMD
= 0x00000040, // Persistent Memory Descriptor
66 kIOMemoryTypeUIO
= 0x00000050,
67 kIOMemoryTypeMask
= 0x000000f0,
69 kIOMemoryAsReference
= 0x00000100,
70 kIOMemoryBufferPageable
= 0x00000400,
71 kIOMemoryDontMap
= 0x00000800,
72 kIOMemoryPersistent
= 0x00010000
75 #define kIOMapperNone ((IOMapper *) -1)
76 #define kIOMapperSystem ((IOMapper *) 0)
80 kIOMemoryPurgeableKeepCurrent
= 1,
81 kIOMemoryPurgeableNonVolatile
= 2,
82 kIOMemoryPurgeableVolatile
= 3,
83 kIOMemoryPurgeableEmpty
= 4
87 kIOMemoryIncoherentIOFlush
= 1,
88 kIOMemoryIncoherentIOStore
= 2,
91 /*! @class IOMemoryDescriptor : public OSObject
92 @abstract An abstract base class defining common methods for describing physical or virtual memory.
93 @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. */
95 class IOMemoryDescriptor
: public OSObject
97 friend class _IOMemoryMap
;
98 friend class IOSubMemoryDescriptor
;
100 OSDeclareDefaultStructors(IOMemoryDescriptor
);
103 /*! @struct ExpansionData
104 @discussion This structure will be used to expand the capablilties of this class in the future.
106 struct ExpansionData
{
108 unsigned int pagerContig
:1;
109 unsigned int unused
:31;
110 IOMemoryDescriptor
* memory
;
114 Reserved for future use. (Internal use only) */
115 ExpansionData
* reserved
;
122 IODirection _direction
; /* DEPRECATED: use _flags instead. direction of transfer */
123 IOByteCount _length
; /* length of all ranges */
128 virtual IOPhysicalAddress
getSourceSegment( IOByteCount offset
,
129 IOByteCount
* length
);
130 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 0);
132 /*! @function initWithOptions
133 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
134 @discussion Note this function can be used to re-init a previously created memory descriptor.
135 @result true on success, false on failure. */
136 virtual bool initWithOptions(void * buffers
,
140 IOOptionBits options
,
141 IOMapper
* mapper
= 0);
142 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 1);
144 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
145 IOByteCount
* length
);
146 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 2);
149 /*! @function setPurgeable
150 @abstract Control the purgeable status of a memory descriptors memory.
151 @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.
152 @param newState - the desired new purgeable state of the memory:<br>
153 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
154 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
155 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
156 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
157 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
158 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
159 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
160 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
161 @result An IOReturn code. */
163 virtual IOReturn
setPurgeable( IOOptionBits newState
,
164 IOOptionBits
* oldState
);
165 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 3);
167 /*! @function performOperation
168 @abstract Perform an operation on the memory descriptor's memory.
169 @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.
170 @param options The operation to perform on the memory:<br>
171 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>
172 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.
173 @param offset A byte offset into the memory descriptor's memory.
174 @param length The length of the data range.
175 @result An IOReturn code. */
177 virtual IOReturn
performOperation( IOOptionBits options
,
178 IOByteCount offset
, IOByteCount length
);
179 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 4);
183 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 5);
184 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 6);
185 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 7);
186 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 8);
187 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 9);
188 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 10);
189 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 11);
190 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 12);
191 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 13);
192 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 14);
193 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 15);
198 static void initialize( void );
201 /*! @function withAddress
202 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
203 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map.
204 @param address The virtual address of the first byte in the memory.
205 @param withLength The length of memory.
206 @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.
207 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
209 static IOMemoryDescriptor
* withAddress(void * address
,
210 IOByteCount withLength
,
211 IODirection withDirection
);
213 /*! @function withAddress
214 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
215 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
216 @param address The virtual address of the first byte in the memory.
217 @param withLength The length of memory.
218 @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.
219 @param withTask The task the virtual ranges are mapped into.
220 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
222 static IOMemoryDescriptor
* withAddress(vm_address_t address
,
223 IOByteCount withLength
,
224 IODirection withDirection
,
227 /*! @function withPhysicalAddress
228 @abstract Create an IOMemoryDescriptor to describe one physical range.
229 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
230 @param address The physical address of the first byte in the memory.
231 @param withLength The length of memory.
232 @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.
233 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
235 static IOMemoryDescriptor
* withPhysicalAddress(
236 IOPhysicalAddress address
,
237 IOByteCount withLength
,
238 IODirection withDirection
);
240 /*! @function withRanges
241 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
242 @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.
243 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
244 @param withCount The member count of the ranges array.
245 @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.
246 @param withTask The task each of the virtual ranges are mapped into.
247 @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.
248 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
250 static IOMemoryDescriptor
* withRanges(IOVirtualRange
* ranges
,
252 IODirection withDirection
,
254 bool asReference
= false);
256 /*! @function withOptions
257 @abstract Master initialiser for all variants of memory descriptors.
258 @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.
261 @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.
263 @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.
265 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
267 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
270 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.
271 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.
272 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.
273 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.
274 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
276 @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.
278 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
280 static IOMemoryDescriptor
*withOptions(void * buffers
,
284 IOOptionBits options
,
285 IOMapper
* mapper
= 0);
287 /*! @function withPhysicalRanges
288 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
289 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
290 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
291 @param withCount The member count of the ranges array.
292 @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.
293 @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.
294 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
296 static IOMemoryDescriptor
* withPhysicalRanges(
297 IOPhysicalRange
* ranges
,
299 IODirection withDirection
,
300 bool asReference
= false);
302 /*! @function withSubRange
303 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
304 @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.
305 @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.
306 @param offset A byte offset into the parent memory descriptor's memory.
307 @param length The length of the subrange.
308 @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.
309 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
311 static IOMemoryDescriptor
* withSubRange(IOMemoryDescriptor
*of
,
314 IODirection withDirection
);
316 /*! @function withPersistentMemoryDescriptor
317 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
318 @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.
319 @param originalMD The memory descriptor to be duplicated.
320 @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. */
321 static IOMemoryDescriptor
*
322 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
);
324 /*! @function initWithAddress
325 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
326 @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.
327 @param address The virtual address of the first byte in the memory.
328 @param withLength The length of memory.
329 @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.
330 @result true on success, false on failure. */
332 virtual bool initWithAddress(void * address
,
333 IOByteCount withLength
,
334 IODirection withDirection
) = 0;
336 /*! @function initWithAddress
337 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
338 @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.
339 @param address The virtual address of the first byte in the memory.
340 @param withLength The length of memory.
341 @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.
342 @param withTask The task the virtual ranges are mapped into.
343 @result true on success, false on failure. */
345 virtual bool initWithAddress(vm_address_t address
,
346 IOByteCount withLength
,
347 IODirection withDirection
,
348 task_t withTask
) = 0;
350 /*! @function initWithPhysicalAddress
351 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
352 @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.
353 @param address The physical address of the first byte in the memory.
354 @param withLength The length of memory.
355 @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.
356 @result true on success, false on failure. */
358 virtual bool initWithPhysicalAddress(
359 IOPhysicalAddress address
,
360 IOByteCount withLength
,
361 IODirection withDirection
) = 0;
363 /*! @function initWithRanges
364 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
365 @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.
366 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
367 @param withCount The member count of the ranges array.
368 @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.
369 @param withTask The task each of the virtual ranges are mapped into.
370 @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.
371 @result true on success, false on failure. */
373 virtual bool initWithRanges(IOVirtualRange
* ranges
,
375 IODirection withDirection
,
377 bool asReference
= false) = 0;
379 /*! @function initWithPhysicalRanges
380 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
381 @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.
382 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
383 @param withCount The member count of the ranges array.
384 @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.
385 @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.
386 @result true on success, false on failure. */
388 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
390 IODirection withDirection
,
391 bool asReference
= false) = 0;
393 /*! @function getDirection
394 @abstract Accessor to get the direction the memory descriptor was created with.
395 @discussion This method returns the direction the memory descriptor was created with.
396 @result The direction. */
398 virtual IODirection
getDirection() const;
400 /*! @function getLength
401 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
402 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
403 @result The byte count. */
405 virtual IOByteCount
getLength() const;
408 @abstract Set the tag for the memory descriptor.
409 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
410 @param tag The tag. */
412 virtual void setTag( IOOptionBits tag
);
415 @abstract Accessor to the retrieve the tag for the memory descriptor.
416 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
419 virtual IOOptionBits
getTag( void );
421 /*! @function readBytes
422 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
423 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
424 @param offset A byte offset into the memory descriptor's memory.
425 @param bytes The caller supplied buffer to copy the data to.
426 @param withLength The length of the data to copy.
427 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
429 virtual IOByteCount
readBytes(IOByteCount offset
,
430 void * bytes
, IOByteCount withLength
);
432 /*! @function writeBytes
433 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
434 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
435 @param offset A byte offset into the memory descriptor's memory.
436 @param bytes The caller supplied buffer to copy the data from.
437 @param withLength The length of the data to copy.
438 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
440 virtual IOByteCount
writeBytes(IOByteCount offset
,
441 const void * bytes
, IOByteCount withLength
);
443 /*! @function getPhysicalSegment
444 @abstract Break a memory descriptor into its physically contiguous segments.
445 @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.
446 @param offset A byte offset into the memory whose physical address to return.
447 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
448 @result A physical address, or zero if the offset is beyond the length of the memory. */
450 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
451 IOByteCount
* length
) = 0;
453 /*! @function getPhysicalAddress
454 @abstract Return the physical address of the first byte in the memory.
455 @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.
456 @result A physical address. */
458 /* inline */ IOPhysicalAddress
getPhysicalAddress();
459 /* { return( getPhysicalSegment( 0, 0 )); } */
461 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
462 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
463 /* DEPRECATED */ IOByteCount
* length
) = 0;
464 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
466 /*! @function prepare
467 @abstract Prepare the memory for an I/O transfer.
468 @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.
469 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
470 @result An IOReturn code. */
472 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
) = 0;
474 /*! @function complete
475 @abstract Complete processing of the memory after an I/O transfer finishes.
476 @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.
477 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
478 @result An IOReturn code. */
480 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
) = 0;
487 @abstract Maps a IOMemoryDescriptor into a task.
488 @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.
489 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
490 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
491 @param options Mapping options are defined in IOTypes.h,<br>
492 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>
493 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
494 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
495 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
496 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>
497 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>
498 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
499 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
500 @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. */
502 virtual IOMemoryMap
* map(
504 IOVirtualAddress atAddress
,
505 IOOptionBits options
,
506 IOByteCount offset
= 0,
507 IOByteCount length
= 0 );
510 @abstract Maps a IOMemoryDescriptor into the kernel map.
511 @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.
512 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
513 @result See the full version of the map method. */
515 virtual IOMemoryMap
* map(
516 IOOptionBits options
= 0 );
518 /*! @function setMapping
519 @abstract Establishes an already existing mapping.
520 @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.
521 @param task Address space in which the mapping exists.
522 @param mapAddress Virtual address of the mapping.
523 @param options Caching and read-only attributes of the mapping.
524 @result A IOMemoryMap object created to represent the mapping. */
526 virtual IOMemoryMap
* setMapping(
528 IOVirtualAddress mapAddress
,
529 IOOptionBits options
= 0 );
531 // Following methods are private implementation
534 IOReturn
redirect( task_t safeTask
, bool redirect
);
536 IOReturn
handleFault(
539 IOVirtualAddress address
,
540 IOByteCount sourceOffset
,
542 IOOptionBits options
);
545 virtual IOMemoryMap
* makeMapping(
546 IOMemoryDescriptor
* owner
,
548 IOVirtualAddress atAddress
,
549 IOOptionBits options
,
551 IOByteCount length
);
553 virtual void addMapping(
554 IOMemoryMap
* mapping
);
556 virtual void removeMapping(
557 IOMemoryMap
* mapping
);
559 virtual IOReturn
doMap(
561 IOVirtualAddress
* atAddress
,
562 IOOptionBits options
,
563 IOByteCount sourceOffset
= 0,
564 IOByteCount length
= 0 );
566 virtual IOReturn
doUnmap(
568 IOVirtualAddress logical
,
569 IOByteCount length
);
572 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
574 /*! @class IOMemoryMap : public OSObject
575 @abstract An abstract base class defining common methods for describing a memory mapping.
576 @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. */
578 class IOMemoryMap
: public OSObject
580 OSDeclareAbstractStructors(IOMemoryMap
)
583 /*! @function getVirtualAddress
584 @abstract Accessor to the virtual address of the first byte in the mapping.
585 @discussion This method returns the virtual address of the first byte in the mapping.
586 @result A virtual address. */
588 virtual IOVirtualAddress
getVirtualAddress() = 0;
590 /*! @function getPhysicalSegment
591 @abstract Break a mapping into its physically contiguous segments.
592 @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.
593 @param offset A byte offset into the mapping whose physical address to return.
594 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
595 @result A physical address, or zero if the offset is beyond the length of the mapping. */
597 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
598 IOByteCount
* length
) = 0;
600 /*! @function getPhysicalAddress
601 @abstract Return the physical address of the first byte in the mapping.
602 @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.
603 @result A physical address. */
605 /* inline */ IOPhysicalAddress
getPhysicalAddress();
606 /* { return( getPhysicalSegment( 0, 0 )); } */
608 /*! @function getLength
609 @abstract Accessor to the length of the mapping.
610 @discussion This method returns the length of the mapping.
611 @result A byte count. */
613 virtual IOByteCount
getLength() = 0;
615 /*! @function getAddressTask
616 @abstract Accessor to the task of the mapping.
617 @discussion This method returns the mach task the mapping exists in.
618 @result A mach task_t. */
620 virtual task_t
getAddressTask() = 0;
622 /*! @function getMemoryDescriptor
623 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
624 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
625 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
627 virtual IOMemoryDescriptor
* getMemoryDescriptor() = 0;
629 /*! @function getMapOptions
630 @abstract Accessor to the options the mapping was created with.
631 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
632 @result Options for the mapping, including cache settings. */
634 virtual IOOptionBits
getMapOptions() = 0;
637 @abstract Force the IOMemoryMap to unmap, without destroying the object.
638 @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.
639 @result An IOReturn code. */
641 virtual IOReturn
unmap() = 0;
643 virtual void taskDied() = 0;
645 /*! @function redirect
646 @abstract Replace the memory mapped in a process with new backing memory.
647 @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.
648 @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.
649 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
650 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
651 @result An IOReturn code. */
653 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
654 IOOptionBits options
,
655 IOByteCount offset
= 0) = 0;
658 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
660 // The following classes are private implementation of IOMemoryDescriptor - they
661 // should not be referenced directly, just through the public API's in the
662 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
663 // might be created by IOMemoryDescriptor::withAddress(), but there should be
664 // no need to reference as anything but a generic IOMemoryDescriptor *.
666 // Also these flags should not overlap with the options to
667 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
670 kIOMemoryPreparedReadOnly
= 0x00008000,
673 class IOGeneralMemoryDescriptor
: public IOMemoryDescriptor
675 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor
);
685 unsigned _rangesCount
; /* number of address ranges in list */
686 bool _rangesIsAllocated
; /* is list allocated by us? */
688 task_t _task
; /* task where all ranges are mapped to */
693 } _singleRange
; /* storage space for a single range */
695 unsigned _wireCount
; /* number of outstanding wires */
697 /* DEPRECATED */ vm_address_t _cachedVirtualAddress
; /* a cached virtual-to-physical */
699 /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress
;
701 bool _initialized
; /* has superclass been initialized? */
707 // Internal APIs may be made virtual at some time in the future.
708 IOReturn
wireVirtual(IODirection forDirection
);
709 void *createNamedEntry();
712 /* DEPRECATED */ IOByteCount _position
; /* absolute position over all ranges */
713 /* DEPRECATED */ virtual void setPosition(IOByteCount position
);
716 * DEPRECATED IOByteCount _positionAtIndex; // relative position within range #n
718 * Re-use the _positionAtIndex as a count of the number of pages in
719 * this memory descriptor. Convieniently vm_address_t is an unsigned integer
720 * type so I can get away without having to change the type.
724 /* DEPRECATED */ unsigned _positionAtOffset
; //range #n in which position is now
726 OSData
*_memoryEntries
;
728 /* DEPRECATED */ vm_offset_t _kernPtrAligned
;
729 /* DEPRECATED */ unsigned _kernPtrAtIndex
;
730 /* DEPRECATED */ IOByteCount _kernSize
;
732 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex
);
733 /* DEPRECATED */ virtual void unmapFromKernel();
737 * IOMemoryDescriptor required methods
741 virtual bool initWithOptions(void * buffers
,
745 IOOptionBits options
,
746 IOMapper
* mapper
= 0);
748 // Secondary initialisers
749 virtual bool initWithAddress(void * address
,
750 IOByteCount withLength
,
751 IODirection withDirection
);
753 virtual bool initWithAddress(vm_address_t address
,
754 IOByteCount withLength
,
755 IODirection withDirection
,
758 virtual bool initWithPhysicalAddress(
759 IOPhysicalAddress address
,
760 IOByteCount withLength
,
761 IODirection withDirection
);
763 virtual bool initWithRanges( IOVirtualRange
* ranges
,
765 IODirection withDirection
,
767 bool asReference
= false);
769 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
771 IODirection withDirection
,
772 bool asReference
= false);
774 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
775 IOByteCount
* length
);
777 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
778 IOByteCount
* length
);
780 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
781 /* DEPRECATED */ IOByteCount
* length
);
783 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
785 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
787 virtual IOReturn
doMap(
789 IOVirtualAddress
* atAddress
,
790 IOOptionBits options
,
791 IOByteCount sourceOffset
= 0,
792 IOByteCount length
= 0 );
794 virtual IOReturn
doUnmap(
796 IOVirtualAddress logical
,
797 IOByteCount length
);
798 virtual bool serialize(OSSerialize
*s
) const;
800 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
801 static IOMemoryDescriptor
*
802 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
);
805 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
807 class IOSubMemoryDescriptor
: public IOMemoryDescriptor
809 friend class IOMemoryDescriptor
;
811 OSDeclareDefaultStructors(IOSubMemoryDescriptor
);
814 IOMemoryDescriptor
* _parent
;
819 virtual bool initWithAddress(void * address
,
820 IOByteCount withLength
,
821 IODirection withDirection
);
823 virtual bool initWithAddress(vm_address_t address
,
824 IOByteCount withLength
,
825 IODirection withDirection
,
828 virtual bool initWithPhysicalAddress(
829 IOPhysicalAddress address
,
830 IOByteCount withLength
,
831 IODirection withDirection
);
833 virtual bool initWithRanges( IOVirtualRange
* ranges
,
835 IODirection withDirection
,
837 bool asReference
= false);
839 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
841 IODirection withDirection
,
842 bool asReference
= false);
844 IOMemoryDescriptor::withAddress
;
845 IOMemoryDescriptor::withPhysicalAddress
;
846 IOMemoryDescriptor::withPhysicalRanges
;
847 IOMemoryDescriptor::withRanges
;
848 IOMemoryDescriptor::withSubRange
;
852 * Initialize or reinitialize an IOSubMemoryDescriptor to describe
853 * a subrange of an existing descriptor.
855 * An IOSubMemoryDescriptor can be re-used by calling initSubRange
856 * again on an existing instance -- note that this behavior is not
857 * commonly supported in other IOKit classes, although it is here.
859 virtual bool initSubRange( IOMemoryDescriptor
* parent
,
860 IOByteCount offset
, IOByteCount length
,
861 IODirection withDirection
);
864 * IOMemoryDescriptor required methods
867 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
868 IOByteCount
* length
);
870 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
871 IOByteCount
* length
);
873 virtual IOByteCount
readBytes(IOByteCount offset
,
874 void * bytes
, IOByteCount withLength
);
876 virtual IOByteCount
writeBytes(IOByteCount offset
,
877 const void * bytes
, IOByteCount withLength
);
879 virtual void * getVirtualSegment(IOByteCount offset
,
880 IOByteCount
* length
);
882 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
884 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
887 IOReturn
redirect( task_t safeTask
, bool redirect
);
889 virtual bool serialize(OSSerialize
*s
) const;
891 virtual IOReturn
setPurgeable( IOOptionBits newState
,
892 IOOptionBits
* oldState
);
893 virtual IOReturn
performOperation( IOOptionBits options
,
894 IOByteCount offset
, IOByteCount length
);
897 virtual IOMemoryMap
* makeMapping(
898 IOMemoryDescriptor
* owner
,
900 IOVirtualAddress atAddress
,
901 IOOptionBits options
,
903 IOByteCount length
);
905 virtual IOReturn
doMap(
907 IOVirtualAddress
* atAddress
,
908 IOOptionBits options
,
909 IOByteCount sourceOffset
= 0,
910 IOByteCount length
= 0 );
913 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
915 #endif /* !_IOMEMORYDESCRIPTOR_H */