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