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