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>
39 #include "IOKitKernelInternal.h"
40 #include "IOCopyMapper.h"
43 void ipc_port_release_send(ipc_port_t port
);
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
52 kInternalFlagRealloc
= 0x00000001,
55 volatile ppnum_t gIOHighestAllocatedPage
;
57 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
59 #define super IOGeneralMemoryDescriptor
60 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
61 IOGeneralMemoryDescriptor
);
63 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66 bool IOBufferMemoryDescriptor::initWithOptions(
69 vm_offset_t alignment
,
72 mach_vm_address_t physicalMask
= 0;
73 return (initWithPhysicalMask(inTask
, options
, capacity
, alignment
, physicalMask
));
75 #endif /* !__LP64__ */
77 bool IOBufferMemoryDescriptor::initWithPhysicalMask(
80 mach_vm_size_t capacity
,
81 mach_vm_address_t alignment
,
82 mach_vm_address_t physicalMask
)
85 task_t mapTask
= NULL
;
86 vm_map_t vmmap
= NULL
;
88 mach_vm_address_t highestMask
= 0;
90 IOOptionBits iomdOptions
= kIOMemoryTypeVirtual64
| kIOMemoryAsReference
;
98 _internalReserved
= 0;
101 _ranges
.v64
= IONew(IOAddressRange
, 1);
104 _ranges
.v64
->address
= 0;
105 _ranges
.v64
->length
= 0;
107 // Grab IOMD bits from the Buffer MD options
108 iomdOptions
|= (options
& kIOBufferDescriptorMemoryFlags
);
110 if (physicalMask
&& (alignment
<= 1))
112 alignment
= ((physicalMask
^ (-1ULL)) & (physicalMask
- 1));
113 highestMask
= (physicalMask
| alignment
);
117 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
)) && (alignment
< page_size
))
118 alignment
= page_size
;
120 if (alignment
>= page_size
)
121 capacity
= round_page(capacity
);
123 if (alignment
> page_size
)
124 options
|= kIOMemoryPhysicallyContiguous
;
126 _alignment
= alignment
;
128 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
))
131 if ((options
& kIOMemoryPhysicallyContiguous
) && !physicalMask
)
132 physicalMask
= 0xFFFFFFFF;
134 // set flags for entry + object create
135 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
;
137 // set memory entry cache mode
138 switch (options
& kIOMapCacheMask
)
140 case kIOMapInhibitCache
:
141 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
144 case kIOMapWriteThruCache
:
145 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
148 case kIOMapWriteCombineCache
:
149 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
152 case kIOMapCopybackCache
:
153 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
156 case kIOMapDefaultCache
:
158 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
162 if (options
& kIOMemoryPageable
)
164 iomdOptions
|= kIOMemoryBufferPageable
;
166 // must create the entry before any pages are allocated
168 // set flags for entry + object create
169 memEntryCacheMode
|= MAP_MEM_NAMED_CREATE
;
171 if (options
& kIOMemoryPurgeable
)
172 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
176 memEntryCacheMode
|= MAP_MEM_NAMED_REUSE
;
178 if (IOMapper::gSystem
)
179 // assuming mapped space is 2G
180 lastIOAddr
= (1UL << 31) - PAGE_SIZE
;
182 lastIOAddr
= ptoa_64(gIOHighestAllocatedPage
);
184 usePhys
= (highestMask
&& (lastIOAddr
!= (lastIOAddr
& highestMask
))
185 && (alignment
<= page_size
));
187 if (!usePhys
&& (options
& kIOMemoryPhysicallyContiguous
))
189 _buffer
= (void *) IOKernelAllocateContiguous(capacity
, highestMask
, alignment
);
190 usePhys
= (NULL
== _buffer
);
194 mach_vm_address_t address
;
195 iomdOptions
&= ~kIOMemoryTypeVirtual64
;
196 iomdOptions
|= kIOMemoryTypePhysical64
;
198 address
= IOMallocPhysical(capacity
, highestMask
);
199 _buffer
= (void *) address
;
210 // Buffer shouldn't auto prepare they should be prepared explicitly
211 // But it never was enforced so what are you going to do?
212 iomdOptions
|= kIOMemoryAutoPrepare
;
214 /* Allocate a wired-down buffer inside kernel space. */
215 if (options
& kIOMemoryPhysicallyContiguous
)
217 // attempted allocate already
219 else if (alignment
> 1)
221 _buffer
= IOMallocAligned(capacity
, alignment
);
225 _buffer
= IOMalloc(capacity
);
232 if( (kIOMemoryTypePhysical64
!= (kIOMemoryTypeMask
& iomdOptions
))
233 && (options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
234 ipc_port_t sharedMem
;
235 vm_size_t size
= round_page(capacity
);
237 kr
= mach_make_memory_entry(vmmap
,
238 &size
, (vm_offset_t
)_buffer
,
239 memEntryCacheMode
, &sharedMem
,
242 if( (KERN_SUCCESS
== kr
) && (size
!= round_page(capacity
))) {
243 ipc_port_release_send( sharedMem
);
244 kr
= kIOReturnVMError
;
246 if( KERN_SUCCESS
!= kr
)
249 _memEntry
= (void *) sharedMem
;
251 if( options
& kIOMemoryPageable
) {
253 debug_iomallocpageable_size
+= size
;
257 inTask
= kernel_task
;
259 else if (options
& kIOMapCacheMask
)
261 // Prefetch each page to put entries into the pmap
262 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
263 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
265 while (startAddr
< endAddr
)
268 startAddr
+= page_size
;
273 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;;
274 _ranges
.v64
->length
= _capacity
;
276 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
277 inTask
, iomdOptions
, /* System mapper */ 0))
280 if (highestMask
&& !IOMapper::gSystem
)
282 IOMDDMACharacteristics mdSummary
;
284 bzero(&mdSummary
, sizeof(mdSummary
));
285 IOReturn rtn
= dmaCommandOperation(
286 kIOMDGetCharacteristics
,
287 &mdSummary
, sizeof(mdSummary
));
291 if (mdSummary
.fHighestPage
)
294 while (mdSummary
.fHighestPage
> (highest
= gIOHighestAllocatedPage
))
296 if (OSCompareAndSwap(highest
, mdSummary
.fHighestPage
,
297 (UInt32
*) &gIOHighestAllocatedPage
))
300 lastIOAddr
= ptoa_64(mdSummary
.fHighestPage
);
303 lastIOAddr
= ptoa_64(gIOLastPage
);
305 if (lastIOAddr
!= (lastIOAddr
& highestMask
))
307 if (kIOMemoryTypePhysical64
!= (_flags
& kIOMemoryTypeMask
))
310 _internalFlags
|= kInternalFlagRealloc
;
319 reserved
= IONew( ExpansionData
, 1 );
323 reserved
->map
= createMappingInTask(mapTask
, 0,
324 kIOMapAnywhere
| (options
& kIOMapCacheMask
), 0, 0);
330 release(); // map took a retain on this
331 reserved
->map
->retain();
332 removeMapping(reserved
->map
);
333 mach_vm_address_t buffer
= reserved
->map
->getAddress();
334 _buffer
= (void *) buffer
;
335 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
))
336 _ranges
.v64
->address
= buffer
;
339 setLength(_capacity
);
344 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
346 IOOptionBits options
,
348 vm_offset_t alignment
)
350 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
352 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
353 bool retry
= (0 != (kInternalFlagRealloc
& me
->_internalFlags
));
358 me
= new IOBufferMemoryDescriptor
;
359 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0))
369 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
371 IOOptionBits options
,
372 mach_vm_size_t capacity
,
373 mach_vm_address_t physicalMask
)
375 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
377 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
379 bool retry
= (0 != (kInternalFlagRealloc
& me
->_internalFlags
));
384 me
= new IOBufferMemoryDescriptor
;
385 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
396 bool IOBufferMemoryDescriptor::initWithOptions(
397 IOOptionBits options
,
399 vm_offset_t alignment
)
401 return (initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0));
403 #endif /* !__LP64__ */
405 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
406 IOOptionBits options
,
408 vm_offset_t alignment
)
410 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
412 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
413 bool retry
= (0 != (kInternalFlagRealloc
& me
->_internalFlags
));
418 me
= new IOBufferMemoryDescriptor
;
419 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0))
433 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
434 * hold capacity bytes. The descriptor's length is initially set to the capacity.
436 IOBufferMemoryDescriptor
*
437 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
438 IODirection inDirection
,
441 return( IOBufferMemoryDescriptor::withOptions(
442 inDirection
| kIOMemoryUnshared
443 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
444 inCapacity
, inContiguous
? inCapacity
: 1 ));
451 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
452 * The descriptor's length and capacity are set to the input buffer's size.
454 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
456 IODirection inDirection
,
459 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
460 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
461 inLength
, inLength
, (mach_vm_address_t
)0))
464 // start out with no data
467 if (!appendBytes(inBytes
, inLength
))
472 #endif /* !__LP64__ */
477 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
478 * The descriptor's length and capacity are set to the input buffer's size.
480 IOBufferMemoryDescriptor
*
481 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
483 IODirection inDirection
,
486 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
488 if (me
&& !me
->initWithPhysicalMask(
489 kernel_task
, inDirection
| kIOMemoryUnshared
490 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
491 inLength
, inLength
, 0 ))
493 bool retry
= (0 != (kInternalFlagRealloc
& me
->_internalFlags
));
498 me
= new IOBufferMemoryDescriptor
;
499 if (me
&& !me
->initWithPhysicalMask(
500 kernel_task
, inDirection
| kIOMemoryUnshared
501 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
502 inLength
, inLength
, 0 ))
513 // start out with no data
516 if (!me
->appendBytes(inBytes
, inLength
))
530 void IOBufferMemoryDescriptor::free()
532 // Cache all of the relevant information on the stack for use
533 // after we call super::free()!
534 IOOptionBits flags
= _flags
;
535 IOOptionBits options
= _options
;
536 vm_size_t size
= _capacity
;
537 void * buffer
= _buffer
;
538 IOMemoryMap
* map
= 0;
539 IOAddressRange
* range
= _ranges
.v64
;
540 mach_vm_address_t source
= range
? range
->address
: 0;
541 vm_offset_t alignment
= _alignment
;
543 if (alignment
>= page_size
)
544 size
= round_page(size
);
549 IODelete( reserved
, ExpansionData
, 1 );
554 /* super::free may unwire - deallocate buffer afterwards */
557 if (options
& kIOMemoryPageable
)
560 debug_iomallocpageable_size
-= round_page(size
);
565 if (kIOMemoryTypePhysical64
== (flags
& kIOMemoryTypeMask
))
566 IOFreePhysical(source
, size
);
567 else if (options
& kIOMemoryPhysicallyContiguous
)
568 IOKernelFreeContiguous((mach_vm_address_t
) buffer
, size
);
569 else if (alignment
> 1)
570 IOFreeAligned(buffer
, size
);
572 IOFree(buffer
, size
);
574 if (range
&& (kIOMemoryAsReference
& flags
))
575 IODelete(range
, IOAddressRange
, 1);
581 * Get the buffer capacity
583 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
591 * Change the buffer length of the memory descriptor. When a new buffer
592 * is created, the initial length of the buffer is set to be the same as
593 * the capacity. The length can be adjusted via setLength for a shorter
594 * transfer (there is no need to create more buffer descriptors when you
595 * can reuse an existing one, even for different transfer sizes). Note
596 * that the specified length must not exceed the capacity of the buffer.
598 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
600 assert(length
<= _capacity
);
603 _ranges
.v64
->length
= length
;
609 * Change the direction of the transfer. This method allows one to redirect
610 * the descriptor's transfer direction. This eliminates the need to destroy
611 * and create new buffers when different transfer directions are needed.
613 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
615 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
617 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
618 #endif /* !__LP64__ */
624 * Add some data to the end of the buffer. This method automatically
625 * maintains the memory descriptor buffer length. Note that appendBytes
626 * will not copy past the end of the memory descriptor's current capacity.
629 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
631 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
634 assert(_length
<= _capacity
);
637 _length
+= actualBytesToCopy
;
638 _ranges
.v64
->length
+= actualBytesToCopy
;
640 if (_task
== kernel_task
)
641 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
644 writeBytes(offset
, bytes
, actualBytesToCopy
);
652 * Return the virtual address of the beginning of the buffer
654 void * IOBufferMemoryDescriptor::getBytesNoCopy()
656 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
659 return (void *)_ranges
.v64
->address
;
666 * Return the virtual address of an offset from the beginning of the buffer
669 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
671 IOVirtualAddress address
;
672 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
673 address
= (IOVirtualAddress
) _buffer
;
675 address
= _ranges
.v64
->address
;
677 if (start
< _length
&& (start
+ withLength
) <= _length
)
678 return (void *)(address
+ start
);
683 void * IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
684 IOByteCount
* lengthOfSegment
)
686 void * bytes
= getBytesNoCopy(offset
, 0);
688 if (bytes
&& lengthOfSegment
)
689 *lengthOfSegment
= _length
- offset
;
693 #endif /* !__LP64__ */
696 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
697 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
698 #else /* !__LP64__ */
699 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
700 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
701 #endif /* !__LP64__ */
702 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
703 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
704 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
705 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
706 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
707 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
708 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
709 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
710 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
711 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
712 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
713 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
714 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
715 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);