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