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