]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOMemoryDescriptor.h
xnu-4570.1.46.tar.gz
[apple/xnu.git] / iokit / IOKit / IOMemoryDescriptor.h
1 /*
2 * Copyright (c) 1998-2016 Apple 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 #ifdef XNU_KERNEL_PRIVATE
37 #include <IOKit/IOKitDebug.h>
38 #endif
39
40 #include <mach/memory_object_types.h>
41
42 class IOMemoryMap;
43 class IOMapper;
44 class IOService;
45 class IODMACommand;
46
47 /*
48 * Direction of transfer, with respect to the described memory.
49 */
50 #ifdef __LP64__
51 enum
52 #else /* !__LP64__ */
53 enum IODirection
54 #endif /* !__LP64__ */
55 {
56 kIODirectionNone = 0x0, // same as VM_PROT_NONE
57 kIODirectionIn = 0x1, // User land 'read', same as VM_PROT_READ
58 kIODirectionOut = 0x2, // User land 'write', same as VM_PROT_WRITE
59 kIODirectionOutIn = kIODirectionOut | kIODirectionIn,
60 kIODirectionInOut = kIODirectionIn | kIODirectionOut,
61
62 // these flags are valid for the prepare() method only
63 kIODirectionPrepareToPhys32 = 0x00000004,
64 kIODirectionPrepareNoFault = 0x00000008,
65 kIODirectionPrepareReserved1 = 0x00000010,
66 #define IODIRECTIONPREPARENONCOHERENTDEFINED 1
67 kIODirectionPrepareNonCoherent = 0x00000020,
68
69 // these flags are valid for the complete() method only
70 #define IODIRECTIONCOMPLETEWITHERRORDEFINED 1
71 kIODirectionCompleteWithError = 0x00000040,
72 #define IODIRECTIONCOMPLETEWITHDATAVALIDDEFINED 1
73 kIODirectionCompleteWithDataValid = 0x00000080,
74 };
75
76 #ifdef __LP64__
77 typedef IOOptionBits IODirection;
78 #endif /* __LP64__ */
79
80 /*
81 * IOOptionBits used in the withOptions variant
82 */
83 enum {
84 kIOMemoryDirectionMask = 0x00000007,
85 #ifdef XNU_KERNEL_PRIVATE
86 kIOMemoryAutoPrepare = 0x00000008, // Shared with Buffer MD
87 #endif
88
89 kIOMemoryTypeVirtual = 0x00000010,
90 kIOMemoryTypePhysical = 0x00000020,
91 kIOMemoryTypeUPL = 0x00000030,
92 kIOMemoryTypePersistentMD = 0x00000040, // Persistent Memory Descriptor
93 kIOMemoryTypeUIO = 0x00000050,
94 #ifdef __LP64__
95 kIOMemoryTypeVirtual64 = kIOMemoryTypeVirtual,
96 kIOMemoryTypePhysical64 = kIOMemoryTypePhysical,
97 #else /* !__LP64__ */
98 kIOMemoryTypeVirtual64 = 0x00000060,
99 kIOMemoryTypePhysical64 = 0x00000070,
100 #endif /* !__LP64__ */
101 kIOMemoryTypeMask = 0x000000f0,
102
103 kIOMemoryAsReference = 0x00000100,
104 kIOMemoryBufferPageable = 0x00000400,
105 kIOMemoryMapperNone = 0x00000800, // Shared with Buffer MD
106 kIOMemoryHostOnly = 0x00001000, // Never DMA accessible
107 #ifdef XNU_KERNEL_PRIVATE
108 kIOMemoryRedirected = 0x00004000,
109 kIOMemoryPreparedReadOnly = 0x00008000,
110 #endif
111 kIOMemoryPersistent = 0x00010000,
112 #ifdef XNU_KERNEL_PRIVATE
113 kIOMemoryMapCopyOnWrite = 0x00020000,
114 #endif
115 kIOMemoryRemote = 0x00040000,
116 kIOMemoryThreadSafe = 0x00100000, // Shared with Buffer MD
117 kIOMemoryClearEncrypt = 0x00200000, // Shared with Buffer MD
118 kIOMemoryUseReserve = 0x00800000, // Shared with Buffer MD
119 #define IOMEMORYUSERESERVEDEFINED 1
120
121 #ifdef XNU_KERNEL_PRIVATE
122 kIOMemoryBufferPurgeable = 0x00400000,
123 kIOMemoryBufferCacheMask = 0x70000000,
124 kIOMemoryBufferCacheShift = 28,
125 #endif
126 };
127
128 #define kIOMapperSystem ((IOMapper *) 0)
129
130 enum
131 {
132 kIOMemoryPurgeableKeepCurrent = 1,
133
134 kIOMemoryPurgeableNonVolatile = 2,
135 kIOMemoryPurgeableVolatile = 3,
136 kIOMemoryPurgeableEmpty = 4,
137
138 // modifiers for kIOMemoryPurgeableVolatile behavior
139 kIOMemoryPurgeableVolatileGroup0 = VM_VOLATILE_GROUP_0,
140 kIOMemoryPurgeableVolatileGroup1 = VM_VOLATILE_GROUP_1,
141 kIOMemoryPurgeableVolatileGroup2 = VM_VOLATILE_GROUP_2,
142 kIOMemoryPurgeableVolatileGroup3 = VM_VOLATILE_GROUP_3,
143 kIOMemoryPurgeableVolatileGroup4 = VM_VOLATILE_GROUP_4,
144 kIOMemoryPurgeableVolatileGroup5 = VM_VOLATILE_GROUP_5,
145 kIOMemoryPurgeableVolatileGroup6 = VM_VOLATILE_GROUP_6,
146 kIOMemoryPurgeableVolatileGroup7 = VM_VOLATILE_GROUP_7,
147 kIOMemoryPurgeableVolatileBehaviorFifo = VM_PURGABLE_BEHAVIOR_FIFO,
148 kIOMemoryPurgeableVolatileBehaviorLifo = VM_PURGABLE_BEHAVIOR_LIFO,
149 kIOMemoryPurgeableVolatileOrderingObsolete = VM_PURGABLE_ORDERING_OBSOLETE,
150 kIOMemoryPurgeableVolatileOrderingNormal = VM_PURGABLE_ORDERING_NORMAL,
151 kIOMemoryPurgeableFaultOnAccess = VM_PURGABLE_DEBUG_FAULT,
152 };
153 enum
154 {
155 kIOMemoryIncoherentIOFlush = 1,
156 kIOMemoryIncoherentIOStore = 2,
157
158 kIOMemoryClearEncrypted = 50,
159 kIOMemorySetEncrypted = 51,
160 };
161
162 #define IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND 1
163
164 struct IODMAMapSpecification
165 {
166 uint64_t alignment;
167 IOService * device;
168 uint32_t options;
169 uint8_t numAddressBits;
170 uint8_t resvA[3];
171 uint32_t resvB[4];
172 };
173
174 struct IODMAMapPageList
175 {
176 uint32_t pageOffset;
177 uint32_t pageListCount;
178 const upl_page_info_t * pageList;
179 };
180
181 // mapOptions for iovmMapMemory
182 enum
183 {
184 kIODMAMapReadAccess = 0x00000001,
185 kIODMAMapWriteAccess = 0x00000002,
186 kIODMAMapPhysicallyContiguous = 0x00000010,
187 kIODMAMapDeviceMemory = 0x00000020,
188 kIODMAMapPagingPath = 0x00000040,
189 kIODMAMapIdentityMap = 0x00000080,
190
191 kIODMAMapPageListFullyOccupied = 0x00000100,
192 kIODMAMapFixedAddress = 0x00000200,
193 };
194
195 #ifdef KERNEL_PRIVATE
196
197 // Used for dmaCommandOperation communications for IODMACommand and mappers
198
199 enum {
200 kIOMDWalkSegments = 0x01000000,
201 kIOMDFirstSegment = 1 | kIOMDWalkSegments,
202 kIOMDGetCharacteristics = 0x02000000,
203 kIOMDGetCharacteristicsMapped = 1 | kIOMDGetCharacteristics,
204 kIOMDDMAActive = 0x03000000,
205 kIOMDSetDMAActive = 1 | kIOMDDMAActive,
206 kIOMDSetDMAInactive = kIOMDDMAActive,
207 kIOMDAddDMAMapSpec = 0x04000000,
208 kIOMDDMAMap = 0x05000000,
209 kIOMDDMAUnmap = 0x06000000,
210 kIOMDDMACommandOperationMask = 0xFF000000,
211 };
212 struct IOMDDMACharacteristics {
213 UInt64 fLength;
214 UInt32 fSGCount;
215 UInt32 fPages;
216 UInt32 fPageAlign;
217 ppnum_t fHighestPage;
218 IODirection fDirection;
219 UInt8 fIsPrepared;
220 };
221
222 struct IOMDDMAMapArgs {
223 IOMapper * fMapper;
224 IODMACommand * fCommand;
225 IODMAMapSpecification fMapSpec;
226 uint64_t fOffset;
227 uint64_t fLength;
228 uint64_t fAlloc;
229 uint64_t fAllocLength;
230 uint8_t fMapContig;
231 };
232
233 struct IOMDDMAWalkSegmentArgs {
234 UInt64 fOffset; // Input/Output offset
235 UInt64 fIOVMAddr, fLength; // Output variables
236 UInt8 fMapped; // Input Variable, Require mapped IOVMA
237 };
238 typedef UInt8 IOMDDMAWalkSegmentState[128];
239
240 #endif /* KERNEL_PRIVATE */
241
242 enum
243 {
244 kIOPreparationIDUnprepared = 0,
245 kIOPreparationIDUnsupported = 1,
246 kIOPreparationIDAlwaysPrepared = 2,
247 };
248
249 #ifdef XNU_KERNEL_PRIVATE
250 struct IOMemoryReference;
251 #endif
252
253
254 /*! @class IOMemoryDescriptor : public OSObject
255 @abstract An abstract base class defining common methods for describing physical or virtual memory.
256 @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. */
257
258 class IOMemoryDescriptor : public OSObject
259 {
260 friend class IOMemoryMap;
261 friend class IOMultiMemoryDescriptor;
262
263 OSDeclareDefaultStructors(IOMemoryDescriptor);
264
265 protected:
266
267 /*! @var reserved
268 Reserved for future use. (Internal use only) */
269 struct IOMemoryDescriptorReserved * reserved;
270
271 protected:
272 OSSet * _mappings;
273 IOOptionBits _flags;
274
275
276 #ifdef XNU_KERNEL_PRIVATE
277 public:
278 struct IOMemoryReference * _memRef;
279 vm_tag_t _kernelTag;
280 vm_tag_t _userTag;
281 int16_t _dmaReferences;
282 uint16_t _internalFlags;
283 kern_allocation_name_t _mapName;
284 protected:
285 #else /* XNU_KERNEL_PRIVATE */
286 void * __iomd_reserved5;
287 uint16_t __iomd_reserved1[4];
288 uintptr_t __iomd_reserved2;
289 #endif /* XNU_KERNEL_PRIVATE */
290
291 uintptr_t __iomd_reserved3;
292 uintptr_t __iomd_reserved4;
293
294 #ifndef __LP64__
295 IODirection _direction; /* use _flags instead */
296 #endif /* !__LP64__ */
297 IOByteCount _length; /* length of all ranges */
298 IOOptionBits _tag;
299
300 public:
301 typedef IOOptionBits DMACommandOps;
302 #ifndef __LP64__
303 virtual IOPhysicalAddress getSourceSegment( IOByteCount offset,
304 IOByteCount * length ) APPLE_KEXT_DEPRECATED;
305 #endif /* !__LP64__ */
306
307 /*! @function initWithOptions
308 @abstract Master initialiser for all variants of memory descriptors. For a more complete description see IOMemoryDescriptor::withOptions.
309 @discussion Note this function can be used to re-init a previously created memory descriptor.
310 @result true on success, false on failure. */
311 virtual bool initWithOptions(void * buffers,
312 UInt32 count,
313 UInt32 offset,
314 task_t task,
315 IOOptionBits options,
316 IOMapper * mapper = kIOMapperSystem);
317
318 #ifndef __LP64__
319 virtual addr64_t getPhysicalSegment64( IOByteCount offset,
320 IOByteCount * length ) APPLE_KEXT_DEPRECATED; /* use getPhysicalSegment() and kIOMemoryMapperNone instead */
321 #endif /* !__LP64__ */
322
323 /*! @function setPurgeable
324 @abstract Control the purgeable status of a memory descriptors memory.
325 @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.
326 @param newState - the desired new purgeable state of the memory:<br>
327 kIOMemoryPurgeableKeepCurrent - make no changes to the memory's purgeable state.<br>
328 kIOMemoryPurgeableVolatile - make the memory volatile - the memory may be reclaimed by the VM system without saving its contents to backing store.<br>
329 kIOMemoryPurgeableNonVolatile - make the memory nonvolatile - the memory is treated as with usual allocations and must be saved to backing store if paged.<br>
330 kIOMemoryPurgeableEmpty - make the memory volatile, and discard any pages allocated to it.
331 @param oldState - if non-NULL, the previous purgeable state of the memory is returned here:<br>
332 kIOMemoryPurgeableNonVolatile - the memory was nonvolatile.<br>
333 kIOMemoryPurgeableVolatile - the memory was volatile but its content has not been discarded by the VM system.<br>
334 kIOMemoryPurgeableEmpty - the memory was volatile and has been discarded by the VM system.<br>
335 @result An IOReturn code. */
336
337 virtual IOReturn setPurgeable( IOOptionBits newState,
338 IOOptionBits * oldState );
339
340
341 /*! @function getPageCounts
342 @abstract Retrieve the number of resident and/or dirty pages encompassed by an IOMemoryDescriptor.
343 @discussion This method returns the number of resident and/or dirty pages encompassed by an IOMemoryDescriptor.
344 @param residentPageCount - If non-null, a pointer to a byte count that will return the number of resident pages encompassed by this IOMemoryDescriptor.
345 @param dirtyPageCount - If non-null, a pointer to a byte count that will return the number of dirty pages encompassed by this IOMemoryDescriptor.
346 @result An IOReturn code. */
347
348 IOReturn getPageCounts( IOByteCount * residentPageCount,
349 IOByteCount * dirtyPageCount);
350
351 /*! @function performOperation
352 @abstract Perform an operation on the memory descriptor's memory.
353 @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.
354 @param options The operation to perform on the memory:<br>
355 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>
356 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.
357 @param offset A byte offset into the memory descriptor's memory.
358 @param length The length of the data range.
359 @result An IOReturn code. */
360
361 virtual IOReturn performOperation( IOOptionBits options,
362 IOByteCount offset, IOByteCount length );
363
364 // Used for dedicated communications for IODMACommand
365 virtual IOReturn dmaCommandOperation(DMACommandOps op, void *vData, UInt dataSize) const;
366
367 /*! @function getPhysicalSegment
368 @abstract Break a memory descriptor into its physically contiguous segments.
369 @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.
370 @param offset A byte offset into the memory whose physical address to return.
371 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
372 @result A physical address, or zero if the offset is beyond the length of the memory. */
373
374 #ifdef __LP64__
375 virtual addr64_t getPhysicalSegment( IOByteCount offset,
376 IOByteCount * length,
377 IOOptionBits options = 0 ) = 0;
378 #else /* !__LP64__ */
379 virtual addr64_t getPhysicalSegment( IOByteCount offset,
380 IOByteCount * length,
381 IOOptionBits options );
382 #endif /* !__LP64__ */
383
384 virtual uint64_t getPreparationID( void );
385 void setPreparationID( void );
386
387 #ifdef XNU_KERNEL_PRIVATE
388 IOMemoryDescriptorReserved * getKernelReserved( void );
389 IOReturn dmaMap(
390 IOMapper * mapper,
391 IODMACommand * command,
392 const IODMAMapSpecification * mapSpec,
393 uint64_t offset,
394 uint64_t length,
395 uint64_t * mapAddress,
396 uint64_t * mapLength);
397 IOReturn dmaUnmap(
398 IOMapper * mapper,
399 IODMACommand * command,
400 uint64_t offset,
401 uint64_t mapAddress,
402 uint64_t mapLength);
403 void dmaMapRecord(
404 IOMapper * mapper,
405 IODMACommand * command,
406 uint64_t mapLength);
407
408 void setVMTags(vm_tag_t kernelTag, vm_tag_t userTag);
409 vm_tag_t getVMTag(vm_map_t map);
410 #endif
411
412 private:
413 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 0);
414 #ifdef __LP64__
415 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 1);
416 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 2);
417 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 3);
418 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 4);
419 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 5);
420 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 6);
421 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 7);
422 #else /* !__LP64__ */
423 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 1);
424 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 2);
425 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 3);
426 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 4);
427 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 5);
428 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 6);
429 OSMetaClassDeclareReservedUsed(IOMemoryDescriptor, 7);
430 #endif /* !__LP64__ */
431 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 8);
432 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 9);
433 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 10);
434 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 11);
435 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 12);
436 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 13);
437 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 14);
438 OSMetaClassDeclareReservedUnused(IOMemoryDescriptor, 15);
439
440 protected:
441 virtual void free() APPLE_KEXT_OVERRIDE;
442 public:
443 static void initialize( void );
444
445 public:
446 /*! @function withAddress
447 @abstract Create an IOMemoryDescriptor to describe one virtual range of the kernel task.
448 @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.
449 @param address The virtual address of the first byte in the memory.
450 @param withLength The length of memory.
451 @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.
452 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
453
454 static IOMemoryDescriptor * withAddress(void * address,
455 IOByteCount withLength,
456 IODirection withDirection);
457
458 #ifndef __LP64__
459 static IOMemoryDescriptor * withAddress(IOVirtualAddress address,
460 IOByteCount withLength,
461 IODirection withDirection,
462 task_t withTask) APPLE_KEXT_DEPRECATED; /* use withAddressRange() and prepare() instead */
463 #endif /* !__LP64__ */
464
465 /*! @function withPhysicalAddress
466 @abstract Create an IOMemoryDescriptor to describe one physical range.
467 @discussion This method creates and initializes an IOMemoryDescriptor for memory consisting of a single physical memory range.
468 @param address The physical address of the first byte in the memory.
469 @param withLength The length of memory.
470 @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.
471 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
472
473 static IOMemoryDescriptor * withPhysicalAddress(
474 IOPhysicalAddress address,
475 IOByteCount withLength,
476 IODirection withDirection );
477
478 #ifndef __LP64__
479 static IOMemoryDescriptor * withRanges(IOVirtualRange * ranges,
480 UInt32 withCount,
481 IODirection withDirection,
482 task_t withTask,
483 bool asReference = false) APPLE_KEXT_DEPRECATED; /* use withAddressRanges() instead */
484 #endif /* !__LP64__ */
485
486 /*! @function withAddressRange
487 @abstract Create an IOMemoryDescriptor to describe one virtual range of the specified map.
488 @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.
489 @param address The virtual address of the first byte in the memory.
490 @param length The length of memory.
491 @param options
492 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.
493 @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.
494 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
495
496 static IOMemoryDescriptor * withAddressRange(
497 mach_vm_address_t address,
498 mach_vm_size_t length,
499 IOOptionBits options,
500 task_t task);
501
502 /*! @function withAddressRanges
503 @abstract Create an IOMemoryDescriptor to describe one or more virtual ranges.
504 @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.
505 @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.
506 @param rangeCount The member count of the ranges array.
507 @param options
508 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.
509 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.
510 @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.
511 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
512
513 static IOMemoryDescriptor * withAddressRanges(
514 IOAddressRange * ranges,
515 UInt32 rangeCount,
516 IOOptionBits options,
517 task_t task);
518
519 /*! @function withOptions
520 @abstract Master initialiser for all variants of memory descriptors.
521 @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.
522
523
524 @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.
525
526 @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.
527
528 @param offset Only used when options:type = UPL, in which case this field contains an offset for the memory within the buffers upl.
529
530 @param task Only used options:type = Virtual, The task each of the virtual ranges are mapped into.
531
532 @param options
533 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.
534 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.
535 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.
536 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.
537
538 @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.
539
540 @result The created IOMemoryDescriptor on success, to be released by the caller, or zero on failure. */
541
542 static IOMemoryDescriptor *withOptions(void * buffers,
543 UInt32 count,
544 UInt32 offset,
545 task_t task,
546 IOOptionBits options,
547 IOMapper * mapper = kIOMapperSystem);
548
549 #ifndef __LP64__
550 static IOMemoryDescriptor * withPhysicalRanges(
551 IOPhysicalRange * ranges,
552 UInt32 withCount,
553 IODirection withDirection,
554 bool asReference = false) APPLE_KEXT_DEPRECATED; /* use withOptions() and kIOMemoryTypePhysical instead */
555 #endif /* !__LP64__ */
556
557 #ifndef __LP64__
558 static IOMemoryDescriptor * withSubRange(IOMemoryDescriptor *of,
559 IOByteCount offset,
560 IOByteCount length,
561 IODirection withDirection) APPLE_KEXT_DEPRECATED; /* use IOSubMemoryDescriptor::withSubRange() and kIOMemoryThreadSafe instead */
562 #endif /* !__LP64__ */
563
564 /*! @function withPersistentMemoryDescriptor
565 @abstract Copy constructor that generates a new memory descriptor if the backing memory for the same task's virtual address and length has changed.
566 @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.
567 @param originalMD The memory descriptor to be duplicated.
568 @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. */
569 static IOMemoryDescriptor *
570 withPersistentMemoryDescriptor(IOMemoryDescriptor *originalMD);
571
572 #ifndef __LP64__
573 // obsolete initializers
574 // - initWithOptions is the designated initializer
575 virtual bool initWithAddress(void * address,
576 IOByteCount withLength,
577 IODirection withDirection) APPLE_KEXT_DEPRECATED; /* use initWithOptions() instead */
578 virtual bool initWithAddress(IOVirtualAddress address,
579 IOByteCount withLength,
580 IODirection withDirection,
581 task_t withTask) APPLE_KEXT_DEPRECATED; /* use initWithOptions() instead */
582 virtual bool initWithPhysicalAddress(
583 IOPhysicalAddress address,
584 IOByteCount withLength,
585 IODirection withDirection ) APPLE_KEXT_DEPRECATED; /* use initWithOptions() instead */
586 virtual bool initWithRanges(IOVirtualRange * ranges,
587 UInt32 withCount,
588 IODirection withDirection,
589 task_t withTask,
590 bool asReference = false) APPLE_KEXT_DEPRECATED; /* use initWithOptions() instead */
591 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
592 UInt32 withCount,
593 IODirection withDirection,
594 bool asReference = false) APPLE_KEXT_DEPRECATED; /* use initWithOptions() instead */
595 #endif /* __LP64__ */
596
597 /*! @function getDirection
598 @abstract Accessor to get the direction the memory descriptor was created with.
599 @discussion This method returns the direction the memory descriptor was created with.
600 @result The direction. */
601
602 virtual IODirection getDirection() const;
603
604 /*! @function getLength
605 @abstract Accessor to get the length of the memory descriptor (over all its ranges).
606 @discussion This method returns the total length of the memory described by the descriptor, ie. the sum of its ranges' lengths.
607 @result The byte count. */
608
609 virtual IOByteCount getLength() const;
610
611 /*! @function setTag
612 @abstract Set the tag for the memory descriptor.
613 @discussion This method sets the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
614 @param tag The tag. */
615
616 virtual void setTag( IOOptionBits tag );
617
618 /*! @function getTag
619 @abstract Accessor to the retrieve the tag for the memory descriptor.
620 @discussion This method returns the tag for the memory descriptor. Tag bits are not interpreted by IOMemoryDescriptor.
621 @result The tag. */
622
623 virtual IOOptionBits getTag( void );
624
625 /*! @function getFlags
626 @abstract Accessor to the retrieve the options the memory descriptor was created with.
627 @discussion Accessor to the retrieve the options the memory descriptor was created with, and flags with its state. These bits are defined by the kIOMemory* enum.
628 @result The flags bitfield. */
629
630 uint64_t getFlags(void);
631
632 /*! @function readBytes
633 @abstract Copy data from the memory descriptor's buffer to the specified buffer.
634 @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.
635 @param offset A byte offset into the memory descriptor's memory.
636 @param bytes The caller supplied buffer to copy the data to.
637 @param withLength The length of the data to copy.
638 @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. */
639
640 virtual IOByteCount readBytes(IOByteCount offset,
641 void * bytes, IOByteCount withLength);
642
643 /*! @function writeBytes
644 @abstract Copy data to the memory descriptor's buffer from the specified buffer.
645 @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.
646 @param offset A byte offset into the memory descriptor's memory.
647 @param bytes The caller supplied buffer to copy the data from.
648 @param withLength The length of the data to copy.
649 @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. */
650
651 virtual IOByteCount writeBytes(IOByteCount offset,
652 const void * bytes, IOByteCount withLength);
653
654 #ifndef __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 memory.
661 @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.
662 @result A physical address. */
663
664 IOPhysicalAddress getPhysicalAddress();
665
666 #ifndef __LP64__
667 virtual void * getVirtualSegment(IOByteCount offset,
668 IOByteCount * length) APPLE_KEXT_DEPRECATED; /* use map() and getVirtualAddress() instead */
669 #endif /* !__LP64__ */
670
671 /*! @function prepare
672 @abstract Prepare the memory for an I/O transfer.
673 @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.
674 @param forDirection The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
675 @result An IOReturn code. */
676
677 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone) = 0;
678
679 /*! @function complete
680 @abstract Complete processing of the memory after an I/O transfer finishes.
681 @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.
682 @param forDirection DEPRECATED The direction of the I/O just completed, or kIODirectionNone for the direction specified by the memory descriptor.
683 @result An IOReturn code. */
684
685 virtual IOReturn complete(IODirection forDirection = kIODirectionNone) = 0;
686
687 /*
688 * Mapping functions.
689 */
690
691 /*! @function createMappingInTask
692 @abstract Maps a IOMemoryDescriptor into a task.
693 @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.
694 @param intoTask Sets the target task for the mapping. Pass kernel_task for the kernel address space.
695 @param atAddress If a placed mapping is requested, atAddress specifies its address, and the kIOMapAnywhere should not be set. Otherwise, atAddress is ignored.
696 @param options Mapping options are defined in IOTypes.h,<br>
697 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>
698 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
699 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
700 kIOMapReadOnly to allow only read only accesses to the memory - writes will cause and access fault.<br>
701 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>
702 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>
703 kIOMapPrefault will try to prefault the pages corresponding to the mapping. This must not be done on the kernel task, and the memory must have been wired via prepare(). Otherwise, the function will fail.<br>
704 @param offset Is a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default to map all the memory.
705 @param length Is the length of the mapping requested for a subset of the IOMemoryDescriptor. Zero is the default to map all the memory.
706 @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. */
707
708 IOMemoryMap * createMappingInTask(
709 task_t intoTask,
710 mach_vm_address_t atAddress,
711 IOOptionBits options,
712 mach_vm_size_t offset = 0,
713 mach_vm_size_t length = 0 );
714
715 #ifndef __LP64__
716 virtual IOMemoryMap * map(
717 task_t intoTask,
718 IOVirtualAddress atAddress,
719 IOOptionBits options,
720 IOByteCount offset = 0,
721 IOByteCount length = 0 ) APPLE_KEXT_DEPRECATED; /* use createMappingInTask() instead */
722 #endif /* !__LP64__ */
723
724 /*! @function map
725 @abstract Maps a IOMemoryDescriptor into the kernel map.
726 @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.
727 @param options Mapping options as in the full version of the createMappingInTask method, with kIOMapAnywhere assumed.
728 @result See the full version of the createMappingInTask method. */
729
730 virtual IOMemoryMap * map(
731 IOOptionBits options = 0 );
732
733 /*! @function setMapping
734 @abstract Establishes an already existing mapping.
735 @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.
736 @param task Address space in which the mapping exists.
737 @param mapAddress Virtual address of the mapping.
738 @param options Caching and read-only attributes of the mapping.
739 @result A IOMemoryMap object created to represent the mapping. */
740
741 virtual IOMemoryMap * setMapping(
742 task_t task,
743 IOVirtualAddress mapAddress,
744 IOOptionBits options = 0 );
745
746 // Following methods are private implementation
747
748 #ifdef __LP64__
749 virtual
750 #endif /* __LP64__ */
751 IOReturn redirect( task_t safeTask, bool redirect );
752
753 IOReturn handleFault(
754 void * _pager,
755 mach_vm_size_t sourceOffset,
756 mach_vm_size_t length);
757
758 IOReturn populateDevicePager(
759 void * pager,
760 vm_map_t addressMap,
761 mach_vm_address_t address,
762 mach_vm_size_t sourceOffset,
763 mach_vm_size_t length,
764 IOOptionBits options );
765
766 virtual IOMemoryMap * makeMapping(
767 IOMemoryDescriptor * owner,
768 task_t intoTask,
769 IOVirtualAddress atAddress,
770 IOOptionBits options,
771 IOByteCount offset,
772 IOByteCount length );
773
774 protected:
775 virtual void addMapping(
776 IOMemoryMap * mapping );
777
778 virtual void removeMapping(
779 IOMemoryMap * mapping );
780
781 virtual IOReturn doMap(
782 vm_map_t addressMap,
783 IOVirtualAddress * atAddress,
784 IOOptionBits options,
785 IOByteCount sourceOffset = 0,
786 IOByteCount length = 0 );
787
788 virtual IOReturn doUnmap(
789 vm_map_t addressMap,
790 IOVirtualAddress logical,
791 IOByteCount length );
792 };
793
794 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
795
796 /*! @class IOMemoryMap : public OSObject
797 @abstract A class defining common methods for describing a memory mapping.
798 @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. */
799
800 class IOMemoryMap : public OSObject
801 {
802 OSDeclareDefaultStructors(IOMemoryMap)
803 #ifdef XNU_KERNEL_PRIVATE
804 public:
805 IOMemoryDescriptor * fMemory;
806 IOMemoryMap * fSuperMap;
807 mach_vm_size_t fOffset;
808 mach_vm_address_t fAddress;
809 mach_vm_size_t fLength;
810 task_t fAddressTask;
811 vm_map_t fAddressMap;
812 IOOptionBits fOptions;
813 upl_t fRedirUPL;
814 ipc_port_t fRedirEntry;
815 IOMemoryDescriptor * fOwner;
816 uint8_t fUserClientUnmap;
817 #if IOTRACKING
818 IOTrackingUser fTracking;
819 #endif
820 #endif /* XNU_KERNEL_PRIVATE */
821
822 protected:
823 virtual void taggedRelease(const void *tag = 0) const APPLE_KEXT_OVERRIDE;
824 virtual void free() APPLE_KEXT_OVERRIDE;
825
826 public:
827 /*! @function getVirtualAddress
828 @abstract Accessor to the virtual address of the first byte in the mapping.
829 @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.
830 @result A virtual address. */
831
832 virtual IOVirtualAddress getVirtualAddress();
833
834 /*! @function getPhysicalSegment
835 @abstract Break a mapping into its physically contiguous segments.
836 @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.
837 @param offset A byte offset into the mapping whose physical address to return.
838 @param length If non-zero, getPhysicalSegment will store here the length of the physically contiguous segement at the given offset.
839 @result A physical address, or zero if the offset is beyond the length of the mapping. */
840
841 #ifdef __LP64__
842 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
843 IOByteCount * length,
844 IOOptionBits options = 0);
845 #else /* !__LP64__ */
846 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
847 IOByteCount * length);
848 #endif /* !__LP64__ */
849
850 /*! @function getPhysicalAddress
851 @abstract Return the physical address of the first byte in the mapping.
852 @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.
853 @result A physical address. */
854
855 IOPhysicalAddress getPhysicalAddress();
856
857 /*! @function getLength
858 @abstract Accessor to the length of the mapping.
859 @discussion This method returns the length of the mapping.
860 @result A byte count. */
861
862 virtual IOByteCount getLength();
863
864 /*! @function getAddressTask
865 @abstract Accessor to the task of the mapping.
866 @discussion This method returns the mach task the mapping exists in.
867 @result A mach task_t. */
868
869 virtual task_t getAddressTask();
870
871 /*! @function getMemoryDescriptor
872 @abstract Accessor to the IOMemoryDescriptor the mapping was created from.
873 @discussion This method returns the IOMemoryDescriptor the mapping was created from.
874 @result An IOMemoryDescriptor reference, which is valid while the IOMemoryMap object is retained. It should not be released by the caller. */
875
876 virtual IOMemoryDescriptor * getMemoryDescriptor();
877
878 /*! @function getMapOptions
879 @abstract Accessor to the options the mapping was created with.
880 @discussion This method returns the options to IOMemoryDescriptor::map the mapping was created with.
881 @result Options for the mapping, including cache settings. */
882
883 virtual IOOptionBits getMapOptions();
884
885 /*! @function unmap
886 @abstract Force the IOMemoryMap to unmap, without destroying the object.
887 @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.
888 @result An IOReturn code. */
889
890 virtual IOReturn unmap();
891
892 virtual void taskDied();
893
894 /*! @function redirect
895 @abstract Replace the memory mapped in a process with new backing memory.
896 @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.
897 @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.
898 @param options Mapping options are defined in IOTypes.h, and are documented in IOMemoryDescriptor::map()
899 @param offset As with IOMemoryDescriptor::map(), a beginning offset into the IOMemoryDescriptor's memory where the mapping starts. Zero is the default.
900 @result An IOReturn code. */
901
902 #ifndef __LP64__
903 // For 32 bit XNU, there is a 32 bit (IOByteCount) and a 64 bit (mach_vm_size_t) interface;
904 // for 64 bit, these fall together on the 64 bit one.
905 virtual IOReturn redirect(IOMemoryDescriptor * newBackingMemory,
906 IOOptionBits options,
907 IOByteCount offset = 0);
908 #endif
909 virtual IOReturn redirect(IOMemoryDescriptor * newBackingMemory,
910 IOOptionBits options,
911 mach_vm_size_t offset = 0);
912
913 #ifdef __LP64__
914 /*! @function getAddress
915 @abstract Accessor to the virtual address of the first byte in the mapping.
916 @discussion This method returns the virtual address of the first byte in the mapping.
917 @result A virtual address. */
918 inline mach_vm_address_t getAddress() __attribute__((always_inline));
919 /*! @function getSize
920 @abstract Accessor to the length of the mapping.
921 @discussion This method returns the length of the mapping.
922 @result A byte count. */
923 inline mach_vm_size_t getSize() __attribute__((always_inline));
924 #else /* !__LP64__ */
925 /*! @function getAddress
926 @abstract Accessor to the virtual address of the first byte in the mapping.
927 @discussion This method returns the virtual address of the first byte in the mapping.
928 @result A virtual address. */
929 virtual mach_vm_address_t getAddress();
930 /*! @function getSize
931 @abstract Accessor to the length of the mapping.
932 @discussion This method returns the length of the mapping.
933 @result A byte count. */
934 virtual mach_vm_size_t getSize();
935 #endif /* !__LP64__ */
936
937 #ifdef XNU_KERNEL_PRIVATE
938 // for IOMemoryDescriptor use
939 IOMemoryMap * copyCompatible( IOMemoryMap * newMapping );
940
941 bool init(
942 task_t intoTask,
943 mach_vm_address_t toAddress,
944 IOOptionBits options,
945 mach_vm_size_t offset,
946 mach_vm_size_t length );
947
948 bool setMemoryDescriptor(IOMemoryDescriptor * _memory, mach_vm_size_t _offset);
949
950 IOReturn redirect(
951 task_t intoTask, bool redirect );
952
953 IOReturn userClientUnmap();
954 #endif /* XNU_KERNEL_PRIVATE */
955
956 IOReturn wireRange(
957 uint32_t options,
958 mach_vm_size_t offset,
959 mach_vm_size_t length);
960
961 OSMetaClassDeclareReservedUnused(IOMemoryMap, 0);
962 OSMetaClassDeclareReservedUnused(IOMemoryMap, 1);
963 OSMetaClassDeclareReservedUnused(IOMemoryMap, 2);
964 OSMetaClassDeclareReservedUnused(IOMemoryMap, 3);
965 OSMetaClassDeclareReservedUnused(IOMemoryMap, 4);
966 OSMetaClassDeclareReservedUnused(IOMemoryMap, 5);
967 OSMetaClassDeclareReservedUnused(IOMemoryMap, 6);
968 OSMetaClassDeclareReservedUnused(IOMemoryMap, 7);
969 };
970
971 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
972 #ifdef XNU_KERNEL_PRIVATE
973 // Also these flags should not overlap with the options to
974 // IOMemoryDescriptor::initWithRanges(... IOOptionsBits options);
975 enum {
976 _kIOMemorySourceSegment = 0x00002000
977 };
978 #endif /* XNU_KERNEL_PRIVATE */
979
980 // The following classes are private implementation of IOMemoryDescriptor - they
981 // should not be referenced directly, just through the public API's in the
982 // IOMemoryDescriptor class. For example, an IOGeneralMemoryDescriptor instance
983 // might be created by IOMemoryDescriptor::withAddressRange(), but there should be
984 // no need to reference as anything but a generic IOMemoryDescriptor *.
985
986 class IOGeneralMemoryDescriptor : public IOMemoryDescriptor
987 {
988 OSDeclareDefaultStructors(IOGeneralMemoryDescriptor);
989
990 public:
991 union Ranges {
992 IOVirtualRange *v;
993 IOAddressRange *v64;
994 IOPhysicalRange *p;
995 void *uio;
996 };
997 protected:
998 Ranges _ranges;
999 unsigned _rangesCount; /* number of address ranges in list */
1000 #ifndef __LP64__
1001 bool _rangesIsAllocated; /* is list allocated by us? */
1002 #endif /* !__LP64__ */
1003
1004 task_t _task; /* task where all ranges are mapped to */
1005
1006 union {
1007 IOVirtualRange v;
1008 IOPhysicalRange p;
1009 } _singleRange; /* storage space for a single range */
1010
1011 unsigned _wireCount; /* number of outstanding wires */
1012
1013 #ifndef __LP64__
1014 uintptr_t _cachedVirtualAddress;
1015
1016 IOPhysicalAddress _cachedPhysicalAddress;
1017 #endif /* !__LP64__ */
1018
1019 bool _initialized; /* has superclass been initialized? */
1020
1021 public:
1022 virtual void free() APPLE_KEXT_OVERRIDE;
1023
1024 virtual IOReturn dmaCommandOperation(DMACommandOps op, void *vData, UInt dataSize) const APPLE_KEXT_OVERRIDE;
1025
1026 virtual uint64_t getPreparationID( void ) APPLE_KEXT_OVERRIDE;
1027
1028 #ifdef XNU_KERNEL_PRIVATE
1029 // Internal APIs may be made virtual at some time in the future.
1030 IOReturn wireVirtual(IODirection forDirection);
1031 IOReturn dmaMap(
1032 IOMapper * mapper,
1033 IODMACommand * command,
1034 const IODMAMapSpecification * mapSpec,
1035 uint64_t offset,
1036 uint64_t length,
1037 uint64_t * mapAddress,
1038 uint64_t * mapLength);
1039 bool initMemoryEntries(size_t size, IOMapper * mapper);
1040
1041 IOMemoryReference * memoryReferenceAlloc(uint32_t capacity,
1042 IOMemoryReference * realloc);
1043 void memoryReferenceFree(IOMemoryReference * ref);
1044 void memoryReferenceRelease(IOMemoryReference * ref);
1045
1046 IOReturn memoryReferenceCreate(
1047 IOOptionBits options,
1048 IOMemoryReference ** reference);
1049
1050 IOReturn memoryReferenceMap(IOMemoryReference * ref,
1051 vm_map_t map,
1052 mach_vm_size_t inoffset,
1053 mach_vm_size_t size,
1054 IOOptionBits options,
1055 mach_vm_address_t * inaddr);
1056
1057 static IOReturn memoryReferenceSetPurgeable(
1058 IOMemoryReference * ref,
1059 IOOptionBits newState,
1060 IOOptionBits * oldState);
1061 static IOReturn memoryReferenceGetPageCounts(
1062 IOMemoryReference * ref,
1063 IOByteCount * residentPageCount,
1064 IOByteCount * dirtyPageCount);
1065 #endif
1066
1067 private:
1068
1069 #ifndef __LP64__
1070 virtual void setPosition(IOByteCount position);
1071 virtual void mapIntoKernel(unsigned rangeIndex);
1072 virtual void unmapFromKernel();
1073 #endif /* !__LP64__ */
1074
1075 // Internal
1076 OSData * _memoryEntries;
1077 unsigned int _pages;
1078 ppnum_t _highestPage;
1079 uint32_t __iomd_reservedA;
1080 uint32_t __iomd_reservedB;
1081
1082 IOLock * _prepareLock;
1083
1084 public:
1085 /*
1086 * IOMemoryDescriptor required methods
1087 */
1088
1089 // Master initaliser
1090 virtual bool initWithOptions(void * buffers,
1091 UInt32 count,
1092 UInt32 offset,
1093 task_t task,
1094 IOOptionBits options,
1095 IOMapper * mapper = kIOMapperSystem) APPLE_KEXT_OVERRIDE;
1096
1097 #ifndef __LP64__
1098 // Secondary initialisers
1099 virtual bool initWithAddress(void * address,
1100 IOByteCount withLength,
1101 IODirection withDirection) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1102
1103 virtual bool initWithAddress(IOVirtualAddress address,
1104 IOByteCount withLength,
1105 IODirection withDirection,
1106 task_t withTask) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1107
1108 virtual bool initWithPhysicalAddress(
1109 IOPhysicalAddress address,
1110 IOByteCount withLength,
1111 IODirection withDirection ) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1112
1113 virtual bool initWithRanges( IOVirtualRange * ranges,
1114 UInt32 withCount,
1115 IODirection withDirection,
1116 task_t withTask,
1117 bool asReference = false) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1118
1119 virtual bool initWithPhysicalRanges(IOPhysicalRange * ranges,
1120 UInt32 withCount,
1121 IODirection withDirection,
1122 bool asReference = false) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1123
1124 virtual addr64_t getPhysicalSegment64( IOByteCount offset,
1125 IOByteCount * length ) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1126
1127 virtual IOPhysicalAddress getPhysicalSegment(IOByteCount offset,
1128 IOByteCount * length) APPLE_KEXT_OVERRIDE;
1129
1130 virtual IOPhysicalAddress getSourceSegment(IOByteCount offset,
1131 IOByteCount * length) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1132
1133 virtual void * getVirtualSegment(IOByteCount offset,
1134 IOByteCount * length) APPLE_KEXT_OVERRIDE APPLE_KEXT_DEPRECATED;
1135 #endif /* !__LP64__ */
1136
1137 virtual IOReturn setPurgeable( IOOptionBits newState,
1138 IOOptionBits * oldState ) APPLE_KEXT_OVERRIDE;
1139
1140 virtual addr64_t getPhysicalSegment( IOByteCount offset,
1141 IOByteCount * length,
1142 #ifdef __LP64__
1143 IOOptionBits options = 0 ) APPLE_KEXT_OVERRIDE;
1144 #else /* !__LP64__ */
1145 IOOptionBits options ) APPLE_KEXT_OVERRIDE;
1146 #endif /* !__LP64__ */
1147
1148 virtual IOReturn prepare(IODirection forDirection = kIODirectionNone) APPLE_KEXT_OVERRIDE;
1149
1150 virtual IOReturn complete(IODirection forDirection = kIODirectionNone) APPLE_KEXT_OVERRIDE;
1151
1152 virtual IOReturn doMap(
1153 vm_map_t addressMap,
1154 IOVirtualAddress * atAddress,
1155 IOOptionBits options,
1156 IOByteCount sourceOffset = 0,
1157 IOByteCount length = 0 ) APPLE_KEXT_OVERRIDE;
1158
1159 virtual IOReturn doUnmap(
1160 vm_map_t addressMap,
1161 IOVirtualAddress logical,
1162 IOByteCount length ) APPLE_KEXT_OVERRIDE;
1163
1164 virtual bool serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
1165
1166 // Factory method for cloning a persistent IOMD, see IOMemoryDescriptor
1167 static IOMemoryDescriptor *
1168 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor *originalMD);
1169
1170 };
1171
1172 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1173
1174 #ifdef __LP64__
1175 mach_vm_address_t IOMemoryMap::getAddress()
1176 {
1177 return (getVirtualAddress());
1178 }
1179
1180 mach_vm_size_t IOMemoryMap::getSize()
1181 {
1182 return (getLength());
1183 }
1184 #else /* !__LP64__ */
1185 #include <IOKit/IOSubMemoryDescriptor.h>
1186 #endif /* !__LP64__ */
1187
1188 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1189
1190 #endif /* !_IOMEMORYDESCRIPTOR_H */