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