2 * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
28 // 45678901234567890123456789012345678901234567890123456789012345678901234567890
29 #include <sys/cdefs.h>
31 #include <IOKit/assert.h>
32 #include <IOKit/system.h>
33 #include <IOKit/IOLib.h>
34 #include <IOKit/IOMemoryDescriptor.h>
35 #include <IOKit/IOMapper.h>
36 #include <IOKit/IOKitKeysPrivate.h>
38 #include <IOKit/IOKitDebug.h>
40 #include "IOKitKernelInternal.h"
42 #include <libkern/c++/OSContainers.h>
43 #include <libkern/c++/OSDictionary.h>
44 #include <libkern/c++/OSArray.h>
45 #include <libkern/c++/OSSymbol.h>
46 #include <libkern/c++/OSNumber.h>
52 #include <vm/vm_pageout.h>
53 #include <vm/vm_shared_memory_server.h>
54 #include <mach/memory_object_types.h>
55 #include <device/device_port.h>
58 #include <mach/vm_prot.h>
59 #include <vm/vm_fault.h>
60 struct phys_entry
*pmap_find_physentry(ppnum_t pa
);
63 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
64 void ipc_port_release_send(ipc_port_t port
);
66 /* Copy between a physical page and a virtual address in the given vm_map */
67 kern_return_t
copypv(addr64_t source
, addr64_t sink
, unsigned int size
, int which
);
71 memory_object_t pager
,
76 device_pager_deallocate(
79 device_pager_populate_object(
80 memory_object_t pager
,
81 vm_object_offset_t offset
,
85 memory_object_iopl_request(
87 memory_object_offset_t offset
,
90 upl_page_info_array_t user_page_list
,
91 unsigned int *page_list_count
,
94 unsigned int IOTranslateCacheBits(struct phys_entry
*pp
);
98 #define kIOMaximumMappedIOByteCount (512*1024*1024)
100 static IOMapper
* gIOSystemMapper
;
101 static ppnum_t gIOMaximumMappedIOPageCount
= atop_32(kIOMaximumMappedIOByteCount
);
103 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
105 OSDefineMetaClassAndAbstractStructors( IOMemoryDescriptor
, OSObject
)
107 #define super IOMemoryDescriptor
109 OSDefineMetaClassAndStructors(IOGeneralMemoryDescriptor
, IOMemoryDescriptor
)
111 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
113 static IORecursiveLock
* gIOMemoryLock
;
115 #define LOCK IORecursiveLockLock( gIOMemoryLock)
116 #define UNLOCK IORecursiveLockUnlock( gIOMemoryLock)
117 #define SLEEP IORecursiveLockSleep( gIOMemoryLock, (void *)this, THREAD_UNINT)
119 IORecursiveLockWakeup( gIOMemoryLock, (void *)this, /* one-thread */ false)
121 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
123 class _IOMemoryMap
: public IOMemoryMap
125 OSDeclareDefaultStructors(_IOMemoryMap
)
127 IOMemoryDescriptor
* memory
;
128 IOMemoryMap
* superMap
;
131 IOVirtualAddress logical
;
134 IOOptionBits options
;
136 ipc_port_t redirEntry
;
137 IOMemoryDescriptor
* owner
;
140 virtual void taggedRelease(const void *tag
= 0) const;
145 // IOMemoryMap methods
146 virtual IOVirtualAddress
getVirtualAddress();
147 virtual IOByteCount
getLength();
148 virtual task_t
getAddressTask();
149 virtual IOMemoryDescriptor
* getMemoryDescriptor();
150 virtual IOOptionBits
getMapOptions();
152 virtual IOReturn
unmap();
153 virtual void taskDied();
155 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
156 IOOptionBits options
,
157 IOByteCount offset
= 0);
159 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
160 IOByteCount
* length
);
162 // for IOMemoryDescriptor use
163 _IOMemoryMap
* copyCompatible(
164 IOMemoryDescriptor
* owner
,
166 IOVirtualAddress toAddress
,
167 IOOptionBits options
,
169 IOByteCount length
);
172 IOMemoryDescriptor
* memory
,
173 IOMemoryMap
* superMap
,
175 IOByteCount length
);
177 bool initWithDescriptor(
178 IOMemoryDescriptor
* memory
,
180 IOVirtualAddress toAddress
,
181 IOOptionBits options
,
183 IOByteCount length
);
186 task_t intoTask
, bool redirect
);
189 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
191 // Some data structures and accessor macros used by the initWithOptions
194 enum ioPLBlockFlags
{
195 kIOPLOnDevice
= 0x00000001,
196 kIOPLExternUPL
= 0x00000002,
199 struct typePersMDData
201 const IOGeneralMemoryDescriptor
*fMD
;
202 ipc_port_t fMemEntry
;
207 vm_address_t fIOMDOffset
; // The offset of this iopl in descriptor
208 vm_offset_t fPageInfo
; // Pointer to page list or index into it
209 ppnum_t fMappedBase
; // Page number of first page in this iopl
210 unsigned int fPageOffset
; // Offset within first page of iopl
211 unsigned int fFlags
; // Flags
216 unsigned int fPageCnt
;
217 upl_page_info_t fPageList
[];
221 #define getDataP(osd) ((ioGMDData *) (osd)->getBytesNoCopy())
222 #define getIOPLList(d) ((ioPLBlock *) &(d->fPageList[d->fPageCnt]))
223 #define getNumIOPL(osd, d) \
224 (((osd)->getLength() - ((char *) getIOPLList(d) - (char *) d)) / sizeof(ioPLBlock))
225 #define getPageList(d) (&(d->fPageList[0]))
226 #define computeDataSize(p, u) \
227 (sizeof(ioGMDData) + p * sizeof(upl_page_info_t) + u * sizeof(ioPLBlock))
230 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
232 #define next_page(a) ( trunc_page_32(a) + PAGE_SIZE )
237 kern_return_t
device_data_action(
239 ipc_port_t device_pager
,
240 vm_prot_t protection
,
241 vm_object_offset_t offset
,
244 struct ExpansionData
{
246 unsigned int pagerContig
:1;
247 unsigned int unused
:31;
248 IOMemoryDescriptor
* memory
;
251 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
252 IOMemoryDescriptor
* memDesc
;
255 memDesc
= ref
->memory
;
259 kr
= memDesc
->handleFault( device_pager
, 0, 0,
260 offset
, size
, kIOMapDefaultCache
/*?*/);
270 kern_return_t
device_close(
273 struct ExpansionData
{
275 unsigned int pagerContig
:1;
276 unsigned int unused
:31;
277 IOMemoryDescriptor
* memory
;
279 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
281 IODelete( ref
, ExpansionData
, 1 );
283 return( kIOReturnSuccess
);
287 // Note this inline function uses C++ reference arguments to return values
288 // This means that pointers are not passed and NULLs don't have to be
289 // checked for as a NULL reference is illegal.
291 getAddrLenForInd(user_addr_t
&addr
, IOPhysicalLength
&len
, // Output variables
292 UInt32 type
, IOGeneralMemoryDescriptor::Ranges r
, UInt32 ind
)
294 assert(kIOMemoryTypePhysical
== type
|| kIOMemoryTypeUIO
== type
295 || kIOMemoryTypeVirtual
== type
);
296 if (kIOMemoryTypeUIO
== type
) {
298 uio_getiov((uio_t
) r
.uio
, ind
, &addr
, &us
); len
= us
;
301 IOVirtualRange cur
= r
.v
[ind
];
307 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
312 * Create a new IOMemoryDescriptor. The buffer is a virtual address
313 * relative to the specified task. If no task is supplied, the kernel
317 IOMemoryDescriptor::withAddress(void * address
,
319 IODirection direction
)
321 return IOMemoryDescriptor::
322 withAddress((vm_address_t
) address
, length
, direction
, kernel_task
);
326 IOMemoryDescriptor::withAddress(vm_address_t address
,
328 IODirection direction
,
331 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
334 if (that
->initWithAddress(address
, length
, direction
, task
))
343 IOMemoryDescriptor::withPhysicalAddress(
344 IOPhysicalAddress address
,
346 IODirection direction
)
348 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
350 && !self
->initWithPhysicalAddress(address
, length
, direction
)) {
359 IOMemoryDescriptor::withRanges( IOVirtualRange
* ranges
,
361 IODirection direction
,
365 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
368 if (that
->initWithRanges(ranges
, withCount
, direction
, task
, asReference
))
380 * Create a new IOMemoryDescriptor. The buffer is made up of several
381 * virtual address ranges, from a given task.
383 * Passing the ranges as a reference will avoid an extra allocation.
386 IOMemoryDescriptor::withOptions(void * buffers
,
393 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
396 && !self
->initWithOptions(buffers
, count
, offset
, task
, opts
, mapper
))
405 // Can't leave abstract but this should never be used directly,
406 bool IOMemoryDescriptor::initWithOptions(void * buffers
,
410 IOOptionBits options
,
413 // @@@ gvdl: Should I panic?
414 panic("IOMD::initWithOptions called\n");
419 IOMemoryDescriptor::withPhysicalRanges( IOPhysicalRange
* ranges
,
421 IODirection direction
,
424 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
427 if (that
->initWithPhysicalRanges(ranges
, withCount
, direction
, asReference
))
436 IOMemoryDescriptor::withSubRange(IOMemoryDescriptor
* of
,
439 IODirection direction
)
441 IOSubMemoryDescriptor
*self
= new IOSubMemoryDescriptor
;
443 if (self
&& !self
->initSubRange(of
, offset
, length
, direction
)) {
450 IOMemoryDescriptor
* IOMemoryDescriptor::
451 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
)
453 IOGeneralMemoryDescriptor
*origGenMD
=
454 OSDynamicCast(IOGeneralMemoryDescriptor
, originalMD
);
457 return IOGeneralMemoryDescriptor::
458 withPersistentMemoryDescriptor(origGenMD
);
463 IOMemoryDescriptor
* IOGeneralMemoryDescriptor::
464 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
)
466 ipc_port_t sharedMem
= (ipc_port_t
) originalMD
->createNamedEntry();
471 if (sharedMem
== originalMD
->_memEntry
) {
472 originalMD
->retain(); // Add a new reference to ourselves
473 ipc_port_release_send(sharedMem
); // Remove extra send right
477 IOGeneralMemoryDescriptor
* self
= new IOGeneralMemoryDescriptor
;
478 typePersMDData initData
= { originalMD
, sharedMem
};
481 && !self
->initWithOptions(&initData
, 1, 0, 0, kIOMemoryTypePersistentMD
, 0)) {
488 void *IOGeneralMemoryDescriptor::createNamedEntry()
491 ipc_port_t sharedMem
;
493 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
495 user_addr_t range0Addr
;
496 IOByteCount range0Len
;
497 getAddrLenForInd(range0Addr
, range0Len
, type
, _ranges
, 0);
498 range0Addr
= trunc_page_64(range0Addr
);
500 vm_size_t size
= ptoa_32(_pages
);
501 vm_address_t kernelPage
= (vm_address_t
) range0Addr
;
503 vm_map_t theMap
= ((_task
== kernel_task
)
504 && (kIOMemoryBufferPageable
& _flags
))
505 ? IOPageableMapForAddress(kernelPage
)
506 : get_task_map(_task
);
508 memory_object_size_t actualSize
= size
;
509 vm_prot_t prot
= VM_PROT_READ
| VM_PROT_WRITE
;
511 prot
|= MAP_MEM_NAMED_REUSE
;
513 error
= mach_make_memory_entry_64(theMap
,
514 &actualSize
, range0Addr
, prot
, &sharedMem
, (ipc_port_t
) _memEntry
);
516 if (KERN_SUCCESS
== error
) {
517 if (actualSize
== size
) {
521 IOLog("IOGMD::mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
522 (UInt64
)range0Addr
, (UInt32
)actualSize
, size
);
524 ipc_port_release_send( sharedMem
);
528 return MACH_PORT_NULL
;
534 * Initialize an IOMemoryDescriptor. The buffer is a virtual address
535 * relative to the specified task. If no task is supplied, the kernel
538 * An IOMemoryDescriptor can be re-used by calling initWithAddress or
539 * initWithRanges again on an existing instance -- note this behavior
540 * is not commonly supported in other I/O Kit classes, although it is
544 IOGeneralMemoryDescriptor::initWithAddress(void * address
,
545 IOByteCount withLength
,
546 IODirection withDirection
)
548 _singleRange
.v
.address
= (vm_address_t
) address
;
549 _singleRange
.v
.length
= withLength
;
551 return initWithRanges(&_singleRange
.v
, 1, withDirection
, kernel_task
, true);
555 IOGeneralMemoryDescriptor::initWithAddress(vm_address_t address
,
556 IOByteCount withLength
,
557 IODirection withDirection
,
560 _singleRange
.v
.address
= address
;
561 _singleRange
.v
.length
= withLength
;
563 return initWithRanges(&_singleRange
.v
, 1, withDirection
, withTask
, true);
567 IOGeneralMemoryDescriptor::initWithPhysicalAddress(
568 IOPhysicalAddress address
,
569 IOByteCount withLength
,
570 IODirection withDirection
)
572 _singleRange
.p
.address
= address
;
573 _singleRange
.p
.length
= withLength
;
575 return initWithPhysicalRanges( &_singleRange
.p
, 1, withDirection
, true);
579 IOGeneralMemoryDescriptor::initWithPhysicalRanges(
580 IOPhysicalRange
* ranges
,
582 IODirection direction
,
585 IOOptionBits mdOpts
= direction
| kIOMemoryTypePhysical
;
588 mdOpts
|= kIOMemoryAsReference
;
590 return initWithOptions(ranges
, count
, 0, 0, mdOpts
, /* mapper */ 0);
594 IOGeneralMemoryDescriptor::initWithRanges(
595 IOVirtualRange
* ranges
,
597 IODirection direction
,
601 IOOptionBits mdOpts
= direction
;
604 mdOpts
|= kIOMemoryAsReference
;
607 mdOpts
|= kIOMemoryTypeVirtual
;
609 // Auto-prepare if this is a kernel memory descriptor as very few
610 // clients bother to prepare() kernel memory.
611 // But it was not enforced so what are you going to do?
612 if (task
== kernel_task
)
613 mdOpts
|= kIOMemoryAutoPrepare
;
616 mdOpts
|= kIOMemoryTypePhysical
;
618 return initWithOptions(ranges
, count
, 0, task
, mdOpts
, /* mapper */ 0);
624 * IOMemoryDescriptor. The buffer is made up of several virtual address ranges,
625 * from a given task, several physical ranges, an UPL from the ubc
626 * system or a uio (may be 64bit) from the BSD subsystem.
628 * Passing the ranges as a reference will avoid an extra allocation.
630 * An IOMemoryDescriptor can be re-used by calling initWithOptions again on an
631 * existing instance -- note this behavior is not commonly supported in other
632 * I/O Kit classes, although it is supported here.
636 IOGeneralMemoryDescriptor::initWithOptions(void * buffers
,
640 IOOptionBits options
,
643 IOOptionBits type
= options
& kIOMemoryTypeMask
;
645 // Grab the original MD's configuation data to initialse the
646 // arguments to this function.
647 if (kIOMemoryTypePersistentMD
== type
) {
649 typePersMDData
*initData
= (typePersMDData
*) buffers
;
650 const IOGeneralMemoryDescriptor
*orig
= initData
->fMD
;
651 ioGMDData
*dataP
= getDataP(orig
->_memoryEntries
);
653 // Only accept persistent memory descriptors with valid dataP data.
654 assert(orig
->_rangesCount
== 1);
655 if ( !(orig
->_flags
& kIOMemoryPersistent
) || !dataP
)
658 _memEntry
= initData
->fMemEntry
; // Grab the new named entry
659 options
= orig
->_flags
| kIOMemoryAsReference
;
660 _singleRange
= orig
->_singleRange
; // Initialise our range
661 buffers
= &_singleRange
;
664 // Now grab the original task and whatever mapper was previously used
666 mapper
= dataP
->fMapper
;
668 // We are ready to go through the original initialisation now
672 case kIOMemoryTypeUIO
:
673 case kIOMemoryTypeVirtual
:
680 case kIOMemoryTypePhysical
: // Neither Physical nor UPL should have a task
681 mapper
= kIOMapperNone
;
683 case kIOMemoryTypeUPL
:
687 return false; /* bad argument */
694 * We can check the _initialized instance variable before having ever set
695 * it to an initial value because I/O Kit guarantees that all our instance
696 * variables are zeroed on an object's allocation.
701 * An existing memory descriptor is being retargeted to point to
702 * somewhere else. Clean up our present state.
709 if (_ranges
.v
&& _rangesIsAllocated
)
710 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
712 { ipc_port_release_send((ipc_port_t
) _memEntry
); _memEntry
= 0; }
720 // Grab the appropriate mapper
721 if (mapper
== kIOMapperNone
)
722 mapper
= 0; // No Mapper
724 IOMapper::checkForSystemMapper();
725 gIOSystemMapper
= mapper
= IOMapper::gSystem
;
728 // Remove the dynamic internal use flags from the initial setting
729 options
&= ~(kIOMemoryPreparedReadOnly
);
733 // DEPRECATED variable initialisation
734 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
737 _cachedPhysicalAddress
= 0;
738 _cachedVirtualAddress
= 0;
740 if (kIOMemoryTypeUPL
== type
) {
743 unsigned int dataSize
= computeDataSize(/* pages */ 0, /* upls */ 1);
745 if (!_memoryEntries
) {
746 _memoryEntries
= OSData::withCapacity(dataSize
);
750 else if (!_memoryEntries
->initWithCapacity(dataSize
))
753 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
754 dataP
= getDataP(_memoryEntries
);
755 dataP
->fMapper
= mapper
;
758 _wireCount
++; // UPLs start out life wired
761 _pages
+= atop_32(offset
+ count
+ PAGE_MASK
) - atop_32(offset
);
764 upl_page_info_t
*pageList
= UPL_GET_INTERNAL_PAGE_LIST((upl_t
) buffers
);
766 iopl
.fIOPL
= (upl_t
) buffers
;
767 // Set the flag kIOPLOnDevice convieniently equal to 1
768 iopl
.fFlags
= pageList
->device
| kIOPLExternUPL
;
769 iopl
.fIOMDOffset
= 0;
770 if (!pageList
->device
) {
771 // Pre-compute the offset into the UPL's page list
772 pageList
= &pageList
[atop_32(offset
)];
775 iopl
.fMappedBase
= mapper
->iovmAlloc(_pages
);
776 mapper
->iovmInsert(iopl
.fMappedBase
, 0, pageList
, _pages
);
779 iopl
.fMappedBase
= 0;
782 iopl
.fMappedBase
= 0;
783 iopl
.fPageInfo
= (vm_address_t
) pageList
;
784 iopl
.fPageOffset
= offset
;
786 _memoryEntries
->appendBytes(&iopl
, sizeof(iopl
));
789 // kIOMemoryTypeVirtual | kIOMemoryTypeUIO | kIOMemoryTypePhysical
791 // Initialize the memory descriptor
792 if (options
& kIOMemoryAsReference
) {
793 _rangesIsAllocated
= false;
795 // Hack assignment to get the buffer arg into _ranges.
796 // I'd prefer to do _ranges = (Ranges) buffers, but that doesn't
798 // This also initialises the uio & physical ranges.
799 _ranges
.v
= (IOVirtualRange
*) buffers
;
802 assert(kIOMemoryTypeUIO
!= type
);
804 _rangesIsAllocated
= true;
805 _ranges
.v
= IONew(IOVirtualRange
, count
);
808 bcopy(buffers
, _ranges
.v
, count
* sizeof(IOVirtualRange
));
811 // Find starting address within the vector of ranges
812 Ranges vec
= _ranges
;
815 for (unsigned ind
= 0; ind
< count
; ind
++) {
819 // addr & len are returned by this function
820 getAddrLenForInd(addr
, len
, type
, vec
, ind
);
821 pages
+= (atop_64(addr
+ len
+ PAGE_MASK
) - atop_64(addr
));
823 assert(len
> length
); // Check for 32 bit wrap around
828 _rangesCount
= count
;
830 // Auto-prepare memory at creation time.
831 // Implied completion when descriptor is free-ed
832 if (kIOMemoryTypePhysical
== type
)
833 _wireCount
++; // Physical MDs are, by definition, wired
834 else { /* kIOMemoryTypeVirtual | kIOMemoryTypeUIO */
836 unsigned dataSize
= computeDataSize(_pages
, /* upls */ count
* 2);
838 if (!_memoryEntries
) {
839 _memoryEntries
= OSData::withCapacity(dataSize
);
843 else if (!_memoryEntries
->initWithCapacity(dataSize
))
846 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
847 dataP
= getDataP(_memoryEntries
);
848 dataP
->fMapper
= mapper
;
849 dataP
->fPageCnt
= _pages
;
851 if ( (kIOMemoryPersistent
& _flags
) && !_memEntry
)
852 _memEntry
= createNamedEntry();
854 if ((_flags
& kIOMemoryAutoPrepare
)
855 && prepare() != kIOReturnSuccess
)
868 void IOGeneralMemoryDescriptor::free()
872 reserved
->memory
= 0;
878 _memoryEntries
->release();
882 if (_ranges
.v
&& _rangesIsAllocated
)
883 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
885 if (reserved
&& reserved
->devicePager
)
886 device_pager_deallocate( (memory_object_t
) reserved
->devicePager
);
888 // memEntry holds a ref on the device pager which owns reserved
889 // (ExpansionData) so no reserved access after this point
891 ipc_port_release_send( (ipc_port_t
) _memEntry
);
896 /* DEPRECATED */ void IOGeneralMemoryDescriptor::unmapFromKernel()
898 panic("IOGMD::unmapFromKernel deprecated");
901 /* DEPRECATED */ void IOGeneralMemoryDescriptor::mapIntoKernel(unsigned rangeIndex
)
903 panic("IOGMD::mapIntoKernel deprecated");
909 * Get the direction of the transfer.
911 IODirection
IOMemoryDescriptor::getDirection() const
919 * Get the length of the transfer (over all ranges).
921 IOByteCount
IOMemoryDescriptor::getLength() const
926 void IOMemoryDescriptor::setTag( IOOptionBits tag
)
931 IOOptionBits
IOMemoryDescriptor::getTag( void )
936 // @@@ gvdl: who is using this API? Seems like a wierd thing to implement.
937 IOPhysicalAddress
IOMemoryDescriptor::getSourceSegment( IOByteCount offset
,
938 IOByteCount
* length
)
940 IOPhysicalAddress physAddr
= 0;
942 if( prepare() == kIOReturnSuccess
) {
943 physAddr
= getPhysicalSegment( offset
, length
);
950 IOByteCount
IOMemoryDescriptor::readBytes
951 (IOByteCount offset
, void *bytes
, IOByteCount length
)
953 addr64_t dstAddr
= (addr64_t
) (UInt32
) bytes
;
954 IOByteCount remaining
;
956 // Assert that this entire I/O is withing the available range
957 assert(offset
< _length
);
958 assert(offset
+ length
<= _length
);
959 if (offset
>= _length
) {
960 IOLog("IOGMD(%p): rB = o%lx, l%lx\n", this, offset
, length
); // @@@ gvdl
964 remaining
= length
= min(length
, _length
- offset
);
965 while (remaining
) { // (process another target segment?)
969 srcAddr64
= getPhysicalSegment64(offset
, &srcLen
);
973 // Clip segment length to remaining
974 if (srcLen
> remaining
)
977 copypv(srcAddr64
, dstAddr
, srcLen
,
978 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
987 return length
- remaining
;
990 IOByteCount
IOMemoryDescriptor::writeBytes
991 (IOByteCount offset
, const void *bytes
, IOByteCount length
)
993 addr64_t srcAddr
= (addr64_t
) (UInt32
) bytes
;
994 IOByteCount remaining
;
996 // Assert that this entire I/O is withing the available range
997 assert(offset
< _length
);
998 assert(offset
+ length
<= _length
);
1000 assert( !(kIOMemoryPreparedReadOnly
& _flags
) );
1002 if ( (kIOMemoryPreparedReadOnly
& _flags
) || offset
>= _length
) {
1003 IOLog("IOGMD(%p): wB = o%lx, l%lx\n", this, offset
, length
); // @@@ gvdl
1007 remaining
= length
= min(length
, _length
- offset
);
1008 while (remaining
) { // (process another target segment?)
1012 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1016 // Clip segment length to remaining
1017 if (dstLen
> remaining
)
1020 copypv(srcAddr
, (addr64_t
) dstAddr64
, dstLen
,
1021 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1025 remaining
-= dstLen
;
1030 return length
- remaining
;
1033 // osfmk/device/iokit_rpc.c
1034 extern "C" unsigned int IODefaultCacheBits(addr64_t pa
);
1036 /* DEPRECATED */ void IOGeneralMemoryDescriptor::setPosition(IOByteCount position
)
1038 panic("IOGMD::setPosition deprecated");
1041 IOPhysicalAddress
IOGeneralMemoryDescriptor::getPhysicalSegment
1042 (IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1044 IOPhysicalAddress address
= 0;
1045 IOPhysicalLength length
= 0;
1047 // assert(offset <= _length);
1048 if (offset
< _length
) // (within bounds?)
1050 if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
) {
1053 // Physical address based memory descriptor
1055 // Find offset within descriptor and make it relative
1056 // to the current _range.
1057 for (ind
= 0 ; offset
>= _ranges
.p
[ind
].length
; ind
++ )
1058 offset
-= _ranges
.p
[ind
].length
;
1060 IOPhysicalRange cur
= _ranges
.p
[ind
];
1061 address
= cur
.address
+ offset
;
1062 length
= cur
.length
- offset
;
1064 // see how far we can coalesce ranges
1065 for (++ind
; ind
< _rangesCount
; ind
++) {
1066 cur
= _ranges
.p
[ind
];
1068 if (address
+ length
!= cur
.address
)
1071 length
+= cur
.length
;
1074 // @@@ gvdl: should be assert(address);
1075 // but can't as NVidia GeForce creates a bogus physical mem
1077 || /* nvidia */ (!_ranges
.p
[0].address
&& 1 == _rangesCount
));
1081 // We need wiring & we are wired.
1086 panic("IOGMD: not wired for getPhysicalSegment()");
1090 assert(_memoryEntries
);
1092 ioGMDData
* dataP
= getDataP(_memoryEntries
);
1093 const ioPLBlock
*ioplList
= getIOPLList(dataP
);
1094 UInt ind
, numIOPLs
= getNumIOPL(_memoryEntries
, dataP
);
1095 upl_page_info_t
*pageList
= getPageList(dataP
);
1097 assert(numIOPLs
> 0);
1099 // Scan through iopl info blocks looking for block containing offset
1100 for (ind
= 1; ind
< numIOPLs
; ind
++) {
1101 if (offset
< ioplList
[ind
].fIOMDOffset
)
1105 // Go back to actual range as search goes past it
1106 ioPLBlock ioplInfo
= ioplList
[ind
- 1];
1109 length
= ioplList
[ind
].fIOMDOffset
;
1112 length
-= offset
; // Remainder within iopl
1114 // Subtract offset till this iopl in total list
1115 offset
-= ioplInfo
.fIOMDOffset
;
1117 // This is a mapped IOPL so we just need to compute an offset
1118 // relative to the mapped base.
1119 if (ioplInfo
.fMappedBase
) {
1120 offset
+= (ioplInfo
.fPageOffset
& PAGE_MASK
);
1121 address
= ptoa_32(ioplInfo
.fMappedBase
) + offset
;
1125 // Currently the offset is rebased into the current iopl.
1126 // Now add the iopl 1st page offset.
1127 offset
+= ioplInfo
.fPageOffset
;
1129 // For external UPLs the fPageInfo field points directly to
1130 // the upl's upl_page_info_t array.
1131 if (ioplInfo
.fFlags
& kIOPLExternUPL
)
1132 pageList
= (upl_page_info_t
*) ioplInfo
.fPageInfo
;
1134 pageList
= &pageList
[ioplInfo
.fPageInfo
];
1136 // Check for direct device non-paged memory
1137 if ( ioplInfo
.fFlags
& kIOPLOnDevice
) {
1138 address
= ptoa_32(pageList
->phys_addr
) + offset
;
1142 // Now we need compute the index into the pageList
1143 ind
= atop_32(offset
);
1144 offset
&= PAGE_MASK
;
1146 IOPhysicalAddress pageAddr
= pageList
[ind
].phys_addr
;
1147 address
= ptoa_32(pageAddr
) + offset
;
1149 // Check for the remaining data in this upl being longer than the
1150 // remainder on the current page. This should be checked for
1152 if (length
> PAGE_SIZE
- offset
) {
1153 // See if the next page is contiguous. Stop looking when we hit
1154 // the end of this upl, which is indicated by the
1155 // contigLength >= length.
1156 IOByteCount contigLength
= PAGE_SIZE
- offset
;
1158 // Look for contiguous segment
1159 while (contigLength
< length
1160 && ++pageAddr
== pageList
[++ind
].phys_addr
) {
1161 contigLength
+= PAGE_SIZE
;
1163 if (length
> contigLength
)
1164 length
= contigLength
;
1176 if (lengthOfSegment
)
1177 *lengthOfSegment
= length
;
1182 addr64_t
IOMemoryDescriptor::getPhysicalSegment64
1183 (IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1185 IOPhysicalAddress phys32
;
1189 phys32
= getPhysicalSegment(offset
, lengthOfSegment
);
1193 if (gIOSystemMapper
)
1195 IOByteCount origLen
;
1197 phys64
= gIOSystemMapper
->mapAddr(phys32
);
1198 origLen
= *lengthOfSegment
;
1199 length
= page_size
- (phys64
& (page_size
- 1));
1200 while ((length
< origLen
)
1201 && ((phys64
+ length
) == gIOSystemMapper
->mapAddr(phys32
+ length
)))
1202 length
+= page_size
;
1203 if (length
> origLen
)
1206 *lengthOfSegment
= length
;
1209 phys64
= (addr64_t
) phys32
;
1214 IOPhysicalAddress
IOGeneralMemoryDescriptor::
1215 getSourceSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1217 IOPhysicalAddress address
= 0;
1218 IOPhysicalLength length
= 0;
1219 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1221 assert(offset
<= _length
);
1223 if ( type
== kIOMemoryTypeUPL
)
1224 return super::getSourceSegment( offset
, lengthOfSegment
);
1225 else if ( offset
< _length
) // (within bounds?)
1227 unsigned rangesIndex
= 0;
1228 Ranges vec
= _ranges
;
1231 // Find starting address within the vector of ranges
1233 getAddrLenForInd(addr
, length
, type
, vec
, rangesIndex
);
1234 if (offset
< length
)
1236 offset
-= length
; // (make offset relative)
1240 // Now that we have the starting range,
1241 // lets find the last contiguous range
1245 for ( ++rangesIndex
; rangesIndex
< _rangesCount
; rangesIndex
++ ) {
1246 user_addr_t newAddr
;
1247 IOPhysicalLength newLen
;
1249 getAddrLenForInd(newAddr
, newLen
, type
, vec
, rangesIndex
);
1250 if (addr
+ length
!= newAddr
)
1255 address
= (IOPhysicalAddress
) addr
; // Truncate address to 32bit
1260 if ( lengthOfSegment
) *lengthOfSegment
= length
;
1265 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1266 /* DEPRECATED */ void * IOGeneralMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
1267 /* DEPRECATED */ IOByteCount
* lengthOfSegment
)
1269 if (_task
== kernel_task
)
1270 return (void *) getSourceSegment(offset
, lengthOfSegment
);
1272 panic("IOGMD::getVirtualSegment deprecated");
1276 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1280 IOReturn
IOMemoryDescriptor::setPurgeable( IOOptionBits newState
,
1281 IOOptionBits
* oldState
)
1283 IOReturn err
= kIOReturnSuccess
;
1284 vm_purgable_t control
;
1291 err
= kIOReturnNotReady
;
1295 control
= VM_PURGABLE_SET_STATE
;
1298 case kIOMemoryPurgeableKeepCurrent
:
1299 control
= VM_PURGABLE_GET_STATE
;
1302 case kIOMemoryPurgeableNonVolatile
:
1303 state
= VM_PURGABLE_NONVOLATILE
;
1305 case kIOMemoryPurgeableVolatile
:
1306 state
= VM_PURGABLE_VOLATILE
;
1308 case kIOMemoryPurgeableEmpty
:
1309 state
= VM_PURGABLE_EMPTY
;
1312 err
= kIOReturnBadArgument
;
1316 if (kIOReturnSuccess
!= err
)
1319 err
= mach_memory_entry_purgable_control((ipc_port_t
) _memEntry
, control
, &state
);
1323 if (kIOReturnSuccess
== err
)
1327 case VM_PURGABLE_NONVOLATILE
:
1328 state
= kIOMemoryPurgeableNonVolatile
;
1330 case VM_PURGABLE_VOLATILE
:
1331 state
= kIOMemoryPurgeableVolatile
;
1333 case VM_PURGABLE_EMPTY
:
1334 state
= kIOMemoryPurgeableEmpty
;
1337 state
= kIOMemoryPurgeableNonVolatile
;
1338 err
= kIOReturnNotReady
;
1350 extern "C" void dcache_incoherent_io_flush64(addr64_t pa
, unsigned int count
);
1351 extern "C" void dcache_incoherent_io_store64(addr64_t pa
, unsigned int count
);
1353 IOReturn
IOMemoryDescriptor::performOperation( IOOptionBits options
,
1354 IOByteCount offset
, IOByteCount length
)
1356 IOByteCount remaining
;
1357 void (*func
)(addr64_t pa
, unsigned int count
) = 0;
1361 case kIOMemoryIncoherentIOFlush
:
1362 func
= &dcache_incoherent_io_flush64
;
1364 case kIOMemoryIncoherentIOStore
:
1365 func
= &dcache_incoherent_io_store64
;
1370 return (kIOReturnUnsupported
);
1372 remaining
= length
= min(length
, getLength() - offset
);
1374 // (process another target segment?)
1379 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1383 // Clip segment length to remaining
1384 if (dstLen
> remaining
)
1387 (*func
)(dstAddr64
, dstLen
);
1390 remaining
-= dstLen
;
1393 return (remaining
? kIOReturnUnderrun
: kIOReturnSuccess
);
1397 extern vm_offset_t static_memory_end
;
1398 #define io_kernel_static_end static_memory_end
1400 extern vm_offset_t first_avail
;
1401 #define io_kernel_static_end first_avail
1404 static kern_return_t
1405 io_get_kernel_static_upl(
1407 vm_address_t offset
,
1408 vm_size_t
*upl_size
,
1410 upl_page_info_array_t page_list
,
1411 unsigned int *count
)
1413 unsigned int pageCount
, page
;
1416 pageCount
= atop_32(*upl_size
);
1417 if (pageCount
> *count
)
1422 for (page
= 0; page
< pageCount
; page
++)
1424 phys
= pmap_find_phys(kernel_pmap
, ((addr64_t
)offset
) + ptoa_64(page
));
1427 page_list
[page
].phys_addr
= phys
;
1428 page_list
[page
].pageout
= 0;
1429 page_list
[page
].absent
= 0;
1430 page_list
[page
].dirty
= 0;
1431 page_list
[page
].precious
= 0;
1432 page_list
[page
].device
= 0;
1435 return ((page
>= pageCount
) ? kIOReturnSuccess
: kIOReturnVMError
);
1438 IOReturn
IOGeneralMemoryDescriptor::wireVirtual(IODirection forDirection
)
1440 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1441 IOReturn error
= kIOReturnNoMemory
;
1443 ppnum_t mapBase
= 0;
1445 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
1447 assert(!_wireCount
);
1448 assert(kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeUIO
== type
);
1450 if (_pages
>= gIOMaximumMappedIOPageCount
)
1451 return kIOReturnNoResources
;
1453 dataP
= getDataP(_memoryEntries
);
1454 mapper
= dataP
->fMapper
;
1455 if (mapper
&& _pages
)
1456 mapBase
= mapper
->iovmAlloc(_pages
);
1458 // Note that appendBytes(NULL) zeros the data up to the
1460 _memoryEntries
->appendBytes(0, dataP
->fPageCnt
* sizeof(upl_page_info_t
));
1461 dataP
= 0; // May no longer be valid so lets not get tempted.
1463 if (forDirection
== kIODirectionNone
)
1464 forDirection
= _direction
;
1466 int uplFlags
; // This Mem Desc's default flags for upl creation
1467 switch (forDirection
)
1469 case kIODirectionOut
:
1470 // Pages do not need to be marked as dirty on commit
1471 uplFlags
= UPL_COPYOUT_FROM
;
1472 _flags
|= kIOMemoryPreparedReadOnly
;
1475 case kIODirectionIn
:
1477 uplFlags
= 0; // i.e. ~UPL_COPYOUT_FROM
1480 uplFlags
|= UPL_SET_IO_WIRE
| UPL_SET_LITE
;
1482 // Find the appropriate vm_map for the given task
1484 if (_task
== kernel_task
&& (kIOMemoryBufferPageable
& _flags
))
1487 { curMap
= get_task_map(_task
); }
1489 // Iterate over the vector of virtual ranges
1490 Ranges vec
= _ranges
;
1491 unsigned int pageIndex
= 0;
1492 IOByteCount mdOffset
= 0;
1493 for (UInt range
= 0; range
< _rangesCount
; range
++) {
1495 user_addr_t startPage
;
1496 IOByteCount numBytes
;
1498 // Get the startPage address and length of vec[range]
1499 getAddrLenForInd(startPage
, numBytes
, type
, vec
, range
);
1500 iopl
.fPageOffset
= (short) startPage
& PAGE_MASK
;
1501 numBytes
+= iopl
.fPageOffset
;
1502 startPage
= trunc_page_64(startPage
);
1505 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1507 iopl
.fMappedBase
= 0;
1509 // Iterate over the current range, creating UPLs
1511 dataP
= getDataP(_memoryEntries
);
1512 vm_address_t kernelStart
= (vm_address_t
) startPage
;
1516 else if (!sharedMem
) {
1517 assert(_task
== kernel_task
);
1518 theMap
= IOPageableMapForAddress(kernelStart
);
1523 upl_page_info_array_t pageInfo
= getPageList(dataP
);
1524 int ioplFlags
= uplFlags
;
1525 upl_page_list_ptr_t baseInfo
= &pageInfo
[pageIndex
];
1527 vm_size_t ioplSize
= round_page_32(numBytes
);
1528 unsigned int numPageInfo
= atop_32(ioplSize
);
1530 if (theMap
== kernel_map
&& kernelStart
< io_kernel_static_end
) {
1531 error
= io_get_kernel_static_upl(theMap
,
1538 else if (sharedMem
) {
1539 error
= memory_object_iopl_request(sharedMem
,
1549 error
= vm_map_create_upl(theMap
,
1559 if (error
!= KERN_SUCCESS
)
1562 error
= kIOReturnNoMemory
;
1564 if (baseInfo
->device
) {
1566 iopl
.fFlags
= kIOPLOnDevice
;
1567 // Don't translate device memory at all
1568 if (mapper
&& mapBase
) {
1569 mapper
->iovmFree(mapBase
, _pages
);
1571 iopl
.fMappedBase
= 0;
1577 mapper
->iovmInsert(mapBase
, pageIndex
,
1578 baseInfo
, numPageInfo
);
1581 iopl
.fIOMDOffset
= mdOffset
;
1582 iopl
.fPageInfo
= pageIndex
;
1584 if ((_flags
& kIOMemoryAutoPrepare
) && iopl
.fIOPL
)
1586 upl_commit(iopl
.fIOPL
, 0, 0);
1587 upl_deallocate(iopl
.fIOPL
);
1591 if (!_memoryEntries
->appendBytes(&iopl
, sizeof(iopl
))) {
1592 // Clean up partial created and unsaved iopl
1594 upl_abort(iopl
.fIOPL
, 0);
1595 upl_deallocate(iopl
.fIOPL
);
1600 // Check for a multiple iopl's in one virtual range
1601 pageIndex
+= numPageInfo
;
1602 mdOffset
-= iopl
.fPageOffset
;
1603 if (ioplSize
< numBytes
) {
1604 numBytes
-= ioplSize
;
1605 startPage
+= ioplSize
;
1606 mdOffset
+= ioplSize
;
1607 iopl
.fPageOffset
= 0;
1609 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1612 mdOffset
+= numBytes
;
1618 return kIOReturnSuccess
;
1622 dataP
= getDataP(_memoryEntries
);
1623 UInt done
= getNumIOPL(_memoryEntries
, dataP
);
1624 ioPLBlock
*ioplList
= getIOPLList(dataP
);
1626 for (UInt range
= 0; range
< done
; range
++)
1628 if (ioplList
[range
].fIOPL
) {
1629 upl_abort(ioplList
[range
].fIOPL
, 0);
1630 upl_deallocate(ioplList
[range
].fIOPL
);
1633 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
1635 if (mapper
&& mapBase
)
1636 mapper
->iovmFree(mapBase
, _pages
);
1645 * Prepare the memory for an I/O transfer. This involves paging in
1646 * the memory, if necessary, and wiring it down for the duration of
1647 * the transfer. The complete() method completes the processing of
1648 * the memory after the I/O transfer finishes. This method needn't
1649 * called for non-pageable memory.
1651 IOReturn
IOGeneralMemoryDescriptor::prepare(IODirection forDirection
)
1653 IOReturn error
= kIOReturnSuccess
;
1654 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1657 && (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeUIO
== type
) ) {
1658 error
= wireVirtual(forDirection
);
1665 return kIOReturnSuccess
;
1671 * Complete processing of the memory after an I/O transfer finishes.
1672 * This method should not be called unless a prepare was previously
1673 * issued; the prepare() and complete() must occur in pairs, before
1674 * before and after an I/O transfer involving pageable memory.
1677 IOReturn
IOGeneralMemoryDescriptor::complete(IODirection
/* forDirection */)
1682 return kIOReturnSuccess
;
1686 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1688 if (kIOMemoryTypePhysical
== type
) {
1689 /* kIOMemoryTypePhysical */
1693 ioGMDData
* dataP
= getDataP(_memoryEntries
);
1694 ioPLBlock
*ioplList
= getIOPLList(dataP
);
1695 UInt count
= getNumIOPL(_memoryEntries
, dataP
);
1697 if (dataP
->fMapper
&& _pages
&& ioplList
[0].fMappedBase
)
1698 dataP
->fMapper
->iovmFree(ioplList
[0].fMappedBase
, _pages
);
1700 // Only complete iopls that we created which are for TypeVirtual
1701 if (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeUIO
== type
) {
1702 for (UInt ind
= 0; ind
< count
; ind
++)
1703 if (ioplList
[ind
].fIOPL
) {
1704 upl_commit(ioplList
[ind
].fIOPL
, 0, 0);
1705 upl_deallocate(ioplList
[ind
].fIOPL
);
1709 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
1712 return kIOReturnSuccess
;
1715 IOReturn
IOGeneralMemoryDescriptor::doMap(
1716 vm_map_t addressMap
,
1717 IOVirtualAddress
* atAddress
,
1718 IOOptionBits options
,
1719 IOByteCount sourceOffset
,
1720 IOByteCount length
)
1723 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
1725 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1726 Ranges vec
= _ranges
;
1728 user_addr_t range0Addr
= 0;
1729 IOByteCount range0Len
= 0;
1732 getAddrLenForInd(range0Addr
, range0Len
, type
, vec
, 0);
1734 // mapping source == dest? (could be much better)
1736 && (addressMap
== get_task_map(_task
)) && (options
& kIOMapAnywhere
)
1737 && (1 == _rangesCount
) && (0 == sourceOffset
)
1738 && range0Addr
&& (length
<= range0Len
) ) {
1739 if (sizeof(user_addr_t
) > 4 && ((UInt64
) range0Addr
) >> 32)
1740 return kIOReturnOverrun
; // Doesn't fit in 32bit return field
1742 *atAddress
= range0Addr
;
1743 return( kIOReturnSuccess
);
1747 if( 0 == sharedMem
) {
1749 vm_size_t size
= ptoa_32(_pages
);
1753 memory_object_size_t actualSize
= size
;
1754 kr
= mach_make_memory_entry_64(get_task_map(_task
),
1755 &actualSize
, range0Addr
,
1756 VM_PROT_READ
| VM_PROT_WRITE
, &sharedMem
,
1759 if( (KERN_SUCCESS
== kr
) && (actualSize
!= round_page_32(size
))) {
1761 IOLog("mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
1762 range0Addr
, (UInt32
) actualSize
, size
);
1764 kr
= kIOReturnVMError
;
1765 ipc_port_release_send( sharedMem
);
1768 if( KERN_SUCCESS
!= kr
)
1770 sharedMem
= MACH_PORT_NULL
;
1774 memory_object_t pager
;
1775 unsigned int flags
= 0;
1777 IOPhysicalLength segLen
;
1779 pa
= getPhysicalSegment64( sourceOffset
, &segLen
);
1782 reserved
= IONew( ExpansionData
, 1 );
1786 reserved
->pagerContig
= (1 == _rangesCount
);
1787 reserved
->memory
= this;
1789 /*What cache mode do we need*/
1790 switch(options
& kIOMapCacheMask
) {
1792 case kIOMapDefaultCache
:
1794 flags
= IODefaultCacheBits(pa
);
1797 case kIOMapInhibitCache
:
1798 flags
= DEVICE_PAGER_CACHE_INHIB
|
1799 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
1802 case kIOMapWriteThruCache
:
1803 flags
= DEVICE_PAGER_WRITE_THROUGH
|
1804 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
1807 case kIOMapCopybackCache
:
1808 flags
= DEVICE_PAGER_COHERENT
;
1811 case kIOMapWriteCombineCache
:
1812 flags
= DEVICE_PAGER_CACHE_INHIB
|
1813 DEVICE_PAGER_COHERENT
;
1817 flags
|= reserved
->pagerContig
? DEVICE_PAGER_CONTIGUOUS
: 0;
1819 pager
= device_pager_setup( (memory_object_t
) 0, (int) reserved
,
1824 kr
= mach_memory_object_memory_entry_64( (host_t
) 1, false /*internal*/,
1825 size
, VM_PROT_READ
| VM_PROT_WRITE
, pager
, &sharedMem
);
1827 assert( KERN_SUCCESS
== kr
);
1828 if( KERN_SUCCESS
!= kr
) {
1829 device_pager_deallocate( pager
);
1830 pager
= MACH_PORT_NULL
;
1831 sharedMem
= MACH_PORT_NULL
;
1834 if( pager
&& sharedMem
)
1835 reserved
->devicePager
= pager
;
1837 IODelete( reserved
, ExpansionData
, 1 );
1843 _memEntry
= (void *) sharedMem
;
1849 kr
= kIOReturnVMError
;
1852 kr
= super::doMap( addressMap
, atAddress
,
1853 options
, sourceOffset
, length
);
1858 IOReturn
IOGeneralMemoryDescriptor::doUnmap(
1859 vm_map_t addressMap
,
1860 IOVirtualAddress logical
,
1861 IOByteCount length
)
1863 // could be much better
1864 if( _task
&& (addressMap
== get_task_map(_task
)) && (1 == _rangesCount
)) {
1866 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1867 user_addr_t range0Addr
;
1868 IOByteCount range0Len
;
1870 getAddrLenForInd(range0Addr
, range0Len
, type
, _ranges
, 0);
1871 if (logical
== range0Addr
&& length
<= range0Len
)
1872 return( kIOReturnSuccess
);
1875 return( super::doUnmap( addressMap
, logical
, length
));
1878 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1880 OSDefineMetaClassAndAbstractStructors( IOMemoryMap
, OSObject
)
1882 /* inline function implementation */
1883 IOPhysicalAddress
IOMemoryMap::getPhysicalAddress()
1884 { return( getPhysicalSegment( 0, 0 )); }
1888 #define super IOMemoryMap
1890 OSDefineMetaClassAndStructors(_IOMemoryMap
, IOMemoryMap
)
1892 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1894 bool _IOMemoryMap::initCompatible(
1895 IOMemoryDescriptor
* _memory
,
1896 IOMemoryMap
* _superMap
,
1897 IOByteCount _offset
,
1898 IOByteCount _length
)
1904 if( (_offset
+ _length
) > _superMap
->getLength())
1909 _superMap
->retain();
1910 superMap
= _superMap
;
1916 length
= _memory
->getLength();
1918 options
= superMap
->getMapOptions();
1919 logical
= superMap
->getVirtualAddress() + offset
;
1924 bool _IOMemoryMap::initWithDescriptor(
1925 IOMemoryDescriptor
* _memory
,
1927 IOVirtualAddress toAddress
,
1928 IOOptionBits _options
,
1929 IOByteCount _offset
,
1930 IOByteCount _length
)
1933 bool redir
= ((kIOMapUnique
|kIOMapReference
) == ((kIOMapUnique
|kIOMapReference
) & _options
));
1935 if ((!_memory
) || (!intoTask
))
1938 if( (_offset
+ _length
) > _memory
->getLength())
1945 addressMap
= get_task_map(intoTask
);
1948 vm_map_reference(addressMap
);
1949 addressTask
= intoTask
;
1950 logical
= toAddress
;
1960 length
= _memory
->getLength();
1962 if( options
& kIOMapStatic
)
1965 ok
= (kIOReturnSuccess
== _memory
->doMap( addressMap
, &toAddress
,
1966 _options
, offset
, length
));
1972 logical
= toAddress
;
1981 vm_map_deallocate(addressMap
);
1989 /* LP64todo - these need to expand */
1990 struct IOMemoryDescriptorMapAllocRef
1992 ipc_port_t sharedMem
;
1995 IOByteCount sourceOffset
;
1996 IOOptionBits options
;
1999 static kern_return_t
IOMemoryDescriptorMapAlloc(vm_map_t map
, void * _ref
)
2001 IOMemoryDescriptorMapAllocRef
* ref
= (IOMemoryDescriptorMapAllocRef
*)_ref
;
2005 if( ref
->sharedMem
) {
2006 vm_prot_t prot
= VM_PROT_READ
2007 | ((ref
->options
& kIOMapReadOnly
) ? 0 : VM_PROT_WRITE
);
2009 // set memory entry cache
2010 vm_prot_t memEntryCacheMode
= prot
| MAP_MEM_ONLY
;
2011 switch (ref
->options
& kIOMapCacheMask
)
2013 case kIOMapInhibitCache
:
2014 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
2017 case kIOMapWriteThruCache
:
2018 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
2021 case kIOMapWriteCombineCache
:
2022 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
2025 case kIOMapCopybackCache
:
2026 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
2029 case kIOMapDefaultCache
:
2031 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
2035 vm_size_t unused
= 0;
2037 err
= mach_make_memory_entry( NULL
/*unused*/, &unused
, 0 /*unused*/,
2038 memEntryCacheMode
, NULL
, ref
->sharedMem
);
2039 if (KERN_SUCCESS
!= err
)
2040 IOLog("MAP_MEM_ONLY failed %d\n", err
);
2044 ref
->size
, 0 /* mask */,
2045 (( ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2046 | VM_MAKE_TAG(VM_MEMORY_IOKIT
),
2047 ref
->sharedMem
, ref
->sourceOffset
,
2053 if( KERN_SUCCESS
!= err
) {
2060 err
= vm_allocate( map
, &ref
->mapped
, ref
->size
,
2061 ((ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2062 | VM_MAKE_TAG(VM_MEMORY_IOKIT
) );
2064 if( KERN_SUCCESS
!= err
) {
2069 // we have to make sure that these guys don't get copied if we fork.
2070 err
= vm_inherit( map
, ref
->mapped
, ref
->size
, VM_INHERIT_NONE
);
2071 assert( KERN_SUCCESS
== err
);
2080 IOReturn
IOMemoryDescriptor::doMap(
2081 vm_map_t addressMap
,
2082 IOVirtualAddress
* atAddress
,
2083 IOOptionBits options
,
2084 IOByteCount sourceOffset
,
2085 IOByteCount length
)
2087 IOReturn err
= kIOReturnSuccess
;
2088 memory_object_t pager
;
2089 vm_address_t logical
;
2090 IOByteCount pageOffset
;
2091 IOPhysicalAddress sourceAddr
;
2092 IOMemoryDescriptorMapAllocRef ref
;
2094 ref
.sharedMem
= (ipc_port_t
) _memEntry
;
2095 ref
.sourceOffset
= sourceOffset
;
2096 ref
.options
= options
;
2101 length
= getLength();
2103 sourceAddr
= getSourceSegment( sourceOffset
, NULL
);
2104 pageOffset
= sourceAddr
- trunc_page_32( sourceAddr
);
2106 ref
.size
= round_page_32( length
+ pageOffset
);
2108 if ((kIOMapReference
|kIOMapUnique
) == ((kIOMapReference
|kIOMapUnique
) & options
))
2114 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) *atAddress
;
2115 ref
.mapped
= mapping
->getVirtualAddress();
2119 err
= kIOReturnNotReadable
;
2124 flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
2125 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
2127 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) _memEntry
, 0, &size
, &redirUPL2
,
2132 err
= upl_transpose(redirUPL2
, mapping
->redirUPL
);
2133 if (kIOReturnSuccess
!= err
)
2135 IOLog("upl_transpose(%x)\n", err
);
2136 err
= kIOReturnSuccess
;
2141 upl_commit(redirUPL2
, NULL
, 0);
2142 upl_deallocate(redirUPL2
);
2146 // swap the memEntries since they now refer to different vm_objects
2147 void * me
= _memEntry
;
2148 _memEntry
= mapping
->memory
->_memEntry
;
2149 mapping
->memory
->_memEntry
= me
;
2155 logical
= *atAddress
;
2156 if( options
& kIOMapAnywhere
)
2157 // vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
2160 ref
.mapped
= trunc_page_32( logical
);
2161 if( (logical
- ref
.mapped
) != pageOffset
) {
2162 err
= kIOReturnVMError
;
2167 if( ref
.sharedMem
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2168 err
= IOIteratePageableMaps( ref
.size
, &IOMemoryDescriptorMapAlloc
, &ref
);
2170 err
= IOMemoryDescriptorMapAlloc( addressMap
, &ref
);
2173 if( err
!= KERN_SUCCESS
)
2177 pager
= (memory_object_t
) reserved
->devicePager
;
2179 pager
= MACH_PORT_NULL
;
2181 if( !ref
.sharedMem
|| pager
)
2182 err
= handleFault( pager
, addressMap
, ref
.mapped
, sourceOffset
, length
, options
);
2186 if( err
!= KERN_SUCCESS
) {
2188 doUnmap( addressMap
, ref
.mapped
, ref
.size
);
2191 *atAddress
= ref
.mapped
+ pageOffset
;
2197 kIOMemoryRedirected
= 0x00010000
2200 IOReturn
IOMemoryDescriptor::handleFault(
2202 vm_map_t addressMap
,
2203 IOVirtualAddress address
,
2204 IOByteCount sourceOffset
,
2206 IOOptionBits options
)
2208 IOReturn err
= kIOReturnSuccess
;
2209 memory_object_t pager
= (memory_object_t
) _pager
;
2213 IOByteCount pageOffset
;
2214 IOByteCount pagerOffset
;
2215 IOPhysicalLength segLen
;
2220 if( kIOMemoryRedirected
& _flags
) {
2222 IOLog("sleep mem redirect %p, %lx\n", this, sourceOffset
);
2226 } while( kIOMemoryRedirected
& _flags
);
2229 return( kIOReturnSuccess
);
2232 physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
);
2234 pageOffset
= physAddr
- trunc_page_64( physAddr
);
2235 pagerOffset
= sourceOffset
;
2237 size
= length
+ pageOffset
;
2238 physAddr
-= pageOffset
;
2240 segLen
+= pageOffset
;
2243 // in the middle of the loop only map whole pages
2244 if( segLen
>= bytes
)
2246 else if( segLen
!= trunc_page_32( segLen
))
2247 err
= kIOReturnVMError
;
2248 if( physAddr
!= trunc_page_64( physAddr
))
2249 err
= kIOReturnBadArgument
;
2252 if( kIOLogMapping
& gIOKitDebug
)
2253 IOLog("_IOMemoryMap::map(%p) %08lx->%08qx:%08lx\n",
2254 addressMap
, address
+ pageOffset
, physAddr
+ pageOffset
,
2255 segLen
- pageOffset
);
2263 /* i386 doesn't support faulting on device memory yet */
2264 if( addressMap
&& (kIOReturnSuccess
== err
))
2265 err
= IOMapPages( addressMap
, address
, (IOPhysicalAddress
) physAddr
, segLen
, options
);
2266 assert( KERN_SUCCESS
== err
);
2272 if( reserved
&& reserved
->pagerContig
) {
2273 IOPhysicalLength allLen
;
2276 allPhys
= getPhysicalSegment64( 0, &allLen
);
2278 err
= device_pager_populate_object( pager
, 0, allPhys
>> PAGE_SHIFT
, round_page_32(allLen
) );
2283 (page
< segLen
) && (KERN_SUCCESS
== err
);
2284 page
+= page_size
) {
2285 err
= device_pager_populate_object(pager
, pagerOffset
,
2286 (ppnum_t
)((physAddr
+ page
) >> PAGE_SHIFT
), page_size
);
2287 pagerOffset
+= page_size
;
2290 assert( KERN_SUCCESS
== err
);
2296 /* *** Temporary Workaround *** */
2298 /* This call to vm_fault causes an early pmap level resolution */
2299 /* of the mappings created above. Need for this is in absolute */
2300 /* violation of the basic tenet that the pmap layer is a cache. */
2301 /* Further, it implies a serious I/O architectural violation on */
2302 /* the part of some user of the mapping. As of this writing, */
2303 /* the call to vm_fault is needed because the NVIDIA driver */
2304 /* makes a call to pmap_extract. The NVIDIA driver needs to be */
2305 /* fixed as soon as possible. The NVIDIA driver should not */
2306 /* need to query for this info as it should know from the doMap */
2307 /* call where the physical memory is mapped. When a query is */
2308 /* necessary to find a physical mapping, it should be done */
2309 /* through an iokit call which includes the mapped memory */
2310 /* handle. This is required for machine architecture independence.*/
2312 if(!(kIOMemoryRedirected
& _flags
)) {
2313 vm_fault(addressMap
,
2314 (vm_map_offset_t
)address
,
2315 VM_PROT_READ
|VM_PROT_WRITE
,
2316 FALSE
, THREAD_UNINT
, NULL
,
2317 (vm_map_offset_t
)0);
2320 /* *** Temporary Workaround *** */
2323 sourceOffset
+= segLen
- pageOffset
;
2329 && (physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
)));
2332 err
= kIOReturnBadArgument
;
2337 IOReturn
IOMemoryDescriptor::doUnmap(
2338 vm_map_t addressMap
,
2339 IOVirtualAddress logical
,
2340 IOByteCount length
)
2345 if( kIOLogMapping
& gIOKitDebug
)
2346 kprintf("IOMemoryDescriptor::doUnmap(%x) %08x:%08x\n",
2347 addressMap
, logical
, length
);
2350 if( true /* && (addressMap == kernel_map) || (addressMap == get_task_map(current_task()))*/) {
2352 if( _memEntry
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2353 addressMap
= IOPageableMapForAddress( logical
);
2355 err
= vm_deallocate( addressMap
, logical
, length
);
2358 err
= kIOReturnSuccess
;
2363 IOReturn
IOMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2365 IOReturn err
= kIOReturnSuccess
;
2366 _IOMemoryMap
* mapping
= 0;
2372 _flags
|= kIOMemoryRedirected
;
2374 _flags
&= ~kIOMemoryRedirected
;
2377 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
2378 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject()))
2379 mapping
->redirect( safeTask
, doRedirect
);
2392 // temporary binary compatibility
2393 IOSubMemoryDescriptor
* subMem
;
2394 if( (subMem
= OSDynamicCast( IOSubMemoryDescriptor
, this)))
2395 err
= subMem
->redirect( safeTask
, doRedirect
);
2397 err
= kIOReturnSuccess
;
2402 IOReturn
IOSubMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2404 return( _parent
->redirect( safeTask
, doRedirect
));
2407 IOReturn
_IOMemoryMap::redirect( task_t safeTask
, bool doRedirect
)
2409 IOReturn err
= kIOReturnSuccess
;
2412 // err = ((_IOMemoryMap *)superMap)->redirect( safeTask, doRedirect );
2416 if( logical
&& addressMap
2417 && (!safeTask
|| (get_task_map(safeTask
) != addressMap
))
2418 && (0 == (options
& kIOMapStatic
)))
2420 IOUnmapPages( addressMap
, logical
, length
);
2421 if(!doRedirect
&& safeTask
2422 && ((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
))
2424 err
= vm_deallocate( addressMap
, logical
, length
);
2425 err
= memory
->doMap( addressMap
, &logical
,
2426 (options
& ~kIOMapAnywhere
) /*| kIOMapReserve*/,
2429 err
= kIOReturnSuccess
;
2431 IOLog("IOMemoryMap::redirect(%d, %p) %x:%lx from %p\n", doRedirect
, this, logical
, length
, addressMap
);
2437 if (((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2439 && (doRedirect
!= (0 != (memory
->_flags
& kIOMemoryRedirected
))))
2440 memory
->redirect(safeTask
, doRedirect
);
2445 IOReturn
_IOMemoryMap::unmap( void )
2451 if( logical
&& addressMap
&& (0 == superMap
)
2452 && (0 == (options
& kIOMapStatic
))) {
2454 err
= memory
->doUnmap( addressMap
, logical
, length
);
2455 vm_map_deallocate(addressMap
);
2459 err
= kIOReturnSuccess
;
2468 void _IOMemoryMap::taskDied( void )
2472 vm_map_deallocate(addressMap
);
2480 // Overload the release mechanism. All mappings must be a member
2481 // of a memory descriptors _mappings set. This means that we
2482 // always have 2 references on a mapping. When either of these mappings
2483 // are released we need to free ourselves.
2484 void _IOMemoryMap::taggedRelease(const void *tag
) const
2487 super::taggedRelease(tag
, 2);
2491 void _IOMemoryMap::free()
2497 memory
->removeMapping( this);
2502 if (owner
&& (owner
!= memory
))
2505 owner
->removeMapping(this);
2510 superMap
->release();
2513 upl_commit(redirUPL
, NULL
, 0);
2514 upl_deallocate(redirUPL
);
2520 IOByteCount
_IOMemoryMap::getLength()
2525 IOVirtualAddress
_IOMemoryMap::getVirtualAddress()
2530 task_t
_IOMemoryMap::getAddressTask()
2533 return( superMap
->getAddressTask());
2535 return( addressTask
);
2538 IOOptionBits
_IOMemoryMap::getMapOptions()
2543 IOMemoryDescriptor
* _IOMemoryMap::getMemoryDescriptor()
2548 _IOMemoryMap
* _IOMemoryMap::copyCompatible(
2549 IOMemoryDescriptor
* owner
,
2551 IOVirtualAddress toAddress
,
2552 IOOptionBits _options
,
2553 IOByteCount _offset
,
2554 IOByteCount _length
)
2556 _IOMemoryMap
* mapping
;
2558 if( (!task
) || (!addressMap
) || (addressMap
!= get_task_map(task
)))
2560 if( options
& kIOMapUnique
)
2562 if( (options
^ _options
) & kIOMapReadOnly
)
2564 if( (kIOMapDefaultCache
!= (_options
& kIOMapCacheMask
))
2565 && ((options
^ _options
) & kIOMapCacheMask
))
2568 if( (0 == (_options
& kIOMapAnywhere
)) && (logical
!= toAddress
))
2571 if( _offset
< offset
)
2576 if( (_offset
+ _length
) > length
)
2579 if( (length
== _length
) && (!_offset
)) {
2584 mapping
= new _IOMemoryMap
;
2586 && !mapping
->initCompatible( owner
, this, _offset
, _length
)) {
2595 IOPhysicalAddress
_IOMemoryMap::getPhysicalSegment( IOByteCount _offset
,
2596 IOPhysicalLength
* _length
)
2598 IOPhysicalAddress address
;
2601 address
= memory
->getPhysicalSegment( offset
+ _offset
, _length
);
2607 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2610 #define super OSObject
2612 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2614 void IOMemoryDescriptor::initialize( void )
2616 if( 0 == gIOMemoryLock
)
2617 gIOMemoryLock
= IORecursiveLockAlloc();
2619 IORegistryEntry::getRegistryRoot()->setProperty(kIOMaximumMappedIOByteCountKey
,
2620 ptoa_64(gIOMaximumMappedIOPageCount
), 64);
2623 void IOMemoryDescriptor::free( void )
2626 _mappings
->release();
2631 IOMemoryMap
* IOMemoryDescriptor::setMapping(
2633 IOVirtualAddress mapAddress
,
2634 IOOptionBits options
)
2636 _IOMemoryMap
* newMap
;
2638 newMap
= new _IOMemoryMap
;
2643 && !newMap
->initWithDescriptor( this, intoTask
, mapAddress
,
2644 options
| kIOMapStatic
, 0, getLength() )) {
2649 addMapping( newMap
);
2656 IOMemoryMap
* IOMemoryDescriptor::map(
2657 IOOptionBits options
)
2660 return( makeMapping( this, kernel_task
, 0,
2661 options
| kIOMapAnywhere
,
2665 IOMemoryMap
* IOMemoryDescriptor::map(
2667 IOVirtualAddress toAddress
,
2668 IOOptionBits options
,
2670 IOByteCount length
)
2673 length
= getLength();
2675 return( makeMapping( this, intoTask
, toAddress
, options
, offset
, length
));
2678 IOReturn
_IOMemoryMap::redirect(IOMemoryDescriptor
* newBackingMemory
,
2679 IOOptionBits options
,
2682 IOReturn err
= kIOReturnSuccess
;
2683 IOMemoryDescriptor
* physMem
= 0;
2687 if (logical
&& addressMap
) do
2689 if ((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2697 vm_size_t size
= length
;
2698 int flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
2699 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
2700 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) memory
->_memEntry
, 0, &size
, &redirUPL
,
2707 IOUnmapPages( addressMap
, logical
, length
);
2708 physMem
->redirect(0, true);
2712 if (newBackingMemory
)
2714 if (newBackingMemory
!= memory
)
2716 if (this != newBackingMemory
->makeMapping(newBackingMemory
, addressTask
, (IOVirtualAddress
) this,
2717 options
| kIOMapUnique
| kIOMapReference
,
2719 err
= kIOReturnError
;
2723 upl_commit(redirUPL
, NULL
, 0);
2724 upl_deallocate(redirUPL
);
2728 physMem
->redirect(0, false);
2741 IOMemoryMap
* IOMemoryDescriptor::makeMapping(
2742 IOMemoryDescriptor
* owner
,
2744 IOVirtualAddress toAddress
,
2745 IOOptionBits options
,
2747 IOByteCount length
)
2749 IOMemoryDescriptor
* mapDesc
= 0;
2750 _IOMemoryMap
* mapping
= 0;
2757 if (kIOMapUnique
& options
)
2759 IOPhysicalAddress phys
;
2760 IOByteCount physLen
;
2765 if ((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2767 phys
= getPhysicalSegment(offset
, &physLen
);
2768 if (!phys
|| (physLen
< length
))
2771 mapDesc
= IOMemoryDescriptor::withPhysicalAddress(
2772 phys
, length
, _direction
);
2783 if (kIOMapReference
& options
)
2785 mapping
= (_IOMemoryMap
*) toAddress
;
2789 uint32_t pageOffset1
= mapDesc
->getSourceSegment( offset
, NULL
);
2790 pageOffset1
-= trunc_page_32( pageOffset1
);
2792 uint32_t pageOffset2
= mapping
->getVirtualAddress();
2793 pageOffset2
-= trunc_page_32( pageOffset2
);
2795 if (pageOffset1
!= pageOffset2
)
2796 IOLog("::redirect can't map offset %x to addr %x\n",
2797 pageOffset1
, mapping
->getVirtualAddress());
2801 if (!mapping
->initWithDescriptor( mapDesc
, intoTask
, toAddress
, options
,
2805 IOLog("Didn't redirect map %08lx : %08lx\n", offset
, length
);
2810 mapping
->owner
->removeMapping(mapping
);
2816 // look for an existing mapping
2817 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
2819 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject())) {
2821 if( (mapping
= mapping
->copyCompatible(
2822 owner
, intoTask
, toAddress
,
2823 options
| kIOMapReference
,
2834 if( mapping
|| (options
& kIOMapReference
))
2842 mapping
= new _IOMemoryMap
;
2844 && !mapping
->initWithDescriptor( mapDesc
, intoTask
, toAddress
, options
,
2847 IOLog("Didn't make map %08lx : %08lx\n", offset
, length
);
2860 mapping
->owner
= owner
;
2861 owner
->addMapping( mapping
);
2873 void IOMemoryDescriptor::addMapping(
2874 IOMemoryMap
* mapping
)
2878 _mappings
= OSSet::withCapacity(1);
2880 _mappings
->setObject( mapping
);
2884 void IOMemoryDescriptor::removeMapping(
2885 IOMemoryMap
* mapping
)
2888 _mappings
->removeObject( mapping
);
2891 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2894 #define super IOMemoryDescriptor
2896 OSDefineMetaClassAndStructors(IOSubMemoryDescriptor
, IOMemoryDescriptor
)
2898 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2900 bool IOSubMemoryDescriptor::initSubRange( IOMemoryDescriptor
* parent
,
2901 IOByteCount offset
, IOByteCount length
,
2902 IODirection direction
)
2907 if( (offset
+ length
) > parent
->getLength())
2911 * We can check the _parent instance variable before having ever set it
2912 * to an initial value because I/O Kit guarantees that all our instance
2913 * variables are zeroed on an object's allocation.
2921 * An existing memory descriptor is being retargeted to
2922 * point to somewhere else. Clean up our present state.
2933 _direction
= direction
;
2934 _tag
= parent
->getTag();
2939 void IOSubMemoryDescriptor::free( void )
2948 IOPhysicalAddress
IOSubMemoryDescriptor::getPhysicalSegment( IOByteCount offset
,
2949 IOByteCount
* length
)
2951 IOPhysicalAddress address
;
2952 IOByteCount actualLength
;
2954 assert(offset
<= _length
);
2959 if( offset
>= _length
)
2962 address
= _parent
->getPhysicalSegment( offset
+ _start
, &actualLength
);
2964 if( address
&& length
)
2965 *length
= min( _length
- offset
, actualLength
);
2971 IOReturn
IOSubMemoryDescriptor::doMap(
2972 vm_map_t addressMap
,
2973 IOVirtualAddress
* atAddress
,
2974 IOOptionBits options
,
2975 IOByteCount sourceOffset
,
2976 IOByteCount length
)
2978 if( sourceOffset
>= _length
)
2979 return( kIOReturnOverrun
);
2980 return (_parent
->doMap(addressMap
, atAddress
, options
, sourceOffset
+ _start
, length
));
2983 IOPhysicalAddress
IOSubMemoryDescriptor::getSourceSegment( IOByteCount offset
,
2984 IOByteCount
* length
)
2986 IOPhysicalAddress address
;
2987 IOByteCount actualLength
;
2989 assert(offset
<= _length
);
2994 if( offset
>= _length
)
2997 address
= _parent
->getSourceSegment( offset
+ _start
, &actualLength
);
2999 if( address
&& length
)
3000 *length
= min( _length
- offset
, actualLength
);
3005 void * IOSubMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
3006 IOByteCount
* lengthOfSegment
)
3011 IOByteCount
IOSubMemoryDescriptor::readBytes(IOByteCount offset
,
3012 void * bytes
, IOByteCount length
)
3014 IOByteCount byteCount
;
3016 assert(offset
<= _length
);
3018 if( offset
>= _length
)
3022 byteCount
= _parent
->readBytes( _start
+ offset
, bytes
,
3023 min(length
, _length
- offset
) );
3026 return( byteCount
);
3029 IOByteCount
IOSubMemoryDescriptor::writeBytes(IOByteCount offset
,
3030 const void* bytes
, IOByteCount length
)
3032 IOByteCount byteCount
;
3034 assert(offset
<= _length
);
3036 if( offset
>= _length
)
3040 byteCount
= _parent
->writeBytes( _start
+ offset
, bytes
,
3041 min(length
, _length
- offset
) );
3044 return( byteCount
);
3047 IOReturn
IOSubMemoryDescriptor::setPurgeable( IOOptionBits newState
,
3048 IOOptionBits
* oldState
)
3053 err
= _parent
->setPurgeable( newState
, oldState
);
3059 IOReturn
IOSubMemoryDescriptor::performOperation( IOOptionBits options
,
3060 IOByteCount offset
, IOByteCount length
)
3064 assert(offset
<= _length
);
3066 if( offset
>= _length
)
3067 return( kIOReturnOverrun
);
3070 err
= _parent
->performOperation( options
, _start
+ offset
,
3071 min(length
, _length
- offset
) );
3077 IOReturn
IOSubMemoryDescriptor::prepare(
3078 IODirection forDirection
)
3083 err
= _parent
->prepare( forDirection
);
3089 IOReturn
IOSubMemoryDescriptor::complete(
3090 IODirection forDirection
)
3095 err
= _parent
->complete( forDirection
);
3101 IOMemoryMap
* IOSubMemoryDescriptor::makeMapping(
3102 IOMemoryDescriptor
* owner
,
3104 IOVirtualAddress toAddress
,
3105 IOOptionBits options
,
3107 IOByteCount length
)
3109 IOMemoryMap
* mapping
= 0;
3111 if (!(kIOMapUnique
& options
))
3112 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3114 toAddress
- (_start
+ offset
),
3115 options
| kIOMapReference
,
3116 _start
+ offset
, length
);
3119 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3122 options
, _start
+ offset
, length
);
3125 mapping
= super::makeMapping( owner
, intoTask
, toAddress
, options
,
3134 IOSubMemoryDescriptor::initWithAddress(void * address
,
3136 IODirection direction
)
3142 IOSubMemoryDescriptor::initWithAddress(vm_address_t address
,
3144 IODirection direction
,
3151 IOSubMemoryDescriptor::initWithPhysicalAddress(
3152 IOPhysicalAddress address
,
3154 IODirection direction
)
3160 IOSubMemoryDescriptor::initWithRanges(
3161 IOVirtualRange
* ranges
,
3163 IODirection direction
,
3171 IOSubMemoryDescriptor::initWithPhysicalRanges( IOPhysicalRange
* ranges
,
3173 IODirection direction
,
3179 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3181 bool IOGeneralMemoryDescriptor::serialize(OSSerialize
* s
) const
3183 OSSymbol
const *keys
[2];
3184 OSObject
*values
[2];
3186 user_addr_t address
;
3189 unsigned int index
, nRanges
;
3192 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
3194 if (s
== NULL
) return false;
3195 if (s
->previouslySerialized(this)) return true;
3197 // Pretend we are an array.
3198 if (!s
->addXMLStartTag(this, "array")) return false;
3200 nRanges
= _rangesCount
;
3201 vcopy
= (SerData
*) IOMalloc(sizeof(SerData
) * nRanges
);
3202 if (vcopy
== 0) return false;
3204 keys
[0] = OSSymbol::withCString("address");
3205 keys
[1] = OSSymbol::withCString("length");
3208 values
[0] = values
[1] = 0;
3210 // From this point on we can go to bail.
3212 // Copy the volatile data so we don't have to allocate memory
3213 // while the lock is held.
3215 if (nRanges
== _rangesCount
) {
3216 Ranges vec
= _ranges
;
3217 for (index
= 0; index
< nRanges
; index
++) {
3218 user_addr_t addr
; IOByteCount len
;
3219 getAddrLenForInd(addr
, len
, type
, vec
, index
);
3220 vcopy
[index
].address
= addr
;
3221 vcopy
[index
].length
= len
;
3224 // The descriptor changed out from under us. Give up.
3231 for (index
= 0; index
< nRanges
; index
++)
3233 user_addr_t addr
= vcopy
[index
].address
;
3234 IOByteCount len
= (IOByteCount
) vcopy
[index
].length
;
3236 OSNumber::withNumber(addr
, (((UInt64
) addr
) >> 32)? 64 : 32);
3237 if (values
[0] == 0) {
3241 values
[1] = OSNumber::withNumber(len
, sizeof(len
) * 8);
3242 if (values
[1] == 0) {
3246 OSDictionary
*dict
= OSDictionary::withObjects((const OSObject
**)values
, (const OSSymbol
**)keys
, 2);
3251 values
[0]->release();
3252 values
[1]->release();
3253 values
[0] = values
[1] = 0;
3255 result
= dict
->serialize(s
);
3261 result
= s
->addXMLEndTag("array");
3265 values
[0]->release();
3267 values
[1]->release();
3273 IOFree(vcopy
, sizeof(IOVirtualRange
) * nRanges
);
3277 bool IOSubMemoryDescriptor::serialize(OSSerialize
* s
) const
3282 if (s
->previouslySerialized(this)) return true;
3284 // Pretend we are a dictionary.
3285 // We must duplicate the functionality of OSDictionary here
3286 // because otherwise object references will not work;
3287 // they are based on the value of the object passed to
3288 // previouslySerialized and addXMLStartTag.
3290 if (!s
->addXMLStartTag(this, "dict")) return false;
3292 char const *keys
[3] = {"offset", "length", "parent"};
3294 OSObject
*values
[3];
3295 values
[0] = OSNumber::withNumber(_start
, sizeof(_start
) * 8);
3298 values
[1] = OSNumber::withNumber(_length
, sizeof(_length
) * 8);
3299 if (values
[1] == 0) {
3300 values
[0]->release();
3303 values
[2] = _parent
;
3306 for (int i
=0; i
<3; i
++) {
3307 if (!s
->addString("<key>") ||
3308 !s
->addString(keys
[i
]) ||
3309 !s
->addXMLEndTag("key") ||
3310 !values
[i
]->serialize(s
)) {
3315 values
[0]->release();
3316 values
[1]->release();
3321 return s
->addXMLEndTag("dict");
3324 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3326 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 0);
3327 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 1);
3328 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 2);
3329 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 3);
3330 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 4);
3331 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 5);
3332 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 6);
3333 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 7);
3334 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 8);
3335 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 9);
3336 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 10);
3337 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 11);
3338 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 12);
3339 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 13);
3340 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 14);
3341 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 15);
3343 /* ex-inline function implementation */
3344 IOPhysicalAddress
IOMemoryDescriptor::getPhysicalAddress()
3345 { return( getPhysicalSegment( 0, 0 )); }