2 * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
36 // 45678901234567890123456789012345678901234567890123456789012345678901234567890
37 #include <sys/cdefs.h>
39 #include <IOKit/assert.h>
40 #include <IOKit/system.h>
41 #include <IOKit/IOLib.h>
42 #include <IOKit/IOMemoryDescriptor.h>
43 #include <IOKit/IOMapper.h>
44 #include <IOKit/IOKitKeysPrivate.h>
46 #include <IOKit/IOKitDebug.h>
48 #include "IOKitKernelInternal.h"
49 #include "IOCopyMapper.h"
51 #include <libkern/c++/OSContainers.h>
52 #include <libkern/c++/OSDictionary.h>
53 #include <libkern/c++/OSArray.h>
54 #include <libkern/c++/OSSymbol.h>
55 #include <libkern/c++/OSNumber.h>
61 #include <vm/vm_pageout.h>
62 #include <vm/vm_shared_memory_server.h>
63 #include <mach/memory_object_types.h>
64 #include <device/device_port.h>
66 #include <mach/vm_prot.h>
67 #include <vm/vm_fault.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
* memory
;
145 IOMemoryMap
* superMap
;
148 IOVirtualAddress logical
;
151 IOOptionBits options
;
153 ipc_port_t redirEntry
;
154 IOMemoryDescriptor
* owner
;
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 IOMemoryDescriptor
* getMemoryDescriptor();
167 virtual IOOptionBits
getMapOptions();
169 virtual IOReturn
unmap();
170 virtual void taskDied();
172 virtual IOReturn
redirect(IOMemoryDescriptor
* newBackingMemory
,
173 IOOptionBits options
,
174 IOByteCount offset
= 0);
176 virtual IOPhysicalAddress
getPhysicalSegment(IOByteCount offset
,
177 IOByteCount
* length
);
179 // for IOMemoryDescriptor use
180 _IOMemoryMap
* copyCompatible(
181 IOMemoryDescriptor
* owner
,
183 IOVirtualAddress toAddress
,
184 IOOptionBits options
,
186 IOByteCount length
);
189 IOMemoryDescriptor
* memory
,
190 IOMemoryMap
* superMap
,
192 IOByteCount length
);
194 bool initWithDescriptor(
195 IOMemoryDescriptor
* memory
,
197 IOVirtualAddress toAddress
,
198 IOOptionBits options
,
200 IOByteCount length
);
203 task_t intoTask
, bool redirect
);
206 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
208 // Some data structures and accessor macros used by the initWithOptions
211 enum ioPLBlockFlags
{
212 kIOPLOnDevice
= 0x00000001,
213 kIOPLExternUPL
= 0x00000002,
216 struct typePersMDData
218 const IOGeneralMemoryDescriptor
*fMD
;
219 ipc_port_t fMemEntry
;
224 vm_address_t fIOMDOffset
; // The offset of this iopl in descriptor
225 vm_offset_t fPageInfo
; // Pointer to page list or index into it
226 ppnum_t fMappedBase
; // Page number of first page in this iopl
227 unsigned int fPageOffset
; // Offset within first page of iopl
228 unsigned int fFlags
; // Flags
233 unsigned int fPageCnt
;
234 upl_page_info_t fPageList
[];
238 #define getDataP(osd) ((ioGMDData *) (osd)->getBytesNoCopy())
239 #define getIOPLList(d) ((ioPLBlock *) &(d->fPageList[d->fPageCnt]))
240 #define getNumIOPL(osd, d) \
241 (((osd)->getLength() - ((char *) getIOPLList(d) - (char *) d)) / sizeof(ioPLBlock))
242 #define getPageList(d) (&(d->fPageList[0]))
243 #define computeDataSize(p, u) \
244 (sizeof(ioGMDData) + p * sizeof(upl_page_info_t) + u * sizeof(ioPLBlock))
247 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
249 #define next_page(a) ( trunc_page_32(a) + PAGE_SIZE )
254 kern_return_t
device_data_action(
256 ipc_port_t device_pager
,
257 vm_prot_t protection
,
258 vm_object_offset_t offset
,
261 struct ExpansionData
{
263 unsigned int pagerContig
:1;
264 unsigned int unused
:31;
265 IOMemoryDescriptor
* memory
;
268 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
269 IOMemoryDescriptor
* memDesc
;
272 memDesc
= ref
->memory
;
276 kr
= memDesc
->handleFault( device_pager
, 0, 0,
277 offset
, size
, kIOMapDefaultCache
/*?*/);
287 kern_return_t
device_close(
290 struct ExpansionData
{
292 unsigned int pagerContig
:1;
293 unsigned int unused
:31;
294 IOMemoryDescriptor
* memory
;
296 ExpansionData
* ref
= (ExpansionData
*) device_handle
;
298 IODelete( ref
, ExpansionData
, 1 );
300 return( kIOReturnSuccess
);
304 // Note this inline function uses C++ reference arguments to return values
305 // This means that pointers are not passed and NULLs don't have to be
306 // checked for as a NULL reference is illegal.
308 getAddrLenForInd(addr64_t
&addr
, IOPhysicalLength
&len
, // Output variables
309 UInt32 type
, IOGeneralMemoryDescriptor::Ranges r
, UInt32 ind
)
311 assert(kIOMemoryTypeUIO
== type
312 || kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
313 || kIOMemoryTypePhysical
== type
|| kIOMemoryTypePhysical64
== type
);
314 if (kIOMemoryTypeUIO
== type
) {
316 uio_getiov((uio_t
) r
.uio
, ind
, &addr
, &us
); len
= us
;
318 else if ((kIOMemoryTypeVirtual64
== type
) || (kIOMemoryTypePhysical64
== type
)) {
319 IOAddressRange cur
= r
.v64
[ind
];
324 IOVirtualRange cur
= r
.v
[ind
];
330 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
335 * Create a new IOMemoryDescriptor. The buffer is a virtual address
336 * relative to the specified task. If no task is supplied, the kernel
340 IOMemoryDescriptor::withAddress(void * address
,
342 IODirection direction
)
344 return IOMemoryDescriptor::
345 withAddress((vm_address_t
) address
, length
, direction
, kernel_task
);
349 IOMemoryDescriptor::withAddress(vm_address_t address
,
351 IODirection direction
,
357 IOOptionBits options
= (IOOptionBits
) direction
;
358 if (task
== kernel_task
)
359 options
|= kIOMemoryAutoPrepare
;
360 return (IOMemoryDescriptor::withAddressRange(address
, length
, options
, task
));
363 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
366 if (that
->initWithAddress(address
, length
, direction
, task
))
375 IOMemoryDescriptor::withPhysicalAddress(
376 IOPhysicalAddress address
,
378 IODirection direction
)
381 return (IOMemoryDescriptor::withAddressRange(address
, length
, (IOOptionBits
) direction
, NULL
));
383 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
385 && !self
->initWithPhysicalAddress(address
, length
, direction
)) {
394 IOMemoryDescriptor::withRanges( IOVirtualRange
* ranges
,
396 IODirection direction
,
400 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
403 if (that
->initWithRanges(ranges
, withCount
, direction
, task
, asReference
))
412 IOMemoryDescriptor::withAddressRange(mach_vm_address_t address
,
413 mach_vm_size_t length
,
414 IOOptionBits options
,
417 IOAddressRange range
= { address
, length
};
418 return (IOMemoryDescriptor::withAddressRanges(&range
, 1, options
, task
));
422 IOMemoryDescriptor::withAddressRanges(IOAddressRange
* ranges
,
424 IOOptionBits options
,
427 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
431 options
|= kIOMemoryTypeVirtual64
;
433 options
|= kIOMemoryTypePhysical64
;
435 if (that
->initWithOptions(ranges
, rangeCount
, 0, task
, options
, /* mapper */ 0))
448 * Create a new IOMemoryDescriptor. The buffer is made up of several
449 * virtual address ranges, from a given task.
451 * Passing the ranges as a reference will avoid an extra allocation.
454 IOMemoryDescriptor::withOptions(void * buffers
,
461 IOGeneralMemoryDescriptor
*self
= new IOGeneralMemoryDescriptor
;
464 && !self
->initWithOptions(buffers
, count
, offset
, task
, opts
, mapper
))
473 // Can't leave abstract but this should never be used directly,
474 bool IOMemoryDescriptor::initWithOptions(void * buffers
,
478 IOOptionBits options
,
481 // @@@ gvdl: Should I panic?
482 panic("IOMD::initWithOptions called\n");
487 IOMemoryDescriptor::withPhysicalRanges( IOPhysicalRange
* ranges
,
489 IODirection direction
,
492 IOGeneralMemoryDescriptor
* that
= new IOGeneralMemoryDescriptor
;
495 if (that
->initWithPhysicalRanges(ranges
, withCount
, direction
, asReference
))
504 IOMemoryDescriptor::withSubRange(IOMemoryDescriptor
* of
,
507 IODirection direction
)
509 IOSubMemoryDescriptor
*self
= new IOSubMemoryDescriptor
;
511 if (self
&& !self
->initSubRange(of
, offset
, length
, direction
)) {
519 IOMemoryDescriptor::withPersistentMemoryDescriptor(IOMemoryDescriptor
*originalMD
)
521 IOGeneralMemoryDescriptor
*origGenMD
=
522 OSDynamicCast(IOGeneralMemoryDescriptor
, originalMD
);
525 return IOGeneralMemoryDescriptor::
526 withPersistentMemoryDescriptor(origGenMD
);
532 IOGeneralMemoryDescriptor::withPersistentMemoryDescriptor(IOGeneralMemoryDescriptor
*originalMD
)
534 ipc_port_t sharedMem
= (ipc_port_t
) originalMD
->createNamedEntry();
539 if (sharedMem
== originalMD
->_memEntry
) {
540 originalMD
->retain(); // Add a new reference to ourselves
541 ipc_port_release_send(sharedMem
); // Remove extra send right
545 IOGeneralMemoryDescriptor
* self
= new IOGeneralMemoryDescriptor
;
546 typePersMDData initData
= { originalMD
, sharedMem
};
549 && !self
->initWithOptions(&initData
, 1, 0, 0, kIOMemoryTypePersistentMD
, 0)) {
556 void *IOGeneralMemoryDescriptor::createNamedEntry()
559 ipc_port_t sharedMem
;
561 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
563 user_addr_t range0Addr
;
564 IOByteCount range0Len
;
565 getAddrLenForInd(range0Addr
, range0Len
, type
, _ranges
, 0);
566 range0Addr
= trunc_page_64(range0Addr
);
568 vm_size_t size
= ptoa_32(_pages
);
569 vm_address_t kernelPage
= (vm_address_t
) range0Addr
;
571 vm_map_t theMap
= ((_task
== kernel_task
)
572 && (kIOMemoryBufferPageable
& _flags
))
573 ? IOPageableMapForAddress(kernelPage
)
574 : get_task_map(_task
);
576 memory_object_size_t actualSize
= size
;
577 vm_prot_t prot
= VM_PROT_READ
| 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
:
749 case kIOMemoryTypePhysical
: // Neither Physical nor UPL should have a task
750 case kIOMemoryTypePhysical64
:
751 mapper
= kIOMapperNone
;
753 case kIOMemoryTypeUPL
:
757 return false; /* bad argument */
764 * We can check the _initialized instance variable before having ever set
765 * it to an initial value because I/O Kit guarantees that all our instance
766 * variables are zeroed on an object's allocation.
771 * An existing memory descriptor is being retargeted to point to
772 * somewhere else. Clean up our present state.
777 if (_ranges
.v
&& _rangesIsAllocated
)
779 if (kIOMemoryTypeUIO
== type
)
780 uio_free((uio_t
) _ranges
.v
);
781 else if ((kIOMemoryTypeVirtual64
== type
) || (kIOMemoryTypePhysical64
== type
))
782 IODelete(_ranges
.v64
, IOAddressRange
, _rangesCount
);
784 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
787 { ipc_port_release_send((ipc_port_t
) _memEntry
); _memEntry
= 0; }
795 // Grab the appropriate mapper
796 if (mapper
== kIOMapperNone
)
797 mapper
= 0; // No Mapper
798 else if (mapper
== kIOMapperSystem
) {
799 IOMapper::checkForSystemMapper();
800 gIOSystemMapper
= mapper
= IOMapper::gSystem
;
803 // Remove the dynamic internal use flags from the initial setting
804 options
&= ~(kIOMemoryPreparedReadOnly
);
808 // DEPRECATED variable initialisation
809 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
811 __iomd_reservedA
= 0;
812 __iomd_reservedB
= 0;
813 __iomd_reservedC
= 0;
817 if (kIOMemoryTypeUPL
== type
) {
820 unsigned int dataSize
= computeDataSize(/* pages */ 0, /* upls */ 1);
822 if (!_memoryEntries
) {
823 _memoryEntries
= OSData::withCapacity(dataSize
);
827 else if (!_memoryEntries
->initWithCapacity(dataSize
))
830 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
831 dataP
= getDataP(_memoryEntries
);
832 dataP
->fMapper
= mapper
;
835 // _wireCount++; // UPLs start out life wired
838 _pages
+= atop_32(offset
+ count
+ PAGE_MASK
) - atop_32(offset
);
841 upl_page_info_t
*pageList
= UPL_GET_INTERNAL_PAGE_LIST((upl_t
) buffers
);
843 iopl
.fIOPL
= (upl_t
) buffers
;
844 // Set the flag kIOPLOnDevice convieniently equal to 1
845 iopl
.fFlags
= pageList
->device
| kIOPLExternUPL
;
846 iopl
.fIOMDOffset
= 0;
848 _highestPage
= upl_get_highest_page(iopl
.fIOPL
);
850 if (!pageList
->device
) {
851 // Pre-compute the offset into the UPL's page list
852 pageList
= &pageList
[atop_32(offset
)];
855 iopl
.fMappedBase
= mapper
->iovmAlloc(_pages
);
856 mapper
->iovmInsert(iopl
.fMappedBase
, 0, pageList
, _pages
);
859 iopl
.fMappedBase
= 0;
862 iopl
.fMappedBase
= 0;
863 iopl
.fPageInfo
= (vm_address_t
) pageList
;
864 iopl
.fPageOffset
= offset
;
866 _memoryEntries
->appendBytes(&iopl
, sizeof(iopl
));
869 // kIOMemoryTypeVirtual | kIOMemoryTypeVirtual64 | kIOMemoryTypeUIO
870 // kIOMemoryTypePhysical | kIOMemoryTypePhysical64
872 // Initialize the memory descriptor
873 if (options
& kIOMemoryAsReference
) {
874 _rangesIsAllocated
= false;
876 // Hack assignment to get the buffer arg into _ranges.
877 // I'd prefer to do _ranges = (Ranges) buffers, but that doesn't
879 // This also initialises the uio & physical ranges.
880 _ranges
.v
= (IOVirtualRange
*) buffers
;
883 _rangesIsAllocated
= true;
884 switch (_flags
& kIOMemoryTypeMask
)
886 case kIOMemoryTypeUIO
:
887 _ranges
.v
= (IOVirtualRange
*) uio_duplicate((uio_t
) buffers
);
890 case kIOMemoryTypeVirtual64
:
891 case kIOMemoryTypePhysical64
:
892 _ranges
.v64
= IONew(IOAddressRange
, count
);
895 bcopy(buffers
, _ranges
.v
, count
* sizeof(IOAddressRange
));
897 case kIOMemoryTypeVirtual
:
898 _ranges
.v
= IONew(IOVirtualRange
, count
);
901 bcopy(buffers
, _ranges
.v
, count
* sizeof(IOVirtualRange
));
906 // Find starting address within the vector of ranges
907 Ranges vec
= _ranges
;
910 for (unsigned ind
= 0; ind
< count
; ind
++) {
914 // addr & len are returned by this function
915 getAddrLenForInd(addr
, len
, type
, vec
, ind
);
916 pages
+= (atop_64(addr
+ len
+ PAGE_MASK
) - atop_64(addr
));
918 assert(len
>= length
); // Check for 32 bit wrap around
921 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
923 ppnum_t highPage
= atop_64(addr
+ len
- 1);
924 if (highPage
> _highestPage
)
925 _highestPage
= highPage
;
930 _rangesCount
= count
;
932 // Auto-prepare memory at creation time.
933 // Implied completion when descriptor is free-ed
934 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
))
935 _wireCount
++; // Physical MDs are, by definition, wired
936 else { /* kIOMemoryTypeVirtual | kIOMemoryTypeVirtual64 | kIOMemoryTypeUIO */
938 unsigned dataSize
= computeDataSize(_pages
, /* upls */ count
* 2);
940 if (!_memoryEntries
) {
941 _memoryEntries
= OSData::withCapacity(dataSize
);
945 else if (!_memoryEntries
->initWithCapacity(dataSize
))
948 _memoryEntries
->appendBytes(0, sizeof(ioGMDData
));
949 dataP
= getDataP(_memoryEntries
);
950 dataP
->fMapper
= mapper
;
951 dataP
->fPageCnt
= _pages
;
953 if ( (kIOMemoryPersistent
& _flags
) && !_memEntry
)
954 _memEntry
= createNamedEntry();
956 if ((_flags
& kIOMemoryAutoPrepare
)
957 && prepare() != kIOReturnSuccess
)
970 void IOGeneralMemoryDescriptor::free()
974 reserved
->memory
= 0;
980 _memoryEntries
->release();
982 if (_ranges
.v
&& _rangesIsAllocated
)
984 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
985 if (kIOMemoryTypeUIO
== type
)
986 uio_free((uio_t
) _ranges
.v
);
987 else if ((kIOMemoryTypeVirtual64
== type
) || (kIOMemoryTypePhysical64
== type
))
988 IODelete(_ranges
.v64
, IOAddressRange
, _rangesCount
);
990 IODelete(_ranges
.v
, IOVirtualRange
, _rangesCount
);
993 if (reserved
&& reserved
->devicePager
)
994 device_pager_deallocate( (memory_object_t
) reserved
->devicePager
);
996 // memEntry holds a ref on the device pager which owns reserved
997 // (ExpansionData) so no reserved access after this point
999 ipc_port_release_send( (ipc_port_t
) _memEntry
);
1004 /* DEPRECATED */ void IOGeneralMemoryDescriptor::unmapFromKernel()
1006 panic("IOGMD::unmapFromKernel deprecated");
1009 /* DEPRECATED */ void IOGeneralMemoryDescriptor::mapIntoKernel(unsigned rangeIndex
)
1011 panic("IOGMD::mapIntoKernel deprecated");
1017 * Get the direction of the transfer.
1019 IODirection
IOMemoryDescriptor::getDirection() const
1027 * Get the length of the transfer (over all ranges).
1029 IOByteCount
IOMemoryDescriptor::getLength() const
1034 void IOMemoryDescriptor::setTag( IOOptionBits tag
)
1039 IOOptionBits
IOMemoryDescriptor::getTag( void )
1044 // @@@ gvdl: who is using this API? Seems like a wierd thing to implement.
1046 IOMemoryDescriptor::getSourceSegment( IOByteCount offset
, IOByteCount
* length
)
1048 addr64_t physAddr
= 0;
1050 if( prepare() == kIOReturnSuccess
) {
1051 physAddr
= getPhysicalSegment64( offset
, length
);
1055 return( (IOPhysicalAddress
) physAddr
); // truncated but only page offset is used
1058 IOByteCount
IOMemoryDescriptor::readBytes
1059 (IOByteCount offset
, void *bytes
, IOByteCount length
)
1061 addr64_t dstAddr
= (addr64_t
) (UInt32
) bytes
;
1062 IOByteCount remaining
;
1064 // Assert that this entire I/O is withing the available range
1065 assert(offset
< _length
);
1066 assert(offset
+ length
<= _length
);
1067 if (offset
>= _length
) {
1068 IOLog("IOGMD(%p): rB = o%lx, l%lx\n", this, offset
, length
); // @@@ gvdl
1072 remaining
= length
= min(length
, _length
- offset
);
1073 while (remaining
) { // (process another target segment?)
1077 srcAddr64
= getPhysicalSegment64(offset
, &srcLen
);
1081 // Clip segment length to remaining
1082 if (srcLen
> remaining
)
1085 copypv(srcAddr64
, dstAddr
, srcLen
,
1086 cppvPsrc
| cppvNoRefSrc
| cppvFsnk
| cppvKmap
);
1090 remaining
-= srcLen
;
1095 return length
- remaining
;
1098 IOByteCount
IOMemoryDescriptor::writeBytes
1099 (IOByteCount offset
, const void *bytes
, IOByteCount length
)
1101 addr64_t srcAddr
= (addr64_t
) (UInt32
) bytes
;
1102 IOByteCount remaining
;
1104 // Assert that this entire I/O is withing the available range
1105 assert(offset
< _length
);
1106 assert(offset
+ length
<= _length
);
1108 assert( !(kIOMemoryPreparedReadOnly
& _flags
) );
1110 if ( (kIOMemoryPreparedReadOnly
& _flags
) || offset
>= _length
) {
1111 IOLog("IOGMD(%p): wB = o%lx, l%lx\n", this, offset
, length
); // @@@ gvdl
1115 remaining
= length
= min(length
, _length
- offset
);
1116 while (remaining
) { // (process another target segment?)
1120 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1124 // Clip segment length to remaining
1125 if (dstLen
> remaining
)
1128 copypv(srcAddr
, (addr64_t
) dstAddr64
, dstLen
,
1129 cppvPsnk
| cppvFsnk
| cppvNoRefSrc
| cppvNoModSnk
| cppvKmap
);
1133 remaining
-= dstLen
;
1138 return length
- remaining
;
1141 // osfmk/device/iokit_rpc.c
1142 extern "C" unsigned int IODefaultCacheBits(addr64_t pa
);
1144 /* DEPRECATED */ void IOGeneralMemoryDescriptor::setPosition(IOByteCount position
)
1146 panic("IOGMD::setPosition deprecated");
1149 IOReturn
IOGeneralMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
1151 if (kIOMDGetCharacteristics
== op
) {
1153 if (dataSize
< sizeof(IOMDDMACharacteristics
))
1154 return kIOReturnUnderrun
;
1156 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
1157 data
->fLength
= _length
;
1158 data
->fSGCount
= _rangesCount
;
1159 data
->fPages
= _pages
;
1160 data
->fDirection
= _direction
;
1162 data
->fIsPrepared
= false;
1164 data
->fIsPrepared
= true;
1165 data
->fHighestPage
= _highestPage
;
1166 if (_memoryEntries
) {
1167 ioGMDData
*gmdData
= getDataP(_memoryEntries
);
1168 ioPLBlock
*ioplList
= getIOPLList(gmdData
);
1169 UInt count
= getNumIOPL(_memoryEntries
, gmdData
);
1171 data
->fIsMapped
= (gmdData
->fMapper
&& _pages
&& (count
> 0)
1172 && ioplList
[0].fMappedBase
);
1174 data
->fPageAlign
= (ioplList
[0].fPageOffset
& PAGE_MASK
) | ~PAGE_MASK
;
1177 data
->fIsMapped
= false;
1180 return kIOReturnSuccess
;
1182 else if (!(kIOMDWalkSegments
& op
))
1183 return kIOReturnBadArgument
;
1185 // Get the next segment
1186 struct InternalState
{
1187 IOMDDMAWalkSegmentArgs fIO
;
1193 // Find the next segment
1194 if (dataSize
< sizeof(*isP
))
1195 return kIOReturnUnderrun
;
1197 isP
= (InternalState
*) vData
;
1198 UInt offset
= isP
->fIO
.fOffset
;
1199 bool mapped
= isP
->fIO
.fMapped
;
1201 if (offset
>= _length
)
1202 return (offset
== _length
)? kIOReturnOverrun
: kIOReturnInternalError
;
1204 // Validate the previous offset
1205 UInt ind
, off2Ind
= isP
->fOffset2Index
;
1206 if ((kIOMDFirstSegment
!= op
)
1208 && (offset
== isP
->fNextOffset
|| off2Ind
<= offset
))
1211 ind
= off2Ind
= 0; // Start from beginning
1215 if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
) {
1217 // Physical address based memory descriptor
1218 const IOPhysicalRange
*physP
= (IOPhysicalRange
*) &_ranges
.p
[0];
1220 // Find the range after the one that contains the offset
1222 for (len
= 0; off2Ind
<= offset
; ind
++) {
1223 len
= physP
[ind
].length
;
1227 // Calculate length within range and starting address
1228 length
= off2Ind
- offset
;
1229 address
= physP
[ind
- 1].address
+ len
- length
;
1231 // see how far we can coalesce ranges
1232 while (ind
< _rangesCount
&& address
+ length
== physP
[ind
].address
) {
1233 len
= physP
[ind
].length
;
1239 // correct contiguous check overshoot
1243 else if ( (_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
) {
1245 // Physical address based memory descriptor
1246 const IOAddressRange
*physP
= (IOAddressRange
*) &_ranges
.v64
[0];
1248 // Find the range after the one that contains the offset
1250 for (len
= 0; off2Ind
<= offset
; ind
++) {
1251 len
= physP
[ind
].length
;
1255 // Calculate length within range and starting address
1256 length
= off2Ind
- offset
;
1257 address
= physP
[ind
- 1].address
+ len
- length
;
1259 // see how far we can coalesce ranges
1260 while (ind
< _rangesCount
&& address
+ length
== physP
[ind
].address
) {
1261 len
= physP
[ind
].length
;
1267 // correct contiguous check overshoot
1273 panic("IOGMD: not wired for the IODMACommand");
1275 assert(_memoryEntries
);
1277 ioGMDData
* dataP
= getDataP(_memoryEntries
);
1278 const ioPLBlock
*ioplList
= getIOPLList(dataP
);
1279 UInt numIOPLs
= getNumIOPL(_memoryEntries
, dataP
);
1280 upl_page_info_t
*pageList
= getPageList(dataP
);
1282 assert(numIOPLs
> 0);
1284 // Scan through iopl info blocks looking for block containing offset
1285 while (ind
< numIOPLs
&& offset
>= ioplList
[ind
].fIOMDOffset
)
1288 // Go back to actual range as search goes past it
1289 ioPLBlock ioplInfo
= ioplList
[ind
- 1];
1290 off2Ind
= ioplInfo
.fIOMDOffset
;
1293 length
= ioplList
[ind
].fIOMDOffset
;
1296 length
-= offset
; // Remainder within iopl
1298 // Subtract offset till this iopl in total list
1301 // If a mapped address is requested and this is a pre-mapped IOPL
1302 // then just need to compute an offset relative to the mapped base.
1303 if (mapped
&& ioplInfo
.fMappedBase
) {
1304 offset
+= (ioplInfo
.fPageOffset
& PAGE_MASK
);
1305 address
= ptoa_64(ioplInfo
.fMappedBase
) + offset
;
1306 continue; // Done leave do/while(false) now
1309 // The offset is rebased into the current iopl.
1310 // Now add the iopl 1st page offset.
1311 offset
+= ioplInfo
.fPageOffset
;
1313 // For external UPLs the fPageInfo field points directly to
1314 // the upl's upl_page_info_t array.
1315 if (ioplInfo
.fFlags
& kIOPLExternUPL
)
1316 pageList
= (upl_page_info_t
*) ioplInfo
.fPageInfo
;
1318 pageList
= &pageList
[ioplInfo
.fPageInfo
];
1320 // Check for direct device non-paged memory
1321 if ( ioplInfo
.fFlags
& kIOPLOnDevice
) {
1322 address
= ptoa_64(pageList
->phys_addr
) + offset
;
1323 continue; // Done leave do/while(false) now
1326 // Now we need compute the index into the pageList
1327 UInt pageInd
= atop_32(offset
);
1328 offset
&= PAGE_MASK
;
1330 // Compute the starting address of this segment
1331 IOPhysicalAddress pageAddr
= pageList
[pageInd
].phys_addr
;
1332 address
= ptoa_64(pageAddr
) + offset
;
1334 // length is currently set to the length of the remainider of the iopl.
1335 // We need to check that the remainder of the iopl is contiguous.
1336 // This is indicated by pageList[ind].phys_addr being sequential.
1337 IOByteCount contigLength
= PAGE_SIZE
- offset
;
1338 while (contigLength
< length
1339 && ++pageAddr
== pageList
[++pageInd
].phys_addr
)
1341 contigLength
+= PAGE_SIZE
;
1344 if (contigLength
< length
)
1345 length
= contigLength
;
1353 // Update return values and state
1354 isP
->fIO
.fIOVMAddr
= address
;
1355 isP
->fIO
.fLength
= length
;
1357 isP
->fOffset2Index
= off2Ind
;
1358 isP
->fNextOffset
= isP
->fIO
.fOffset
+ length
;
1360 return kIOReturnSuccess
;
1364 IOGeneralMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1367 IOByteCount length
= 0;
1368 addr64_t address
= 0;
1370 if (offset
< _length
) // (within bounds?)
1372 IOMDDMAWalkSegmentState _state
;
1373 IOMDDMAWalkSegmentArgs
* state
= (IOMDDMAWalkSegmentArgs
*) &_state
;
1375 state
->fOffset
= offset
;
1376 state
->fLength
= _length
- offset
;
1377 state
->fMapped
= false;
1379 ret
= dmaCommandOperation(kIOMDFirstSegment
, _state
, sizeof(_state
));
1381 if ((kIOReturnSuccess
!= ret
) && (kIOReturnOverrun
!= ret
))
1382 DEBG("getPhysicalSegment64 dmaCommandOperation(%lx), %p, offset %qx, addr %qx, len %qx\n",
1383 ret
, this, state
->fOffset
,
1384 state
->fIOVMAddr
, state
->fLength
);
1385 if (kIOReturnSuccess
== ret
)
1387 address
= state
->fIOVMAddr
;
1388 length
= state
->fLength
;
1394 if (lengthOfSegment
)
1395 *lengthOfSegment
= length
;
1401 IOGeneralMemoryDescriptor::getPhysicalSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1404 IOByteCount length
= 0;
1405 addr64_t address
= 0;
1407 // assert(offset <= _length);
1409 if (offset
< _length
) // (within bounds?)
1411 IOMDDMAWalkSegmentState _state
;
1412 IOMDDMAWalkSegmentArgs
* state
= (IOMDDMAWalkSegmentArgs
*) &_state
;
1414 state
->fOffset
= offset
;
1415 state
->fLength
= _length
- offset
;
1416 state
->fMapped
= true;
1418 ret
= dmaCommandOperation(
1419 kIOMDFirstSegment
, _state
, sizeof(_state
));
1421 if ((kIOReturnSuccess
!= ret
) && (kIOReturnOverrun
!= ret
))
1422 DEBG("getPhysicalSegment dmaCommandOperation(%lx), %p, offset %qx, addr %qx, len %qx\n",
1423 ret
, this, state
->fOffset
,
1424 state
->fIOVMAddr
, state
->fLength
);
1425 if (kIOReturnSuccess
== ret
)
1427 address
= state
->fIOVMAddr
;
1428 length
= state
->fLength
;
1435 if ((address
+ length
) > 0x100000000ULL
)
1437 panic("getPhysicalSegment() out of 32b range 0x%qx, len 0x%x, class %s",
1438 address
, length
, (getMetaClass())->getClassName());
1441 if (lengthOfSegment
)
1442 *lengthOfSegment
= length
;
1444 return ((IOPhysicalAddress
) address
);
1448 IOMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1450 IOPhysicalAddress phys32
;
1453 IOMapper
* mapper
= 0;
1455 phys32
= getPhysicalSegment(offset
, lengthOfSegment
);
1459 if (gIOSystemMapper
)
1460 mapper
= gIOSystemMapper
;
1464 IOByteCount origLen
;
1466 phys64
= mapper
->mapAddr(phys32
);
1467 origLen
= *lengthOfSegment
;
1468 length
= page_size
- (phys64
& (page_size
- 1));
1469 while ((length
< origLen
)
1470 && ((phys64
+ length
) == mapper
->mapAddr(phys32
+ length
)))
1471 length
+= page_size
;
1472 if (length
> origLen
)
1475 *lengthOfSegment
= length
;
1478 phys64
= (addr64_t
) phys32
;
1484 IOGeneralMemoryDescriptor::getSourceSegment(IOByteCount offset
, IOByteCount
*lengthOfSegment
)
1486 IOPhysicalAddress address
= 0;
1487 IOPhysicalLength length
= 0;
1488 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1490 assert(offset
<= _length
);
1492 if ( type
== kIOMemoryTypeUPL
)
1493 return super::getSourceSegment( offset
, lengthOfSegment
);
1494 else if ( offset
< _length
) // (within bounds?)
1496 unsigned rangesIndex
= 0;
1497 Ranges vec
= _ranges
;
1500 // Find starting address within the vector of ranges
1502 getAddrLenForInd(addr
, length
, type
, vec
, rangesIndex
);
1503 if (offset
< length
)
1505 offset
-= length
; // (make offset relative)
1509 // Now that we have the starting range,
1510 // lets find the last contiguous range
1514 for ( ++rangesIndex
; rangesIndex
< _rangesCount
; rangesIndex
++ ) {
1515 user_addr_t newAddr
;
1516 IOPhysicalLength newLen
;
1518 getAddrLenForInd(newAddr
, newLen
, type
, vec
, rangesIndex
);
1519 if (addr
+ length
!= newAddr
)
1524 address
= (IOPhysicalAddress
) addr
; // Truncate address to 32bit
1529 if ( lengthOfSegment
) *lengthOfSegment
= length
;
1534 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1535 /* DEPRECATED */ void * IOGeneralMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
1536 /* DEPRECATED */ IOByteCount
* lengthOfSegment
)
1538 if (_task
== kernel_task
)
1539 return (void *) getSourceSegment(offset
, lengthOfSegment
);
1541 panic("IOGMD::getVirtualSegment deprecated");
1545 /* DEPRECATED */ /* USE INSTEAD: map(), readBytes(), writeBytes() */
1550 IOMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
1552 if (kIOMDGetCharacteristics
== op
) {
1553 if (dataSize
< sizeof(IOMDDMACharacteristics
))
1554 return kIOReturnUnderrun
;
1556 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
1557 data
->fLength
= getLength();
1559 data
->fDirection
= _direction
;
1560 if (IOMapper::gSystem
)
1561 data
->fIsMapped
= true;
1562 data
->fIsPrepared
= true; // Assume prepared - fails safe
1564 else if (kIOMDWalkSegments
& op
) {
1565 if (dataSize
< sizeof(IOMDDMAWalkSegmentArgs
))
1566 return kIOReturnUnderrun
;
1568 IOMDDMAWalkSegmentArgs
*data
= (IOMDDMAWalkSegmentArgs
*) vData
;
1569 IOByteCount offset
= (IOByteCount
) data
->fOffset
;
1571 IOPhysicalLength length
;
1572 IOMemoryDescriptor
*ncmd
= const_cast<IOMemoryDescriptor
*>(this);
1573 if (data
->fMapped
&& IOMapper::gSystem
)
1574 data
->fIOVMAddr
= ncmd
->getPhysicalSegment(offset
, &length
);
1576 data
->fIOVMAddr
= ncmd
->getPhysicalSegment64(offset
, &length
);
1577 data
->fLength
= length
;
1580 return kIOReturnBadArgument
;
1582 return kIOReturnSuccess
;
1585 IOReturn
IOMemoryDescriptor::setPurgeable( IOOptionBits newState
,
1586 IOOptionBits
* oldState
)
1588 IOReturn err
= kIOReturnSuccess
;
1589 vm_purgable_t control
;
1596 err
= kIOReturnNotReady
;
1600 control
= VM_PURGABLE_SET_STATE
;
1603 case kIOMemoryPurgeableKeepCurrent
:
1604 control
= VM_PURGABLE_GET_STATE
;
1607 case kIOMemoryPurgeableNonVolatile
:
1608 state
= VM_PURGABLE_NONVOLATILE
;
1610 case kIOMemoryPurgeableVolatile
:
1611 state
= VM_PURGABLE_VOLATILE
;
1613 case kIOMemoryPurgeableEmpty
:
1614 state
= VM_PURGABLE_EMPTY
;
1617 err
= kIOReturnBadArgument
;
1621 if (kIOReturnSuccess
!= err
)
1624 err
= mach_memory_entry_purgable_control((ipc_port_t
) _memEntry
, control
, &state
);
1628 if (kIOReturnSuccess
== err
)
1632 case VM_PURGABLE_NONVOLATILE
:
1633 state
= kIOMemoryPurgeableNonVolatile
;
1635 case VM_PURGABLE_VOLATILE
:
1636 state
= kIOMemoryPurgeableVolatile
;
1638 case VM_PURGABLE_EMPTY
:
1639 state
= kIOMemoryPurgeableEmpty
;
1642 state
= kIOMemoryPurgeableNonVolatile
;
1643 err
= kIOReturnNotReady
;
1655 extern "C" void dcache_incoherent_io_flush64(addr64_t pa
, unsigned int count
);
1656 extern "C" void dcache_incoherent_io_store64(addr64_t pa
, unsigned int count
);
1658 IOReturn
IOMemoryDescriptor::performOperation( IOOptionBits options
,
1659 IOByteCount offset
, IOByteCount length
)
1661 IOByteCount remaining
;
1662 void (*func
)(addr64_t pa
, unsigned int count
) = 0;
1666 case kIOMemoryIncoherentIOFlush
:
1667 func
= &dcache_incoherent_io_flush64
;
1669 case kIOMemoryIncoherentIOStore
:
1670 func
= &dcache_incoherent_io_store64
;
1675 return (kIOReturnUnsupported
);
1677 remaining
= length
= min(length
, getLength() - offset
);
1679 // (process another target segment?)
1684 dstAddr64
= getPhysicalSegment64(offset
, &dstLen
);
1688 // Clip segment length to remaining
1689 if (dstLen
> remaining
)
1692 (*func
)(dstAddr64
, dstLen
);
1695 remaining
-= dstLen
;
1698 return (remaining
? kIOReturnUnderrun
: kIOReturnSuccess
);
1702 extern vm_offset_t static_memory_end
;
1703 #define io_kernel_static_end static_memory_end
1705 extern vm_offset_t first_avail
;
1706 #define io_kernel_static_end first_avail
1709 static kern_return_t
1710 io_get_kernel_static_upl(
1712 vm_address_t offset
,
1713 vm_size_t
*upl_size
,
1715 upl_page_info_array_t page_list
,
1716 unsigned int *count
,
1717 ppnum_t
*highest_page
)
1719 unsigned int pageCount
, page
;
1721 ppnum_t highestPage
= 0;
1723 pageCount
= atop_32(*upl_size
);
1724 if (pageCount
> *count
)
1729 for (page
= 0; page
< pageCount
; page
++)
1731 phys
= pmap_find_phys(kernel_pmap
, ((addr64_t
)offset
) + ptoa_64(page
));
1734 page_list
[page
].phys_addr
= phys
;
1735 page_list
[page
].pageout
= 0;
1736 page_list
[page
].absent
= 0;
1737 page_list
[page
].dirty
= 0;
1738 page_list
[page
].precious
= 0;
1739 page_list
[page
].device
= 0;
1740 if (phys
> highestPage
)
1744 *highest_page
= highestPage
;
1746 return ((page
>= pageCount
) ? kIOReturnSuccess
: kIOReturnVMError
);
1749 IOReturn
IOGeneralMemoryDescriptor::wireVirtual(IODirection forDirection
)
1751 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1752 IOReturn error
= kIOReturnNoMemory
;
1754 ppnum_t mapBase
= 0;
1756 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
1758 assert(!_wireCount
);
1759 assert(kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
);
1761 if (_pages
>= gIOMaximumMappedIOPageCount
)
1762 return kIOReturnNoResources
;
1764 dataP
= getDataP(_memoryEntries
);
1765 mapper
= dataP
->fMapper
;
1766 if (mapper
&& _pages
)
1767 mapBase
= mapper
->iovmAlloc(_pages
);
1769 // Note that appendBytes(NULL) zeros the data up to the
1771 _memoryEntries
->appendBytes(0, dataP
->fPageCnt
* sizeof(upl_page_info_t
));
1772 dataP
= 0; // May no longer be valid so lets not get tempted.
1774 if (forDirection
== kIODirectionNone
)
1775 forDirection
= _direction
;
1777 int uplFlags
; // This Mem Desc's default flags for upl creation
1778 switch (kIODirectionOutIn
& forDirection
)
1780 case kIODirectionOut
:
1781 // Pages do not need to be marked as dirty on commit
1782 uplFlags
= UPL_COPYOUT_FROM
;
1783 _flags
|= kIOMemoryPreparedReadOnly
;
1786 case kIODirectionIn
:
1788 uplFlags
= 0; // i.e. ~UPL_COPYOUT_FROM
1791 uplFlags
|= UPL_SET_IO_WIRE
| UPL_SET_LITE
;
1793 #ifdef UPL_NEED_32BIT_ADDR
1794 if (kIODirectionPrepareToPhys32
& forDirection
)
1795 uplFlags
|= UPL_NEED_32BIT_ADDR
;
1798 // Find the appropriate vm_map for the given task
1800 if (_task
== kernel_task
&& (kIOMemoryBufferPageable
& _flags
))
1803 { curMap
= get_task_map(_task
); }
1805 // Iterate over the vector of virtual ranges
1806 Ranges vec
= _ranges
;
1807 unsigned int pageIndex
= 0;
1808 IOByteCount mdOffset
= 0;
1809 ppnum_t highestPage
= 0;
1810 for (UInt range
= 0; range
< _rangesCount
; range
++) {
1812 user_addr_t startPage
;
1813 IOByteCount numBytes
;
1814 ppnum_t highPage
= 0;
1816 // Get the startPage address and length of vec[range]
1817 getAddrLenForInd(startPage
, numBytes
, type
, vec
, range
);
1818 iopl
.fPageOffset
= (short) startPage
& PAGE_MASK
;
1819 numBytes
+= iopl
.fPageOffset
;
1820 startPage
= trunc_page_64(startPage
);
1823 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1825 iopl
.fMappedBase
= 0;
1827 // Iterate over the current range, creating UPLs
1829 dataP
= getDataP(_memoryEntries
);
1830 vm_address_t kernelStart
= (vm_address_t
) startPage
;
1834 else if (!sharedMem
) {
1835 assert(_task
== kernel_task
);
1836 theMap
= IOPageableMapForAddress(kernelStart
);
1841 upl_page_info_array_t pageInfo
= getPageList(dataP
);
1842 int ioplFlags
= uplFlags
;
1843 upl_page_list_ptr_t baseInfo
= &pageInfo
[pageIndex
];
1845 vm_size_t ioplSize
= round_page_32(numBytes
);
1846 unsigned int numPageInfo
= atop_32(ioplSize
);
1848 if (theMap
== kernel_map
&& kernelStart
< io_kernel_static_end
) {
1849 error
= io_get_kernel_static_upl(theMap
,
1857 else if (sharedMem
) {
1858 error
= memory_object_iopl_request(sharedMem
,
1868 error
= vm_map_create_upl(theMap
,
1878 if (error
!= KERN_SUCCESS
)
1882 highPage
= upl_get_highest_page(iopl
.fIOPL
);
1883 if (highPage
> highestPage
)
1884 highestPage
= highPage
;
1886 error
= kIOReturnNoMemory
;
1888 if (baseInfo
->device
) {
1890 iopl
.fFlags
= kIOPLOnDevice
;
1891 // Don't translate device memory at all
1892 if (mapper
&& mapBase
) {
1893 mapper
->iovmFree(mapBase
, _pages
);
1895 iopl
.fMappedBase
= 0;
1901 mapper
->iovmInsert(mapBase
, pageIndex
,
1902 baseInfo
, numPageInfo
);
1905 iopl
.fIOMDOffset
= mdOffset
;
1906 iopl
.fPageInfo
= pageIndex
;
1908 if ((_flags
& kIOMemoryAutoPrepare
) && iopl
.fIOPL
)
1910 upl_commit(iopl
.fIOPL
, 0, 0);
1911 upl_deallocate(iopl
.fIOPL
);
1915 if (!_memoryEntries
->appendBytes(&iopl
, sizeof(iopl
))) {
1916 // Clean up partial created and unsaved iopl
1918 upl_abort(iopl
.fIOPL
, 0);
1919 upl_deallocate(iopl
.fIOPL
);
1924 // Check for a multiple iopl's in one virtual range
1925 pageIndex
+= numPageInfo
;
1926 mdOffset
-= iopl
.fPageOffset
;
1927 if (ioplSize
< numBytes
) {
1928 numBytes
-= ioplSize
;
1929 startPage
+= ioplSize
;
1930 mdOffset
+= ioplSize
;
1931 iopl
.fPageOffset
= 0;
1933 iopl
.fMappedBase
= mapBase
+ pageIndex
;
1936 mdOffset
+= numBytes
;
1942 _highestPage
= highestPage
;
1944 return kIOReturnSuccess
;
1948 dataP
= getDataP(_memoryEntries
);
1949 UInt done
= getNumIOPL(_memoryEntries
, dataP
);
1950 ioPLBlock
*ioplList
= getIOPLList(dataP
);
1952 for (UInt range
= 0; range
< done
; range
++)
1954 if (ioplList
[range
].fIOPL
) {
1955 upl_abort(ioplList
[range
].fIOPL
, 0);
1956 upl_deallocate(ioplList
[range
].fIOPL
);
1959 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
1961 if (mapper
&& mapBase
)
1962 mapper
->iovmFree(mapBase
, _pages
);
1971 * Prepare the memory for an I/O transfer. This involves paging in
1972 * the memory, if necessary, and wiring it down for the duration of
1973 * the transfer. The complete() method completes the processing of
1974 * the memory after the I/O transfer finishes. This method needn't
1975 * called for non-pageable memory.
1977 IOReturn
IOGeneralMemoryDescriptor::prepare(IODirection forDirection
)
1979 IOReturn error
= kIOReturnSuccess
;
1980 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
1983 && (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
) ) {
1984 error
= wireVirtual(forDirection
);
1991 return kIOReturnSuccess
;
1997 * Complete processing of the memory after an I/O transfer finishes.
1998 * This method should not be called unless a prepare was previously
1999 * issued; the prepare() and complete() must occur in pairs, before
2000 * before and after an I/O transfer involving pageable memory.
2003 IOReturn
IOGeneralMemoryDescriptor::complete(IODirection
/* forDirection */)
2008 return kIOReturnSuccess
;
2012 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2014 if ((kIOMemoryTypePhysical
== type
) || (kIOMemoryTypePhysical64
== type
)) {
2015 /* kIOMemoryTypePhysical */
2019 ioGMDData
* dataP
= getDataP(_memoryEntries
);
2020 ioPLBlock
*ioplList
= getIOPLList(dataP
);
2021 UInt count
= getNumIOPL(_memoryEntries
, dataP
);
2023 if (dataP
->fMapper
&& _pages
&& ioplList
[0].fMappedBase
)
2024 dataP
->fMapper
->iovmFree(ioplList
[0].fMappedBase
, _pages
);
2026 // Only complete iopls that we created which are for TypeVirtual
2027 if (kIOMemoryTypeVirtual
== type
|| kIOMemoryTypeVirtual64
== type
|| kIOMemoryTypeUIO
== type
) {
2028 for (UInt ind
= 0; ind
< count
; ind
++)
2029 if (ioplList
[ind
].fIOPL
) {
2030 upl_commit(ioplList
[ind
].fIOPL
, 0, 0);
2031 upl_deallocate(ioplList
[ind
].fIOPL
);
2035 (void) _memoryEntries
->initWithBytes(dataP
, sizeof(ioGMDData
)); // == setLength()
2038 return kIOReturnSuccess
;
2041 IOReturn
IOGeneralMemoryDescriptor::doMap(
2042 vm_map_t addressMap
,
2043 IOVirtualAddress
* atAddress
,
2044 IOOptionBits options
,
2045 IOByteCount sourceOffset
,
2046 IOByteCount length
)
2049 ipc_port_t sharedMem
= (ipc_port_t
) _memEntry
;
2051 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2052 Ranges vec
= _ranges
;
2054 user_addr_t range0Addr
= 0;
2055 IOByteCount range0Len
= 0;
2058 getAddrLenForInd(range0Addr
, range0Len
, type
, vec
, 0);
2060 // mapping source == dest? (could be much better)
2062 && (addressMap
== get_task_map(_task
)) && (options
& kIOMapAnywhere
)
2063 && (1 == _rangesCount
) && (0 == sourceOffset
)
2064 && range0Addr
&& (length
<= range0Len
) ) {
2065 if (sizeof(user_addr_t
) > 4 && ((UInt64
) range0Addr
) >> 32)
2066 return kIOReturnOverrun
; // Doesn't fit in 32bit return field
2068 *atAddress
= range0Addr
;
2069 return( kIOReturnSuccess
);
2073 if( 0 == sharedMem
) {
2075 vm_size_t size
= ptoa_32(_pages
);
2079 memory_object_size_t actualSize
= size
;
2080 kr
= mach_make_memory_entry_64(get_task_map(_task
),
2081 &actualSize
, range0Addr
,
2082 VM_PROT_READ
| VM_PROT_WRITE
, &sharedMem
,
2085 if( (KERN_SUCCESS
== kr
) && (actualSize
!= round_page_32(size
))) {
2087 IOLog("mach_make_memory_entry_64 (%08llx) size (%08lx:%08x)\n",
2088 range0Addr
, (UInt32
) actualSize
, size
);
2090 kr
= kIOReturnVMError
;
2091 ipc_port_release_send( sharedMem
);
2094 if( KERN_SUCCESS
!= kr
)
2095 sharedMem
= MACH_PORT_NULL
;
2097 } else do { // _task == 0, must be physical
2099 memory_object_t pager
;
2100 unsigned int flags
= 0;
2102 IOPhysicalLength segLen
;
2104 pa
= getPhysicalSegment64( sourceOffset
, &segLen
);
2107 reserved
= IONew( ExpansionData
, 1 );
2111 reserved
->pagerContig
= (1 == _rangesCount
);
2112 reserved
->memory
= this;
2114 /*What cache mode do we need*/
2115 switch(options
& kIOMapCacheMask
) {
2117 case kIOMapDefaultCache
:
2119 flags
= IODefaultCacheBits(pa
);
2122 case kIOMapInhibitCache
:
2123 flags
= DEVICE_PAGER_CACHE_INHIB
|
2124 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
2127 case kIOMapWriteThruCache
:
2128 flags
= DEVICE_PAGER_WRITE_THROUGH
|
2129 DEVICE_PAGER_COHERENT
| DEVICE_PAGER_GUARDED
;
2132 case kIOMapCopybackCache
:
2133 flags
= DEVICE_PAGER_COHERENT
;
2136 case kIOMapWriteCombineCache
:
2137 flags
= DEVICE_PAGER_CACHE_INHIB
|
2138 DEVICE_PAGER_COHERENT
;
2142 flags
|= reserved
->pagerContig
? DEVICE_PAGER_CONTIGUOUS
: 0;
2144 pager
= device_pager_setup( (memory_object_t
) 0, (int) reserved
,
2149 kr
= mach_memory_object_memory_entry_64( (host_t
) 1, false /*internal*/,
2150 size
, VM_PROT_READ
| VM_PROT_WRITE
, pager
, &sharedMem
);
2152 assert( KERN_SUCCESS
== kr
);
2153 if( KERN_SUCCESS
!= kr
) {
2154 device_pager_deallocate( pager
);
2155 pager
= MACH_PORT_NULL
;
2156 sharedMem
= MACH_PORT_NULL
;
2159 if( pager
&& sharedMem
)
2160 reserved
->devicePager
= pager
;
2162 IODelete( reserved
, ExpansionData
, 1 );
2168 _memEntry
= (void *) sharedMem
;
2173 kr
= kIOReturnVMError
;
2175 kr
= super::doMap( addressMap
, atAddress
,
2176 options
, sourceOffset
, length
);
2181 IOReturn
IOGeneralMemoryDescriptor::doUnmap(
2182 vm_map_t addressMap
,
2183 IOVirtualAddress logical
,
2184 IOByteCount length
)
2186 // could be much better
2187 if( _task
&& (addressMap
== get_task_map(_task
)) && (1 == _rangesCount
)) {
2189 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
2190 user_addr_t range0Addr
;
2191 IOByteCount range0Len
;
2193 getAddrLenForInd(range0Addr
, range0Len
, type
, _ranges
, 0);
2194 if (logical
== range0Addr
&& length
<= range0Len
)
2195 return( kIOReturnSuccess
);
2198 return( super::doUnmap( addressMap
, logical
, length
));
2201 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2203 OSDefineMetaClassAndAbstractStructors( IOMemoryMap
, OSObject
)
2205 /* inline function implementation */
2206 IOPhysicalAddress
IOMemoryMap::getPhysicalAddress()
2207 { return( getPhysicalSegment( 0, 0 )); }
2211 #define super IOMemoryMap
2213 OSDefineMetaClassAndStructors(_IOMemoryMap
, IOMemoryMap
)
2215 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2217 bool _IOMemoryMap::initCompatible(
2218 IOMemoryDescriptor
* _memory
,
2219 IOMemoryMap
* _superMap
,
2220 IOByteCount _offset
,
2221 IOByteCount _length
)
2227 if( (_offset
+ _length
) > _superMap
->getLength())
2232 _superMap
->retain();
2233 superMap
= _superMap
;
2239 length
= _memory
->getLength();
2241 options
= superMap
->getMapOptions();
2242 logical
= superMap
->getVirtualAddress() + offset
;
2247 bool _IOMemoryMap::initWithDescriptor(
2248 IOMemoryDescriptor
* _memory
,
2250 IOVirtualAddress toAddress
,
2251 IOOptionBits _options
,
2252 IOByteCount _offset
,
2253 IOByteCount _length
)
2256 bool redir
= ((kIOMapUnique
|kIOMapReference
) == ((kIOMapUnique
|kIOMapReference
) & _options
));
2258 if ((!_memory
) || (!intoTask
))
2261 if( (_offset
+ _length
) > _memory
->getLength())
2268 addressMap
= get_task_map(intoTask
);
2271 vm_map_reference(addressMap
);
2272 addressTask
= intoTask
;
2273 logical
= toAddress
;
2283 length
= _memory
->getLength();
2285 if( options
& kIOMapStatic
)
2288 ok
= (kIOReturnSuccess
== _memory
->doMap( addressMap
, &toAddress
,
2289 _options
, offset
, length
));
2295 logical
= toAddress
;
2304 vm_map_deallocate(addressMap
);
2312 /* LP64todo - these need to expand */
2313 struct IOMemoryDescriptorMapAllocRef
2315 ipc_port_t sharedMem
;
2318 IOByteCount sourceOffset
;
2319 IOOptionBits options
;
2322 static kern_return_t
IOMemoryDescriptorMapAlloc(vm_map_t map
, void * _ref
)
2324 IOMemoryDescriptorMapAllocRef
* ref
= (IOMemoryDescriptorMapAllocRef
*)_ref
;
2328 if( ref
->sharedMem
) {
2329 vm_prot_t prot
= VM_PROT_READ
2330 | ((ref
->options
& kIOMapReadOnly
) ? 0 : VM_PROT_WRITE
);
2332 // set memory entry cache
2333 vm_prot_t memEntryCacheMode
= prot
| MAP_MEM_ONLY
;
2334 switch (ref
->options
& kIOMapCacheMask
)
2336 case kIOMapInhibitCache
:
2337 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
2340 case kIOMapWriteThruCache
:
2341 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
2344 case kIOMapWriteCombineCache
:
2345 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
2348 case kIOMapCopybackCache
:
2349 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
2352 case kIOMapDefaultCache
:
2354 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
2358 vm_size_t unused
= 0;
2360 err
= mach_make_memory_entry( NULL
/*unused*/, &unused
, 0 /*unused*/,
2361 memEntryCacheMode
, NULL
, ref
->sharedMem
);
2362 if (KERN_SUCCESS
!= err
)
2363 IOLog("MAP_MEM_ONLY failed %d\n", err
);
2367 ref
->size
, 0 /* mask */,
2368 (( ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2369 | VM_MAKE_TAG(VM_MEMORY_IOKIT
),
2370 ref
->sharedMem
, ref
->sourceOffset
,
2376 if( KERN_SUCCESS
!= err
) {
2383 err
= vm_allocate( map
, &ref
->mapped
, ref
->size
,
2384 ((ref
->options
& kIOMapAnywhere
) ? VM_FLAGS_ANYWHERE
: VM_FLAGS_FIXED
)
2385 | VM_MAKE_TAG(VM_MEMORY_IOKIT
) );
2387 if( KERN_SUCCESS
!= err
) {
2392 // we have to make sure that these guys don't get copied if we fork.
2393 err
= vm_inherit( map
, ref
->mapped
, ref
->size
, VM_INHERIT_NONE
);
2394 assert( KERN_SUCCESS
== err
);
2403 IOReturn
IOMemoryDescriptor::doMap(
2404 vm_map_t addressMap
,
2405 IOVirtualAddress
* atAddress
,
2406 IOOptionBits options
,
2407 IOByteCount sourceOffset
,
2408 IOByteCount length
)
2410 IOReturn err
= kIOReturnSuccess
;
2411 memory_object_t pager
;
2412 vm_address_t logical
;
2413 IOByteCount pageOffset
;
2414 IOPhysicalAddress sourceAddr
;
2415 IOMemoryDescriptorMapAllocRef ref
;
2417 ref
.sharedMem
= (ipc_port_t
) _memEntry
;
2418 ref
.sourceOffset
= sourceOffset
;
2419 ref
.options
= options
;
2424 length
= getLength();
2426 sourceAddr
= getSourceSegment( sourceOffset
, NULL
);
2427 pageOffset
= sourceAddr
- trunc_page_32( sourceAddr
);
2429 ref
.size
= round_page_32( length
+ pageOffset
);
2431 if ((kIOMapReference
|kIOMapUnique
) == ((kIOMapReference
|kIOMapUnique
) & options
))
2437 _IOMemoryMap
* mapping
= (_IOMemoryMap
*) *atAddress
;
2438 ref
.mapped
= mapping
->getVirtualAddress();
2442 err
= kIOReturnNotReadable
;
2447 flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
2448 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
2450 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) _memEntry
, 0, &size
, &redirUPL2
,
2455 err
= upl_transpose(redirUPL2
, mapping
->redirUPL
);
2456 if (kIOReturnSuccess
!= err
)
2458 IOLog("upl_transpose(%x)\n", err
);
2459 err
= kIOReturnSuccess
;
2464 upl_commit(redirUPL2
, NULL
, 0);
2465 upl_deallocate(redirUPL2
);
2469 // swap the memEntries since they now refer to different vm_objects
2470 void * me
= _memEntry
;
2471 _memEntry
= mapping
->memory
->_memEntry
;
2472 mapping
->memory
->_memEntry
= me
;
2478 logical
= *atAddress
;
2479 if( options
& kIOMapAnywhere
)
2480 // vm_map looks for addresses above here, even when VM_FLAGS_ANYWHERE
2483 ref
.mapped
= trunc_page_32( logical
);
2484 if( (logical
- ref
.mapped
) != pageOffset
) {
2485 err
= kIOReturnVMError
;
2490 if( ref
.sharedMem
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2491 err
= IOIteratePageableMaps( ref
.size
, &IOMemoryDescriptorMapAlloc
, &ref
);
2493 err
= IOMemoryDescriptorMapAlloc( addressMap
, &ref
);
2496 if( err
!= KERN_SUCCESS
)
2500 pager
= (memory_object_t
) reserved
->devicePager
;
2502 pager
= MACH_PORT_NULL
;
2504 if( !ref
.sharedMem
|| pager
)
2505 err
= handleFault( pager
, addressMap
, ref
.mapped
, sourceOffset
, length
, options
);
2509 if( err
!= KERN_SUCCESS
) {
2511 doUnmap( addressMap
, ref
.mapped
, ref
.size
);
2514 *atAddress
= ref
.mapped
+ pageOffset
;
2520 kIOMemoryRedirected
= 0x00010000
2523 IOReturn
IOMemoryDescriptor::handleFault(
2525 vm_map_t addressMap
,
2526 IOVirtualAddress address
,
2527 IOByteCount sourceOffset
,
2529 IOOptionBits options
)
2531 IOReturn err
= kIOReturnSuccess
;
2532 memory_object_t pager
= (memory_object_t
) _pager
;
2536 IOByteCount pageOffset
;
2537 IOByteCount pagerOffset
;
2538 IOPhysicalLength segLen
;
2543 if( kIOMemoryRedirected
& _flags
) {
2545 IOLog("sleep mem redirect %p, %lx\n", this, sourceOffset
);
2549 } while( kIOMemoryRedirected
& _flags
);
2552 return( kIOReturnSuccess
);
2555 physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
);
2557 pageOffset
= physAddr
- trunc_page_64( physAddr
);
2558 pagerOffset
= sourceOffset
;
2560 size
= length
+ pageOffset
;
2561 physAddr
-= pageOffset
;
2563 segLen
+= pageOffset
;
2566 // in the middle of the loop only map whole pages
2567 if( segLen
>= bytes
)
2569 else if( segLen
!= trunc_page_32( segLen
))
2570 err
= kIOReturnVMError
;
2571 if( physAddr
!= trunc_page_64( physAddr
))
2572 err
= kIOReturnBadArgument
;
2575 if( kIOLogMapping
& gIOKitDebug
)
2576 IOLog("_IOMemoryMap::map(%p) %08lx->%08qx:%08lx\n",
2577 addressMap
, address
+ pageOffset
, physAddr
+ pageOffset
,
2578 segLen
- pageOffset
);
2582 if( reserved
&& reserved
->pagerContig
) {
2583 IOPhysicalLength allLen
;
2586 allPhys
= getPhysicalSegment64( 0, &allLen
);
2588 err
= device_pager_populate_object( pager
, 0, allPhys
>> PAGE_SHIFT
, round_page_32(allLen
) );
2593 (page
< segLen
) && (KERN_SUCCESS
== err
);
2594 page
+= page_size
) {
2595 err
= device_pager_populate_object(pager
, pagerOffset
,
2596 (ppnum_t
)((physAddr
+ page
) >> PAGE_SHIFT
), page_size
);
2597 pagerOffset
+= page_size
;
2600 assert( KERN_SUCCESS
== err
);
2606 /* *** Temporary Workaround *** */
2608 /* This call to vm_fault causes an early pmap level resolution */
2609 /* of the mappings created above. Need for this is in absolute */
2610 /* violation of the basic tenet that the pmap layer is a cache. */
2611 /* Further, it implies a serious I/O architectural violation on */
2612 /* the part of some user of the mapping. As of this writing, */
2613 /* the call to vm_fault is needed because the NVIDIA driver */
2614 /* makes a call to pmap_extract. The NVIDIA driver needs to be */
2615 /* fixed as soon as possible. The NVIDIA driver should not */
2616 /* need to query for this info as it should know from the doMap */
2617 /* call where the physical memory is mapped. When a query is */
2618 /* necessary to find a physical mapping, it should be done */
2619 /* through an iokit call which includes the mapped memory */
2620 /* handle. This is required for machine architecture independence.*/
2622 if(!(kIOMemoryRedirected
& _flags
)) {
2623 vm_fault(addressMap
,
2624 (vm_map_offset_t
)address
,
2625 VM_PROT_READ
|VM_PROT_WRITE
,
2626 FALSE
, THREAD_UNINT
, NULL
,
2627 (vm_map_offset_t
)0);
2630 /* *** Temporary Workaround *** */
2633 sourceOffset
+= segLen
- pageOffset
;
2639 && (physAddr
= getPhysicalSegment64( sourceOffset
, &segLen
)));
2642 err
= kIOReturnBadArgument
;
2647 IOReturn
IOMemoryDescriptor::doUnmap(
2648 vm_map_t addressMap
,
2649 IOVirtualAddress logical
,
2650 IOByteCount length
)
2655 if( kIOLogMapping
& gIOKitDebug
)
2656 kprintf("IOMemoryDescriptor::doUnmap(%x) %08x:%08x\n",
2657 addressMap
, logical
, length
);
2660 if( true /* && (addressMap == kernel_map) || (addressMap == get_task_map(current_task()))*/) {
2662 if( _memEntry
&& (addressMap
== kernel_map
) && (kIOMemoryBufferPageable
& _flags
))
2663 addressMap
= IOPageableMapForAddress( logical
);
2665 err
= vm_deallocate( addressMap
, logical
, length
);
2668 err
= kIOReturnSuccess
;
2673 IOReturn
IOMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2675 IOReturn err
= kIOReturnSuccess
;
2676 _IOMemoryMap
* mapping
= 0;
2682 _flags
|= kIOMemoryRedirected
;
2684 _flags
&= ~kIOMemoryRedirected
;
2687 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
2688 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject()))
2689 mapping
->redirect( safeTask
, doRedirect
);
2702 // temporary binary compatibility
2703 IOSubMemoryDescriptor
* subMem
;
2704 if( (subMem
= OSDynamicCast( IOSubMemoryDescriptor
, this)))
2705 err
= subMem
->redirect( safeTask
, doRedirect
);
2707 err
= kIOReturnSuccess
;
2712 IOReturn
IOSubMemoryDescriptor::redirect( task_t safeTask
, bool doRedirect
)
2714 return( _parent
->redirect( safeTask
, doRedirect
));
2717 IOReturn
_IOMemoryMap::redirect( task_t safeTask
, bool doRedirect
)
2719 IOReturn err
= kIOReturnSuccess
;
2722 // err = ((_IOMemoryMap *)superMap)->redirect( safeTask, doRedirect );
2734 if ((!safeTask
|| (get_task_map(safeTask
) != addressMap
))
2735 && (0 == (options
& kIOMapStatic
)))
2737 IOUnmapPages( addressMap
, logical
, length
);
2738 if(!doRedirect
&& safeTask
2739 && (((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2740 || ((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
)))
2742 err
= vm_deallocate( addressMap
, logical
, length
);
2743 err
= memory
->doMap( addressMap
, &logical
,
2744 (options
& ~kIOMapAnywhere
) /*| kIOMapReserve*/,
2747 err
= kIOReturnSuccess
;
2749 IOLog("IOMemoryMap::redirect(%d, %p) %x:%lx from %p\n", doRedirect
, this, logical
, length
, addressMap
);
2752 else if (kIOMapWriteCombineCache
== (options
& kIOMapCacheMask
))
2754 IOOptionBits newMode
;
2755 newMode
= (options
& ~kIOMapCacheMask
) | (doRedirect
? kIOMapInhibitCache
: kIOMapWriteCombineCache
);
2756 IOProtectCacheMode(addressMap
, logical
, length
, newMode
);
2764 if ((((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
2765 || ((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
2767 && (doRedirect
!= (0 != (memory
->_flags
& kIOMemoryRedirected
))))
2768 memory
->redirect(safeTask
, doRedirect
);
2773 IOReturn
_IOMemoryMap::unmap( void )
2779 if( logical
&& addressMap
&& (0 == superMap
)
2780 && (0 == (options
& kIOMapStatic
))) {
2782 err
= memory
->doUnmap( addressMap
, logical
, length
);
2783 vm_map_deallocate(addressMap
);
2787 err
= kIOReturnSuccess
;
2796 void _IOMemoryMap::taskDied( void )
2800 vm_map_deallocate(addressMap
);
2808 // Overload the release mechanism. All mappings must be a member
2809 // of a memory descriptors _mappings set. This means that we
2810 // always have 2 references on a mapping. When either of these mappings
2811 // are released we need to free ourselves.
2812 void _IOMemoryMap::taggedRelease(const void *tag
) const
2815 super::taggedRelease(tag
, 2);
2819 void _IOMemoryMap::free()
2825 memory
->removeMapping( this);
2830 if (owner
&& (owner
!= memory
))
2833 owner
->removeMapping(this);
2838 superMap
->release();
2841 upl_commit(redirUPL
, NULL
, 0);
2842 upl_deallocate(redirUPL
);
2848 IOByteCount
_IOMemoryMap::getLength()
2853 IOVirtualAddress
_IOMemoryMap::getVirtualAddress()
2858 task_t
_IOMemoryMap::getAddressTask()
2861 return( superMap
->getAddressTask());
2863 return( addressTask
);
2866 IOOptionBits
_IOMemoryMap::getMapOptions()
2871 IOMemoryDescriptor
* _IOMemoryMap::getMemoryDescriptor()
2876 _IOMemoryMap
* _IOMemoryMap::copyCompatible(
2877 IOMemoryDescriptor
* owner
,
2879 IOVirtualAddress toAddress
,
2880 IOOptionBits _options
,
2881 IOByteCount _offset
,
2882 IOByteCount _length
)
2884 _IOMemoryMap
* mapping
;
2886 if( (!task
) || (!addressMap
) || (addressMap
!= get_task_map(task
)))
2888 if( options
& kIOMapUnique
)
2890 if( (options
^ _options
) & kIOMapReadOnly
)
2892 if( (kIOMapDefaultCache
!= (_options
& kIOMapCacheMask
))
2893 && ((options
^ _options
) & kIOMapCacheMask
))
2896 if( (0 == (_options
& kIOMapAnywhere
)) && (logical
!= toAddress
))
2899 if( _offset
< offset
)
2904 if( (_offset
+ _length
) > length
)
2907 if( (length
== _length
) && (!_offset
)) {
2912 mapping
= new _IOMemoryMap
;
2914 && !mapping
->initCompatible( owner
, this, _offset
, _length
)) {
2924 _IOMemoryMap::getPhysicalSegment( IOByteCount _offset
, IOPhysicalLength
* _length
)
2926 IOPhysicalAddress address
;
2929 address
= memory
->getPhysicalSegment( offset
+ _offset
, _length
);
2935 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2938 #define super OSObject
2940 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2942 void IOMemoryDescriptor::initialize( void )
2944 if( 0 == gIOMemoryLock
)
2945 gIOMemoryLock
= IORecursiveLockAlloc();
2947 IORegistryEntry::getRegistryRoot()->setProperty(kIOMaximumMappedIOByteCountKey
,
2948 ptoa_64(gIOMaximumMappedIOPageCount
), 64);
2952 mapper
= new IOCopyMapper
;
2955 if (mapper
->init() && mapper
->start(NULL
))
2956 gIOCopyMapper
= (IOCopyMapper
*) mapper
;
2962 gIOLastPage
= IOGetLastPageNumber();
2965 void IOMemoryDescriptor::free( void )
2968 _mappings
->release();
2973 IOMemoryMap
* IOMemoryDescriptor::setMapping(
2975 IOVirtualAddress mapAddress
,
2976 IOOptionBits options
)
2978 _IOMemoryMap
* newMap
;
2980 newMap
= new _IOMemoryMap
;
2985 && !newMap
->initWithDescriptor( this, intoTask
, mapAddress
,
2986 options
| kIOMapStatic
, 0, getLength() )) {
2991 addMapping( newMap
);
2998 IOMemoryMap
* IOMemoryDescriptor::map(
2999 IOOptionBits options
)
3002 return( makeMapping( this, kernel_task
, 0,
3003 options
| kIOMapAnywhere
,
3007 IOMemoryMap
* IOMemoryDescriptor::map(
3009 IOVirtualAddress toAddress
,
3010 IOOptionBits options
,
3012 IOByteCount length
)
3015 length
= getLength();
3017 return( makeMapping( this, intoTask
, toAddress
, options
, offset
, length
));
3020 IOReturn
_IOMemoryMap::redirect(IOMemoryDescriptor
* newBackingMemory
,
3021 IOOptionBits options
,
3024 IOReturn err
= kIOReturnSuccess
;
3025 IOMemoryDescriptor
* physMem
= 0;
3029 if (logical
&& addressMap
) do
3031 if (((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
3032 || ((memory
->_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
3040 vm_size_t size
= length
;
3041 int flags
= UPL_COPYOUT_FROM
| UPL_SET_INTERNAL
3042 | UPL_SET_LITE
| UPL_SET_IO_WIRE
| UPL_BLOCK_ACCESS
;
3043 if (KERN_SUCCESS
!= memory_object_iopl_request((ipc_port_t
) memory
->_memEntry
, 0, &size
, &redirUPL
,
3050 IOUnmapPages( addressMap
, logical
, length
);
3051 physMem
->redirect(0, true);
3055 if (newBackingMemory
)
3057 if (newBackingMemory
!= memory
)
3059 if (this != newBackingMemory
->makeMapping(newBackingMemory
, addressTask
, (IOVirtualAddress
) this,
3060 options
| kIOMapUnique
| kIOMapReference
,
3062 err
= kIOReturnError
;
3066 upl_commit(redirUPL
, NULL
, 0);
3067 upl_deallocate(redirUPL
);
3071 physMem
->redirect(0, false);
3084 IOMemoryMap
* IOMemoryDescriptor::makeMapping(
3085 IOMemoryDescriptor
* owner
,
3087 IOVirtualAddress toAddress
,
3088 IOOptionBits options
,
3090 IOByteCount length
)
3092 IOMemoryDescriptor
* mapDesc
= 0;
3093 _IOMemoryMap
* mapping
= 0;
3100 if (kIOMapUnique
& options
)
3102 IOPhysicalAddress phys
;
3103 IOByteCount physLen
;
3108 if (((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical
)
3109 || ((_flags
& kIOMemoryTypeMask
) == kIOMemoryTypePhysical64
))
3111 phys
= getPhysicalSegment(offset
, &physLen
);
3112 if (!phys
|| (physLen
< length
))
3115 mapDesc
= IOMemoryDescriptor::withPhysicalAddress(
3116 phys
, length
, _direction
);
3127 if (kIOMapReference
& options
)
3129 mapping
= (_IOMemoryMap
*) toAddress
;
3133 uint32_t pageOffset1
= mapDesc
->getSourceSegment( offset
, NULL
);
3134 pageOffset1
-= trunc_page_32( pageOffset1
);
3136 uint32_t pageOffset2
= mapping
->getVirtualAddress();
3137 pageOffset2
-= trunc_page_32( pageOffset2
);
3139 if (pageOffset1
!= pageOffset2
)
3140 IOLog("::redirect can't map offset %x to addr %x\n",
3141 pageOffset1
, mapping
->getVirtualAddress());
3145 if (!mapping
->initWithDescriptor( mapDesc
, intoTask
, toAddress
, options
,
3149 IOLog("Didn't redirect map %08lx : %08lx\n", offset
, length
);
3154 mapping
->owner
->removeMapping(mapping
);
3160 // look for an existing mapping
3161 if( (iter
= OSCollectionIterator::withCollection( _mappings
))) {
3163 while( (mapping
= (_IOMemoryMap
*) iter
->getNextObject())) {
3165 if( (mapping
= mapping
->copyCompatible(
3166 owner
, intoTask
, toAddress
,
3167 options
| kIOMapReference
,
3178 if( mapping
|| (options
& kIOMapReference
))
3186 mapping
= new _IOMemoryMap
;
3188 && !mapping
->initWithDescriptor( mapDesc
, intoTask
, toAddress
, options
,
3191 IOLog("Didn't make map %08lx : %08lx\n", offset
, length
);
3204 mapping
->owner
= owner
;
3205 owner
->addMapping( mapping
);
3217 void IOMemoryDescriptor::addMapping(
3218 IOMemoryMap
* mapping
)
3222 _mappings
= OSSet::withCapacity(1);
3224 _mappings
->setObject( mapping
);
3228 void IOMemoryDescriptor::removeMapping(
3229 IOMemoryMap
* mapping
)
3232 _mappings
->removeObject( mapping
);
3235 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3238 #define super IOMemoryDescriptor
3240 OSDefineMetaClassAndStructors(IOSubMemoryDescriptor
, IOMemoryDescriptor
)
3242 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3244 bool IOSubMemoryDescriptor::initSubRange( IOMemoryDescriptor
* parent
,
3245 IOByteCount offset
, IOByteCount length
,
3246 IODirection direction
)
3251 if( (offset
+ length
) > parent
->getLength())
3255 * We can check the _parent instance variable before having ever set it
3256 * to an initial value because I/O Kit guarantees that all our instance
3257 * variables are zeroed on an object's allocation.
3265 * An existing memory descriptor is being retargeted to
3266 * point to somewhere else. Clean up our present state.
3277 _direction
= direction
;
3278 _tag
= parent
->getTag();
3283 void IOSubMemoryDescriptor::free( void )
3293 IOSubMemoryDescriptor::dmaCommandOperation(DMACommandOps op
, void *vData
, UInt dataSize
) const
3297 if (kIOMDGetCharacteristics
== op
) {
3299 rtn
= _parent
->dmaCommandOperation(op
, vData
, dataSize
);
3300 if (kIOReturnSuccess
== rtn
) {
3301 IOMDDMACharacteristics
*data
= (IOMDDMACharacteristics
*) vData
;
3302 data
->fLength
= _length
;
3303 data
->fSGCount
= 0; // XXX gvdl: need to compute and pages
3305 data
->fPageAlign
= 0;
3310 else if (kIOMDWalkSegments
& op
) {
3311 if (dataSize
< sizeof(IOMDDMAWalkSegmentArgs
))
3312 return kIOReturnUnderrun
;
3314 IOMDDMAWalkSegmentArgs
*data
=
3315 reinterpret_cast<IOMDDMAWalkSegmentArgs
*>(vData
);
3316 UInt offset
= data
->fOffset
;
3317 UInt remain
= _length
- offset
;
3318 if ((int) remain
<= 0)
3319 return (!remain
)? kIOReturnOverrun
: kIOReturnInternalError
;
3321 data
->fOffset
= offset
+ _start
;
3322 rtn
= _parent
->dmaCommandOperation(op
, vData
, dataSize
);
3323 if (data
->fLength
> remain
)
3324 data
->fLength
= remain
;
3325 data
->fOffset
= offset
;
3330 return kIOReturnBadArgument
;
3334 IOSubMemoryDescriptor::getPhysicalSegment64(IOByteCount offset
, IOByteCount
* length
)
3337 IOByteCount actualLength
;
3339 assert(offset
<= _length
);
3344 if( offset
>= _length
)
3347 address
= _parent
->getPhysicalSegment64( offset
+ _start
, &actualLength
);
3349 if( address
&& length
)
3350 *length
= min( _length
- offset
, actualLength
);
3356 IOSubMemoryDescriptor::getPhysicalSegment( IOByteCount offset
, IOByteCount
* length
)
3358 IOPhysicalAddress address
;
3359 IOByteCount actualLength
;
3361 assert(offset
<= _length
);
3366 if( offset
>= _length
)
3369 address
= _parent
->getPhysicalSegment( offset
+ _start
, &actualLength
);
3371 if( address
&& length
)
3372 *length
= min( _length
- offset
, actualLength
);
3378 IOReturn
IOSubMemoryDescriptor::doMap(
3379 vm_map_t addressMap
,
3380 IOVirtualAddress
* atAddress
,
3381 IOOptionBits options
,
3382 IOByteCount sourceOffset
,
3383 IOByteCount length
)
3385 if( sourceOffset
>= _length
)
3386 return( kIOReturnOverrun
);
3387 return (_parent
->doMap(addressMap
, atAddress
, options
, sourceOffset
+ _start
, length
));
3391 IOSubMemoryDescriptor::getSourceSegment( IOByteCount offset
, IOByteCount
* length
)
3393 IOPhysicalAddress address
;
3394 IOByteCount actualLength
;
3396 assert(offset
<= _length
);
3401 if( offset
>= _length
)
3404 address
= _parent
->getSourceSegment( offset
+ _start
, &actualLength
);
3406 if( address
&& length
)
3407 *length
= min( _length
- offset
, actualLength
);
3412 void * IOSubMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
3413 IOByteCount
* lengthOfSegment
)
3418 IOByteCount
IOSubMemoryDescriptor::readBytes(IOByteCount offset
,
3419 void * bytes
, IOByteCount length
)
3421 IOByteCount byteCount
;
3423 assert(offset
<= _length
);
3425 if( offset
>= _length
)
3429 byteCount
= _parent
->readBytes( _start
+ offset
, bytes
,
3430 min(length
, _length
- offset
) );
3433 return( byteCount
);
3436 IOByteCount
IOSubMemoryDescriptor::writeBytes(IOByteCount offset
,
3437 const void* bytes
, IOByteCount length
)
3439 IOByteCount byteCount
;
3441 assert(offset
<= _length
);
3443 if( offset
>= _length
)
3447 byteCount
= _parent
->writeBytes( _start
+ offset
, bytes
,
3448 min(length
, _length
- offset
) );
3451 return( byteCount
);
3454 IOReturn
IOSubMemoryDescriptor::setPurgeable( IOOptionBits newState
,
3455 IOOptionBits
* oldState
)
3460 err
= _parent
->setPurgeable( newState
, oldState
);
3466 IOReturn
IOSubMemoryDescriptor::performOperation( IOOptionBits options
,
3467 IOByteCount offset
, IOByteCount length
)
3471 assert(offset
<= _length
);
3473 if( offset
>= _length
)
3474 return( kIOReturnOverrun
);
3477 err
= _parent
->performOperation( options
, _start
+ offset
,
3478 min(length
, _length
- offset
) );
3484 IOReturn
IOSubMemoryDescriptor::prepare(
3485 IODirection forDirection
)
3490 err
= _parent
->prepare( forDirection
);
3496 IOReturn
IOSubMemoryDescriptor::complete(
3497 IODirection forDirection
)
3502 err
= _parent
->complete( forDirection
);
3508 IOMemoryMap
* IOSubMemoryDescriptor::makeMapping(
3509 IOMemoryDescriptor
* owner
,
3511 IOVirtualAddress toAddress
,
3512 IOOptionBits options
,
3514 IOByteCount length
)
3516 IOMemoryMap
* mapping
= 0;
3518 if (!(kIOMapUnique
& options
))
3519 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3521 toAddress
- (_start
+ offset
),
3522 options
| kIOMapReference
,
3523 _start
+ offset
, length
);
3526 mapping
= (IOMemoryMap
*) _parent
->makeMapping(
3529 options
, _start
+ offset
, length
);
3532 mapping
= super::makeMapping( owner
, intoTask
, toAddress
, options
,
3541 IOSubMemoryDescriptor::initWithAddress(void * address
,
3543 IODirection direction
)
3549 IOSubMemoryDescriptor::initWithAddress(vm_address_t address
,
3551 IODirection direction
,
3558 IOSubMemoryDescriptor::initWithPhysicalAddress(
3559 IOPhysicalAddress address
,
3561 IODirection direction
)
3567 IOSubMemoryDescriptor::initWithRanges(
3568 IOVirtualRange
* ranges
,
3570 IODirection direction
,
3578 IOSubMemoryDescriptor::initWithPhysicalRanges( IOPhysicalRange
* ranges
,
3580 IODirection direction
,
3586 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3588 bool IOGeneralMemoryDescriptor::serialize(OSSerialize
* s
) const
3590 OSSymbol
const *keys
[2];
3591 OSObject
*values
[2];
3593 user_addr_t address
;
3596 unsigned int index
, nRanges
;
3599 IOOptionBits type
= _flags
& kIOMemoryTypeMask
;
3601 if (s
== NULL
) return false;
3602 if (s
->previouslySerialized(this)) return true;
3604 // Pretend we are an array.
3605 if (!s
->addXMLStartTag(this, "array")) return false;
3607 nRanges
= _rangesCount
;
3608 vcopy
= (SerData
*) IOMalloc(sizeof(SerData
) * nRanges
);
3609 if (vcopy
== 0) return false;
3611 keys
[0] = OSSymbol::withCString("address");
3612 keys
[1] = OSSymbol::withCString("length");
3615 values
[0] = values
[1] = 0;
3617 // From this point on we can go to bail.
3619 // Copy the volatile data so we don't have to allocate memory
3620 // while the lock is held.
3622 if (nRanges
== _rangesCount
) {
3623 Ranges vec
= _ranges
;
3624 for (index
= 0; index
< nRanges
; index
++) {
3625 user_addr_t addr
; IOByteCount len
;
3626 getAddrLenForInd(addr
, len
, type
, vec
, index
);
3627 vcopy
[index
].address
= addr
;
3628 vcopy
[index
].length
= len
;
3631 // The descriptor changed out from under us. Give up.
3638 for (index
= 0; index
< nRanges
; index
++)
3640 user_addr_t addr
= vcopy
[index
].address
;
3641 IOByteCount len
= (IOByteCount
) vcopy
[index
].length
;
3643 OSNumber::withNumber(addr
, (((UInt64
) addr
) >> 32)? 64 : 32);
3644 if (values
[0] == 0) {
3648 values
[1] = OSNumber::withNumber(len
, sizeof(len
) * 8);
3649 if (values
[1] == 0) {
3653 OSDictionary
*dict
= OSDictionary::withObjects((const OSObject
**)values
, (const OSSymbol
**)keys
, 2);
3658 values
[0]->release();
3659 values
[1]->release();
3660 values
[0] = values
[1] = 0;
3662 result
= dict
->serialize(s
);
3668 result
= s
->addXMLEndTag("array");
3672 values
[0]->release();
3674 values
[1]->release();
3680 IOFree(vcopy
, sizeof(IOVirtualRange
) * nRanges
);
3684 bool IOSubMemoryDescriptor::serialize(OSSerialize
* s
) const
3689 if (s
->previouslySerialized(this)) return true;
3691 // Pretend we are a dictionary.
3692 // We must duplicate the functionality of OSDictionary here
3693 // because otherwise object references will not work;
3694 // they are based on the value of the object passed to
3695 // previouslySerialized and addXMLStartTag.
3697 if (!s
->addXMLStartTag(this, "dict")) return false;
3699 char const *keys
[3] = {"offset", "length", "parent"};
3701 OSObject
*values
[3];
3702 values
[0] = OSNumber::withNumber(_start
, sizeof(_start
) * 8);
3705 values
[1] = OSNumber::withNumber(_length
, sizeof(_length
) * 8);
3706 if (values
[1] == 0) {
3707 values
[0]->release();
3710 values
[2] = _parent
;
3713 for (int i
=0; i
<3; i
++) {
3714 if (!s
->addString("<key>") ||
3715 !s
->addString(keys
[i
]) ||
3716 !s
->addXMLEndTag("key") ||
3717 !values
[i
]->serialize(s
)) {
3722 values
[0]->release();
3723 values
[1]->release();
3728 return s
->addXMLEndTag("dict");
3731 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3733 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 0);
3734 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 1);
3735 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 2);
3736 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 3);
3737 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 4);
3738 OSMetaClassDefineReservedUsed(IOMemoryDescriptor
, 5);
3739 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 6);
3740 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 7);
3741 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 8);
3742 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 9);
3743 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 10);
3744 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 11);
3745 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 12);
3746 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 13);
3747 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 14);
3748 OSMetaClassDefineReservedUnused(IOMemoryDescriptor
, 15);
3750 /* ex-inline function implementation */
3752 IOMemoryDescriptor::getPhysicalAddress()
3753 { return( getPhysicalSegment( 0, 0 )); }