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