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@
28 #define IOKIT_ENABLE_SHARED_PTR
30 #define _IOMEMORYDESCRIPTOR_INTERNAL_
32 #include <IOKit/assert.h>
33 #include <IOKit/system.h>
35 #include <IOKit/IOLib.h>
36 #include <IOKit/IOMapper.h>
37 #include <IOKit/IOBufferMemoryDescriptor.h>
38 #include <libkern/OSDebug.h>
39 #include <mach/mach_vm.h>
41 #include "IOKitKernelInternal.h"
44 #include <libkern/c++/OSCPPDebug.h>
46 #include <IOKit/IOStatisticsPrivate.h>
49 #define IOStatisticsAlloc(type, size) \
51 IOStatistics::countAlloc(type, size); \
54 #define IOStatisticsAlloc(type, size)
55 #endif /* IOKITSTATS */
59 void ipc_port_release_send(ipc_port_t port
);
64 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
67 kInternalFlagPhysical
= 0x00000001,
68 kInternalFlagPageSized
= 0x00000002,
69 kInternalFlagPageAllocated
= 0x00000004,
70 kInternalFlagInit
= 0x00000008
73 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
75 #define super IOGeneralMemoryDescriptor
76 OSDefineMetaClassAndStructorsWithZone(IOBufferMemoryDescriptor
,
77 IOGeneralMemoryDescriptor
, ZC_ZFREE_CLEARMEM
);
79 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
82 IOBMDPageProc(iopa_t
* a
)
85 vm_address_t vmaddr
= 0;
86 int options
= 0;// KMA_LOMEM;
88 kr
= kernel_memory_allocate(kernel_map
, &vmaddr
,
89 page_size
, 0, options
, VM_KERN_MEMORY_IOKIT
);
91 if (KERN_SUCCESS
!= kr
) {
94 bzero((void *) vmaddr
, page_size
);
97 return (uintptr_t) vmaddr
;
100 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
104 IOBufferMemoryDescriptor::initWithOptions(
105 IOOptionBits options
,
107 vm_offset_t alignment
,
110 mach_vm_address_t physicalMask
= 0;
111 return initWithPhysicalMask(inTask
, options
, capacity
, alignment
, physicalMask
);
113 #endif /* !__LP64__ */
115 OSSharedPtr
<IOBufferMemoryDescriptor
>
116 IOBufferMemoryDescriptor::withCopy(
118 IOOptionBits options
,
120 mach_vm_address_t source
,
123 OSSharedPtr
<IOBufferMemoryDescriptor
> inst
;
126 vm_map_address_t address
;
130 err
= kIOReturnNoMemory
;
131 inst
= OSMakeShared
<IOBufferMemoryDescriptor
>();
135 inst
->_ranges
.v64
= IONew(IOAddressRange
, 1);
136 if (!inst
->_ranges
.v64
) {
140 err
= vm_map_copyin(sourceMap
, source
, size
,
141 false /* src_destroy */, ©
);
142 if (KERN_SUCCESS
!= err
) {
146 err
= vm_map_copyout(get_task_map(inTask
), &address
, copy
);
147 if (KERN_SUCCESS
!= err
) {
152 inst
->_ranges
.v64
->address
= address
;
153 inst
->_ranges
.v64
->length
= size
;
155 if (!inst
->initWithPhysicalMask(inTask
, options
, size
, page_size
, 0)) {
156 err
= kIOReturnError
;
160 if (KERN_SUCCESS
== err
) {
165 vm_map_copy_discard(copy
);
173 IOBufferMemoryDescriptor::initWithPhysicalMask(
175 IOOptionBits options
,
176 mach_vm_size_t capacity
,
177 mach_vm_address_t alignment
,
178 mach_vm_address_t physicalMask
)
180 task_t mapTask
= NULL
;
181 vm_map_t vmmap
= NULL
;
182 mach_vm_address_t highestMask
= 0;
183 IOOptionBits iomdOptions
= kIOMemoryTypeVirtual64
| kIOMemoryAsReference
;
184 IODMAMapSpecification mapSpec
;
186 bool withCopy
= false;
187 bool mappedOrShared
= false;
194 _capacity
= capacity
;
196 _internalReserved
= 0;
200 _ranges
.v64
= IONew(IOAddressRange
, 1);
204 _ranges
.v64
->address
= 0;
205 _ranges
.v64
->length
= 0;
207 if (!_ranges
.v64
->address
) {
210 if (!(kIOMemoryPageable
& options
)) {
216 _buffer
= (void *) _ranges
.v64
->address
;
219 // make sure super::free doesn't dealloc _ranges before super::init
220 _flags
= kIOMemoryAsReference
;
222 // Grab IOMD bits from the Buffer MD options
223 iomdOptions
|= (options
& kIOBufferDescriptorMemoryFlags
);
225 if (!(kIOMemoryMapperNone
& options
)) {
226 IOMapper::checkForSystemMapper();
227 mapped
= (NULL
!= IOMapper::gSystem
);
230 if (physicalMask
&& (alignment
<= 1)) {
231 alignment
= ((physicalMask
^ (-1ULL)) & (physicalMask
- 1));
232 highestMask
= (physicalMask
| alignment
);
234 if (alignment
< page_size
) {
235 alignment
= page_size
;
239 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
| kIOMemoryClearEncrypt
)) && (alignment
< page_size
)) {
240 alignment
= page_size
;
243 if (alignment
>= page_size
) {
244 if (round_page_overflow(capacity
, &capacity
)) {
249 if (alignment
> page_size
) {
250 options
|= kIOMemoryPhysicallyContiguous
;
253 _alignment
= alignment
;
255 if ((capacity
+ alignment
) < _capacity
) {
259 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
)) {
263 bzero(&mapSpec
, sizeof(mapSpec
));
264 mapSpec
.alignment
= _alignment
;
265 mapSpec
.numAddressBits
= 64;
266 if (highestMask
&& mapped
) {
267 if (highestMask
<= 0xFFFFFFFF) {
268 mapSpec
.numAddressBits
= (uint8_t)(32 - __builtin_clz((unsigned int) highestMask
));
270 mapSpec
.numAddressBits
= (uint8_t)(64 - __builtin_clz((unsigned int) (highestMask
>> 32)));
275 // set memory entry cache mode, pageable, purgeable
276 iomdOptions
|= ((options
& kIOMapCacheMask
) >> kIOMapCacheShift
) << kIOMemoryBufferCacheShift
;
277 if (options
& kIOMemoryPageable
) {
278 iomdOptions
|= kIOMemoryBufferPageable
;
279 if (options
& kIOMemoryPurgeable
) {
280 iomdOptions
|= kIOMemoryBufferPurgeable
;
285 // Buffer shouldn't auto prepare they should be prepared explicitly
286 // But it never was enforced so what are you going to do?
287 iomdOptions
|= kIOMemoryAutoPrepare
;
289 /* Allocate a wired-down buffer inside kernel space. */
291 bool contig
= (0 != (options
& kIOMemoryHostPhysicallyContiguous
));
293 if (!contig
&& (0 != (options
& kIOMemoryPhysicallyContiguous
))) {
295 contig
|= (0 != (kIOMemoryMapperNone
& options
));
297 // treat kIOMemoryPhysicallyContiguous as kIOMemoryHostPhysicallyContiguous for now
302 mappedOrShared
= (mapped
|| (0 != (kIOMemorySharingTypeMask
& options
)));
303 if (contig
|| highestMask
|| (alignment
> page_size
)) {
304 _internalFlags
|= kInternalFlagPhysical
;
306 _internalFlags
|= kInternalFlagPageSized
;
307 if (round_page_overflow(capacity
, &capacity
)) {
311 _buffer
= (void *) IOKernelAllocateWithPhysicalRestrict(
312 capacity
, highestMask
, alignment
, contig
);
313 } else if (mappedOrShared
314 && (capacity
+ alignment
) <= (page_size
- gIOPageAllocChunkBytes
)) {
315 _internalFlags
|= kInternalFlagPageAllocated
;
316 _buffer
= (void *) iopa_alloc(&gIOBMDPageAllocator
, &IOBMDPageProc
, capacity
, alignment
);
318 IOStatisticsAlloc(kIOStatisticsMallocAligned
, capacity
);
320 OSAddAtomicLong(capacity
, &debug_iomalloc_size
);
323 } else if (alignment
> 1) {
324 _buffer
= IOMallocAligned(capacity
, alignment
);
326 _buffer
= IOMalloc(capacity
);
331 bzero(_buffer
, capacity
);
334 if ((options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
335 vm_size_t size
= round_page(capacity
);
337 // initWithOptions will create memory entry
339 iomdOptions
|= kIOMemoryPersistent
;
342 if (options
& kIOMemoryPageable
) {
344 OSAddAtomicLong(size
, &debug_iomallocpageable_size
);
349 if (NULL
== inTask
) {
350 inTask
= kernel_task
;
352 } else if (options
& kIOMapCacheMask
) {
353 // Prefetch each page to put entries into the pmap
354 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
355 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
357 while (startAddr
< endAddr
) {
358 UInt8 dummyVar
= *startAddr
;
360 startAddr
+= page_size
;
365 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;
366 _ranges
.v64
->length
= _capacity
;
368 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
369 inTask
, iomdOptions
, /* System mapper */ NULL
)) {
373 _internalFlags
|= kInternalFlagInit
;
375 if (!(options
& kIOMemoryPageable
)) {
376 trackingAccumSize(capacity
);
378 #endif /* IOTRACKING */
380 // give any system mapper the allocation params
381 if (kIOReturnSuccess
!= dmaCommandOperation(kIOMDAddDMAMapSpec
,
382 &mapSpec
, sizeof(mapSpec
))) {
388 reserved
= IONew( ExpansionData
, 1 );
393 reserved
->map
= createMappingInTask(mapTask
, 0,
394 kIOMapAnywhere
| (options
& kIOMapPrefault
) | (options
& kIOMapCacheMask
), 0, 0).detach();
395 if (!reserved
->map
) {
399 release(); // map took a retain on this
400 reserved
->map
->retain();
401 removeMapping(reserved
->map
);
402 mach_vm_address_t buffer
= reserved
->map
->getAddress();
403 _buffer
= (void *) buffer
;
404 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
)) {
405 _ranges
.v64
->address
= buffer
;
409 setLength(_capacity
);
414 OSSharedPtr
<IOBufferMemoryDescriptor
>
415 IOBufferMemoryDescriptor::inTaskWithOptions(
417 IOOptionBits options
,
419 vm_offset_t alignment
)
421 OSSharedPtr
<IOBufferMemoryDescriptor
> me
= OSMakeShared
<IOBufferMemoryDescriptor
>();
423 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
429 OSSharedPtr
<IOBufferMemoryDescriptor
>
430 IOBufferMemoryDescriptor::inTaskWithOptions(
432 IOOptionBits options
,
434 vm_offset_t alignment
,
438 OSSharedPtr
<IOBufferMemoryDescriptor
> me
= OSMakeShared
<IOBufferMemoryDescriptor
>();
441 me
->setVMTags(kernTag
, userTag
);
443 if (!me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
450 OSSharedPtr
<IOBufferMemoryDescriptor
>
451 IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
453 IOOptionBits options
,
454 mach_vm_size_t capacity
,
455 mach_vm_address_t physicalMask
)
457 OSSharedPtr
<IOBufferMemoryDescriptor
> me
= OSMakeShared
<IOBufferMemoryDescriptor
>();
459 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
)) {
467 IOBufferMemoryDescriptor::initWithOptions(
468 IOOptionBits options
,
470 vm_offset_t alignment
)
472 return initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0);
474 #endif /* !__LP64__ */
476 OSSharedPtr
<IOBufferMemoryDescriptor
>
477 IOBufferMemoryDescriptor::withOptions(
478 IOOptionBits options
,
480 vm_offset_t alignment
)
482 OSSharedPtr
<IOBufferMemoryDescriptor
> me
= OSMakeShared
<IOBufferMemoryDescriptor
>();
484 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
494 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
495 * hold capacity bytes. The descriptor's length is initially set to the capacity.
497 OSSharedPtr
<IOBufferMemoryDescriptor
>
498 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
499 IODirection inDirection
,
502 return IOBufferMemoryDescriptor::withOptions(
503 inDirection
| kIOMemoryUnshared
504 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
505 inCapacity
, inContiguous
? inCapacity
: 1 );
512 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
513 * The descriptor's length and capacity are set to the input buffer's size.
516 IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
518 IODirection inDirection
,
521 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
522 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
523 inLength
, inLength
, (mach_vm_address_t
)0)) {
527 // start out with no data
530 if (!appendBytes(inBytes
, inLength
)) {
536 #endif /* !__LP64__ */
541 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
542 * The descriptor's length and capacity are set to the input buffer's size.
544 OSSharedPtr
<IOBufferMemoryDescriptor
>
545 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
547 IODirection inDirection
,
550 OSSharedPtr
<IOBufferMemoryDescriptor
> me
= OSMakeShared
<IOBufferMemoryDescriptor
>();
552 if (me
&& !me
->initWithPhysicalMask(
553 kernel_task
, inDirection
| kIOMemoryUnshared
554 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
555 inLength
, inLength
, 0 )) {
560 // start out with no data
563 if (!me
->appendBytes(inBytes
, inLength
)) {
576 IOBufferMemoryDescriptor::free()
578 // Cache all of the relevant information on the stack for use
579 // after we call super::free()!
580 IOOptionBits flags
= _flags
;
581 IOOptionBits internalFlags
= _internalFlags
;
582 IOOptionBits options
= _options
;
583 vm_size_t size
= _capacity
;
584 void * buffer
= _buffer
;
585 IOMemoryMap
* map
= NULL
;
586 IOAddressRange
* range
= _ranges
.v64
;
587 vm_offset_t alignment
= _alignment
;
589 if (alignment
>= page_size
) {
590 size
= round_page(size
);
595 IODelete( reserved
, ExpansionData
, 1 );
601 if ((options
& kIOMemoryPageable
)
602 || (kInternalFlagPageSized
& internalFlags
)) {
603 size
= round_page(size
);
607 if (!(options
& kIOMemoryPageable
)
609 && (kInternalFlagInit
& _internalFlags
)) {
610 trackingAccumSize(-size
);
612 #endif /* IOTRACKING */
614 /* super::free may unwire - deallocate buffer afterwards */
617 if (options
& kIOMemoryPageable
) {
619 OSAddAtomicLong(-size
, &debug_iomallocpageable_size
);
622 if (kInternalFlagPhysical
& internalFlags
) {
623 IOKernelFreePhysical((mach_vm_address_t
) buffer
, size
);
624 } else if (kInternalFlagPageAllocated
& internalFlags
) {
626 page
= iopa_free(&gIOBMDPageAllocator
, (uintptr_t) buffer
, size
);
628 kmem_free(kernel_map
, page
, page_size
);
631 OSAddAtomicLong(-size
, &debug_iomalloc_size
);
633 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
634 } else if (alignment
> 1) {
635 IOFreeAligned(buffer
, size
);
637 IOFree(buffer
, size
);
640 if (range
&& (kIOMemoryAsReference
& flags
)) {
641 IODelete(range
, IOAddressRange
, 1);
648 * Get the buffer capacity
651 IOBufferMemoryDescriptor::getCapacity() const
659 * Change the buffer length of the memory descriptor. When a new buffer
660 * is created, the initial length of the buffer is set to be the same as
661 * the capacity. The length can be adjusted via setLength for a shorter
662 * transfer (there is no need to create more buffer descriptors when you
663 * can reuse an existing one, even for different transfer sizes). Note
664 * that the specified length must not exceed the capacity of the buffer.
667 IOBufferMemoryDescriptor::setLength(vm_size_t length
)
669 assert(length
<= _capacity
);
670 if (length
> _capacity
) {
675 _ranges
.v64
->length
= length
;
681 * Change the direction of the transfer. This method allows one to redirect
682 * the descriptor's transfer direction. This eliminates the need to destroy
683 * and create new buffers when different transfer directions are needed.
686 IOBufferMemoryDescriptor::setDirection(IODirection direction
)
688 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
690 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
691 #endif /* !__LP64__ */
697 * Add some data to the end of the buffer. This method automatically
698 * maintains the memory descriptor buffer length. Note that appendBytes
699 * will not copy past the end of the memory descriptor's current capacity.
702 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
704 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
707 assert(_length
<= _capacity
);
710 _length
+= actualBytesToCopy
;
711 _ranges
.v64
->length
+= actualBytesToCopy
;
713 if (_task
== kernel_task
) {
714 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
717 writeBytes(offset
, bytes
, actualBytesToCopy
);
726 * Return the virtual address of the beginning of the buffer
729 IOBufferMemoryDescriptor::getBytesNoCopy()
731 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
)) {
734 return (void *)_ranges
.v64
->address
;
742 * Return the virtual address of an offset from the beginning of the buffer
745 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
747 IOVirtualAddress address
;
749 if ((start
+ withLength
) < start
) {
753 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
)) {
754 address
= (IOVirtualAddress
) _buffer
;
756 address
= _ranges
.v64
->address
;
759 if (start
< _length
&& (start
+ withLength
) <= _length
) {
760 return (void *)(address
+ start
);
767 IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
768 IOByteCount
* lengthOfSegment
)
770 void * bytes
= getBytesNoCopy(offset
, 0);
772 if (bytes
&& lengthOfSegment
) {
773 *lengthOfSegment
= _length
- offset
;
778 #endif /* !__LP64__ */
781 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
782 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
783 #else /* !__LP64__ */
784 OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor
, 0);
785 OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor
, 1);
786 #endif /* !__LP64__ */
787 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
788 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
789 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
790 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
791 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
792 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
793 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
794 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
795 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
796 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
797 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
798 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
799 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
800 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);