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@
28 #include <IOKit/assert.h>
29 #include <IOKit/system.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/IOBufferMemoryDescriptor.h>
34 #include "IOKitKernelInternal.h"
37 void ipc_port_release_send(ipc_port_t port
);
40 vm_map_t
IOPageableMapForAddress( vm_address_t address
);
43 #define super IOGeneralMemoryDescriptor
44 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
45 IOGeneralMemoryDescriptor
);
47 bool IOBufferMemoryDescriptor::initWithAddress(
48 void * /* address */ ,
49 IOByteCount
/* withLength */ ,
50 IODirection
/* withDirection */ )
55 bool IOBufferMemoryDescriptor::initWithAddress(
56 vm_address_t
/* address */ ,
57 IOByteCount
/* withLength */ ,
58 IODirection
/* withDirection */ ,
59 task_t
/* withTask */ )
64 bool IOBufferMemoryDescriptor::initWithPhysicalAddress(
65 IOPhysicalAddress
/* address */ ,
66 IOByteCount
/* withLength */ ,
67 IODirection
/* withDirection */ )
72 bool IOBufferMemoryDescriptor::initWithPhysicalRanges(
73 IOPhysicalRange
* /* ranges */ ,
74 UInt32
/* withCount */ ,
75 IODirection
/* withDirection */ ,
76 bool /* asReference */ )
81 bool IOBufferMemoryDescriptor::initWithRanges(
82 IOVirtualRange
* /* ranges */ ,
83 UInt32
/* withCount */ ,
84 IODirection
/* withDirection */ ,
85 task_t
/* withTask */ ,
86 bool /* asReference */ )
91 bool IOBufferMemoryDescriptor::initWithOptions(
94 vm_offset_t alignment
,
99 IOOptionBits iomdOptions
= kIOMemoryAsReference
| kIOMemoryTypeVirtual
;
105 _capacity
= capacity
;
110 // Grab the direction and the Auto Prepare bits from the Buffer MD options
111 iomdOptions
|= options
& (kIOMemoryDirectionMask
| kIOMemoryAutoPrepare
);
113 if ((options
& kIOMemorySharingTypeMask
) && (alignment
< page_size
))
114 alignment
= page_size
;
116 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
))
119 _alignment
= alignment
;
120 if (options
& kIOMemoryPageable
)
122 iomdOptions
|= kIOMemoryBufferPageable
;
124 ipc_port_t sharedMem
;
125 vm_size_t size
= round_page_32(capacity
);
127 // must create the entry before any pages are allocated
129 // set flags for entry + object create
130 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
131 | MAP_MEM_NAMED_CREATE
;
133 if (options
& kIOMemoryPurgeable
)
134 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
136 // set memory entry cache mode
137 switch (options
& kIOMapCacheMask
)
139 case kIOMapInhibitCache
:
140 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
143 case kIOMapWriteThruCache
:
144 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
147 case kIOMapWriteCombineCache
:
148 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
151 case kIOMapCopybackCache
:
152 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
155 case kIOMapDefaultCache
:
157 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
161 kr
= mach_make_memory_entry( vmmap
,
163 memEntryCacheMode
, &sharedMem
,
166 if( (KERN_SUCCESS
== kr
) && (size
!= round_page_32(capacity
))) {
167 ipc_port_release_send( sharedMem
);
168 kr
= kIOReturnVMError
;
170 if( KERN_SUCCESS
!= kr
)
173 _memEntry
= (void *) sharedMem
;
175 debug_iomallocpageable_size
+= size
;
177 if ((NULL
== inTask
) && (options
& kIOMemoryPageable
))
178 inTask
= kernel_task
;
179 else if (inTask
== kernel_task
)
187 reserved
= IONew( ExpansionData
, 1 );
191 vmmap
= get_task_map(inTask
);
192 vm_map_reference(vmmap
);
193 reserved
->map
= vmmap
;
198 // @@@ gvdl: Need to remove this
199 // Buffer should never auto prepare they should be prepared explicitly
200 // But it never was enforced so what are you going to do?
201 iomdOptions
|= kIOMemoryAutoPrepare
;
203 /* Allocate a wired-down buffer inside kernel space. */
204 if (options
& kIOMemoryPhysicallyContiguous
)
205 _buffer
= IOMallocContiguous(capacity
, alignment
, 0);
206 else if (alignment
> 1)
207 _buffer
= IOMallocAligned(capacity
, alignment
);
209 _buffer
= IOMalloc(capacity
);
215 _singleRange
.v
.address
= (vm_address_t
) _buffer
;
216 _singleRange
.v
.length
= capacity
;
218 if (!super::initWithOptions(&_singleRange
.v
, 1, 0,
219 inTask
, iomdOptions
, /* System mapper */ 0))
222 if (options
& kIOMemoryPageable
)
228 kr
= doMap(vmmap
, (IOVirtualAddress
*) &_buffer
, kIOMapAnywhere
, 0, round_page_32(capacity
));
229 if (KERN_SUCCESS
!= kr
)
234 _singleRange
.v
.address
= (vm_address_t
) _buffer
;
243 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
245 IOOptionBits options
,
247 vm_offset_t alignment
)
249 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
251 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, inTask
)) {
258 bool IOBufferMemoryDescriptor::initWithOptions(
259 IOOptionBits options
,
261 vm_offset_t alignment
)
263 return( initWithOptions(options
, capacity
, alignment
, kernel_task
) );
266 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
267 IOOptionBits options
,
269 vm_offset_t alignment
)
271 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
273 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, kernel_task
)) {
284 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
285 * hold capacity bytes. The descriptor's length is initially set to the capacity.
287 IOBufferMemoryDescriptor
*
288 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
289 IODirection inDirection
,
292 return( IOBufferMemoryDescriptor::withOptions(
293 inDirection
| kIOMemoryUnshared
294 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
295 inCapacity
, inContiguous
? inCapacity
: 1 ));
301 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
302 * The descriptor's length and capacity are set to the input buffer's size.
304 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
306 IODirection inDirection
,
309 if (!initWithOptions(
310 inDirection
| kIOMemoryUnshared
311 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
312 inLength
, inLength
))
315 // start out with no data
318 if (!appendBytes(inBytes
, inLength
))
327 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
328 * The descriptor's length and capacity are set to the input buffer's size.
330 IOBufferMemoryDescriptor
*
331 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
333 IODirection inDirection
,
336 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
338 if (me
&& !me
->initWithBytes(inBytes
, inLength
, inDirection
, inContiguous
)){
350 void IOBufferMemoryDescriptor::free()
352 // Cache all of the relevant information on the stack for use
353 // after we call super::free()!
354 IOOptionBits options
= _options
;
355 vm_size_t size
= _capacity
;
356 void * buffer
= _buffer
;
358 vm_offset_t alignment
= _alignment
;
362 vmmap
= reserved
->map
;
363 IODelete( reserved
, ExpansionData
, 1 );
366 /* super::free may unwire - deallocate buffer afterwards */
369 if (options
& kIOMemoryPageable
)
372 if (!buffer
|| vmmap
)
373 debug_iomallocpageable_size
-= round_page_32(size
);
378 vm_deallocate(vmmap
, (vm_address_t
) buffer
, round_page_32(size
));
380 IOFreePageable(buffer
, size
);
385 if (options
& kIOMemoryPhysicallyContiguous
)
386 IOFreeContiguous(buffer
, size
);
387 else if (alignment
> 1)
388 IOFreeAligned(buffer
, size
);
390 IOFree(buffer
, size
);
393 vm_map_deallocate(vmmap
);
399 * Get the buffer capacity
401 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
409 * Change the buffer length of the memory descriptor. When a new buffer
410 * is created, the initial length of the buffer is set to be the same as
411 * the capacity. The length can be adjusted via setLength for a shorter
412 * transfer (there is no need to create more buffer descriptors when you
413 * can reuse an existing one, even for different transfer sizes). Note
414 * that the specified length must not exceed the capacity of the buffer.
416 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
418 assert(length
<= _capacity
);
421 _singleRange
.v
.length
= length
;
427 * Change the direction of the transfer. This method allows one to redirect
428 * the descriptor's transfer direction. This eliminates the need to destroy
429 * and create new buffers when different transfer directions are needed.
431 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
433 _direction
= direction
;
439 * Add some data to the end of the buffer. This method automatically
440 * maintains the memory descriptor buffer length. Note that appendBytes
441 * will not copy past the end of the memory descriptor's current capacity.
444 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
446 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
448 assert(_length
<= _capacity
);
449 bcopy(/* from */ bytes
, (void *)(_singleRange
.v
.address
+ _length
),
451 _length
+= actualBytesToCopy
;
452 _singleRange
.v
.length
+= actualBytesToCopy
;
460 * Return the virtual address of the beginning of the buffer
462 void * IOBufferMemoryDescriptor::getBytesNoCopy()
464 return (void *)_singleRange
.v
.address
;
470 * Return the virtual address of an offset from the beginning of the buffer
473 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
475 if (start
< _length
&& (start
+ withLength
) <= _length
)
476 return (void *)(_singleRange
.v
.address
+ start
);
480 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
481 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
482 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
483 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
484 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
485 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
486 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
487 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
488 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
489 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
490 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
491 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
492 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
493 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
494 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
495 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);