]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOMemoryDescriptor.h
bbcab96802443e7f767d00bec3a0d79611c5ff37
[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 <IOKit/IOTypes.h>
29 #include <libkern/c++/OSContainers.h>
30
31 struct IOPhysicalRange
32 {
33 IOPhysicalAddress address;
34 IOByteCount length;
35 };
36
37 class IOMemoryMap;
38
39 /*
40 * Direction of transfer, with respect to the described memory.
41 */
42 enum IODirection
43 {
44 kIODirectionNone = 0x0, // same as VM_PROT_NONE
45 kIODirectionIn = 0x1, // User land 'read', same as VM_PROT_READ
46 kIODirectionOut = 0x2, // User land 'write', same as VM_PROT_WRITE
47 kIODirectionOutIn = kIODirectionIn | kIODirectionOut,
48 };
49
50 /*! @class IOMemoryDescriptor : public OSObject
51 @abstract An abstract base class defining common methods for describing physical or virtual memory.
52 @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. */
53
54 class IOMemoryDescriptor : public OSObject
55 {
56 friend class _IOMemoryMap;
57 friend class IOSubMemoryDescriptor;
58
59 OSDeclareDefaultStructors(IOMemoryDescriptor);
60
61 protected:
62 /*! @struct ExpansionData
63 @discussion This structure will be used to expand the capablilties of this class in the future.
64 */
65 struct ExpansionData {
66 void * devicePager;
67 unsigned int pagerContig:1;
68 unsigned int unused:31;
69 IOMemoryDescriptor * memory;
70 };
71
72 /*! @var reserved
73 Reserved for future use. (Internal use only) */
74 ExpansionData * reserved;
75
76 protected:
77 OSSet * _mappings;
78 IOOptionBits _flags;
79 void * _memEntry;
80
81 IODirection _direction; /* direction of transfer */
82 IOByteCount _length; /* length of all ranges */
83 IOOptionBits _tag;
84
85 public:
86
87 virtual IOPhysicalAddress getSourceSegment( IOByteCount offset,
88 IOByteCount * length );
89
90 private:
91 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 0);
92 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 1);
93 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 2);
94 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 3);
95 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 4);
96 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 5);
97 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 6);
98 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 7);
99 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 8);
100 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 9);
101 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 10);
102 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 11);
103 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 12);
104 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 13);
105 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 14);
106 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 15);
107
108 protected:
109 virtual void free();
110 public:
111 static void initialize( void );
112
113 public:
114 /*! @function withAddress
115 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
116 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the kernel map.
117 @param address The virtual address of the first byte in the memory.
118 @param withLength The length of memory.
119 @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.
120 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
121
122 static IOMemoryDescriptor * withAddress(void * address,
123 IOByteCount withLength,
124 IODirection withDirection);
125
126 /*! @function withAddress
127 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
128 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single virtual memory range mapped into the specified map.
129 @param address The virtual address of the first byte in the memory.
130 @param withLength The length of memory.
131 @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.
132 @param withTask The task the virtual ranges are mapped into.
133 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
134
135 static IOMemoryDescriptor * withAddress(vm_address_t address,
136 IOByteCount withLength,
137 IODirection withDirection,
138 task_t withTask);
139
140 /*! @function withPhysicalAddress
141 @abstract Create an IOMemoryDescriptor to describe one physical range.
142 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
143 @param address The physical address of the first byte in the memory.
144 @param withLength The length of memory.
145 @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.
146 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
147
148 static IOMemoryDescriptor * withPhysicalAddress(
149 IOPhysicalAddress address,
150 IOByteCount withLength,
151 IODirection withDirection );
152
153 /*! @function withRanges
154 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
155 @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.
156 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
157 @param withCount The member count of the ranges array.
158 @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.
159 @param withTask The task each of the virtual ranges are mapped into.
160 @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.
161 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
162
163 static IOMemoryDescriptor * withRanges(IOVirtualRange * ranges,
164 UInt32 withCount,
165 IODirection withDirection,
166 task_t withTask,
167 bool asReference = false);
168
169 /*! @function withPhysicalRanges
170 @abstract Create an IOMemoryDescriptor to describe one or more physical ranges.
171 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of an array of physical memory ranges.
172 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
173 @param withCount The member count of the ranges array.
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 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.
176 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
177
178 static IOMemoryDescriptor * withPhysicalRanges(
179 IOPhysicalRange * ranges,
180 UInt32 withCount,
181 IODirection withDirection,
182 bool asReference = false);
183
184 /*! @function withSubRange
185 @abstract Create an IOMemoryDescriptor to describe a subrange of an existing descriptor.
186 @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.
187 @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.
188 @param offset A byte offset into the parent memory descriptor's memory.
189 @param length The length of the subrange.
190 @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.
191 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
192
193 static IOMemoryDescriptor * withSubRange(IOMemoryDescriptor * of,
194 IOByteCount offset,
195 IOByteCount length,
196 IODirection withDirection);
197
198 /*! @function initWithAddress
199 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the kernel task.
200 @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.
201 @param address The virtual address of the first byte in the memory.
202 @param withLength The length of memory.
203 @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.
204 @result true on success, false on failure. */
205
206 virtual bool initWithAddress(void * address,
207 IOByteCount withLength,
208 IODirection withDirection) = 0;
209
210 /*! @function initWithAddress
211 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one virtual range of the specified map.
212 @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.
213 @param address The virtual address of the first byte in the memory.
214 @param withLength The length of memory.
215 @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.
216 @param withTask The task the virtual ranges are mapped into.
217 @result true on success, false on failure. */
218
219 virtual bool initWithAddress(vm_address_t address,
220 IOByteCount withLength,
221 IODirection withDirection,
222 task_t withTask) = 0;
223
224 /*! @function initWithPhysicalAddress
225 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one physical range.
226 @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.
227 @param address The physical address of the first byte in the memory.
228 @param withLength The length of memory.
229 @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.
230 @result true on success, false on failure. */
231
232 virtual bool initWithPhysicalAddress(
233 IOPhysicalAddress address,
234 IOByteCount withLength,
235 IODirection withDirection ) = 0;
236
237 /*! @function initWithRanges
238 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more virtual ranges.
239 @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.
240 @param ranges An array of IOVirtualRange structures which specify the virtual ranges in the specified map which make up the memory to be described.
241 @param withCount The member count of the ranges array.
242 @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.
243 @param withTask The task each of the virtual ranges are mapped into.
244 @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.
245 @result true on success, false on failure. */
246
247 virtual bool initWithRanges( IOVirtualRange * ranges,
248 UInt32 withCount,
249 IODirection withDirection,
250 task_t withTask,
251 bool asReference = false) = 0;
252
253 /*! @function initWithPhysicalRanges
254 @abstract Initialize or reinitialize an IOMemoryDescriptor to describe one or more physical ranges.
255 @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.
256 @param ranges An array of IOPhysicalRange structures which specify the physical ranges which make up the memory to be described.
257 @param withCount The member count of the ranges array.
258 @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.
259 @param asReference If false, the IOMemoryDescriptor object will make a copy of the ranges array, otherwise, the array will be used in situ, avoiding an extra allocation.
260 @result true on success, false on failure. */
261
262 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
263 UInt32 withCount,
264 IODirection withDirection,
265 bool asReference = false) = 0;
266
267 /*! @function getDirection
268 @abstract Accessor to get the direction the memory descriptor was created with.
269 @discussion This method returns the direction the memory descriptor was created with.
270 @result The direction. */
271
272 virtual IODirection getDirection() const;
273
274 /*! @function getLength
275 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
276 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
277 @result The byte count. */
278
279 virtual IOByteCount getLength() const;
280
281 /*! @function setTag
282 @abstract Set the tag for the memory descriptor.
283 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
284 @param tag The tag. */
285
286 virtual void setTag( IOOptionBits tag );
287
288 /*! @function getTag
289 @abstract Accessor to the retrieve the tag for the memory descriptor.
290 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
291 @result The tag. */
292
293 virtual IOOptionBits getTag( void );
294
295 /*! @function readBytes
296 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
297 @discussion This method copies data from the memory descriptor's memory at the given offset, to the caller's buffer.
298 @param offset A byte offset into the memory descriptor's memory.
299 @param bytes The caller supplied buffer to copy the data to.
300 @param withLength The length of the data to copy.
301 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
302
303 virtual IOByteCount readBytes(IOByteCount offset,
304 void * bytes, IOByteCount withLength);
305
306 /*! @function writeBytes
307 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
308 @discussion This method copies data to the memory descriptor's memory at the given offset, from the caller's buffer.
309 @param offset A byte offset into the memory descriptor's memory.
310 @param bytes The caller supplied buffer to copy the data from.
311 @param withLength The length of the data to copy.
312 @result The number of bytes copied, zero will be returned if the specified offset is beyond the length of the descriptor. */
313
314 virtual IOByteCount writeBytes(IOByteCount offset,
315 const void * bytes, IOByteCount withLength);
316
317 /*! @function getPhysicalSegment
318 @abstract Break a memory descriptor into its physically contiguous segments.
319 @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.
320 @param offset A byte offset into the memory whose physical address to return.
321 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
322 @result A physical address, or zero if the offset is beyond the length of the memory. */
323
324 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
325 IOByteCount * length) = 0;
326
327 /*! @function getPhysicalAddress
328 @abstract Return the physical address of the first byte in the memory.
329 @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.
330 @result A physical address. */
331
332 /* inline */ IOPhysicalAddress getPhysicalAddress();
333 /* { return( getPhysicalSegment( 0, 0 )); } */
334
335 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
336 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
337 /* DEPRECATED */ IOByteCount * length) = 0;
338 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
339
340 /*! @function prepare
341 @abstract Prepare the memory for an I/O transfer.
342 @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. This method needn't called for non-pageable memory.
343 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
344 @result An IOReturn code. */
345
346 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone) = 0;
347
348 /*! @function complete
349 @abstract Complete processing of the memory after an I/O transfer finishes.
350 @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.
351 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
352 @result An IOReturn code. */
353
354 virtual IOReturn complete(IODirection forDirection = kIODirectionNone) = 0;
355
356 /*
357 * Mapping functions.
358 */
359
360 /*! @function map
361 @abstract Maps a IOMemoryDescriptor into a task.
362 @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.
363 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
364 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
365 @param options Mapping options are defined in IOTypes.h,<br>
366 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>
367 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
368 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
369 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
370 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>
371 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
372 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
373 @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. */
374
375 virtual IOMemoryMap * map(
376 task_t intoTask,
377 IOVirtualAddress atAddress,
378 IOOptionBits options,
379 IOByteCount offset = 0,
380 IOByteCount length = 0 );
381
382 /*! @function map
383 @abstract Maps a IOMemoryDescriptor into the kernel map.
384 @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.
385 @param options Mapping options as in the full version of the map method, with kIOMapAnywhere assumed.
386 @result See the full version of the map method. */
387
388 virtual IOMemoryMap * map(
389 IOOptionBits options = 0 );
390
391 /*! @function setMapping
392 @abstract Establishes an already existing mapping.
393 @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.
394 @param task Address space in which the mapping exists.
395 @param mapAddress Virtual address of the mapping.
396 @param options Caching and read-only attributes of the mapping.
397 @result A IOMemoryMap object created to represent the mapping. */
398
399 virtual IOMemoryMap * setMapping(
400 task_t task,
401 IOVirtualAddress mapAddress,
402 IOOptionBits options = 0 );
403
404 // Following methods are private implementation
405
406 // make virtual
407 IOReturn redirect( task_t safeTask, bool redirect );
408
409 IOReturn handleFault(
410 void * pager,
411 vm_map_t addressMap,
412 IOVirtualAddress address,
413 IOByteCount sourceOffset,
414 IOByteCount length,
415 IOOptionBits options );
416
417 protected:
418 virtual IOMemoryMap * makeMapping(
419 IOMemoryDescriptor * owner,
420 task_t intoTask,
421 IOVirtualAddress atAddress,
422 IOOptionBits options,
423 IOByteCount offset,
424 IOByteCount length );
425
426 virtual void addMapping(
427 IOMemoryMap * mapping );
428
429 virtual void removeMapping(
430 IOMemoryMap * mapping );
431
432 virtual IOReturn doMap(
433 vm_map_t addressMap,
434 IOVirtualAddress * atAddress,
435 IOOptionBits options,
436 IOByteCount sourceOffset = 0,
437 IOByteCount length = 0 );
438
439 virtual IOReturn doUnmap(
440 vm_map_t addressMap,
441 IOVirtualAddress logical,
442 IOByteCount length );
443 };
444
445 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
446
447 /*! @class IOMemoryMap : public OSObject
448 @abstract An abstract base class defining common methods for describing a memory mapping.
449 @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. */
450
451 class IOMemoryMap : public OSObject
452 {
453 OSDeclareAbstractStructors(IOMemoryMap)
454
455 public:
456 /*! @function getVirtualAddress
457 @abstract Accessor to the virtual address of the first byte in the mapping.
458 @discussion This method returns the virtual address of the first byte in the mapping.
459 @result A virtual address. */
460
461 virtual IOVirtualAddress getVirtualAddress() = 0;
462
463 /*! @function getPhysicalSegment
464 @abstract Break a mapping into its physically contiguous segments.
465 @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.
466 @param offset A byte offset into the mapping whose physical address to return.
467 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
468 @result A physical address, or zero if the offset is beyond the length of the mapping. */
469
470 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
471 IOByteCount * length) = 0;
472
473 /*! @function getPhysicalAddress
474 @abstract Return the physical address of the first byte in the mapping.
475 @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.
476 @result A physical address. */
477
478 /* inline */ IOPhysicalAddress getPhysicalAddress();
479 /* { return( getPhysicalSegment( 0, 0 )); } */
480
481 /*! @function getLength
482 @abstract Accessor to the length of the mapping.
483 @discussion This method returns the length of the mapping.
484 @result A byte count. */
485
486 virtual IOByteCount getLength() = 0;
487
488 /*! @function getAddressTask
489 @abstract Accessor to the task of the mapping.
490 @discussion This method returns the mach task the mapping exists in.
491 @result A mach task_t. */
492
493 virtual task_t getAddressTask() = 0;
494
495 /*! @function getMemoryDescriptor
496 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
497 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
498 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
499
500 virtual IOMemoryDescriptor * getMemoryDescriptor() = 0;
501
502 /*! @function getMapOptions
503 @abstract Accessor to the options the mapping was created with.
504 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
505 @result Options for the mapping, including cache settings. */
506
507 virtual IOOptionBits getMapOptions() = 0;
508
509 /*! @function unmap
510 @abstract Force the IOMemoryMap to unmap, without destroying the object.
511 @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.
512 @result An IOReturn code. */
513
514 virtual IOReturn unmap() = 0;
515
516 virtual void taskDied() = 0;
517 };
518
519 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
520
521 // The following classes are private implementation of IOMemoryDescriptor - they
522 // should not be referenced directly, just through the public API's in the
523 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
524 // might be created by IOMemoryDescriptor::withAddress(), but there should be
525 // no need to reference as anything but a generic IOMemoryDescriptor *.
526
527 enum {
528 kIOMemoryRequiresWire = 0x00000001
529 };
530
531 class IOGeneralMemoryDescriptor : public IOMemoryDescriptor
532 {
533 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor);
534
535 protected:
536 union {
537 IOVirtualRange * v;
538 IOPhysicalRange * p;
539 } _ranges; /* list of address ranges */
540 unsigned _rangesCount; /* number of address ranges in list */
541 bool _rangesIsAllocated; /* is list allocated by us? */
542
543 task_t _task; /* task where all ranges are mapped to */
544
545 union {
546 IOVirtualRange v;
547 IOPhysicalRange p;
548 } _singleRange; /* storage space for a single range */
549
550 unsigned _wireCount; /* number of outstanding wires */
551
552 vm_address_t _cachedVirtualAddress; /* a cached virtual-to-physical */
553 IOPhysicalAddress _cachedPhysicalAddress; /* mapping, for optimization */
554
555 bool _initialized; /* has superclass been initialized? */
556
557 virtual void free();
558
559 protected:
560 /* DEPRECATED */ IOByteCount _position; /* absolute position over all ranges */
561 /* DEPRECATED */ virtual void setPosition(IOByteCount position);
562
563 private:
564 /* DEPRECATED */ unsigned _positionAtIndex; /* range #n in which position is now */
565 /* DEPRECATED */ IOByteCount _positionAtOffset; /* relative position within range #n */
566 OSData *_memoryEntries;
567
568 /* DEPRECATED */ vm_offset_t _kernPtrAligned;
569 /* DEPRECATED */ unsigned _kernPtrAtIndex;
570 /* DEPRECATED */ IOByteCount _kernSize;
571 /* DEPRECATED */ virtual void mapIntoKernel(unsigned rangeIndex);
572 /* DEPRECATED */ virtual void unmapFromKernel();
573 inline vm_map_t getMapForTask( task_t task, vm_address_t address );
574
575 public:
576 /*
577 * IOMemoryDescriptor required methods
578 */
579
580 virtual bool initWithAddress(void * address,
581 IOByteCount withLength,
582 IODirection withDirection);
583
584 virtual bool initWithAddress(vm_address_t address,
585 IOByteCount withLength,
586 IODirection withDirection,
587 task_t withTask);
588
589 virtual bool initWithPhysicalAddress(
590 IOPhysicalAddress address,
591 IOByteCount withLength,
592 IODirection withDirection );
593
594 virtual bool initWithRanges( IOVirtualRange * ranges,
595 UInt32 withCount,
596 IODirection withDirection,
597 task_t withTask,
598 bool asReference = false);
599
600 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
601 UInt32 withCount,
602 IODirection withDirection,
603 bool asReference = false);
604
605 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
606 IOByteCount * length);
607
608 virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
609 IOByteCount * length);
610
611 /* DEPRECATED */ virtual void * getVirtualSegment(IOByteCount offset,
612 /* DEPRECATED */ IOByteCount * length);
613
614 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone);
615
616 virtual IOReturn complete(IODirection forDirection = kIODirectionNone);
617
618 virtual IOReturn doMap(
619 vm_map_t addressMap,
620 IOVirtualAddress * atAddress,
621 IOOptionBits options,
622 IOByteCount sourceOffset = 0,
623 IOByteCount length = 0 );
624
625 virtual IOReturn doUnmap(
626 vm_map_t addressMap,
627 IOVirtualAddress logical,
628 IOByteCount length );
629 virtual bool serialize(OSSerialize *s) const;
630 };
631
632 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
633
634 class IOSubMemoryDescriptor : public IOMemoryDescriptor
635 {
636 friend class IOMemoryDescriptor;
637
638 OSDeclareDefaultStructors(IOSubMemoryDescriptor);
639
640 protected:
641 IOMemoryDescriptor * _parent;
642 IOByteCount _start;
643
644 virtual void free();
645
646 virtual bool initSubRange( IOMemoryDescriptor * parent,
647 IOByteCount offset, IOByteCount length,
648 IODirection withDirection );
649
650 virtual bool initWithAddress(void * address,
651 IOByteCount withLength,
652 IODirection withDirection);
653
654 virtual bool initWithAddress(vm_address_t address,
655 IOByteCount withLength,
656 IODirection withDirection,
657 task_t withTask);
658
659 virtual bool initWithPhysicalAddress(
660 IOPhysicalAddress address,
661 IOByteCount withLength,
662 IODirection withDirection );
663
664 virtual bool initWithRanges( IOVirtualRange * ranges,
665 UInt32 withCount,
666 IODirection withDirection,
667 task_t withTask,
668 bool asReference = false);
669
670 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
671 UInt32 withCount,
672 IODirection withDirection,
673 bool asReference = false);
674
675 IOMemoryDescriptor::withAddress;
676 IOMemoryDescriptor::withPhysicalAddress;
677 IOMemoryDescriptor::withPhysicalRanges;
678 IOMemoryDescriptor::withRanges;
679 IOMemoryDescriptor::withSubRange;
680
681 public:
682 /*
683 * IOMemoryDescriptor required methods
684 */
685
686 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
687 IOByteCount * length);
688
689 virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
690 IOByteCount * length);
691
692 virtual IOByteCount readBytes(IOByteCount offset,
693 void * bytes, IOByteCount withLength);
694
695 virtual IOByteCount writeBytes(IOByteCount offset,
696 const void * bytes, IOByteCount withLength);
697
698 virtual void * getVirtualSegment(IOByteCount offset,
699 IOByteCount * length);
700
701 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone);
702
703 virtual IOReturn complete(IODirection forDirection = kIODirectionNone);
704
705 // make virtual
706 IOReturn redirect( task_t safeTask, bool redirect );
707
708 virtual bool serialize(OSSerialize *s) const;
709
710 protected:
711 virtual IOMemoryMap * makeMapping(
712 IOMemoryDescriptor * owner,
713 task_t intoTask,
714 IOVirtualAddress atAddress,
715 IOOptionBits options,
716 IOByteCount offset,
717 IOByteCount length );
718 };
719
720 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
721
722 #endif /* !_IOMEMORYDESCRIPTOR_H */