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 kIOMemoryTypeVirtual64
= 0x00000060,
68 kIOMemoryTypePhysical64
= 0x00000070,
69 kIOMemoryTypeMask
= 0x000000f0,
71 kIOMemoryAsReference
= 0x00000100,
72 kIOMemoryBufferPageable
= 0x00000400,
73 kIOMemoryDontMap
= 0x00000800,
74 kIOMemoryPersistent
= 0x00010000
77 #define kIOMapperNone ((IOMapper *) -1)
78 #define kIOMapperSystem ((IOMapper *) 0)
82 kIOMemoryPurgeableKeepCurrent
= 1,
83 kIOMemoryPurgeableNonVolatile
= 2,
84 kIOMemoryPurgeableVolatile
= 3,
85 kIOMemoryPurgeableEmpty
= 4
89 kIOMemoryIncoherentIOFlush
= 1,
90 kIOMemoryIncoherentIOStore
= 2,
93 #define IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND 1
96 /*! @class IOMemoryDescriptor : public OSObject
97 @abstract An abstract base class defining common methods for describing physical or virtual memory.
98 @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. */
100 class IOMemoryDescriptor
: public OSObject
102 friend class _IOMemoryMap
;
103 friend class IOSubMemoryDescriptor
;
105 OSDeclareDefaultStructors(IOMemoryDescriptor
);
108 /*! @struct ExpansionData
109 @discussion This structure will be used to expand the capablilties of this class in the future.
111 struct ExpansionData
{
113 unsigned int pagerContig
:1;
114 unsigned int unused
:31;
115 IOMemoryDescriptor
* memory
;
119 Reserved for future use. (Internal use only) */
120 ExpansionData
* reserved
;
127 IODirection _direction
; /* DEPRECATED: use _flags instead. direction of transfer */
128 IOByteCount _length
; /* length of all ranges */
132 typedef IOOptionBits DMACommandOps
;
133 virtual IOPhysicalAddress
getSourceSegment( IOByteCount offset
,
134 IOByteCount
* length
);
135 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 0);
137 /*! @function initWithOptions
138 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
139 @discussion Note this function can be used to re-init a previously created memory descriptor.
140 @result true on success, false on failure. */
141 virtual bool initWithOptions(void * buffers
,
145 IOOptionBits options
,
146 IOMapper
* mapper
= kIOMapperSystem
);
147 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 1);
149 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
150 IOByteCount
* length
);
151 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 2);
154 /*! @function setPurgeable
155 @abstract Control the purgeable status of a memory descriptors memory.
156 @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.
157 @param newState - the desired new purgeable state of the memory:<br>
158 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
159 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
160 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
161 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
162 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
163 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
164 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
165 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
166 @result An IOReturn code. */
168 virtual IOReturn
setPurgeable( IOOptionBits newState
,
169 IOOptionBits
* oldState
);
170 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 3);
172 /*! @function performOperation
173 @abstract Perform an operation on the memory descriptor's memory.
174 @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.
175 @param options The operation to perform on the memory:<br>
176 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>
177 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.
178 @param offset A byte offset into the memory descriptor's memory.
179 @param length The length of the data range.
180 @result An IOReturn code. */
182 virtual IOReturn
performOperation( IOOptionBits options
,
183 IOByteCount offset
, IOByteCount length
);
184 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 4);
186 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
187 // Used for dedicated communications for IODMACommand
188 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
189 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 5);
193 #if (defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
194 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 5);
196 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 6);
197 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 7);
198 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 8);
199 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 9);
200 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 10);
201 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 11);
202 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 12);
203 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 13);
204 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 14);
205 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 15);
210 static void initialize( void );
213 /*! @function withAddress
214 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
215 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel 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 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
221 static IOMemoryDescriptor
* withAddress(void * address
,
222 IOByteCount withLength
,
223 IODirection withDirection
);
225 /*! @function withAddress
226 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
227 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
228 @param address The virtual address of the first byte in the memory.
229 @param withLength The length of memory.
230 @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.
231 @param withTask The task the virtual ranges are mapped into.
232 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
234 static IOMemoryDescriptor
* withAddress(vm_address_t address
,
235 IOByteCount withLength
,
236 IODirection withDirection
,
239 /*! @function withPhysicalAddress
240 @abstract Create an IOMemoryDescriptor to describe one physical range.
241 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
242 @param address The physical address of the first byte in the memory.
243 @param withLength The length of memory.
244 @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.
245 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
247 static IOMemoryDescriptor
* withPhysicalAddress(
248 IOPhysicalAddress address
,
249 IOByteCount withLength
,
250 IODirection withDirection
);
252 /*! @function withRanges
253 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
254 @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.
255 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
256 @param withCount The member count of the ranges array.
257 @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.
258 @param withTask The task each of the virtual ranges are mapped into.
259 @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.
260 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
262 static IOMemoryDescriptor
* withRanges(IOVirtualRange
* ranges
,
264 IODirection withDirection
,
266 bool asReference
= false);
268 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
269 /*! @function withAddressRange
270 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
271 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
272 @param address The virtual address of the first byte in the memory.
273 @param withLength The length of memory.
275 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.
276 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
277 @param task The task the virtual ranges are mapped into.
278 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
280 static IOMemoryDescriptor
* withAddressRange(
281 mach_vm_address_t address
,
282 mach_vm_size_t length
,
283 IOOptionBits options
,
286 /*! @function withAddressRanges
287 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
288 @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.
289 @param ranges An array of IOAddressRange structures which specify the virtual ranges in the specified map which make up the memory to be described. IOAddressRange is the 64bit version of IOVirtualRange.
290 @param rangeCount The member count of the ranges array.
292 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.
293 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.
294 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
295 @param task The task each of the virtual ranges are mapped into.
296 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
298 static IOMemoryDescriptor
* withAddressRanges(
299 IOAddressRange
* ranges
,
301 IOOptionBits options
,
305 /*! @function withOptions
306 @abstract Master initialiser for all variants of memory descriptors.
307 @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.
310 @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.
312 @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.
314 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
316 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
319 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.
320 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.
321 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.
322 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.
323 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
325 @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.
327 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
329 static IOMemoryDescriptor
*withOptions(void * buffers
,
333 IOOptionBits options
,
334 IOMapper
* mapper
= kIOMapperSystem
);
336 /*! @function withPhysicalRanges
337 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
338 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
339 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
340 @param withCount The member count of the ranges array.
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 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.
343 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
345 static IOMemoryDescriptor
* withPhysicalRanges(
346 IOPhysicalRange
* ranges
,
348 IODirection withDirection
,
349 bool asReference
= false);
351 /*! @function withSubRange
352 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
353 @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.
354 @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.
355 @param offset A byte offset into the parent memory descriptor's memory.
356 @param length The length of the subrange.
357 @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.
358 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
360 static IOMemoryDescriptor
* withSubRange(IOMemoryDescriptor
*of
,
363 IODirection withDirection
);
365 /*! @function withPersistentMemoryDescriptor
366 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
367 @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.
368 @param originalMD The memory descriptor to be duplicated.
369 @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. */
370 static IOMemoryDescriptor
*
371 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
);
373 /*! @function initWithAddress
374 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
375 @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.
376 @param address The virtual address of the first byte in the memory.
377 @param withLength The length of memory.
378 @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.
379 @result true on success, false on failure. */
381 virtual bool initWithAddress(void * address
,
382 IOByteCount withLength
,
383 IODirection withDirection
) = 0;
385 /*! @function initWithAddress
386 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
387 @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.
388 @param address The virtual address of the first byte in the memory.
389 @param withLength The length of memory.
390 @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.
391 @param withTask The task the virtual ranges are mapped into.
392 @result true on success, false on failure. */
394 virtual bool initWithAddress(vm_address_t address
,
395 IOByteCount withLength
,
396 IODirection withDirection
,
397 task_t withTask
) = 0;
399 /*! @function initWithPhysicalAddress
400 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
401 @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.
402 @param address The physical address of the first byte in the memory.
403 @param withLength The length of memory.
404 @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.
405 @result true on success, false on failure. */
407 virtual bool initWithPhysicalAddress(
408 IOPhysicalAddress address
,
409 IOByteCount withLength
,
410 IODirection withDirection
) = 0;
412 /*! @function initWithRanges
413 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
414 @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.
415 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
416 @param withCount The member count of the ranges array.
417 @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.
418 @param withTask The task each of the virtual ranges are mapped into.
419 @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.
420 @result true on success, false on failure. */
422 virtual bool initWithRanges(IOVirtualRange
* ranges
,
424 IODirection withDirection
,
426 bool asReference
= false) = 0;
428 /*! @function initWithPhysicalRanges
429 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
430 @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.
431 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
432 @param withCount The member count of the ranges array.
433 @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.
434 @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.
435 @result true on success, false on failure. */
437 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
439 IODirection withDirection
,
440 bool asReference
= false) = 0;
442 /*! @function getDirection
443 @abstract Accessor to get the direction the memory descriptor was created with.
444 @discussion This method returns the direction the memory descriptor was created with.
445 @result The direction. */
447 virtual IODirection
getDirection() const;
449 /*! @function getLength
450 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
451 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
452 @result The byte count. */
454 virtual IOByteCount
getLength() const;
457 @abstract Set the tag for the memory descriptor.
458 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
459 @param tag The tag. */
461 virtual void setTag( IOOptionBits tag
);
464 @abstract Accessor to the retrieve the tag for the memory descriptor.
465 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
468 virtual IOOptionBits
getTag( void );
470 /*! @function readBytes
471 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
472 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
473 @param offset A byte offset into the memory descriptor's memory.
474 @param bytes The caller supplied buffer to copy the data to.
475 @param withLength The length of the data to copy.
476 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
478 virtual IOByteCount
readBytes(IOByteCount offset
,
479 void * bytes
, IOByteCount withLength
);
481 /*! @function writeBytes
482 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
483 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
484 @param offset A byte offset into the memory descriptor's memory.
485 @param bytes The caller supplied buffer to copy the data from.
486 @param withLength The length of the data to copy.
487 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
489 virtual IOByteCount
writeBytes(IOByteCount offset
,
490 const void * bytes
, IOByteCount withLength
);
492 /*! @function getPhysicalSegment
493 @abstract Break a memory descriptor into its physically contiguous segments.
494 @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.
495 @param offset A byte offset into the memory whose physical address to return.
496 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
497 @result A physical address, or zero if the offset is beyond the length of the memory. */
499 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
500 IOByteCount
* length
) = 0;
502 /*! @function getPhysicalAddress
503 @abstract Return the physical address of the first byte in the memory.
504 @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.
505 @result A physical address. */
507 /* inline */ IOPhysicalAddress
getPhysicalAddress();
508 /* { return( getPhysicalSegment( 0, 0 )); } */
510 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
511 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
512 /* DEPRECATED */ IOByteCount
* length
) = 0;
513 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
515 /*! @function prepare
516 @abstract Prepare the memory for an I/O transfer.
517 @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.
518 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
519 @result An IOReturn code. */
521 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
) = 0;
523 /*! @function complete
524 @abstract Complete processing of the memory after an I/O transfer finishes.
525 @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.
526 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
527 @result An IOReturn code. */
529 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
) = 0;
536 @abstract Maps a IOMemoryDescriptor into a task.
537 @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.
538 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
539 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
540 @param options Mapping options are defined in IOTypes.h,<br>
541 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>
542 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
543 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
544 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
545 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>
546 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>
547 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
548 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
549 @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. */
551 virtual IOMemoryMap
* map(
553 IOVirtualAddress atAddress
,
554 IOOptionBits options
,
555 IOByteCount offset
= 0,
556 IOByteCount length
= 0 );
559 @abstract Maps a IOMemoryDescriptor into the kernel map.
560 @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.
561 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
562 @result See the full version of the map method. */
564 virtual IOMemoryMap
* map(
565 IOOptionBits options
= 0 );
567 /*! @function setMapping
568 @abstract Establishes an already existing mapping.
569 @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.
570 @param task Address space in which the mapping exists.
571 @param mapAddress Virtual address of the mapping.
572 @param options Caching and read-only attributes of the mapping.
573 @result A IOMemoryMap object created to represent the mapping. */
575 virtual IOMemoryMap
* setMapping(
577 IOVirtualAddress mapAddress
,
578 IOOptionBits options
= 0 );
580 // Following methods are private implementation
583 IOReturn
redirect( task_t safeTask
, bool redirect
);
585 IOReturn
handleFault(
588 IOVirtualAddress address
,
589 IOByteCount sourceOffset
,
591 IOOptionBits options
);
594 virtual IOMemoryMap
* makeMapping(
595 IOMemoryDescriptor
* owner
,
597 IOVirtualAddress atAddress
,
598 IOOptionBits options
,
600 IOByteCount length
);
602 virtual void addMapping(
603 IOMemoryMap
* mapping
);
605 virtual void removeMapping(
606 IOMemoryMap
* mapping
);
608 virtual IOReturn
doMap(
610 IOVirtualAddress
* atAddress
,
611 IOOptionBits options
,
612 IOByteCount sourceOffset
= 0,
613 IOByteCount length
= 0 );
615 virtual IOReturn
doUnmap(
617 IOVirtualAddress logical
,
618 IOByteCount length
);
621 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
623 /*! @class IOMemoryMap : public OSObject
624 @abstract An abstract base class defining common methods for describing a memory mapping.
625 @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. */
627 class IOMemoryMap
: public OSObject
629 OSDeclareAbstractStructors(IOMemoryMap
)
632 /*! @function getVirtualAddress
633 @abstract Accessor to the virtual address of the first byte in the mapping.
634 @discussion This method returns the virtual address of the first byte in the mapping.
635 @result A virtual address. */
637 virtual IOVirtualAddress
getVirtualAddress() = 0;
639 /*! @function getPhysicalSegment
640 @abstract Break a mapping into its physically contiguous segments.
641 @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.
642 @param offset A byte offset into the mapping whose physical address to return.
643 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
644 @result A physical address, or zero if the offset is beyond the length of the mapping. */
646 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
647 IOByteCount
* length
) = 0;
649 /*! @function getPhysicalAddress
650 @abstract Return the physical address of the first byte in the mapping.
651 @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.
652 @result A physical address. */
654 /* inline */ IOPhysicalAddress
getPhysicalAddress();
655 /* { return( getPhysicalSegment( 0, 0 )); } */
657 /*! @function getLength
658 @abstract Accessor to the length of the mapping.
659 @discussion This method returns the length of the mapping.
660 @result A byte count. */
662 virtual IOByteCount
getLength() = 0;
664 /*! @function getAddressTask
665 @abstract Accessor to the task of the mapping.
666 @discussion This method returns the mach task the mapping exists in.
667 @result A mach task_t. */
669 virtual task_t
getAddressTask() = 0;
671 /*! @function getMemoryDescriptor
672 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
673 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
674 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
676 virtual IOMemoryDescriptor
* getMemoryDescriptor() = 0;
678 /*! @function getMapOptions
679 @abstract Accessor to the options the mapping was created with.
680 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
681 @result Options for the mapping, including cache settings. */
683 virtual IOOptionBits
getMapOptions() = 0;
686 @abstract Force the IOMemoryMap to unmap, without destroying the object.
687 @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.
688 @result An IOReturn code. */
690 virtual IOReturn
unmap() = 0;
692 virtual void taskDied() = 0;
694 /*! @function redirect
695 @abstract Replace the memory mapped in a process with new backing memory.
696 @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.
697 @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.
698 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
699 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
700 @result An IOReturn code. */
702 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
703 IOOptionBits options
,
704 IOByteCount offset
= 0) = 0;
707 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
709 // The following classes are private implementation of IOMemoryDescriptor - they
710 // should not be referenced directly, just through the public API's in the
711 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
712 // might be created by IOMemoryDescriptor::withAddress(), but there should be
713 // no need to reference as anything but a generic IOMemoryDescriptor *.
715 // Also these flags should not overlap with the options to
716 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
719 kIOMemoryPreparedReadOnly
= 0x00008000,
722 class IOGeneralMemoryDescriptor
: public IOMemoryDescriptor
724 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor
);
735 unsigned _rangesCount
; /* number of address ranges in list */
736 bool _rangesIsAllocated
; /* is list allocated by us? */
738 task_t _task
; /* task where all ranges are mapped to */
743 } _singleRange
; /* storage space for a single range */
745 unsigned _wireCount
; /* number of outstanding wires */
747 /* DEPRECATED */ vm_address_t _cachedVirtualAddress
; /* a cached virtual-to-physical */
749 /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress
;
751 bool _initialized
; /* has superclass been initialized? */
755 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
756 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
761 /* DEPRECATED */ virtual void setPosition(IOByteCount position
);
762 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex
);
763 /* DEPRECATED */ virtual void unmapFromKernel();
765 // Internal APIs may be made virtual at some time in the future.
766 IOReturn
wireVirtual(IODirection forDirection
);
767 void *createNamedEntry();
770 OSData
* _memoryEntries
;
772 ppnum_t _highestPage
;
773 uint32_t __iomd_reservedA
;
774 uint32_t __iomd_reservedB
;
775 uint32_t __iomd_reservedC
;
779 * IOMemoryDescriptor required methods
783 virtual bool initWithOptions(void * buffers
,
787 IOOptionBits options
,
788 IOMapper
* mapper
= kIOMapperSystem
);
790 // Secondary initialisers
791 virtual bool initWithAddress(void * address
,
792 IOByteCount withLength
,
793 IODirection withDirection
);
795 virtual bool initWithAddress(vm_address_t address
,
796 IOByteCount withLength
,
797 IODirection withDirection
,
800 virtual bool initWithPhysicalAddress(
801 IOPhysicalAddress address
,
802 IOByteCount withLength
,
803 IODirection withDirection
);
805 virtual bool initWithRanges( IOVirtualRange
* ranges
,
807 IODirection withDirection
,
809 bool asReference
= false);
811 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
813 IODirection withDirection
,
814 bool asReference
= false);
816 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
817 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
818 IOByteCount
* length
);
821 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
822 IOByteCount
* length
);
824 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
825 IOByteCount
* length
);
827 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
828 /* DEPRECATED */ IOByteCount
* length
);
830 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
832 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
834 virtual IOReturn
doMap(
836 IOVirtualAddress
* atAddress
,
837 IOOptionBits options
,
838 IOByteCount sourceOffset
= 0,
839 IOByteCount length
= 0 );
841 virtual IOReturn
doUnmap(
843 IOVirtualAddress logical
,
844 IOByteCount length
);
845 virtual bool serialize(OSSerialize
*s
) const;
847 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
848 static IOMemoryDescriptor
*
849 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
);
852 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
854 class IOSubMemoryDescriptor
: public IOMemoryDescriptor
856 friend class IOMemoryDescriptor
;
858 OSDeclareDefaultStructors(IOSubMemoryDescriptor
);
861 IOMemoryDescriptor
* _parent
;
866 virtual bool initWithAddress(void * address
,
867 IOByteCount withLength
,
868 IODirection withDirection
);
870 virtual bool initWithAddress(vm_address_t address
,
871 IOByteCount withLength
,
872 IODirection withDirection
,
875 virtual bool initWithPhysicalAddress(
876 IOPhysicalAddress address
,
877 IOByteCount withLength
,
878 IODirection withDirection
);
880 virtual bool initWithRanges( IOVirtualRange
* ranges
,
882 IODirection withDirection
,
884 bool asReference
= false);
886 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
888 IODirection withDirection
,
889 bool asReference
= false);
891 IOMemoryDescriptor::withAddress
;
892 IOMemoryDescriptor::withPhysicalAddress
;
893 IOMemoryDescriptor::withPhysicalRanges
;
894 IOMemoryDescriptor::withRanges
;
895 IOMemoryDescriptor::withSubRange
;
897 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
898 // used by IODMACommand
899 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
904 * Initialize or reinitialize an IOSubMemoryDescriptor to describe
905 * a subrange of an existing descriptor.
907 * An IOSubMemoryDescriptor can be re-used by calling initSubRange
908 * again on an existing instance -- note that this behavior is not
909 * commonly supported in other IOKit classes, although it is here.
911 virtual bool initSubRange( IOMemoryDescriptor
* parent
,
912 IOByteCount offset
, IOByteCount length
,
913 IODirection withDirection
);
916 * IOMemoryDescriptor required methods
919 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
920 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
921 IOByteCount
* length
);
924 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
925 IOByteCount
* length
);
927 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
928 IOByteCount
* length
);
930 virtual IOByteCount
readBytes(IOByteCount offset
,
931 void * bytes
, IOByteCount withLength
);
933 virtual IOByteCount
writeBytes(IOByteCount offset
,
934 const void * bytes
, IOByteCount withLength
);
936 virtual void * getVirtualSegment(IOByteCount offset
,
937 IOByteCount
* length
);
939 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
941 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
944 IOReturn
redirect( task_t safeTask
, bool redirect
);
946 virtual bool serialize(OSSerialize
*s
) const;
948 virtual IOReturn
setPurgeable( IOOptionBits newState
,
949 IOOptionBits
* oldState
);
950 virtual IOReturn
performOperation( IOOptionBits options
,
951 IOByteCount offset
, IOByteCount length
);
954 virtual IOMemoryMap
* makeMapping(
955 IOMemoryDescriptor
* owner
,
957 IOVirtualAddress atAddress
,
958 IOOptionBits options
,
960 IOByteCount length
);
962 virtual IOReturn
doMap(
964 IOVirtualAddress
* atAddress
,
965 IOOptionBits options
,
966 IOByteCount sourceOffset
= 0,
967 IOByteCount length
= 0 );
970 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
972 #endif /* !_IOMEMORYDESCRIPTOR_H */