2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <IOKit/assert.h>
23 #include <IOKit/system.h>
25 #include <IOKit/IOLib.h>
26 #include <IOKit/IOBufferMemoryDescriptor.h>
28 #include "IOKitKernelInternal.h"
31 void ipc_port_release_send(ipc_port_t port
);
34 vm_map_t
IOPageableMapForAddress( vm_address_t address
);
37 #define super IOGeneralMemoryDescriptor
38 OSDefineMetaClassAndStructors(IOBufferMemoryDescriptor
,
39 IOGeneralMemoryDescriptor
);
41 bool IOBufferMemoryDescriptor::initWithAddress(
42 void * /* address */ ,
43 IOByteCount
/* withLength */ ,
44 IODirection
/* withDirection */ )
49 bool IOBufferMemoryDescriptor::initWithAddress(
50 vm_address_t
/* address */ ,
51 IOByteCount
/* withLength */ ,
52 IODirection
/* withDirection */ ,
53 task_t
/* withTask */ )
58 bool IOBufferMemoryDescriptor::initWithPhysicalAddress(
59 IOPhysicalAddress
/* address */ ,
60 IOByteCount
/* withLength */ ,
61 IODirection
/* withDirection */ )
66 bool IOBufferMemoryDescriptor::initWithPhysicalRanges(
67 IOPhysicalRange
* /* ranges */ ,
68 UInt32
/* withCount */ ,
69 IODirection
/* withDirection */ ,
70 bool /* asReference */ )
75 bool IOBufferMemoryDescriptor::initWithRanges(
76 IOVirtualRange
* /* ranges */ ,
77 UInt32
/* withCount */ ,
78 IODirection
/* withDirection */ ,
79 task_t
/* withTask */ ,
80 bool /* asReference */ )
85 bool IOBufferMemoryDescriptor::initWithOptions(
88 vm_offset_t alignment
,
93 IOOptionBits iomdOptions
= kIOMemoryAsReference
| kIOMemoryTypeVirtual
;
104 // Grab the direction and the Auto Prepare bits from the Buffer MD options
105 iomdOptions
|= options
& (kIOMemoryDirectionMask
| kIOMemoryAutoPrepare
);
107 if ((options
& kIOMemorySharingTypeMask
) && (alignment
< page_size
))
108 alignment
= page_size
;
110 if ((inTask
!= kernel_task
) && !(options
& kIOMemoryPageable
))
113 _alignment
= alignment
;
114 if (options
& kIOMemoryPageable
)
116 iomdOptions
|= kIOMemoryBufferPageable
;
118 ipc_port_t sharedMem
;
119 vm_size_t size
= round_page_32(capacity
);
121 // must create the entry before any pages are allocated
123 // set flags for entry + object create
124 vm_prot_t memEntryCacheMode
= VM_PROT_READ
| VM_PROT_WRITE
125 | MAP_MEM_NAMED_CREATE
;
127 if (options
& kIOMemoryPurgeable
)
128 memEntryCacheMode
|= MAP_MEM_PURGABLE
;
130 // set memory entry cache mode
131 switch (options
& kIOMapCacheMask
)
133 case kIOMapInhibitCache
:
134 SET_MAP_MEM(MAP_MEM_IO
, memEntryCacheMode
);
137 case kIOMapWriteThruCache
:
138 SET_MAP_MEM(MAP_MEM_WTHRU
, memEntryCacheMode
);
141 case kIOMapWriteCombineCache
:
142 SET_MAP_MEM(MAP_MEM_WCOMB
, memEntryCacheMode
);
145 case kIOMapCopybackCache
:
146 SET_MAP_MEM(MAP_MEM_COPYBACK
, memEntryCacheMode
);
149 case kIOMapDefaultCache
:
151 SET_MAP_MEM(MAP_MEM_NOOP
, memEntryCacheMode
);
155 kr
= mach_make_memory_entry( vmmap
,
157 memEntryCacheMode
, &sharedMem
,
160 if( (KERN_SUCCESS
== kr
) && (size
!= round_page_32(capacity
))) {
161 ipc_port_release_send( sharedMem
);
162 kr
= kIOReturnVMError
;
164 if( KERN_SUCCESS
!= kr
)
167 _memEntry
= (void *) sharedMem
;
169 debug_iomallocpageable_size
+= size
;
171 if ((NULL
== inTask
) && (options
& kIOMemoryPageable
))
172 inTask
= kernel_task
;
173 else if (inTask
== kernel_task
)
181 reserved
= IONew( ExpansionData
, 1 );
185 vmmap
= get_task_map(inTask
);
186 vm_map_reference(vmmap
);
187 reserved
->map
= vmmap
;
192 // @@@ gvdl: Need to remove this
193 // Buffer should never auto prepare they should be prepared explicitly
194 // But it never was enforced so what are you going to do?
195 iomdOptions
|= kIOMemoryAutoPrepare
;
197 /* Allocate a wired-down buffer inside kernel space. */
198 if (options
& kIOMemoryPhysicallyContiguous
)
199 _buffer
= IOMallocContiguous(capacity
, alignment
, 0);
200 else if (alignment
> 1)
201 _buffer
= IOMallocAligned(capacity
, alignment
);
203 _buffer
= IOMalloc(capacity
);
209 _singleRange
.v
.address
= (vm_address_t
) _buffer
;
210 _singleRange
.v
.length
= capacity
;
212 if (!super::initWithOptions(&_singleRange
.v
, 1, 0,
213 inTask
, iomdOptions
, /* System mapper */ 0))
216 if (options
& kIOMemoryPageable
)
222 kr
= doMap(vmmap
, (IOVirtualAddress
*) &_buffer
, kIOMapAnywhere
, 0, round_page_32(capacity
));
223 if (KERN_SUCCESS
!= kr
)
228 _singleRange
.v
.address
= (vm_address_t
) _buffer
;
237 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::inTaskWithOptions(
239 IOOptionBits options
,
241 vm_offset_t alignment
)
243 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
245 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, inTask
)) {
252 bool IOBufferMemoryDescriptor::initWithOptions(
253 IOOptionBits options
,
255 vm_offset_t alignment
)
257 return( initWithOptions(options
, capacity
, alignment
, kernel_task
) );
260 IOBufferMemoryDescriptor
* IOBufferMemoryDescriptor::withOptions(
261 IOOptionBits options
,
263 vm_offset_t alignment
)
265 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
267 if (me
&& !me
->initWithOptions(options
, capacity
, alignment
, kernel_task
)) {
278 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
279 * hold capacity bytes. The descriptor's length is initially set to the capacity.
281 IOBufferMemoryDescriptor
*
282 IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity
,
283 IODirection inDirection
,
286 return( IOBufferMemoryDescriptor::withOptions(
287 inDirection
| kIOMemoryUnshared
288 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
289 inCapacity
, inContiguous
? inCapacity
: 1 ));
295 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
296 * The descriptor's length and capacity are set to the input buffer's size.
298 bool IOBufferMemoryDescriptor::initWithBytes(const void * inBytes
,
300 IODirection inDirection
,
303 if (!initWithOptions(
304 inDirection
| kIOMemoryUnshared
305 | (inContiguous
? kIOMemoryPhysicallyContiguous
: 0),
306 inLength
, inLength
))
309 // start out with no data
312 if (!appendBytes(inBytes
, inLength
))
321 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
322 * The descriptor's length and capacity are set to the input buffer's size.
324 IOBufferMemoryDescriptor
*
325 IOBufferMemoryDescriptor::withBytes(const void * inBytes
,
327 IODirection inDirection
,
330 IOBufferMemoryDescriptor
*me
= new IOBufferMemoryDescriptor
;
332 if (me
&& !me
->initWithBytes(inBytes
, inLength
, inDirection
, inContiguous
)){
344 void IOBufferMemoryDescriptor::free()
346 // Cache all of the relevant information on the stack for use
347 // after we call super::free()!
348 IOOptionBits options
= _options
;
349 vm_size_t size
= _capacity
;
350 void * buffer
= _buffer
;
352 vm_offset_t alignment
= _alignment
;
356 vmmap
= reserved
->map
;
357 IODelete( reserved
, ExpansionData
, 1 );
360 /* super::free may unwire - deallocate buffer afterwards */
363 if (options
& kIOMemoryPageable
)
366 if (!buffer
|| vmmap
)
367 debug_iomallocpageable_size
-= round_page_32(size
);
372 vm_deallocate(vmmap
, (vm_address_t
) buffer
, round_page_32(size
));
374 IOFreePageable(buffer
, size
);
379 if (options
& kIOMemoryPhysicallyContiguous
)
380 IOFreeContiguous(buffer
, size
);
381 else if (alignment
> 1)
382 IOFreeAligned(buffer
, size
);
384 IOFree(buffer
, size
);
387 vm_map_deallocate(vmmap
);
393 * Get the buffer capacity
395 vm_size_t
IOBufferMemoryDescriptor::getCapacity() const
403 * Change the buffer length of the memory descriptor. When a new buffer
404 * is created, the initial length of the buffer is set to be the same as
405 * the capacity. The length can be adjusted via setLength for a shorter
406 * transfer (there is no need to create more buffer descriptors when you
407 * can reuse an existing one, even for different transfer sizes). Note
408 * that the specified length must not exceed the capacity of the buffer.
410 void IOBufferMemoryDescriptor::setLength(vm_size_t length
)
412 assert(length
<= _capacity
);
415 _singleRange
.v
.length
= length
;
421 * Change the direction of the transfer. This method allows one to redirect
422 * the descriptor's transfer direction. This eliminates the need to destroy
423 * and create new buffers when different transfer directions are needed.
425 void IOBufferMemoryDescriptor::setDirection(IODirection direction
)
427 _direction
= direction
;
433 * Add some data to the end of the buffer. This method automatically
434 * maintains the memory descriptor buffer length. Note that appendBytes
435 * will not copy past the end of the memory descriptor's current capacity.
438 IOBufferMemoryDescriptor::appendBytes(const void * bytes
, vm_size_t withLength
)
440 vm_size_t actualBytesToCopy
= min(withLength
, _capacity
- _length
);
442 assert(_length
<= _capacity
);
443 bcopy(/* from */ bytes
, (void *)(_singleRange
.v
.address
+ _length
),
445 _length
+= actualBytesToCopy
;
446 _singleRange
.v
.length
+= actualBytesToCopy
;
454 * Return the virtual address of the beginning of the buffer
456 void * IOBufferMemoryDescriptor::getBytesNoCopy()
458 return (void *)_singleRange
.v
.address
;
464 * Return the virtual address of an offset from the beginning of the buffer
467 IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start
, vm_size_t withLength
)
469 if (start
< _length
&& (start
+ withLength
) <= _length
)
470 return (void *)(_singleRange
.v
.address
+ start
);
474 OSMetaClassDefineReservedUsed(IOBufferMemoryDescriptor
, 0);
475 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 1);
476 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 2);
477 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 3);
478 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 4);
479 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 5);
480 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 6);
481 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 7);
482 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 8);
483 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 9);
484 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 10);
485 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 11);
486 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 12);
487 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 13);
488 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 14);
489 OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor
, 15);