2 * Copyright (c) 1998-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
34 // 45678901234567890123456789012345678901234567890123456789012345678901234567890
35 #include <sys/cdefs.h>
37 #include <IOKit/assert.h>
38 #include <IOKit/system.h>
39 #include <IOKit/IOLib.h>
40 #include <IOKit/IOMemoryDescriptor.h>
41 #include <IOKit/IOMapper.h>
42 #include <IOKit/IOKitKeysPrivate.h>
44 #include <IOKit/IOKitDebug.h>
45 #include <libkern/OSDebug.h>
47 #include "IOKitKernelInternal.h"
48 #include "IOCopyMapper.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 <mach/memory_object_types.h>
62 #include <device/device_port.h>
64 #include <mach/vm_prot.h>
65 #include <mach/mach_vm.h>
66 #include <vm/vm_fault.h>
67 #include <vm/vm_protos.h>
69 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
70 void ipc_port_release_send(ipc_port_t port
);
72 /* Copy between a physical page and a virtual address in the given vm_map */
73 kern_return_t
copypv(addr64_t source
, addr64_t sink
, unsigned int size
, int which
);
77 memory_object_t pager
,
82 device_pager_deallocate(
85 device_pager_populate_object(
86 memory_object_t pager
,
87 vm_object_offset_t offset
,
91 memory_object_iopl_request(
93 memory_object_offset_t offset
,
96 upl_page_info_array_t user_page_list
,
97 unsigned int *page_list_count
,
100 unsigned int IOTranslateCacheBits(struct phys_entry
*pp
);
104 #define kIOMaximumMappedIOByteCount (512*1024*1024)
106 static IOMapper
* gIOSystemMapper
= NULL
;
108 IOCopyMapper
* gIOCopyMapper
= NULL
;
110 static ppnum_t gIOMaximumMappedIOPageCount
= atop_32(kIOMaximumMappedIOByteCount
);
114 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
116 OSDefineMetaClassAndAbstractStructors( IOMemoryDescriptor
, OSObject
)
118 #define super IOMemoryDescriptor
120 OSDefineMetaClassAndStructors(IOGeneralMemoryDescriptor
, IOMemoryDescriptor
)
122 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
124 static IORecursiveLock
* gIOMemoryLock
;
126 #define LOCK IORecursiveLockLock( gIOMemoryLock)
127 #define UNLOCK IORecursiveLockUnlock( gIOMemoryLock)
128 #define SLEEP IORecursiveLockSleep( gIOMemoryLock, (void *)this, THREAD_UNINT)
130 IORecursiveLockWakeup( gIOMemoryLock, (void *)this, /* one-thread */ false)
133 #define DEBG(fmt, args...) { kprintf(fmt, ## args); }
135 #define DEBG(fmt, args...) {}
138 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
140 class _IOMemoryMap
: public IOMemoryMap
142 OSDeclareDefaultStructors(_IOMemoryMap
)
144 IOMemoryDescriptor
* fMemory
;
145 IOMemoryMap
* fSuperMap
;
146 mach_vm_size_t fOffset
;
147 mach_vm_address_t fAddress
;
148 mach_vm_size_t fLength
;
150 vm_map_t fAddressMap
;
151 IOOptionBits fOptions
;
153 ipc_port_t fRedirEntry
;
154 IOMemoryDescriptor
* fOwner
;
157 virtual void taggedRelease(const void *tag
= 0) const;
162 // IOMemoryMap methods
163 virtual IOVirtualAddress
getVirtualAddress();
164 virtual IOByteCount
getLength();
165 virtual task_t
getAddressTask();
166 virtual mach_vm_address_t
getAddress();
167 virtual mach_vm_size_t
getSize();
168 virtual IOMemoryDescriptor
* getMemoryDescriptor();
169 virtual IOOptionBits
getMapOptions();
171 virtual IOReturn
unmap();
172 virtual void taskDied();
174 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
175 IOOptionBits options
,
176 IOByteCount offset
= 0);
178 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
179 IOOptionBits options
,
180 mach_vm_size_t offset
= 0);
182 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
183 IOByteCount
* length
);
185 // for IOMemoryDescriptor use
186 _IOMemoryMap
* copyCompatible( _IOMemoryMap
* newMapping
);
190 mach_vm_address_t toAddress
,
191 IOOptionBits options
,
192 mach_vm_size_t offset
,
193 mach_vm_size_t length
);
195 bool setMemoryDescriptor(IOMemoryDescriptor
* _memory
, mach_vm_size_t _offset
);
198 task_t intoTask
, bool redirect
);
201 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
203 // Some data structures and accessor macros used by the initWithOptions
206 enum ioPLBlockFlags
{
207 kIOPLOnDevice
= 0x00000001,
208 kIOPLExternUPL
= 0x00000002,
211 struct typePersMDData
213 const IOGeneralMemoryDescriptor
*fMD
;
214 ipc_port_t fMemEntry
;
219 vm_address_t fIOMDOffset
; // The offset of this iopl in descriptor
220 vm_offset_t fPageInfo
; // Pointer to page list or index into it
221 ppnum_t fMappedBase
; // Page number of first page in this iopl
222 unsigned int fPageOffset
; // Offset within first page of iopl
223 unsigned int fFlags
; // Flags
228 unsigned int fPageCnt
;
229 upl_page_info_t fPageList
[];
233 #define getDataP(osd) ((ioGMDData *) (osd)->getBytesNoCopy())
234 #define getIOPLList(d) ((ioPLBlock *) &(d->fPageList[d->fPageCnt]))
235 #define getNumIOPL(osd, d) \
236 (((osd)->getLength() - ((char *) getIOPLList(d) - (char *) d)) / sizeof(ioPLBlock))
237 #define getPageList(d) (&(d->fPageList[0]))
238 #define computeDataSize(p, u) \
239 (sizeof(ioGMDData) + p * sizeof(upl_page_info_t) + u * sizeof(ioPLBlock))
242 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
244 #define next_page(a) ( trunc_page_32(a) + PAGE_SIZE )
249 kern_return_t
device_data_action(
251 ipc_port_t device_pager
,
252 vm_prot_t protection
,
253 vm_object_offset_t offset
,
256 struct ExpansionData
{
258 unsigned int pagerContig
:1;
259 unsigned int unused
:31;
260 IOMemoryDescriptor
* memory
;
263 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
264 IOMemoryDescriptor
* memDesc
;
267 memDesc
= ref
->memory
;
271 kr
= memDesc
->handleFault( device_pager
, 0, 0,
272 offset
, size
, kIOMapDefaultCache
/*?*/);
282 kern_return_t
device_close(
285 struct ExpansionData
{
287 unsigned int pagerContig
:1;
288 unsigned int unused
:31;
289 IOMemoryDescriptor
* memory
;
291 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
293 IODelete( ref
, ExpansionData
, 1 );
295 return( kIOReturnSuccess
);
299 // Note this inline function uses C++ reference arguments to return values
300 // This means that pointers are not passed and NULLs don't have to be
301 // checked for as a NULL reference is illegal.
303 getAddrLenForInd(user_addr_t
&addr
, IOPhysicalLength
&len
, // Output variables
304 UInt32 type
, IOGeneralMemoryDescriptor::Ranges r
, UInt32 ind
)
306 assert(kIOMemoryTypeUIO
== type
307 || kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
308 || kIOMemoryTypePhysical
== type
|| kIOMemoryTypePhysical64
== type
);
309 if (kIOMemoryTypeUIO
== type
) {
311 uio_getiov((uio_t
) r
.uio
, ind
, &addr
, &us
); len
= us
;
313 else if ((kIOMemoryTypeVirtual64
== type
) || (kIOMemoryTypePhysical64
== type
)) {
314 IOAddressRange cur
= r
.v64
[ind
];
319 IOVirtualRange cur
= r
.v
[ind
];
325 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
330 * Create a new IOMemoryDescriptor. The buffer is a virtual address
331 * relative to the specified task. If no task is supplied, the kernel
335 IOMemoryDescriptor::withAddress(void * address
,
337 IODirection direction
)
339 return IOMemoryDescriptor::
340 withAddress((vm_address_t
) address
, length
, direction
, kernel_task
);
344 IOMemoryDescriptor::withAddress(vm_address_t address
,
346 IODirection direction
,
352 IOOptionBits options
= (IOOptionBits
) direction
;
353 if (task
== kernel_task
)
354 options
|= kIOMemoryAutoPrepare
;
355 return (IOMemoryDescriptor::withAddressRange(address
, length
, options
, task
));
358 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
361 if (that
->initWithAddress(address
, length
, direction
, task
))
370 IOMemoryDescriptor::withPhysicalAddress(
371 IOPhysicalAddress address
,
373 IODirection direction
)
376 return (IOMemoryDescriptor::withAddressRange(address
, length
, (IOOptionBits
) direction
, NULL
));
378 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
380 && !self
->initWithPhysicalAddress(address
, length
, direction
)) {
389 IOMemoryDescriptor::withRanges( IOVirtualRange
* ranges
,
391 IODirection direction
,
395 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
398 if (that
->initWithRanges(ranges
, withCount
, direction
, task
, asReference
))
407 IOMemoryDescriptor::withAddressRange(mach_vm_address_t address
,
408 mach_vm_size_t length
,
409 IOOptionBits options
,
412 IOAddressRange range
= { address
, length
};
413 return (IOMemoryDescriptor::withAddressRanges(&range
, 1, options
, task
));
417 IOMemoryDescriptor::withAddressRanges(IOAddressRange
* ranges
,
419 IOOptionBits options
,
422 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
426 options
|= kIOMemoryTypeVirtual64
;
428 options
|= kIOMemoryTypePhysical64
;
430 if (that
->initWithOptions(ranges
, rangeCount
, 0, task
, options
, /* mapper */ 0))
443 * Create a new IOMemoryDescriptor. The buffer is made up of several
444 * virtual address ranges, from a given task.
446 * Passing the ranges as a reference will avoid an extra allocation.
449 IOMemoryDescriptor::withOptions(void * buffers
,
456 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
459 && !self
->initWithOptions(buffers
, count
, offset
, task
, opts
, mapper
))
468 // Can't leave abstract but this should never be used directly,
469 bool IOMemoryDescriptor::initWithOptions(void * buffers
,
473 IOOptionBits options
,
476 // @@@ gvdl: Should I panic?
477 panic("IOMD::initWithOptions called\n");
482 IOMemoryDescriptor::withPhysicalRanges( IOPhysicalRange
* ranges
,
484 IODirection direction
,
487 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
490 if (that
->initWithPhysicalRanges(ranges
, withCount
, direction
, asReference
))
499 IOMemoryDescriptor::withSubRange(IOMemoryDescriptor
* of
,
502 IODirection direction
)
504 IOSubMemoryDescriptor
*self
= new IOSubMemoryDescriptor
;
506 if (self
&& !self
->initSubRange(of
, offset
, length
, direction
)) {
514 IOMemoryDescriptor::withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
)
516 IOGeneralMemoryDescriptor
*origGenMD
=
517 OSDynamicCast(IOGeneralMemoryDescriptor
, originalMD
);
520 return IOGeneralMemoryDescriptor::
521 withPersistentMemoryDescriptor(origGenMD
);
527 IOGeneralMemoryDescriptor::withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
)
529 ipc_port_t sharedMem
= (ipc_port_t
) originalMD
->createNamedEntry();
534 if (sharedMem
== originalMD
->_memEntry
) {
535 originalMD
->retain(); // Add a new reference to ourselves
536 ipc_port_release_send(sharedMem
); // Remove extra send right
540 IOGeneralMemoryDescriptor
* self
= new IOGeneralMemoryDescriptor
;
541 typePersMDData initData
= { originalMD
, sharedMem
};
544 && !self
->initWithOptions(&initData
, 1, 0, 0, kIOMemoryTypePersistentMD
, 0)) {
551 void *IOGeneralMemoryDescriptor::createNamedEntry()
554 ipc_port_t sharedMem
;
556 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
558 user_addr_t range0Addr
;
559 IOByteCount range0Len
;
560 getAddrLenForInd(range0Addr
, range0Len
, type
, _ranges
, 0);
561 range0Addr
= trunc_page_64(range0Addr
);
563 vm_size_t size
= ptoa_32(_pages
);
564 vm_address_t kernelPage
= (vm_address_t
) range0Addr
;
566 vm_map_t theMap
= ((_task
== kernel_task
)
567 && (kIOMemoryBufferPageable
& _flags
))
568 ? IOPageableMapForAddress(kernelPage
)
569 : get_task_map(_task
);
571 memory_object_size_t actualSize
= size
;
572 vm_prot_t prot
= VM_PROT_READ
;
574 if (kIODirectionOut
!= (kIODirectionOutIn
& _flags
))
576 prot
|= VM_PROT_WRITE
;
579 prot
|= MAP_MEM_NAMED_REUSE
;
581 error
= mach_make_memory_entry_64(theMap
,
582 &actualSize
, range0Addr
, prot
, &sharedMem
, (ipc_port_t
) _memEntry
);
584 if (KERN_SUCCESS
== error
) {
585 if (actualSize
== size
) {
589 IOLog("IOGMD::mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
590 (UInt64
)range0Addr
, (UInt32
)actualSize
, size
);
592 ipc_port_release_send( sharedMem
);
596 return MACH_PORT_NULL
;
602 * Initialize an IOMemoryDescriptor. The buffer is a virtual address
603 * relative to the specified task. If no task is supplied, the kernel
606 * An IOMemoryDescriptor can be re-used by calling initWithAddress or
607 * initWithRanges again on an existing instance -- note this behavior
608 * is not commonly supported in other I/O Kit classes, although it is
612 IOGeneralMemoryDescriptor::initWithAddress(void * address
,
613 IOByteCount withLength
,
614 IODirection withDirection
)
616 _singleRange
.v
.address
= (vm_address_t
) address
;
617 _singleRange
.v
.length
= withLength
;
619 return initWithRanges(&_singleRange
.v
, 1, withDirection
, kernel_task
, true);
623 IOGeneralMemoryDescriptor::initWithAddress(vm_address_t address
,
624 IOByteCount withLength
,
625 IODirection withDirection
,
628 _singleRange
.v
.address
= address
;
629 _singleRange
.v
.length
= withLength
;
631 return initWithRanges(&_singleRange
.v
, 1, withDirection
, withTask
, true);
635 IOGeneralMemoryDescriptor::initWithPhysicalAddress(
636 IOPhysicalAddress address
,
637 IOByteCount withLength
,
638 IODirection withDirection
)
640 _singleRange
.p
.address
= address
;
641 _singleRange
.p
.length
= withLength
;
643 return initWithPhysicalRanges( &_singleRange
.p
, 1, withDirection
, true);
647 IOGeneralMemoryDescriptor::initWithPhysicalRanges(
648 IOPhysicalRange
* ranges
,
650 IODirection direction
,
653 IOOptionBits mdOpts
= direction
| kIOMemoryTypePhysical
;
656 mdOpts
|= kIOMemoryAsReference
;
658 return initWithOptions(ranges
, count
, 0, 0, mdOpts
, /* mapper */ 0);
662 IOGeneralMemoryDescriptor::initWithRanges(
663 IOVirtualRange
* ranges
,
665 IODirection direction
,
669 IOOptionBits mdOpts
= direction
;
672 mdOpts
|= kIOMemoryAsReference
;
675 mdOpts
|= kIOMemoryTypeVirtual
;
677 // Auto-prepare if this is a kernel memory descriptor as very few
678 // clients bother to prepare() kernel memory.
679 // But it was not enforced so what are you going to do?
680 if (task
== kernel_task
)
681 mdOpts
|= kIOMemoryAutoPrepare
;
684 mdOpts
|= kIOMemoryTypePhysical
;
686 return initWithOptions(ranges
, count
, 0, task
, mdOpts
, /* mapper */ 0);
692 * IOMemoryDescriptor. The buffer is made up of several virtual address ranges,
693 * from a given task, several physical ranges, an UPL from the ubc
694 * system or a uio (may be 64bit) from the BSD subsystem.
696 * Passing the ranges as a reference will avoid an extra allocation.
698 * An IOMemoryDescriptor can be re-used by calling initWithOptions again on an
699 * existing instance -- note this behavior is not commonly supported in other
700 * I/O Kit classes, although it is supported here.
704 IOGeneralMemoryDescriptor::initWithOptions(void * buffers
,
708 IOOptionBits options
,
711 IOOptionBits type
= options
& kIOMemoryTypeMask
;
713 // Grab the original MD's configuation data to initialse the
714 // arguments to this function.
715 if (kIOMemoryTypePersistentMD
== type
) {
717 typePersMDData
*initData
= (typePersMDData
*) buffers
;
718 const IOGeneralMemoryDescriptor
*orig
= initData
->fMD
;
719 ioGMDData
*dataP
= getDataP(orig
->_memoryEntries
);
721 // Only accept persistent memory descriptors with valid dataP data.
722 assert(orig
->_rangesCount
== 1);
723 if ( !(orig
->_flags
& kIOMemoryPersistent
) || !dataP
)
726 _memEntry
= initData
->fMemEntry
; // Grab the new named entry
727 options
= orig
->_flags
| kIOMemoryAsReference
;
728 _singleRange
= orig
->_singleRange
; // Initialise our range
729 buffers
= &_singleRange
;
732 // Now grab the original task and whatever mapper was previously used
734 mapper
= dataP
->fMapper
;
736 // We are ready to go through the original initialisation now
740 case kIOMemoryTypeUIO
:
741 case kIOMemoryTypeVirtual
:
742 case kIOMemoryTypeVirtual64
:
747 if (vm_map_is_64bit(get_task_map(task
))
748 && (kIOMemoryTypeVirtual
== type
)
749 && ((IOVirtualRange
*) buffers
)->address
)
751 OSReportWithBacktrace("IOMemoryDescriptor: attempt to create 32b virtual in 64b task, use ::withAddressRange()");
756 case kIOMemoryTypePhysical
: // Neither Physical nor UPL should have a task
757 case kIOMemoryTypePhysical64
:
758 mapper
= kIOMapperNone
;
760 case kIOMemoryTypeUPL
:
764 return false; /* bad argument */
771 * We can check the _initialized instance variable before having ever set
772 * it to an initial value because I/O Kit guarantees that all our instance
773 * variables are zeroed on an object's allocation.
778 * An existing memory descriptor is being retargeted to point to
779 * somewhere else. Clean up our present state.
781 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
782 if ((kIOMemoryTypePhysical
!= type
) && (kIOMemoryTypePhysical64
!= type
))
787 if (_ranges
.v
&& _rangesIsAllocated
)
789 if (kIOMemoryTypeUIO
== type
)
790 uio_free((uio_t
) _ranges
.v
);
791 else if ((kIOMemoryTypeVirtual64
== type
) || (kIOMemoryTypePhysical64
== type
))
792 IODelete(_ranges
.v64
, IOAddressRange
, _rangesCount
);
794 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
798 { ipc_port_release_send((ipc_port_t
) _memEntry
); _memEntry
= 0; }
800 _mappings
->flushCollection();
808 // Grab the appropriate mapper
809 if (mapper
== kIOMapperNone
)
810 mapper
= 0; // No Mapper
811 else if (mapper
== kIOMapperSystem
) {
812 IOMapper::checkForSystemMapper();
813 gIOSystemMapper
= mapper
= IOMapper::gSystem
;
816 // Remove the dynamic internal use flags from the initial setting
817 options
&= ~(kIOMemoryPreparedReadOnly
);
821 // DEPRECATED variable initialisation
822 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
824 __iomd_reservedA
= 0;
825 __iomd_reservedB
= 0;
828 if (kIOMemoryThreadSafe
& options
)
831 _prepareLock
= IOLockAlloc();
833 else if (_prepareLock
)
835 IOLockFree(_prepareLock
);
839 if (kIOMemoryTypeUPL
== type
) {
842 unsigned int dataSize
= computeDataSize(/* pages */ 0, /* upls */ 1);
844 if (!_memoryEntries
) {
845 _memoryEntries
= OSData::withCapacity(dataSize
);
849 else if (!_memoryEntries
->initWithCapacity(dataSize
))
852 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
853 dataP
= getDataP(_memoryEntries
);
854 dataP
->fMapper
= mapper
;
857 // _wireCount++; // UPLs start out life wired
860 _pages
+= atop_32(offset
+ count
+ PAGE_MASK
) - atop_32(offset
);
863 upl_page_info_t
*pageList
= UPL_GET_INTERNAL_PAGE_LIST((upl_t
) buffers
);
865 iopl
.fIOPL
= (upl_t
) buffers
;
866 // Set the flag kIOPLOnDevice convieniently equal to 1
867 iopl
.fFlags
= pageList
->device
| kIOPLExternUPL
;
868 iopl
.fIOMDOffset
= 0;
870 _highestPage
= upl_get_highest_page(iopl
.fIOPL
);
872 if (!pageList
->device
) {
873 // Pre-compute the offset into the UPL's page list
874 pageList
= &pageList
[atop_32(offset
)];
877 iopl
.fMappedBase
= mapper
->iovmAlloc(_pages
);
878 mapper
->iovmInsert(iopl
.fMappedBase
, 0, pageList
, _pages
);
881 iopl
.fMappedBase
= 0;
884 iopl
.fMappedBase
= 0;
885 iopl
.fPageInfo
= (vm_address_t
) pageList
;
886 iopl
.fPageOffset
= offset
;
888 _memoryEntries
->appendBytes(&iopl
, sizeof(iopl
));
891 // kIOMemoryTypeVirtual | kIOMemoryTypeVirtual64 | kIOMemoryTypeUIO
892 // kIOMemoryTypePhysical | kIOMemoryTypePhysical64
894 // Initialize the memory descriptor
895 if (options
& kIOMemoryAsReference
) {
896 _rangesIsAllocated
= false;
898 // Hack assignment to get the buffer arg into _ranges.
899 // I'd prefer to do _ranges = (Ranges) buffers, but that doesn't
901 // This also initialises the uio & physical ranges.
902 _ranges
.v
= (IOVirtualRange
*) buffers
;
905 _rangesIsAllocated
= true;
906 switch (_flags
& kIOMemoryTypeMask
)
908 case kIOMemoryTypeUIO
:
909 _ranges
.v
= (IOVirtualRange
*) uio_duplicate((uio_t
) buffers
);
912 case kIOMemoryTypeVirtual64
:
913 case kIOMemoryTypePhysical64
:
914 _ranges
.v64
= IONew(IOAddressRange
, count
);
917 bcopy(buffers
, _ranges
.v
, count
* sizeof(IOAddressRange
));
919 case kIOMemoryTypeVirtual
:
920 case kIOMemoryTypePhysical
:
921 _ranges
.v
= IONew(IOVirtualRange
, count
);
924 bcopy(buffers
, _ranges
.v
, count
* sizeof(IOVirtualRange
));
929 // Find starting address within the vector of ranges
930 Ranges vec
= _ranges
;
933 for (unsigned ind
= 0; ind
< count
; ind
++) {
937 // addr & len are returned by this function
938 getAddrLenForInd(addr
, len
, type
, vec
, ind
);
939 pages
+= (atop_64(addr
+ len
+ PAGE_MASK
) - atop_64(addr
));
941 assert(len
>= length
); // Check for 32 bit wrap around
944 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
946 ppnum_t highPage
= atop_64(addr
+ len
- 1);
947 if (highPage
> _highestPage
)
948 _highestPage
= highPage
;
953 _rangesCount
= count
;
955 // Auto-prepare memory at creation time.
956 // Implied completion when descriptor is free-ed
957 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
958 _wireCount
++; // Physical MDs are, by definition, wired
959 else { /* kIOMemoryTypeVirtual | kIOMemoryTypeVirtual64 | kIOMemoryTypeUIO */
961 unsigned dataSize
= computeDataSize(_pages
, /* upls */ count
* 2);
963 if (!_memoryEntries
) {
964 _memoryEntries
= OSData::withCapacity(dataSize
);
968 else if (!_memoryEntries
->initWithCapacity(dataSize
))
971 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
972 dataP
= getDataP(_memoryEntries
);
973 dataP
->fMapper
= mapper
;
974 dataP
->fPageCnt
= _pages
;
976 if ( (kIOMemoryPersistent
& _flags
) && !_memEntry
)
977 _memEntry
= createNamedEntry();
979 if ((_flags
& kIOMemoryAutoPrepare
)
980 && prepare() != kIOReturnSuccess
)
993 void IOGeneralMemoryDescriptor::free()
995 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1000 reserved
->memory
= 0;
1004 if ((kIOMemoryTypePhysical
!= type
) && (kIOMemoryTypePhysical64
!= type
))
1010 _memoryEntries
->release();
1012 if (_ranges
.v
&& _rangesIsAllocated
)
1014 if (kIOMemoryTypeUIO
== type
)
1015 uio_free((uio_t
) _ranges
.v
);
1016 else if ((kIOMemoryTypeVirtual64
== type
) || (kIOMemoryTypePhysical64
== type
))
1017 IODelete(_ranges
.v64
, IOAddressRange
, _rangesCount
);
1019 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
1022 if (reserved
&& reserved
->devicePager
)
1023 device_pager_deallocate( (memory_object_t
) reserved
->devicePager
);
1025 // memEntry holds a ref on the device pager which owns reserved
1026 // (ExpansionData) so no reserved access after this point
1028 ipc_port_release_send( (ipc_port_t
) _memEntry
);
1031 IOLockFree(_prepareLock
);
1036 /* DEPRECATED */ void IOGeneralMemoryDescriptor::unmapFromKernel()
1038 panic("IOGMD::unmapFromKernel deprecated");
1041 /* DEPRECATED */ void IOGeneralMemoryDescriptor::mapIntoKernel(unsigned rangeIndex
)
1043 panic("IOGMD::mapIntoKernel deprecated");
1049 * Get the direction of the transfer.
1051 IODirection
IOMemoryDescriptor::getDirection() const
1059 * Get the length of the transfer (over all ranges).
1061 IOByteCount
IOMemoryDescriptor::getLength() const
1066 void IOMemoryDescriptor::setTag( IOOptionBits tag
)
1071 IOOptionBits
IOMemoryDescriptor::getTag( void )
1076 // @@@ gvdl: who is using this API? Seems like a wierd thing to implement.
1078 IOMemoryDescriptor::getSourceSegment( IOByteCount offset
, IOByteCount
* length
)
1080 addr64_t physAddr
= 0;
1082 if( prepare() == kIOReturnSuccess
) {
1083 physAddr
= getPhysicalSegment64( offset
, length
);
1087 return( (IOPhysicalAddress
) physAddr
); // truncated but only page offset is used
1090 IOByteCount
IOMemoryDescriptor::readBytes
1091 (IOByteCount offset
, void *bytes
, IOByteCount length
)
1093 addr64_t dstAddr
= (addr64_t
) (UInt32
) bytes
;
1094 IOByteCount remaining
;
1096 // Assert that this entire I/O is withing the available range
1097 assert(offset
< _length
);
1098 assert(offset
+ length
<= _length
);
1099 if (offset
>= _length
) {
1103 remaining
= length
= min(length
, _length
- offset
);
1104 while (remaining
) { // (process another target segment?)
1108 srcAddr64
= getPhysicalSegment64(offset
, &srcLen
);
1112 // Clip segment length to remaining
1113 if (srcLen
> remaining
)
1116 copypv(srcAddr64
, dstAddr
, srcLen
,
1117 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
1121 remaining
-= srcLen
;
1126 return length
- remaining
;
1129 IOByteCount
IOMemoryDescriptor::writeBytes
1130 (IOByteCount offset
, const void *bytes
, IOByteCount length
)
1132 addr64_t srcAddr
= (addr64_t
) (UInt32
) bytes
;
1133 IOByteCount remaining
;
1135 // Assert that this entire I/O is withing the available range
1136 assert(offset
< _length
);
1137 assert(offset
+ length
<= _length
);
1139 assert( !(kIOMemoryPreparedReadOnly
& _flags
) );
1141 if ( (kIOMemoryPreparedReadOnly
& _flags
) || offset
>= _length
) {
1145 remaining
= length
= min(length
, _length
- offset
);
1146 while (remaining
) { // (process another target segment?)
1150 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1154 // Clip segment length to remaining
1155 if (dstLen
> remaining
)
1158 copypv(srcAddr
, (addr64_t
) dstAddr64
, dstLen
,
1159 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1163 remaining
-= dstLen
;
1168 return length
- remaining
;
1171 // osfmk/device/iokit_rpc.c
1172 extern "C" unsigned int IODefaultCacheBits(addr64_t pa
);
1174 /* DEPRECATED */ void IOGeneralMemoryDescriptor::setPosition(IOByteCount position
)
1176 panic("IOGMD::setPosition deprecated");
1179 IOReturn
IOGeneralMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
1181 if (kIOMDGetCharacteristics
== op
) {
1183 if (dataSize
< sizeof(IOMDDMACharacteristics
))
1184 return kIOReturnUnderrun
;
1186 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
1187 data
->fLength
= _length
;
1188 data
->fSGCount
= _rangesCount
;
1189 data
->fPages
= _pages
;
1190 data
->fDirection
= _direction
;
1192 data
->fIsPrepared
= false;
1194 data
->fIsPrepared
= true;
1195 data
->fHighestPage
= _highestPage
;
1196 if (_memoryEntries
) {
1197 ioGMDData
*gmdData
= getDataP(_memoryEntries
);
1198 ioPLBlock
*ioplList
= getIOPLList(gmdData
);
1199 UInt count
= getNumIOPL(_memoryEntries
, gmdData
);
1201 data
->fIsMapped
= (gmdData
->fMapper
&& _pages
&& (count
> 0)
1202 && ioplList
[0].fMappedBase
);
1204 data
->fPageAlign
= (ioplList
[0].fPageOffset
& PAGE_MASK
) | ~PAGE_MASK
;
1207 data
->fIsMapped
= false;
1210 return kIOReturnSuccess
;
1212 else if (!(kIOMDWalkSegments
& op
))
1213 return kIOReturnBadArgument
;
1215 // Get the next segment
1216 struct InternalState
{
1217 IOMDDMAWalkSegmentArgs fIO
;
1223 // Find the next segment
1224 if (dataSize
< sizeof(*isP
))
1225 return kIOReturnUnderrun
;
1227 isP
= (InternalState
*) vData
;
1228 UInt offset
= isP
->fIO
.fOffset
;
1229 bool mapped
= isP
->fIO
.fMapped
;
1231 if (offset
>= _length
)
1232 return (offset
== _length
)? kIOReturnOverrun
: kIOReturnInternalError
;
1234 // Validate the previous offset
1235 UInt ind
, off2Ind
= isP
->fOffset2Index
;
1236 if ((kIOMDFirstSegment
!= op
)
1238 && (offset
== isP
->fNextOffset
|| off2Ind
<= offset
))
1241 ind
= off2Ind
= 0; // Start from beginning
1245 if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
) {
1247 // Physical address based memory descriptor
1248 const IOPhysicalRange
*physP
= (IOPhysicalRange
*) &_ranges
.p
[0];
1250 // Find the range after the one that contains the offset
1252 for (len
= 0; off2Ind
<= offset
; ind
++) {
1253 len
= physP
[ind
].length
;
1257 // Calculate length within range and starting address
1258 length
= off2Ind
- offset
;
1259 address
= physP
[ind
- 1].address
+ len
- length
;
1261 // see how far we can coalesce ranges
1262 while (ind
< _rangesCount
&& address
+ length
== physP
[ind
].address
) {
1263 len
= physP
[ind
].length
;
1269 // correct contiguous check overshoot
1273 else if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
) {
1275 // Physical address based memory descriptor
1276 const IOAddressRange
*physP
= (IOAddressRange
*) &_ranges
.v64
[0];
1278 // Find the range after the one that contains the offset
1280 for (len
= 0; off2Ind
<= offset
; ind
++) {
1281 len
= physP
[ind
].length
;
1285 // Calculate length within range and starting address
1286 length
= off2Ind
- offset
;
1287 address
= physP
[ind
- 1].address
+ len
- length
;
1289 // see how far we can coalesce ranges
1290 while (ind
< _rangesCount
&& address
+ length
== physP
[ind
].address
) {
1291 len
= physP
[ind
].length
;
1297 // correct contiguous check overshoot
1303 panic("IOGMD: not wired for the IODMACommand");
1305 assert(_memoryEntries
);
1307 ioGMDData
* dataP
= getDataP(_memoryEntries
);
1308 const ioPLBlock
*ioplList
= getIOPLList(dataP
);
1309 UInt numIOPLs
= getNumIOPL(_memoryEntries
, dataP
);
1310 upl_page_info_t
*pageList
= getPageList(dataP
);
1312 assert(numIOPLs
> 0);
1314 // Scan through iopl info blocks looking for block containing offset
1315 while (ind
< numIOPLs
&& offset
>= ioplList
[ind
].fIOMDOffset
)
1318 // Go back to actual range as search goes past it
1319 ioPLBlock ioplInfo
= ioplList
[ind
- 1];
1320 off2Ind
= ioplInfo
.fIOMDOffset
;
1323 length
= ioplList
[ind
].fIOMDOffset
;
1326 length
-= offset
; // Remainder within iopl
1328 // Subtract offset till this iopl in total list
1331 // If a mapped address is requested and this is a pre-mapped IOPL
1332 // then just need to compute an offset relative to the mapped base.
1333 if (mapped
&& ioplInfo
.fMappedBase
) {
1334 offset
+= (ioplInfo
.fPageOffset
& PAGE_MASK
);
1335 address
= ptoa_64(ioplInfo
.fMappedBase
) + offset
;
1336 continue; // Done leave do/while(false) now
1339 // The offset is rebased into the current iopl.
1340 // Now add the iopl 1st page offset.
1341 offset
+= ioplInfo
.fPageOffset
;
1343 // For external UPLs the fPageInfo field points directly to
1344 // the upl's upl_page_info_t array.
1345 if (ioplInfo
.fFlags
& kIOPLExternUPL
)
1346 pageList
= (upl_page_info_t
*) ioplInfo
.fPageInfo
;
1348 pageList
= &pageList
[ioplInfo
.fPageInfo
];
1350 // Check for direct device non-paged memory
1351 if ( ioplInfo
.fFlags
& kIOPLOnDevice
) {
1352 address
= ptoa_64(pageList
->phys_addr
) + offset
;
1353 continue; // Done leave do/while(false) now
1356 // Now we need compute the index into the pageList
1357 UInt pageInd
= atop_32(offset
);
1358 offset
&= PAGE_MASK
;
1360 // Compute the starting address of this segment
1361 IOPhysicalAddress pageAddr
= pageList
[pageInd
].phys_addr
;
1362 address
= ptoa_64(pageAddr
) + offset
;
1364 // length is currently set to the length of the remainider of the iopl.
1365 // We need to check that the remainder of the iopl is contiguous.
1366 // This is indicated by pageList[ind].phys_addr being sequential.
1367 IOByteCount contigLength
= PAGE_SIZE
- offset
;
1368 while (contigLength
< length
1369 && ++pageAddr
== pageList
[++pageInd
].phys_addr
)
1371 contigLength
+= PAGE_SIZE
;
1374 if (contigLength
< length
)
1375 length
= contigLength
;
1383 // Update return values and state
1384 isP
->fIO
.fIOVMAddr
= address
;
1385 isP
->fIO
.fLength
= length
;
1387 isP
->fOffset2Index
= off2Ind
;
1388 isP
->fNextOffset
= isP
->fIO
.fOffset
+ length
;
1390 return kIOReturnSuccess
;
1394 IOGeneralMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1397 IOByteCount length
= 0;
1398 addr64_t address
= 0;
1400 if (gIOSystemMapper
&& (kIOMemoryTypePhysical
== (_flags
& kIOMemoryTypeMask
)))
1401 return (super::getPhysicalSegment64(offset
, lengthOfSegment
));
1403 if (offset
< _length
) // (within bounds?)
1405 IOMDDMAWalkSegmentState _state
;
1406 IOMDDMAWalkSegmentArgs
* state
= (IOMDDMAWalkSegmentArgs
*) &_state
;
1408 state
->fOffset
= offset
;
1409 state
->fLength
= _length
- offset
;
1410 state
->fMapped
= false;
1412 ret
= dmaCommandOperation(kIOMDFirstSegment
, _state
, sizeof(_state
));
1414 if ((kIOReturnSuccess
!= ret
) && (kIOReturnOverrun
!= ret
))
1415 DEBG("getPhysicalSegment64 dmaCommandOperation(%lx), %p, offset %qx, addr %qx, len %qx\n",
1416 ret
, this, state
->fOffset
,
1417 state
->fIOVMAddr
, state
->fLength
);
1418 if (kIOReturnSuccess
== ret
)
1420 address
= state
->fIOVMAddr
;
1421 length
= state
->fLength
;
1427 if (lengthOfSegment
)
1428 *lengthOfSegment
= length
;
1434 IOGeneralMemoryDescriptor::getPhysicalSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1437 IOByteCount length
= 0;
1438 addr64_t address
= 0;
1440 // assert(offset <= _length);
1442 if (offset
< _length
) // (within bounds?)
1444 IOMDDMAWalkSegmentState _state
;
1445 IOMDDMAWalkSegmentArgs
* state
= (IOMDDMAWalkSegmentArgs
*) &_state
;
1447 state
->fOffset
= offset
;
1448 state
->fLength
= _length
- offset
;
1449 state
->fMapped
= true;
1451 ret
= dmaCommandOperation(
1452 kIOMDFirstSegment
, _state
, sizeof(_state
));
1454 if ((kIOReturnSuccess
!= ret
) && (kIOReturnOverrun
!= ret
))
1455 DEBG("getPhysicalSegment dmaCommandOperation(%lx), %p, offset %qx, addr %qx, len %qx\n",
1456 ret
, this, state
->fOffset
,
1457 state
->fIOVMAddr
, state
->fLength
);
1458 if (kIOReturnSuccess
== ret
)
1460 address
= state
->fIOVMAddr
;
1461 length
= state
->fLength
;
1468 if ((address
+ length
) > 0x100000000ULL
)
1470 panic("getPhysicalSegment() out of 32b range 0x%qx, len 0x%lx, class %s",
1471 address
, length
, (getMetaClass())->getClassName());
1474 if (lengthOfSegment
)
1475 *lengthOfSegment
= length
;
1477 return ((IOPhysicalAddress
) address
);
1481 IOMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1483 IOPhysicalAddress phys32
;
1486 IOMapper
* mapper
= 0;
1488 phys32
= getPhysicalSegment(offset
, lengthOfSegment
);
1492 if (gIOSystemMapper
)
1493 mapper
= gIOSystemMapper
;
1497 IOByteCount origLen
;
1499 phys64
= mapper
->mapAddr(phys32
);
1500 origLen
= *lengthOfSegment
;
1501 length
= page_size
- (phys64
& (page_size
- 1));
1502 while ((length
< origLen
)
1503 && ((phys64
+ length
) == mapper
->mapAddr(phys32
+ length
)))
1504 length
+= page_size
;
1505 if (length
> origLen
)
1508 *lengthOfSegment
= length
;
1511 phys64
= (addr64_t
) phys32
;
1517 IOGeneralMemoryDescriptor::getSourceSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1519 IOPhysicalAddress address
= 0;
1520 IOPhysicalLength length
= 0;
1521 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1523 assert(offset
<= _length
);
1525 if ( type
== kIOMemoryTypeUPL
)
1526 return super::getSourceSegment( offset
, lengthOfSegment
);
1527 else if ( offset
< _length
) // (within bounds?)
1529 unsigned rangesIndex
= 0;
1530 Ranges vec
= _ranges
;
1533 // Find starting address within the vector of ranges
1535 getAddrLenForInd(addr
, length
, type
, vec
, rangesIndex
);
1536 if (offset
< length
)
1538 offset
-= length
; // (make offset relative)
1542 // Now that we have the starting range,
1543 // lets find the last contiguous range
1547 for ( ++rangesIndex
; rangesIndex
< _rangesCount
; rangesIndex
++ ) {
1548 user_addr_t newAddr
;
1549 IOPhysicalLength newLen
;
1551 getAddrLenForInd(newAddr
, newLen
, type
, vec
, rangesIndex
);
1552 if (addr
+ length
!= newAddr
)
1557 address
= (IOPhysicalAddress
) addr
; // Truncate address to 32bit
1562 if ( lengthOfSegment
) *lengthOfSegment
= length
;
1567 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1568 /* DEPRECATED */ void * IOGeneralMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
1569 /* DEPRECATED */ IOByteCount
* lengthOfSegment
)
1571 if (_task
== kernel_task
)
1572 return (void *) getSourceSegment(offset
, lengthOfSegment
);
1574 panic("IOGMD::getVirtualSegment deprecated");
1578 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1583 IOMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
1585 if (kIOMDGetCharacteristics
== op
) {
1586 if (dataSize
< sizeof(IOMDDMACharacteristics
))
1587 return kIOReturnUnderrun
;
1589 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
1590 data
->fLength
= getLength();
1592 data
->fDirection
= _direction
;
1593 if (IOMapper::gSystem
)
1594 data
->fIsMapped
= true;
1595 data
->fIsPrepared
= true; // Assume prepared - fails safe
1597 else if (kIOMDWalkSegments
& op
) {
1598 if (dataSize
< sizeof(IOMDDMAWalkSegmentArgs
))
1599 return kIOReturnUnderrun
;
1601 IOMDDMAWalkSegmentArgs
*data
= (IOMDDMAWalkSegmentArgs
*) vData
;
1602 IOByteCount offset
= (IOByteCount
) data
->fOffset
;
1604 IOPhysicalLength length
;
1605 IOMemoryDescriptor
*ncmd
= const_cast<IOMemoryDescriptor
*>(this);
1606 if (data
->fMapped
&& IOMapper::gSystem
)
1607 data
->fIOVMAddr
= ncmd
->getPhysicalSegment(offset
, &length
);
1609 data
->fIOVMAddr
= ncmd
->getPhysicalSegment64(offset
, &length
);
1610 data
->fLength
= length
;
1613 return kIOReturnBadArgument
;
1615 return kIOReturnSuccess
;
1618 IOReturn
IOMemoryDescriptor::setPurgeable( IOOptionBits newState
,
1619 IOOptionBits
* oldState
)
1621 IOReturn err
= kIOReturnSuccess
;
1622 vm_purgable_t control
;
1629 err
= kIOReturnNotReady
;
1633 control
= VM_PURGABLE_SET_STATE
;
1636 case kIOMemoryPurgeableKeepCurrent
:
1637 control
= VM_PURGABLE_GET_STATE
;
1640 case kIOMemoryPurgeableNonVolatile
:
1641 state
= VM_PURGABLE_NONVOLATILE
;
1643 case kIOMemoryPurgeableVolatile
:
1644 state
= VM_PURGABLE_VOLATILE
;
1646 case kIOMemoryPurgeableEmpty
:
1647 state
= VM_PURGABLE_EMPTY
;
1650 err
= kIOReturnBadArgument
;
1654 if (kIOReturnSuccess
!= err
)
1657 err
= mach_memory_entry_purgable_control((ipc_port_t
) _memEntry
, control
, &state
);
1661 if (kIOReturnSuccess
== err
)
1665 case VM_PURGABLE_NONVOLATILE
:
1666 state
= kIOMemoryPurgeableNonVolatile
;
1668 case VM_PURGABLE_VOLATILE
:
1669 state
= kIOMemoryPurgeableVolatile
;
1671 case VM_PURGABLE_EMPTY
:
1672 state
= kIOMemoryPurgeableEmpty
;
1675 state
= kIOMemoryPurgeableNonVolatile
;
1676 err
= kIOReturnNotReady
;
1688 extern "C" void dcache_incoherent_io_flush64(addr64_t pa
, unsigned int count
);
1689 extern "C" void dcache_incoherent_io_store64(addr64_t pa
, unsigned int count
);
1691 IOReturn
IOMemoryDescriptor::performOperation( IOOptionBits options
,
1692 IOByteCount offset
, IOByteCount length
)
1694 IOByteCount remaining
;
1695 void (*func
)(addr64_t pa
, unsigned int count
) = 0;
1699 case kIOMemoryIncoherentIOFlush
:
1700 func
= &dcache_incoherent_io_flush64
;
1702 case kIOMemoryIncoherentIOStore
:
1703 func
= &dcache_incoherent_io_store64
;
1708 return (kIOReturnUnsupported
);
1710 remaining
= length
= min(length
, getLength() - offset
);
1712 // (process another target segment?)
1717 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1721 // Clip segment length to remaining
1722 if (dstLen
> remaining
)
1725 (*func
)(dstAddr64
, dstLen
);
1728 remaining
-= dstLen
;
1731 return (remaining
? kIOReturnUnderrun
: kIOReturnSuccess
);
1734 #if defined(__ppc__) || defined(__arm__)
1735 extern vm_offset_t static_memory_end
;
1736 #define io_kernel_static_end static_memory_end
1738 extern vm_offset_t first_avail
;
1739 #define io_kernel_static_end first_avail
1742 static kern_return_t
1743 io_get_kernel_static_upl(
1745 vm_address_t offset
,
1746 vm_size_t
*upl_size
,
1748 upl_page_info_array_t page_list
,
1749 unsigned int *count
,
1750 ppnum_t
*highest_page
)
1752 unsigned int pageCount
, page
;
1754 ppnum_t highestPage
= 0;
1756 pageCount
= atop_32(*upl_size
);
1757 if (pageCount
> *count
)
1762 for (page
= 0; page
< pageCount
; page
++)
1764 phys
= pmap_find_phys(kernel_pmap
, ((addr64_t
)offset
) + ptoa_64(page
));
1767 page_list
[page
].phys_addr
= phys
;
1768 page_list
[page
].pageout
= 0;
1769 page_list
[page
].absent
= 0;
1770 page_list
[page
].dirty
= 0;
1771 page_list
[page
].precious
= 0;
1772 page_list
[page
].device
= 0;
1773 if (phys
> highestPage
)
1777 *highest_page
= highestPage
;
1779 return ((page
>= pageCount
) ? kIOReturnSuccess
: kIOReturnVMError
);
1782 IOReturn
IOGeneralMemoryDescriptor::wireVirtual(IODirection forDirection
)
1784 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1785 IOReturn error
= kIOReturnCannotWire
;
1787 ppnum_t mapBase
= 0;
1789 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
1791 assert(!_wireCount
);
1792 assert(kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
);
1794 if (_pages
>= gIOMaximumMappedIOPageCount
)
1795 return kIOReturnNoResources
;
1797 dataP
= getDataP(_memoryEntries
);
1798 mapper
= dataP
->fMapper
;
1799 if (mapper
&& _pages
)
1800 mapBase
= mapper
->iovmAlloc(_pages
);
1802 // Note that appendBytes(NULL) zeros the data up to the
1804 _memoryEntries
->appendBytes(0, dataP
->fPageCnt
* sizeof(upl_page_info_t
));
1805 dataP
= 0; // May no longer be valid so lets not get tempted.
1807 if (forDirection
== kIODirectionNone
)
1808 forDirection
= _direction
;
1810 int uplFlags
; // This Mem Desc's default flags for upl creation
1811 switch (kIODirectionOutIn
& forDirection
)
1813 case kIODirectionOut
:
1814 // Pages do not need to be marked as dirty on commit
1815 uplFlags
= UPL_COPYOUT_FROM
;
1816 _flags
|= kIOMemoryPreparedReadOnly
;
1819 case kIODirectionIn
:
1821 uplFlags
= 0; // i.e. ~UPL_COPYOUT_FROM
1824 uplFlags
|= UPL_SET_IO_WIRE
| UPL_SET_LITE
;
1826 #ifdef UPL_NEED_32BIT_ADDR
1827 if (kIODirectionPrepareToPhys32
& forDirection
)
1828 uplFlags
|= UPL_NEED_32BIT_ADDR
;
1831 // Find the appropriate vm_map for the given task
1833 if (_task
== kernel_task
&& (kIOMemoryBufferPageable
& _flags
))
1836 { curMap
= get_task_map(_task
); }
1838 // Iterate over the vector of virtual ranges
1839 Ranges vec
= _ranges
;
1840 unsigned int pageIndex
= 0;
1841 IOByteCount mdOffset
= 0;
1842 ppnum_t highestPage
= 0;
1843 for (UInt range
= 0; range
< _rangesCount
; range
++) {
1845 user_addr_t startPage
;
1846 IOByteCount numBytes
;
1847 ppnum_t highPage
= 0;
1849 // Get the startPage address and length of vec[range]
1850 getAddrLenForInd(startPage
, numBytes
, type
, vec
, range
);
1851 iopl
.fPageOffset
= (short) startPage
& PAGE_MASK
;
1852 numBytes
+= iopl
.fPageOffset
;
1853 startPage
= trunc_page_64(startPage
);
1856 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1858 iopl
.fMappedBase
= 0;
1860 // Iterate over the current range, creating UPLs
1862 dataP
= getDataP(_memoryEntries
);
1863 vm_address_t kernelStart
= (vm_address_t
) startPage
;
1867 else if (!sharedMem
) {
1868 assert(_task
== kernel_task
);
1869 theMap
= IOPageableMapForAddress(kernelStart
);
1874 upl_page_info_array_t pageInfo
= getPageList(dataP
);
1875 int ioplFlags
= uplFlags
;
1876 upl_page_list_ptr_t baseInfo
= &pageInfo
[pageIndex
];
1878 vm_size_t ioplSize
= round_page_32(numBytes
);
1879 unsigned int numPageInfo
= atop_32(ioplSize
);
1881 if (theMap
== kernel_map
&& kernelStart
< io_kernel_static_end
) {
1882 error
= io_get_kernel_static_upl(theMap
,
1890 else if (sharedMem
) {
1891 error
= memory_object_iopl_request(sharedMem
,
1901 error
= vm_map_create_upl(theMap
,
1911 if (error
!= KERN_SUCCESS
)
1915 highPage
= upl_get_highest_page(iopl
.fIOPL
);
1916 if (highPage
> highestPage
)
1917 highestPage
= highPage
;
1919 error
= kIOReturnCannotWire
;
1921 if (baseInfo
->device
) {
1923 iopl
.fFlags
= kIOPLOnDevice
;
1924 // Don't translate device memory at all
1925 if (mapper
&& mapBase
) {
1926 mapper
->iovmFree(mapBase
, _pages
);
1928 iopl
.fMappedBase
= 0;
1934 mapper
->iovmInsert(mapBase
, pageIndex
,
1935 baseInfo
, numPageInfo
);
1938 iopl
.fIOMDOffset
= mdOffset
;
1939 iopl
.fPageInfo
= pageIndex
;
1941 if ((_flags
& kIOMemoryAutoPrepare
) && iopl
.fIOPL
)
1943 upl_commit(iopl
.fIOPL
, 0, 0);
1944 upl_deallocate(iopl
.fIOPL
);
1948 if (!_memoryEntries
->appendBytes(&iopl
, sizeof(iopl
))) {
1949 // Clean up partial created and unsaved iopl
1951 upl_abort(iopl
.fIOPL
, 0);
1952 upl_deallocate(iopl
.fIOPL
);
1957 // Check for a multiple iopl's in one virtual range
1958 pageIndex
+= numPageInfo
;
1959 mdOffset
-= iopl
.fPageOffset
;
1960 if (ioplSize
< numBytes
) {
1961 numBytes
-= ioplSize
;
1962 startPage
+= ioplSize
;
1963 mdOffset
+= ioplSize
;
1964 iopl
.fPageOffset
= 0;
1966 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1969 mdOffset
+= numBytes
;
1975 _highestPage
= highestPage
;
1977 return kIOReturnSuccess
;
1981 dataP
= getDataP(_memoryEntries
);
1982 UInt done
= getNumIOPL(_memoryEntries
, dataP
);
1983 ioPLBlock
*ioplList
= getIOPLList(dataP
);
1985 for (UInt range
= 0; range
< done
; range
++)
1987 if (ioplList
[range
].fIOPL
) {
1988 upl_abort(ioplList
[range
].fIOPL
, 0);
1989 upl_deallocate(ioplList
[range
].fIOPL
);
1992 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
1994 if (mapper
&& mapBase
)
1995 mapper
->iovmFree(mapBase
, _pages
);
1998 if (error
== KERN_FAILURE
)
1999 error
= kIOReturnCannotWire
;
2007 * Prepare the memory for an I/O transfer. This involves paging in
2008 * the memory, if necessary, and wiring it down for the duration of
2009 * the transfer. The complete() method completes the processing of
2010 * the memory after the I/O transfer finishes. This method needn't
2011 * called for non-pageable memory.
2013 IOReturn
IOGeneralMemoryDescriptor::prepare(IODirection forDirection
)
2015 IOReturn error
= kIOReturnSuccess
;
2016 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2018 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
2019 return kIOReturnSuccess
;
2022 IOLockLock(_prepareLock
);
2025 && (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
) ) {
2026 error
= wireVirtual(forDirection
);
2029 if (kIOReturnSuccess
== error
)
2033 IOLockUnlock(_prepareLock
);
2041 * Complete processing of the memory after an I/O transfer finishes.
2042 * This method should not be called unless a prepare was previously
2043 * issued; the prepare() and complete() must occur in pairs, before
2044 * before and after an I/O transfer involving pageable memory.
2047 IOReturn
IOGeneralMemoryDescriptor::complete(IODirection
/* forDirection */)
2049 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2051 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
2052 return kIOReturnSuccess
;
2055 IOLockLock(_prepareLock
);
2064 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2065 ioGMDData
* dataP
= getDataP(_memoryEntries
);
2066 ioPLBlock
*ioplList
= getIOPLList(dataP
);
2067 UInt count
= getNumIOPL(_memoryEntries
, dataP
);
2069 if (dataP
->fMapper
&& _pages
&& ioplList
[0].fMappedBase
)
2070 dataP
->fMapper
->iovmFree(ioplList
[0].fMappedBase
, _pages
);
2072 // Only complete iopls that we created which are for TypeVirtual
2073 if (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
) {
2074 for (UInt ind
= 0; ind
< count
; ind
++)
2075 if (ioplList
[ind
].fIOPL
) {
2076 upl_commit(ioplList
[ind
].fIOPL
, 0, 0);
2077 upl_deallocate(ioplList
[ind
].fIOPL
);
2080 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
2085 IOLockUnlock(_prepareLock
);
2087 return kIOReturnSuccess
;
2090 IOReturn
IOGeneralMemoryDescriptor::doMap(
2091 vm_map_t __addressMap
,
2092 IOVirtualAddress
* __address
,
2093 IOOptionBits options
,
2094 IOByteCount __offset
,
2095 IOByteCount __length
)
2098 if (!(kIOMap64Bit
& options
)) panic("IOGeneralMemoryDescriptor::doMap !64bit");
2100 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) *__address
;
2101 mach_vm_size_t offset
= mapping
->fOffset
+ __offset
;
2102 mach_vm_size_t length
= mapping
->fLength
;
2105 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
2107 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2108 Ranges vec
= _ranges
;
2110 user_addr_t range0Addr
= 0;
2111 IOByteCount range0Len
= 0;
2114 getAddrLenForInd(range0Addr
, range0Len
, type
, vec
, 0);
2116 // mapping source == dest? (could be much better)
2118 && (mapping
->fAddressMap
== get_task_map(_task
)) && (options
& kIOMapAnywhere
)
2119 && (1 == _rangesCount
) && (0 == offset
)
2120 && range0Addr
&& (length
<= range0Len
) )
2122 mapping
->fAddress
= range0Addr
;
2123 mapping
->fOptions
|= kIOMapStatic
;
2125 return( kIOReturnSuccess
);
2128 if( 0 == sharedMem
) {
2130 vm_size_t size
= ptoa_32(_pages
);
2134 memory_object_size_t actualSize
= size
;
2135 vm_prot_t prot
= VM_PROT_READ
;
2136 if (!(kIOMapReadOnly
& options
))
2137 prot
|= VM_PROT_WRITE
;
2138 else if (kIOMapDefaultCache
!= (options
& kIOMapCacheMask
))
2139 prot
|= VM_PROT_WRITE
;
2141 kr
= mach_make_memory_entry_64(get_task_map(_task
),
2142 &actualSize
, range0Addr
,
2146 if( (KERN_SUCCESS
== kr
) && (actualSize
!= round_page_32(size
))) {
2148 IOLog("mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
2149 range0Addr
, (UInt32
) actualSize
, size
);
2151 kr
= kIOReturnVMError
;
2152 ipc_port_release_send( sharedMem
);
2155 if( KERN_SUCCESS
!= kr
)
2156 sharedMem
= MACH_PORT_NULL
;
2158 } else do { // _task == 0, must be physical
2160 memory_object_t pager
;
2161 unsigned int flags
= 0;
2163 IOPhysicalLength segLen
;
2165 pa
= getPhysicalSegment64( offset
, &segLen
);
2168 reserved
= IONew( ExpansionData
, 1 );
2172 reserved
->pagerContig
= (1 == _rangesCount
);
2173 reserved
->memory
= this;
2175 /*What cache mode do we need*/
2176 switch(options
& kIOMapCacheMask
) {
2178 case kIOMapDefaultCache
:
2180 flags
= IODefaultCacheBits(pa
);
2181 if (DEVICE_PAGER_CACHE_INHIB
& flags
)
2183 if (DEVICE_PAGER_GUARDED
& flags
)
2184 mapping
->fOptions
|= kIOMapInhibitCache
;
2186 mapping
->fOptions
|= kIOMapWriteCombineCache
;
2188 else if (DEVICE_PAGER_WRITE_THROUGH
& flags
)
2189 mapping
->fOptions
|= kIOMapWriteThruCache
;
2191 mapping
->fOptions
|= kIOMapCopybackCache
;
2194 case kIOMapInhibitCache
:
2195 flags
= DEVICE_PAGER_CACHE_INHIB
|
2196 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
2199 case kIOMapWriteThruCache
:
2200 flags
= DEVICE_PAGER_WRITE_THROUGH
|
2201 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
2204 case kIOMapCopybackCache
:
2205 flags
= DEVICE_PAGER_COHERENT
;
2208 case kIOMapWriteCombineCache
:
2209 flags
= DEVICE_PAGER_CACHE_INHIB
|
2210 DEVICE_PAGER_COHERENT
;
2214 flags
|= reserved
->pagerContig
? DEVICE_PAGER_CONTIGUOUS
: 0;
2216 pager
= device_pager_setup( (memory_object_t
) 0, (int) reserved
,
2221 kr
= mach_memory_object_memory_entry_64( (host_t
) 1, false /*internal*/,
2222 size
, VM_PROT_READ
| VM_PROT_WRITE
, pager
, &sharedMem
);
2224 assert( KERN_SUCCESS
== kr
);
2225 if( KERN_SUCCESS
!= kr
)
2227 device_pager_deallocate( pager
);
2228 pager
= MACH_PORT_NULL
;
2229 sharedMem
= MACH_PORT_NULL
;
2232 if( pager
&& sharedMem
)
2233 reserved
->devicePager
= pager
;
2235 IODelete( reserved
, ExpansionData
, 1 );
2241 _memEntry
= (void *) sharedMem
;
2246 result
= kIOReturnVMError
;
2248 result
= super::doMap( __addressMap
, __address
,
2249 options
, __offset
, __length
);
2254 IOReturn
IOGeneralMemoryDescriptor::doUnmap(
2255 vm_map_t addressMap
,
2256 IOVirtualAddress __address
,
2257 IOByteCount __length
)
2259 return (super::doUnmap(addressMap
, __address
, __length
));
2262 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2264 OSDefineMetaClassAndAbstractStructors( IOMemoryMap
, OSObject
)
2266 /* inline function implementation */
2267 IOPhysicalAddress
IOMemoryMap::getPhysicalAddress()
2268 { return( getPhysicalSegment( 0, 0 )); }
2272 #define super IOMemoryMap
2274 OSDefineMetaClassAndStructors(_IOMemoryMap
, IOMemoryMap
)
2276 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2278 bool _IOMemoryMap::init(
2280 mach_vm_address_t toAddress
,
2281 IOOptionBits _options
,
2282 mach_vm_size_t _offset
,
2283 mach_vm_size_t _length
)
2291 fAddressMap
= get_task_map(intoTask
);
2294 vm_map_reference(fAddressMap
);
2296 fAddressTask
= intoTask
;
2297 fOptions
= _options
;
2300 fAddress
= toAddress
;
2305 bool _IOMemoryMap::setMemoryDescriptor(IOMemoryDescriptor
* _memory
, mach_vm_size_t _offset
)
2312 if( (_offset
+ fLength
) > _memory
->getLength())
2320 if (fMemory
!= _memory
)
2321 fMemory
->removeMapping(this);
2329 struct IOMemoryDescriptorMapAllocRef
2331 ipc_port_t sharedMem
;
2332 mach_vm_address_t mapped
;
2333 mach_vm_size_t size
;
2334 mach_vm_size_t sourceOffset
;
2335 IOOptionBits options
;
2338 static kern_return_t
IOMemoryDescriptorMapAlloc(vm_map_t map
, void * _ref
)
2340 IOMemoryDescriptorMapAllocRef
* ref
= (IOMemoryDescriptorMapAllocRef
*)_ref
;
2346 vm_prot_t prot
= VM_PROT_READ
2347 | ((ref
->options
& kIOMapReadOnly
) ? 0 : VM_PROT_WRITE
);
2349 // VM system requires write access to change cache mode
2350 if (kIOMapDefaultCache
!= (ref
->options
& kIOMapCacheMask
))
2351 prot
|= VM_PROT_WRITE
;
2353 // set memory entry cache
2354 vm_prot_t memEntryCacheMode
= prot
| MAP_MEM_ONLY
;
2355 switch (ref
->options
& kIOMapCacheMask
)
2357 case kIOMapInhibitCache
:
2358 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
2361 case kIOMapWriteThruCache
:
2362 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
2365 case kIOMapWriteCombineCache
:
2366 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
2369 case kIOMapCopybackCache
:
2370 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
2373 case kIOMapDefaultCache
:
2375 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
2379 vm_size_t unused
= 0;
2381 err
= mach_make_memory_entry( NULL
/*unused*/, &unused
, 0 /*unused*/,
2382 memEntryCacheMode
, NULL
, ref
->sharedMem
);
2383 if (KERN_SUCCESS
!= err
)
2384 IOLog("MAP_MEM_ONLY failed %d\n", err
);
2386 err
= mach_vm_map( map
,
2388 ref
->size
, 0 /* mask */,
2389 (( ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2390 | VM_MAKE_TAG(VM_MEMORY_IOKIT
),
2391 ref
->sharedMem
, ref
->sourceOffset
,
2397 if( KERN_SUCCESS
!= err
) {
2405 err
= mach_vm_allocate( map
, &ref
->mapped
, ref
->size
,
2406 ((ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2407 | VM_MAKE_TAG(VM_MEMORY_IOKIT
) );
2408 if( KERN_SUCCESS
!= err
) {
2412 // we have to make sure that these guys don't get copied if we fork.
2413 err
= vm_inherit( map
, ref
->mapped
, ref
->size
, VM_INHERIT_NONE
);
2414 assert( KERN_SUCCESS
== err
);
2423 IOMemoryDescriptorMapMemEntry(vm_map_t map
, ipc_port_t entry
, IOOptionBits options
, bool pageable
,
2424 mach_vm_size_t offset
,
2425 mach_vm_address_t
* address
, mach_vm_size_t length
)
2428 IOMemoryDescriptorMapAllocRef ref
;
2430 ref
.sharedMem
= entry
;
2431 ref
.sourceOffset
= offset
;
2432 ref
.options
= options
;
2436 if (options
& kIOMapAnywhere
)
2437 // vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
2440 ref
.mapped
= *address
;
2442 if( ref
.sharedMem
&& (map
== kernel_map
) && pageable
)
2443 err
= IOIteratePageableMaps( ref
.size
, &IOMemoryDescriptorMapAlloc
, &ref
);
2445 err
= IOMemoryDescriptorMapAlloc( map
, &ref
);
2447 *address
= ref
.mapped
;
2452 IOReturn
IOMemoryDescriptor::doMap(
2453 vm_map_t __addressMap
,
2454 IOVirtualAddress
* __address
,
2455 IOOptionBits options
,
2456 IOByteCount __offset
,
2457 IOByteCount __length
)
2459 if (!(kIOMap64Bit
& options
)) panic("IOMemoryDescriptor::doMap !64bit");
2461 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) *__address
;
2462 mach_vm_size_t offset
= mapping
->fOffset
+ __offset
;
2463 mach_vm_size_t length
= mapping
->fLength
;
2465 IOReturn err
= kIOReturnSuccess
;
2466 memory_object_t pager
;
2467 mach_vm_size_t pageOffset
;
2468 IOPhysicalAddress sourceAddr
;
2472 sourceAddr
= getSourceSegment( offset
, NULL
);
2473 pageOffset
= sourceAddr
- trunc_page_32( sourceAddr
);
2476 pager
= (memory_object_t
) reserved
->devicePager
;
2478 pager
= MACH_PORT_NULL
;
2480 if ((kIOMapReference
|kIOMapUnique
) == ((kIOMapReference
|kIOMapUnique
) & options
))
2488 err
= kIOReturnNotReadable
;
2492 size
= mapping
->fLength
+ pageOffset
;
2493 flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
2494 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
2496 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) _memEntry
, 0, &size
, &redirUPL2
,
2501 err
= upl_transpose(redirUPL2
, mapping
->fRedirUPL
);
2502 if (kIOReturnSuccess
!= err
)
2504 IOLog("upl_transpose(%x)\n", err
);
2505 err
= kIOReturnSuccess
;
2510 upl_commit(redirUPL2
, NULL
, 0);
2511 upl_deallocate(redirUPL2
);
2515 // swap the memEntries since they now refer to different vm_objects
2516 void * me
= _memEntry
;
2517 _memEntry
= mapping
->fMemory
->_memEntry
;
2518 mapping
->fMemory
->_memEntry
= me
;
2521 err
= handleFault( reserved
->devicePager
, mapping
->fAddressMap
, mapping
->fAddress
, offset
, length
, options
);
2525 mach_vm_address_t address
;
2527 if (!(options
& kIOMapAnywhere
))
2529 address
= trunc_page_64(mapping
->fAddress
);
2530 if( (mapping
->fAddress
- address
) != pageOffset
)
2532 err
= kIOReturnVMError
;
2537 err
= IOMemoryDescriptorMapMemEntry(mapping
->fAddressMap
, (ipc_port_t
) _memEntry
,
2538 options
, (kIOMemoryBufferPageable
& _flags
),
2539 offset
, &address
, round_page_64(length
+ pageOffset
));
2540 if( err
!= KERN_SUCCESS
)
2543 if (!_memEntry
|| pager
)
2545 err
= handleFault( pager
, mapping
->fAddressMap
, address
, offset
, length
, options
);
2546 if (err
!= KERN_SUCCESS
)
2547 doUnmap( mapping
->fAddressMap
, (IOVirtualAddress
) mapping
, 0 );
2551 if (kIOLogMapping
& gIOKitDebug
)
2552 IOLog("mapping(%x) desc %p @ %lx, map %p, address %qx, offset %qx, length %qx\n",
2553 err
, this, sourceAddr
, mapping
, address
, offset
, length
);
2556 if (err
== KERN_SUCCESS
)
2557 mapping
->fAddress
= address
+ pageOffset
;
2559 mapping
->fAddress
= NULL
;
2568 kIOMemoryRedirected
= 0x00010000
2571 IOReturn
IOMemoryDescriptor::handleFault(
2573 vm_map_t addressMap
,
2574 mach_vm_address_t address
,
2575 mach_vm_size_t sourceOffset
,
2576 mach_vm_size_t length
,
2577 IOOptionBits options
)
2579 IOReturn err
= kIOReturnSuccess
;
2580 memory_object_t pager
= (memory_object_t
) _pager
;
2581 mach_vm_size_t size
;
2582 mach_vm_size_t bytes
;
2583 mach_vm_size_t page
;
2584 mach_vm_size_t pageOffset
;
2585 mach_vm_size_t pagerOffset
;
2586 IOPhysicalLength segLen
;
2591 if( kIOMemoryRedirected
& _flags
)
2594 IOLog("sleep mem redirect %p, %qx\n", this, sourceOffset
);
2598 } while( kIOMemoryRedirected
& _flags
);
2601 return( kIOReturnSuccess
);
2604 physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
);
2606 pageOffset
= physAddr
- trunc_page_64( physAddr
);
2607 pagerOffset
= sourceOffset
;
2609 size
= length
+ pageOffset
;
2610 physAddr
-= pageOffset
;
2612 segLen
+= pageOffset
;
2616 // in the middle of the loop only map whole pages
2617 if( segLen
>= bytes
)
2619 else if( segLen
!= trunc_page_32( segLen
))
2620 err
= kIOReturnVMError
;
2621 if( physAddr
!= trunc_page_64( physAddr
))
2622 err
= kIOReturnBadArgument
;
2623 if (kIOReturnSuccess
!= err
)
2627 if( kIOLogMapping
& gIOKitDebug
)
2628 IOLog("_IOMemoryMap::map(%p) 0x%qx->0x%qx:0x%qx\n",
2629 addressMap
, address
+ pageOffset
, physAddr
+ pageOffset
,
2630 segLen
- pageOffset
);
2635 if( reserved
&& reserved
->pagerContig
) {
2636 IOPhysicalLength allLen
;
2639 allPhys
= getPhysicalSegment64( 0, &allLen
);
2641 err
= device_pager_populate_object( pager
, 0, atop_64(allPhys
), round_page_32(allLen
) );
2647 (page
< segLen
) && (KERN_SUCCESS
== err
);
2650 err
= device_pager_populate_object(pager
, pagerOffset
,
2651 (ppnum_t
)(atop_64(physAddr
+ page
)), page_size
);
2652 pagerOffset
+= page_size
;
2655 assert( KERN_SUCCESS
== err
);
2660 // This call to vm_fault causes an early pmap level resolution
2661 // of the mappings created above for kernel mappings, since
2662 // faulting in later can't take place from interrupt level.
2664 /* *** Temporary Workaround *** */
2666 if ((addressMap
== kernel_map
) && !(kIOMemoryRedirected
& _flags
))
2668 vm_fault(addressMap
,
2669 (vm_map_offset_t
)address
,
2670 VM_PROT_READ
|VM_PROT_WRITE
,
2671 FALSE
, THREAD_UNINT
, NULL
,
2672 (vm_map_offset_t
)0);
2675 /* *** Temporary Workaround *** */
2678 sourceOffset
+= segLen
- pageOffset
;
2684 while (bytes
&& (physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
)));
2687 err
= kIOReturnBadArgument
;
2692 IOReturn
IOMemoryDescriptor::doUnmap(
2693 vm_map_t addressMap
,
2694 IOVirtualAddress __address
,
2695 IOByteCount __length
)
2698 mach_vm_address_t address
;
2699 mach_vm_size_t length
;
2703 address
= __address
;
2708 addressMap
= ((_IOMemoryMap
*) __address
)->fAddressMap
;
2709 address
= ((_IOMemoryMap
*) __address
)->fAddress
;
2710 length
= ((_IOMemoryMap
*) __address
)->fLength
;
2713 if( _memEntry
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2714 addressMap
= IOPageableMapForAddress( address
);
2717 if( kIOLogMapping
& gIOKitDebug
)
2718 IOLog("IOMemoryDescriptor::doUnmap map %p, 0x%qx:0x%qx\n",
2719 addressMap
, address
, length
);
2722 err
= mach_vm_deallocate( addressMap
, address
, length
);
2727 IOReturn
IOMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2729 IOReturn err
= kIOReturnSuccess
;
2730 _IOMemoryMap
* mapping
= 0;
2736 _flags
|= kIOMemoryRedirected
;
2738 _flags
&= ~kIOMemoryRedirected
;
2741 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
2742 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject()))
2743 mapping
->redirect( safeTask
, doRedirect
);
2756 // temporary binary compatibility
2757 IOSubMemoryDescriptor
* subMem
;
2758 if( (subMem
= OSDynamicCast( IOSubMemoryDescriptor
, this)))
2759 err
= subMem
->redirect( safeTask
, doRedirect
);
2761 err
= kIOReturnSuccess
;
2766 IOReturn
IOSubMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2768 return( _parent
->redirect( safeTask
, doRedirect
));
2771 IOReturn
_IOMemoryMap::redirect( task_t safeTask
, bool doRedirect
)
2773 IOReturn err
= kIOReturnSuccess
;
2776 // err = ((_IOMemoryMap *)superMap)->redirect( safeTask, doRedirect );
2788 if ((!safeTask
|| (get_task_map(safeTask
) != fAddressMap
))
2789 && (0 == (fOptions
& kIOMapStatic
)))
2791 IOUnmapPages( fAddressMap
, fAddress
, fLength
);
2792 if(!doRedirect
&& safeTask
2793 && (((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2794 || ((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
)))
2796 IOVirtualAddress iova
= (IOVirtualAddress
) this;
2797 err
= mach_vm_deallocate( fAddressMap
, fAddress
, fLength
);
2798 err
= fMemory
->doMap( fAddressMap
, &iova
,
2799 (fOptions
& ~kIOMapAnywhere
) | kIOMap64Bit
/*| kIOMapReserve*/,
2802 err
= kIOReturnSuccess
;
2804 IOLog("IOMemoryMap::redirect(%d, %p) 0x%qx:0x%qx from %p\n", doRedirect
, this, fAddress
, fLength
, fAddressMap
);
2807 else if (kIOMapWriteCombineCache
== (fOptions
& kIOMapCacheMask
))
2809 IOOptionBits newMode
;
2810 newMode
= (fOptions
& ~kIOMapCacheMask
) | (doRedirect
? kIOMapInhibitCache
: kIOMapWriteCombineCache
);
2811 IOProtectCacheMode(fAddressMap
, fAddress
, fLength
, newMode
);
2818 if ((((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2819 || ((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
2821 && (doRedirect
!= (0 != (fMemory
->_flags
& kIOMemoryRedirected
))))
2822 fMemory
->redirect(safeTask
, doRedirect
);
2827 IOReturn
_IOMemoryMap::unmap( void )
2833 if( fAddress
&& fAddressMap
&& (0 == fSuperMap
) && fMemory
2834 && (0 == (fOptions
& kIOMapStatic
))) {
2836 err
= fMemory
->doUnmap(fAddressMap
, (IOVirtualAddress
) this, 0);
2839 err
= kIOReturnSuccess
;
2843 vm_map_deallocate(fAddressMap
);
2854 void _IOMemoryMap::taskDied( void )
2858 vm_map_deallocate(fAddressMap
);
2866 // Overload the release mechanism. All mappings must be a member
2867 // of a memory descriptors _mappings set. This means that we
2868 // always have 2 references on a mapping. When either of these mappings
2869 // are released we need to free ourselves.
2870 void _IOMemoryMap::taggedRelease(const void *tag
) const
2873 super::taggedRelease(tag
, 2);
2877 void _IOMemoryMap::free()
2884 fMemory
->removeMapping(this);
2889 if (fOwner
&& (fOwner
!= fMemory
))
2892 fOwner
->removeMapping(this);
2897 fSuperMap
->release();
2900 upl_commit(fRedirUPL
, NULL
, 0);
2901 upl_deallocate(fRedirUPL
);
2907 IOByteCount
_IOMemoryMap::getLength()
2912 IOVirtualAddress
_IOMemoryMap::getVirtualAddress()
2915 fSuperMap
->getVirtualAddress();
2916 else if (fAddressMap
&& vm_map_is_64bit(fAddressMap
))
2918 OSReportWithBacktrace("IOMemoryMap::getVirtualAddress(0x%qx) called on 64b map; use ::getAddress()", fAddress
);
2924 mach_vm_address_t
_IOMemoryMap::getAddress()
2929 mach_vm_size_t
_IOMemoryMap::getSize()
2935 task_t
_IOMemoryMap::getAddressTask()
2938 return( fSuperMap
->getAddressTask());
2940 return( fAddressTask
);
2943 IOOptionBits
_IOMemoryMap::getMapOptions()
2948 IOMemoryDescriptor
* _IOMemoryMap::getMemoryDescriptor()
2953 _IOMemoryMap
* _IOMemoryMap::copyCompatible(
2954 _IOMemoryMap
* newMapping
)
2956 task_t task
= newMapping
->getAddressTask();
2957 mach_vm_address_t toAddress
= newMapping
->fAddress
;
2958 IOOptionBits _options
= newMapping
->fOptions
;
2959 mach_vm_size_t _offset
= newMapping
->fOffset
;
2960 mach_vm_size_t _length
= newMapping
->fLength
;
2962 if( (!task
) || (!fAddressMap
) || (fAddressMap
!= get_task_map(task
)))
2964 if( (fOptions
^ _options
) & kIOMapReadOnly
)
2966 if( (kIOMapDefaultCache
!= (_options
& kIOMapCacheMask
))
2967 && ((fOptions
^ _options
) & kIOMapCacheMask
))
2970 if( (0 == (_options
& kIOMapAnywhere
)) && (fAddress
!= toAddress
))
2973 if( _offset
< fOffset
)
2978 if( (_offset
+ _length
) > fLength
)
2982 if( (fLength
== _length
) && (!_offset
))
2984 newMapping
->release();
2989 newMapping
->fSuperMap
= this;
2990 newMapping
->fOffset
= _offset
;
2991 newMapping
->fAddress
= fAddress
+ _offset
;
2994 return( newMapping
);
2998 _IOMemoryMap::getPhysicalSegment( IOByteCount _offset
, IOPhysicalLength
* _length
)
3000 IOPhysicalAddress address
;
3003 address
= fMemory
->getPhysicalSegment( fOffset
+ _offset
, _length
);
3009 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3012 #define super OSObject
3014 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3016 void IOMemoryDescriptor::initialize( void )
3018 if( 0 == gIOMemoryLock
)
3019 gIOMemoryLock
= IORecursiveLockAlloc();
3021 IORegistryEntry::getRegistryRoot()->setProperty(kIOMaximumMappedIOByteCountKey
,
3022 ptoa_64(gIOMaximumMappedIOPageCount
), 64);
3026 mapper
= new IOCopyMapper
;
3029 if (mapper
->init() && mapper
->start(NULL
))
3030 gIOCopyMapper
= (IOCopyMapper
*) mapper
;
3036 gIOLastPage
= IOGetLastPageNumber();
3039 void IOMemoryDescriptor::free( void )
3042 _mappings
->release();
3047 IOMemoryMap
* IOMemoryDescriptor::setMapping(
3049 IOVirtualAddress mapAddress
,
3050 IOOptionBits options
)
3052 return (createMappingInTask( intoTask
, mapAddress
,
3053 options
| kIOMapStatic
,
3057 IOMemoryMap
* IOMemoryDescriptor::map(
3058 IOOptionBits options
)
3060 return (createMappingInTask( kernel_task
, 0,
3061 options
| kIOMapAnywhere
,
3065 IOMemoryMap
* IOMemoryDescriptor::map(
3067 IOVirtualAddress atAddress
,
3068 IOOptionBits options
,
3070 IOByteCount length
)
3072 if ((!(kIOMapAnywhere
& options
)) && vm_map_is_64bit(get_task_map(intoTask
)))
3074 OSReportWithBacktrace("IOMemoryDescriptor::map() in 64b task, use ::createMappingInTask()");
3078 return (createMappingInTask(intoTask
, atAddress
,
3079 options
, offset
, length
));
3082 IOMemoryMap
* IOMemoryDescriptor::createMappingInTask(
3084 mach_vm_address_t atAddress
,
3085 IOOptionBits options
,
3086 mach_vm_size_t offset
,
3087 mach_vm_size_t length
)
3089 IOMemoryMap
* result
;
3090 _IOMemoryMap
* mapping
;
3093 length
= getLength();
3095 mapping
= new _IOMemoryMap
;
3098 && !mapping
->init( intoTask
, atAddress
,
3099 options
, offset
, length
)) {
3105 result
= makeMapping(this, intoTask
, (IOVirtualAddress
) mapping
, options
| kIOMap64Bit
, 0, 0);
3111 IOLog("createMappingInTask failed desc %p, addr %qx, options %lx, offset %qx, length %qx\n",
3112 this, atAddress
, options
, offset
, length
);
3118 IOReturn
_IOMemoryMap::redirect(IOMemoryDescriptor
* newBackingMemory
,
3119 IOOptionBits options
,
3122 return (redirect(newBackingMemory
, options
, (mach_vm_size_t
)offset
));
3125 IOReturn
_IOMemoryMap::redirect(IOMemoryDescriptor
* newBackingMemory
,
3126 IOOptionBits options
,
3127 mach_vm_size_t offset
)
3129 IOReturn err
= kIOReturnSuccess
;
3130 IOMemoryDescriptor
* physMem
= 0;
3134 if (fAddress
&& fAddressMap
) do
3136 if (((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
3137 || ((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
3145 vm_size_t size
= fLength
;
3146 int flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
3147 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
3148 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) fMemory
->_memEntry
, 0, &size
, &fRedirUPL
,
3155 IOUnmapPages( fAddressMap
, fAddress
, fLength
);
3156 physMem
->redirect(0, true);
3160 if (newBackingMemory
)
3162 if (newBackingMemory
!= fMemory
)
3165 if (this != newBackingMemory
->makeMapping(newBackingMemory
, fAddressTask
, (IOVirtualAddress
) this,
3166 options
| kIOMapUnique
| kIOMapReference
| kIOMap64Bit
,
3168 err
= kIOReturnError
;
3172 upl_commit(fRedirUPL
, NULL
, 0);
3173 upl_deallocate(fRedirUPL
);
3177 physMem
->redirect(0, false);
3190 IOMemoryMap
* IOMemoryDescriptor::makeMapping(
3191 IOMemoryDescriptor
* owner
,
3193 IOVirtualAddress __address
,
3194 IOOptionBits options
,
3195 IOByteCount __offset
,
3196 IOByteCount __length
)
3198 if (!(kIOMap64Bit
& options
)) panic("IOMemoryDescriptor::makeMapping !64bit");
3200 IOMemoryDescriptor
* mapDesc
= 0;
3201 _IOMemoryMap
* result
= 0;
3204 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) __address
;
3205 mach_vm_size_t offset
= mapping
->fOffset
+ __offset
;
3206 mach_vm_size_t length
= mapping
->fLength
;
3208 mapping
->fOffset
= offset
;
3214 if (kIOMapStatic
& options
)
3217 addMapping(mapping
);
3218 mapping
->setMemoryDescriptor(this, 0);
3222 if (kIOMapUnique
& options
)
3224 IOPhysicalAddress phys
;
3225 IOByteCount physLen
;
3227 // if (owner != this) continue;
3229 if (((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
3230 || ((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
3232 phys
= getPhysicalSegment(offset
, &physLen
);
3233 if (!phys
|| (physLen
< length
))
3236 mapDesc
= IOMemoryDescriptor::withPhysicalAddress(
3237 phys
, length
, _direction
);
3241 mapping
->fOffset
= offset
;
3246 // look for a compatible existing mapping
3247 if( (iter
= OSCollectionIterator::withCollection(_mappings
)))
3249 _IOMemoryMap
* lookMapping
;
3250 while ((lookMapping
= (_IOMemoryMap
*) iter
->getNextObject()))
3252 if ((result
= lookMapping
->copyCompatible(mapping
)))
3255 result
->setMemoryDescriptor(this, offset
);
3261 if (result
|| (options
& kIOMapReference
))
3271 kr
= mapDesc
->doMap( 0, (IOVirtualAddress
*) &mapping
, options
, 0, 0 );
3272 if (kIOReturnSuccess
== kr
)
3275 mapDesc
->addMapping(result
);
3276 result
->setMemoryDescriptor(mapDesc
, offset
);
3294 void IOMemoryDescriptor::addMapping(
3295 IOMemoryMap
* mapping
)
3300 _mappings
= OSSet::withCapacity(1);
3302 _mappings
->setObject( mapping
);
3306 void IOMemoryDescriptor::removeMapping(
3307 IOMemoryMap
* mapping
)
3310 _mappings
->removeObject( mapping
);
3313 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3316 #define super IOMemoryDescriptor
3318 OSDefineMetaClassAndStructors(IOSubMemoryDescriptor
, IOMemoryDescriptor
)
3320 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3322 bool IOSubMemoryDescriptor::initSubRange( IOMemoryDescriptor
* parent
,
3323 IOByteCount offset
, IOByteCount length
,
3324 IODirection direction
)
3329 if( (offset
+ length
) > parent
->getLength())
3333 * We can check the _parent instance variable before having ever set it
3334 * to an initial value because I/O Kit guarantees that all our instance
3335 * variables are zeroed on an object's allocation.
3343 * An existing memory descriptor is being retargeted to
3344 * point to somewhere else. Clean up our present state.
3355 _direction
= direction
;
3356 _tag
= parent
->getTag();
3361 void IOSubMemoryDescriptor::free( void )
3371 IOSubMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
3375 if (kIOMDGetCharacteristics
== op
) {
3377 rtn
= _parent
->dmaCommandOperation(op
, vData
, dataSize
);
3378 if (kIOReturnSuccess
== rtn
) {
3379 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
3380 data
->fLength
= _length
;
3381 data
->fSGCount
= 0; // XXX gvdl: need to compute and pages
3383 data
->fPageAlign
= 0;
3388 else if (kIOMDWalkSegments
& op
) {
3389 if (dataSize
< sizeof(IOMDDMAWalkSegmentArgs
))
3390 return kIOReturnUnderrun
;
3392 IOMDDMAWalkSegmentArgs
*data
=
3393 reinterpret_cast<IOMDDMAWalkSegmentArgs
*>(vData
);
3394 UInt offset
= data
->fOffset
;
3395 UInt remain
= _length
- offset
;
3396 if ((int) remain
<= 0)
3397 return (!remain
)? kIOReturnOverrun
: kIOReturnInternalError
;
3399 data
->fOffset
= offset
+ _start
;
3400 rtn
= _parent
->dmaCommandOperation(op
, vData
, dataSize
);
3401 if (data
->fLength
> remain
)
3402 data
->fLength
= remain
;
3403 data
->fOffset
= offset
;
3408 return kIOReturnBadArgument
;
3412 IOSubMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
* length
)
3415 IOByteCount actualLength
;
3417 assert(offset
<= _length
);
3422 if( offset
>= _length
)
3425 address
= _parent
->getPhysicalSegment64( offset
+ _start
, &actualLength
);
3427 if( address
&& length
)
3428 *length
= min( _length
- offset
, actualLength
);
3434 IOSubMemoryDescriptor::getPhysicalSegment( IOByteCount offset
, IOByteCount
* length
)
3436 IOPhysicalAddress address
;
3437 IOByteCount actualLength
;
3439 assert(offset
<= _length
);
3444 if( offset
>= _length
)
3447 address
= _parent
->getPhysicalSegment( offset
+ _start
, &actualLength
);
3449 if( address
&& length
)
3450 *length
= min( _length
- offset
, actualLength
);
3456 IOSubMemoryDescriptor::getSourceSegment( IOByteCount offset
, IOByteCount
* length
)
3458 IOPhysicalAddress address
;
3459 IOByteCount actualLength
;
3461 assert(offset
<= _length
);
3466 if( offset
>= _length
)
3469 address
= _parent
->getSourceSegment( offset
+ _start
, &actualLength
);
3471 if( address
&& length
)
3472 *length
= min( _length
- offset
, actualLength
);
3477 void * IOSubMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
3478 IOByteCount
* lengthOfSegment
)
3483 IOReturn
IOSubMemoryDescriptor::doMap(
3484 vm_map_t addressMap
,
3485 IOVirtualAddress
* atAddress
,
3486 IOOptionBits options
,
3487 IOByteCount sourceOffset
,
3488 IOByteCount length
)
3490 panic("IOSubMemoryDescriptor::doMap");
3491 return (IOMemoryDescriptor::doMap(addressMap
, atAddress
, options
, sourceOffset
, length
));
3494 IOByteCount
IOSubMemoryDescriptor::readBytes(IOByteCount offset
,
3495 void * bytes
, IOByteCount length
)
3497 IOByteCount byteCount
;
3499 assert(offset
<= _length
);
3501 if( offset
>= _length
)
3505 byteCount
= _parent
->readBytes( _start
+ offset
, bytes
,
3506 min(length
, _length
- offset
) );
3509 return( byteCount
);
3512 IOByteCount
IOSubMemoryDescriptor::writeBytes(IOByteCount offset
,
3513 const void* bytes
, IOByteCount length
)
3515 IOByteCount byteCount
;
3517 assert(offset
<= _length
);
3519 if( offset
>= _length
)
3523 byteCount
= _parent
->writeBytes( _start
+ offset
, bytes
,
3524 min(length
, _length
- offset
) );
3527 return( byteCount
);
3530 IOReturn
IOSubMemoryDescriptor::setPurgeable( IOOptionBits newState
,
3531 IOOptionBits
* oldState
)
3536 err
= _parent
->setPurgeable( newState
, oldState
);
3542 IOReturn
IOSubMemoryDescriptor::performOperation( IOOptionBits options
,
3543 IOByteCount offset
, IOByteCount length
)
3547 assert(offset
<= _length
);
3549 if( offset
>= _length
)
3550 return( kIOReturnOverrun
);
3553 err
= _parent
->performOperation( options
, _start
+ offset
,
3554 min(length
, _length
- offset
) );
3560 IOReturn
IOSubMemoryDescriptor::prepare(
3561 IODirection forDirection
)
3566 err
= _parent
->prepare( forDirection
);
3572 IOReturn
IOSubMemoryDescriptor::complete(
3573 IODirection forDirection
)
3578 err
= _parent
->complete( forDirection
);
3584 IOMemoryMap
* IOSubMemoryDescriptor::makeMapping(
3585 IOMemoryDescriptor
* owner
,
3587 IOVirtualAddress address
,
3588 IOOptionBits options
,
3590 IOByteCount length
)
3592 IOMemoryMap
* mapping
= 0;
3594 if (!(kIOMap64Bit
& options
))
3596 panic("IOSubMemoryDescriptor::makeMapping !64bit");
3599 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3603 options
, _start
+ offset
, length
);
3611 IOSubMemoryDescriptor::initWithAddress(void * address
,
3613 IODirection direction
)
3619 IOSubMemoryDescriptor::initWithAddress(vm_address_t address
,
3621 IODirection direction
,
3628 IOSubMemoryDescriptor::initWithPhysicalAddress(
3629 IOPhysicalAddress address
,
3631 IODirection direction
)
3637 IOSubMemoryDescriptor::initWithRanges(
3638 IOVirtualRange
* ranges
,
3640 IODirection direction
,
3648 IOSubMemoryDescriptor::initWithPhysicalRanges( IOPhysicalRange
* ranges
,
3650 IODirection direction
,
3656 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3658 bool IOGeneralMemoryDescriptor::serialize(OSSerialize
* s
) const
3660 OSSymbol
const *keys
[2];
3661 OSObject
*values
[2];
3663 user_addr_t address
;
3666 unsigned int index
, nRanges
;
3669 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
3671 if (s
== NULL
) return false;
3672 if (s
->previouslySerialized(this)) return true;
3674 // Pretend we are an array.
3675 if (!s
->addXMLStartTag(this, "array")) return false;
3677 nRanges
= _rangesCount
;
3678 vcopy
= (SerData
*) IOMalloc(sizeof(SerData
) * nRanges
);
3679 if (vcopy
== 0) return false;
3681 keys
[0] = OSSymbol::withCString("address");
3682 keys
[1] = OSSymbol::withCString("length");
3685 values
[0] = values
[1] = 0;
3687 // From this point on we can go to bail.
3689 // Copy the volatile data so we don't have to allocate memory
3690 // while the lock is held.
3692 if (nRanges
== _rangesCount
) {
3693 Ranges vec
= _ranges
;
3694 for (index
= 0; index
< nRanges
; index
++) {
3695 user_addr_t addr
; IOByteCount len
;
3696 getAddrLenForInd(addr
, len
, type
, vec
, index
);
3697 vcopy
[index
].address
= addr
;
3698 vcopy
[index
].length
= len
;
3701 // The descriptor changed out from under us. Give up.
3708 for (index
= 0; index
< nRanges
; index
++)
3710 user_addr_t addr
= vcopy
[index
].address
;
3711 IOByteCount len
= (IOByteCount
) vcopy
[index
].length
;
3713 OSNumber::withNumber(addr
, (((UInt64
) addr
) >> 32)? 64 : 32);
3714 if (values
[0] == 0) {
3718 values
[1] = OSNumber::withNumber(len
, sizeof(len
) * 8);
3719 if (values
[1] == 0) {
3723 OSDictionary
*dict
= OSDictionary::withObjects((const OSObject
**)values
, (const OSSymbol
**)keys
, 2);
3728 values
[0]->release();
3729 values
[1]->release();
3730 values
[0] = values
[1] = 0;
3732 result
= dict
->serialize(s
);
3738 result
= s
->addXMLEndTag("array");
3742 values
[0]->release();
3744 values
[1]->release();
3750 IOFree(vcopy
, sizeof(SerData
) * nRanges
);
3754 bool IOSubMemoryDescriptor::serialize(OSSerialize
* s
) const
3759 if (s
->previouslySerialized(this)) return true;
3761 // Pretend we are a dictionary.
3762 // We must duplicate the functionality of OSDictionary here
3763 // because otherwise object references will not work;
3764 // they are based on the value of the object passed to
3765 // previouslySerialized and addXMLStartTag.
3767 if (!s
->addXMLStartTag(this, "dict")) return false;
3769 char const *keys
[3] = {"offset", "length", "parent"};
3771 OSObject
*values
[3];
3772 values
[0] = OSNumber::withNumber(_start
, sizeof(_start
) * 8);
3775 values
[1] = OSNumber::withNumber(_length
, sizeof(_length
) * 8);
3776 if (values
[1] == 0) {
3777 values
[0]->release();
3780 values
[2] = _parent
;
3783 for (int i
=0; i
<3; i
++) {
3784 if (!s
->addString("<key>") ||
3785 !s
->addString(keys
[i
]) ||
3786 !s
->addXMLEndTag("key") ||
3787 !values
[i
]->serialize(s
)) {
3792 values
[0]->release();
3793 values
[1]->release();
3798 return s
->addXMLEndTag("dict");
3801 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3803 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 0);
3804 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 1);
3805 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 2);
3806 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 3);
3807 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 4);
3808 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 5);
3809 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 6);
3810 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 7);
3811 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 8);
3812 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 9);
3813 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 10);
3814 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 11);
3815 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 12);
3816 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 13);
3817 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 14);
3818 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 15);
3820 /* ex-inline function implementation */
3822 IOMemoryDescriptor::getPhysicalAddress()
3823 { return( getPhysicalSegment( 0, 0 )); }