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__ */
115 IOBufferMemoryDescriptor::initWithPhysicalMask(
117 IOOptionBits options
,
118 mach_vm_size_t capacity
,
119 mach_vm_address_t alignment
,
120 mach_vm_address_t physicalMask
)
122 task_t mapTask
= NULL
;
123 vm_map_t vmmap
= NULL
;
124 mach_vm_address_t highestMask
= 0;
125 IOOptionBits iomdOptions
= kIOMemoryTypeVirtual64
| kIOMemoryAsReference
;
126 IODMAMapSpecification mapSpec
;
135 _capacity
= capacity
;
137 _internalReserved
= 0;
140 _ranges
.v64
= IONew(IOAddressRange
, 1);
144 _ranges
.v64
->address
= 0;
145 _ranges
.v64
->length
= 0;
146 // make sure super::free doesn't dealloc _ranges before super::init
147 _flags
= kIOMemoryAsReference
;
149 // Grab IOMD bits from the Buffer MD options
150 iomdOptions
|= (options
& kIOBufferDescriptorMemoryFlags
);
152 if (!(kIOMemoryMapperNone
& options
)) {
153 IOMapper::checkForSystemMapper();
154 mapped
= (0 != IOMapper::gSystem
);
156 needZero
= (mapped
|| (0 != (kIOMemorySharingTypeMask
& options
)));
158 if (physicalMask
&& (alignment
<= 1)) {
159 alignment
= ((physicalMask
^ (-1ULL)) & (physicalMask
- 1));
160 highestMask
= (physicalMask
| alignment
);
162 if (alignment
< page_size
) {
163 alignment
= page_size
;
167 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
| kIOMemoryClearEncrypt
)) && (alignment
< page_size
)) {
168 alignment
= page_size
;
171 if (alignment
>= page_size
) {
172 capacity
= round_page(capacity
);
175 if (alignment
> page_size
) {
176 options
|= kIOMemoryPhysicallyContiguous
;
179 _alignment
= alignment
;
181 if ((capacity
+ alignment
) < _capacity
) {
185 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
)) {
189 bzero(&mapSpec
, sizeof(mapSpec
));
190 mapSpec
.alignment
= _alignment
;
191 mapSpec
.numAddressBits
= 64;
192 if (highestMask
&& mapped
) {
193 if (highestMask
<= 0xFFFFFFFF) {
194 mapSpec
.numAddressBits
= (32 - __builtin_clz((unsigned int) highestMask
));
196 mapSpec
.numAddressBits
= (64 - __builtin_clz((unsigned int) (highestMask
>> 32)));
201 // set memory entry cache mode, pageable, purgeable
202 iomdOptions
|= ((options
& kIOMapCacheMask
) >> kIOMapCacheShift
) << kIOMemoryBufferCacheShift
;
203 if (options
& kIOMemoryPageable
) {
204 iomdOptions
|= kIOMemoryBufferPageable
;
205 if (options
& kIOMemoryPurgeable
) {
206 iomdOptions
|= kIOMemoryBufferPurgeable
;
211 // Buffer shouldn't auto prepare they should be prepared explicitly
212 // But it never was enforced so what are you going to do?
213 iomdOptions
|= kIOMemoryAutoPrepare
;
215 /* Allocate a wired-down buffer inside kernel space. */
217 bool contig
= (0 != (options
& kIOMemoryHostPhysicallyContiguous
));
219 if (!contig
&& (0 != (options
& kIOMemoryPhysicallyContiguous
))) {
221 contig
|= (0 != (kIOMemoryMapperNone
& options
));
223 // treat kIOMemoryPhysicallyContiguous as kIOMemoryHostPhysicallyContiguous for now
228 if (contig
|| highestMask
|| (alignment
> page_size
)) {
229 _internalFlags
|= kInternalFlagPhysical
;
231 _internalFlags
|= kInternalFlagPageSized
;
232 capacity
= round_page(capacity
);
234 _buffer
= (void *) IOKernelAllocateWithPhysicalRestrict(
235 capacity
, highestMask
, alignment
, contig
);
237 && ((capacity
+ alignment
) <= (page_size
- gIOPageAllocChunkBytes
))) {
238 _internalFlags
|= kInternalFlagPageAllocated
;
240 _buffer
= (void *) iopa_alloc(&gIOBMDPageAllocator
, &IOBMDPageProc
, capacity
, alignment
);
242 IOStatisticsAlloc(kIOStatisticsMallocAligned
, capacity
);
244 OSAddAtomic(capacity
, &debug_iomalloc_size
);
247 } else if (alignment
> 1) {
248 _buffer
= IOMallocAligned(capacity
, alignment
);
250 _buffer
= IOMalloc(capacity
);
256 bzero(_buffer
, capacity
);
260 if ((options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
261 vm_size_t size
= round_page(capacity
);
263 // initWithOptions will create memory entry
264 iomdOptions
|= kIOMemoryPersistent
;
266 if (options
& kIOMemoryPageable
) {
268 OSAddAtomicLong(size
, &debug_iomallocpageable_size
);
271 if (NULL
== inTask
) {
272 inTask
= kernel_task
;
274 } else if (options
& kIOMapCacheMask
) {
275 // Prefetch each page to put entries into the pmap
276 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
277 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
279 while (startAddr
< endAddr
) {
280 UInt8 dummyVar
= *startAddr
;
282 startAddr
+= page_size
;
287 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;;
288 _ranges
.v64
->length
= _capacity
;
290 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
291 inTask
, iomdOptions
, /* System mapper */ 0)) {
295 _internalFlags
|= kInternalFlagInit
;
297 if (!(options
& kIOMemoryPageable
)) {
298 trackingAccumSize(capacity
);
300 #endif /* IOTRACKING */
302 // give any system mapper the allocation params
303 if (kIOReturnSuccess
!= dmaCommandOperation(kIOMDAddDMAMapSpec
,
304 &mapSpec
, sizeof(mapSpec
))) {
310 reserved
= IONew( ExpansionData
, 1 );
315 reserved
->map
= createMappingInTask(mapTask
, 0,
316 kIOMapAnywhere
| (options
& kIOMapPrefault
) | (options
& kIOMapCacheMask
), 0, 0);
317 if (!reserved
->map
) {
321 release(); // map took a retain on this
322 reserved
->map
->retain();
323 removeMapping(reserved
->map
);
324 mach_vm_address_t buffer
= reserved
->map
->getAddress();
325 _buffer
= (void *) buffer
;
326 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
)) {
327 _ranges
.v64
->address
= buffer
;
331 setLength(_capacity
);
336 IOBufferMemoryDescriptor
*
337 IOBufferMemoryDescriptor::inTaskWithOptions(
339 IOOptionBits options
,
341 vm_offset_t alignment
)
343 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
345 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
352 IOBufferMemoryDescriptor
*
353 IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
355 IOOptionBits options
,
356 mach_vm_size_t capacity
,
357 mach_vm_address_t physicalMask
)
359 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
361 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
)) {
370 IOBufferMemoryDescriptor::initWithOptions(
371 IOOptionBits options
,
373 vm_offset_t alignment
)
375 return initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0);
377 #endif /* !__LP64__ */
379 IOBufferMemoryDescriptor
*
380 IOBufferMemoryDescriptor::withOptions(
381 IOOptionBits options
,
383 vm_offset_t alignment
)
385 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
387 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
398 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
399 * hold capacity bytes. The descriptor's length is initially set to the capacity.
401 IOBufferMemoryDescriptor
*
402 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
403 IODirection inDirection
,
406 return IOBufferMemoryDescriptor::withOptions(
407 inDirection
| kIOMemoryUnshared
408 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
409 inCapacity
, inContiguous
? inCapacity
: 1 );
416 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
417 * The descriptor's length and capacity are set to the input buffer's size.
420 IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
422 IODirection inDirection
,
425 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
426 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
427 inLength
, inLength
, (mach_vm_address_t
)0)) {
431 // start out with no data
434 if (!appendBytes(inBytes
, inLength
)) {
440 #endif /* !__LP64__ */
445 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
446 * The descriptor's length and capacity are set to the input buffer's size.
448 IOBufferMemoryDescriptor
*
449 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
451 IODirection inDirection
,
454 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
456 if (me
&& !me
->initWithPhysicalMask(
457 kernel_task
, inDirection
| kIOMemoryUnshared
458 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
459 inLength
, inLength
, 0 )) {
465 // start out with no data
468 if (!me
->appendBytes(inBytes
, inLength
)) {
482 IOBufferMemoryDescriptor::free()
484 // Cache all of the relevant information on the stack for use
485 // after we call super::free()!
486 IOOptionBits flags
= _flags
;
487 IOOptionBits internalFlags
= _internalFlags
;
488 IOOptionBits options
= _options
;
489 vm_size_t size
= _capacity
;
490 void * buffer
= _buffer
;
491 IOMemoryMap
* map
= 0;
492 IOAddressRange
* range
= _ranges
.v64
;
493 vm_offset_t alignment
= _alignment
;
495 if (alignment
>= page_size
) {
496 size
= round_page(size
);
501 IODelete( reserved
, ExpansionData
, 1 );
507 if ((options
& kIOMemoryPageable
)
508 || (kInternalFlagPageSized
& internalFlags
)) {
509 size
= round_page(size
);
513 if (!(options
& kIOMemoryPageable
)
515 && (kInternalFlagInit
& _internalFlags
)) {
516 trackingAccumSize(-size
);
518 #endif /* IOTRACKING */
520 /* super::free may unwire - deallocate buffer afterwards */
523 if (options
& kIOMemoryPageable
) {
525 OSAddAtomicLong(-size
, &debug_iomallocpageable_size
);
528 if (kInternalFlagPhysical
& internalFlags
) {
529 IOKernelFreePhysical((mach_vm_address_t
) buffer
, size
);
530 } else if (kInternalFlagPageAllocated
& internalFlags
) {
532 page
= iopa_free(&gIOBMDPageAllocator
, (uintptr_t) buffer
, size
);
534 kmem_free(kernel_map
, page
, page_size
);
537 OSAddAtomic(-size
, &debug_iomalloc_size
);
539 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
540 } else if (alignment
> 1) {
541 IOFreeAligned(buffer
, size
);
543 IOFree(buffer
, size
);
546 if (range
&& (kIOMemoryAsReference
& flags
)) {
547 IODelete(range
, IOAddressRange
, 1);
554 * Get the buffer capacity
557 IOBufferMemoryDescriptor::getCapacity() const
565 * Change the buffer length of the memory descriptor. When a new buffer
566 * is created, the initial length of the buffer is set to be the same as
567 * the capacity. The length can be adjusted via setLength for a shorter
568 * transfer (there is no need to create more buffer descriptors when you
569 * can reuse an existing one, even for different transfer sizes). Note
570 * that the specified length must not exceed the capacity of the buffer.
573 IOBufferMemoryDescriptor::setLength(vm_size_t length
)
575 assert(length
<= _capacity
);
576 if (length
> _capacity
) {
581 _ranges
.v64
->length
= length
;
587 * Change the direction of the transfer. This method allows one to redirect
588 * the descriptor's transfer direction. This eliminates the need to destroy
589 * and create new buffers when different transfer directions are needed.
592 IOBufferMemoryDescriptor::setDirection(IODirection direction
)
594 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
596 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
597 #endif /* !__LP64__ */
603 * Add some data to the end of the buffer. This method automatically
604 * maintains the memory descriptor buffer length. Note that appendBytes
605 * will not copy past the end of the memory descriptor's current capacity.
608 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
610 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
613 assert(_length
<= _capacity
);
616 _length
+= actualBytesToCopy
;
617 _ranges
.v64
->length
+= actualBytesToCopy
;
619 if (_task
== kernel_task
) {
620 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
623 writeBytes(offset
, bytes
, actualBytesToCopy
);
632 * Return the virtual address of the beginning of the buffer
635 IOBufferMemoryDescriptor::getBytesNoCopy()
637 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
)) {
640 return (void *)_ranges
.v64
->address
;
648 * Return the virtual address of an offset from the beginning of the buffer
651 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
653 IOVirtualAddress address
;
655 if ((start
+ withLength
) < start
) {
659 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
)) {
660 address
= (IOVirtualAddress
) _buffer
;
662 address
= _ranges
.v64
->address
;
665 if (start
< _length
&& (start
+ withLength
) <= _length
) {
666 return (void *)(address
+ start
);
673 IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
674 IOByteCount
* lengthOfSegment
)
676 void * bytes
= getBytesNoCopy(offset
, 0);
678 if (bytes
&& lengthOfSegment
) {
679 *lengthOfSegment
= _length
- offset
;
684 #endif /* !__LP64__ */
687 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
688 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
689 #else /* !__LP64__ */
690 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
691 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
692 #endif /* !__LP64__ */
693 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
694 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
695 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
696 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
697 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
698 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
699 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
700 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
701 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
702 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
703 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
704 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
705 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
706 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);