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
);
1024 if (reserved
&& reserved
->devicePager
)
1025 device_pager_deallocate( (memory_object_t
) reserved
->devicePager
);
1027 // memEntry holds a ref on the device pager which owns reserved
1028 // (ExpansionData) so no reserved access after this point
1030 ipc_port_release_send( (ipc_port_t
) _memEntry
);
1033 IOLockFree(_prepareLock
);
1038 /* DEPRECATED */ void IOGeneralMemoryDescriptor::unmapFromKernel()
1040 panic("IOGMD::unmapFromKernel deprecated");
1043 /* DEPRECATED */ void IOGeneralMemoryDescriptor::mapIntoKernel(unsigned rangeIndex
)
1045 panic("IOGMD::mapIntoKernel deprecated");
1051 * Get the direction of the transfer.
1053 IODirection
IOMemoryDescriptor::getDirection() const
1061 * Get the length of the transfer (over all ranges).
1063 IOByteCount
IOMemoryDescriptor::getLength() const
1068 void IOMemoryDescriptor::setTag( IOOptionBits tag
)
1073 IOOptionBits
IOMemoryDescriptor::getTag( void )
1078 // @@@ gvdl: who is using this API? Seems like a wierd thing to implement.
1080 IOMemoryDescriptor::getSourceSegment( IOByteCount offset
, IOByteCount
* length
)
1082 addr64_t physAddr
= 0;
1084 if( prepare() == kIOReturnSuccess
) {
1085 physAddr
= getPhysicalSegment64( offset
, length
);
1089 return( (IOPhysicalAddress
) physAddr
); // truncated but only page offset is used
1092 IOByteCount
IOMemoryDescriptor::readBytes
1093 (IOByteCount offset
, void *bytes
, IOByteCount length
)
1095 addr64_t dstAddr
= (addr64_t
) (UInt32
) bytes
;
1096 IOByteCount remaining
;
1098 // Assert that this entire I/O is withing the available range
1099 assert(offset
< _length
);
1100 assert(offset
+ length
<= _length
);
1101 if (offset
>= _length
) {
1105 remaining
= length
= min(length
, _length
- offset
);
1106 while (remaining
) { // (process another target segment?)
1110 srcAddr64
= getPhysicalSegment64(offset
, &srcLen
);
1114 // Clip segment length to remaining
1115 if (srcLen
> remaining
)
1118 copypv(srcAddr64
, dstAddr
, srcLen
,
1119 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
1123 remaining
-= srcLen
;
1128 return length
- remaining
;
1131 IOByteCount
IOMemoryDescriptor::writeBytes
1132 (IOByteCount offset
, const void *bytes
, IOByteCount length
)
1134 addr64_t srcAddr
= (addr64_t
) (UInt32
) bytes
;
1135 IOByteCount remaining
;
1137 // Assert that this entire I/O is withing the available range
1138 assert(offset
< _length
);
1139 assert(offset
+ length
<= _length
);
1141 assert( !(kIOMemoryPreparedReadOnly
& _flags
) );
1143 if ( (kIOMemoryPreparedReadOnly
& _flags
) || offset
>= _length
) {
1147 remaining
= length
= min(length
, _length
- offset
);
1148 while (remaining
) { // (process another target segment?)
1152 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1156 // Clip segment length to remaining
1157 if (dstLen
> remaining
)
1160 copypv(srcAddr
, (addr64_t
) dstAddr64
, dstLen
,
1161 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1165 remaining
-= dstLen
;
1170 return length
- remaining
;
1173 // osfmk/device/iokit_rpc.c
1174 extern "C" unsigned int IODefaultCacheBits(addr64_t pa
);
1176 /* DEPRECATED */ void IOGeneralMemoryDescriptor::setPosition(IOByteCount position
)
1178 panic("IOGMD::setPosition deprecated");
1181 IOReturn
IOGeneralMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
1183 if (kIOMDGetCharacteristics
== op
) {
1185 if (dataSize
< sizeof(IOMDDMACharacteristics
))
1186 return kIOReturnUnderrun
;
1188 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
1189 data
->fLength
= _length
;
1190 data
->fSGCount
= _rangesCount
;
1191 data
->fPages
= _pages
;
1192 data
->fDirection
= _direction
;
1194 data
->fIsPrepared
= false;
1196 data
->fIsPrepared
= true;
1197 data
->fHighestPage
= _highestPage
;
1198 if (_memoryEntries
) {
1199 ioGMDData
*gmdData
= getDataP(_memoryEntries
);
1200 ioPLBlock
*ioplList
= getIOPLList(gmdData
);
1201 UInt count
= getNumIOPL(_memoryEntries
, gmdData
);
1203 data
->fIsMapped
= (gmdData
->fMapper
&& _pages
&& (count
> 0)
1204 && ioplList
[0].fMappedBase
);
1206 data
->fPageAlign
= (ioplList
[0].fPageOffset
& PAGE_MASK
) | ~PAGE_MASK
;
1209 data
->fIsMapped
= false;
1212 return kIOReturnSuccess
;
1214 else if (!(kIOMDWalkSegments
& op
))
1215 return kIOReturnBadArgument
;
1217 // Get the next segment
1218 struct InternalState
{
1219 IOMDDMAWalkSegmentArgs fIO
;
1225 // Find the next segment
1226 if (dataSize
< sizeof(*isP
))
1227 return kIOReturnUnderrun
;
1229 isP
= (InternalState
*) vData
;
1230 UInt offset
= isP
->fIO
.fOffset
;
1231 bool mapped
= isP
->fIO
.fMapped
;
1233 if (offset
>= _length
)
1234 return (offset
== _length
)? kIOReturnOverrun
: kIOReturnInternalError
;
1236 // Validate the previous offset
1237 UInt ind
, off2Ind
= isP
->fOffset2Index
;
1238 if ((kIOMDFirstSegment
!= op
)
1240 && (offset
== isP
->fNextOffset
|| off2Ind
<= offset
))
1243 ind
= off2Ind
= 0; // Start from beginning
1247 if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
) {
1249 // Physical address based memory descriptor
1250 const IOPhysicalRange
*physP
= (IOPhysicalRange
*) &_ranges
.p
[0];
1252 // Find the range after the one that contains the offset
1254 for (len
= 0; off2Ind
<= offset
; ind
++) {
1255 len
= physP
[ind
].length
;
1259 // Calculate length within range and starting address
1260 length
= off2Ind
- offset
;
1261 address
= physP
[ind
- 1].address
+ len
- length
;
1263 // see how far we can coalesce ranges
1264 while (ind
< _rangesCount
&& address
+ length
== physP
[ind
].address
) {
1265 len
= physP
[ind
].length
;
1271 // correct contiguous check overshoot
1275 else if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
) {
1277 // Physical address based memory descriptor
1278 const IOAddressRange
*physP
= (IOAddressRange
*) &_ranges
.v64
[0];
1280 // Find the range after the one that contains the offset
1282 for (len
= 0; off2Ind
<= offset
; ind
++) {
1283 len
= physP
[ind
].length
;
1287 // Calculate length within range and starting address
1288 length
= off2Ind
- offset
;
1289 address
= physP
[ind
- 1].address
+ len
- length
;
1291 // see how far we can coalesce ranges
1292 while (ind
< _rangesCount
&& address
+ length
== physP
[ind
].address
) {
1293 len
= physP
[ind
].length
;
1299 // correct contiguous check overshoot
1305 panic("IOGMD: not wired for the IODMACommand");
1307 assert(_memoryEntries
);
1309 ioGMDData
* dataP
= getDataP(_memoryEntries
);
1310 const ioPLBlock
*ioplList
= getIOPLList(dataP
);
1311 UInt numIOPLs
= getNumIOPL(_memoryEntries
, dataP
);
1312 upl_page_info_t
*pageList
= getPageList(dataP
);
1314 assert(numIOPLs
> 0);
1316 // Scan through iopl info blocks looking for block containing offset
1317 while (ind
< numIOPLs
&& offset
>= ioplList
[ind
].fIOMDOffset
)
1320 // Go back to actual range as search goes past it
1321 ioPLBlock ioplInfo
= ioplList
[ind
- 1];
1322 off2Ind
= ioplInfo
.fIOMDOffset
;
1325 length
= ioplList
[ind
].fIOMDOffset
;
1328 length
-= offset
; // Remainder within iopl
1330 // Subtract offset till this iopl in total list
1333 // If a mapped address is requested and this is a pre-mapped IOPL
1334 // then just need to compute an offset relative to the mapped base.
1335 if (mapped
&& ioplInfo
.fMappedBase
) {
1336 offset
+= (ioplInfo
.fPageOffset
& PAGE_MASK
);
1337 address
= ptoa_64(ioplInfo
.fMappedBase
) + offset
;
1338 continue; // Done leave do/while(false) now
1341 // The offset is rebased into the current iopl.
1342 // Now add the iopl 1st page offset.
1343 offset
+= ioplInfo
.fPageOffset
;
1345 // For external UPLs the fPageInfo field points directly to
1346 // the upl's upl_page_info_t array.
1347 if (ioplInfo
.fFlags
& kIOPLExternUPL
)
1348 pageList
= (upl_page_info_t
*) ioplInfo
.fPageInfo
;
1350 pageList
= &pageList
[ioplInfo
.fPageInfo
];
1352 // Check for direct device non-paged memory
1353 if ( ioplInfo
.fFlags
& kIOPLOnDevice
) {
1354 address
= ptoa_64(pageList
->phys_addr
) + offset
;
1355 continue; // Done leave do/while(false) now
1358 // Now we need compute the index into the pageList
1359 UInt pageInd
= atop_32(offset
);
1360 offset
&= PAGE_MASK
;
1362 // Compute the starting address of this segment
1363 IOPhysicalAddress pageAddr
= pageList
[pageInd
].phys_addr
;
1364 address
= ptoa_64(pageAddr
) + offset
;
1366 // length is currently set to the length of the remainider of the iopl.
1367 // We need to check that the remainder of the iopl is contiguous.
1368 // This is indicated by pageList[ind].phys_addr being sequential.
1369 IOByteCount contigLength
= PAGE_SIZE
- offset
;
1370 while (contigLength
< length
1371 && ++pageAddr
== pageList
[++pageInd
].phys_addr
)
1373 contigLength
+= PAGE_SIZE
;
1376 if (contigLength
< length
)
1377 length
= contigLength
;
1385 // Update return values and state
1386 isP
->fIO
.fIOVMAddr
= address
;
1387 isP
->fIO
.fLength
= length
;
1389 isP
->fOffset2Index
= off2Ind
;
1390 isP
->fNextOffset
= isP
->fIO
.fOffset
+ length
;
1392 return kIOReturnSuccess
;
1396 IOGeneralMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1399 IOByteCount length
= 0;
1400 addr64_t address
= 0;
1402 if (gIOSystemMapper
&& (kIOMemoryTypePhysical
== (_flags
& kIOMemoryTypeMask
)))
1403 return (super::getPhysicalSegment64(offset
, lengthOfSegment
));
1405 if (offset
< _length
) // (within bounds?)
1407 IOMDDMAWalkSegmentState _state
;
1408 IOMDDMAWalkSegmentArgs
* state
= (IOMDDMAWalkSegmentArgs
*) &_state
;
1410 state
->fOffset
= offset
;
1411 state
->fLength
= _length
- offset
;
1412 state
->fMapped
= false;
1414 ret
= dmaCommandOperation(kIOMDFirstSegment
, _state
, sizeof(_state
));
1416 if ((kIOReturnSuccess
!= ret
) && (kIOReturnOverrun
!= ret
))
1417 DEBG("getPhysicalSegment64 dmaCommandOperation(%lx), %p, offset %qx, addr %qx, len %qx\n",
1418 ret
, this, state
->fOffset
,
1419 state
->fIOVMAddr
, state
->fLength
);
1420 if (kIOReturnSuccess
== ret
)
1422 address
= state
->fIOVMAddr
;
1423 length
= state
->fLength
;
1429 if (lengthOfSegment
)
1430 *lengthOfSegment
= length
;
1436 IOGeneralMemoryDescriptor::getPhysicalSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1439 IOByteCount length
= 0;
1440 addr64_t address
= 0;
1442 // assert(offset <= _length);
1444 if (offset
< _length
) // (within bounds?)
1446 IOMDDMAWalkSegmentState _state
;
1447 IOMDDMAWalkSegmentArgs
* state
= (IOMDDMAWalkSegmentArgs
*) &_state
;
1449 state
->fOffset
= offset
;
1450 state
->fLength
= _length
- offset
;
1451 state
->fMapped
= true;
1453 ret
= dmaCommandOperation(
1454 kIOMDFirstSegment
, _state
, sizeof(_state
));
1456 if ((kIOReturnSuccess
!= ret
) && (kIOReturnOverrun
!= ret
))
1457 DEBG("getPhysicalSegment dmaCommandOperation(%lx), %p, offset %qx, addr %qx, len %qx\n",
1458 ret
, this, state
->fOffset
,
1459 state
->fIOVMAddr
, state
->fLength
);
1460 if (kIOReturnSuccess
== ret
)
1462 address
= state
->fIOVMAddr
;
1463 length
= state
->fLength
;
1470 if ((address
+ length
) > 0x100000000ULL
)
1472 panic("getPhysicalSegment() out of 32b range 0x%qx, len 0x%lx, class %s",
1473 address
, length
, (getMetaClass())->getClassName());
1476 if (lengthOfSegment
)
1477 *lengthOfSegment
= length
;
1479 return ((IOPhysicalAddress
) address
);
1483 IOMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1485 IOPhysicalAddress phys32
;
1488 IOMapper
* mapper
= 0;
1490 phys32
= getPhysicalSegment(offset
, lengthOfSegment
);
1494 if (gIOSystemMapper
)
1495 mapper
= gIOSystemMapper
;
1499 IOByteCount origLen
;
1501 phys64
= mapper
->mapAddr(phys32
);
1502 origLen
= *lengthOfSegment
;
1503 length
= page_size
- (phys64
& (page_size
- 1));
1504 while ((length
< origLen
)
1505 && ((phys64
+ length
) == mapper
->mapAddr(phys32
+ length
)))
1506 length
+= page_size
;
1507 if (length
> origLen
)
1510 *lengthOfSegment
= length
;
1513 phys64
= (addr64_t
) phys32
;
1519 IOGeneralMemoryDescriptor::getSourceSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1521 IOPhysicalAddress address
= 0;
1522 IOPhysicalLength length
= 0;
1523 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1525 assert(offset
<= _length
);
1527 if ( type
== kIOMemoryTypeUPL
)
1528 return super::getSourceSegment( offset
, lengthOfSegment
);
1529 else if ( offset
< _length
) // (within bounds?)
1531 unsigned rangesIndex
= 0;
1532 Ranges vec
= _ranges
;
1535 // Find starting address within the vector of ranges
1537 getAddrLenForInd(addr
, length
, type
, vec
, rangesIndex
);
1538 if (offset
< length
)
1540 offset
-= length
; // (make offset relative)
1544 // Now that we have the starting range,
1545 // lets find the last contiguous range
1549 for ( ++rangesIndex
; rangesIndex
< _rangesCount
; rangesIndex
++ ) {
1550 user_addr_t newAddr
;
1551 IOPhysicalLength newLen
;
1553 getAddrLenForInd(newAddr
, newLen
, type
, vec
, rangesIndex
);
1554 if (addr
+ length
!= newAddr
)
1559 address
= (IOPhysicalAddress
) addr
; // Truncate address to 32bit
1564 if ( lengthOfSegment
) *lengthOfSegment
= length
;
1569 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1570 /* DEPRECATED */ void * IOGeneralMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
1571 /* DEPRECATED */ IOByteCount
* lengthOfSegment
)
1573 if (_task
== kernel_task
)
1574 return (void *) getSourceSegment(offset
, lengthOfSegment
);
1576 panic("IOGMD::getVirtualSegment deprecated");
1580 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1585 IOMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
1587 if (kIOMDGetCharacteristics
== op
) {
1588 if (dataSize
< sizeof(IOMDDMACharacteristics
))
1589 return kIOReturnUnderrun
;
1591 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
1592 data
->fLength
= getLength();
1594 data
->fDirection
= _direction
;
1595 if (IOMapper::gSystem
)
1596 data
->fIsMapped
= true;
1597 data
->fIsPrepared
= true; // Assume prepared - fails safe
1599 else if (kIOMDWalkSegments
& op
) {
1600 if (dataSize
< sizeof(IOMDDMAWalkSegmentArgs
))
1601 return kIOReturnUnderrun
;
1603 IOMDDMAWalkSegmentArgs
*data
= (IOMDDMAWalkSegmentArgs
*) vData
;
1604 IOByteCount offset
= (IOByteCount
) data
->fOffset
;
1606 IOPhysicalLength length
;
1607 IOMemoryDescriptor
*ncmd
= const_cast<IOMemoryDescriptor
*>(this);
1608 if (data
->fMapped
&& IOMapper::gSystem
)
1609 data
->fIOVMAddr
= ncmd
->getPhysicalSegment(offset
, &length
);
1611 data
->fIOVMAddr
= ncmd
->getPhysicalSegment64(offset
, &length
);
1612 data
->fLength
= length
;
1615 return kIOReturnBadArgument
;
1617 return kIOReturnSuccess
;
1620 IOReturn
IOMemoryDescriptor::setPurgeable( IOOptionBits newState
,
1621 IOOptionBits
* oldState
)
1623 IOReturn err
= kIOReturnSuccess
;
1624 vm_purgable_t control
;
1631 err
= kIOReturnNotReady
;
1635 control
= VM_PURGABLE_SET_STATE
;
1638 case kIOMemoryPurgeableKeepCurrent
:
1639 control
= VM_PURGABLE_GET_STATE
;
1642 case kIOMemoryPurgeableNonVolatile
:
1643 state
= VM_PURGABLE_NONVOLATILE
;
1645 case kIOMemoryPurgeableVolatile
:
1646 state
= VM_PURGABLE_VOLATILE
;
1648 case kIOMemoryPurgeableEmpty
:
1649 state
= VM_PURGABLE_EMPTY
;
1652 err
= kIOReturnBadArgument
;
1656 if (kIOReturnSuccess
!= err
)
1659 err
= mach_memory_entry_purgable_control((ipc_port_t
) _memEntry
, control
, &state
);
1663 if (kIOReturnSuccess
== err
)
1667 case VM_PURGABLE_NONVOLATILE
:
1668 state
= kIOMemoryPurgeableNonVolatile
;
1670 case VM_PURGABLE_VOLATILE
:
1671 state
= kIOMemoryPurgeableVolatile
;
1673 case VM_PURGABLE_EMPTY
:
1674 state
= kIOMemoryPurgeableEmpty
;
1677 state
= kIOMemoryPurgeableNonVolatile
;
1678 err
= kIOReturnNotReady
;
1690 extern "C" void dcache_incoherent_io_flush64(addr64_t pa
, unsigned int count
);
1691 extern "C" void dcache_incoherent_io_store64(addr64_t pa
, unsigned int count
);
1693 IOReturn
IOMemoryDescriptor::performOperation( IOOptionBits options
,
1694 IOByteCount offset
, IOByteCount length
)
1696 IOByteCount remaining
;
1697 void (*func
)(addr64_t pa
, unsigned int count
) = 0;
1701 case kIOMemoryIncoherentIOFlush
:
1702 func
= &dcache_incoherent_io_flush64
;
1704 case kIOMemoryIncoherentIOStore
:
1705 func
= &dcache_incoherent_io_store64
;
1710 return (kIOReturnUnsupported
);
1712 remaining
= length
= min(length
, getLength() - offset
);
1714 // (process another target segment?)
1719 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1723 // Clip segment length to remaining
1724 if (dstLen
> remaining
)
1727 (*func
)(dstAddr64
, dstLen
);
1730 remaining
-= dstLen
;
1733 return (remaining
? kIOReturnUnderrun
: kIOReturnSuccess
);
1736 #if defined(__ppc__) || defined(__arm__)
1737 extern vm_offset_t static_memory_end
;
1738 #define io_kernel_static_end static_memory_end
1740 extern vm_offset_t first_avail
;
1741 #define io_kernel_static_end first_avail
1744 static kern_return_t
1745 io_get_kernel_static_upl(
1747 vm_address_t offset
,
1748 vm_size_t
*upl_size
,
1750 upl_page_info_array_t page_list
,
1751 unsigned int *count
,
1752 ppnum_t
*highest_page
)
1754 unsigned int pageCount
, page
;
1756 ppnum_t highestPage
= 0;
1758 pageCount
= atop_32(*upl_size
);
1759 if (pageCount
> *count
)
1764 for (page
= 0; page
< pageCount
; page
++)
1766 phys
= pmap_find_phys(kernel_pmap
, ((addr64_t
)offset
) + ptoa_64(page
));
1769 page_list
[page
].phys_addr
= phys
;
1770 page_list
[page
].pageout
= 0;
1771 page_list
[page
].absent
= 0;
1772 page_list
[page
].dirty
= 0;
1773 page_list
[page
].precious
= 0;
1774 page_list
[page
].device
= 0;
1775 if (phys
> highestPage
)
1779 *highest_page
= highestPage
;
1781 return ((page
>= pageCount
) ? kIOReturnSuccess
: kIOReturnVMError
);
1784 IOReturn
IOGeneralMemoryDescriptor::wireVirtual(IODirection forDirection
)
1786 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1787 IOReturn error
= kIOReturnCannotWire
;
1789 ppnum_t mapBase
= 0;
1791 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
1793 assert(!_wireCount
);
1794 assert(kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
);
1796 if (_pages
>= gIOMaximumMappedIOPageCount
)
1797 return kIOReturnNoResources
;
1799 dataP
= getDataP(_memoryEntries
);
1800 mapper
= dataP
->fMapper
;
1801 if (mapper
&& _pages
)
1802 mapBase
= mapper
->iovmAlloc(_pages
);
1804 // Note that appendBytes(NULL) zeros the data up to the
1806 _memoryEntries
->appendBytes(0, dataP
->fPageCnt
* sizeof(upl_page_info_t
));
1807 dataP
= 0; // May no longer be valid so lets not get tempted.
1809 if (forDirection
== kIODirectionNone
)
1810 forDirection
= _direction
;
1812 int uplFlags
; // This Mem Desc's default flags for upl creation
1813 switch (kIODirectionOutIn
& forDirection
)
1815 case kIODirectionOut
:
1816 // Pages do not need to be marked as dirty on commit
1817 uplFlags
= UPL_COPYOUT_FROM
;
1818 _flags
|= kIOMemoryPreparedReadOnly
;
1821 case kIODirectionIn
:
1823 uplFlags
= 0; // i.e. ~UPL_COPYOUT_FROM
1826 uplFlags
|= UPL_SET_IO_WIRE
| UPL_SET_LITE
;
1828 #ifdef UPL_NEED_32BIT_ADDR
1829 if (kIODirectionPrepareToPhys32
& forDirection
)
1830 uplFlags
|= UPL_NEED_32BIT_ADDR
;
1833 // Find the appropriate vm_map for the given task
1835 if (_task
== kernel_task
&& (kIOMemoryBufferPageable
& _flags
))
1838 { curMap
= get_task_map(_task
); }
1840 // Iterate over the vector of virtual ranges
1841 Ranges vec
= _ranges
;
1842 unsigned int pageIndex
= 0;
1843 IOByteCount mdOffset
= 0;
1844 ppnum_t highestPage
= 0;
1845 for (UInt range
= 0; range
< _rangesCount
; range
++) {
1847 user_addr_t startPage
;
1848 IOByteCount numBytes
;
1849 ppnum_t highPage
= 0;
1851 // Get the startPage address and length of vec[range]
1852 getAddrLenForInd(startPage
, numBytes
, type
, vec
, range
);
1853 iopl
.fPageOffset
= (short) startPage
& PAGE_MASK
;
1854 numBytes
+= iopl
.fPageOffset
;
1855 startPage
= trunc_page_64(startPage
);
1858 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1860 iopl
.fMappedBase
= 0;
1862 // Iterate over the current range, creating UPLs
1864 dataP
= getDataP(_memoryEntries
);
1865 vm_address_t kernelStart
= (vm_address_t
) startPage
;
1869 else if (!sharedMem
) {
1870 assert(_task
== kernel_task
);
1871 theMap
= IOPageableMapForAddress(kernelStart
);
1876 upl_page_info_array_t pageInfo
= getPageList(dataP
);
1877 int ioplFlags
= uplFlags
;
1878 upl_page_list_ptr_t baseInfo
= &pageInfo
[pageIndex
];
1880 vm_size_t ioplSize
= round_page_32(numBytes
);
1881 unsigned int numPageInfo
= atop_32(ioplSize
);
1883 if (theMap
== kernel_map
&& kernelStart
< io_kernel_static_end
) {
1884 error
= io_get_kernel_static_upl(theMap
,
1892 else if (sharedMem
) {
1893 error
= memory_object_iopl_request(sharedMem
,
1903 error
= vm_map_create_upl(theMap
,
1913 if (error
!= KERN_SUCCESS
)
1917 highPage
= upl_get_highest_page(iopl
.fIOPL
);
1918 if (highPage
> highestPage
)
1919 highestPage
= highPage
;
1921 error
= kIOReturnCannotWire
;
1923 if (baseInfo
->device
) {
1925 iopl
.fFlags
= kIOPLOnDevice
;
1926 // Don't translate device memory at all
1927 if (mapper
&& mapBase
) {
1928 mapper
->iovmFree(mapBase
, _pages
);
1930 iopl
.fMappedBase
= 0;
1936 mapper
->iovmInsert(mapBase
, pageIndex
,
1937 baseInfo
, numPageInfo
);
1940 iopl
.fIOMDOffset
= mdOffset
;
1941 iopl
.fPageInfo
= pageIndex
;
1943 if ((_flags
& kIOMemoryAutoPrepare
) && iopl
.fIOPL
)
1945 upl_commit(iopl
.fIOPL
, 0, 0);
1946 upl_deallocate(iopl
.fIOPL
);
1950 if (!_memoryEntries
->appendBytes(&iopl
, sizeof(iopl
))) {
1951 // Clean up partial created and unsaved iopl
1953 upl_abort(iopl
.fIOPL
, 0);
1954 upl_deallocate(iopl
.fIOPL
);
1959 // Check for a multiple iopl's in one virtual range
1960 pageIndex
+= numPageInfo
;
1961 mdOffset
-= iopl
.fPageOffset
;
1962 if (ioplSize
< numBytes
) {
1963 numBytes
-= ioplSize
;
1964 startPage
+= ioplSize
;
1965 mdOffset
+= ioplSize
;
1966 iopl
.fPageOffset
= 0;
1968 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1971 mdOffset
+= numBytes
;
1977 _highestPage
= highestPage
;
1979 return kIOReturnSuccess
;
1983 dataP
= getDataP(_memoryEntries
);
1984 UInt done
= getNumIOPL(_memoryEntries
, dataP
);
1985 ioPLBlock
*ioplList
= getIOPLList(dataP
);
1987 for (UInt range
= 0; range
< done
; range
++)
1989 if (ioplList
[range
].fIOPL
) {
1990 upl_abort(ioplList
[range
].fIOPL
, 0);
1991 upl_deallocate(ioplList
[range
].fIOPL
);
1994 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
1996 if (mapper
&& mapBase
)
1997 mapper
->iovmFree(mapBase
, _pages
);
2000 if (error
== KERN_FAILURE
)
2001 error
= kIOReturnCannotWire
;
2009 * Prepare the memory for an I/O transfer. This involves paging in
2010 * the memory, if necessary, and wiring it down for the duration of
2011 * the transfer. The complete() method completes the processing of
2012 * the memory after the I/O transfer finishes. This method needn't
2013 * called for non-pageable memory.
2015 IOReturn
IOGeneralMemoryDescriptor::prepare(IODirection forDirection
)
2017 IOReturn error
= kIOReturnSuccess
;
2018 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2020 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
2021 return kIOReturnSuccess
;
2024 IOLockLock(_prepareLock
);
2027 && (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
) ) {
2028 error
= wireVirtual(forDirection
);
2031 if (kIOReturnSuccess
== error
)
2035 IOLockUnlock(_prepareLock
);
2043 * Complete processing of the memory after an I/O transfer finishes.
2044 * This method should not be called unless a prepare was previously
2045 * issued; the prepare() and complete() must occur in pairs, before
2046 * before and after an I/O transfer involving pageable memory.
2049 IOReturn
IOGeneralMemoryDescriptor::complete(IODirection
/* forDirection */)
2051 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2053 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
2054 return kIOReturnSuccess
;
2057 IOLockLock(_prepareLock
);
2066 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2067 ioGMDData
* dataP
= getDataP(_memoryEntries
);
2068 ioPLBlock
*ioplList
= getIOPLList(dataP
);
2069 UInt count
= getNumIOPL(_memoryEntries
, dataP
);
2071 if (dataP
->fMapper
&& _pages
&& ioplList
[0].fMappedBase
)
2072 dataP
->fMapper
->iovmFree(ioplList
[0].fMappedBase
, _pages
);
2074 // Only complete iopls that we created which are for TypeVirtual
2075 if (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
) {
2076 for (UInt ind
= 0; ind
< count
; ind
++)
2077 if (ioplList
[ind
].fIOPL
) {
2078 upl_commit(ioplList
[ind
].fIOPL
, 0, 0);
2079 upl_deallocate(ioplList
[ind
].fIOPL
);
2082 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
2087 IOLockUnlock(_prepareLock
);
2089 return kIOReturnSuccess
;
2092 IOReturn
IOGeneralMemoryDescriptor::doMap(
2093 vm_map_t __addressMap
,
2094 IOVirtualAddress
* __address
,
2095 IOOptionBits options
,
2096 IOByteCount __offset
,
2097 IOByteCount __length
)
2100 if (!(kIOMap64Bit
& options
)) panic("IOGeneralMemoryDescriptor::doMap !64bit");
2102 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) *__address
;
2103 mach_vm_size_t offset
= mapping
->fOffset
+ __offset
;
2104 mach_vm_size_t length
= mapping
->fLength
;
2107 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
2109 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2110 Ranges vec
= _ranges
;
2112 user_addr_t range0Addr
= 0;
2113 IOByteCount range0Len
= 0;
2116 getAddrLenForInd(range0Addr
, range0Len
, type
, vec
, 0);
2118 // mapping source == dest? (could be much better)
2120 && (mapping
->fAddressMap
== get_task_map(_task
)) && (options
& kIOMapAnywhere
)
2121 && (1 == _rangesCount
) && (0 == offset
)
2122 && range0Addr
&& (length
<= range0Len
) )
2124 mapping
->fAddress
= range0Addr
;
2125 mapping
->fOptions
|= kIOMapStatic
;
2127 return( kIOReturnSuccess
);
2130 if( 0 == sharedMem
) {
2132 vm_size_t size
= ptoa_32(_pages
);
2136 memory_object_size_t actualSize
= size
;
2137 vm_prot_t prot
= VM_PROT_READ
;
2138 if (!(kIOMapReadOnly
& options
))
2139 prot
|= VM_PROT_WRITE
;
2140 else if (kIOMapDefaultCache
!= (options
& kIOMapCacheMask
))
2141 prot
|= VM_PROT_WRITE
;
2143 kr
= mach_make_memory_entry_64(get_task_map(_task
),
2144 &actualSize
, range0Addr
,
2148 if( (KERN_SUCCESS
== kr
) && (actualSize
!= round_page_32(size
))) {
2150 IOLog("mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
2151 range0Addr
, (UInt32
) actualSize
, size
);
2153 kr
= kIOReturnVMError
;
2154 ipc_port_release_send( sharedMem
);
2157 if( KERN_SUCCESS
!= kr
)
2158 sharedMem
= MACH_PORT_NULL
;
2160 } else do { // _task == 0, must be physical
2162 memory_object_t pager
;
2163 unsigned int flags
= 0;
2165 IOPhysicalLength segLen
;
2167 pa
= getPhysicalSegment64( offset
, &segLen
);
2170 reserved
= IONew( ExpansionData
, 1 );
2174 reserved
->pagerContig
= (1 == _rangesCount
);
2175 reserved
->memory
= this;
2177 /*What cache mode do we need*/
2178 switch(options
& kIOMapCacheMask
) {
2180 case kIOMapDefaultCache
:
2182 flags
= IODefaultCacheBits(pa
);
2183 if (DEVICE_PAGER_CACHE_INHIB
& flags
)
2185 if (DEVICE_PAGER_GUARDED
& flags
)
2186 mapping
->fOptions
|= kIOMapInhibitCache
;
2188 mapping
->fOptions
|= kIOMapWriteCombineCache
;
2190 else if (DEVICE_PAGER_WRITE_THROUGH
& flags
)
2191 mapping
->fOptions
|= kIOMapWriteThruCache
;
2193 mapping
->fOptions
|= kIOMapCopybackCache
;
2196 case kIOMapInhibitCache
:
2197 flags
= DEVICE_PAGER_CACHE_INHIB
|
2198 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
2201 case kIOMapWriteThruCache
:
2202 flags
= DEVICE_PAGER_WRITE_THROUGH
|
2203 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
2206 case kIOMapCopybackCache
:
2207 flags
= DEVICE_PAGER_COHERENT
;
2210 case kIOMapWriteCombineCache
:
2211 flags
= DEVICE_PAGER_CACHE_INHIB
|
2212 DEVICE_PAGER_COHERENT
;
2216 flags
|= reserved
->pagerContig
? DEVICE_PAGER_CONTIGUOUS
: 0;
2218 pager
= device_pager_setup( (memory_object_t
) 0, (int) reserved
,
2223 kr
= mach_memory_object_memory_entry_64( (host_t
) 1, false /*internal*/,
2224 size
, VM_PROT_READ
| VM_PROT_WRITE
, pager
, &sharedMem
);
2226 assert( KERN_SUCCESS
== kr
);
2227 if( KERN_SUCCESS
!= kr
)
2229 device_pager_deallocate( pager
);
2230 pager
= MACH_PORT_NULL
;
2231 sharedMem
= MACH_PORT_NULL
;
2234 if( pager
&& sharedMem
)
2235 reserved
->devicePager
= pager
;
2237 IODelete( reserved
, ExpansionData
, 1 );
2243 _memEntry
= (void *) sharedMem
;
2248 result
= kIOReturnVMError
;
2250 result
= super::doMap( __addressMap
, __address
,
2251 options
, __offset
, __length
);
2256 IOReturn
IOGeneralMemoryDescriptor::doUnmap(
2257 vm_map_t addressMap
,
2258 IOVirtualAddress __address
,
2259 IOByteCount __length
)
2261 return (super::doUnmap(addressMap
, __address
, __length
));
2264 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2266 OSDefineMetaClassAndAbstractStructors( IOMemoryMap
, OSObject
)
2268 /* inline function implementation */
2269 IOPhysicalAddress
IOMemoryMap::getPhysicalAddress()
2270 { return( getPhysicalSegment( 0, 0 )); }
2274 #define super IOMemoryMap
2276 OSDefineMetaClassAndStructors(_IOMemoryMap
, IOMemoryMap
)
2278 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2280 bool _IOMemoryMap::init(
2282 mach_vm_address_t toAddress
,
2283 IOOptionBits _options
,
2284 mach_vm_size_t _offset
,
2285 mach_vm_size_t _length
)
2293 fAddressMap
= get_task_map(intoTask
);
2296 vm_map_reference(fAddressMap
);
2298 fAddressTask
= intoTask
;
2299 fOptions
= _options
;
2302 fAddress
= toAddress
;
2307 bool _IOMemoryMap::setMemoryDescriptor(IOMemoryDescriptor
* _memory
, mach_vm_size_t _offset
)
2314 if( (_offset
+ fLength
) > _memory
->getLength())
2322 if (fMemory
!= _memory
)
2323 fMemory
->removeMapping(this);
2331 struct IOMemoryDescriptorMapAllocRef
2333 ipc_port_t sharedMem
;
2334 mach_vm_address_t mapped
;
2335 mach_vm_size_t size
;
2336 mach_vm_size_t sourceOffset
;
2337 IOOptionBits options
;
2340 static kern_return_t
IOMemoryDescriptorMapAlloc(vm_map_t map
, void * _ref
)
2342 IOMemoryDescriptorMapAllocRef
* ref
= (IOMemoryDescriptorMapAllocRef
*)_ref
;
2348 vm_prot_t prot
= VM_PROT_READ
2349 | ((ref
->options
& kIOMapReadOnly
) ? 0 : VM_PROT_WRITE
);
2351 // VM system requires write access to change cache mode
2352 if (kIOMapDefaultCache
!= (ref
->options
& kIOMapCacheMask
))
2353 prot
|= VM_PROT_WRITE
;
2355 // set memory entry cache
2356 vm_prot_t memEntryCacheMode
= prot
| MAP_MEM_ONLY
;
2357 switch (ref
->options
& kIOMapCacheMask
)
2359 case kIOMapInhibitCache
:
2360 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
2363 case kIOMapWriteThruCache
:
2364 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
2367 case kIOMapWriteCombineCache
:
2368 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
2371 case kIOMapCopybackCache
:
2372 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
2375 case kIOMapDefaultCache
:
2377 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
2381 vm_size_t unused
= 0;
2383 err
= mach_make_memory_entry( NULL
/*unused*/, &unused
, 0 /*unused*/,
2384 memEntryCacheMode
, NULL
, ref
->sharedMem
);
2385 if (KERN_SUCCESS
!= err
)
2386 IOLog("MAP_MEM_ONLY failed %d\n", err
);
2388 err
= mach_vm_map( map
,
2390 ref
->size
, 0 /* mask */,
2391 (( ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2392 | VM_MAKE_TAG(VM_MEMORY_IOKIT
),
2393 ref
->sharedMem
, ref
->sourceOffset
,
2399 if( KERN_SUCCESS
!= err
) {
2407 err
= mach_vm_allocate( map
, &ref
->mapped
, ref
->size
,
2408 ((ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2409 | VM_MAKE_TAG(VM_MEMORY_IOKIT
) );
2410 if( KERN_SUCCESS
!= err
) {
2414 // we have to make sure that these guys don't get copied if we fork.
2415 err
= vm_inherit( map
, ref
->mapped
, ref
->size
, VM_INHERIT_NONE
);
2416 assert( KERN_SUCCESS
== err
);
2425 IOMemoryDescriptorMapMemEntry(vm_map_t map
, ipc_port_t entry
, IOOptionBits options
, bool pageable
,
2426 mach_vm_size_t offset
,
2427 mach_vm_address_t
* address
, mach_vm_size_t length
)
2430 IOMemoryDescriptorMapAllocRef ref
;
2432 ref
.sharedMem
= entry
;
2433 ref
.sourceOffset
= trunc_page_64(offset
);
2434 ref
.options
= options
;
2438 if (options
& kIOMapAnywhere
)
2439 // vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
2442 ref
.mapped
= *address
;
2444 if( ref
.sharedMem
&& (map
== kernel_map
) && pageable
)
2445 err
= IOIteratePageableMaps( ref
.size
, &IOMemoryDescriptorMapAlloc
, &ref
);
2447 err
= IOMemoryDescriptorMapAlloc( map
, &ref
);
2449 *address
= ref
.mapped
;
2454 IOReturn
IOMemoryDescriptor::doMap(
2455 vm_map_t __addressMap
,
2456 IOVirtualAddress
* __address
,
2457 IOOptionBits options
,
2458 IOByteCount __offset
,
2459 IOByteCount __length
)
2461 if (!(kIOMap64Bit
& options
)) panic("IOMemoryDescriptor::doMap !64bit");
2463 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) *__address
;
2464 mach_vm_size_t offset
= mapping
->fOffset
+ __offset
;
2465 mach_vm_size_t length
= mapping
->fLength
;
2467 IOReturn err
= kIOReturnSuccess
;
2468 memory_object_t pager
;
2469 mach_vm_size_t pageOffset
;
2470 IOPhysicalAddress sourceAddr
;
2474 sourceAddr
= getSourceSegment( offset
, NULL
);
2475 pageOffset
= sourceAddr
- trunc_page_32( sourceAddr
);
2478 pager
= (memory_object_t
) reserved
->devicePager
;
2480 pager
= MACH_PORT_NULL
;
2482 if ((kIOMapReference
|kIOMapUnique
) == ((kIOMapReference
|kIOMapUnique
) & options
))
2490 err
= kIOReturnNotReadable
;
2494 size
= mapping
->fLength
+ pageOffset
;
2495 flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
2496 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
2498 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) _memEntry
, 0, &size
, &redirUPL2
,
2503 err
= upl_transpose(redirUPL2
, mapping
->fRedirUPL
);
2504 if (kIOReturnSuccess
!= err
)
2506 IOLog("upl_transpose(%x)\n", err
);
2507 err
= kIOReturnSuccess
;
2512 upl_commit(redirUPL2
, NULL
, 0);
2513 upl_deallocate(redirUPL2
);
2517 // swap the memEntries since they now refer to different vm_objects
2518 void * me
= _memEntry
;
2519 _memEntry
= mapping
->fMemory
->_memEntry
;
2520 mapping
->fMemory
->_memEntry
= me
;
2523 err
= handleFault( reserved
->devicePager
, mapping
->fAddressMap
, mapping
->fAddress
, offset
, length
, options
);
2527 mach_vm_address_t address
;
2529 if (!(options
& kIOMapAnywhere
))
2531 address
= trunc_page_64(mapping
->fAddress
);
2532 if( (mapping
->fAddress
- address
) != pageOffset
)
2534 err
= kIOReturnVMError
;
2539 err
= IOMemoryDescriptorMapMemEntry(mapping
->fAddressMap
, (ipc_port_t
) _memEntry
,
2540 options
, (kIOMemoryBufferPageable
& _flags
),
2541 offset
, &address
, round_page_64(length
+ pageOffset
));
2542 if( err
!= KERN_SUCCESS
)
2545 if (!_memEntry
|| pager
)
2547 err
= handleFault( pager
, mapping
->fAddressMap
, address
, offset
, length
, options
);
2548 if (err
!= KERN_SUCCESS
)
2549 doUnmap( mapping
->fAddressMap
, (IOVirtualAddress
) mapping
, 0 );
2553 if (kIOLogMapping
& gIOKitDebug
)
2554 IOLog("mapping(%x) desc %p @ %lx, map %p, address %qx, offset %qx, length %qx\n",
2555 err
, this, sourceAddr
, mapping
, address
, offset
, length
);
2558 if (err
== KERN_SUCCESS
)
2559 mapping
->fAddress
= address
+ pageOffset
;
2561 mapping
->fAddress
= NULL
;
2570 kIOMemoryRedirected
= 0x00010000
2573 IOReturn
IOMemoryDescriptor::handleFault(
2575 vm_map_t addressMap
,
2576 mach_vm_address_t address
,
2577 mach_vm_size_t sourceOffset
,
2578 mach_vm_size_t length
,
2579 IOOptionBits options
)
2581 IOReturn err
= kIOReturnSuccess
;
2582 memory_object_t pager
= (memory_object_t
) _pager
;
2583 mach_vm_size_t size
;
2584 mach_vm_size_t bytes
;
2585 mach_vm_size_t page
;
2586 mach_vm_size_t pageOffset
;
2587 mach_vm_size_t pagerOffset
;
2588 IOPhysicalLength segLen
;
2593 if( kIOMemoryRedirected
& _flags
)
2596 IOLog("sleep mem redirect %p, %qx\n", this, sourceOffset
);
2600 } while( kIOMemoryRedirected
& _flags
);
2603 return( kIOReturnSuccess
);
2606 physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
);
2608 pageOffset
= physAddr
- trunc_page_64( physAddr
);
2609 pagerOffset
= sourceOffset
;
2611 size
= length
+ pageOffset
;
2612 physAddr
-= pageOffset
;
2614 segLen
+= pageOffset
;
2618 // in the middle of the loop only map whole pages
2619 if( segLen
>= bytes
)
2621 else if( segLen
!= trunc_page_32( segLen
))
2622 err
= kIOReturnVMError
;
2623 if( physAddr
!= trunc_page_64( physAddr
))
2624 err
= kIOReturnBadArgument
;
2625 if (kIOReturnSuccess
!= err
)
2629 if( kIOLogMapping
& gIOKitDebug
)
2630 IOLog("_IOMemoryMap::map(%p) 0x%qx->0x%qx:0x%qx\n",
2631 addressMap
, address
+ pageOffset
, physAddr
+ pageOffset
,
2632 segLen
- pageOffset
);
2637 if( reserved
&& reserved
->pagerContig
) {
2638 IOPhysicalLength allLen
;
2641 allPhys
= getPhysicalSegment64( 0, &allLen
);
2643 err
= device_pager_populate_object( pager
, 0, atop_64(allPhys
), round_page_32(allLen
) );
2649 (page
< segLen
) && (KERN_SUCCESS
== err
);
2652 err
= device_pager_populate_object(pager
, pagerOffset
,
2653 (ppnum_t
)(atop_64(physAddr
+ page
)), page_size
);
2654 pagerOffset
+= page_size
;
2657 assert( KERN_SUCCESS
== err
);
2662 // This call to vm_fault causes an early pmap level resolution
2663 // of the mappings created above for kernel mappings, since
2664 // faulting in later can't take place from interrupt level.
2666 /* *** Temporary Workaround *** */
2668 if ((addressMap
== kernel_map
) && !(kIOMemoryRedirected
& _flags
))
2670 vm_fault(addressMap
,
2671 (vm_map_offset_t
)address
,
2672 VM_PROT_READ
|VM_PROT_WRITE
,
2673 FALSE
, THREAD_UNINT
, NULL
,
2674 (vm_map_offset_t
)0);
2677 /* *** Temporary Workaround *** */
2680 sourceOffset
+= segLen
- pageOffset
;
2686 while (bytes
&& (physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
)));
2689 err
= kIOReturnBadArgument
;
2694 IOReturn
IOMemoryDescriptor::doUnmap(
2695 vm_map_t addressMap
,
2696 IOVirtualAddress __address
,
2697 IOByteCount __length
)
2700 mach_vm_address_t address
;
2701 mach_vm_size_t length
;
2705 address
= __address
;
2710 addressMap
= ((_IOMemoryMap
*) __address
)->fAddressMap
;
2711 address
= ((_IOMemoryMap
*) __address
)->fAddress
;
2712 length
= ((_IOMemoryMap
*) __address
)->fLength
;
2715 if( _memEntry
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2716 addressMap
= IOPageableMapForAddress( address
);
2719 if( kIOLogMapping
& gIOKitDebug
)
2720 IOLog("IOMemoryDescriptor::doUnmap map %p, 0x%qx:0x%qx\n",
2721 addressMap
, address
, length
);
2724 err
= mach_vm_deallocate( addressMap
, address
, length
);
2729 IOReturn
IOMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2731 IOReturn err
= kIOReturnSuccess
;
2732 _IOMemoryMap
* mapping
= 0;
2738 _flags
|= kIOMemoryRedirected
;
2740 _flags
&= ~kIOMemoryRedirected
;
2743 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
2744 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject()))
2745 mapping
->redirect( safeTask
, doRedirect
);
2758 // temporary binary compatibility
2759 IOSubMemoryDescriptor
* subMem
;
2760 if( (subMem
= OSDynamicCast( IOSubMemoryDescriptor
, this)))
2761 err
= subMem
->redirect( safeTask
, doRedirect
);
2763 err
= kIOReturnSuccess
;
2768 IOReturn
IOSubMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2770 return( _parent
->redirect( safeTask
, doRedirect
));
2773 IOReturn
_IOMemoryMap::redirect( task_t safeTask
, bool doRedirect
)
2775 IOReturn err
= kIOReturnSuccess
;
2778 // err = ((_IOMemoryMap *)superMap)->redirect( safeTask, doRedirect );
2790 if ((!safeTask
|| (get_task_map(safeTask
) != fAddressMap
))
2791 && (0 == (fOptions
& kIOMapStatic
)))
2793 IOUnmapPages( fAddressMap
, fAddress
, fLength
);
2794 if(!doRedirect
&& safeTask
2795 && (((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2796 || ((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
)))
2798 IOVirtualAddress iova
= (IOVirtualAddress
) this;
2799 err
= mach_vm_deallocate( fAddressMap
, fAddress
, fLength
);
2800 err
= fMemory
->doMap( fAddressMap
, &iova
,
2801 (fOptions
& ~kIOMapAnywhere
) | kIOMap64Bit
/*| kIOMapReserve*/,
2804 err
= kIOReturnSuccess
;
2806 IOLog("IOMemoryMap::redirect(%d, %p) 0x%qx:0x%qx from %p\n", doRedirect
, this, fAddress
, fLength
, fAddressMap
);
2809 else if (kIOMapWriteCombineCache
== (fOptions
& kIOMapCacheMask
))
2811 IOOptionBits newMode
;
2812 newMode
= (fOptions
& ~kIOMapCacheMask
) | (doRedirect
? kIOMapInhibitCache
: kIOMapWriteCombineCache
);
2813 IOProtectCacheMode(fAddressMap
, fAddress
, fLength
, newMode
);
2820 if ((((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2821 || ((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
2823 && (doRedirect
!= (0 != (fMemory
->_flags
& kIOMemoryRedirected
))))
2824 fMemory
->redirect(safeTask
, doRedirect
);
2829 IOReturn
_IOMemoryMap::unmap( void )
2835 if( fAddress
&& fAddressMap
&& (0 == fSuperMap
) && fMemory
2836 && (0 == (fOptions
& kIOMapStatic
))) {
2838 err
= fMemory
->doUnmap(fAddressMap
, (IOVirtualAddress
) this, 0);
2841 err
= kIOReturnSuccess
;
2845 vm_map_deallocate(fAddressMap
);
2856 void _IOMemoryMap::taskDied( void )
2860 vm_map_deallocate(fAddressMap
);
2868 // Overload the release mechanism. All mappings must be a member
2869 // of a memory descriptors _mappings set. This means that we
2870 // always have 2 references on a mapping. When either of these mappings
2871 // are released we need to free ourselves.
2872 void _IOMemoryMap::taggedRelease(const void *tag
) const
2875 super::taggedRelease(tag
, 2);
2879 void _IOMemoryMap::free()
2886 fMemory
->removeMapping(this);
2891 if (fOwner
&& (fOwner
!= fMemory
))
2894 fOwner
->removeMapping(this);
2899 fSuperMap
->release();
2902 upl_commit(fRedirUPL
, NULL
, 0);
2903 upl_deallocate(fRedirUPL
);
2909 IOByteCount
_IOMemoryMap::getLength()
2914 IOVirtualAddress
_IOMemoryMap::getVirtualAddress()
2917 fSuperMap
->getVirtualAddress();
2918 else if (fAddressMap
&& vm_map_is_64bit(fAddressMap
))
2920 OSReportWithBacktrace("IOMemoryMap::getVirtualAddress(0x%qx) called on 64b map; use ::getAddress()", fAddress
);
2926 mach_vm_address_t
_IOMemoryMap::getAddress()
2931 mach_vm_size_t
_IOMemoryMap::getSize()
2937 task_t
_IOMemoryMap::getAddressTask()
2940 return( fSuperMap
->getAddressTask());
2942 return( fAddressTask
);
2945 IOOptionBits
_IOMemoryMap::getMapOptions()
2950 IOMemoryDescriptor
* _IOMemoryMap::getMemoryDescriptor()
2955 _IOMemoryMap
* _IOMemoryMap::copyCompatible(
2956 _IOMemoryMap
* newMapping
)
2958 task_t task
= newMapping
->getAddressTask();
2959 mach_vm_address_t toAddress
= newMapping
->fAddress
;
2960 IOOptionBits _options
= newMapping
->fOptions
;
2961 mach_vm_size_t _offset
= newMapping
->fOffset
;
2962 mach_vm_size_t _length
= newMapping
->fLength
;
2964 if( (!task
) || (!fAddressMap
) || (fAddressMap
!= get_task_map(task
)))
2966 if( (fOptions
^ _options
) & kIOMapReadOnly
)
2968 if( (kIOMapDefaultCache
!= (_options
& kIOMapCacheMask
))
2969 && ((fOptions
^ _options
) & kIOMapCacheMask
))
2972 if( (0 == (_options
& kIOMapAnywhere
)) && (fAddress
!= toAddress
))
2975 if( _offset
< fOffset
)
2980 if( (_offset
+ _length
) > fLength
)
2984 if( (fLength
== _length
) && (!_offset
))
2986 newMapping
->release();
2991 newMapping
->fSuperMap
= this;
2992 newMapping
->fOffset
= _offset
;
2993 newMapping
->fAddress
= fAddress
+ _offset
;
2996 return( newMapping
);
3000 _IOMemoryMap::getPhysicalSegment( IOByteCount _offset
, IOPhysicalLength
* _length
)
3002 IOPhysicalAddress address
;
3005 address
= fMemory
->getPhysicalSegment( fOffset
+ _offset
, _length
);
3011 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3014 #define super OSObject
3016 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3018 void IOMemoryDescriptor::initialize( void )
3020 if( 0 == gIOMemoryLock
)
3021 gIOMemoryLock
= IORecursiveLockAlloc();
3023 IORegistryEntry::getRegistryRoot()->setProperty(kIOMaximumMappedIOByteCountKey
,
3024 ptoa_64(gIOMaximumMappedIOPageCount
), 64);
3028 mapper
= new IOCopyMapper
;
3031 if (mapper
->init() && mapper
->start(NULL
))
3032 gIOCopyMapper
= (IOCopyMapper
*) mapper
;
3038 gIOLastPage
= IOGetLastPageNumber();
3041 void IOMemoryDescriptor::free( void )
3044 _mappings
->release();
3049 IOMemoryMap
* IOMemoryDescriptor::setMapping(
3051 IOVirtualAddress mapAddress
,
3052 IOOptionBits options
)
3054 return (createMappingInTask( intoTask
, mapAddress
,
3055 options
| kIOMapStatic
,
3059 IOMemoryMap
* IOMemoryDescriptor::map(
3060 IOOptionBits options
)
3062 return (createMappingInTask( kernel_task
, 0,
3063 options
| kIOMapAnywhere
,
3067 IOMemoryMap
* IOMemoryDescriptor::map(
3069 IOVirtualAddress atAddress
,
3070 IOOptionBits options
,
3072 IOByteCount length
)
3074 if ((!(kIOMapAnywhere
& options
)) && vm_map_is_64bit(get_task_map(intoTask
)))
3076 OSReportWithBacktrace("IOMemoryDescriptor::map() in 64b task, use ::createMappingInTask()");
3080 return (createMappingInTask(intoTask
, atAddress
,
3081 options
, offset
, length
));
3084 IOMemoryMap
* IOMemoryDescriptor::createMappingInTask(
3086 mach_vm_address_t atAddress
,
3087 IOOptionBits options
,
3088 mach_vm_size_t offset
,
3089 mach_vm_size_t length
)
3091 IOMemoryMap
* result
;
3092 _IOMemoryMap
* mapping
;
3095 length
= getLength();
3097 mapping
= new _IOMemoryMap
;
3100 && !mapping
->init( intoTask
, atAddress
,
3101 options
, offset
, length
)) {
3107 result
= makeMapping(this, intoTask
, (IOVirtualAddress
) mapping
, options
| kIOMap64Bit
, 0, 0);
3113 IOLog("createMappingInTask failed desc %p, addr %qx, options %lx, offset %qx, length %qx\n",
3114 this, atAddress
, options
, offset
, length
);
3120 IOReturn
_IOMemoryMap::redirect(IOMemoryDescriptor
* newBackingMemory
,
3121 IOOptionBits options
,
3124 return (redirect(newBackingMemory
, options
, (mach_vm_size_t
)offset
));
3127 IOReturn
_IOMemoryMap::redirect(IOMemoryDescriptor
* newBackingMemory
,
3128 IOOptionBits options
,
3129 mach_vm_size_t offset
)
3131 IOReturn err
= kIOReturnSuccess
;
3132 IOMemoryDescriptor
* physMem
= 0;
3136 if (fAddress
&& fAddressMap
) do
3138 if (((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
3139 || ((fMemory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
3147 vm_size_t size
= fLength
;
3148 int flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
3149 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
3150 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) fMemory
->_memEntry
, 0, &size
, &fRedirUPL
,
3157 IOUnmapPages( fAddressMap
, fAddress
, fLength
);
3158 physMem
->redirect(0, true);
3162 if (newBackingMemory
)
3164 if (newBackingMemory
!= fMemory
)
3167 if (this != newBackingMemory
->makeMapping(newBackingMemory
, fAddressTask
, (IOVirtualAddress
) this,
3168 options
| kIOMapUnique
| kIOMapReference
| kIOMap64Bit
,
3170 err
= kIOReturnError
;
3174 upl_commit(fRedirUPL
, NULL
, 0);
3175 upl_deallocate(fRedirUPL
);
3179 physMem
->redirect(0, false);
3192 IOMemoryMap
* IOMemoryDescriptor::makeMapping(
3193 IOMemoryDescriptor
* owner
,
3195 IOVirtualAddress __address
,
3196 IOOptionBits options
,
3197 IOByteCount __offset
,
3198 IOByteCount __length
)
3200 if (!(kIOMap64Bit
& options
)) panic("IOMemoryDescriptor::makeMapping !64bit");
3202 IOMemoryDescriptor
* mapDesc
= 0;
3203 _IOMemoryMap
* result
= 0;
3206 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) __address
;
3207 mach_vm_size_t offset
= mapping
->fOffset
+ __offset
;
3208 mach_vm_size_t length
= mapping
->fLength
;
3210 mapping
->fOffset
= offset
;
3216 if (kIOMapStatic
& options
)
3219 addMapping(mapping
);
3220 mapping
->setMemoryDescriptor(this, 0);
3224 if (kIOMapUnique
& options
)
3226 IOPhysicalAddress phys
;
3227 IOByteCount physLen
;
3229 // if (owner != this) continue;
3231 if (((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
3232 || ((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
3234 phys
= getPhysicalSegment(offset
, &physLen
);
3235 if (!phys
|| (physLen
< length
))
3238 mapDesc
= IOMemoryDescriptor::withPhysicalAddress(
3239 phys
, length
, _direction
);
3243 mapping
->fOffset
= offset
;
3248 // look for a compatible existing mapping
3249 if( (iter
= OSCollectionIterator::withCollection(_mappings
)))
3251 _IOMemoryMap
* lookMapping
;
3252 while ((lookMapping
= (_IOMemoryMap
*) iter
->getNextObject()))
3254 if ((result
= lookMapping
->copyCompatible(mapping
)))
3257 result
->setMemoryDescriptor(this, offset
);
3263 if (result
|| (options
& kIOMapReference
))
3273 kr
= mapDesc
->doMap( 0, (IOVirtualAddress
*) &mapping
, options
, 0, 0 );
3274 if (kIOReturnSuccess
== kr
)
3277 mapDesc
->addMapping(result
);
3278 result
->setMemoryDescriptor(mapDesc
, offset
);
3296 void IOMemoryDescriptor::addMapping(
3297 IOMemoryMap
* mapping
)
3302 _mappings
= OSSet::withCapacity(1);
3304 _mappings
->setObject( mapping
);
3308 void IOMemoryDescriptor::removeMapping(
3309 IOMemoryMap
* mapping
)
3312 _mappings
->removeObject( mapping
);
3315 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3318 #define super IOMemoryDescriptor
3320 OSDefineMetaClassAndStructors(IOSubMemoryDescriptor
, IOMemoryDescriptor
)
3322 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3324 bool IOSubMemoryDescriptor::initSubRange( IOMemoryDescriptor
* parent
,
3325 IOByteCount offset
, IOByteCount length
,
3326 IODirection direction
)
3331 if( (offset
+ length
) > parent
->getLength())
3335 * We can check the _parent instance variable before having ever set it
3336 * to an initial value because I/O Kit guarantees that all our instance
3337 * variables are zeroed on an object's allocation.
3345 * An existing memory descriptor is being retargeted to
3346 * point to somewhere else. Clean up our present state.
3357 _direction
= direction
;
3358 _tag
= parent
->getTag();
3363 void IOSubMemoryDescriptor::free( void )
3373 IOSubMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
3377 if (kIOMDGetCharacteristics
== op
) {
3379 rtn
= _parent
->dmaCommandOperation(op
, vData
, dataSize
);
3380 if (kIOReturnSuccess
== rtn
) {
3381 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
3382 data
->fLength
= _length
;
3383 data
->fSGCount
= 0; // XXX gvdl: need to compute and pages
3385 data
->fPageAlign
= 0;
3390 else if (kIOMDWalkSegments
& op
) {
3391 if (dataSize
< sizeof(IOMDDMAWalkSegmentArgs
))
3392 return kIOReturnUnderrun
;
3394 IOMDDMAWalkSegmentArgs
*data
=
3395 reinterpret_cast<IOMDDMAWalkSegmentArgs
*>(vData
);
3396 UInt offset
= data
->fOffset
;
3397 UInt remain
= _length
- offset
;
3398 if ((int) remain
<= 0)
3399 return (!remain
)? kIOReturnOverrun
: kIOReturnInternalError
;
3401 data
->fOffset
= offset
+ _start
;
3402 rtn
= _parent
->dmaCommandOperation(op
, vData
, dataSize
);
3403 if (data
->fLength
> remain
)
3404 data
->fLength
= remain
;
3405 data
->fOffset
= offset
;
3410 return kIOReturnBadArgument
;
3414 IOSubMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
* length
)
3417 IOByteCount actualLength
;
3419 assert(offset
<= _length
);
3424 if( offset
>= _length
)
3427 address
= _parent
->getPhysicalSegment64( offset
+ _start
, &actualLength
);
3429 if( address
&& length
)
3430 *length
= min( _length
- offset
, actualLength
);
3436 IOSubMemoryDescriptor::getPhysicalSegment( IOByteCount offset
, IOByteCount
* length
)
3438 IOPhysicalAddress address
;
3439 IOByteCount actualLength
;
3441 assert(offset
<= _length
);
3446 if( offset
>= _length
)
3449 address
= _parent
->getPhysicalSegment( offset
+ _start
, &actualLength
);
3451 if( address
&& length
)
3452 *length
= min( _length
- offset
, actualLength
);
3458 IOSubMemoryDescriptor::getSourceSegment( IOByteCount offset
, IOByteCount
* length
)
3460 IOPhysicalAddress address
;
3461 IOByteCount actualLength
;
3463 assert(offset
<= _length
);
3468 if( offset
>= _length
)
3471 address
= _parent
->getSourceSegment( offset
+ _start
, &actualLength
);
3473 if( address
&& length
)
3474 *length
= min( _length
- offset
, actualLength
);
3479 void * IOSubMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
3480 IOByteCount
* lengthOfSegment
)
3485 IOReturn
IOSubMemoryDescriptor::doMap(
3486 vm_map_t addressMap
,
3487 IOVirtualAddress
* atAddress
,
3488 IOOptionBits options
,
3489 IOByteCount sourceOffset
,
3490 IOByteCount length
)
3492 panic("IOSubMemoryDescriptor::doMap");
3493 return (IOMemoryDescriptor::doMap(addressMap
, atAddress
, options
, sourceOffset
, length
));
3496 IOByteCount
IOSubMemoryDescriptor::readBytes(IOByteCount offset
,
3497 void * bytes
, IOByteCount length
)
3499 IOByteCount byteCount
;
3501 assert(offset
<= _length
);
3503 if( offset
>= _length
)
3507 byteCount
= _parent
->readBytes( _start
+ offset
, bytes
,
3508 min(length
, _length
- offset
) );
3511 return( byteCount
);
3514 IOByteCount
IOSubMemoryDescriptor::writeBytes(IOByteCount offset
,
3515 const void* bytes
, IOByteCount length
)
3517 IOByteCount byteCount
;
3519 assert(offset
<= _length
);
3521 if( offset
>= _length
)
3525 byteCount
= _parent
->writeBytes( _start
+ offset
, bytes
,
3526 min(length
, _length
- offset
) );
3529 return( byteCount
);
3532 IOReturn
IOSubMemoryDescriptor::setPurgeable( IOOptionBits newState
,
3533 IOOptionBits
* oldState
)
3538 err
= _parent
->setPurgeable( newState
, oldState
);
3544 IOReturn
IOSubMemoryDescriptor::performOperation( IOOptionBits options
,
3545 IOByteCount offset
, IOByteCount length
)
3549 assert(offset
<= _length
);
3551 if( offset
>= _length
)
3552 return( kIOReturnOverrun
);
3555 err
= _parent
->performOperation( options
, _start
+ offset
,
3556 min(length
, _length
- offset
) );
3562 IOReturn
IOSubMemoryDescriptor::prepare(
3563 IODirection forDirection
)
3568 err
= _parent
->prepare( forDirection
);
3574 IOReturn
IOSubMemoryDescriptor::complete(
3575 IODirection forDirection
)
3580 err
= _parent
->complete( forDirection
);
3586 IOMemoryMap
* IOSubMemoryDescriptor::makeMapping(
3587 IOMemoryDescriptor
* owner
,
3589 IOVirtualAddress address
,
3590 IOOptionBits options
,
3592 IOByteCount length
)
3594 IOMemoryMap
* mapping
= 0;
3596 if (!(kIOMap64Bit
& options
))
3598 panic("IOSubMemoryDescriptor::makeMapping !64bit");
3601 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3605 options
, _start
+ offset
, length
);
3613 IOSubMemoryDescriptor::initWithAddress(void * address
,
3615 IODirection direction
)
3621 IOSubMemoryDescriptor::initWithAddress(vm_address_t address
,
3623 IODirection direction
,
3630 IOSubMemoryDescriptor::initWithPhysicalAddress(
3631 IOPhysicalAddress address
,
3633 IODirection direction
)
3639 IOSubMemoryDescriptor::initWithRanges(
3640 IOVirtualRange
* ranges
,
3642 IODirection direction
,
3650 IOSubMemoryDescriptor::initWithPhysicalRanges( IOPhysicalRange
* ranges
,
3652 IODirection direction
,
3658 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3660 bool IOGeneralMemoryDescriptor::serialize(OSSerialize
* s
) const
3662 OSSymbol
const *keys
[2];
3663 OSObject
*values
[2];
3665 user_addr_t address
;
3668 unsigned int index
, nRanges
;
3671 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
3673 if (s
== NULL
) return false;
3674 if (s
->previouslySerialized(this)) return true;
3676 // Pretend we are an array.
3677 if (!s
->addXMLStartTag(this, "array")) return false;
3679 nRanges
= _rangesCount
;
3680 vcopy
= (SerData
*) IOMalloc(sizeof(SerData
) * nRanges
);
3681 if (vcopy
== 0) return false;
3683 keys
[0] = OSSymbol::withCString("address");
3684 keys
[1] = OSSymbol::withCString("length");
3687 values
[0] = values
[1] = 0;
3689 // From this point on we can go to bail.
3691 // Copy the volatile data so we don't have to allocate memory
3692 // while the lock is held.
3694 if (nRanges
== _rangesCount
) {
3695 Ranges vec
= _ranges
;
3696 for (index
= 0; index
< nRanges
; index
++) {
3697 user_addr_t addr
; IOByteCount len
;
3698 getAddrLenForInd(addr
, len
, type
, vec
, index
);
3699 vcopy
[index
].address
= addr
;
3700 vcopy
[index
].length
= len
;
3703 // The descriptor changed out from under us. Give up.
3710 for (index
= 0; index
< nRanges
; index
++)
3712 user_addr_t addr
= vcopy
[index
].address
;
3713 IOByteCount len
= (IOByteCount
) vcopy
[index
].length
;
3715 OSNumber::withNumber(addr
, (((UInt64
) addr
) >> 32)? 64 : 32);
3716 if (values
[0] == 0) {
3720 values
[1] = OSNumber::withNumber(len
, sizeof(len
) * 8);
3721 if (values
[1] == 0) {
3725 OSDictionary
*dict
= OSDictionary::withObjects((const OSObject
**)values
, (const OSSymbol
**)keys
, 2);
3730 values
[0]->release();
3731 values
[1]->release();
3732 values
[0] = values
[1] = 0;
3734 result
= dict
->serialize(s
);
3740 result
= s
->addXMLEndTag("array");
3744 values
[0]->release();
3746 values
[1]->release();
3752 IOFree(vcopy
, sizeof(SerData
) * nRanges
);
3756 bool IOSubMemoryDescriptor::serialize(OSSerialize
* s
) const
3761 if (s
->previouslySerialized(this)) return true;
3763 // Pretend we are a dictionary.
3764 // We must duplicate the functionality of OSDictionary here
3765 // because otherwise object references will not work;
3766 // they are based on the value of the object passed to
3767 // previouslySerialized and addXMLStartTag.
3769 if (!s
->addXMLStartTag(this, "dict")) return false;
3771 char const *keys
[3] = {"offset", "length", "parent"};
3773 OSObject
*values
[3];
3774 values
[0] = OSNumber::withNumber(_start
, sizeof(_start
) * 8);
3777 values
[1] = OSNumber::withNumber(_length
, sizeof(_length
) * 8);
3778 if (values
[1] == 0) {
3779 values
[0]->release();
3782 values
[2] = _parent
;
3785 for (int i
=0; i
<3; i
++) {
3786 if (!s
->addString("<key>") ||
3787 !s
->addString(keys
[i
]) ||
3788 !s
->addXMLEndTag("key") ||
3789 !values
[i
]->serialize(s
)) {
3794 values
[0]->release();
3795 values
[1]->release();
3800 return s
->addXMLEndTag("dict");
3803 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3805 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 0);
3806 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 1);
3807 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 2);
3808 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 3);
3809 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 4);
3810 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 5);
3811 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 6);
3812 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 7);
3813 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 8);
3814 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 9);
3815 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 10);
3816 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 11);
3817 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 12);
3818 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 13);
3819 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 14);
3820 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 15);
3822 /* ex-inline function implementation */
3824 IOMemoryDescriptor::getPhysicalAddress()
3825 { return( getPhysicalSegment( 0, 0 )); }