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
96 kIOMemoryClearEncrypt
= 0x00200000, // Shared with Buffer MD
99 #define kIOMapperSystem ((IOMapper *) 0)
103 kIOMemoryPurgeableKeepCurrent
= 1,
104 kIOMemoryPurgeableNonVolatile
= 2,
105 kIOMemoryPurgeableVolatile
= 3,
106 kIOMemoryPurgeableEmpty
= 4
110 kIOMemoryIncoherentIOFlush
= 1,
111 kIOMemoryIncoherentIOStore
= 2,
113 kIOMemoryClearEncrypted
= 50,
114 kIOMemorySetEncrypted
= 51,
117 #define IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND 1
121 kIOPreparationIDUnprepared
= 0,
122 kIOPreparationIDUnsupported
= 1,
123 kIOPreparationIDAlwaysPrepared
= 2,
126 /*! @class IOMemoryDescriptor : public OSObject
127 @abstract An abstract base class defining common methods for describing physical or virtual memory.
128 @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. */
130 class IOMemoryDescriptor
: public OSObject
132 friend class IOMemoryMap
;
134 OSDeclareDefaultStructors(IOMemoryDescriptor
);
137 /*! @struct ExpansionData
138 @discussion This structure will be used to expand the capablilties of this class in the future.
140 struct ExpansionData
{
142 unsigned int pagerContig
:1;
143 unsigned int unused
:31;
144 IOMemoryDescriptor
* memory
;
148 Reserved for future use. (Internal use only) */
149 ExpansionData
* reserved
;
157 uint64_t __iomd_reserved1
;
158 uint64_t __iomd_reserved2
;
159 uint64_t __iomd_reserved3
;
160 uint64_t __iomd_reserved4
;
161 #else /* !__LP64__ */
162 IODirection _direction
; /* use _flags instead */
163 #endif /* !__LP64__ */
164 IOByteCount _length
; /* length of all ranges */
168 typedef IOOptionBits DMACommandOps
;
170 virtual IOPhysicalAddress
getSourceSegment( IOByteCount offset
,
171 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
172 #endif /* !__LP64__ */
174 /*! @function initWithOptions
175 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
176 @discussion Note this function can be used to re-init a previously created memory descriptor.
177 @result true on success, false on failure. */
178 virtual bool initWithOptions(void * buffers
,
182 IOOptionBits options
,
183 IOMapper
* mapper
= kIOMapperSystem
);
186 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
187 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
; /* use getPhysicalSegment() and kIOMemoryMapperNone instead */
188 #endif /* !__LP64__ */
190 /*! @function setPurgeable
191 @abstract Control the purgeable status of a memory descriptors memory.
192 @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.
193 @param newState - the desired new purgeable state of the memory:<br>
194 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
195 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
196 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
197 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
198 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
199 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
200 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
201 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
202 @result An IOReturn code. */
204 virtual IOReturn
setPurgeable( IOOptionBits newState
,
205 IOOptionBits
* oldState
);
207 /*! @function performOperation
208 @abstract Perform an operation on the memory descriptor's memory.
209 @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.
210 @param options The operation to perform on the memory:<br>
211 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>
212 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.
213 @param offset A byte offset into the memory descriptor's memory.
214 @param length The length of the data range.
215 @result An IOReturn code. */
217 virtual IOReturn
performOperation( IOOptionBits options
,
218 IOByteCount offset
, IOByteCount length
);
220 // Used for dedicated communications for IODMACommand
221 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
223 /*! @function getPhysicalSegment
224 @abstract Break a memory descriptor into its physically contiguous segments.
225 @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.
226 @param offset A byte offset into the memory whose physical address to return.
227 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
228 @result A physical address, or zero if the offset is beyond the length of the memory. */
231 virtual addr64_t
getPhysicalSegment( IOByteCount offset
,
232 IOByteCount
* length
,
233 IOOptionBits options
= 0 ) = 0;
234 #else /* !__LP64__ */
235 virtual addr64_t
getPhysicalSegment( IOByteCount offset
,
236 IOByteCount
* length
,
237 IOOptionBits options
);
238 #endif /* !__LP64__ */
240 virtual uint64_t getPreparationID( void );
243 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 0);
245 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 1);
246 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 2);
247 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 3);
248 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 4);
249 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 5);
250 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 6);
251 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 7);
252 #else /* !__LP64__ */
253 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 1);
254 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 2);
255 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 3);
256 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 4);
257 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 5);
258 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 6);
259 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 7);
260 #endif /* !__LP64__ */
261 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 8);
262 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 9);
263 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 10);
264 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 11);
265 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 12);
266 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 13);
267 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 14);
268 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 15);
273 static void initialize( void );
276 /*! @function withAddress
277 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
278 @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.
279 @param address The virtual address of the first byte in the memory.
280 @param withLength The length of memory.
281 @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.
282 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
284 static IOMemoryDescriptor
* withAddress(void * address
,
285 IOByteCount withLength
,
286 IODirection withDirection
);
289 static IOMemoryDescriptor
* withAddress(IOVirtualAddress address
,
290 IOByteCount withLength
,
291 IODirection withDirection
,
292 task_t withTask
) APPLE_KEXT_DEPRECATED
; /* use withAddressRange() and prepare() instead */
293 #endif /* !__LP64__ */
295 /*! @function withPhysicalAddress
296 @abstract Create an IOMemoryDescriptor to describe one physical range.
297 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
298 @param address The physical address of the first byte in the memory.
299 @param withLength The length of memory.
300 @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.
301 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
303 static IOMemoryDescriptor
* withPhysicalAddress(
304 IOPhysicalAddress address
,
305 IOByteCount withLength
,
306 IODirection withDirection
);
309 static IOMemoryDescriptor
* withRanges(IOVirtualRange
* ranges
,
311 IODirection withDirection
,
313 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use withAddressRanges() instead */
314 #endif /* !__LP64__ */
316 /*! @function withAddressRange
317 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
318 @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.
319 @param address The virtual address of the first byte in the memory.
320 @param withLength The length of memory.
322 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.
323 @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. The task argument may be NULL to specify memory by physical address.
324 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
326 static IOMemoryDescriptor
* withAddressRange(
327 mach_vm_address_t address
,
328 mach_vm_size_t length
,
329 IOOptionBits options
,
332 /*! @function withAddressRanges
333 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
334 @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.
335 @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.
336 @param rangeCount The member count of the ranges array.
338 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.
339 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.
340 @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. The task argument may be NULL to specify memory by physical address.
341 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
343 static IOMemoryDescriptor
* withAddressRanges(
344 IOAddressRange
* ranges
,
346 IOOptionBits options
,
349 /*! @function withOptions
350 @abstract Master initialiser for all variants of memory descriptors.
351 @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.
354 @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.
356 @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.
358 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
360 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
363 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.
364 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.
365 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.
366 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.
368 @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.
370 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
372 static IOMemoryDescriptor
*withOptions(void * buffers
,
376 IOOptionBits options
,
377 IOMapper
* mapper
= kIOMapperSystem
);
380 static IOMemoryDescriptor
* withPhysicalRanges(
381 IOPhysicalRange
* ranges
,
383 IODirection withDirection
,
384 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use withOptions() and kIOMemoryTypePhysical instead */
385 #endif /* !__LP64__ */
388 static IOMemoryDescriptor
* withSubRange(IOMemoryDescriptor
*of
,
391 IODirection withDirection
) APPLE_KEXT_DEPRECATED
; /* use IOSubMemoryDescriptor::withSubRange() and kIOMemoryThreadSafe instead */
392 #endif /* !__LP64__ */
394 /*! @function withPersistentMemoryDescriptor
395 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
396 @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.
397 @param originalMD The memory descriptor to be duplicated.
398 @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. */
399 static IOMemoryDescriptor
*
400 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
);
403 // obsolete initializers
404 // - initWithOptions is the designated initializer
405 virtual bool initWithAddress(void * address
,
406 IOByteCount withLength
,
407 IODirection withDirection
) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
408 virtual bool initWithAddress(IOVirtualAddress address
,
409 IOByteCount withLength
,
410 IODirection withDirection
,
411 task_t withTask
) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
412 virtual bool initWithPhysicalAddress(
413 IOPhysicalAddress address
,
414 IOByteCount withLength
,
415 IODirection withDirection
) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
416 virtual bool initWithRanges(IOVirtualRange
* ranges
,
418 IODirection withDirection
,
420 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
421 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
423 IODirection withDirection
,
424 bool asReference
= false) APPLE_KEXT_DEPRECATED
; /* use initWithOptions() instead */
425 #endif /* __LP64__ */
427 /*! @function getDirection
428 @abstract Accessor to get the direction the memory descriptor was created with.
429 @discussion This method returns the direction the memory descriptor was created with.
430 @result The direction. */
432 virtual IODirection
getDirection() const;
434 /*! @function getLength
435 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
436 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
437 @result The byte count. */
439 virtual IOByteCount
getLength() const;
442 @abstract Set the tag for the memory descriptor.
443 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
444 @param tag The tag. */
446 virtual void setTag( IOOptionBits tag
);
449 @abstract Accessor to the retrieve the tag for the memory descriptor.
450 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
453 virtual IOOptionBits
getTag( void );
455 /*! @function readBytes
456 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
457 @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.
458 @param offset A byte offset into the memory descriptor's memory.
459 @param bytes The caller supplied buffer to copy the data to.
460 @param withLength The length of the data to copy.
461 @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. */
463 virtual IOByteCount
readBytes(IOByteCount offset
,
464 void * bytes
, IOByteCount withLength
);
466 /*! @function writeBytes
467 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
468 @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.
469 @param offset A byte offset into the memory descriptor's memory.
470 @param bytes The caller supplied buffer to copy the data from.
471 @param withLength The length of the data to copy.
472 @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. */
474 virtual IOByteCount
writeBytes(IOByteCount offset
,
475 const void * bytes
, IOByteCount withLength
);
478 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
479 IOByteCount
* length
);
480 #endif /* !__LP64__ */
482 /*! @function getPhysicalAddress
483 @abstract Return the physical address of the first byte in the memory.
484 @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.
485 @result A physical address. */
487 IOPhysicalAddress
getPhysicalAddress();
490 virtual void * getVirtualSegment(IOByteCount offset
,
491 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
; /* use map() and getVirtualAddress() instead */
492 #endif /* !__LP64__ */
494 /*! @function prepare
495 @abstract Prepare the memory for an I/O transfer.
496 @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.
497 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
498 @result An IOReturn code. */
500 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
) = 0;
502 /*! @function complete
503 @abstract Complete processing of the memory after an I/O transfer finishes.
504 @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.
505 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
506 @result An IOReturn code. */
508 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
) = 0;
514 /*! @function createMappingInTask
515 @abstract Maps a IOMemoryDescriptor into a task.
516 @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.
517 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
518 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
519 @param options Mapping options are defined in IOTypes.h,<br>
520 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>
521 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
522 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
523 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
524 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>
525 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>
526 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
527 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
528 @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. */
530 IOMemoryMap
* createMappingInTask(
532 mach_vm_address_t atAddress
,
533 IOOptionBits options
,
534 mach_vm_size_t offset
= 0,
535 mach_vm_size_t length
= 0 );
538 virtual IOMemoryMap
* map(
540 IOVirtualAddress atAddress
,
541 IOOptionBits options
,
542 IOByteCount offset
= 0,
543 IOByteCount length
= 0 ) APPLE_KEXT_DEPRECATED
; /* use createMappingInTask() instead */
544 #endif /* !__LP64__ */
547 @abstract Maps a IOMemoryDescriptor into the kernel map.
548 @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.
549 @param options Mapping options as in the full version of the createMappingInTask method, with kIOMapAnywhere assumed.
550 @result See the full version of the createMappingInTask method. */
552 virtual IOMemoryMap
* map(
553 IOOptionBits options
= 0 );
555 /*! @function setMapping
556 @abstract Establishes an already existing mapping.
557 @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.
558 @param task Address space in which the mapping exists.
559 @param mapAddress Virtual address of the mapping.
560 @param options Caching and read-only attributes of the mapping.
561 @result A IOMemoryMap object created to represent the mapping. */
563 virtual IOMemoryMap
* setMapping(
565 IOVirtualAddress mapAddress
,
566 IOOptionBits options
= 0 );
568 // Following methods are private implementation
572 #endif /* __LP64__ */
573 IOReturn
redirect( task_t safeTask
, bool redirect
);
575 IOReturn
handleFault(
578 mach_vm_address_t address
,
579 mach_vm_size_t sourceOffset
,
580 mach_vm_size_t length
,
581 IOOptionBits options
);
583 virtual IOMemoryMap
* makeMapping(
584 IOMemoryDescriptor
* owner
,
586 IOVirtualAddress atAddress
,
587 IOOptionBits options
,
589 IOByteCount length
);
592 virtual void addMapping(
593 IOMemoryMap
* mapping
);
595 virtual void removeMapping(
596 IOMemoryMap
* mapping
);
598 virtual IOReturn
doMap(
600 IOVirtualAddress
* atAddress
,
601 IOOptionBits options
,
602 IOByteCount sourceOffset
= 0,
603 IOByteCount length
= 0 );
605 virtual IOReturn
doUnmap(
607 IOVirtualAddress logical
,
608 IOByteCount length
);
611 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
613 /*! @class IOMemoryMap : public OSObject
614 @abstract A class defining common methods for describing a memory mapping.
615 @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. */
617 class IOMemoryMap
: public OSObject
619 OSDeclareDefaultStructors(IOMemoryMap
)
620 #ifdef XNU_KERNEL_PRIVATE
622 IOMemoryDescriptor
* fMemory
;
623 IOMemoryMap
* fSuperMap
;
624 mach_vm_size_t fOffset
;
625 mach_vm_address_t fAddress
;
626 mach_vm_size_t fLength
;
628 vm_map_t fAddressMap
;
629 IOOptionBits fOptions
;
631 ipc_port_t fRedirEntry
;
632 IOMemoryDescriptor
* fOwner
;
633 uint8_t fUserClientUnmap
;
634 #endif /* XNU_KERNEL_PRIVATE */
637 virtual void taggedRelease(const void *tag
= 0) const;
641 /*! @function getVirtualAddress
642 @abstract Accessor to the virtual address of the first byte in the mapping.
643 @discussion This method returns the virtual address of the first byte in the mapping. Since the IOVirtualAddress is only 32bit in 32bit kernels, the getAddress() method should be used for compatibility with 64bit task mappings.
644 @result A virtual address. */
646 virtual IOVirtualAddress
getVirtualAddress();
648 /*! @function getPhysicalSegment
649 @abstract Break a mapping into its physically contiguous segments.
650 @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.
651 @param offset A byte offset into the mapping whose physical address to return.
652 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
653 @result A physical address, or zero if the offset is beyond the length of the mapping. */
656 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
657 IOByteCount
* length
,
658 IOOptionBits options
= 0);
659 #else /* !__LP64__ */
660 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
661 IOByteCount
* length
);
662 #endif /* !__LP64__ */
664 /*! @function getPhysicalAddress
665 @abstract Return the physical address of the first byte in the mapping.
666 @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.
667 @result A physical address. */
669 IOPhysicalAddress
getPhysicalAddress();
671 /*! @function getLength
672 @abstract Accessor to the length of the mapping.
673 @discussion This method returns the length of the mapping.
674 @result A byte count. */
676 virtual IOByteCount
getLength();
678 /*! @function getAddressTask
679 @abstract Accessor to the task of the mapping.
680 @discussion This method returns the mach task the mapping exists in.
681 @result A mach task_t. */
683 virtual task_t
getAddressTask();
685 /*! @function getMemoryDescriptor
686 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
687 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
688 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
690 virtual IOMemoryDescriptor
* getMemoryDescriptor();
692 /*! @function getMapOptions
693 @abstract Accessor to the options the mapping was created with.
694 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
695 @result Options for the mapping, including cache settings. */
697 virtual IOOptionBits
getMapOptions();
700 @abstract Force the IOMemoryMap to unmap, without destroying the object.
701 @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.
702 @result An IOReturn code. */
704 virtual IOReturn
unmap();
706 virtual void taskDied();
708 /*! @function redirect
709 @abstract Replace the memory mapped in a process with new backing memory.
710 @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.
711 @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.
712 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
713 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
714 @result An IOReturn code. */
717 // For 32 bit XNU, there is a 32 bit (IOByteCount) and a 64 bit (mach_vm_size_t) interface;
718 // for 64 bit, these fall together on the 64 bit one.
719 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
720 IOOptionBits options
,
721 IOByteCount offset
= 0);
723 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
724 IOOptionBits options
,
725 mach_vm_size_t offset
= 0);
728 /*! @function getAddress
729 @abstract Accessor to the virtual address of the first byte in the mapping.
730 @discussion This method returns the virtual address of the first byte in the mapping.
731 @result A virtual address. */
732 /*! @function getSize
733 @abstract Accessor to the length of the mapping.
734 @discussion This method returns the length of the mapping.
735 @result A byte count. */
736 inline mach_vm_address_t
getAddress() __attribute__((always_inline
));
737 inline mach_vm_size_t
getSize() __attribute__((always_inline
));
738 #else /* !__LP64__ */
739 /*! @function getAddress
740 @abstract Accessor to the virtual address of the first byte in the mapping.
741 @discussion This method returns the virtual address of the first byte in the mapping.
742 @result A virtual address. */
743 /*! @function getSize
744 @abstract Accessor to the length of the mapping.
745 @discussion This method returns the length of the mapping.
746 @result A byte count. */
747 virtual mach_vm_address_t
getAddress();
748 virtual mach_vm_size_t
getSize();
749 #endif /* !__LP64__ */
751 #ifdef XNU_KERNEL_PRIVATE
752 // for IOMemoryDescriptor use
753 IOMemoryMap
* copyCompatible( IOMemoryMap
* newMapping
);
757 mach_vm_address_t toAddress
,
758 IOOptionBits options
,
759 mach_vm_size_t offset
,
760 mach_vm_size_t length
);
762 bool setMemoryDescriptor(IOMemoryDescriptor
* _memory
, mach_vm_size_t _offset
);
765 task_t intoTask
, bool redirect
);
767 IOReturn
userClientUnmap();
768 #endif /* XNU_KERNEL_PRIVATE */
770 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 0);
771 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 1);
772 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 2);
773 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 3);
774 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 4);
775 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 5);
776 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 6);
777 OSMetaClassDeclareReservedUnused(IOMemoryMap
, 7);
780 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
781 #ifdef XNU_KERNEL_PRIVATE
782 // Also these flags should not overlap with the options to
783 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
785 _kIOMemorySourceSegment
= 0x00002000
787 #endif /* XNU_KERNEL_PRIVATE */
789 // The following classes are private implementation of IOMemoryDescriptor - they
790 // should not be referenced directly, just through the public API's in the
791 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
792 // might be created by IOMemoryDescriptor::withAddressRange(), but there should be
793 // no need to reference as anything but a generic IOMemoryDescriptor *.
795 class IOGeneralMemoryDescriptor
: public IOMemoryDescriptor
797 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor
);
808 unsigned _rangesCount
; /* number of address ranges in list */
810 bool _rangesIsAllocated
; /* is list allocated by us? */
811 #endif /* !__LP64__ */
813 task_t _task
; /* task where all ranges are mapped to */
818 } _singleRange
; /* storage space for a single range */
820 unsigned _wireCount
; /* number of outstanding wires */
823 uintptr_t _cachedVirtualAddress
;
825 IOPhysicalAddress _cachedPhysicalAddress
;
826 #endif /* !__LP64__ */
828 bool _initialized
; /* has superclass been initialized? */
833 virtual IOReturn
dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const;
835 virtual uint64_t getPreparationID( void );
840 virtual void setPosition(IOByteCount position
);
841 virtual void mapIntoKernel(unsigned rangeIndex
);
842 virtual void unmapFromKernel();
843 #endif /* !__LP64__ */
845 // Internal APIs may be made virtual at some time in the future.
846 IOReturn
wireVirtual(IODirection forDirection
);
847 void *createNamedEntry();
850 OSData
* _memoryEntries
;
852 ppnum_t _highestPage
;
853 uint32_t __iomd_reservedA
;
854 uint32_t __iomd_reservedB
;
856 IOLock
* _prepareLock
;
860 * IOMemoryDescriptor required methods
864 virtual bool initWithOptions(void * buffers
,
868 IOOptionBits options
,
869 IOMapper
* mapper
= kIOMapperSystem
);
872 // Secondary initialisers
873 virtual bool initWithAddress(void * address
,
874 IOByteCount withLength
,
875 IODirection withDirection
) APPLE_KEXT_DEPRECATED
;
877 virtual bool initWithAddress(IOVirtualAddress address
,
878 IOByteCount withLength
,
879 IODirection withDirection
,
880 task_t withTask
) APPLE_KEXT_DEPRECATED
;
882 virtual bool initWithPhysicalAddress(
883 IOPhysicalAddress address
,
884 IOByteCount withLength
,
885 IODirection withDirection
) APPLE_KEXT_DEPRECATED
;
887 virtual bool initWithRanges( IOVirtualRange
* ranges
,
889 IODirection withDirection
,
891 bool asReference
= false) APPLE_KEXT_DEPRECATED
;
893 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
895 IODirection withDirection
,
896 bool asReference
= false) APPLE_KEXT_DEPRECATED
;
898 virtual addr64_t
getPhysicalSegment64( IOByteCount offset
,
899 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
901 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
902 IOByteCount
* length
);
904 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
905 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
907 virtual void * getVirtualSegment(IOByteCount offset
,
908 IOByteCount
* length
) APPLE_KEXT_DEPRECATED
;
909 #endif /* !__LP64__ */
911 virtual IOReturn
setPurgeable( IOOptionBits newState
,
912 IOOptionBits
* oldState
);
914 virtual addr64_t
getPhysicalSegment( IOByteCount offset
,
915 IOByteCount
* length
,
917 IOOptionBits options
= 0 );
918 #else /* !__LP64__ */
919 IOOptionBits options
);
920 #endif /* !__LP64__ */
922 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
924 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
926 virtual IOReturn
doMap(
928 IOVirtualAddress
* atAddress
,
929 IOOptionBits options
,
930 IOByteCount sourceOffset
= 0,
931 IOByteCount length
= 0 );
933 virtual IOReturn
doUnmap(
935 IOVirtualAddress logical
,
936 IOByteCount length
);
938 virtual bool serialize(OSSerialize
*s
) const;
940 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
941 static IOMemoryDescriptor
*
942 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
);
946 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
949 mach_vm_address_t
IOMemoryMap::getAddress()
951 return (getVirtualAddress());
954 mach_vm_size_t
IOMemoryMap::getSize()
956 return (getLength());
958 #else /* !__LP64__ */
959 #include <IOKit/IOSubMemoryDescriptor.h>
960 #endif /* !__LP64__ */
962 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
964 #endif /* !_IOMEMORYDESCRIPTOR_H */