2 * Copyright (c) 1998-2000 Apple Computer, 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 #define _IOMEMORYDESCRIPTOR_INTERNAL_
31 #include <IOKit/assert.h>
32 #include <IOKit/system.h>
34 #include <IOKit/IOLib.h>
35 #include <IOKit/IOMapper.h>
36 #include <IOKit/IOBufferMemoryDescriptor.h>
37 #include <libkern/OSDebug.h>
38 #include <mach/mach_vm.h>
40 #include "IOKitKernelInternal.h"
43 #include <libkern/c++/OSCPPDebug.h>
45 #include <IOKit/IOStatisticsPrivate.h>
48 #define IOStatisticsAlloc(type, size) \
50 IOStatistics::countAlloc(type, size); \
53 #define IOStatisticsAlloc(type, size)
54 #endif /* IOKITSTATS */
58 void ipc_port_release_send(ipc_port_t port
);
63 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
67 kInternalFlagPhysical
= 0x00000001,
68 kInternalFlagPageSized
= 0x00000002,
69 kInternalFlagPageAllocated
= 0x00000004
72 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
74 #define super IOGeneralMemoryDescriptor
75 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
76 IOGeneralMemoryDescriptor
);
78 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
80 static uintptr_t IOBMDPageProc(iopa_t
* a
)
83 vm_address_t vmaddr
= 0;
84 int options
= 0; // KMA_LOMEM;
86 kr
= kernel_memory_allocate(kernel_map
, &vmaddr
,
87 page_size
, 0, options
);
89 if (KERN_SUCCESS
!= kr
) vmaddr
= 0;
90 else bzero((void *) vmaddr
, page_size
);
92 return ((uintptr_t) vmaddr
);
95 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
98 bool IOBufferMemoryDescriptor::initWithOptions(
101 vm_offset_t alignment
,
104 mach_vm_address_t physicalMask
= 0;
105 return (initWithPhysicalMask(inTask
, options
, capacity
, alignment
, physicalMask
));
107 #endif /* !__LP64__ */
109 bool IOBufferMemoryDescriptor::initWithPhysicalMask(
111 IOOptionBits options
,
112 mach_vm_size_t capacity
,
113 mach_vm_address_t alignment
,
114 mach_vm_address_t physicalMask
)
117 task_t mapTask
= NULL
;
118 vm_map_t vmmap
= NULL
;
119 mach_vm_address_t highestMask
= 0;
120 IOOptionBits iomdOptions
= kIOMemoryTypeVirtual64
| kIOMemoryAsReference
;
121 IODMAMapSpecification mapSpec
;
129 _capacity
= capacity
;
131 _internalReserved
= 0;
134 _ranges
.v64
= IONew(IOAddressRange
, 1);
137 _ranges
.v64
->address
= 0;
138 _ranges
.v64
->length
= 0;
139 // make sure super::free doesn't dealloc _ranges before super::init
140 _flags
= kIOMemoryAsReference
;
142 // Grab IOMD bits from the Buffer MD options
143 iomdOptions
|= (options
& kIOBufferDescriptorMemoryFlags
);
145 if (!(kIOMemoryMapperNone
& options
))
147 IOMapper::checkForSystemMapper();
148 mapped
= (0 != IOMapper::gSystem
);
152 if (physicalMask
&& (alignment
<= 1))
154 alignment
= ((physicalMask
^ (-1ULL)) & (physicalMask
- 1));
155 highestMask
= (physicalMask
| alignment
);
157 if (alignment
< page_size
)
158 alignment
= page_size
;
161 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
| kIOMemoryClearEncrypt
)) && (alignment
< page_size
))
162 alignment
= page_size
;
164 if (alignment
>= page_size
)
165 capacity
= round_page(capacity
);
167 if (alignment
> page_size
)
168 options
|= kIOMemoryPhysicallyContiguous
;
170 _alignment
= alignment
;
172 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
))
175 bzero(&mapSpec
, sizeof(mapSpec
));
176 mapSpec
.alignment
= _alignment
;
177 mapSpec
.numAddressBits
= 64;
178 if (highestMask
&& mapped
)
180 if (highestMask
<= 0xFFFFFFFF)
181 mapSpec
.numAddressBits
= (32 - __builtin_clz((unsigned int) highestMask
));
183 mapSpec
.numAddressBits
= (64 - __builtin_clz((unsigned int) (highestMask
>> 32)));
187 // set flags for entry + object create
188 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
;
190 // set memory entry cache mode
191 switch (options
& kIOMapCacheMask
)
193 case kIOMapInhibitCache
:
194 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
197 case kIOMapWriteThruCache
:
198 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
201 case kIOMapWriteCombineCache
:
202 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
205 case kIOMapCopybackCache
:
206 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
209 case kIOMapCopybackInnerCache
:
210 SET_MAP_MEM(MAP_MEM_INNERWBACK
, memEntryCacheMode
);
213 case kIOMapDefaultCache
:
215 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
219 if (options
& kIOMemoryPageable
)
221 iomdOptions
|= kIOMemoryBufferPageable
;
223 // must create the entry before any pages are allocated
225 // set flags for entry + object create
226 memEntryCacheMode
|= MAP_MEM_NAMED_CREATE
;
228 if (options
& kIOMemoryPurgeable
)
229 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
233 memEntryCacheMode
|= MAP_MEM_NAMED_REUSE
;
236 // Buffer shouldn't auto prepare they should be prepared explicitly
237 // But it never was enforced so what are you going to do?
238 iomdOptions
|= kIOMemoryAutoPrepare
;
240 /* Allocate a wired-down buffer inside kernel space. */
242 bool contig
= (0 != (options
& kIOMemoryHostPhysicallyContiguous
));
244 if (!contig
&& (0 != (options
& kIOMemoryPhysicallyContiguous
)))
247 contig
|= (0 != (kIOMemoryMapperNone
& options
));
249 // treat kIOMemoryPhysicallyContiguous as kIOMemoryHostPhysicallyContiguous for now
254 if (contig
|| highestMask
|| (alignment
> page_size
))
256 _internalFlags
|= kInternalFlagPhysical
;
259 _internalFlags
|= kInternalFlagPageSized
;
260 capacity
= round_page(capacity
);
262 _buffer
= (void *) IOKernelAllocateWithPhysicalRestrict(
263 capacity
, highestMask
, alignment
, contig
);
266 && ((capacity
+ alignment
) <= (page_size
- kIOPageAllocChunkBytes
)))
268 _internalFlags
|= kInternalFlagPageAllocated
;
270 _buffer
= (void *) iopa_alloc(&gIOBMDPageAllocator
, &IOBMDPageProc
, capacity
, alignment
);
273 IOStatisticsAlloc(kIOStatisticsMallocAligned
, capacity
);
275 debug_iomalloc_size
+= capacity
;
279 else if (alignment
> 1)
281 _buffer
= IOMallocAligned(capacity
, alignment
);
285 _buffer
= IOMalloc(capacity
);
291 if (needZero
) bzero(_buffer
, capacity
);
294 if( (options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
295 ipc_port_t sharedMem
;
296 vm_size_t size
= round_page(capacity
);
298 kr
= mach_make_memory_entry(vmmap
,
299 &size
, (vm_offset_t
)_buffer
,
300 memEntryCacheMode
, &sharedMem
,
303 if( (KERN_SUCCESS
== kr
) && (size
!= round_page(capacity
))) {
304 ipc_port_release_send( sharedMem
);
305 kr
= kIOReturnVMError
;
307 if( KERN_SUCCESS
!= kr
)
310 _memEntry
= (void *) sharedMem
;
312 if( options
& kIOMemoryPageable
) {
314 debug_iomallocpageable_size
+= size
;
318 inTask
= kernel_task
;
320 else if (options
& kIOMapCacheMask
)
322 // Prefetch each page to put entries into the pmap
323 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
324 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
326 while (startAddr
< endAddr
)
328 UInt8 dummyVar
= *startAddr
;
330 startAddr
+= page_size
;
335 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;;
336 _ranges
.v64
->length
= _capacity
;
338 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
339 inTask
, iomdOptions
, /* System mapper */ 0))
342 // give any system mapper the allocation params
343 if (kIOReturnSuccess
!= dmaCommandOperation(kIOMDAddDMAMapSpec
,
344 &mapSpec
, sizeof(mapSpec
)))
350 reserved
= IONew( ExpansionData
, 1 );
354 reserved
->map
= createMappingInTask(mapTask
, 0,
355 kIOMapAnywhere
| (options
& kIOMapCacheMask
), 0, 0);
361 release(); // map took a retain on this
362 reserved
->map
->retain();
363 removeMapping(reserved
->map
);
364 mach_vm_address_t buffer
= reserved
->map
->getAddress();
365 _buffer
= (void *) buffer
;
366 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
))
367 _ranges
.v64
->address
= buffer
;
370 setLength(_capacity
);
375 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
377 IOOptionBits options
,
379 vm_offset_t alignment
)
381 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
383 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
390 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
392 IOOptionBits options
,
393 mach_vm_size_t capacity
,
394 mach_vm_address_t physicalMask
)
396 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
398 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
407 bool IOBufferMemoryDescriptor::initWithOptions(
408 IOOptionBits options
,
410 vm_offset_t alignment
)
412 return (initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0));
414 #endif /* !__LP64__ */
416 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
417 IOOptionBits options
,
419 vm_offset_t alignment
)
421 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
423 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
434 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
435 * hold capacity bytes. The descriptor's length is initially set to the capacity.
437 IOBufferMemoryDescriptor
*
438 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
439 IODirection inDirection
,
442 return( IOBufferMemoryDescriptor::withOptions(
443 inDirection
| kIOMemoryUnshared
444 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
445 inCapacity
, inContiguous
? inCapacity
: 1 ));
452 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
453 * The descriptor's length and capacity are set to the input buffer's size.
455 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
457 IODirection inDirection
,
460 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
461 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
462 inLength
, inLength
, (mach_vm_address_t
)0))
465 // start out with no data
468 if (!appendBytes(inBytes
, inLength
))
473 #endif /* !__LP64__ */
478 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
479 * The descriptor's length and capacity are set to the input buffer's size.
481 IOBufferMemoryDescriptor
*
482 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
484 IODirection inDirection
,
487 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
489 if (me
&& !me
->initWithPhysicalMask(
490 kernel_task
, inDirection
| kIOMemoryUnshared
491 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
492 inLength
, inLength
, 0 ))
500 // start out with no data
503 if (!me
->appendBytes(inBytes
, inLength
))
517 void IOBufferMemoryDescriptor::free()
519 // Cache all of the relevant information on the stack for use
520 // after we call super::free()!
521 IOOptionBits flags
= _flags
;
522 IOOptionBits internalFlags
= _internalFlags
;
523 IOOptionBits options
= _options
;
524 vm_size_t size
= _capacity
;
525 void * buffer
= _buffer
;
526 IOMemoryMap
* map
= 0;
527 IOAddressRange
* range
= _ranges
.v64
;
528 vm_offset_t alignment
= _alignment
;
530 if (alignment
>= page_size
)
531 size
= round_page(size
);
536 IODelete( reserved
, ExpansionData
, 1 );
541 /* super::free may unwire - deallocate buffer afterwards */
544 if (options
& kIOMemoryPageable
)
547 debug_iomallocpageable_size
-= round_page(size
);
552 if (kInternalFlagPageSized
& internalFlags
) size
= round_page(size
);
554 if (kInternalFlagPhysical
& internalFlags
)
556 IOKernelFreePhysical((mach_vm_address_t
) buffer
, size
);
558 else if (kInternalFlagPageAllocated
& internalFlags
)
561 page
= iopa_free(&gIOBMDPageAllocator
, (uintptr_t) buffer
, size
);
564 kmem_free(kernel_map
, page
, page_size
);
567 debug_iomalloc_size
-= size
;
569 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
571 else if (alignment
> 1)
573 IOFreeAligned(buffer
, size
);
577 IOFree(buffer
, size
);
580 if (range
&& (kIOMemoryAsReference
& flags
))
581 IODelete(range
, IOAddressRange
, 1);
587 * Get the buffer capacity
589 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
597 * Change the buffer length of the memory descriptor. When a new buffer
598 * is created, the initial length of the buffer is set to be the same as
599 * the capacity. The length can be adjusted via setLength for a shorter
600 * transfer (there is no need to create more buffer descriptors when you
601 * can reuse an existing one, even for different transfer sizes). Note
602 * that the specified length must not exceed the capacity of the buffer.
604 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
606 assert(length
<= _capacity
);
609 _ranges
.v64
->length
= length
;
615 * Change the direction of the transfer. This method allows one to redirect
616 * the descriptor's transfer direction. This eliminates the need to destroy
617 * and create new buffers when different transfer directions are needed.
619 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
621 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
623 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
624 #endif /* !__LP64__ */
630 * Add some data to the end of the buffer. This method automatically
631 * maintains the memory descriptor buffer length. Note that appendBytes
632 * will not copy past the end of the memory descriptor's current capacity.
635 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
637 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
640 assert(_length
<= _capacity
);
643 _length
+= actualBytesToCopy
;
644 _ranges
.v64
->length
+= actualBytesToCopy
;
646 if (_task
== kernel_task
)
647 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
650 writeBytes(offset
, bytes
, actualBytesToCopy
);
658 * Return the virtual address of the beginning of the buffer
660 void * IOBufferMemoryDescriptor::getBytesNoCopy()
662 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
665 return (void *)_ranges
.v64
->address
;
672 * Return the virtual address of an offset from the beginning of the buffer
675 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
677 IOVirtualAddress address
;
678 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
679 address
= (IOVirtualAddress
) _buffer
;
681 address
= _ranges
.v64
->address
;
683 if (start
< _length
&& (start
+ withLength
) <= _length
)
684 return (void *)(address
+ start
);
689 void * IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
690 IOByteCount
* lengthOfSegment
)
692 void * bytes
= getBytesNoCopy(offset
, 0);
694 if (bytes
&& lengthOfSegment
)
695 *lengthOfSegment
= _length
- offset
;
699 #endif /* !__LP64__ */
702 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
703 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
704 #else /* !__LP64__ */
705 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
706 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
707 #endif /* !__LP64__ */
708 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
709 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
710 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
711 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
712 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
713 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
714 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
715 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
716 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
717 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
718 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
719 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
720 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
721 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);