2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #ifndef _IOMEMORYDESCRIPTOR_H
24 #define _IOMEMORYDESCRIPTOR_H
26 #include <sys/cdefs.h>
28 #include <IOKit/IOTypes.h>
29 #include <libkern/c++/OSContainers.h>
32 #include <mach/memory_object_types.h>
35 struct IOPhysicalRange
37 IOPhysicalAddress address
;
45 * Direction of transfer, with respect to the described memory.
49 kIODirectionNone
= 0x0, // same as VM_PROT_NONE
50 kIODirectionIn
= 0x1, // User land 'read', same as VM_PROT_READ
51 kIODirectionOut
= 0x2, // User land 'write', same as VM_PROT_WRITE
52 kIODirectionOutIn
= kIODirectionOut
| kIODirectionIn
,
53 kIODirectionInOut
= kIODirectionIn
| kIODirectionOut
57 * IOOptionBits used in the withOptions variant
60 kIOMemoryDirectionMask
= 0x00000007,
61 kIOMemoryAutoPrepare
= 0x00000008, // Shared with Buffer MD
63 kIOMemoryTypeVirtual
= 0x00000010,
64 kIOMemoryTypePhysical
= 0x00000020,
65 kIOMemoryTypeUPL
= 0x00000030,
66 kIOMemoryTypePersistentMD
= 0x00000040, // Persistent Memory Descriptor
67 kIOMemoryTypeUIO
= 0x00000050,
68 kIOMemoryTypeMask
= 0x000000f0,
70 kIOMemoryAsReference
= 0x00000100,
71 kIOMemoryBufferPageable
= 0x00000400,
72 kIOMemoryDontMap
= 0x00000800,
73 kIOMemoryPersistent
= 0x00010000
76 #define kIOMapperNone ((IOMapper *) -1)
77 #define kIOMapperSystem ((IOMapper *) 0)
81 kIOMemoryPurgeableKeepCurrent
= 1,
82 kIOMemoryPurgeableNonVolatile
= 2,
83 kIOMemoryPurgeableVolatile
= 3,
84 kIOMemoryPurgeableEmpty
= 4
88 kIOMemoryIncoherentIOFlush
= 1,
89 kIOMemoryIncoherentIOStore
= 2,
92 /*! @class IOMemoryDescriptor : public OSObject
93 @abstract An abstract base class defining common methods for describing physical or virtual memory.
94 @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. */
96 class IOMemoryDescriptor
: public OSObject
98 friend class _IOMemoryMap
;
99 friend class IOSubMemoryDescriptor
;
101 OSDeclareDefaultStructors(IOMemoryDescriptor
);
104 /*! @struct ExpansionData
105 @discussion This structure will be used to expand the capablilties of this class in the future.
107 struct ExpansionData
{
109 unsigned int pagerContig
:1;
110 unsigned int unused
:31;
111 IOMemoryDescriptor
* memory
;
115 Reserved for future use. (Internal use only) */
116 ExpansionData
* reserved
;
123 IODirection _direction
; /* DEPRECATED: use _flags instead. direction of transfer */
124 IOByteCount _length
; /* length of all ranges */
129 virtual IOPhysicalAddress
getSourceSegment( IOByteCount offset
,
130 IOByteCount
* length
);
131 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 0);
133 /*! @function initWithOptions
134 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
135 @discussion Note this function can be used to re-init a previously created memory descriptor.
136 @result true on success, false on failure. */
137 virtual bool initWithOptions(void * buffers
,
141 IOOptionBits options
,
142 IOMapper
* mapper
= 0);
143 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 1);
145 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
146 IOByteCount
* length
);
147 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 2);
150 /*! @function setPurgeable
151 @abstract Control the purgeable status of a memory descriptors memory.
152 @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.
153 @param newState - the desired new purgeable state of the memory:<br>
154 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
155 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
156 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
157 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
158 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
159 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
160 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
161 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
162 @result An IOReturn code. */
164 virtual IOReturn
setPurgeable( IOOptionBits newState
,
165 IOOptionBits
* oldState
);
166 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 3);
168 /*! @function performOperation
169 @abstract Perform an operation on the memory descriptor's memory.
170 @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.
171 @param options The operation to perform on the memory:<br>
172 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>
173 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.
174 @param offset A byte offset into the memory descriptor's memory.
175 @param length The length of the data range.
176 @result An IOReturn code. */
178 virtual IOReturn
performOperation( IOOptionBits options
,
179 IOByteCount offset
, IOByteCount length
);
180 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 4);
184 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 5);
185 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 6);
186 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 7);
187 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 8);
188 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 9);
189 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 10);
190 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 11);
191 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 12);
192 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 13);
193 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 14);
194 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 15);
199 static void initialize( void );
202 /*! @function withAddress
203 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
204 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map.
205 @param address The virtual address of the first byte in the memory.
206 @param withLength The length of memory.
207 @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.
208 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
210 static IOMemoryDescriptor
* withAddress(void * address
,
211 IOByteCount withLength
,
212 IODirection withDirection
);
214 /*! @function withAddress
215 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
216 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
217 @param address The virtual address of the first byte in the memory.
218 @param withLength The length of memory.
219 @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.
220 @param withTask The task the virtual ranges are mapped into.
221 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
223 static IOMemoryDescriptor
* withAddress(vm_address_t address
,
224 IOByteCount withLength
,
225 IODirection withDirection
,
228 /*! @function withPhysicalAddress
229 @abstract Create an IOMemoryDescriptor to describe one physical range.
230 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
231 @param address The physical address of the first byte in the memory.
232 @param withLength The length of memory.
233 @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.
234 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
236 static IOMemoryDescriptor
* withPhysicalAddress(
237 IOPhysicalAddress address
,
238 IOByteCount withLength
,
239 IODirection withDirection
);
241 /*! @function withRanges
242 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
243 @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.
244 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
245 @param withCount The member count of the ranges array.
246 @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.
247 @param withTask The task each of the virtual ranges are mapped into.
248 @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.
249 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
251 static IOMemoryDescriptor
* withRanges(IOVirtualRange
* ranges
,
253 IODirection withDirection
,
255 bool asReference
= false);
257 /*! @function withOptions
258 @abstract Master initialiser for all variants of memory descriptors.
259 @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.
262 @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.
264 @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.
266 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
268 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
271 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.
272 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.
273 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.
274 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.
275 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
277 @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.
279 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
281 static IOMemoryDescriptor
*withOptions(void * buffers
,
285 IOOptionBits options
,
286 IOMapper
* mapper
= 0);
288 /*! @function withPhysicalRanges
289 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
290 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
291 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
292 @param withCount The member count of the ranges array.
293 @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.
294 @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.
295 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
297 static IOMemoryDescriptor
* withPhysicalRanges(
298 IOPhysicalRange
* ranges
,
300 IODirection withDirection
,
301 bool asReference
= false);
303 /*! @function withSubRange
304 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
305 @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.
306 @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.
307 @param offset A byte offset into the parent memory descriptor's memory.
308 @param length The length of the subrange.
309 @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.
310 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
312 static IOMemoryDescriptor
* withSubRange(IOMemoryDescriptor
*of
,
315 IODirection withDirection
);
317 /*! @function withPersistentMemoryDescriptor
318 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
319 @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.
320 @param originalMD The memory descriptor to be duplicated.
321 @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. */
322 static IOMemoryDescriptor
*
323 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
);
325 /*! @function initWithAddress
326 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
327 @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.
328 @param address The virtual address of the first byte in the memory.
329 @param withLength The length of memory.
330 @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.
331 @result true on success, false on failure. */
333 virtual bool initWithAddress(void * address
,
334 IOByteCount withLength
,
335 IODirection withDirection
) = 0;
337 /*! @function initWithAddress
338 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
339 @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.
340 @param address The virtual address of the first byte in the memory.
341 @param withLength The length of memory.
342 @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.
343 @param withTask The task the virtual ranges are mapped into.
344 @result true on success, false on failure. */
346 virtual bool initWithAddress(vm_address_t address
,
347 IOByteCount withLength
,
348 IODirection withDirection
,
349 task_t withTask
) = 0;
351 /*! @function initWithPhysicalAddress
352 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
353 @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.
354 @param address The physical address of the first byte in the memory.
355 @param withLength The length of memory.
356 @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.
357 @result true on success, false on failure. */
359 virtual bool initWithPhysicalAddress(
360 IOPhysicalAddress address
,
361 IOByteCount withLength
,
362 IODirection withDirection
) = 0;
364 /*! @function initWithRanges
365 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
366 @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.
367 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
368 @param withCount The member count of the ranges array.
369 @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.
370 @param withTask The task each of the virtual ranges are mapped into.
371 @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.
372 @result true on success, false on failure. */
374 virtual bool initWithRanges(IOVirtualRange
* ranges
,
376 IODirection withDirection
,
378 bool asReference
= false) = 0;
380 /*! @function initWithPhysicalRanges
381 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
382 @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.
383 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
384 @param withCount The member count of the ranges array.
385 @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.
386 @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.
387 @result true on success, false on failure. */
389 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
391 IODirection withDirection
,
392 bool asReference
= false) = 0;
394 /*! @function getDirection
395 @abstract Accessor to get the direction the memory descriptor was created with.
396 @discussion This method returns the direction the memory descriptor was created with.
397 @result The direction. */
399 virtual IODirection
getDirection() const;
401 /*! @function getLength
402 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
403 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
404 @result The byte count. */
406 virtual IOByteCount
getLength() const;
409 @abstract Set the tag for the memory descriptor.
410 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
411 @param tag The tag. */
413 virtual void setTag( IOOptionBits tag
);
416 @abstract Accessor to the retrieve the tag for the memory descriptor.
417 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
420 virtual IOOptionBits
getTag( void );
422 /*! @function readBytes
423 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
424 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
425 @param offset A byte offset into the memory descriptor's memory.
426 @param bytes The caller supplied buffer to copy the data to.
427 @param withLength The length of the data to copy.
428 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
430 virtual IOByteCount
readBytes(IOByteCount offset
,
431 void * bytes
, IOByteCount withLength
);
433 /*! @function writeBytes
434 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
435 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
436 @param offset A byte offset into the memory descriptor's memory.
437 @param bytes The caller supplied buffer to copy the data from.
438 @param withLength The length of the data to copy.
439 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
441 virtual IOByteCount
writeBytes(IOByteCount offset
,
442 const void * bytes
, IOByteCount withLength
);
444 /*! @function getPhysicalSegment
445 @abstract Break a memory descriptor into its physically contiguous segments.
446 @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.
447 @param offset A byte offset into the memory whose physical address to return.
448 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
449 @result A physical address, or zero if the offset is beyond the length of the memory. */
451 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
452 IOByteCount
* length
) = 0;
454 /*! @function getPhysicalAddress
455 @abstract Return the physical address of the first byte in the memory.
456 @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.
457 @result A physical address. */
459 /* inline */ IOPhysicalAddress
getPhysicalAddress();
460 /* { return( getPhysicalSegment( 0, 0 )); } */
462 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
463 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
464 /* DEPRECATED */ IOByteCount
* length
) = 0;
465 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
467 /*! @function prepare
468 @abstract Prepare the memory for an I/O transfer.
469 @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.
470 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
471 @result An IOReturn code. */
473 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
) = 0;
475 /*! @function complete
476 @abstract Complete processing of the memory after an I/O transfer finishes.
477 @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.
478 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
479 @result An IOReturn code. */
481 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
) = 0;
488 @abstract Maps a IOMemoryDescriptor into a task.
489 @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.
490 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
491 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
492 @param options Mapping options are defined in IOTypes.h,<br>
493 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>
494 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
495 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
496 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
497 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>
498 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>
499 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
500 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
501 @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. */
503 virtual IOMemoryMap
* map(
505 IOVirtualAddress atAddress
,
506 IOOptionBits options
,
507 IOByteCount offset
= 0,
508 IOByteCount length
= 0 );
511 @abstract Maps a IOMemoryDescriptor into the kernel map.
512 @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.
513 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
514 @result See the full version of the map method. */
516 virtual IOMemoryMap
* map(
517 IOOptionBits options
= 0 );
519 /*! @function setMapping
520 @abstract Establishes an already existing mapping.
521 @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.
522 @param task Address space in which the mapping exists.
523 @param mapAddress Virtual address of the mapping.
524 @param options Caching and read-only attributes of the mapping.
525 @result A IOMemoryMap object created to represent the mapping. */
527 virtual IOMemoryMap
* setMapping(
529 IOVirtualAddress mapAddress
,
530 IOOptionBits options
= 0 );
532 // Following methods are private implementation
535 IOReturn
redirect( task_t safeTask
, bool redirect
);
537 IOReturn
handleFault(
540 IOVirtualAddress address
,
541 IOByteCount sourceOffset
,
543 IOOptionBits options
);
546 virtual IOMemoryMap
* makeMapping(
547 IOMemoryDescriptor
* owner
,
549 IOVirtualAddress atAddress
,
550 IOOptionBits options
,
552 IOByteCount length
);
554 virtual void addMapping(
555 IOMemoryMap
* mapping
);
557 virtual void removeMapping(
558 IOMemoryMap
* mapping
);
560 virtual IOReturn
doMap(
562 IOVirtualAddress
* atAddress
,
563 IOOptionBits options
,
564 IOByteCount sourceOffset
= 0,
565 IOByteCount length
= 0 );
567 virtual IOReturn
doUnmap(
569 IOVirtualAddress logical
,
570 IOByteCount length
);
573 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
575 /*! @class IOMemoryMap : public OSObject
576 @abstract An abstract base class defining common methods for describing a memory mapping.
577 @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. */
579 class IOMemoryMap
: public OSObject
581 OSDeclareAbstractStructors(IOMemoryMap
)
584 /*! @function getVirtualAddress
585 @abstract Accessor to the virtual address of the first byte in the mapping.
586 @discussion This method returns the virtual address of the first byte in the mapping.
587 @result A virtual address. */
589 virtual IOVirtualAddress
getVirtualAddress() = 0;
591 /*! @function getPhysicalSegment
592 @abstract Break a mapping into its physically contiguous segments.
593 @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.
594 @param offset A byte offset into the mapping whose physical address to return.
595 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
596 @result A physical address, or zero if the offset is beyond the length of the mapping. */
598 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
599 IOByteCount
* length
) = 0;
601 /*! @function getPhysicalAddress
602 @abstract Return the physical address of the first byte in the mapping.
603 @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.
604 @result A physical address. */
606 /* inline */ IOPhysicalAddress
getPhysicalAddress();
607 /* { return( getPhysicalSegment( 0, 0 )); } */
609 /*! @function getLength
610 @abstract Accessor to the length of the mapping.
611 @discussion This method returns the length of the mapping.
612 @result A byte count. */
614 virtual IOByteCount
getLength() = 0;
616 /*! @function getAddressTask
617 @abstract Accessor to the task of the mapping.
618 @discussion This method returns the mach task the mapping exists in.
619 @result A mach task_t. */
621 virtual task_t
getAddressTask() = 0;
623 /*! @function getMemoryDescriptor
624 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
625 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
626 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
628 virtual IOMemoryDescriptor
* getMemoryDescriptor() = 0;
630 /*! @function getMapOptions
631 @abstract Accessor to the options the mapping was created with.
632 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
633 @result Options for the mapping, including cache settings. */
635 virtual IOOptionBits
getMapOptions() = 0;
638 @abstract Force the IOMemoryMap to unmap, without destroying the object.
639 @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.
640 @result An IOReturn code. */
642 virtual IOReturn
unmap() = 0;
644 virtual void taskDied() = 0;
646 /*! @function redirect
647 @abstract Replace the memory mapped in a process with new backing memory.
648 @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.
649 @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.
650 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
651 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
652 @result An IOReturn code. */
654 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
655 IOOptionBits options
,
656 IOByteCount offset
= 0) = 0;
659 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
661 // The following classes are private implementation of IOMemoryDescriptor - they
662 // should not be referenced directly, just through the public API's in the
663 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
664 // might be created by IOMemoryDescriptor::withAddress(), but there should be
665 // no need to reference as anything but a generic IOMemoryDescriptor *.
667 // Also these flags should not overlap with the options to
668 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
671 kIOMemoryPreparedReadOnly
= 0x00008000,
674 class IOGeneralMemoryDescriptor
: public IOMemoryDescriptor
676 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor
);
686 unsigned _rangesCount
; /* number of address ranges in list */
687 bool _rangesIsAllocated
; /* is list allocated by us? */
689 task_t _task
; /* task where all ranges are mapped to */
694 } _singleRange
; /* storage space for a single range */
696 unsigned _wireCount
; /* number of outstanding wires */
698 /* DEPRECATED */ vm_address_t _cachedVirtualAddress
; /* a cached virtual-to-physical */
700 /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress
;
702 bool _initialized
; /* has superclass been initialized? */
708 // Internal APIs may be made virtual at some time in the future.
709 IOReturn
wireVirtual(IODirection forDirection
);
710 void *createNamedEntry();
713 /* DEPRECATED */ IOByteCount _position
; /* absolute position over all ranges */
714 /* DEPRECATED */ virtual void setPosition(IOByteCount position
);
717 * DEPRECATED IOByteCount _positionAtIndex; // relative position within range #n
719 * Re-use the _positionAtIndex as a count of the number of pages in
720 * this memory descriptor. Convieniently vm_address_t is an unsigned integer
721 * type so I can get away without having to change the type.
725 /* DEPRECATED */ unsigned _positionAtOffset
; //range #n in which position is now
727 OSData
*_memoryEntries
;
729 /* DEPRECATED */ vm_offset_t _kernPtrAligned
;
730 /* DEPRECATED */ unsigned _kernPtrAtIndex
;
731 /* DEPRECATED */ IOByteCount _kernSize
;
733 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex
);
734 /* DEPRECATED */ virtual void unmapFromKernel();
738 * IOMemoryDescriptor required methods
742 virtual bool initWithOptions(void * buffers
,
746 IOOptionBits options
,
747 IOMapper
* mapper
= 0);
749 // Secondary initialisers
750 virtual bool initWithAddress(void * address
,
751 IOByteCount withLength
,
752 IODirection withDirection
);
754 virtual bool initWithAddress(vm_address_t address
,
755 IOByteCount withLength
,
756 IODirection withDirection
,
759 virtual bool initWithPhysicalAddress(
760 IOPhysicalAddress address
,
761 IOByteCount withLength
,
762 IODirection withDirection
);
764 virtual bool initWithRanges( IOVirtualRange
* ranges
,
766 IODirection withDirection
,
768 bool asReference
= false);
770 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
772 IODirection withDirection
,
773 bool asReference
= false);
775 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
776 IOByteCount
* length
);
778 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
779 IOByteCount
* length
);
781 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
782 /* DEPRECATED */ IOByteCount
* length
);
784 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
786 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
788 virtual IOReturn
doMap(
790 IOVirtualAddress
* atAddress
,
791 IOOptionBits options
,
792 IOByteCount sourceOffset
= 0,
793 IOByteCount length
= 0 );
795 virtual IOReturn
doUnmap(
797 IOVirtualAddress logical
,
798 IOByteCount length
);
799 virtual bool serialize(OSSerialize
*s
) const;
801 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
802 static IOMemoryDescriptor
*
803 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
);
806 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
808 class IOSubMemoryDescriptor
: public IOMemoryDescriptor
810 friend class IOMemoryDescriptor
;
812 OSDeclareDefaultStructors(IOSubMemoryDescriptor
);
815 IOMemoryDescriptor
* _parent
;
820 virtual bool initWithAddress(void * address
,
821 IOByteCount withLength
,
822 IODirection withDirection
);
824 virtual bool initWithAddress(vm_address_t address
,
825 IOByteCount withLength
,
826 IODirection withDirection
,
829 virtual bool initWithPhysicalAddress(
830 IOPhysicalAddress address
,
831 IOByteCount withLength
,
832 IODirection withDirection
);
834 virtual bool initWithRanges( IOVirtualRange
* ranges
,
836 IODirection withDirection
,
838 bool asReference
= false);
840 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
842 IODirection withDirection
,
843 bool asReference
= false);
845 IOMemoryDescriptor::withAddress
;
846 IOMemoryDescriptor::withPhysicalAddress
;
847 IOMemoryDescriptor::withPhysicalRanges
;
848 IOMemoryDescriptor::withRanges
;
849 IOMemoryDescriptor::withSubRange
;
853 * Initialize or reinitialize an IOSubMemoryDescriptor to describe
854 * a subrange of an existing descriptor.
856 * An IOSubMemoryDescriptor can be re-used by calling initSubRange
857 * again on an existing instance -- note that this behavior is not
858 * commonly supported in other IOKit classes, although it is here.
860 virtual bool initSubRange( IOMemoryDescriptor
* parent
,
861 IOByteCount offset
, IOByteCount length
,
862 IODirection withDirection
);
865 * IOMemoryDescriptor required methods
868 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
869 IOByteCount
* length
);
871 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
872 IOByteCount
* length
);
874 virtual IOByteCount
readBytes(IOByteCount offset
,
875 void * bytes
, IOByteCount withLength
);
877 virtual IOByteCount
writeBytes(IOByteCount offset
,
878 const void * bytes
, IOByteCount withLength
);
880 virtual void * getVirtualSegment(IOByteCount offset
,
881 IOByteCount
* length
);
883 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
885 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
888 IOReturn
redirect( task_t safeTask
, bool redirect
);
890 virtual bool serialize(OSSerialize
*s
) const;
892 virtual IOReturn
setPurgeable( IOOptionBits newState
,
893 IOOptionBits
* oldState
);
894 virtual IOReturn
performOperation( IOOptionBits options
,
895 IOByteCount offset
, IOByteCount length
);
898 virtual IOMemoryMap
* makeMapping(
899 IOMemoryDescriptor
* owner
,
901 IOVirtualAddress atAddress
,
902 IOOptionBits options
,
904 IOByteCount length
);
906 virtual IOReturn
doMap(
908 IOVirtualAddress
* atAddress
,
909 IOOptionBits options
,
910 IOByteCount sourceOffset
= 0,
911 IOByteCount length
= 0 );
914 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
916 #endif /* !_IOMEMORYDESCRIPTOR_H */