2 * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
36 // 45678901234567890123456789012345678901234567890123456789012345678901234567890
37 #include <sys/cdefs.h>
39 #include <IOKit/assert.h>
40 #include <IOKit/system.h>
41 #include <IOKit/IOLib.h>
42 #include <IOKit/IOMemoryDescriptor.h>
43 #include <IOKit/IOMapper.h>
44 #include <IOKit/IOKitKeysPrivate.h>
46 #include <IOKit/IOKitDebug.h>
48 #include "IOKitKernelInternal.h"
50 #include <libkern/c++/OSContainers.h>
51 #include <libkern/c++/OSDictionary.h>
52 #include <libkern/c++/OSArray.h>
53 #include <libkern/c++/OSSymbol.h>
54 #include <libkern/c++/OSNumber.h>
60 #include <vm/vm_pageout.h>
61 #include <vm/vm_shared_memory_server.h>
62 #include <mach/memory_object_types.h>
63 #include <device/device_port.h>
66 #include <mach/vm_prot.h>
67 #include <vm/vm_fault.h>
68 struct phys_entry
*pmap_find_physentry(ppnum_t pa
);
71 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
72 void ipc_port_release_send(ipc_port_t port
);
74 /* Copy between a physical page and a virtual address in the given vm_map */
75 kern_return_t
copypv(addr64_t source
, addr64_t sink
, unsigned int size
, int which
);
79 memory_object_t pager
,
84 device_pager_deallocate(
87 device_pager_populate_object(
88 memory_object_t pager
,
89 vm_object_offset_t offset
,
93 memory_object_iopl_request(
95 memory_object_offset_t offset
,
98 upl_page_info_array_t user_page_list
,
99 unsigned int *page_list_count
,
102 unsigned int IOTranslateCacheBits(struct phys_entry
*pp
);
106 #define kIOMaximumMappedIOByteCount (512*1024*1024)
108 static IOMapper
* gIOSystemMapper
;
109 static ppnum_t gIOMaximumMappedIOPageCount
= atop_32(kIOMaximumMappedIOByteCount
);
111 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
113 OSDefineMetaClassAndAbstractStructors( IOMemoryDescriptor
, OSObject
)
115 #define super IOMemoryDescriptor
117 OSDefineMetaClassAndStructors(IOGeneralMemoryDescriptor
, IOMemoryDescriptor
)
119 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
121 static IORecursiveLock
* gIOMemoryLock
;
123 #define LOCK IORecursiveLockLock( gIOMemoryLock)
124 #define UNLOCK IORecursiveLockUnlock( gIOMemoryLock)
125 #define SLEEP IORecursiveLockSleep( gIOMemoryLock, (void *)this, THREAD_UNINT)
127 IORecursiveLockWakeup( gIOMemoryLock, (void *)this, /* one-thread */ false)
129 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
131 class _IOMemoryMap
: public IOMemoryMap
133 OSDeclareDefaultStructors(_IOMemoryMap
)
135 IOMemoryDescriptor
* memory
;
136 IOMemoryMap
* superMap
;
139 IOVirtualAddress logical
;
142 IOOptionBits options
;
144 ipc_port_t redirEntry
;
145 IOMemoryDescriptor
* owner
;
148 virtual void taggedRelease(const void *tag
= 0) const;
153 // IOMemoryMap methods
154 virtual IOVirtualAddress
getVirtualAddress();
155 virtual IOByteCount
getLength();
156 virtual task_t
getAddressTask();
157 virtual IOMemoryDescriptor
* getMemoryDescriptor();
158 virtual IOOptionBits
getMapOptions();
160 virtual IOReturn
unmap();
161 virtual void taskDied();
163 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
164 IOOptionBits options
,
165 IOByteCount offset
= 0);
167 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
168 IOByteCount
* length
);
170 // for IOMemoryDescriptor use
171 _IOMemoryMap
* copyCompatible(
172 IOMemoryDescriptor
* owner
,
174 IOVirtualAddress toAddress
,
175 IOOptionBits options
,
177 IOByteCount length
);
180 IOMemoryDescriptor
* memory
,
181 IOMemoryMap
* superMap
,
183 IOByteCount length
);
185 bool initWithDescriptor(
186 IOMemoryDescriptor
* memory
,
188 IOVirtualAddress toAddress
,
189 IOOptionBits options
,
191 IOByteCount length
);
194 task_t intoTask
, bool redirect
);
197 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
199 // Some data structures and accessor macros used by the initWithOptions
202 enum ioPLBlockFlags
{
203 kIOPLOnDevice
= 0x00000001,
204 kIOPLExternUPL
= 0x00000002,
207 struct typePersMDData
209 const IOGeneralMemoryDescriptor
*fMD
;
210 ipc_port_t fMemEntry
;
215 vm_address_t fIOMDOffset
; // The offset of this iopl in descriptor
216 vm_offset_t fPageInfo
; // Pointer to page list or index into it
217 ppnum_t fMappedBase
; // Page number of first page in this iopl
218 unsigned int fPageOffset
; // Offset within first page of iopl
219 unsigned int fFlags
; // Flags
224 unsigned int fPageCnt
;
225 upl_page_info_t fPageList
[];
229 #define getDataP(osd) ((ioGMDData *) (osd)->getBytesNoCopy())
230 #define getIOPLList(d) ((ioPLBlock *) &(d->fPageList[d->fPageCnt]))
231 #define getNumIOPL(osd, d) \
232 (((osd)->getLength() - ((char *) getIOPLList(d) - (char *) d)) / sizeof(ioPLBlock))
233 #define getPageList(d) (&(d->fPageList[0]))
234 #define computeDataSize(p, u) \
235 (sizeof(ioGMDData) + p * sizeof(upl_page_info_t) + u * sizeof(ioPLBlock))
238 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
240 #define next_page(a) ( trunc_page_32(a) + PAGE_SIZE )
245 kern_return_t
device_data_action(
247 ipc_port_t device_pager
,
248 vm_prot_t protection
,
249 vm_object_offset_t offset
,
252 struct ExpansionData
{
254 unsigned int pagerContig
:1;
255 unsigned int unused
:31;
256 IOMemoryDescriptor
* memory
;
259 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
260 IOMemoryDescriptor
* memDesc
;
263 memDesc
= ref
->memory
;
267 kr
= memDesc
->handleFault( device_pager
, 0, 0,
268 offset
, size
, kIOMapDefaultCache
/*?*/);
278 kern_return_t
device_close(
281 struct ExpansionData
{
283 unsigned int pagerContig
:1;
284 unsigned int unused
:31;
285 IOMemoryDescriptor
* memory
;
287 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
289 IODelete( ref
, ExpansionData
, 1 );
291 return( kIOReturnSuccess
);
295 // Note this inline function uses C++ reference arguments to return values
296 // This means that pointers are not passed and NULLs don't have to be
297 // checked for as a NULL reference is illegal.
299 getAddrLenForInd(user_addr_t
&addr
, IOPhysicalLength
&len
, // Output variables
300 UInt32 type
, IOGeneralMemoryDescriptor::Ranges r
, UInt32 ind
)
302 assert(kIOMemoryTypePhysical
== type
|| kIOMemoryTypeUIO
== type
303 || kIOMemoryTypeVirtual
== type
);
304 if (kIOMemoryTypeUIO
== type
) {
306 uio_getiov((uio_t
) r
.uio
, ind
, &addr
, &us
); len
= us
;
309 IOVirtualRange cur
= r
.v
[ind
];
315 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
320 * Create a new IOMemoryDescriptor. The buffer is a virtual address
321 * relative to the specified task. If no task is supplied, the kernel
325 IOMemoryDescriptor::withAddress(void * address
,
327 IODirection direction
)
329 return IOMemoryDescriptor::
330 withAddress((vm_address_t
) address
, length
, direction
, kernel_task
);
334 IOMemoryDescriptor::withAddress(vm_address_t address
,
336 IODirection direction
,
339 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
342 if (that
->initWithAddress(address
, length
, direction
, task
))
351 IOMemoryDescriptor::withPhysicalAddress(
352 IOPhysicalAddress address
,
354 IODirection direction
)
356 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
358 && !self
->initWithPhysicalAddress(address
, length
, direction
)) {
367 IOMemoryDescriptor::withRanges( IOVirtualRange
* ranges
,
369 IODirection direction
,
373 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
376 if (that
->initWithRanges(ranges
, withCount
, direction
, task
, asReference
))
388 * Create a new IOMemoryDescriptor. The buffer is made up of several
389 * virtual address ranges, from a given task.
391 * Passing the ranges as a reference will avoid an extra allocation.
394 IOMemoryDescriptor::withOptions(void * buffers
,
401 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
404 && !self
->initWithOptions(buffers
, count
, offset
, task
, opts
, mapper
))
413 // Can't leave abstract but this should never be used directly,
414 bool IOMemoryDescriptor::initWithOptions(void * buffers
,
418 IOOptionBits options
,
421 // @@@ gvdl: Should I panic?
422 panic("IOMD::initWithOptions called\n");
427 IOMemoryDescriptor::withPhysicalRanges( IOPhysicalRange
* ranges
,
429 IODirection direction
,
432 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
435 if (that
->initWithPhysicalRanges(ranges
, withCount
, direction
, asReference
))
444 IOMemoryDescriptor::withSubRange(IOMemoryDescriptor
* of
,
447 IODirection direction
)
449 IOSubMemoryDescriptor
*self
= new IOSubMemoryDescriptor
;
451 if (self
&& !self
->initSubRange(of
, offset
, length
, direction
)) {
458 IOMemoryDescriptor
* IOMemoryDescriptor::
459 withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
)
461 IOGeneralMemoryDescriptor
*origGenMD
=
462 OSDynamicCast(IOGeneralMemoryDescriptor
, originalMD
);
465 return IOGeneralMemoryDescriptor::
466 withPersistentMemoryDescriptor(origGenMD
);
471 IOMemoryDescriptor
* IOGeneralMemoryDescriptor::
472 withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
)
474 ipc_port_t sharedMem
= (ipc_port_t
) originalMD
->createNamedEntry();
479 if (sharedMem
== originalMD
->_memEntry
) {
480 originalMD
->retain(); // Add a new reference to ourselves
481 ipc_port_release_send(sharedMem
); // Remove extra send right
485 IOGeneralMemoryDescriptor
* self
= new IOGeneralMemoryDescriptor
;
486 typePersMDData initData
= { originalMD
, sharedMem
};
489 && !self
->initWithOptions(&initData
, 1, 0, 0, kIOMemoryTypePersistentMD
, 0)) {
496 void *IOGeneralMemoryDescriptor::createNamedEntry()
499 ipc_port_t sharedMem
;
501 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
503 user_addr_t range0Addr
;
504 IOByteCount range0Len
;
505 getAddrLenForInd(range0Addr
, range0Len
, type
, _ranges
, 0);
506 range0Addr
= trunc_page_64(range0Addr
);
508 vm_size_t size
= ptoa_32(_pages
);
509 vm_address_t kernelPage
= (vm_address_t
) range0Addr
;
511 vm_map_t theMap
= ((_task
== kernel_task
)
512 && (kIOMemoryBufferPageable
& _flags
))
513 ? IOPageableMapForAddress(kernelPage
)
514 : get_task_map(_task
);
516 memory_object_size_t actualSize
= size
;
517 vm_prot_t prot
= VM_PROT_READ
| VM_PROT_WRITE
;
519 prot
|= MAP_MEM_NAMED_REUSE
;
521 error
= mach_make_memory_entry_64(theMap
,
522 &actualSize
, range0Addr
, prot
, &sharedMem
, (ipc_port_t
) _memEntry
);
524 if (KERN_SUCCESS
== error
) {
525 if (actualSize
== size
) {
529 IOLog("IOGMD::mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
530 (UInt64
)range0Addr
, (UInt32
)actualSize
, size
);
532 ipc_port_release_send( sharedMem
);
536 return MACH_PORT_NULL
;
542 * Initialize an IOMemoryDescriptor. The buffer is a virtual address
543 * relative to the specified task. If no task is supplied, the kernel
546 * An IOMemoryDescriptor can be re-used by calling initWithAddress or
547 * initWithRanges again on an existing instance -- note this behavior
548 * is not commonly supported in other I/O Kit classes, although it is
552 IOGeneralMemoryDescriptor::initWithAddress(void * address
,
553 IOByteCount withLength
,
554 IODirection withDirection
)
556 _singleRange
.v
.address
= (vm_address_t
) address
;
557 _singleRange
.v
.length
= withLength
;
559 return initWithRanges(&_singleRange
.v
, 1, withDirection
, kernel_task
, true);
563 IOGeneralMemoryDescriptor::initWithAddress(vm_address_t address
,
564 IOByteCount withLength
,
565 IODirection withDirection
,
568 _singleRange
.v
.address
= address
;
569 _singleRange
.v
.length
= withLength
;
571 return initWithRanges(&_singleRange
.v
, 1, withDirection
, withTask
, true);
575 IOGeneralMemoryDescriptor::initWithPhysicalAddress(
576 IOPhysicalAddress address
,
577 IOByteCount withLength
,
578 IODirection withDirection
)
580 _singleRange
.p
.address
= address
;
581 _singleRange
.p
.length
= withLength
;
583 return initWithPhysicalRanges( &_singleRange
.p
, 1, withDirection
, true);
587 IOGeneralMemoryDescriptor::initWithPhysicalRanges(
588 IOPhysicalRange
* ranges
,
590 IODirection direction
,
593 IOOptionBits mdOpts
= direction
| kIOMemoryTypePhysical
;
596 mdOpts
|= kIOMemoryAsReference
;
598 return initWithOptions(ranges
, count
, 0, 0, mdOpts
, /* mapper */ 0);
602 IOGeneralMemoryDescriptor::initWithRanges(
603 IOVirtualRange
* ranges
,
605 IODirection direction
,
609 IOOptionBits mdOpts
= direction
;
612 mdOpts
|= kIOMemoryAsReference
;
615 mdOpts
|= kIOMemoryTypeVirtual
;
617 // Auto-prepare if this is a kernel memory descriptor as very few
618 // clients bother to prepare() kernel memory.
619 // But it was not enforced so what are you going to do?
620 if (task
== kernel_task
)
621 mdOpts
|= kIOMemoryAutoPrepare
;
624 mdOpts
|= kIOMemoryTypePhysical
;
626 return initWithOptions(ranges
, count
, 0, task
, mdOpts
, /* mapper */ 0);
632 * IOMemoryDescriptor. The buffer is made up of several virtual address ranges,
633 * from a given task, several physical ranges, an UPL from the ubc
634 * system or a uio (may be 64bit) from the BSD subsystem.
636 * Passing the ranges as a reference will avoid an extra allocation.
638 * An IOMemoryDescriptor can be re-used by calling initWithOptions again on an
639 * existing instance -- note this behavior is not commonly supported in other
640 * I/O Kit classes, although it is supported here.
644 IOGeneralMemoryDescriptor::initWithOptions(void * buffers
,
648 IOOptionBits options
,
651 IOOptionBits type
= options
& kIOMemoryTypeMask
;
653 // Grab the original MD's configuation data to initialse the
654 // arguments to this function.
655 if (kIOMemoryTypePersistentMD
== type
) {
657 typePersMDData
*initData
= (typePersMDData
*) buffers
;
658 const IOGeneralMemoryDescriptor
*orig
= initData
->fMD
;
659 ioGMDData
*dataP
= getDataP(orig
->_memoryEntries
);
661 // Only accept persistent memory descriptors with valid dataP data.
662 assert(orig
->_rangesCount
== 1);
663 if ( !(orig
->_flags
& kIOMemoryPersistent
) || !dataP
)
666 _memEntry
= initData
->fMemEntry
; // Grab the new named entry
667 options
= orig
->_flags
| kIOMemoryAsReference
;
668 _singleRange
= orig
->_singleRange
; // Initialise our range
669 buffers
= &_singleRange
;
672 // Now grab the original task and whatever mapper was previously used
674 mapper
= dataP
->fMapper
;
676 // We are ready to go through the original initialisation now
680 case kIOMemoryTypeUIO
:
681 case kIOMemoryTypeVirtual
:
688 case kIOMemoryTypePhysical
: // Neither Physical nor UPL should have a task
689 mapper
= kIOMapperNone
;
691 case kIOMemoryTypeUPL
:
695 return false; /* bad argument */
702 * We can check the _initialized instance variable before having ever set
703 * it to an initial value because I/O Kit guarantees that all our instance
704 * variables are zeroed on an object's allocation.
709 * An existing memory descriptor is being retargeted to point to
710 * somewhere else. Clean up our present state.
717 if (_ranges
.v
&& _rangesIsAllocated
)
718 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
720 { ipc_port_release_send((ipc_port_t
) _memEntry
); _memEntry
= 0; }
728 // Grab the appropriate mapper
729 if (mapper
== kIOMapperNone
)
730 mapper
= 0; // No Mapper
732 IOMapper::checkForSystemMapper();
733 gIOSystemMapper
= mapper
= IOMapper::gSystem
;
736 // Remove the dynamic internal use flags from the initial setting
737 options
&= ~(kIOMemoryPreparedReadOnly
);
741 // DEPRECATED variable initialisation
742 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
745 _cachedPhysicalAddress
= 0;
746 _cachedVirtualAddress
= 0;
748 if (kIOMemoryTypeUPL
== type
) {
751 unsigned int dataSize
= computeDataSize(/* pages */ 0, /* upls */ 1);
753 if (!_memoryEntries
) {
754 _memoryEntries
= OSData::withCapacity(dataSize
);
758 else if (!_memoryEntries
->initWithCapacity(dataSize
))
761 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
762 dataP
= getDataP(_memoryEntries
);
763 dataP
->fMapper
= mapper
;
766 _wireCount
++; // UPLs start out life wired
769 _pages
+= atop_32(offset
+ count
+ PAGE_MASK
) - atop_32(offset
);
772 upl_page_info_t
*pageList
= UPL_GET_INTERNAL_PAGE_LIST((upl_t
) buffers
);
774 iopl
.fIOPL
= (upl_t
) buffers
;
775 // Set the flag kIOPLOnDevice convieniently equal to 1
776 iopl
.fFlags
= pageList
->device
| kIOPLExternUPL
;
777 iopl
.fIOMDOffset
= 0;
778 if (!pageList
->device
) {
779 // Pre-compute the offset into the UPL's page list
780 pageList
= &pageList
[atop_32(offset
)];
783 iopl
.fMappedBase
= mapper
->iovmAlloc(_pages
);
784 mapper
->iovmInsert(iopl
.fMappedBase
, 0, pageList
, _pages
);
787 iopl
.fMappedBase
= 0;
790 iopl
.fMappedBase
= 0;
791 iopl
.fPageInfo
= (vm_address_t
) pageList
;
792 iopl
.fPageOffset
= offset
;
794 _memoryEntries
->appendBytes(&iopl
, sizeof(iopl
));
797 // kIOMemoryTypeVirtual | kIOMemoryTypeUIO | kIOMemoryTypePhysical
799 // Initialize the memory descriptor
800 if (options
& kIOMemoryAsReference
) {
801 _rangesIsAllocated
= false;
803 // Hack assignment to get the buffer arg into _ranges.
804 // I'd prefer to do _ranges = (Ranges) buffers, but that doesn't
806 // This also initialises the uio & physical ranges.
807 _ranges
.v
= (IOVirtualRange
*) buffers
;
810 assert(kIOMemoryTypeUIO
!= type
);
812 _rangesIsAllocated
= true;
813 _ranges
.v
= IONew(IOVirtualRange
, count
);
816 bcopy(buffers
, _ranges
.v
, count
* sizeof(IOVirtualRange
));
819 // Find starting address within the vector of ranges
820 Ranges vec
= _ranges
;
823 for (unsigned ind
= 0; ind
< count
; ind
++) {
827 // addr & len are returned by this function
828 getAddrLenForInd(addr
, len
, type
, vec
, ind
);
829 pages
+= (atop_64(addr
+ len
+ PAGE_MASK
) - atop_64(addr
));
831 assert(len
> length
); // Check for 32 bit wrap around
836 _rangesCount
= count
;
838 // Auto-prepare memory at creation time.
839 // Implied completion when descriptor is free-ed
840 if (kIOMemoryTypePhysical
== type
)
841 _wireCount
++; // Physical MDs are, by definition, wired
842 else { /* kIOMemoryTypeVirtual | kIOMemoryTypeUIO */
844 unsigned dataSize
= computeDataSize(_pages
, /* upls */ count
* 2);
846 if (!_memoryEntries
) {
847 _memoryEntries
= OSData::withCapacity(dataSize
);
851 else if (!_memoryEntries
->initWithCapacity(dataSize
))
854 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
855 dataP
= getDataP(_memoryEntries
);
856 dataP
->fMapper
= mapper
;
857 dataP
->fPageCnt
= _pages
;
859 if ( (kIOMemoryPersistent
& _flags
) && !_memEntry
)
860 _memEntry
= createNamedEntry();
862 if ((_flags
& kIOMemoryAutoPrepare
)
863 && prepare() != kIOReturnSuccess
)
876 void IOGeneralMemoryDescriptor::free()
880 reserved
->memory
= 0;
886 _memoryEntries
->release();
890 if (_ranges
.v
&& _rangesIsAllocated
)
891 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
893 if (reserved
&& reserved
->devicePager
)
894 device_pager_deallocate( (memory_object_t
) reserved
->devicePager
);
896 // memEntry holds a ref on the device pager which owns reserved
897 // (ExpansionData) so no reserved access after this point
899 ipc_port_release_send( (ipc_port_t
) _memEntry
);
904 /* DEPRECATED */ void IOGeneralMemoryDescriptor::unmapFromKernel()
906 panic("IOGMD::unmapFromKernel deprecated");
909 /* DEPRECATED */ void IOGeneralMemoryDescriptor::mapIntoKernel(unsigned rangeIndex
)
911 panic("IOGMD::mapIntoKernel deprecated");
917 * Get the direction of the transfer.
919 IODirection
IOMemoryDescriptor::getDirection() const
927 * Get the length of the transfer (over all ranges).
929 IOByteCount
IOMemoryDescriptor::getLength() const
934 void IOMemoryDescriptor::setTag( IOOptionBits tag
)
939 IOOptionBits
IOMemoryDescriptor::getTag( void )
944 // @@@ gvdl: who is using this API? Seems like a wierd thing to implement.
945 IOPhysicalAddress
IOMemoryDescriptor::getSourceSegment( IOByteCount offset
,
946 IOByteCount
* length
)
948 IOPhysicalAddress physAddr
= 0;
950 if( prepare() == kIOReturnSuccess
) {
951 physAddr
= getPhysicalSegment( offset
, length
);
958 IOByteCount
IOMemoryDescriptor::readBytes
959 (IOByteCount offset
, void *bytes
, IOByteCount length
)
961 addr64_t dstAddr
= (addr64_t
) (UInt32
) bytes
;
962 IOByteCount remaining
;
964 // Assert that this entire I/O is withing the available range
965 assert(offset
< _length
);
966 assert(offset
+ length
<= _length
);
967 if (offset
>= _length
) {
968 IOLog("IOGMD(%p): rB = o%lx, l%lx\n", this, offset
, length
); // @@@ gvdl
972 remaining
= length
= min(length
, _length
- offset
);
973 while (remaining
) { // (process another target segment?)
977 srcAddr64
= getPhysicalSegment64(offset
, &srcLen
);
981 // Clip segment length to remaining
982 if (srcLen
> remaining
)
985 copypv(srcAddr64
, dstAddr
, srcLen
,
986 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
995 return length
- remaining
;
998 IOByteCount
IOMemoryDescriptor::writeBytes
999 (IOByteCount offset
, const void *bytes
, IOByteCount length
)
1001 addr64_t srcAddr
= (addr64_t
) (UInt32
) bytes
;
1002 IOByteCount remaining
;
1004 // Assert that this entire I/O is withing the available range
1005 assert(offset
< _length
);
1006 assert(offset
+ length
<= _length
);
1008 assert( !(kIOMemoryPreparedReadOnly
& _flags
) );
1010 if ( (kIOMemoryPreparedReadOnly
& _flags
) || offset
>= _length
) {
1011 IOLog("IOGMD(%p): wB = o%lx, l%lx\n", this, offset
, length
); // @@@ gvdl
1015 remaining
= length
= min(length
, _length
- offset
);
1016 while (remaining
) { // (process another target segment?)
1020 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1024 // Clip segment length to remaining
1025 if (dstLen
> remaining
)
1028 copypv(srcAddr
, (addr64_t
) dstAddr64
, dstLen
,
1029 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1033 remaining
-= dstLen
;
1038 return length
- remaining
;
1041 // osfmk/device/iokit_rpc.c
1042 extern "C" unsigned int IODefaultCacheBits(addr64_t pa
);
1044 /* DEPRECATED */ void IOGeneralMemoryDescriptor::setPosition(IOByteCount position
)
1046 panic("IOGMD::setPosition deprecated");
1049 IOPhysicalAddress
IOGeneralMemoryDescriptor::getPhysicalSegment
1050 (IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1052 IOPhysicalAddress address
= 0;
1053 IOPhysicalLength length
= 0;
1055 // assert(offset <= _length);
1056 if (offset
< _length
) // (within bounds?)
1058 if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
) {
1061 // Physical address based memory descriptor
1063 // Find offset within descriptor and make it relative
1064 // to the current _range.
1065 for (ind
= 0 ; offset
>= _ranges
.p
[ind
].length
; ind
++ )
1066 offset
-= _ranges
.p
[ind
].length
;
1068 IOPhysicalRange cur
= _ranges
.p
[ind
];
1069 address
= cur
.address
+ offset
;
1070 length
= cur
.length
- offset
;
1072 // see how far we can coalesce ranges
1073 for (++ind
; ind
< _rangesCount
; ind
++) {
1074 cur
= _ranges
.p
[ind
];
1076 if (address
+ length
!= cur
.address
)
1079 length
+= cur
.length
;
1082 // @@@ gvdl: should be assert(address);
1083 // but can't as NVidia GeForce creates a bogus physical mem
1085 || /* nvidia */ (!_ranges
.p
[0].address
&& 1 == _rangesCount
));
1089 // We need wiring & we are wired.
1094 panic("IOGMD: not wired for getPhysicalSegment()");
1098 assert(_memoryEntries
);
1100 ioGMDData
* dataP
= getDataP(_memoryEntries
);
1101 const ioPLBlock
*ioplList
= getIOPLList(dataP
);
1102 UInt ind
, numIOPLs
= getNumIOPL(_memoryEntries
, dataP
);
1103 upl_page_info_t
*pageList
= getPageList(dataP
);
1105 assert(numIOPLs
> 0);
1107 // Scan through iopl info blocks looking for block containing offset
1108 for (ind
= 1; ind
< numIOPLs
; ind
++) {
1109 if (offset
< ioplList
[ind
].fIOMDOffset
)
1113 // Go back to actual range as search goes past it
1114 ioPLBlock ioplInfo
= ioplList
[ind
- 1];
1117 length
= ioplList
[ind
].fIOMDOffset
;
1120 length
-= offset
; // Remainder within iopl
1122 // Subtract offset till this iopl in total list
1123 offset
-= ioplInfo
.fIOMDOffset
;
1125 // This is a mapped IOPL so we just need to compute an offset
1126 // relative to the mapped base.
1127 if (ioplInfo
.fMappedBase
) {
1128 offset
+= (ioplInfo
.fPageOffset
& PAGE_MASK
);
1129 address
= ptoa_32(ioplInfo
.fMappedBase
) + offset
;
1133 // Currently the offset is rebased into the current iopl.
1134 // Now add the iopl 1st page offset.
1135 offset
+= ioplInfo
.fPageOffset
;
1137 // For external UPLs the fPageInfo field points directly to
1138 // the upl's upl_page_info_t array.
1139 if (ioplInfo
.fFlags
& kIOPLExternUPL
)
1140 pageList
= (upl_page_info_t
*) ioplInfo
.fPageInfo
;
1142 pageList
= &pageList
[ioplInfo
.fPageInfo
];
1144 // Check for direct device non-paged memory
1145 if ( ioplInfo
.fFlags
& kIOPLOnDevice
) {
1146 address
= ptoa_32(pageList
->phys_addr
) + offset
;
1150 // Now we need compute the index into the pageList
1151 ind
= atop_32(offset
);
1152 offset
&= PAGE_MASK
;
1154 IOPhysicalAddress pageAddr
= pageList
[ind
].phys_addr
;
1155 address
= ptoa_32(pageAddr
) + offset
;
1157 // Check for the remaining data in this upl being longer than the
1158 // remainder on the current page. This should be checked for
1160 if (length
> PAGE_SIZE
- offset
) {
1161 // See if the next page is contiguous. Stop looking when we hit
1162 // the end of this upl, which is indicated by the
1163 // contigLength >= length.
1164 IOByteCount contigLength
= PAGE_SIZE
- offset
;
1166 // Look for contiguous segment
1167 while (contigLength
< length
1168 && ++pageAddr
== pageList
[++ind
].phys_addr
) {
1169 contigLength
+= PAGE_SIZE
;
1171 if (length
> contigLength
)
1172 length
= contigLength
;
1184 if (lengthOfSegment
)
1185 *lengthOfSegment
= length
;
1190 addr64_t
IOMemoryDescriptor::getPhysicalSegment64
1191 (IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1193 IOPhysicalAddress phys32
;
1197 phys32
= getPhysicalSegment(offset
, lengthOfSegment
);
1201 if (gIOSystemMapper
)
1203 IOByteCount origLen
;
1205 phys64
= gIOSystemMapper
->mapAddr(phys32
);
1206 origLen
= *lengthOfSegment
;
1207 length
= page_size
- (phys64
& (page_size
- 1));
1208 while ((length
< origLen
)
1209 && ((phys64
+ length
) == gIOSystemMapper
->mapAddr(phys32
+ length
)))
1210 length
+= page_size
;
1211 if (length
> origLen
)
1214 *lengthOfSegment
= length
;
1217 phys64
= (addr64_t
) phys32
;
1222 IOPhysicalAddress
IOGeneralMemoryDescriptor::
1223 getSourceSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1225 IOPhysicalAddress address
= 0;
1226 IOPhysicalLength length
= 0;
1227 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1229 assert(offset
<= _length
);
1231 if ( type
== kIOMemoryTypeUPL
)
1232 return super::getSourceSegment( offset
, lengthOfSegment
);
1233 else if ( offset
< _length
) // (within bounds?)
1235 unsigned rangesIndex
= 0;
1236 Ranges vec
= _ranges
;
1239 // Find starting address within the vector of ranges
1241 getAddrLenForInd(addr
, length
, type
, vec
, rangesIndex
);
1242 if (offset
< length
)
1244 offset
-= length
; // (make offset relative)
1248 // Now that we have the starting range,
1249 // lets find the last contiguous range
1253 for ( ++rangesIndex
; rangesIndex
< _rangesCount
; rangesIndex
++ ) {
1254 user_addr_t newAddr
;
1255 IOPhysicalLength newLen
;
1257 getAddrLenForInd(newAddr
, newLen
, type
, vec
, rangesIndex
);
1258 if (addr
+ length
!= newAddr
)
1263 address
= (IOPhysicalAddress
) addr
; // Truncate address to 32bit
1268 if ( lengthOfSegment
) *lengthOfSegment
= length
;
1273 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1274 /* DEPRECATED */ void * IOGeneralMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
1275 /* DEPRECATED */ IOByteCount
* lengthOfSegment
)
1277 if (_task
== kernel_task
)
1278 return (void *) getSourceSegment(offset
, lengthOfSegment
);
1280 panic("IOGMD::getVirtualSegment deprecated");
1284 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1288 IOReturn
IOMemoryDescriptor::setPurgeable( IOOptionBits newState
,
1289 IOOptionBits
* oldState
)
1291 IOReturn err
= kIOReturnSuccess
;
1292 vm_purgable_t control
;
1299 err
= kIOReturnNotReady
;
1303 control
= VM_PURGABLE_SET_STATE
;
1306 case kIOMemoryPurgeableKeepCurrent
:
1307 control
= VM_PURGABLE_GET_STATE
;
1310 case kIOMemoryPurgeableNonVolatile
:
1311 state
= VM_PURGABLE_NONVOLATILE
;
1313 case kIOMemoryPurgeableVolatile
:
1314 state
= VM_PURGABLE_VOLATILE
;
1316 case kIOMemoryPurgeableEmpty
:
1317 state
= VM_PURGABLE_EMPTY
;
1320 err
= kIOReturnBadArgument
;
1324 if (kIOReturnSuccess
!= err
)
1327 err
= mach_memory_entry_purgable_control((ipc_port_t
) _memEntry
, control
, &state
);
1331 if (kIOReturnSuccess
== err
)
1335 case VM_PURGABLE_NONVOLATILE
:
1336 state
= kIOMemoryPurgeableNonVolatile
;
1338 case VM_PURGABLE_VOLATILE
:
1339 state
= kIOMemoryPurgeableVolatile
;
1341 case VM_PURGABLE_EMPTY
:
1342 state
= kIOMemoryPurgeableEmpty
;
1345 state
= kIOMemoryPurgeableNonVolatile
;
1346 err
= kIOReturnNotReady
;
1358 extern "C" void dcache_incoherent_io_flush64(addr64_t pa
, unsigned int count
);
1359 extern "C" void dcache_incoherent_io_store64(addr64_t pa
, unsigned int count
);
1361 IOReturn
IOMemoryDescriptor::performOperation( IOOptionBits options
,
1362 IOByteCount offset
, IOByteCount length
)
1364 IOByteCount remaining
;
1365 void (*func
)(addr64_t pa
, unsigned int count
) = 0;
1369 case kIOMemoryIncoherentIOFlush
:
1370 func
= &dcache_incoherent_io_flush64
;
1372 case kIOMemoryIncoherentIOStore
:
1373 func
= &dcache_incoherent_io_store64
;
1378 return (kIOReturnUnsupported
);
1380 remaining
= length
= min(length
, getLength() - offset
);
1382 // (process another target segment?)
1387 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1391 // Clip segment length to remaining
1392 if (dstLen
> remaining
)
1395 (*func
)(dstAddr64
, dstLen
);
1398 remaining
-= dstLen
;
1401 return (remaining
? kIOReturnUnderrun
: kIOReturnSuccess
);
1405 extern vm_offset_t static_memory_end
;
1406 #define io_kernel_static_end static_memory_end
1408 extern vm_offset_t first_avail
;
1409 #define io_kernel_static_end first_avail
1412 static kern_return_t
1413 io_get_kernel_static_upl(
1415 vm_address_t offset
,
1416 vm_size_t
*upl_size
,
1418 upl_page_info_array_t page_list
,
1419 unsigned int *count
)
1421 unsigned int pageCount
, page
;
1424 pageCount
= atop_32(*upl_size
);
1425 if (pageCount
> *count
)
1430 for (page
= 0; page
< pageCount
; page
++)
1432 phys
= pmap_find_phys(kernel_pmap
, ((addr64_t
)offset
) + ptoa_64(page
));
1435 page_list
[page
].phys_addr
= phys
;
1436 page_list
[page
].pageout
= 0;
1437 page_list
[page
].absent
= 0;
1438 page_list
[page
].dirty
= 0;
1439 page_list
[page
].precious
= 0;
1440 page_list
[page
].device
= 0;
1443 return ((page
>= pageCount
) ? kIOReturnSuccess
: kIOReturnVMError
);
1446 IOReturn
IOGeneralMemoryDescriptor::wireVirtual(IODirection forDirection
)
1448 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1449 IOReturn error
= kIOReturnNoMemory
;
1451 ppnum_t mapBase
= 0;
1453 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
1455 assert(!_wireCount
);
1456 assert(kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeUIO
== type
);
1458 if (_pages
>= gIOMaximumMappedIOPageCount
)
1459 return kIOReturnNoResources
;
1461 dataP
= getDataP(_memoryEntries
);
1462 mapper
= dataP
->fMapper
;
1463 if (mapper
&& _pages
)
1464 mapBase
= mapper
->iovmAlloc(_pages
);
1466 // Note that appendBytes(NULL) zeros the data up to the
1468 _memoryEntries
->appendBytes(0, dataP
->fPageCnt
* sizeof(upl_page_info_t
));
1469 dataP
= 0; // May no longer be valid so lets not get tempted.
1471 if (forDirection
== kIODirectionNone
)
1472 forDirection
= _direction
;
1474 int uplFlags
; // This Mem Desc's default flags for upl creation
1475 switch (forDirection
)
1477 case kIODirectionOut
:
1478 // Pages do not need to be marked as dirty on commit
1479 uplFlags
= UPL_COPYOUT_FROM
;
1480 _flags
|= kIOMemoryPreparedReadOnly
;
1483 case kIODirectionIn
:
1485 uplFlags
= 0; // i.e. ~UPL_COPYOUT_FROM
1488 uplFlags
|= UPL_SET_IO_WIRE
| UPL_SET_LITE
;
1490 // Find the appropriate vm_map for the given task
1492 if (_task
== kernel_task
&& (kIOMemoryBufferPageable
& _flags
))
1495 { curMap
= get_task_map(_task
); }
1497 // Iterate over the vector of virtual ranges
1498 Ranges vec
= _ranges
;
1499 unsigned int pageIndex
= 0;
1500 IOByteCount mdOffset
= 0;
1501 for (UInt range
= 0; range
< _rangesCount
; range
++) {
1503 user_addr_t startPage
;
1504 IOByteCount numBytes
;
1506 // Get the startPage address and length of vec[range]
1507 getAddrLenForInd(startPage
, numBytes
, type
, vec
, range
);
1508 iopl
.fPageOffset
= (short) startPage
& PAGE_MASK
;
1509 numBytes
+= iopl
.fPageOffset
;
1510 startPage
= trunc_page_64(startPage
);
1513 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1515 iopl
.fMappedBase
= 0;
1517 // Iterate over the current range, creating UPLs
1519 dataP
= getDataP(_memoryEntries
);
1520 vm_address_t kernelStart
= (vm_address_t
) startPage
;
1524 else if (!sharedMem
) {
1525 assert(_task
== kernel_task
);
1526 theMap
= IOPageableMapForAddress(kernelStart
);
1531 upl_page_info_array_t pageInfo
= getPageList(dataP
);
1532 int ioplFlags
= uplFlags
;
1533 upl_page_list_ptr_t baseInfo
= &pageInfo
[pageIndex
];
1535 vm_size_t ioplSize
= round_page_32(numBytes
);
1536 unsigned int numPageInfo
= atop_32(ioplSize
);
1538 if (theMap
== kernel_map
&& kernelStart
< io_kernel_static_end
) {
1539 error
= io_get_kernel_static_upl(theMap
,
1546 else if (sharedMem
) {
1547 error
= memory_object_iopl_request(sharedMem
,
1557 error
= vm_map_create_upl(theMap
,
1567 if (error
!= KERN_SUCCESS
)
1570 error
= kIOReturnNoMemory
;
1572 if (baseInfo
->device
) {
1574 iopl
.fFlags
= kIOPLOnDevice
;
1575 // Don't translate device memory at all
1576 if (mapper
&& mapBase
) {
1577 mapper
->iovmFree(mapBase
, _pages
);
1579 iopl
.fMappedBase
= 0;
1585 mapper
->iovmInsert(mapBase
, pageIndex
,
1586 baseInfo
, numPageInfo
);
1589 iopl
.fIOMDOffset
= mdOffset
;
1590 iopl
.fPageInfo
= pageIndex
;
1592 if ((_flags
& kIOMemoryAutoPrepare
) && iopl
.fIOPL
)
1594 upl_commit(iopl
.fIOPL
, 0, 0);
1595 upl_deallocate(iopl
.fIOPL
);
1599 if (!_memoryEntries
->appendBytes(&iopl
, sizeof(iopl
))) {
1600 // Clean up partial created and unsaved iopl
1602 upl_abort(iopl
.fIOPL
, 0);
1603 upl_deallocate(iopl
.fIOPL
);
1608 // Check for a multiple iopl's in one virtual range
1609 pageIndex
+= numPageInfo
;
1610 mdOffset
-= iopl
.fPageOffset
;
1611 if (ioplSize
< numBytes
) {
1612 numBytes
-= ioplSize
;
1613 startPage
+= ioplSize
;
1614 mdOffset
+= ioplSize
;
1615 iopl
.fPageOffset
= 0;
1617 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1620 mdOffset
+= numBytes
;
1626 return kIOReturnSuccess
;
1630 dataP
= getDataP(_memoryEntries
);
1631 UInt done
= getNumIOPL(_memoryEntries
, dataP
);
1632 ioPLBlock
*ioplList
= getIOPLList(dataP
);
1634 for (UInt range
= 0; range
< done
; range
++)
1636 if (ioplList
[range
].fIOPL
) {
1637 upl_abort(ioplList
[range
].fIOPL
, 0);
1638 upl_deallocate(ioplList
[range
].fIOPL
);
1641 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
1643 if (mapper
&& mapBase
)
1644 mapper
->iovmFree(mapBase
, _pages
);
1653 * Prepare the memory for an I/O transfer. This involves paging in
1654 * the memory, if necessary, and wiring it down for the duration of
1655 * the transfer. The complete() method completes the processing of
1656 * the memory after the I/O transfer finishes. This method needn't
1657 * called for non-pageable memory.
1659 IOReturn
IOGeneralMemoryDescriptor::prepare(IODirection forDirection
)
1661 IOReturn error
= kIOReturnSuccess
;
1662 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1665 && (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeUIO
== type
) ) {
1666 error
= wireVirtual(forDirection
);
1673 return kIOReturnSuccess
;
1679 * Complete processing of the memory after an I/O transfer finishes.
1680 * This method should not be called unless a prepare was previously
1681 * issued; the prepare() and complete() must occur in pairs, before
1682 * before and after an I/O transfer involving pageable memory.
1685 IOReturn
IOGeneralMemoryDescriptor::complete(IODirection
/* forDirection */)
1690 return kIOReturnSuccess
;
1694 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1696 if (kIOMemoryTypePhysical
== type
) {
1697 /* kIOMemoryTypePhysical */
1701 ioGMDData
* dataP
= getDataP(_memoryEntries
);
1702 ioPLBlock
*ioplList
= getIOPLList(dataP
);
1703 UInt count
= getNumIOPL(_memoryEntries
, dataP
);
1705 if (dataP
->fMapper
&& _pages
&& ioplList
[0].fMappedBase
)
1706 dataP
->fMapper
->iovmFree(ioplList
[0].fMappedBase
, _pages
);
1708 // Only complete iopls that we created which are for TypeVirtual
1709 if (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeUIO
== type
) {
1710 for (UInt ind
= 0; ind
< count
; ind
++)
1711 if (ioplList
[ind
].fIOPL
) {
1712 upl_commit(ioplList
[ind
].fIOPL
, 0, 0);
1713 upl_deallocate(ioplList
[ind
].fIOPL
);
1717 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
1720 return kIOReturnSuccess
;
1723 IOReturn
IOGeneralMemoryDescriptor::doMap(
1724 vm_map_t addressMap
,
1725 IOVirtualAddress
* atAddress
,
1726 IOOptionBits options
,
1727 IOByteCount sourceOffset
,
1728 IOByteCount length
)
1731 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
1733 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1734 Ranges vec
= _ranges
;
1736 user_addr_t range0Addr
= 0;
1737 IOByteCount range0Len
= 0;
1740 getAddrLenForInd(range0Addr
, range0Len
, type
, vec
, 0);
1742 // mapping source == dest? (could be much better)
1744 && (addressMap
== get_task_map(_task
)) && (options
& kIOMapAnywhere
)
1745 && (1 == _rangesCount
) && (0 == sourceOffset
)
1746 && range0Addr
&& (length
<= range0Len
) ) {
1747 if (sizeof(user_addr_t
) > 4 && ((UInt64
) range0Addr
) >> 32)
1748 return kIOReturnOverrun
; // Doesn't fit in 32bit return field
1750 *atAddress
= range0Addr
;
1751 return( kIOReturnSuccess
);
1755 if( 0 == sharedMem
) {
1757 vm_size_t size
= ptoa_32(_pages
);
1761 memory_object_size_t actualSize
= size
;
1762 kr
= mach_make_memory_entry_64(get_task_map(_task
),
1763 &actualSize
, range0Addr
,
1764 VM_PROT_READ
| VM_PROT_WRITE
, &sharedMem
,
1767 if( (KERN_SUCCESS
== kr
) && (actualSize
!= round_page_32(size
))) {
1769 IOLog("mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
1770 range0Addr
, (UInt32
) actualSize
, size
);
1772 kr
= kIOReturnVMError
;
1773 ipc_port_release_send( sharedMem
);
1776 if( KERN_SUCCESS
!= kr
)
1778 sharedMem
= MACH_PORT_NULL
;
1782 memory_object_t pager
;
1783 unsigned int flags
= 0;
1785 IOPhysicalLength segLen
;
1787 pa
= getPhysicalSegment64( sourceOffset
, &segLen
);
1790 reserved
= IONew( ExpansionData
, 1 );
1794 reserved
->pagerContig
= (1 == _rangesCount
);
1795 reserved
->memory
= this;
1797 /*What cache mode do we need*/
1798 switch(options
& kIOMapCacheMask
) {
1800 case kIOMapDefaultCache
:
1802 flags
= IODefaultCacheBits(pa
);
1805 case kIOMapInhibitCache
:
1806 flags
= DEVICE_PAGER_CACHE_INHIB
|
1807 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
1810 case kIOMapWriteThruCache
:
1811 flags
= DEVICE_PAGER_WRITE_THROUGH
|
1812 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
1815 case kIOMapCopybackCache
:
1816 flags
= DEVICE_PAGER_COHERENT
;
1819 case kIOMapWriteCombineCache
:
1820 flags
= DEVICE_PAGER_CACHE_INHIB
|
1821 DEVICE_PAGER_COHERENT
;
1825 flags
|= reserved
->pagerContig
? DEVICE_PAGER_CONTIGUOUS
: 0;
1827 pager
= device_pager_setup( (memory_object_t
) 0, (int) reserved
,
1832 kr
= mach_memory_object_memory_entry_64( (host_t
) 1, false /*internal*/,
1833 size
, VM_PROT_READ
| VM_PROT_WRITE
, pager
, &sharedMem
);
1835 assert( KERN_SUCCESS
== kr
);
1836 if( KERN_SUCCESS
!= kr
) {
1837 device_pager_deallocate( pager
);
1838 pager
= MACH_PORT_NULL
;
1839 sharedMem
= MACH_PORT_NULL
;
1842 if( pager
&& sharedMem
)
1843 reserved
->devicePager
= pager
;
1845 IODelete( reserved
, ExpansionData
, 1 );
1851 _memEntry
= (void *) sharedMem
;
1857 kr
= kIOReturnVMError
;
1860 kr
= super::doMap( addressMap
, atAddress
,
1861 options
, sourceOffset
, length
);
1866 IOReturn
IOGeneralMemoryDescriptor::doUnmap(
1867 vm_map_t addressMap
,
1868 IOVirtualAddress logical
,
1869 IOByteCount length
)
1871 // could be much better
1872 if( _task
&& (addressMap
== get_task_map(_task
)) && (1 == _rangesCount
)) {
1874 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1875 user_addr_t range0Addr
;
1876 IOByteCount range0Len
;
1878 getAddrLenForInd(range0Addr
, range0Len
, type
, _ranges
, 0);
1879 if (logical
== range0Addr
&& length
<= range0Len
)
1880 return( kIOReturnSuccess
);
1883 return( super::doUnmap( addressMap
, logical
, length
));
1886 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1888 OSDefineMetaClassAndAbstractStructors( IOMemoryMap
, OSObject
)
1890 /* inline function implementation */
1891 IOPhysicalAddress
IOMemoryMap::getPhysicalAddress()
1892 { return( getPhysicalSegment( 0, 0 )); }
1896 #define super IOMemoryMap
1898 OSDefineMetaClassAndStructors(_IOMemoryMap
, IOMemoryMap
)
1900 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1902 bool _IOMemoryMap::initCompatible(
1903 IOMemoryDescriptor
* _memory
,
1904 IOMemoryMap
* _superMap
,
1905 IOByteCount _offset
,
1906 IOByteCount _length
)
1912 if( (_offset
+ _length
) > _superMap
->getLength())
1917 _superMap
->retain();
1918 superMap
= _superMap
;
1924 length
= _memory
->getLength();
1926 options
= superMap
->getMapOptions();
1927 logical
= superMap
->getVirtualAddress() + offset
;
1932 bool _IOMemoryMap::initWithDescriptor(
1933 IOMemoryDescriptor
* _memory
,
1935 IOVirtualAddress toAddress
,
1936 IOOptionBits _options
,
1937 IOByteCount _offset
,
1938 IOByteCount _length
)
1941 bool redir
= ((kIOMapUnique
|kIOMapReference
) == ((kIOMapUnique
|kIOMapReference
) & _options
));
1943 if ((!_memory
) || (!intoTask
))
1946 if( (_offset
+ _length
) > _memory
->getLength())
1953 addressMap
= get_task_map(intoTask
);
1956 vm_map_reference(addressMap
);
1957 addressTask
= intoTask
;
1958 logical
= toAddress
;
1968 length
= _memory
->getLength();
1970 if( options
& kIOMapStatic
)
1973 ok
= (kIOReturnSuccess
== _memory
->doMap( addressMap
, &toAddress
,
1974 _options
, offset
, length
));
1980 logical
= toAddress
;
1989 vm_map_deallocate(addressMap
);
1997 /* LP64todo - these need to expand */
1998 struct IOMemoryDescriptorMapAllocRef
2000 ipc_port_t sharedMem
;
2003 IOByteCount sourceOffset
;
2004 IOOptionBits options
;
2007 static kern_return_t
IOMemoryDescriptorMapAlloc(vm_map_t map
, void * _ref
)
2009 IOMemoryDescriptorMapAllocRef
* ref
= (IOMemoryDescriptorMapAllocRef
*)_ref
;
2013 if( ref
->sharedMem
) {
2014 vm_prot_t prot
= VM_PROT_READ
2015 | ((ref
->options
& kIOMapReadOnly
) ? 0 : VM_PROT_WRITE
);
2017 // set memory entry cache
2018 vm_prot_t memEntryCacheMode
= prot
| MAP_MEM_ONLY
;
2019 switch (ref
->options
& kIOMapCacheMask
)
2021 case kIOMapInhibitCache
:
2022 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
2025 case kIOMapWriteThruCache
:
2026 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
2029 case kIOMapWriteCombineCache
:
2030 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
2033 case kIOMapCopybackCache
:
2034 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
2037 case kIOMapDefaultCache
:
2039 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
2043 vm_size_t unused
= 0;
2045 err
= mach_make_memory_entry( NULL
/*unused*/, &unused
, 0 /*unused*/,
2046 memEntryCacheMode
, NULL
, ref
->sharedMem
);
2047 if (KERN_SUCCESS
!= err
)
2048 IOLog("MAP_MEM_ONLY failed %d\n", err
);
2052 ref
->size
, 0 /* mask */,
2053 (( ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2054 | VM_MAKE_TAG(VM_MEMORY_IOKIT
),
2055 ref
->sharedMem
, ref
->sourceOffset
,
2061 if( KERN_SUCCESS
!= err
) {
2068 err
= vm_allocate( map
, &ref
->mapped
, ref
->size
,
2069 ((ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2070 | VM_MAKE_TAG(VM_MEMORY_IOKIT
) );
2072 if( KERN_SUCCESS
!= err
) {
2077 // we have to make sure that these guys don't get copied if we fork.
2078 err
= vm_inherit( map
, ref
->mapped
, ref
->size
, VM_INHERIT_NONE
);
2079 assert( KERN_SUCCESS
== err
);
2088 IOReturn
IOMemoryDescriptor::doMap(
2089 vm_map_t addressMap
,
2090 IOVirtualAddress
* atAddress
,
2091 IOOptionBits options
,
2092 IOByteCount sourceOffset
,
2093 IOByteCount length
)
2095 IOReturn err
= kIOReturnSuccess
;
2096 memory_object_t pager
;
2097 vm_address_t logical
;
2098 IOByteCount pageOffset
;
2099 IOPhysicalAddress sourceAddr
;
2100 IOMemoryDescriptorMapAllocRef ref
;
2102 ref
.sharedMem
= (ipc_port_t
) _memEntry
;
2103 ref
.sourceOffset
= sourceOffset
;
2104 ref
.options
= options
;
2109 length
= getLength();
2111 sourceAddr
= getSourceSegment( sourceOffset
, NULL
);
2112 pageOffset
= sourceAddr
- trunc_page_32( sourceAddr
);
2114 ref
.size
= round_page_32( length
+ pageOffset
);
2116 if ((kIOMapReference
|kIOMapUnique
) == ((kIOMapReference
|kIOMapUnique
) & options
))
2122 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) *atAddress
;
2123 ref
.mapped
= mapping
->getVirtualAddress();
2127 err
= kIOReturnNotReadable
;
2132 flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
2133 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
2135 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) _memEntry
, 0, &size
, &redirUPL2
,
2140 err
= upl_transpose(redirUPL2
, mapping
->redirUPL
);
2141 if (kIOReturnSuccess
!= err
)
2143 IOLog("upl_transpose(%x)\n", err
);
2144 err
= kIOReturnSuccess
;
2149 upl_commit(redirUPL2
, NULL
, 0);
2150 upl_deallocate(redirUPL2
);
2154 // swap the memEntries since they now refer to different vm_objects
2155 void * me
= _memEntry
;
2156 _memEntry
= mapping
->memory
->_memEntry
;
2157 mapping
->memory
->_memEntry
= me
;
2163 logical
= *atAddress
;
2164 if( options
& kIOMapAnywhere
)
2165 // vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
2168 ref
.mapped
= trunc_page_32( logical
);
2169 if( (logical
- ref
.mapped
) != pageOffset
) {
2170 err
= kIOReturnVMError
;
2175 if( ref
.sharedMem
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2176 err
= IOIteratePageableMaps( ref
.size
, &IOMemoryDescriptorMapAlloc
, &ref
);
2178 err
= IOMemoryDescriptorMapAlloc( addressMap
, &ref
);
2181 if( err
!= KERN_SUCCESS
)
2185 pager
= (memory_object_t
) reserved
->devicePager
;
2187 pager
= MACH_PORT_NULL
;
2189 if( !ref
.sharedMem
|| pager
)
2190 err
= handleFault( pager
, addressMap
, ref
.mapped
, sourceOffset
, length
, options
);
2194 if( err
!= KERN_SUCCESS
) {
2196 doUnmap( addressMap
, ref
.mapped
, ref
.size
);
2199 *atAddress
= ref
.mapped
+ pageOffset
;
2205 kIOMemoryRedirected
= 0x00010000
2208 IOReturn
IOMemoryDescriptor::handleFault(
2210 vm_map_t addressMap
,
2211 IOVirtualAddress address
,
2212 IOByteCount sourceOffset
,
2214 IOOptionBits options
)
2216 IOReturn err
= kIOReturnSuccess
;
2217 memory_object_t pager
= (memory_object_t
) _pager
;
2221 IOByteCount pageOffset
;
2222 IOByteCount pagerOffset
;
2223 IOPhysicalLength segLen
;
2228 if( kIOMemoryRedirected
& _flags
) {
2230 IOLog("sleep mem redirect %p, %lx\n", this, sourceOffset
);
2234 } while( kIOMemoryRedirected
& _flags
);
2237 return( kIOReturnSuccess
);
2240 physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
);
2242 pageOffset
= physAddr
- trunc_page_64( physAddr
);
2243 pagerOffset
= sourceOffset
;
2245 size
= length
+ pageOffset
;
2246 physAddr
-= pageOffset
;
2248 segLen
+= pageOffset
;
2251 // in the middle of the loop only map whole pages
2252 if( segLen
>= bytes
)
2254 else if( segLen
!= trunc_page_32( segLen
))
2255 err
= kIOReturnVMError
;
2256 if( physAddr
!= trunc_page_64( physAddr
))
2257 err
= kIOReturnBadArgument
;
2260 if( kIOLogMapping
& gIOKitDebug
)
2261 IOLog("_IOMemoryMap::map(%p) %08lx->%08qx:%08lx\n",
2262 addressMap
, address
+ pageOffset
, physAddr
+ pageOffset
,
2263 segLen
- pageOffset
);
2271 /* i386 doesn't support faulting on device memory yet */
2272 if( addressMap
&& (kIOReturnSuccess
== err
))
2273 err
= IOMapPages( addressMap
, address
, (IOPhysicalAddress
) physAddr
, segLen
, options
);
2274 assert( KERN_SUCCESS
== err
);
2280 if( reserved
&& reserved
->pagerContig
) {
2281 IOPhysicalLength allLen
;
2284 allPhys
= getPhysicalSegment64( 0, &allLen
);
2286 err
= device_pager_populate_object( pager
, 0, allPhys
>> PAGE_SHIFT
, round_page_32(allLen
) );
2291 (page
< segLen
) && (KERN_SUCCESS
== err
);
2292 page
+= page_size
) {
2293 err
= device_pager_populate_object(pager
, pagerOffset
,
2294 (ppnum_t
)((physAddr
+ page
) >> PAGE_SHIFT
), page_size
);
2295 pagerOffset
+= page_size
;
2298 assert( KERN_SUCCESS
== err
);
2304 /* *** Temporary Workaround *** */
2306 /* This call to vm_fault causes an early pmap level resolution */
2307 /* of the mappings created above. Need for this is in absolute */
2308 /* violation of the basic tenet that the pmap layer is a cache. */
2309 /* Further, it implies a serious I/O architectural violation on */
2310 /* the part of some user of the mapping. As of this writing, */
2311 /* the call to vm_fault is needed because the NVIDIA driver */
2312 /* makes a call to pmap_extract. The NVIDIA driver needs to be */
2313 /* fixed as soon as possible. The NVIDIA driver should not */
2314 /* need to query for this info as it should know from the doMap */
2315 /* call where the physical memory is mapped. When a query is */
2316 /* necessary to find a physical mapping, it should be done */
2317 /* through an iokit call which includes the mapped memory */
2318 /* handle. This is required for machine architecture independence.*/
2320 if(!(kIOMemoryRedirected
& _flags
)) {
2321 vm_fault(addressMap
,
2322 (vm_map_offset_t
)address
,
2323 VM_PROT_READ
|VM_PROT_WRITE
,
2324 FALSE
, THREAD_UNINT
, NULL
,
2325 (vm_map_offset_t
)0);
2328 /* *** Temporary Workaround *** */
2331 sourceOffset
+= segLen
- pageOffset
;
2337 && (physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
)));
2340 err
= kIOReturnBadArgument
;
2345 IOReturn
IOMemoryDescriptor::doUnmap(
2346 vm_map_t addressMap
,
2347 IOVirtualAddress logical
,
2348 IOByteCount length
)
2353 if( kIOLogMapping
& gIOKitDebug
)
2354 kprintf("IOMemoryDescriptor::doUnmap(%x) %08x:%08x\n",
2355 addressMap
, logical
, length
);
2358 if( true /* && (addressMap == kernel_map) || (addressMap == get_task_map(current_task()))*/) {
2360 if( _memEntry
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2361 addressMap
= IOPageableMapForAddress( logical
);
2363 err
= vm_deallocate( addressMap
, logical
, length
);
2366 err
= kIOReturnSuccess
;
2371 IOReturn
IOMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2373 IOReturn err
= kIOReturnSuccess
;
2374 _IOMemoryMap
* mapping
= 0;
2380 _flags
|= kIOMemoryRedirected
;
2382 _flags
&= ~kIOMemoryRedirected
;
2385 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
2386 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject()))
2387 mapping
->redirect( safeTask
, doRedirect
);
2400 // temporary binary compatibility
2401 IOSubMemoryDescriptor
* subMem
;
2402 if( (subMem
= OSDynamicCast( IOSubMemoryDescriptor
, this)))
2403 err
= subMem
->redirect( safeTask
, doRedirect
);
2405 err
= kIOReturnSuccess
;
2410 IOReturn
IOSubMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2412 return( _parent
->redirect( safeTask
, doRedirect
));
2415 IOReturn
_IOMemoryMap::redirect( task_t safeTask
, bool doRedirect
)
2417 IOReturn err
= kIOReturnSuccess
;
2420 // err = ((_IOMemoryMap *)superMap)->redirect( safeTask, doRedirect );
2424 if( logical
&& addressMap
2425 && (!safeTask
|| (get_task_map(safeTask
) != addressMap
))
2426 && (0 == (options
& kIOMapStatic
)))
2428 IOUnmapPages( addressMap
, logical
, length
);
2429 if(!doRedirect
&& safeTask
2430 && ((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
))
2432 err
= vm_deallocate( addressMap
, logical
, length
);
2433 err
= memory
->doMap( addressMap
, &logical
,
2434 (options
& ~kIOMapAnywhere
) /*| kIOMapReserve*/,
2437 err
= kIOReturnSuccess
;
2439 IOLog("IOMemoryMap::redirect(%d, %p) %x:%lx from %p\n", doRedirect
, this, logical
, length
, addressMap
);
2445 if (((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2447 && (doRedirect
!= (0 != (memory
->_flags
& kIOMemoryRedirected
))))
2448 memory
->redirect(safeTask
, doRedirect
);
2453 IOReturn
_IOMemoryMap::unmap( void )
2459 if( logical
&& addressMap
&& (0 == superMap
)
2460 && (0 == (options
& kIOMapStatic
))) {
2462 err
= memory
->doUnmap( addressMap
, logical
, length
);
2463 vm_map_deallocate(addressMap
);
2467 err
= kIOReturnSuccess
;
2476 void _IOMemoryMap::taskDied( void )
2480 vm_map_deallocate(addressMap
);
2488 // Overload the release mechanism. All mappings must be a member
2489 // of a memory descriptors _mappings set. This means that we
2490 // always have 2 references on a mapping. When either of these mappings
2491 // are released we need to free ourselves.
2492 void _IOMemoryMap::taggedRelease(const void *tag
) const
2495 super::taggedRelease(tag
, 2);
2499 void _IOMemoryMap::free()
2505 memory
->removeMapping( this);
2510 if (owner
&& (owner
!= memory
))
2513 owner
->removeMapping(this);
2518 superMap
->release();
2521 upl_commit(redirUPL
, NULL
, 0);
2522 upl_deallocate(redirUPL
);
2528 IOByteCount
_IOMemoryMap::getLength()
2533 IOVirtualAddress
_IOMemoryMap::getVirtualAddress()
2538 task_t
_IOMemoryMap::getAddressTask()
2541 return( superMap
->getAddressTask());
2543 return( addressTask
);
2546 IOOptionBits
_IOMemoryMap::getMapOptions()
2551 IOMemoryDescriptor
* _IOMemoryMap::getMemoryDescriptor()
2556 _IOMemoryMap
* _IOMemoryMap::copyCompatible(
2557 IOMemoryDescriptor
* owner
,
2559 IOVirtualAddress toAddress
,
2560 IOOptionBits _options
,
2561 IOByteCount _offset
,
2562 IOByteCount _length
)
2564 _IOMemoryMap
* mapping
;
2566 if( (!task
) || (!addressMap
) || (addressMap
!= get_task_map(task
)))
2568 if( options
& kIOMapUnique
)
2570 if( (options
^ _options
) & kIOMapReadOnly
)
2572 if( (kIOMapDefaultCache
!= (_options
& kIOMapCacheMask
))
2573 && ((options
^ _options
) & kIOMapCacheMask
))
2576 if( (0 == (_options
& kIOMapAnywhere
)) && (logical
!= toAddress
))
2579 if( _offset
< offset
)
2584 if( (_offset
+ _length
) > length
)
2587 if( (length
== _length
) && (!_offset
)) {
2592 mapping
= new _IOMemoryMap
;
2594 && !mapping
->initCompatible( owner
, this, _offset
, _length
)) {
2603 IOPhysicalAddress
_IOMemoryMap::getPhysicalSegment( IOByteCount _offset
,
2604 IOPhysicalLength
* _length
)
2606 IOPhysicalAddress address
;
2609 address
= memory
->getPhysicalSegment( offset
+ _offset
, _length
);
2615 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2618 #define super OSObject
2620 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2622 void IOMemoryDescriptor::initialize( void )
2624 if( 0 == gIOMemoryLock
)
2625 gIOMemoryLock
= IORecursiveLockAlloc();
2627 IORegistryEntry::getRegistryRoot()->setProperty(kIOMaximumMappedIOByteCountKey
,
2628 ptoa_64(gIOMaximumMappedIOPageCount
), 64);
2631 void IOMemoryDescriptor::free( void )
2634 _mappings
->release();
2639 IOMemoryMap
* IOMemoryDescriptor::setMapping(
2641 IOVirtualAddress mapAddress
,
2642 IOOptionBits options
)
2644 _IOMemoryMap
* newMap
;
2646 newMap
= new _IOMemoryMap
;
2651 && !newMap
->initWithDescriptor( this, intoTask
, mapAddress
,
2652 options
| kIOMapStatic
, 0, getLength() )) {
2657 addMapping( newMap
);
2664 IOMemoryMap
* IOMemoryDescriptor::map(
2665 IOOptionBits options
)
2668 return( makeMapping( this, kernel_task
, 0,
2669 options
| kIOMapAnywhere
,
2673 IOMemoryMap
* IOMemoryDescriptor::map(
2675 IOVirtualAddress toAddress
,
2676 IOOptionBits options
,
2678 IOByteCount length
)
2681 length
= getLength();
2683 return( makeMapping( this, intoTask
, toAddress
, options
, offset
, length
));
2686 IOReturn
_IOMemoryMap::redirect(IOMemoryDescriptor
* newBackingMemory
,
2687 IOOptionBits options
,
2690 IOReturn err
= kIOReturnSuccess
;
2691 IOMemoryDescriptor
* physMem
= 0;
2695 if (logical
&& addressMap
) do
2697 if ((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2705 vm_size_t size
= length
;
2706 int flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
2707 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
2708 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) memory
->_memEntry
, 0, &size
, &redirUPL
,
2715 IOUnmapPages( addressMap
, logical
, length
);
2716 physMem
->redirect(0, true);
2720 if (newBackingMemory
)
2722 if (newBackingMemory
!= memory
)
2724 if (this != newBackingMemory
->makeMapping(newBackingMemory
, addressTask
, (IOVirtualAddress
) this,
2725 options
| kIOMapUnique
| kIOMapReference
,
2727 err
= kIOReturnError
;
2731 upl_commit(redirUPL
, NULL
, 0);
2732 upl_deallocate(redirUPL
);
2736 physMem
->redirect(0, false);
2749 IOMemoryMap
* IOMemoryDescriptor::makeMapping(
2750 IOMemoryDescriptor
* owner
,
2752 IOVirtualAddress toAddress
,
2753 IOOptionBits options
,
2755 IOByteCount length
)
2757 IOMemoryDescriptor
* mapDesc
= 0;
2758 _IOMemoryMap
* mapping
= 0;
2765 if (kIOMapUnique
& options
)
2767 IOPhysicalAddress phys
;
2768 IOByteCount physLen
;
2773 if ((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2775 phys
= getPhysicalSegment(offset
, &physLen
);
2776 if (!phys
|| (physLen
< length
))
2779 mapDesc
= IOMemoryDescriptor::withPhysicalAddress(
2780 phys
, length
, _direction
);
2791 if (kIOMapReference
& options
)
2793 mapping
= (_IOMemoryMap
*) toAddress
;
2797 uint32_t pageOffset1
= mapDesc
->getSourceSegment( offset
, NULL
);
2798 pageOffset1
-= trunc_page_32( pageOffset1
);
2800 uint32_t pageOffset2
= mapping
->getVirtualAddress();
2801 pageOffset2
-= trunc_page_32( pageOffset2
);
2803 if (pageOffset1
!= pageOffset2
)
2804 IOLog("::redirect can't map offset %x to addr %x\n",
2805 pageOffset1
, mapping
->getVirtualAddress());
2809 if (!mapping
->initWithDescriptor( mapDesc
, intoTask
, toAddress
, options
,
2813 IOLog("Didn't redirect map %08lx : %08lx\n", offset
, length
);
2818 mapping
->owner
->removeMapping(mapping
);
2824 // look for an existing mapping
2825 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
2827 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject())) {
2829 if( (mapping
= mapping
->copyCompatible(
2830 owner
, intoTask
, toAddress
,
2831 options
| kIOMapReference
,
2842 if( mapping
|| (options
& kIOMapReference
))
2850 mapping
= new _IOMemoryMap
;
2852 && !mapping
->initWithDescriptor( mapDesc
, intoTask
, toAddress
, options
,
2855 IOLog("Didn't make map %08lx : %08lx\n", offset
, length
);
2868 mapping
->owner
= owner
;
2869 owner
->addMapping( mapping
);
2881 void IOMemoryDescriptor::addMapping(
2882 IOMemoryMap
* mapping
)
2886 _mappings
= OSSet::withCapacity(1);
2888 _mappings
->setObject( mapping
);
2892 void IOMemoryDescriptor::removeMapping(
2893 IOMemoryMap
* mapping
)
2896 _mappings
->removeObject( mapping
);
2899 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2902 #define super IOMemoryDescriptor
2904 OSDefineMetaClassAndStructors(IOSubMemoryDescriptor
, IOMemoryDescriptor
)
2906 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2908 bool IOSubMemoryDescriptor::initSubRange( IOMemoryDescriptor
* parent
,
2909 IOByteCount offset
, IOByteCount length
,
2910 IODirection direction
)
2915 if( (offset
+ length
) > parent
->getLength())
2919 * We can check the _parent instance variable before having ever set it
2920 * to an initial value because I/O Kit guarantees that all our instance
2921 * variables are zeroed on an object's allocation.
2929 * An existing memory descriptor is being retargeted to
2930 * point to somewhere else. Clean up our present state.
2941 _direction
= direction
;
2942 _tag
= parent
->getTag();
2947 void IOSubMemoryDescriptor::free( void )
2956 IOPhysicalAddress
IOSubMemoryDescriptor::getPhysicalSegment( IOByteCount offset
,
2957 IOByteCount
* length
)
2959 IOPhysicalAddress address
;
2960 IOByteCount actualLength
;
2962 assert(offset
<= _length
);
2967 if( offset
>= _length
)
2970 address
= _parent
->getPhysicalSegment( offset
+ _start
, &actualLength
);
2972 if( address
&& length
)
2973 *length
= min( _length
- offset
, actualLength
);
2979 IOReturn
IOSubMemoryDescriptor::doMap(
2980 vm_map_t addressMap
,
2981 IOVirtualAddress
* atAddress
,
2982 IOOptionBits options
,
2983 IOByteCount sourceOffset
,
2984 IOByteCount length
)
2986 if( sourceOffset
>= _length
)
2987 return( kIOReturnOverrun
);
2988 return (_parent
->doMap(addressMap
, atAddress
, options
, sourceOffset
+ _start
, length
));
2991 IOPhysicalAddress
IOSubMemoryDescriptor::getSourceSegment( IOByteCount offset
,
2992 IOByteCount
* length
)
2994 IOPhysicalAddress address
;
2995 IOByteCount actualLength
;
2997 assert(offset
<= _length
);
3002 if( offset
>= _length
)
3005 address
= _parent
->getSourceSegment( offset
+ _start
, &actualLength
);
3007 if( address
&& length
)
3008 *length
= min( _length
- offset
, actualLength
);
3013 void * IOSubMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
3014 IOByteCount
* lengthOfSegment
)
3019 IOByteCount
IOSubMemoryDescriptor::readBytes(IOByteCount offset
,
3020 void * bytes
, IOByteCount length
)
3022 IOByteCount byteCount
;
3024 assert(offset
<= _length
);
3026 if( offset
>= _length
)
3030 byteCount
= _parent
->readBytes( _start
+ offset
, bytes
,
3031 min(length
, _length
- offset
) );
3034 return( byteCount
);
3037 IOByteCount
IOSubMemoryDescriptor::writeBytes(IOByteCount offset
,
3038 const void* bytes
, IOByteCount length
)
3040 IOByteCount byteCount
;
3042 assert(offset
<= _length
);
3044 if( offset
>= _length
)
3048 byteCount
= _parent
->writeBytes( _start
+ offset
, bytes
,
3049 min(length
, _length
- offset
) );
3052 return( byteCount
);
3055 IOReturn
IOSubMemoryDescriptor::setPurgeable( IOOptionBits newState
,
3056 IOOptionBits
* oldState
)
3061 err
= _parent
->setPurgeable( newState
, oldState
);
3067 IOReturn
IOSubMemoryDescriptor::performOperation( IOOptionBits options
,
3068 IOByteCount offset
, IOByteCount length
)
3072 assert(offset
<= _length
);
3074 if( offset
>= _length
)
3075 return( kIOReturnOverrun
);
3078 err
= _parent
->performOperation( options
, _start
+ offset
,
3079 min(length
, _length
- offset
) );
3085 IOReturn
IOSubMemoryDescriptor::prepare(
3086 IODirection forDirection
)
3091 err
= _parent
->prepare( forDirection
);
3097 IOReturn
IOSubMemoryDescriptor::complete(
3098 IODirection forDirection
)
3103 err
= _parent
->complete( forDirection
);
3109 IOMemoryMap
* IOSubMemoryDescriptor::makeMapping(
3110 IOMemoryDescriptor
* owner
,
3112 IOVirtualAddress toAddress
,
3113 IOOptionBits options
,
3115 IOByteCount length
)
3117 IOMemoryMap
* mapping
= 0;
3119 if (!(kIOMapUnique
& options
))
3120 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3122 toAddress
- (_start
+ offset
),
3123 options
| kIOMapReference
,
3124 _start
+ offset
, length
);
3127 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3130 options
, _start
+ offset
, length
);
3133 mapping
= super::makeMapping( owner
, intoTask
, toAddress
, options
,
3142 IOSubMemoryDescriptor::initWithAddress(void * address
,
3144 IODirection direction
)
3150 IOSubMemoryDescriptor::initWithAddress(vm_address_t address
,
3152 IODirection direction
,
3159 IOSubMemoryDescriptor::initWithPhysicalAddress(
3160 IOPhysicalAddress address
,
3162 IODirection direction
)
3168 IOSubMemoryDescriptor::initWithRanges(
3169 IOVirtualRange
* ranges
,
3171 IODirection direction
,
3179 IOSubMemoryDescriptor::initWithPhysicalRanges( IOPhysicalRange
* ranges
,
3181 IODirection direction
,
3187 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3189 bool IOGeneralMemoryDescriptor::serialize(OSSerialize
* s
) const
3191 OSSymbol
const *keys
[2];
3192 OSObject
*values
[2];
3194 user_addr_t address
;
3197 unsigned int index
, nRanges
;
3200 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
3202 if (s
== NULL
) return false;
3203 if (s
->previouslySerialized(this)) return true;
3205 // Pretend we are an array.
3206 if (!s
->addXMLStartTag(this, "array")) return false;
3208 nRanges
= _rangesCount
;
3209 vcopy
= (SerData
*) IOMalloc(sizeof(SerData
) * nRanges
);
3210 if (vcopy
== 0) return false;
3212 keys
[0] = OSSymbol::withCString("address");
3213 keys
[1] = OSSymbol::withCString("length");
3216 values
[0] = values
[1] = 0;
3218 // From this point on we can go to bail.
3220 // Copy the volatile data so we don't have to allocate memory
3221 // while the lock is held.
3223 if (nRanges
== _rangesCount
) {
3224 Ranges vec
= _ranges
;
3225 for (index
= 0; index
< nRanges
; index
++) {
3226 user_addr_t addr
; IOByteCount len
;
3227 getAddrLenForInd(addr
, len
, type
, vec
, index
);
3228 vcopy
[index
].address
= addr
;
3229 vcopy
[index
].length
= len
;
3232 // The descriptor changed out from under us. Give up.
3239 for (index
= 0; index
< nRanges
; index
++)
3241 user_addr_t addr
= vcopy
[index
].address
;
3242 IOByteCount len
= (IOByteCount
) vcopy
[index
].length
;
3244 OSNumber::withNumber(addr
, (((UInt64
) addr
) >> 32)? 64 : 32);
3245 if (values
[0] == 0) {
3249 values
[1] = OSNumber::withNumber(len
, sizeof(len
) * 8);
3250 if (values
[1] == 0) {
3254 OSDictionary
*dict
= OSDictionary::withObjects((const OSObject
**)values
, (const OSSymbol
**)keys
, 2);
3259 values
[0]->release();
3260 values
[1]->release();
3261 values
[0] = values
[1] = 0;
3263 result
= dict
->serialize(s
);
3269 result
= s
->addXMLEndTag("array");
3273 values
[0]->release();
3275 values
[1]->release();
3281 IOFree(vcopy
, sizeof(IOVirtualRange
) * nRanges
);
3285 bool IOSubMemoryDescriptor::serialize(OSSerialize
* s
) const
3290 if (s
->previouslySerialized(this)) return true;
3292 // Pretend we are a dictionary.
3293 // We must duplicate the functionality of OSDictionary here
3294 // because otherwise object references will not work;
3295 // they are based on the value of the object passed to
3296 // previouslySerialized and addXMLStartTag.
3298 if (!s
->addXMLStartTag(this, "dict")) return false;
3300 char const *keys
[3] = {"offset", "length", "parent"};
3302 OSObject
*values
[3];
3303 values
[0] = OSNumber::withNumber(_start
, sizeof(_start
) * 8);
3306 values
[1] = OSNumber::withNumber(_length
, sizeof(_length
) * 8);
3307 if (values
[1] == 0) {
3308 values
[0]->release();
3311 values
[2] = _parent
;
3314 for (int i
=0; i
<3; i
++) {
3315 if (!s
->addString("<key>") ||
3316 !s
->addString(keys
[i
]) ||
3317 !s
->addXMLEndTag("key") ||
3318 !values
[i
]->serialize(s
)) {
3323 values
[0]->release();
3324 values
[1]->release();
3329 return s
->addXMLEndTag("dict");
3332 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3334 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 0);
3335 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 1);
3336 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 2);
3337 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 3);
3338 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 4);
3339 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 5);
3340 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 6);
3341 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 7);
3342 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 8);
3343 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 9);
3344 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 10);
3345 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 11);
3346 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 12);
3347 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 13);
3348 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 14);
3349 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 15);
3351 /* ex-inline function implementation */
3352 IOPhysicalAddress
IOMemoryDescriptor::getPhysicalAddress()
3353 { return( getPhysicalSegment( 0, 0 )); }