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