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