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"
42 void ipc_port_release_send(ipc_port_t port
);
47 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
51 kInternalFlagPhysical
= 0x00000001,
52 kInternalFlagPageSized
= 0x00000002
55 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
57 #define super IOGeneralMemoryDescriptor
58 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
59 IOGeneralMemoryDescriptor
);
61 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
64 bool IOBufferMemoryDescriptor::initWithOptions(
67 vm_offset_t alignment
,
70 mach_vm_address_t physicalMask
= 0;
71 return (initWithPhysicalMask(inTask
, options
, capacity
, alignment
, physicalMask
));
73 #endif /* !__LP64__ */
75 bool IOBufferMemoryDescriptor::initWithPhysicalMask(
78 mach_vm_size_t capacity
,
79 mach_vm_address_t alignment
,
80 mach_vm_address_t physicalMask
)
83 task_t mapTask
= NULL
;
84 vm_map_t vmmap
= NULL
;
85 mach_vm_address_t highestMask
= 0;
86 IOOptionBits iomdOptions
= kIOMemoryTypeVirtual64
| kIOMemoryAsReference
;
94 _internalReserved
= 0;
97 _ranges
.v64
= IONew(IOAddressRange
, 1);
100 _ranges
.v64
->address
= 0;
101 _ranges
.v64
->length
= 0;
102 // make sure super::free doesn't dealloc _ranges before super::init
103 _flags
= kIOMemoryAsReference
;
105 // Grab IOMD bits from the Buffer MD options
106 iomdOptions
|= (options
& kIOBufferDescriptorMemoryFlags
);
108 if (physicalMask
&& (alignment
<= 1))
110 alignment
= ((physicalMask
^ (-1ULL)) & (physicalMask
- 1));
111 highestMask
= (physicalMask
| alignment
);
113 if (alignment
< page_size
)
114 alignment
= page_size
;
117 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
| kIOMemoryClearEncrypt
)) && (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 // set flags for entry + object create
132 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
;
134 // set memory entry cache mode
135 switch (options
& kIOMapCacheMask
)
137 case kIOMapInhibitCache
:
138 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
141 case kIOMapWriteThruCache
:
142 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
145 case kIOMapWriteCombineCache
:
146 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
149 case kIOMapCopybackCache
:
150 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
153 case kIOMapCopybackInnerCache
:
154 SET_MAP_MEM(MAP_MEM_INNERWBACK
, memEntryCacheMode
);
157 case kIOMapDefaultCache
:
159 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
163 if (options
& kIOMemoryPageable
)
165 iomdOptions
|= kIOMemoryBufferPageable
;
167 // must create the entry before any pages are allocated
169 // set flags for entry + object create
170 memEntryCacheMode
|= MAP_MEM_NAMED_CREATE
;
172 if (options
& kIOMemoryPurgeable
)
173 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
177 memEntryCacheMode
|= MAP_MEM_NAMED_REUSE
;
180 // Buffer shouldn't auto prepare they should be prepared explicitly
181 // But it never was enforced so what are you going to do?
182 iomdOptions
|= kIOMemoryAutoPrepare
;
184 /* Allocate a wired-down buffer inside kernel space. */
186 if ((options
& kIOMemoryPhysicallyContiguous
) || highestMask
|| (alignment
> page_size
))
188 _internalFlags
|= kInternalFlagPhysical
;
191 _internalFlags
|= kInternalFlagPageSized
;
192 capacity
= round_page(capacity
);
194 _buffer
= (void *) IOKernelAllocateWithPhysicalRestrict(capacity
, highestMask
, alignment
,
195 (0 != (options
& kIOMemoryPhysicallyContiguous
)));
197 else if (alignment
> 1)
199 _buffer
= IOMallocAligned(capacity
, alignment
);
203 _buffer
= IOMalloc(capacity
);
212 if( (options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
213 ipc_port_t sharedMem
;
214 vm_size_t size
= round_page(capacity
);
216 kr
= mach_make_memory_entry(vmmap
,
217 &size
, (vm_offset_t
)_buffer
,
218 memEntryCacheMode
, &sharedMem
,
221 if( (KERN_SUCCESS
== kr
) && (size
!= round_page(capacity
))) {
222 ipc_port_release_send( sharedMem
);
223 kr
= kIOReturnVMError
;
225 if( KERN_SUCCESS
!= kr
)
228 _memEntry
= (void *) sharedMem
;
230 if( options
& kIOMemoryPageable
) {
232 debug_iomallocpageable_size
+= size
;
236 inTask
= kernel_task
;
238 else if (options
& kIOMapCacheMask
)
240 // Prefetch each page to put entries into the pmap
241 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
242 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
244 while (startAddr
< endAddr
)
247 startAddr
+= page_size
;
252 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;;
253 _ranges
.v64
->length
= _capacity
;
255 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
256 inTask
, iomdOptions
, /* System mapper */ 0))
262 reserved
= IONew( ExpansionData
, 1 );
266 reserved
->map
= createMappingInTask(mapTask
, 0,
267 kIOMapAnywhere
| (options
& kIOMapCacheMask
), 0, 0);
273 release(); // map took a retain on this
274 reserved
->map
->retain();
275 removeMapping(reserved
->map
);
276 mach_vm_address_t buffer
= reserved
->map
->getAddress();
277 _buffer
= (void *) buffer
;
278 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
))
279 _ranges
.v64
->address
= buffer
;
282 setLength(_capacity
);
287 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
289 IOOptionBits options
,
291 vm_offset_t alignment
)
293 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
295 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
302 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
304 IOOptionBits options
,
305 mach_vm_size_t capacity
,
306 mach_vm_address_t physicalMask
)
308 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
310 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
319 bool IOBufferMemoryDescriptor::initWithOptions(
320 IOOptionBits options
,
322 vm_offset_t alignment
)
324 return (initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0));
326 #endif /* !__LP64__ */
328 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
329 IOOptionBits options
,
331 vm_offset_t alignment
)
333 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
335 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
346 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
347 * hold capacity bytes. The descriptor's length is initially set to the capacity.
349 IOBufferMemoryDescriptor
*
350 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
351 IODirection inDirection
,
354 return( IOBufferMemoryDescriptor::withOptions(
355 inDirection
| kIOMemoryUnshared
356 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
357 inCapacity
, inContiguous
? inCapacity
: 1 ));
364 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
365 * The descriptor's length and capacity are set to the input buffer's size.
367 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
369 IODirection inDirection
,
372 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
373 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
374 inLength
, inLength
, (mach_vm_address_t
)0))
377 // start out with no data
380 if (!appendBytes(inBytes
, inLength
))
385 #endif /* !__LP64__ */
390 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
391 * The descriptor's length and capacity are set to the input buffer's size.
393 IOBufferMemoryDescriptor
*
394 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
396 IODirection inDirection
,
399 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
401 if (me
&& !me
->initWithPhysicalMask(
402 kernel_task
, inDirection
| kIOMemoryUnshared
403 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
404 inLength
, inLength
, 0 ))
412 // start out with no data
415 if (!me
->appendBytes(inBytes
, inLength
))
429 void IOBufferMemoryDescriptor::free()
431 // Cache all of the relevant information on the stack for use
432 // after we call super::free()!
433 IOOptionBits flags
= _flags
;
434 IOOptionBits internalFlags
= _internalFlags
;
435 IOOptionBits options
= _options
;
436 vm_size_t size
= _capacity
;
437 void * buffer
= _buffer
;
438 IOMemoryMap
* map
= 0;
439 IOAddressRange
* range
= _ranges
.v64
;
440 vm_offset_t alignment
= _alignment
;
442 if (alignment
>= page_size
)
443 size
= round_page(size
);
448 IODelete( reserved
, ExpansionData
, 1 );
453 /* super::free may unwire - deallocate buffer afterwards */
456 if (options
& kIOMemoryPageable
)
459 debug_iomallocpageable_size
-= round_page(size
);
464 if (internalFlags
& kInternalFlagPhysical
)
466 if (kInternalFlagPageSized
& internalFlags
)
467 size
= round_page(size
);
468 IOKernelFreePhysical((mach_vm_address_t
) buffer
, size
);
470 else if (alignment
> 1)
471 IOFreeAligned(buffer
, size
);
473 IOFree(buffer
, size
);
475 if (range
&& (kIOMemoryAsReference
& flags
))
476 IODelete(range
, IOAddressRange
, 1);
482 * Get the buffer capacity
484 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
492 * Change the buffer length of the memory descriptor. When a new buffer
493 * is created, the initial length of the buffer is set to be the same as
494 * the capacity. The length can be adjusted via setLength for a shorter
495 * transfer (there is no need to create more buffer descriptors when you
496 * can reuse an existing one, even for different transfer sizes). Note
497 * that the specified length must not exceed the capacity of the buffer.
499 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
501 assert(length
<= _capacity
);
504 _ranges
.v64
->length
= length
;
510 * Change the direction of the transfer. This method allows one to redirect
511 * the descriptor's transfer direction. This eliminates the need to destroy
512 * and create new buffers when different transfer directions are needed.
514 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
516 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
518 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
519 #endif /* !__LP64__ */
525 * Add some data to the end of the buffer. This method automatically
526 * maintains the memory descriptor buffer length. Note that appendBytes
527 * will not copy past the end of the memory descriptor's current capacity.
530 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
532 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
535 assert(_length
<= _capacity
);
538 _length
+= actualBytesToCopy
;
539 _ranges
.v64
->length
+= actualBytesToCopy
;
541 if (_task
== kernel_task
)
542 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
545 writeBytes(offset
, bytes
, actualBytesToCopy
);
553 * Return the virtual address of the beginning of the buffer
555 void * IOBufferMemoryDescriptor::getBytesNoCopy()
557 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
560 return (void *)_ranges
.v64
->address
;
567 * Return the virtual address of an offset from the beginning of the buffer
570 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
572 IOVirtualAddress address
;
573 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
574 address
= (IOVirtualAddress
) _buffer
;
576 address
= _ranges
.v64
->address
;
578 if (start
< _length
&& (start
+ withLength
) <= _length
)
579 return (void *)(address
+ start
);
584 void * IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
585 IOByteCount
* lengthOfSegment
)
587 void * bytes
= getBytesNoCopy(offset
, 0);
589 if (bytes
&& lengthOfSegment
)
590 *lengthOfSegment
= _length
- offset
;
594 #endif /* !__LP64__ */
597 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
598 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
599 #else /* !__LP64__ */
600 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
601 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
602 #endif /* !__LP64__ */
603 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
604 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
605 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
606 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
607 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
608 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
609 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
610 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
611 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
612 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
613 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
614 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
615 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
616 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);