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;
103 // Grab IOMD bits from the Buffer MD options
104 iomdOptions
|= (options
& kIOBufferDescriptorMemoryFlags
);
106 if (physicalMask
&& (alignment
<= 1))
108 alignment
= ((physicalMask
^ (-1ULL)) & (physicalMask
- 1));
109 highestMask
= (physicalMask
| alignment
);
111 if (alignment
< page_size
)
112 alignment
= page_size
;
115 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
| kIOMemoryClearEncrypt
)) && (alignment
< page_size
))
116 alignment
= page_size
;
118 if (alignment
>= page_size
)
119 capacity
= round_page(capacity
);
121 if (alignment
> page_size
)
122 options
|= kIOMemoryPhysicallyContiguous
;
124 _alignment
= alignment
;
126 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
))
129 // set flags for entry + object create
130 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
;
132 // set memory entry cache mode
133 switch (options
& kIOMapCacheMask
)
135 case kIOMapInhibitCache
:
136 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
139 case kIOMapWriteThruCache
:
140 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
143 case kIOMapWriteCombineCache
:
144 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
147 case kIOMapCopybackCache
:
148 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
151 case kIOMapDefaultCache
:
153 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
157 if (options
& kIOMemoryPageable
)
159 iomdOptions
|= kIOMemoryBufferPageable
;
161 // must create the entry before any pages are allocated
163 // set flags for entry + object create
164 memEntryCacheMode
|= MAP_MEM_NAMED_CREATE
;
166 if (options
& kIOMemoryPurgeable
)
167 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
171 memEntryCacheMode
|= MAP_MEM_NAMED_REUSE
;
174 // Buffer shouldn't auto prepare they should be prepared explicitly
175 // But it never was enforced so what are you going to do?
176 iomdOptions
|= kIOMemoryAutoPrepare
;
178 /* Allocate a wired-down buffer inside kernel space. */
180 if ((options
& kIOMemoryPhysicallyContiguous
) || highestMask
|| (alignment
> page_size
))
182 _internalFlags
|= kInternalFlagPhysical
;
185 _internalFlags
|= kInternalFlagPageSized
;
186 capacity
= round_page(capacity
);
188 _buffer
= (void *) IOKernelAllocateWithPhysicalRestrict(capacity
, highestMask
, alignment
,
189 (0 != (options
& kIOMemoryPhysicallyContiguous
)));
191 else if (alignment
> 1)
193 _buffer
= IOMallocAligned(capacity
, alignment
);
197 _buffer
= IOMalloc(capacity
);
206 if( (options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
207 ipc_port_t sharedMem
;
208 vm_size_t size
= round_page(capacity
);
210 kr
= mach_make_memory_entry(vmmap
,
211 &size
, (vm_offset_t
)_buffer
,
212 memEntryCacheMode
, &sharedMem
,
215 if( (KERN_SUCCESS
== kr
) && (size
!= round_page(capacity
))) {
216 ipc_port_release_send( sharedMem
);
217 kr
= kIOReturnVMError
;
219 if( KERN_SUCCESS
!= kr
)
222 _memEntry
= (void *) sharedMem
;
224 if( options
& kIOMemoryPageable
) {
226 debug_iomallocpageable_size
+= size
;
230 inTask
= kernel_task
;
232 else if (options
& kIOMapCacheMask
)
234 // Prefetch each page to put entries into the pmap
235 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
236 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
238 while (startAddr
< endAddr
)
241 startAddr
+= page_size
;
246 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;;
247 _ranges
.v64
->length
= _capacity
;
249 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
250 inTask
, iomdOptions
, /* System mapper */ 0))
256 reserved
= IONew( ExpansionData
, 1 );
260 reserved
->map
= createMappingInTask(mapTask
, 0,
261 kIOMapAnywhere
| (options
& kIOMapCacheMask
), 0, 0);
267 release(); // map took a retain on this
268 reserved
->map
->retain();
269 removeMapping(reserved
->map
);
270 mach_vm_address_t buffer
= reserved
->map
->getAddress();
271 _buffer
= (void *) buffer
;
272 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
))
273 _ranges
.v64
->address
= buffer
;
276 setLength(_capacity
);
281 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
283 IOOptionBits options
,
285 vm_offset_t alignment
)
287 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
289 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
296 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
298 IOOptionBits options
,
299 mach_vm_size_t capacity
,
300 mach_vm_address_t physicalMask
)
302 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
304 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
313 bool IOBufferMemoryDescriptor::initWithOptions(
314 IOOptionBits options
,
316 vm_offset_t alignment
)
318 return (initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0));
320 #endif /* !__LP64__ */
322 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
323 IOOptionBits options
,
325 vm_offset_t alignment
)
327 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
329 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
340 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
341 * hold capacity bytes. The descriptor's length is initially set to the capacity.
343 IOBufferMemoryDescriptor
*
344 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
345 IODirection inDirection
,
348 return( IOBufferMemoryDescriptor::withOptions(
349 inDirection
| kIOMemoryUnshared
350 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
351 inCapacity
, inContiguous
? inCapacity
: 1 ));
358 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
359 * The descriptor's length and capacity are set to the input buffer's size.
361 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
363 IODirection inDirection
,
366 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
367 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
368 inLength
, inLength
, (mach_vm_address_t
)0))
371 // start out with no data
374 if (!appendBytes(inBytes
, inLength
))
379 #endif /* !__LP64__ */
384 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
385 * The descriptor's length and capacity are set to the input buffer's size.
387 IOBufferMemoryDescriptor
*
388 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
390 IODirection inDirection
,
393 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
395 if (me
&& !me
->initWithPhysicalMask(
396 kernel_task
, inDirection
| kIOMemoryUnshared
397 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
398 inLength
, inLength
, 0 ))
406 // start out with no data
409 if (!me
->appendBytes(inBytes
, inLength
))
423 void IOBufferMemoryDescriptor::free()
425 // Cache all of the relevant information on the stack for use
426 // after we call super::free()!
427 IOOptionBits flags
= _flags
;
428 IOOptionBits internalFlags
= _internalFlags
;
429 IOOptionBits options
= _options
;
430 vm_size_t size
= _capacity
;
431 void * buffer
= _buffer
;
432 IOMemoryMap
* map
= 0;
433 IOAddressRange
* range
= _ranges
.v64
;
434 vm_offset_t alignment
= _alignment
;
436 if (alignment
>= page_size
)
437 size
= round_page(size
);
442 IODelete( reserved
, ExpansionData
, 1 );
447 /* super::free may unwire - deallocate buffer afterwards */
450 if (options
& kIOMemoryPageable
)
453 debug_iomallocpageable_size
-= round_page(size
);
458 if (internalFlags
& kInternalFlagPhysical
)
460 if (kInternalFlagPageSized
& internalFlags
)
461 size
= round_page(size
);
462 IOKernelFreePhysical((mach_vm_address_t
) buffer
, size
);
464 else if (alignment
> 1)
465 IOFreeAligned(buffer
, size
);
467 IOFree(buffer
, size
);
469 if (range
&& (kIOMemoryAsReference
& flags
))
470 IODelete(range
, IOAddressRange
, 1);
476 * Get the buffer capacity
478 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
486 * Change the buffer length of the memory descriptor. When a new buffer
487 * is created, the initial length of the buffer is set to be the same as
488 * the capacity. The length can be adjusted via setLength for a shorter
489 * transfer (there is no need to create more buffer descriptors when you
490 * can reuse an existing one, even for different transfer sizes). Note
491 * that the specified length must not exceed the capacity of the buffer.
493 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
495 assert(length
<= _capacity
);
498 _ranges
.v64
->length
= length
;
504 * Change the direction of the transfer. This method allows one to redirect
505 * the descriptor's transfer direction. This eliminates the need to destroy
506 * and create new buffers when different transfer directions are needed.
508 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
510 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
512 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
513 #endif /* !__LP64__ */
519 * Add some data to the end of the buffer. This method automatically
520 * maintains the memory descriptor buffer length. Note that appendBytes
521 * will not copy past the end of the memory descriptor's current capacity.
524 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
526 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
529 assert(_length
<= _capacity
);
532 _length
+= actualBytesToCopy
;
533 _ranges
.v64
->length
+= actualBytesToCopy
;
535 if (_task
== kernel_task
)
536 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
539 writeBytes(offset
, bytes
, actualBytesToCopy
);
547 * Return the virtual address of the beginning of the buffer
549 void * IOBufferMemoryDescriptor::getBytesNoCopy()
551 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
554 return (void *)_ranges
.v64
->address
;
561 * Return the virtual address of an offset from the beginning of the buffer
564 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
566 IOVirtualAddress address
;
567 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
568 address
= (IOVirtualAddress
) _buffer
;
570 address
= _ranges
.v64
->address
;
572 if (start
< _length
&& (start
+ withLength
) <= _length
)
573 return (void *)(address
+ start
);
578 void * IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
579 IOByteCount
* lengthOfSegment
)
581 void * bytes
= getBytesNoCopy(offset
, 0);
583 if (bytes
&& lengthOfSegment
)
584 *lengthOfSegment
= _length
- offset
;
588 #endif /* !__LP64__ */
591 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
592 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
593 #else /* !__LP64__ */
594 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
595 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
596 #endif /* !__LP64__ */
597 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
598 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
599 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
600 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
601 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
602 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
603 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
604 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
605 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
606 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
607 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
608 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
609 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
610 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);