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 second withRanges variant
59 kIOMemoryDirectionMask
= 0x00000007,
60 kIOMemoryAutoPrepare
= 0x00000008, // Shared with Buffer MD
62 kIOMemoryTypeVirtual
= 0x00000010,
63 kIOMemoryTypePhysical
= 0x00000020,
64 kIOMemoryTypeUPL
= 0x00000030,
65 kIOMemoryTypeMask
= 0x000000f0,
67 kIOMemoryAsReference
= 0x00000100,
68 kIOMemoryBufferPageable
= 0x00000400,
69 kIOMemoryDontMap
= 0x00000800,
70 kIOMemoryPersistent
= 0x00010000
73 #define kIOMapperNone ((IOMapper *) -1)
74 #define kIOMapperSystem ((IOMapper *) 0)
76 /*! @class IOMemoryDescriptor : public OSObject
77 @abstract An abstract base class defining common methods for describing physical or virtual memory.
78 @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. */
80 class IOMemoryDescriptor
: public OSObject
82 friend class _IOMemoryMap
;
83 friend class IOSubMemoryDescriptor
;
85 OSDeclareDefaultStructors(IOMemoryDescriptor
);
88 /*! @struct ExpansionData
89 @discussion This structure will be used to expand the capablilties of this class in the future.
91 struct ExpansionData
{
93 unsigned int pagerContig
:1;
94 unsigned int unused
:31;
95 IOMemoryDescriptor
* memory
;
99 Reserved for future use. (Internal use only) */
100 ExpansionData
* reserved
;
107 IODirection _direction
; /* DEPRECATED: use _flags instead. direction of transfer */
108 IOByteCount _length
; /* length of all ranges */
113 /*! @function getBackingID
114 @abstract Get an unique identifier for the virtual memory systems backing memory object.
115 @discussion For memory descriptors that are directly mapped by real memory, IOGeneralMemoryDescriptors that are also persistent (kIOMemoryPersistent) return the id of the backing vm object. This returned value can be tested to see if 2 memory persistent memory descriptors share the same backing. The call itself is fairly heavy weight and can only be applied to persistent memory descriptors so it is not generally useful. This function is NOT virtual at the moment. We may choose to make it virtual in the future however.
116 @result 0 on non-persistent or non IOGeneralMemoryDescriptors, unique id if not. */
117 // See implementation at end of file
118 inline void * getBackingID() const;
120 virtual IOPhysicalAddress
getSourceSegment( IOByteCount offset
,
121 IOByteCount
* length
);
122 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 0);
124 /*! @function initWithOptions
125 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
126 @discussion Note this function can be used to re-init a previously created memory descriptor.
127 @result true on success, false on failure. */
128 virtual bool initWithOptions(void * buffers
,
132 IOOptionBits options
,
133 IOMapper
* mapper
= 0);
134 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 1);
136 virtual addr64_t
IOMemoryDescriptor::getPhysicalSegment64( IOByteCount offset
,
137 IOByteCount
* length
);
138 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor
, 2);
142 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 3);
143 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 4);
144 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 5);
145 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 6);
146 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 7);
147 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 8);
148 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 9);
149 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 10);
150 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 11);
151 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 12);
152 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 13);
153 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 14);
154 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor
, 15);
159 static void initialize( void );
162 /*! @function withAddress
163 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
164 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map.
165 @param address The virtual address of the first byte in the memory.
166 @param withLength The length of memory.
167 @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.
168 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
170 static IOMemoryDescriptor
* withAddress(void * address
,
171 IOByteCount withLength
,
172 IODirection withDirection
);
174 /*! @function withAddress
175 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
176 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
177 @param address The virtual address of the first byte in the memory.
178 @param withLength The length of memory.
179 @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.
180 @param withTask The task the virtual ranges are mapped into.
181 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
183 static IOMemoryDescriptor
* withAddress(vm_address_t address
,
184 IOByteCount withLength
,
185 IODirection withDirection
,
188 /*! @function withPhysicalAddress
189 @abstract Create an IOMemoryDescriptor to describe one physical range.
190 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
191 @param address The physical address of the first byte in the memory.
192 @param withLength The length of memory.
193 @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.
194 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
196 static IOMemoryDescriptor
* withPhysicalAddress(
197 IOPhysicalAddress address
,
198 IOByteCount withLength
,
199 IODirection withDirection
);
201 /*! @function withRanges
202 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
203 @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.
204 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
205 @param withCount The member count of the ranges array.
206 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
207 @param withTask The task each of the virtual ranges are mapped into.
208 @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.
209 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
211 static IOMemoryDescriptor
* withRanges(IOVirtualRange
* ranges
,
213 IODirection withDirection
,
215 bool asReference
= false);
217 /*! @function withOptions
218 @abstract Master initialiser for all variants of memory descriptors.
219 @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.
222 @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.
224 @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.
226 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
228 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
231 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.
232 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.
233 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.
234 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.
235 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
237 @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.
239 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
241 static IOMemoryDescriptor
*withOptions(void * buffers
,
245 IOOptionBits options
,
246 IOMapper
* mapper
= 0);
248 /*! @function withPhysicalRanges
249 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
250 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
251 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
252 @param withCount The member count of the ranges array.
253 @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.
254 @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.
255 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
257 static IOMemoryDescriptor
* withPhysicalRanges(
258 IOPhysicalRange
* ranges
,
260 IODirection withDirection
,
261 bool asReference
= false);
263 /*! @function withSubRange
264 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
265 @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.
266 @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.
267 @param offset A byte offset into the parent memory descriptor's memory.
268 @param length The length of the subrange.
269 @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.
270 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
272 static IOMemoryDescriptor
* withSubRange(IOMemoryDescriptor
*of
,
275 IODirection withDirection
);
277 /*! @function initWithAddress
278 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
279 @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.
280 @param address The virtual address of the first byte in the memory.
281 @param withLength The length of memory.
282 @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.
283 @result true on success, false on failure. */
285 virtual bool initWithAddress(void * address
,
286 IOByteCount withLength
,
287 IODirection withDirection
) = 0;
289 /*! @function initWithAddress
290 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
291 @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.
292 @param address The virtual address of the first byte in the memory.
293 @param withLength The length of memory.
294 @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.
295 @param withTask The task the virtual ranges are mapped into.
296 @result true on success, false on failure. */
298 virtual bool initWithAddress(vm_address_t address
,
299 IOByteCount withLength
,
300 IODirection withDirection
,
301 task_t withTask
) = 0;
303 /*! @function initWithPhysicalAddress
304 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
305 @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.
306 @param address The physical address of the first byte in the memory.
307 @param withLength The length of memory.
308 @param withDirection An I/O direction to be associated with the descriptor, which may affect the operation of the prepare and complete methods on some architectures.
309 @result true on success, false on failure. */
311 virtual bool initWithPhysicalAddress(
312 IOPhysicalAddress address
,
313 IOByteCount withLength
,
314 IODirection withDirection
) = 0;
316 /*! @function initWithRanges
317 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
318 @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.
319 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
320 @param withCount The member count of the ranges array.
321 @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.
322 @param withTask The task each of the virtual ranges are mapped into.
323 @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.
324 @result true on success, false on failure. */
326 virtual bool initWithRanges(IOVirtualRange
* ranges
,
328 IODirection withDirection
,
330 bool asReference
= false) = 0;
332 /*! @function initWithPhysicalRanges
333 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
334 @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.
335 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
336 @param withCount The member count of the ranges array.
337 @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.
338 @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.
339 @result true on success, false on failure. */
341 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
343 IODirection withDirection
,
344 bool asReference
= false) = 0;
346 /*! @function getDirection
347 @abstract Accessor to get the direction the memory descriptor was created with.
348 @discussion This method returns the direction the memory descriptor was created with.
349 @result The direction. */
351 virtual IODirection
getDirection() const;
353 /*! @function getLength
354 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
355 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
356 @result The byte count. */
358 virtual IOByteCount
getLength() const;
361 @abstract Set the tag for the memory descriptor.
362 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
363 @param tag The tag. */
365 virtual void setTag( IOOptionBits tag
);
368 @abstract Accessor to the retrieve the tag for the memory descriptor.
369 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
372 virtual IOOptionBits
getTag( void );
374 /*! @function readBytes
375 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
376 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
377 @param offset A byte offset into the memory descriptor's memory.
378 @param bytes The caller supplied buffer to copy the data to.
379 @param withLength The length of the data to copy.
380 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
382 virtual IOByteCount
readBytes(IOByteCount offset
,
383 void * bytes
, IOByteCount withLength
);
385 /*! @function writeBytes
386 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
387 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
388 @param offset A byte offset into the memory descriptor's memory.
389 @param bytes The caller supplied buffer to copy the data from.
390 @param withLength The length of the data to copy.
391 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
393 virtual IOByteCount
writeBytes(IOByteCount offset
,
394 const void * bytes
, IOByteCount withLength
);
396 /*! @function getPhysicalSegment
397 @abstract Break a memory descriptor into its physically contiguous segments.
398 @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.
399 @param offset A byte offset into the memory whose physical address to return.
400 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
401 @result A physical address, or zero if the offset is beyond the length of the memory. */
403 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
404 IOByteCount
* length
) = 0;
406 /*! @function getPhysicalAddress
407 @abstract Return the physical address of the first byte in the memory.
408 @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.
409 @result A physical address. */
411 /* inline */ IOPhysicalAddress
getPhysicalAddress();
412 /* { return( getPhysicalSegment( 0, 0 )); } */
414 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
415 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
416 /* DEPRECATED */ IOByteCount
* length
) = 0;
417 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
419 /*! @function prepare
420 @abstract Prepare the memory for an I/O transfer.
421 @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.
422 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
423 @result An IOReturn code. */
425 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
) = 0;
427 /*! @function complete
428 @abstract Complete processing of the memory after an I/O transfer finishes.
429 @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.
430 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
431 @result An IOReturn code. */
433 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
) = 0;
440 @abstract Maps a IOMemoryDescriptor into a task.
441 @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.
442 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
443 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
444 @param options Mapping options are defined in IOTypes.h,<br>
445 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>
446 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
447 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
448 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
449 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>
450 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
451 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
452 @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. */
454 virtual IOMemoryMap
* map(
456 IOVirtualAddress atAddress
,
457 IOOptionBits options
,
458 IOByteCount offset
= 0,
459 IOByteCount length
= 0 );
462 @abstract Maps a IOMemoryDescriptor into the kernel map.
463 @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.
464 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
465 @result See the full version of the map method. */
467 virtual IOMemoryMap
* map(
468 IOOptionBits options
= 0 );
470 /*! @function setMapping
471 @abstract Establishes an already existing mapping.
472 @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.
473 @param task Address space in which the mapping exists.
474 @param mapAddress Virtual address of the mapping.
475 @param options Caching and read-only attributes of the mapping.
476 @result A IOMemoryMap object created to represent the mapping. */
478 virtual IOMemoryMap
* setMapping(
480 IOVirtualAddress mapAddress
,
481 IOOptionBits options
= 0 );
483 // Following methods are private implementation
486 IOReturn
redirect( task_t safeTask
, bool redirect
);
488 IOReturn
handleFault(
491 IOVirtualAddress address
,
492 IOByteCount sourceOffset
,
494 IOOptionBits options
);
497 virtual IOMemoryMap
* makeMapping(
498 IOMemoryDescriptor
* owner
,
500 IOVirtualAddress atAddress
,
501 IOOptionBits options
,
503 IOByteCount length
);
505 virtual void addMapping(
506 IOMemoryMap
* mapping
);
508 virtual void removeMapping(
509 IOMemoryMap
* mapping
);
511 virtual IOReturn
doMap(
513 IOVirtualAddress
* atAddress
,
514 IOOptionBits options
,
515 IOByteCount sourceOffset
= 0,
516 IOByteCount length
= 0 );
518 virtual IOReturn
doUnmap(
520 IOVirtualAddress logical
,
521 IOByteCount length
);
524 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
526 /*! @class IOMemoryMap : public OSObject
527 @abstract An abstract base class defining common methods for describing a memory mapping.
528 @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. */
530 class IOMemoryMap
: public OSObject
532 OSDeclareAbstractStructors(IOMemoryMap
)
535 /*! @function getVirtualAddress
536 @abstract Accessor to the virtual address of the first byte in the mapping.
537 @discussion This method returns the virtual address of the first byte in the mapping.
538 @result A virtual address. */
540 virtual IOVirtualAddress
getVirtualAddress() = 0;
542 /*! @function getPhysicalSegment
543 @abstract Break a mapping into its physically contiguous segments.
544 @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.
545 @param offset A byte offset into the mapping whose physical address to return.
546 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
547 @result A physical address, or zero if the offset is beyond the length of the mapping. */
549 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
550 IOByteCount
* length
) = 0;
552 /*! @function getPhysicalAddress
553 @abstract Return the physical address of the first byte in the mapping.
554 @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.
555 @result A physical address. */
557 /* inline */ IOPhysicalAddress
getPhysicalAddress();
558 /* { return( getPhysicalSegment( 0, 0 )); } */
560 /*! @function getLength
561 @abstract Accessor to the length of the mapping.
562 @discussion This method returns the length of the mapping.
563 @result A byte count. */
565 virtual IOByteCount
getLength() = 0;
567 /*! @function getAddressTask
568 @abstract Accessor to the task of the mapping.
569 @discussion This method returns the mach task the mapping exists in.
570 @result A mach task_t. */
572 virtual task_t
getAddressTask() = 0;
574 /*! @function getMemoryDescriptor
575 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
576 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
577 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
579 virtual IOMemoryDescriptor
* getMemoryDescriptor() = 0;
581 /*! @function getMapOptions
582 @abstract Accessor to the options the mapping was created with.
583 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
584 @result Options for the mapping, including cache settings. */
586 virtual IOOptionBits
getMapOptions() = 0;
589 @abstract Force the IOMemoryMap to unmap, without destroying the object.
590 @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.
591 @result An IOReturn code. */
593 virtual IOReturn
unmap() = 0;
595 virtual void taskDied() = 0;
598 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
600 // The following classes are private implementation of IOMemoryDescriptor - they
601 // should not be referenced directly, just through the public API's in the
602 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
603 // might be created by IOMemoryDescriptor::withAddress(), but there should be
604 // no need to reference as anything but a generic IOMemoryDescriptor *.
606 // Also these flags should not overlap with the options to
607 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
610 kIOMemoryPreparedReadOnly
= 0x00008000,
613 class IOGeneralMemoryDescriptor
: public IOMemoryDescriptor
615 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor
);
621 } _ranges
; /* list of address ranges */
622 unsigned _rangesCount
; /* number of address ranges in list */
623 bool _rangesIsAllocated
; /* is list allocated by us? */
625 task_t _task
; /* task where all ranges are mapped to */
630 } _singleRange
; /* storage space for a single range */
632 unsigned _wireCount
; /* number of outstanding wires */
634 /* DEPRECATED */ vm_address_t _cachedVirtualAddress
; /* a cached virtual-to-physical */
636 /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress
;
638 bool _initialized
; /* has superclass been initialized? */
644 // Internal API may be made virtual at some time in the future.
645 IOReturn
wireVirtual(IODirection forDirection
);
647 /* DEPRECATED */ IOByteCount _position
; /* absolute position over all ranges */
648 /* DEPRECATED */ virtual void setPosition(IOByteCount position
);
651 * DEPRECATED IOByteCount _positionAtIndex; // relative position within range #n
653 * Re-use the _positionAtIndex as a count of the number of pages in
654 * this memory descriptor. Convieniently vm_address_t is an unsigned integer
655 * type so I can get away without having to change the type.
659 /* DEPRECATED */ unsigned _positionAtOffset
; //range #n in which position is now
661 OSData
*_memoryEntries
;
663 /* DEPRECATED */ vm_offset_t _kernPtrAligned
;
664 /* DEPRECATED */ unsigned _kernPtrAtIndex
;
665 /* DEPRECATED */ IOByteCount _kernSize
;
667 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex
);
668 /* DEPRECATED */ virtual void unmapFromKernel();
672 * IOMemoryDescriptor required methods
675 /*! @function getBackingID
676 @abstract Returns the vm systems unique id for the memory backing this IOGeneralMemoryDescriptor. See IOMemoryDescriptor::getBackingID for details.
677 @result 0 on non-persistent or non IOGeneralMemoryDescriptors, unique id if not. */
678 void * getBackingID() const;
681 virtual bool initWithOptions(void * buffers
,
685 IOOptionBits options
,
686 IOMapper
* mapper
= 0);
688 // Secondary initialisers
689 virtual bool initWithAddress(void * address
,
690 IOByteCount withLength
,
691 IODirection withDirection
);
693 virtual bool initWithAddress(vm_address_t address
,
694 IOByteCount withLength
,
695 IODirection withDirection
,
698 virtual bool initWithPhysicalAddress(
699 IOPhysicalAddress address
,
700 IOByteCount withLength
,
701 IODirection withDirection
);
703 virtual bool initWithRanges( IOVirtualRange
* ranges
,
705 IODirection withDirection
,
707 bool asReference
= false);
709 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
711 IODirection withDirection
,
712 bool asReference
= false);
714 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
715 IOByteCount
* length
);
717 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
718 IOByteCount
* length
);
720 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset
,
721 /* DEPRECATED */ IOByteCount
* length
);
723 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
725 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
727 virtual IOReturn
doMap(
729 IOVirtualAddress
* atAddress
,
730 IOOptionBits options
,
731 IOByteCount sourceOffset
= 0,
732 IOByteCount length
= 0 );
734 virtual IOReturn
doUnmap(
736 IOVirtualAddress logical
,
737 IOByteCount length
);
738 virtual bool serialize(OSSerialize
*s
) const;
741 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
743 class IOSubMemoryDescriptor
: public IOMemoryDescriptor
745 friend class IOMemoryDescriptor
;
747 OSDeclareDefaultStructors(IOSubMemoryDescriptor
);
750 IOMemoryDescriptor
* _parent
;
755 virtual bool initWithAddress(void * address
,
756 IOByteCount withLength
,
757 IODirection withDirection
);
759 virtual bool initWithAddress(vm_address_t address
,
760 IOByteCount withLength
,
761 IODirection withDirection
,
764 virtual bool initWithPhysicalAddress(
765 IOPhysicalAddress address
,
766 IOByteCount withLength
,
767 IODirection withDirection
);
769 virtual bool initWithRanges( IOVirtualRange
* ranges
,
771 IODirection withDirection
,
773 bool asReference
= false);
775 virtual bool initWithPhysicalRanges(IOPhysicalRange
* ranges
,
777 IODirection withDirection
,
778 bool asReference
= false);
780 IOMemoryDescriptor::withAddress
;
781 IOMemoryDescriptor::withPhysicalAddress
;
782 IOMemoryDescriptor::withPhysicalRanges
;
783 IOMemoryDescriptor::withRanges
;
784 IOMemoryDescriptor::withSubRange
;
788 * Initialize or reinitialize an IOSubMemoryDescriptor to describe
789 * a subrange of an existing descriptor.
791 * An IOSubMemoryDescriptor can be re-used by calling initSubRange
792 * again on an existing instance -- note that this behavior is not
793 * commonly supported in other IOKit classes, although it is here.
795 virtual bool initSubRange( IOMemoryDescriptor
* parent
,
796 IOByteCount offset
, IOByteCount length
,
797 IODirection withDirection
);
800 * IOMemoryDescriptor required methods
803 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
804 IOByteCount
* length
);
806 virtual IOPhysicalAddress
getSourceSegment(IOByteCount offset
,
807 IOByteCount
* length
);
809 virtual IOByteCount
readBytes(IOByteCount offset
,
810 void * bytes
, IOByteCount withLength
);
812 virtual IOByteCount
writeBytes(IOByteCount offset
,
813 const void * bytes
, IOByteCount withLength
);
815 virtual void * getVirtualSegment(IOByteCount offset
,
816 IOByteCount
* length
);
818 virtual IOReturn
prepare(IODirection forDirection
= kIODirectionNone
);
820 virtual IOReturn
complete(IODirection forDirection
= kIODirectionNone
);
823 IOReturn
redirect( task_t safeTask
, bool redirect
);
825 virtual bool serialize(OSSerialize
*s
) const;
828 virtual IOMemoryMap
* makeMapping(
829 IOMemoryDescriptor
* owner
,
831 IOVirtualAddress atAddress
,
832 IOOptionBits options
,
834 IOByteCount length
);
837 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
839 // Implementation of inline functions
840 void * IOMemoryDescriptor::getBackingID() const
842 const IOGeneralMemoryDescriptor
*genMD
= (const IOGeneralMemoryDescriptor
*)
843 OSDynamicCast(IOGeneralMemoryDescriptor
, this);
846 return genMD
->getBackingID();
851 #endif /* !_IOMEMORYDESCRIPTOR_H */