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
);
108 if ((options
& kIOMemoryPhysicallyContiguous
) || ((capacity
== 0x1000) && (inTask
== kernel_task
))
111 highestMask
= physicalMask
= 0xFFFFF000;
116 if (physicalMask
&& (alignment
<= 1))
118 alignment
= ((physicalMask
^ (-1ULL)) & (physicalMask
- 1));
119 highestMask
= (physicalMask
| alignment
);
121 if (alignment
< page_size
)
122 alignment
= page_size
;
125 if ((options
& (kIOMemorySharingTypeMask
| kIOMapCacheMask
| kIOMemoryClearEncrypt
)) && (alignment
< page_size
))
126 alignment
= page_size
;
128 if (alignment
>= page_size
)
129 capacity
= round_page(capacity
);
131 if (alignment
> page_size
)
132 options
|= kIOMemoryPhysicallyContiguous
;
134 _alignment
= alignment
;
136 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
))
139 // set flags for entry + object create
140 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
;
142 // set memory entry cache mode
143 switch (options
& kIOMapCacheMask
)
145 case kIOMapInhibitCache
:
146 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
149 case kIOMapWriteThruCache
:
150 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
153 case kIOMapWriteCombineCache
:
154 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
157 case kIOMapCopybackCache
:
158 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
161 case kIOMapDefaultCache
:
163 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
167 if (options
& kIOMemoryPageable
)
169 iomdOptions
|= kIOMemoryBufferPageable
;
171 // must create the entry before any pages are allocated
173 // set flags for entry + object create
174 memEntryCacheMode
|= MAP_MEM_NAMED_CREATE
;
176 if (options
& kIOMemoryPurgeable
)
177 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
181 memEntryCacheMode
|= MAP_MEM_NAMED_REUSE
;
184 // Buffer shouldn't auto prepare they should be prepared explicitly
185 // But it never was enforced so what are you going to do?
186 iomdOptions
|= kIOMemoryAutoPrepare
;
188 /* Allocate a wired-down buffer inside kernel space. */
190 if ((options
& kIOMemoryPhysicallyContiguous
) || highestMask
|| (alignment
> page_size
))
192 _internalFlags
|= kInternalFlagPhysical
;
195 _internalFlags
|= kInternalFlagPageSized
;
196 capacity
= round_page(capacity
);
198 _buffer
= (void *) IOKernelAllocateWithPhysicalRestrict(capacity
, highestMask
, alignment
,
199 (0 != (options
& kIOMemoryPhysicallyContiguous
)));
201 else if (alignment
> 1)
203 _buffer
= IOMallocAligned(capacity
, alignment
);
207 _buffer
= IOMalloc(capacity
);
216 if( (options
& (kIOMemoryPageable
| kIOMapCacheMask
))) {
217 ipc_port_t sharedMem
;
218 vm_size_t size
= round_page(capacity
);
220 kr
= mach_make_memory_entry(vmmap
,
221 &size
, (vm_offset_t
)_buffer
,
222 memEntryCacheMode
, &sharedMem
,
225 if( (KERN_SUCCESS
== kr
) && (size
!= round_page(capacity
))) {
226 ipc_port_release_send( sharedMem
);
227 kr
= kIOReturnVMError
;
229 if( KERN_SUCCESS
!= kr
)
232 _memEntry
= (void *) sharedMem
;
234 if( options
& kIOMemoryPageable
) {
236 debug_iomallocpageable_size
+= size
;
240 inTask
= kernel_task
;
242 else if (options
& kIOMapCacheMask
)
244 // Prefetch each page to put entries into the pmap
245 volatile UInt8
* startAddr
= (UInt8
*)_buffer
;
246 volatile UInt8
* endAddr
= (UInt8
*)_buffer
+ capacity
;
248 while (startAddr
< endAddr
)
251 startAddr
+= page_size
;
256 _ranges
.v64
->address
= (mach_vm_address_t
) _buffer
;;
257 _ranges
.v64
->length
= _capacity
;
259 if (!super::initWithOptions(_ranges
.v64
, 1, 0,
260 inTask
, iomdOptions
, /* System mapper */ 0))
266 reserved
= IONew( ExpansionData
, 1 );
270 reserved
->map
= createMappingInTask(mapTask
, 0,
271 kIOMapAnywhere
| (options
& kIOMapCacheMask
), 0, 0);
277 release(); // map took a retain on this
278 reserved
->map
->retain();
279 removeMapping(reserved
->map
);
280 mach_vm_address_t buffer
= reserved
->map
->getAddress();
281 _buffer
= (void *) buffer
;
282 if (kIOMemoryTypeVirtual64
== (kIOMemoryTypeMask
& iomdOptions
))
283 _ranges
.v64
->address
= buffer
;
286 setLength(_capacity
);
291 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
293 IOOptionBits options
,
295 vm_offset_t alignment
)
297 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
299 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, alignment
, 0)) {
306 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
308 IOOptionBits options
,
309 mach_vm_size_t capacity
,
310 mach_vm_address_t physicalMask
)
312 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
314 if (me
&& !me
->initWithPhysicalMask(inTask
, options
, capacity
, 1, physicalMask
))
323 bool IOBufferMemoryDescriptor::initWithOptions(
324 IOOptionBits options
,
326 vm_offset_t alignment
)
328 return (initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, (mach_vm_address_t
)0));
330 #endif /* !__LP64__ */
332 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
333 IOOptionBits options
,
335 vm_offset_t alignment
)
337 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
339 if (me
&& !me
->initWithPhysicalMask(kernel_task
, options
, capacity
, alignment
, 0)) {
350 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
351 * hold capacity bytes. The descriptor's length is initially set to the capacity.
353 IOBufferMemoryDescriptor
*
354 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
355 IODirection inDirection
,
358 return( IOBufferMemoryDescriptor::withOptions(
359 inDirection
| kIOMemoryUnshared
360 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
361 inCapacity
, inContiguous
? inCapacity
: 1 ));
368 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
369 * The descriptor's length and capacity are set to the input buffer's size.
371 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
373 IODirection inDirection
,
376 if (!initWithPhysicalMask(kernel_task
, inDirection
| kIOMemoryUnshared
377 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
378 inLength
, inLength
, (mach_vm_address_t
)0))
381 // start out with no data
384 if (!appendBytes(inBytes
, inLength
))
389 #endif /* !__LP64__ */
394 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
395 * The descriptor's length and capacity are set to the input buffer's size.
397 IOBufferMemoryDescriptor
*
398 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
400 IODirection inDirection
,
403 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
405 if (me
&& !me
->initWithPhysicalMask(
406 kernel_task
, inDirection
| kIOMemoryUnshared
407 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
408 inLength
, inLength
, 0 ))
416 // start out with no data
419 if (!me
->appendBytes(inBytes
, inLength
))
433 void IOBufferMemoryDescriptor::free()
435 // Cache all of the relevant information on the stack for use
436 // after we call super::free()!
437 IOOptionBits flags
= _flags
;
438 IOOptionBits internalFlags
= _internalFlags
;
439 IOOptionBits options
= _options
;
440 vm_size_t size
= _capacity
;
441 void * buffer
= _buffer
;
442 IOMemoryMap
* map
= 0;
443 IOAddressRange
* range
= _ranges
.v64
;
444 vm_offset_t alignment
= _alignment
;
446 if (alignment
>= page_size
)
447 size
= round_page(size
);
452 IODelete( reserved
, ExpansionData
, 1 );
457 /* super::free may unwire - deallocate buffer afterwards */
460 if (options
& kIOMemoryPageable
)
463 debug_iomallocpageable_size
-= round_page(size
);
468 if (internalFlags
& kInternalFlagPhysical
)
470 if (kInternalFlagPageSized
& internalFlags
)
471 size
= round_page(size
);
472 IOKernelFreePhysical((mach_vm_address_t
) buffer
, size
);
474 else if (alignment
> 1)
475 IOFreeAligned(buffer
, size
);
477 IOFree(buffer
, size
);
479 if (range
&& (kIOMemoryAsReference
& flags
))
480 IODelete(range
, IOAddressRange
, 1);
486 * Get the buffer capacity
488 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
496 * Change the buffer length of the memory descriptor. When a new buffer
497 * is created, the initial length of the buffer is set to be the same as
498 * the capacity. The length can be adjusted via setLength for a shorter
499 * transfer (there is no need to create more buffer descriptors when you
500 * can reuse an existing one, even for different transfer sizes). Note
501 * that the specified length must not exceed the capacity of the buffer.
503 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
505 assert(length
<= _capacity
);
508 _ranges
.v64
->length
= length
;
514 * Change the direction of the transfer. This method allows one to redirect
515 * the descriptor's transfer direction. This eliminates the need to destroy
516 * and create new buffers when different transfer directions are needed.
518 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
520 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
522 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
523 #endif /* !__LP64__ */
529 * Add some data to the end of the buffer. This method automatically
530 * maintains the memory descriptor buffer length. Note that appendBytes
531 * will not copy past the end of the memory descriptor's current capacity.
534 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
536 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
539 assert(_length
<= _capacity
);
542 _length
+= actualBytesToCopy
;
543 _ranges
.v64
->length
+= actualBytesToCopy
;
545 if (_task
== kernel_task
)
546 bcopy(/* from */ bytes
, (void *)(_ranges
.v64
->address
+ offset
),
549 writeBytes(offset
, bytes
, actualBytesToCopy
);
557 * Return the virtual address of the beginning of the buffer
559 void * IOBufferMemoryDescriptor::getBytesNoCopy()
561 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
564 return (void *)_ranges
.v64
->address
;
571 * Return the virtual address of an offset from the beginning of the buffer
574 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
576 IOVirtualAddress address
;
577 if (kIOMemoryTypePhysical64
== (_flags
& kIOMemoryTypeMask
))
578 address
= (IOVirtualAddress
) _buffer
;
580 address
= _ranges
.v64
->address
;
582 if (start
< _length
&& (start
+ withLength
) <= _length
)
583 return (void *)(address
+ start
);
588 void * IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset
,
589 IOByteCount
* lengthOfSegment
)
591 void * bytes
= getBytesNoCopy(offset
, 0);
593 if (bytes
&& lengthOfSegment
)
594 *lengthOfSegment
= _length
- offset
;
598 #endif /* !__LP64__ */
601 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 0);
602 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
603 #else /* !__LP64__ */
604 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
605 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 1);
606 #endif /* !__LP64__ */
607 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
608 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
609 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
610 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
611 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
612 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
613 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
614 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
615 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
616 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
617 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
618 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
619 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
620 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);