2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #ifndef _IOMEMORYDESCRIPTOR_H
29 #define _IOMEMORYDESCRIPTOR_H
31 #include <sys/cdefs.h>
33 #include <IOKit/IOTypes.h>
34 #include <IOKit/IOLocks.h>
35 #include <libkern/c++/OSContainers.h>
37 #include <mach/memory_object_types.h>
43 * Direction of transfer, with respect to the described memory.
49 #endif /* !__LP64__ */
51 kIODirectionNone
= 0x0, // same as VM_PROT_NONE
52 kIODirectionIn
= 0x1, // User land 'read', same as VM_PROT_READ
53 kIODirectionOut
= 0x2, // User land 'write', same as VM_PROT_WRITE
54 kIODirectionOutIn
= kIODirectionOut
| kIODirectionIn
,
55 kIODirectionInOut
= kIODirectionIn
| kIODirectionOut
58 typedef IOOptionBits IODirection
;
62 * IOOptionBits used in the withOptions variant
65 kIOMemoryDirectionMask
= 0x00000007,
66 #ifdef XNU_KERNEL_PRIVATE
67 kIOMemoryAutoPrepare
= 0x00000008, // Shared with Buffer MD
70 kIOMemoryTypeVirtual
= 0x00000010,
71 kIOMemoryTypePhysical
= 0x00000020,
72 kIOMemoryTypeUPL
= 0x00000030,
73 kIOMemoryTypePersistentMD
= 0x00000040, // Persistent Memory Descriptor
74 kIOMemoryTypeUIO
= 0x00000050,
76 kIOMemoryTypeVirtual64
= kIOMemoryTypeVirtual
,
77 kIOMemoryTypePhysical64
= kIOMemoryTypePhysical
,
79 kIOMemoryTypeVirtual64
= 0x00000060,
80 kIOMemoryTypePhysical64
= 0x00000070,
81 #endif /* !__LP64__ */
82 kIOMemoryTypeMask
= 0x000000f0,
84 kIOMemoryAsReference
= 0x00000100,
85 kIOMemoryBufferPageable
= 0x00000400,
86 kIOMemoryMapperNone
= 0x00000800,
87 #ifdef XNU_KERNEL_PRIVATE
88 kIOMemoryRedirected
= 0x00004000,
89 kIOMemoryPreparedReadOnly
= 0x00008000,
91 kIOMemoryPersistent
= 0x00010000,
92 #ifdef XNU_KERNEL_PRIVATE
93 kIOMemoryReserved6156215
= 0x00020000,
95 kIOMemoryThreadSafe
= 0x00100000, // Shared with Buffer MD
98 #define kIOMapperSystem ((IOMapper *) 0)
102 kIOMemoryPurgeableKeepCurrent
= 1,
103 kIOMemoryPurgeableNonVolatile
= 2,
104 kIOMemoryPurgeableVolatile
= 3,
105 kIOMemoryPurgeableEmpty
= 4
109 kIOMemoryIncoherentIOFlush
= 1,
110 kIOMemoryIncoherentIOStore
= 2,
113 #define IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND 1
117 kIOPreparationIDUnprepared
= 0,
118 kIOPreparationIDUnsupported
= 1,
121 /*! @class IOMemoryDescriptor : public OSObject
122 @abstract An abstract base class defining common methods for describing physical or virtual memory.
123 @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. */
125 class IOMemoryDescriptor
: public OSObject
127 friend class IOMemoryMap
;
129 OSDeclareDefaultStructors(IOMemoryDescriptor
);
132 /*! @struct ExpansionData
133 @discussion This structure will be used to expand the capablilties of this class in the future.
135 struct ExpansionData
{
137 unsigned int pagerContig
:1;
138 unsigned int unused
:31;
139 IOMemoryDescriptor
* memory
;
143 Reserved for future use. (Internal use only) */
144 ExpansionData
* reserved
;
152 uint64_t __iomd_reserved1
;
153 uint64_t __iomd_reserved2
;
154 uint64_t __iomd_reserved3
;
155 uint64_t __iomd_reserved4
;
156 #else /* !__LP64__ */
157 IODirection _direction
; /* use _flags instead */
158 #endif /* !__LP64__ */
159 IOByteCount _length
; /* length of all ranges */
163 typedef IOOptionBits DMACommandOps
;
165 virtual IOPhysicalAddress
getSourceSegment( IOByteCount offset
,
166 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
167 #endif /* !__LP64__ */
169 /*! @function initWithOptions
170 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
171 @discussion Note this function can be used to re-init a previously created memory descriptor.
172 @result true on success, false on failure. */
173 virtual bool initWithOptions(void * buffers
,
177 IOOptionBits options
,
178 IOMapper
* mapper
= kIOMapperSystem
);
181 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
182 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
; /* use getPhysicalSegment() and kIOMemoryMapperNone instead */
183 #endif /* !__LP64__ */
185 /*! @function setPurgeable
186 @abstract Control the purgeable status of a memory descriptors memory.
187 @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.
188 @param newState - the desired new purgeable state of the memory:<br>
189 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
190 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
191 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
192 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
193 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
194 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
195 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
196 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
197 @result An IOReturn code. */
199 virtual IOReturn
setPurgeable( IOOptionBits newState
,
200 IOOptionBits
* oldState
);
202 /*! @function performOperation
203 @abstract Perform an operation on the memory descriptor's memory.
204 @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.
205 @param options The operation to perform on the memory:<br>
206 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>
207 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.
208 @param offset A byte offset into the memory descriptor's memory.
209 @param length The length of the data range.
210 @result An IOReturn code. */
212 virtual IOReturn
performOperation( IOOptionBits options
,
213 IOByteCount offset
, IOByteCount length
);
215 // Used for dedicated communications for IODMACommand
216 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
218 /*! @function getPhysicalSegment
219 @abstract Break a memory descriptor into its physically contiguous segments.
220 @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.
221 @param offset A byte offset into the memory whose physical address to return.
222 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
223 @result A physical address, or zero if the offset is beyond the length of the memory. */
226 virtual addr64_t
getPhysicalSegment( IOByteCount offset
,
227 IOByteCount
* length
,
228 IOOptionBits options
= 0 ) = 0;
229 #else /* !__LP64__ */
230 virtual addr64_t
getPhysicalSegment( IOByteCount offset
,
231 IOByteCount
* length
,
232 IOOptionBits options
);
233 #endif /* !__LP64__ */
235 virtual uint64_t getPreparationID( void );
238 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 0);
240 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 1);
241 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 2);
242 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 3);
243 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 4);
244 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 5);
245 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 6);
246 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 7);
247 #else /* !__LP64__ */
248 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 1);
249 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 2);
250 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 3);
251 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 4);
252 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 5);
253 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 6);
254 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 7);
255 #endif /* !__LP64__ */
256 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 8);
257 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 9);
258 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 10);
259 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 11);
260 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 12);
261 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 13);
262 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 14);
263 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 15);
268 static void initialize( void );
271 /*! @function withAddress
272 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
273 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map. This memory descriptor needs to be prepared before it can be used to extract data from the memory described.
274 @param address The virtual address of the first byte in the memory.
275 @param withLength The length of memory.
276 @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.
277 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
279 static IOMemoryDescriptor
* withAddress(void * address
,
280 IOByteCount withLength
,
281 IODirection withDirection
);
284 static IOMemoryDescriptor
* withAddress(IOVirtualAddress address
,
285 IOByteCount withLength
,
286 IODirection withDirection
,
287 task_t withTask
) APPLE_KEXT_DEPRECATED
; /* use withAddressRange() and prepare() instead */
288 #endif /* !__LP64__ */
290 /*! @function withPhysicalAddress
291 @abstract Create an IOMemoryDescriptor to describe one physical range.
292 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
293 @param address The physical address of the first byte in the memory.
294 @param withLength The length of memory.
295 @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.
296 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
298 static IOMemoryDescriptor
* withPhysicalAddress(
299 IOPhysicalAddress address
,
300 IOByteCount withLength
,
301 IODirection withDirection
);
304 static IOMemoryDescriptor
* withRanges(IOVirtualRange
* ranges
,
306 IODirection withDirection
,
308 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use withAddressRanges() instead */
309 #endif /* !__LP64__ */
311 /*! @function withAddressRange
312 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
313 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map. This memory descriptor needs to be prepared before it can be used to extract data from the memory described.
314 @param address The virtual address of the first byte in the memory.
315 @param withLength The length of memory.
317 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.
318 @param task The task the virtual ranges are mapped into. Note that unlike IOMemoryDescriptor::withAddress(), kernel_task memory must be explicitly prepared when passed to this api.
319 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
321 static IOMemoryDescriptor
* withAddressRange(
322 mach_vm_address_t address
,
323 mach_vm_size_t length
,
324 IOOptionBits options
,
327 /*! @function withAddressRanges
328 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
329 @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. This memory descriptor needs to be prepared before it can be used to extract data from the memory described.
330 @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.
331 @param rangeCount The member count of the ranges array.
333 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.
334 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.
335 @param task The task each of the virtual ranges are mapped into. Note that unlike IOMemoryDescriptor::withAddress(), kernel_task memory must be explicitly prepared when passed to this api.
336 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
338 static IOMemoryDescriptor
* withAddressRanges(
339 IOAddressRange
* ranges
,
341 IOOptionBits options
,
344 /*! @function withOptions
345 @abstract Master initialiser for all variants of memory descriptors.
346 @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.
349 @param buffers A pointer to an array of IOAddressRange when options:type is kIOMemoryTypeVirtual64 or kIOMemoryTypePhysical64 or a 64bit kernel. For type UPL it is a upl_t returned by the mach/memory_object_types.h apis, primarily used internally by the UBC. IOVirtualRanges or IOPhysicalRanges are 32 bit only types for use when options:type is kIOMemoryTypeVirtual or kIOMemoryTypePhysical on 32bit kernels.
351 @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.
353 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
355 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
358 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.
359 kIOMemoryTypeMask (options:type) kIOMemoryTypeVirtual64, kIOMemoryTypeVirtual, kIOMemoryTypePhysical64, 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.
360 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.
361 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.
363 @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.
365 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
367 static IOMemoryDescriptor
*withOptions(void * buffers
,
371 IOOptionBits options
,
372 IOMapper
* mapper
= kIOMapperSystem
);
375 static IOMemoryDescriptor
* withPhysicalRanges(
376 IOPhysicalRange
* ranges
,
378 IODirection withDirection
,
379 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use withOptions() and kIOMemoryTypePhysical instead */
380 #endif /* !__LP64__ */
383 static IOMemoryDescriptor
* withSubRange(IOMemoryDescriptor
*of
,
386 IODirection withDirection
) APPLE_KEXT_DEPRECATED
; /* use IOSubMemoryDescriptor::withSubRange() and kIOMemoryThreadSafe instead */
387 #endif /* !__LP64__ */
389 /*! @function withPersistentMemoryDescriptor
390 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
391 @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.
392 @param originalMD The memory descriptor to be duplicated.
393 @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. */
394 static IOMemoryDescriptor
*
395 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
);
398 // obsolete initializers
399 // - initWithOptions is the designated initializer
400 virtual bool initWithAddress(void * address
,
401 IOByteCount withLength
,
402 IODirection withDirection
) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
403 virtual bool initWithAddress(IOVirtualAddress address
,
404 IOByteCount withLength
,
405 IODirection withDirection
,
406 task_t withTask
) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
407 virtual bool initWithPhysicalAddress(
408 IOPhysicalAddress address
,
409 IOByteCount withLength
,
410 IODirection withDirection
) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
411 virtual bool initWithRanges(IOVirtualRange
* ranges
,
413 IODirection withDirection
,
415 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
416 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
418 IODirection withDirection
,
419 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
420 #endif /* __LP64__ */
422 /*! @function getDirection
423 @abstract Accessor to get the direction the memory descriptor was created with.
424 @discussion This method returns the direction the memory descriptor was created with.
425 @result The direction. */
427 virtual IODirection
getDirection() const;
429 /*! @function getLength
430 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
431 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
432 @result The byte count. */
434 virtual IOByteCount
getLength() const;
437 @abstract Set the tag for the memory descriptor.
438 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
439 @param tag The tag. */
441 virtual void setTag( IOOptionBits tag
);
444 @abstract Accessor to the retrieve the tag for the memory descriptor.
445 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
448 virtual IOOptionBits
getTag( void );
450 /*! @function readBytes
451 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
452 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer. The memory descriptor MUST have the kIODirectionOut direcction bit set and be prepared. kIODirectionOut means that this memory descriptor will be output to an external device, so readBytes is used to get memory into a local buffer for a PIO transfer to the device.
453 @param offset A byte offset into the memory descriptor's memory.
454 @param bytes The caller supplied buffer to copy the data to.
455 @param withLength The length of the data to copy.
456 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. Development/debug kernel builds will assert if the offset is beyond the length of the descriptor. */
458 virtual IOByteCount
readBytes(IOByteCount offset
,
459 void * bytes
, IOByteCount withLength
);
461 /*! @function writeBytes
462 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
463 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer. The memory descriptor MUST have the kIODirectionIn direcction bit set and be prepared. kIODirectionIn means that this memory descriptor will be input from an external device, so writeBytes is used to write memory into the descriptor for PIO drivers.
464 @param offset A byte offset into the memory descriptor's memory.
465 @param bytes The caller supplied buffer to copy the data from.
466 @param withLength The length of the data to copy.
467 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. Development/debug kernel builds will assert if the offset is beyond the length of the descriptor. */
469 virtual IOByteCount
writeBytes(IOByteCount offset
,
470 const void * bytes
, IOByteCount withLength
);
473 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
474 IOByteCount
* length
);
475 #endif /* !__LP64__ */
477 /*! @function getPhysicalAddress
478 @abstract Return the physical address of the first byte in the memory.
479 @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.
480 @result A physical address. */
482 IOPhysicalAddress
getPhysicalAddress();
485 virtual void * getVirtualSegment(IOByteCount offset
,
486 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
; /* use map() and getVirtualAddress() instead */
487 #endif /* !__LP64__ */
489 /*! @function prepare
490 @abstract Prepare the memory for an I/O transfer.
491 @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.
492 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
493 @result An IOReturn code. */
495 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
) = 0;
497 /*! @function complete
498 @abstract Complete processing of the memory after an I/O transfer finishes.
499 @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.
500 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
501 @result An IOReturn code. */
503 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
) = 0;
509 /*! @function createMappingInTask
510 @abstract Maps a IOMemoryDescriptor into a task.
511 @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.
512 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
513 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
514 @param options Mapping options are defined in IOTypes.h,<br>
515 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>
516 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
517 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
518 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
519 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>
520 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>
521 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
522 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
523 @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. */
525 IOMemoryMap
* createMappingInTask(
527 mach_vm_address_t atAddress
,
528 IOOptionBits options
,
529 mach_vm_size_t offset
= 0,
530 mach_vm_size_t length
= 0 );
533 virtual IOMemoryMap
* map(
535 IOVirtualAddress atAddress
,
536 IOOptionBits options
,
537 IOByteCount offset
= 0,
538 IOByteCount length
= 0 ) APPLE_KEXT_DEPRECATED
; /* use createMappingInTask() instead */
539 #endif /* !__LP64__ */
542 @abstract Maps a IOMemoryDescriptor into the kernel map.
543 @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 createMappingInTask method for further details.
544 @param options Mapping options as in the full version of the createMappingInTask method, with kIOMapAnywhere assumed.
545 @result See the full version of the createMappingInTask method. */
547 virtual IOMemoryMap
* map(
548 IOOptionBits options
= 0 );
550 /*! @function setMapping
551 @abstract Establishes an already existing mapping.
552 @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.
553 @param task Address space in which the mapping exists.
554 @param mapAddress Virtual address of the mapping.
555 @param options Caching and read-only attributes of the mapping.
556 @result A IOMemoryMap object created to represent the mapping. */
558 virtual IOMemoryMap
* setMapping(
560 IOVirtualAddress mapAddress
,
561 IOOptionBits options
= 0 );
563 // Following methods are private implementation
567 #endif /* __LP64__ */
568 IOReturn
redirect( task_t safeTask
, bool redirect
);
570 IOReturn
handleFault(
573 mach_vm_address_t address
,
574 mach_vm_size_t sourceOffset
,
575 mach_vm_size_t length
,
576 IOOptionBits options
);
578 virtual IOMemoryMap
* makeMapping(
579 IOMemoryDescriptor
* owner
,
581 IOVirtualAddress atAddress
,
582 IOOptionBits options
,
584 IOByteCount length
);
587 virtual void addMapping(
588 IOMemoryMap
* mapping
);
590 virtual void removeMapping(
591 IOMemoryMap
* mapping
);
593 virtual IOReturn
doMap(
595 IOVirtualAddress
* atAddress
,
596 IOOptionBits options
,
597 IOByteCount sourceOffset
= 0,
598 IOByteCount length
= 0 );
600 virtual IOReturn
doUnmap(
602 IOVirtualAddress logical
,
603 IOByteCount length
);
606 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
608 /*! @class IOMemoryMap : public OSObject
609 @abstract A class defining common methods for describing a memory mapping.
610 @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. */
612 class IOMemoryMap
: public OSObject
614 OSDeclareDefaultStructors(IOMemoryMap
)
615 #ifdef XNU_KERNEL_PRIVATE
617 IOMemoryDescriptor
* fMemory
;
618 IOMemoryMap
* fSuperMap
;
619 mach_vm_size_t fOffset
;
620 mach_vm_address_t fAddress
;
621 mach_vm_size_t fLength
;
623 vm_map_t fAddressMap
;
624 IOOptionBits fOptions
;
626 ipc_port_t fRedirEntry
;
627 IOMemoryDescriptor
* fOwner
;
628 uint8_t fUserClientUnmap
;
629 #endif /* XNU_KERNEL_PRIVATE */
632 virtual void taggedRelease(const void *tag
= 0) const;
636 /*! @function getVirtualAddress
637 @abstract Accessor to the virtual address of the first byte in the mapping.
638 @discussion This method returns the virtual address of the first byte in the mapping.
639 @result A virtual address. */
641 virtual IOVirtualAddress
getVirtualAddress();
643 /*! @function getPhysicalSegment
644 @abstract Break a mapping into its physically contiguous segments.
645 @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.
646 @param offset A byte offset into the mapping whose physical address to return.
647 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
648 @result A physical address, or zero if the offset is beyond the length of the mapping. */
651 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
652 IOByteCount
* length
,
653 IOOptionBits options
= 0);
654 #else /* !__LP64__ */
655 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
656 IOByteCount
* length
);
657 #endif /* !__LP64__ */
659 /*! @function getPhysicalAddress
660 @abstract Return the physical address of the first byte in the mapping.
661 @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.
662 @result A physical address. */
664 IOPhysicalAddress
getPhysicalAddress();
666 /*! @function getLength
667 @abstract Accessor to the length of the mapping.
668 @discussion This method returns the length of the mapping.
669 @result A byte count. */
671 virtual IOByteCount
getLength();
673 /*! @function getAddressTask
674 @abstract Accessor to the task of the mapping.
675 @discussion This method returns the mach task the mapping exists in.
676 @result A mach task_t. */
678 virtual task_t
getAddressTask();
680 /*! @function getMemoryDescriptor
681 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
682 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
683 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
685 virtual IOMemoryDescriptor
* getMemoryDescriptor();
687 /*! @function getMapOptions
688 @abstract Accessor to the options the mapping was created with.
689 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
690 @result Options for the mapping, including cache settings. */
692 virtual IOOptionBits
getMapOptions();
695 @abstract Force the IOMemoryMap to unmap, without destroying the object.
696 @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.
697 @result An IOReturn code. */
699 virtual IOReturn
unmap();
701 virtual void taskDied();
703 /*! @function redirect
704 @abstract Replace the memory mapped in a process with new backing memory.
705 @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.
706 @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.
707 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
708 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
709 @result An IOReturn code. */
712 // For 32 bit XNU, there is a 32 bit (IOByteCount) and a 64 bit (mach_vm_size_t) interface;
713 // for 64 bit, these fall together on the 64 bit one.
714 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
715 IOOptionBits options
,
716 IOByteCount offset
= 0);
718 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
719 IOOptionBits options
,
720 mach_vm_size_t offset
= 0);
723 inline mach_vm_address_t
getAddress() __attribute__((always_inline
));
724 inline mach_vm_size_t
getSize() __attribute__((always_inline
));
725 #else /* !__LP64__ */
726 virtual mach_vm_address_t
getAddress();
727 virtual mach_vm_size_t
getSize();
728 #endif /* !__LP64__ */
730 #ifdef XNU_KERNEL_PRIVATE
731 // for IOMemoryDescriptor use
732 IOMemoryMap
* copyCompatible( IOMemoryMap
* newMapping
);
736 mach_vm_address_t toAddress
,
737 IOOptionBits options
,
738 mach_vm_size_t offset
,
739 mach_vm_size_t length
);
741 bool setMemoryDescriptor(IOMemoryDescriptor
* _memory
, mach_vm_size_t _offset
);
744 task_t intoTask
, bool redirect
);
746 IOReturn
userClientUnmap();
747 #endif /* XNU_KERNEL_PRIVATE */
749 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 0);
750 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 1);
751 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 2);
752 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 3);
753 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 4);
754 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 5);
755 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 6);
756 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 7);
759 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
760 #ifdef XNU_KERNEL_PRIVATE
761 // Also these flags should not overlap with the options to
762 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
764 _kIOMemorySourceSegment
= 0x00002000
766 #endif /* XNU_KERNEL_PRIVATE */
768 #if !defined(__LP64) || defined(_IOMEMORYDESCRIPTOR_INTERNAL_)
770 // The following classes are private implementation of IOMemoryDescriptor - they
771 // should not be referenced directly, just through the public API's in the
772 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
773 // might be created by IOMemoryDescriptor::withAddressRange(), but there should be
774 // no need to reference as anything but a generic IOMemoryDescriptor *.
776 class IOGeneralMemoryDescriptor
: public IOMemoryDescriptor
778 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor
);
789 unsigned _rangesCount
; /* number of address ranges in list */
791 bool _rangesIsAllocated
; /* is list allocated by us? */
792 #endif /* !__LP64__ */
794 task_t _task
; /* task where all ranges are mapped to */
799 } _singleRange
; /* storage space for a single range */
801 unsigned _wireCount
; /* number of outstanding wires */
804 uintptr_t _cachedVirtualAddress
;
806 IOPhysicalAddress _cachedPhysicalAddress
;
807 #endif /* !__LP64__ */
809 bool _initialized
; /* has superclass been initialized? */
814 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
816 virtual uint64_t getPreparationID( void );
821 virtual void setPosition(IOByteCount position
);
822 virtual void mapIntoKernel(unsigned rangeIndex
);
823 virtual void unmapFromKernel();
824 #endif /* !__LP64__ */
826 // Internal APIs may be made virtual at some time in the future.
827 IOReturn
wireVirtual(IODirection forDirection
);
828 void *createNamedEntry();
831 OSData
* _memoryEntries
;
833 ppnum_t _highestPage
;
834 uint32_t __iomd_reservedA
;
835 uint32_t __iomd_reservedB
;
837 IOLock
* _prepareLock
;
841 * IOMemoryDescriptor required methods
845 virtual bool initWithOptions(void * buffers
,
849 IOOptionBits options
,
850 IOMapper
* mapper
= kIOMapperSystem
);
853 // Secondary initialisers
854 virtual bool initWithAddress(void * address
,
855 IOByteCount withLength
,
856 IODirection withDirection
) APPLE_KEXT_DEPRECATED
;
858 virtual bool initWithAddress(IOVirtualAddress address
,
859 IOByteCount withLength
,
860 IODirection withDirection
,
861 task_t withTask
) APPLE_KEXT_DEPRECATED
;
863 virtual bool initWithPhysicalAddress(
864 IOPhysicalAddress address
,
865 IOByteCount withLength
,
866 IODirection withDirection
) APPLE_KEXT_DEPRECATED
;
868 virtual bool initWithRanges( IOVirtualRange
* ranges
,
870 IODirection withDirection
,
872 bool asReference
= false) APPLE_KEXT_DEPRECATED
;
874 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
876 IODirection withDirection
,
877 bool asReference
= false) APPLE_KEXT_DEPRECATED
;
879 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
880 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
882 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
883 IOByteCount
* length
);
885 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
886 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
888 virtual void * getVirtualSegment(IOByteCount offset
,
889 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
890 #endif /* !__LP64__ */
892 virtual IOReturn
setPurgeable( IOOptionBits newState
,
893 IOOptionBits
* oldState
);
895 virtual addr64_t
getPhysicalSegment( IOByteCount offset
,
896 IOByteCount
* length
,
898 IOOptionBits options
= 0 );
899 #else /* !__LP64__ */
900 IOOptionBits options
);
901 #endif /* !__LP64__ */
903 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
905 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
907 virtual IOReturn
doMap(
909 IOVirtualAddress
* atAddress
,
910 IOOptionBits options
,
911 IOByteCount sourceOffset
= 0,
912 IOByteCount length
= 0 );
914 virtual IOReturn
doUnmap(
916 IOVirtualAddress logical
,
917 IOByteCount length
);
919 virtual bool serialize(OSSerialize
*s
) const;
921 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
922 static IOMemoryDescriptor
*
923 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
);
927 #endif /* !defined(__LP64) || defined(_IOMEMORYDESCRIPTOR_INTERNAL_) */
929 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
932 mach_vm_address_t
IOMemoryMap::getAddress()
934 return (getVirtualAddress());
937 mach_vm_size_t
IOMemoryMap::getSize()
939 return (getLength());
941 #else /* !__LP64__ */
942 #include <IOKit/IOSubMemoryDescriptor.h>
943 #endif /* !__LP64__ */
945 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
947 #endif /* !_IOMEMORYDESCRIPTOR_H */