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 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66 kInternalFlagPhysical
= 0x00000001,
67 kInternalFlagPageSized
= 0x00000002,
68 kInternalFlagPageAllocated
= 0x00000004,
69 kInternalFlagInit
= 0x00000008
72 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
74 #define super IOGeneralMemoryDescriptor
75 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
76 IOGeneralMemoryDescriptor
);
78 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
81 IOBMDPageProc(iopa_t
* a
)
84 vm_address_t vmaddr
= 0;
85 int options
= 0;// KMA_LOMEM;
87 kr
= kernel_memory_allocate(kernel_map
, &vmaddr
,
88 page_size
, 0, options
, VM_KERN_MEMORY_IOKIT
);
90 if (KERN_SUCCESS
!= kr
) {
93 bzero((void *) vmaddr
, page_size
);
96 return (uintptr_t) vmaddr
;
99 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
103 IOBufferMemoryDescriptor::initWithOptions(
104 IOOptionBits options
,
106 vm_offset_t alignment
,
109 mach_vm_address_t physicalMask
= 0;
110 return initWithPhysicalMask(inTask
, options
, capacity
, alignment
, physicalMask
);
112 #endif /* !__LP64__ */
114 IOBufferMemoryDescriptor
*
115 IOBufferMemoryDescriptor::withCopy(
117 IOOptionBits options
,
119 mach_vm_address_t source
,
122 IOBufferMemoryDescriptor
* inst
;
125 vm_map_address_t address
;
129 err
= kIOReturnNoMemory
;
130 inst
= new IOBufferMemoryDescriptor
;
134 inst
->_ranges
.v64
= IONew(IOAddressRange
, 1);
135 if (!inst
->_ranges
.v64
) {
139 err
= vm_map_copyin(sourceMap
, source
, size
,
140 false /* src_destroy */, ©
);
141 if (KERN_SUCCESS
!= err
) {
145 err
= vm_map_copyout(get_task_map(inTask
), &address
, copy
);
146 if (KERN_SUCCESS
!= err
) {
151 inst
->_ranges
.v64
->address
= address
;
152 inst
->_ranges
.v64
->length
= size
;
154 if (!inst
->initWithPhysicalMask(inTask
, options
, size
, page_size
, 0)) {
155 err
= kIOReturnError
;
159 if (KERN_SUCCESS
== err
) {
164 vm_map_copy_discard(copy
);
166 OSSafeReleaseNULL(inst
);
172 IOBufferMemoryDescriptor::initWithPhysicalMask(
174 IOOptionBits options
,
175 mach_vm_size_t capacity
,
176 mach_vm_address_t alignment
,
177 mach_vm_address_t physicalMask
)
179 task_t mapTask
= NULL
;
180 vm_map_t vmmap
= NULL
;
181 mach_vm_address_t highestMask
= 0;
182 IOOptionBits iomdOptions
= kIOMemoryTypeVirtual64
| kIOMemoryAsReference
;
183 IODMAMapSpecification mapSpec
;
185 bool withCopy
= false;
193 _capacity
= capacity
;
195 _internalReserved
= 0;
199 _ranges
.v64
= IONew(IOAddressRange
, 1);
203 _ranges
.v64
->address
= 0;
204 _ranges
.v64
->length
= 0;
206 if (!_ranges
.v64
->address
) {
209 if (!(kIOMemoryPageable
& options
)) {
215 _buffer
= (void *) _ranges
.v64
->address
;
218 // make sure super::free doesn't dealloc _ranges before super::init
219 _flags
= kIOMemoryAsReference
;
221 // Grab IOMD bits from the Buffer MD options
222 iomdOptions
|= (options
& kIOBufferDescriptorMemoryFlags
);
224 if (!(kIOMemoryMapperNone
& options
)) {
225 IOMapper::checkForSystemMapper();
226 mapped
= (NULL
!= IOMapper::gSystem
);
228 needZero
= (mapped
|| (0 != (kIOMemorySharingTypeMask
& options
)));
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 capacity
= round_page(capacity
);
247 if (alignment
> page_size
) {
248 options
|= kIOMemoryPhysicallyContiguous
;
251 _alignment
= alignment
;
253 if ((capacity
+ alignment
) < _capacity
) {
257 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
)) {
261 bzero(&mapSpec
, sizeof(mapSpec
));
262 mapSpec
.alignment
= _alignment
;
263 mapSpec
.numAddressBits
= 64;
264 if (highestMask
&& mapped
) {
265 if (highestMask
<= 0xFFFFFFFF) {
266 mapSpec
.numAddressBits
= (32 - __builtin_clz((unsigned int) highestMask
));
268 mapSpec
.numAddressBits
= (64 - __builtin_clz((unsigned int) (highestMask
>> 32)));
273 // set memory entry cache mode, pageable, purgeable
274 iomdOptions
|= ((options
& kIOMapCacheMask
) >> kIOMapCacheShift
) << kIOMemoryBufferCacheShift
;
275 if (options
& kIOMemoryPageable
) {
276 iomdOptions
|= kIOMemoryBufferPageable
;
277 if (options
& kIOMemoryPurgeable
) {
278 iomdOptions
|= kIOMemoryBufferPurgeable
;
283 // Buffer shouldn't auto prepare they should be prepared explicitly
284 // But it never was enforced so what are you going to do?
285 iomdOptions
|= kIOMemoryAutoPrepare
;
287 /* Allocate a wired-down buffer inside kernel space. */
289 bool contig
= (0 != (options
& kIOMemoryHostPhysicallyContiguous
));
291 if (!contig
&& (0 != (options
& kIOMemoryPhysicallyContiguous
))) {
293 contig
|= (0 != (kIOMemoryMapperNone
& options
));
295 // treat kIOMemoryPhysicallyContiguous as kIOMemoryHostPhysicallyContiguous for now
300 if (contig
|| highestMask
|| (alignment
> page_size
)) {
301 _internalFlags
|= kInternalFlagPhysical
;
303 _internalFlags
|= kInternalFlagPageSized
;
304 capacity
= round_page(capacity
);
306 _buffer
= (void *) IOKernelAllocateWithPhysicalRestrict(
307 capacity
, highestMask
, alignment
, contig
);
309 && ((capacity
+ alignment
) <= (page_size
- gIOPageAllocChunkBytes
))) {
310 _internalFlags
|= kInternalFlagPageAllocated
;
312 _buffer
= (void *) iopa_alloc(&gIOBMDPageAllocator
, &IOBMDPageProc
, capacity
, alignment
);
314 IOStatisticsAlloc(kIOStatisticsMallocAligned
, capacity
);
316 OSAddAtomic(capacity
, &debug_iomalloc_size
);
319 } else if (alignment
> 1) {
320 _buffer
= IOMallocAligned(capacity
, alignment
);
322 _buffer
= IOMalloc(capacity
);
328 bzero(_buffer
, capacity
);
332 if ((options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
333 vm_size_t size
= round_page(capacity
);
335 // initWithOptions will create memory entry
337 iomdOptions
|= kIOMemoryPersistent
;
340 if (options
& kIOMemoryPageable
) {
342 OSAddAtomicLong(size
, &debug_iomallocpageable_size
);
347 if (NULL
== inTask
) {
348 inTask
= kernel_task
;
350 } else if (options
& kIOMapCacheMask
) {
351 // Prefetch each page to put entries into the pmap
352 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
353 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
355 while (startAddr
< endAddr
) {
356 UInt8 dummyVar
= *startAddr
;
358 startAddr
+= page_size
;
363 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;
364 _ranges
.v64
->length
= _capacity
;
366 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
367 inTask
, iomdOptions
, /* System mapper */ NULL
)) {
371 _internalFlags
|= kInternalFlagInit
;
373 if (!(options
& kIOMemoryPageable
)) {
374 trackingAccumSize(capacity
);
376 #endif /* IOTRACKING */
378 // give any system mapper the allocation params
379 if (kIOReturnSuccess
!= dmaCommandOperation(kIOMDAddDMAMapSpec
,
380 &mapSpec
, sizeof(mapSpec
))) {
386 reserved
= IONew( ExpansionData
, 1 );
391 reserved
->map
= createMappingInTask(mapTask
, 0,
392 kIOMapAnywhere
| (options
& kIOMapPrefault
) | (options
& kIOMapCacheMask
), 0, 0);
393 if (!reserved
->map
) {
397 release(); // map took a retain on this
398 reserved
->map
->retain();
399 removeMapping(reserved
->map
);
400 mach_vm_address_t buffer
= reserved
->map
->getAddress();
401 _buffer
= (void *) buffer
;
402 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
)) {
403 _ranges
.v64
->address
= buffer
;
407 setLength(_capacity
);
412 IOBufferMemoryDescriptor
*
413 IOBufferMemoryDescriptor::inTaskWithOptions(
415 IOOptionBits options
,
417 vm_offset_t alignment
)
419 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
421 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
428 IOBufferMemoryDescriptor
*
429 IOBufferMemoryDescriptor::inTaskWithOptions(
431 IOOptionBits options
,
433 vm_offset_t alignment
,
437 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
440 me
->setVMTags(kernTag
, userTag
);
442 if (!me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
450 IOBufferMemoryDescriptor
*
451 IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
453 IOOptionBits options
,
454 mach_vm_size_t capacity
,
455 mach_vm_address_t physicalMask
)
457 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
459 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
)) {
468 IOBufferMemoryDescriptor::initWithOptions(
469 IOOptionBits options
,
471 vm_offset_t alignment
)
473 return initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0);
475 #endif /* !__LP64__ */
477 IOBufferMemoryDescriptor
*
478 IOBufferMemoryDescriptor::withOptions(
479 IOOptionBits options
,
481 vm_offset_t alignment
)
483 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
485 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
496 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
497 * hold capacity bytes. The descriptor's length is initially set to the capacity.
499 IOBufferMemoryDescriptor
*
500 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
501 IODirection inDirection
,
504 return IOBufferMemoryDescriptor::withOptions(
505 inDirection
| kIOMemoryUnshared
506 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
507 inCapacity
, inContiguous
? inCapacity
: 1 );
514 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
515 * The descriptor's length and capacity are set to the input buffer's size.
518 IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
520 IODirection inDirection
,
523 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
524 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
525 inLength
, inLength
, (mach_vm_address_t
)0)) {
529 // start out with no data
532 if (!appendBytes(inBytes
, inLength
)) {
538 #endif /* !__LP64__ */
543 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
544 * The descriptor's length and capacity are set to the input buffer's size.
546 IOBufferMemoryDescriptor
*
547 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
549 IODirection inDirection
,
552 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
554 if (me
&& !me
->initWithPhysicalMask(
555 kernel_task
, inDirection
| kIOMemoryUnshared
556 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
557 inLength
, inLength
, 0 )) {
563 // start out with no data
566 if (!me
->appendBytes(inBytes
, inLength
)) {
580 IOBufferMemoryDescriptor::free()
582 // Cache all of the relevant information on the stack for use
583 // after we call super::free()!
584 IOOptionBits flags
= _flags
;
585 IOOptionBits internalFlags
= _internalFlags
;
586 IOOptionBits options
= _options
;
587 vm_size_t size
= _capacity
;
588 void * buffer
= _buffer
;
589 IOMemoryMap
* map
= NULL
;
590 IOAddressRange
* range
= _ranges
.v64
;
591 vm_offset_t alignment
= _alignment
;
593 if (alignment
>= page_size
) {
594 size
= round_page(size
);
599 IODelete( reserved
, ExpansionData
, 1 );
605 if ((options
& kIOMemoryPageable
)
606 || (kInternalFlagPageSized
& internalFlags
)) {
607 size
= round_page(size
);
611 if (!(options
& kIOMemoryPageable
)
613 && (kInternalFlagInit
& _internalFlags
)) {
614 trackingAccumSize(-size
);
616 #endif /* IOTRACKING */
618 /* super::free may unwire - deallocate buffer afterwards */
621 if (options
& kIOMemoryPageable
) {
623 OSAddAtomicLong(-size
, &debug_iomallocpageable_size
);
626 if (kInternalFlagPhysical
& internalFlags
) {
627 IOKernelFreePhysical((mach_vm_address_t
) buffer
, size
);
628 } else if (kInternalFlagPageAllocated
& internalFlags
) {
630 page
= iopa_free(&gIOBMDPageAllocator
, (uintptr_t) buffer
, size
);
632 kmem_free(kernel_map
, page
, page_size
);
635 OSAddAtomic(-size
, &debug_iomalloc_size
);
637 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
638 } else if (alignment
> 1) {
639 IOFreeAligned(buffer
, size
);
641 IOFree(buffer
, size
);
644 if (range
&& (kIOMemoryAsReference
& flags
)) {
645 IODelete(range
, IOAddressRange
, 1);
652 * Get the buffer capacity
655 IOBufferMemoryDescriptor::getCapacity() const
663 * Change the buffer length of the memory descriptor. When a new buffer
664 * is created, the initial length of the buffer is set to be the same as
665 * the capacity. The length can be adjusted via setLength for a shorter
666 * transfer (there is no need to create more buffer descriptors when you
667 * can reuse an existing one, even for different transfer sizes). Note
668 * that the specified length must not exceed the capacity of the buffer.
671 IOBufferMemoryDescriptor::setLength(vm_size_t length
)
673 assert(length
<= _capacity
);
674 if (length
> _capacity
) {
679 _ranges
.v64
->length
= length
;
685 * Change the direction of the transfer. This method allows one to redirect
686 * the descriptor's transfer direction. This eliminates the need to destroy
687 * and create new buffers when different transfer directions are needed.
690 IOBufferMemoryDescriptor::setDirection(IODirection direction
)
692 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
694 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
695 #endif /* !__LP64__ */
701 * Add some data to the end of the buffer. This method automatically
702 * maintains the memory descriptor buffer length. Note that appendBytes
703 * will not copy past the end of the memory descriptor's current capacity.
706 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
708 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
711 assert(_length
<= _capacity
);
714 _length
+= actualBytesToCopy
;
715 _ranges
.v64
->length
+= actualBytesToCopy
;
717 if (_task
== kernel_task
) {
718 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
721 writeBytes(offset
, bytes
, actualBytesToCopy
);
730 * Return the virtual address of the beginning of the buffer
733 IOBufferMemoryDescriptor::getBytesNoCopy()
735 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
)) {
738 return (void *)_ranges
.v64
->address
;
746 * Return the virtual address of an offset from the beginning of the buffer
749 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
751 IOVirtualAddress address
;
753 if ((start
+ withLength
) < start
) {
757 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
)) {
758 address
= (IOVirtualAddress
) _buffer
;
760 address
= _ranges
.v64
->address
;
763 if (start
< _length
&& (start
+ withLength
) <= _length
) {
764 return (void *)(address
+ start
);
771 IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
772 IOByteCount
* lengthOfSegment
)
774 void * bytes
= getBytesNoCopy(offset
, 0);
776 if (bytes
&& lengthOfSegment
) {
777 *lengthOfSegment
= _length
- offset
;
782 #endif /* !__LP64__ */
785 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
786 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
787 #else /* !__LP64__ */
788 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
789 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
790 #endif /* !__LP64__ */
791 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
792 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
793 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
794 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
795 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
796 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
797 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
798 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
799 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
800 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
801 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
802 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
803 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
804 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);