Chameleon Applications

Chameleon Applications Svn Source Tree

Root/branches/iFabio/Chameleon/i386/include/IOKit/IOMemoryDescriptor.h

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

Archive Download this file

Revision: 296