]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOMemoryDescriptor.h
eb138fc03c335b6eef97dd38397592de12d2d10d
[apple/xnu.git] / iokit / IOKit / IOMemoryDescriptor.h
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #ifndef _IOMEMORYDESCRIPTOR_H
26 #define _IOMEMORYDESCRIPTOR_H
27
28 #include <sys/cdefs.h>
29
30 #include <IOKit/IOTypes.h>
31 #include <libkern/c++/OSContainers.h>
32
33 __BEGIN_DECLS
34 #include <mach/memory_object_types.h>
35 __END_DECLS
36
37 struct IOPhysicalRange
38 {
39 IOPhysicalAddress address;
40 IOByteCount length;
41 };
42
43 class IOMemoryMap;
44 class IOMapper;
45
46 /*
47 * Direction of transfer, with respect to the described memory.
48 */
49 enum IODirection
50 {
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
56 };
57
58 /*
59 * IOOptionBits used in the second withRanges variant
60 */
61 enum {
62 kIOMemoryDirectionMask = 0x00000007,
63 kIOMemoryAutoPrepare = 0x00000008, // Shared with Buffer MD
64
65 kIOMemoryTypeVirtual = 0x00000010,
66 kIOMemoryTypePhysical = 0x00000020,
67 kIOMemoryTypeUPL = 0x00000030,
68 kIOMemoryTypeMask = 0x000000f0,
69
70 kIOMemoryAsReference = 0x00000100,
71 kIOMemoryBufferPageable = 0x00000400,
72 kIOMemoryDontMap = 0x00000800,
73 };
74
75 #define kIOMapperNone ((IOMapper *) -1)
76 #define kIOMapperSystem ((IOMapper *) 0)
77
78 /*! @class IOMemoryDescriptor : public OSObject
79 @abstract An abstract base class defining common methods for describing physical or virtual memory.
80 @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. */
81
82 class IOMemoryDescriptor : public OSObject
83 {
84 friend class _IOMemoryMap;
85 friend class IOSubMemoryDescriptor;
86
87 OSDeclareDefaultStructors(IOMemoryDescriptor);
88
89 protected:
90 /*! @struct ExpansionData
91 @discussion This structure will be used to expand the capablilties of this class in the future.
92 */
93 struct ExpansionData {
94 void * devicePager;
95 unsigned int pagerContig:1;
96 unsigned int unused:31;
97 IOMemoryDescriptor * memory;
98 };
99
100 /*! @var reserved
101 Reserved for future use. (Internal use only) */
102 ExpansionData * reserved;
103
104 protected:
105 OSSet * _mappings;
106 IOOptionBits _flags;
107 void * _memEntry;
108
109 IODirection _direction; /* DEPRECATED: use _flags instead. direction of transfer */
110 IOByteCount _length; /* length of all ranges */
111 IOOptionBits _tag;
112
113 public:
114
115 virtual IOPhysicalAddress getSourceSegment( IOByteCount offset,
116 IOByteCount * length );
117 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 0);
118
119 /*! @function initWithOptions
120 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
121 @discussion Note this function can be used to re-init a previously created memory descriptor.
122 @result true on success, false on failure. */
123 virtual bool initWithOptions(void * buffers,
124 UInt32 count,
125 UInt32 offset,
126 task_t task,
127 IOOptionBits options,
128 IOMapper * mapper = 0);
129 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 1);
130
131 virtual addr64_t IOMemoryDescriptor::getPhysicalSegment64( IOByteCount offset,
132 IOByteCount * length );
133 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 2);
134
135 private:
136
137 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 3);
138 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 4);
139 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 5);
140 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 6);
141 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 7);
142 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 8);
143 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 9);
144 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 10);
145 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 11);
146 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 12);
147 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 13);
148 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 14);
149 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 15);
150
151 protected:
152 virtual void free();
153 public:
154 static void initialize( void );
155
156 public:
157 /*! @function withAddress
158 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
159 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map.
160 @param address The virtual address of the first byte in the memory.
161 @param withLength The length of memory.
162 @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.
163 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
164
165 static IOMemoryDescriptor * withAddress(void * address,
166 IOByteCount withLength,
167 IODirection withDirection);
168
169 /*! @function withAddress
170 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
171 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
172 @param address The virtual address of the first byte in the memory.
173 @param withLength The length of memory.
174 @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.
175 @param withTask The task the virtual ranges are mapped into.
176 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
177
178 static IOMemoryDescriptor * withAddress(vm_address_t address,
179 IOByteCount withLength,
180 IODirection withDirection,
181 task_t withTask);
182
183 /*! @function withPhysicalAddress
184 @abstract Create an IOMemoryDescriptor to describe one physical range.
185 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
186 @param address The physical address of the first byte in the memory.
187 @param withLength The length of memory.
188 @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.
189 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
190
191 static IOMemoryDescriptor * withPhysicalAddress(
192 IOPhysicalAddress address,
193 IOByteCount withLength,
194 IODirection withDirection );
195
196 /*! @function withRanges
197 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
198 @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.
199 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
200 @param withCount The member count of the ranges array.
201 @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.
202 @param withTask The task each of the virtual ranges are mapped into.
203 @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.
204 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
205
206 static IOMemoryDescriptor * withRanges(IOVirtualRange * ranges,
207 UInt32 withCount,
208 IODirection withDirection,
209 task_t withTask,
210 bool asReference = false);
211
212 /*! @function withOptions
213 @abstract Master initialiser for all variants of memory descriptors.
214 @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.
215
216
217 @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.
218
219 @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.
220
221 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
222
223 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
224
225 @param options
226 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.
227 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.
228 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.
229 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.
230 kIOMemoryNoAutoPrepare Indicates that the temporary AutoPrepare of kernel_task memory should not be performed.
231
232 @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.
233
234 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
235
236 static IOMemoryDescriptor *withOptions(void * buffers,
237 UInt32 count,
238 UInt32 offset,
239 task_t task,
240 IOOptionBits options,
241 IOMapper * mapper = 0);
242
243 /*! @function withPhysicalRanges
244 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
245 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
246 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
247 @param withCount The member count of the ranges array.
248 @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.
249 @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.
250 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
251
252 static IOMemoryDescriptor * withPhysicalRanges(
253 IOPhysicalRange * ranges,
254 UInt32 withCount,
255 IODirection withDirection,
256 bool asReference = false);
257
258 /*! @function withSubRange
259 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
260 @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.
261 @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.
262 @param offset A byte offset into the parent memory descriptor's memory.
263 @param length The length of the subrange.
264 @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.
265 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
266
267 static IOMemoryDescriptor * withSubRange(IOMemoryDescriptor *of,
268 IOByteCount offset,
269 IOByteCount length,
270 IODirection withDirection);
271
272 /*! @function initWithAddress
273 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
274 @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.
275 @param address The virtual address of the first byte in the memory.
276 @param withLength The length of memory.
277 @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.
278 @result true on success, false on failure. */
279
280 virtual bool initWithAddress(void * address,
281 IOByteCount withLength,
282 IODirection withDirection) = 0;
283
284 /*! @function initWithAddress
285 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
286 @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.
287 @param address The virtual address of the first byte in the memory.
288 @param withLength The length of memory.
289 @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.
290 @param withTask The task the virtual ranges are mapped into.
291 @result true on success, false on failure. */
292
293 virtual bool initWithAddress(vm_address_t address,
294 IOByteCount withLength,
295 IODirection withDirection,
296 task_t withTask) = 0;
297
298 /*! @function initWithPhysicalAddress
299 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
300 @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.
301 @param address The physical address of the first byte in the memory.
302 @param withLength The length of memory.
303 @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.
304 @result true on success, false on failure. */
305
306 virtual bool initWithPhysicalAddress(
307 IOPhysicalAddress address,
308 IOByteCount withLength,
309 IODirection withDirection ) = 0;
310
311 /*! @function initWithRanges
312 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
313 @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.
314 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
315 @param withCount The member count of the ranges array.
316 @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.
317 @param withTask The task each of the virtual ranges are mapped into.
318 @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.
319 @result true on success, false on failure. */
320
321 virtual bool initWithRanges(IOVirtualRange * ranges,
322 UInt32 withCount,
323 IODirection withDirection,
324 task_t withTask,
325 bool asReference = false) = 0;
326
327 /*! @function initWithPhysicalRanges
328 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
329 @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.
330 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
331 @param withCount The member count of the ranges array.
332 @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.
333 @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.
334 @result true on success, false on failure. */
335
336 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
337 UInt32 withCount,
338 IODirection withDirection,
339 bool asReference = false) = 0;
340
341 /*! @function getDirection
342 @abstract Accessor to get the direction the memory descriptor was created with.
343 @discussion This method returns the direction the memory descriptor was created with.
344 @result The direction. */
345
346 virtual IODirection getDirection() const;
347
348 /*! @function getLength
349 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
350 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
351 @result The byte count. */
352
353 virtual IOByteCount getLength() const;
354
355 /*! @function setTag
356 @abstract Set the tag for the memory descriptor.
357 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
358 @param tag The tag. */
359
360 virtual void setTag( IOOptionBits tag );
361
362 /*! @function getTag
363 @abstract Accessor to the retrieve the tag for the memory descriptor.
364 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
365 @result The tag. */
366
367 virtual IOOptionBits getTag( void );
368
369 /*! @function readBytes
370 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
371 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
372 @param offset A byte offset into the memory descriptor's memory.
373 @param bytes The caller supplied buffer to copy the data to.
374 @param withLength The length of the data to copy.
375 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
376
377 virtual IOByteCount readBytes(IOByteCount offset,
378 void * bytes, IOByteCount withLength);
379
380 /*! @function writeBytes
381 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
382 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
383 @param offset A byte offset into the memory descriptor's memory.
384 @param bytes The caller supplied buffer to copy the data from.
385 @param withLength The length of the data to copy.
386 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
387
388 virtual IOByteCount writeBytes(IOByteCount offset,
389 const void * bytes, IOByteCount withLength);
390
391 /*! @function getPhysicalSegment
392 @abstract Break a memory descriptor into its physically contiguous segments.
393 @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.
394 @param offset A byte offset into the memory whose physical address to return.
395 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
396 @result A physical address, or zero if the offset is beyond the length of the memory. */
397
398 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
399 IOByteCount * length) = 0;
400
401 /*! @function getPhysicalAddress
402 @abstract Return the physical address of the first byte in the memory.
403 @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.
404 @result A physical address. */
405
406 /* inline */ IOPhysicalAddress getPhysicalAddress();
407 /* { return( getPhysicalSegment( 0, 0 )); } */
408
409 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
410 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
411 /* DEPRECATED */ IOByteCount * length) = 0;
412 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
413
414 /*! @function prepare
415 @abstract Prepare the memory for an I/O transfer.
416 @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.
417 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
418 @result An IOReturn code. */
419
420 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone) = 0;
421
422 /*! @function complete
423 @abstract Complete processing of the memory after an I/O transfer finishes.
424 @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.
425 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
426 @result An IOReturn code. */
427
428 virtual IOReturn complete(IODirection forDirection = kIODirectionNone) = 0;
429
430 /*
431 * Mapping functions.
432 */
433
434 /*! @function map
435 @abstract Maps a IOMemoryDescriptor into a task.
436 @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.
437 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
438 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
439 @param options Mapping options are defined in IOTypes.h,<br>
440 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>
441 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
442 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
443 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
444 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>
445 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
446 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
447 @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. */
448
449 virtual IOMemoryMap * map(
450 task_t intoTask,
451 IOVirtualAddress atAddress,
452 IOOptionBits options,
453 IOByteCount offset = 0,
454 IOByteCount length = 0 );
455
456 /*! @function map
457 @abstract Maps a IOMemoryDescriptor into the kernel map.
458 @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.
459 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
460 @result See the full version of the map method. */
461
462 virtual IOMemoryMap * map(
463 IOOptionBits options = 0 );
464
465 /*! @function setMapping
466 @abstract Establishes an already existing mapping.
467 @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.
468 @param task Address space in which the mapping exists.
469 @param mapAddress Virtual address of the mapping.
470 @param options Caching and read-only attributes of the mapping.
471 @result A IOMemoryMap object created to represent the mapping. */
472
473 virtual IOMemoryMap * setMapping(
474 task_t task,
475 IOVirtualAddress mapAddress,
476 IOOptionBits options = 0 );
477
478 // Following methods are private implementation
479
480 // make virtual
481 IOReturn redirect( task_t safeTask, bool redirect );
482
483 IOReturn handleFault(
484 void * pager,
485 vm_map_t addressMap,
486 IOVirtualAddress address,
487 IOByteCount sourceOffset,
488 IOByteCount length,
489 IOOptionBits options );
490
491 protected:
492 virtual IOMemoryMap * makeMapping(
493 IOMemoryDescriptor * owner,
494 task_t intoTask,
495 IOVirtualAddress atAddress,
496 IOOptionBits options,
497 IOByteCount offset,
498 IOByteCount length );
499
500 virtual void addMapping(
501 IOMemoryMap * mapping );
502
503 virtual void removeMapping(
504 IOMemoryMap * mapping );
505
506 virtual IOReturn doMap(
507 vm_map_t addressMap,
508 IOVirtualAddress * atAddress,
509 IOOptionBits options,
510 IOByteCount sourceOffset = 0,
511 IOByteCount length = 0 );
512
513 virtual IOReturn doUnmap(
514 vm_map_t addressMap,
515 IOVirtualAddress logical,
516 IOByteCount length );
517 };
518
519 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
520
521 /*! @class IOMemoryMap : public OSObject
522 @abstract An abstract base class defining common methods for describing a memory mapping.
523 @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. */
524
525 class IOMemoryMap : public OSObject
526 {
527 OSDeclareAbstractStructors(IOMemoryMap)
528
529 public:
530 /*! @function getVirtualAddress
531 @abstract Accessor to the virtual address of the first byte in the mapping.
532 @discussion This method returns the virtual address of the first byte in the mapping.
533 @result A virtual address. */
534
535 virtual IOVirtualAddress getVirtualAddress() = 0;
536
537 /*! @function getPhysicalSegment
538 @abstract Break a mapping into its physically contiguous segments.
539 @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.
540 @param offset A byte offset into the mapping whose physical address to return.
541 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
542 @result A physical address, or zero if the offset is beyond the length of the mapping. */
543
544 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
545 IOByteCount * length) = 0;
546
547 /*! @function getPhysicalAddress
548 @abstract Return the physical address of the first byte in the mapping.
549 @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.
550 @result A physical address. */
551
552 /* inline */ IOPhysicalAddress getPhysicalAddress();
553 /* { return( getPhysicalSegment( 0, 0 )); } */
554
555 /*! @function getLength
556 @abstract Accessor to the length of the mapping.
557 @discussion This method returns the length of the mapping.
558 @result A byte count. */
559
560 virtual IOByteCount getLength() = 0;
561
562 /*! @function getAddressTask
563 @abstract Accessor to the task of the mapping.
564 @discussion This method returns the mach task the mapping exists in.
565 @result A mach task_t. */
566
567 virtual task_t getAddressTask() = 0;
568
569 /*! @function getMemoryDescriptor
570 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
571 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
572 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
573
574 virtual IOMemoryDescriptor * getMemoryDescriptor() = 0;
575
576 /*! @function getMapOptions
577 @abstract Accessor to the options the mapping was created with.
578 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
579 @result Options for the mapping, including cache settings. */
580
581 virtual IOOptionBits getMapOptions() = 0;
582
583 /*! @function unmap
584 @abstract Force the IOMemoryMap to unmap, without destroying the object.
585 @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.
586 @result An IOReturn code. */
587
588 virtual IOReturn unmap() = 0;
589
590 virtual void taskDied() = 0;
591 };
592
593 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
594
595 // The following classes are private implementation of IOMemoryDescriptor - they
596 // should not be referenced directly, just through the public API's in the
597 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
598 // might be created by IOMemoryDescriptor::withAddress(), but there should be
599 // no need to reference as anything but a generic IOMemoryDescriptor *.
600
601 // Also these flags should not overlap with the options to
602 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
603
604 enum {
605 kIOMemoryPreparedReadOnly = 0x00008000,
606 };
607
608 class IOGeneralMemoryDescriptor : public IOMemoryDescriptor
609 {
610 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor);
611
612 protected:
613 union {
614 IOVirtualRange * v;
615 IOPhysicalRange * p;
616 } _ranges; /* list of address ranges */
617 unsigned _rangesCount; /* number of address ranges in list */
618 bool _rangesIsAllocated; /* is list allocated by us? */
619
620 task_t _task; /* task where all ranges are mapped to */
621
622 union {
623 IOVirtualRange v;
624 IOPhysicalRange p;
625 } _singleRange; /* storage space for a single range */
626
627 unsigned _wireCount; /* number of outstanding wires */
628
629 /* DEPRECATED */ vm_address_t _cachedVirtualAddress; /* a cached virtual-to-physical */
630
631 /* DEPRECATED */ IOPhysicalAddress _cachedPhysicalAddress;
632
633 bool _initialized; /* has superclass been initialized? */
634
635 virtual void free();
636
637
638 private:
639 // Internal API may be made virtual at some time in the future.
640 IOReturn wireVirtual(IODirection forDirection);
641
642 /* DEPRECATED */ IOByteCount _position; /* absolute position over all ranges */
643 /* DEPRECATED */ virtual void setPosition(IOByteCount position);
644
645 /*
646 * DEPRECATED IOByteCount _positionAtIndex; // relative position within range #n
647 *
648 * Re-use the _positionAtIndex as a count of the number of pages in
649 * this memory descriptor. Convieniently vm_address_t is an unsigned integer
650 * type so I can get away without having to change the type.
651 */
652 unsigned int _pages;
653
654 /* DEPRECATED */ unsigned _positionAtOffset; //range #n in which position is now
655
656 OSData *_memoryEntries;
657
658 /* DEPRECATED */ vm_offset_t _kernPtrAligned;
659 /* DEPRECATED */ unsigned _kernPtrAtIndex;
660 /* DEPRECATED */ IOByteCount _kernSize;
661
662 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex);
663 /* DEPRECATED */ virtual void unmapFromKernel();
664
665 public:
666 /*
667 * IOMemoryDescriptor required methods
668 */
669
670 // Master initaliser
671 virtual bool initWithOptions(void * buffers,
672 UInt32 count,
673 UInt32 offset,
674 task_t task,
675 IOOptionBits options,
676 IOMapper * mapper = 0);
677
678 // Secondary initialisers
679 virtual bool initWithAddress(void * address,
680 IOByteCount withLength,
681 IODirection withDirection);
682
683 virtual bool initWithAddress(vm_address_t address,
684 IOByteCount withLength,
685 IODirection withDirection,
686 task_t withTask);
687
688 virtual bool initWithPhysicalAddress(
689 IOPhysicalAddress address,
690 IOByteCount withLength,
691 IODirection withDirection );
692
693 virtual bool initWithRanges( IOVirtualRange * ranges,
694 UInt32 withCount,
695 IODirection withDirection,
696 task_t withTask,
697 bool asReference = false);
698
699 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
700 UInt32 withCount,
701 IODirection withDirection,
702 bool asReference = false);
703
704 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
705 IOByteCount * length);
706
707 virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
708 IOByteCount * length);
709
710 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
711 /* DEPRECATED */ IOByteCount * length);
712
713 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone);
714
715 virtual IOReturn complete(IODirection forDirection = kIODirectionNone);
716
717 virtual IOReturn doMap(
718 vm_map_t addressMap,
719 IOVirtualAddress * atAddress,
720 IOOptionBits options,
721 IOByteCount sourceOffset = 0,
722 IOByteCount length = 0 );
723
724 virtual IOReturn doUnmap(
725 vm_map_t addressMap,
726 IOVirtualAddress logical,
727 IOByteCount length );
728 virtual bool serialize(OSSerialize *s) const;
729 };
730
731 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
732
733 class IOSubMemoryDescriptor : public IOMemoryDescriptor
734 {
735 friend class IOMemoryDescriptor;
736
737 OSDeclareDefaultStructors(IOSubMemoryDescriptor);
738
739 protected:
740 IOMemoryDescriptor * _parent;
741 IOByteCount _start;
742
743 virtual void free();
744
745 virtual bool initWithAddress(void * address,
746 IOByteCount withLength,
747 IODirection withDirection);
748
749 virtual bool initWithAddress(vm_address_t address,
750 IOByteCount withLength,
751 IODirection withDirection,
752 task_t withTask);
753
754 virtual bool initWithPhysicalAddress(
755 IOPhysicalAddress address,
756 IOByteCount withLength,
757 IODirection withDirection );
758
759 virtual bool initWithRanges( IOVirtualRange * ranges,
760 UInt32 withCount,
761 IODirection withDirection,
762 task_t withTask,
763 bool asReference = false);
764
765 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
766 UInt32 withCount,
767 IODirection withDirection,
768 bool asReference = false);
769
770 IOMemoryDescriptor::withAddress;
771 IOMemoryDescriptor::withPhysicalAddress;
772 IOMemoryDescriptor::withPhysicalRanges;
773 IOMemoryDescriptor::withRanges;
774 IOMemoryDescriptor::withSubRange;
775
776 public:
777 /*
778 * Initialize or reinitialize an IOSubMemoryDescriptor to describe
779 * a subrange of an existing descriptor.
780 *
781 * An IOSubMemoryDescriptor can be re-used by calling initSubRange
782 * again on an existing instance -- note that this behavior is not
783 * commonly supported in other IOKit classes, although it is here.
784 */
785 virtual bool initSubRange( IOMemoryDescriptor * parent,
786 IOByteCount offset, IOByteCount length,
787 IODirection withDirection );
788
789 /*
790 * IOMemoryDescriptor required methods
791 */
792
793 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
794 IOByteCount * length);
795
796 virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
797 IOByteCount * length);
798
799 virtual IOByteCount readBytes(IOByteCount offset,
800 void * bytes, IOByteCount withLength);
801
802 virtual IOByteCount writeBytes(IOByteCount offset,
803 const void * bytes, IOByteCount withLength);
804
805 virtual void * getVirtualSegment(IOByteCount offset,
806 IOByteCount * length);
807
808 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone);
809
810 virtual IOReturn complete(IODirection forDirection = kIODirectionNone);
811
812 // make virtual
813 IOReturn redirect( task_t safeTask, bool redirect );
814
815 virtual bool serialize(OSSerialize *s) const;
816
817 protected:
818 virtual IOMemoryMap * makeMapping(
819 IOMemoryDescriptor * owner,
820 task_t intoTask,
821 IOVirtualAddress atAddress,
822 IOOptionBits options,
823 IOByteCount offset,
824 IOByteCount length );
825 };
826
827 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
828
829 #endif /* !_IOMEMORYDESCRIPTOR_H */