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