2 * Copyright (c) 1998-2004 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@
31 * 17-Apr-91 Portions from libIO.m, Doug Mitchell at NeXT.
36 #include <IOKit/system.h>
37 #include <mach/sync_policy.h>
38 #include <machine/machine_routines.h>
39 #include <libkern/c++/OSCPPDebug.h>
41 #include <IOKit/assert.h>
43 #include <IOKit/IOReturn.h>
44 #include <IOKit/IOLib.h>
45 #include <IOKit/IOLocks.h>
46 #include <IOKit/IOMapper.h>
47 #include <IOKit/IOBufferMemoryDescriptor.h>
48 #include <IOKit/IOKitDebug.h>
50 #include "IOKitKernelInternal.h"
56 mach_timespec_t IOZeroTvalspec
= { 0, 0 };
58 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
60 extern kern_return_t
kmem_suballoc(
68 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
70 lck_grp_t
*IOLockGroup
;
73 * Global variables for use by iLogger
74 * These symbols are for use only by Apple diagnostic code.
75 * Binary compatibility is not guaranteed for kexts that reference these symbols.
78 void *_giDebugLogInternal
= NULL
;
79 void *_giDebugLogDataInternal
= NULL
;
80 void *_giDebugReserved1
= NULL
;
81 void *_giDebugReserved2
= NULL
;
85 * Static variables for this module.
88 static queue_head_t gIOMallocContiguousEntries
;
89 static lck_mtx_t
* gIOMallocContiguousEntriesLock
;
91 enum { kIOMaxPageableMaps
= 16 };
92 enum { kIOPageableMapSize
= 96 * 1024 * 1024 };
93 enum { kIOPageableMaxMapSize
= 96 * 1024 * 1024 };
95 /* LP64todo - these need to expand */
105 IOMapData maps
[ kIOMaxPageableMaps
];
107 } gIOKitPageableSpace
;
109 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
115 static bool libInitialized
;
120 gIOKitPageableSpace
.maps
[0].address
= 0;
121 ret
= kmem_suballoc(kernel_map
,
122 &gIOKitPageableSpace
.maps
[0].address
,
126 &gIOKitPageableSpace
.maps
[0].map
);
127 if (ret
!= KERN_SUCCESS
)
128 panic("failed to allocate iokit pageable map\n");
130 IOLockGroup
= lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL
);
132 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
133 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
134 gIOKitPageableSpace
.hint
= 0;
135 gIOKitPageableSpace
.count
= 1;
137 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
138 queue_init( &gIOMallocContiguousEntries
);
140 libInitialized
= true;
143 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
145 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
147 kern_return_t result
;
150 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
151 if (result
!= KERN_SUCCESS
)
154 thread_deallocate(thread
);
160 void IOExitThread(void)
162 (void) thread_terminate(current_thread());
165 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
168 void * IOMalloc(vm_size_t size
)
172 address
= (void *)kalloc(size
);
175 debug_iomalloc_size
+= size
;
180 void IOFree(void * address
, vm_size_t size
)
183 kfree(address
, size
);
185 debug_iomalloc_size
-= size
;
190 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
192 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
195 vm_address_t address
;
196 vm_address_t allocationAddress
;
197 vm_size_t adjustedSize
;
198 vm_offset_t alignMask
;
205 alignMask
= alignment
- 1;
206 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
208 if (adjustedSize
>= page_size
) {
210 kr
= kernel_memory_allocate(kernel_map
, &address
,
212 if (KERN_SUCCESS
!= kr
)
217 adjustedSize
+= alignMask
;
219 if (adjustedSize
>= page_size
) {
221 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
223 if (KERN_SUCCESS
!= kr
)
224 allocationAddress
= 0;
227 allocationAddress
= (vm_address_t
) kalloc(adjustedSize
);
229 if (allocationAddress
) {
230 address
= (allocationAddress
+ alignMask
231 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
234 *((vm_size_t
*)(address
- sizeof(vm_size_t
)
235 - sizeof(vm_address_t
))) = adjustedSize
;
236 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
242 assert(0 == (address
& alignMask
));
246 debug_iomalloc_size
+= size
;
249 return (void *) address
;
252 void IOFreeAligned(void * address
, vm_size_t size
)
254 vm_address_t allocationAddress
;
255 vm_size_t adjustedSize
;
262 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
263 if (adjustedSize
>= page_size
) {
265 kmem_free( kernel_map
, (vm_address_t
) address
, size
);
268 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
269 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
270 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
271 - sizeof(vm_address_t
) ));
273 if (adjustedSize
>= page_size
)
274 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
276 kfree((void *)allocationAddress
, adjustedSize
);
280 debug_iomalloc_size
-= size
;
284 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
287 IOKernelFreeContiguous(mach_vm_address_t address
, mach_vm_size_t size
)
289 mach_vm_address_t allocationAddress
;
290 mach_vm_size_t adjustedSize
;
297 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
298 if (adjustedSize
>= page_size
) {
300 kmem_free( kernel_map
, (vm_address_t
) address
, size
);
304 adjustedSize
= *((mach_vm_size_t
*)
305 (address
- sizeof(mach_vm_address_t
) - sizeof(mach_vm_size_t
)));
306 allocationAddress
= *((mach_vm_address_t
*)
307 (address
- sizeof(mach_vm_address_t
) ));
308 kfree((void *)allocationAddress
, adjustedSize
);
312 debug_iomalloc_size
-= size
;
317 IOKernelAllocateContiguous(mach_vm_size_t size
, mach_vm_size_t alignment
)
320 mach_vm_address_t address
;
321 mach_vm_address_t allocationAddress
;
322 mach_vm_size_t adjustedSize
;
323 mach_vm_address_t alignMask
;
330 alignMask
= alignment
- 1;
331 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
333 if (adjustedSize
>= page_size
)
337 if (adjustedSize
> page_size
)
339 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
344 kr
= kernel_memory_allocate(kernel_map
, &virt
,
347 if (KERN_SUCCESS
== kr
)
354 adjustedSize
+= alignMask
;
355 allocationAddress
= (mach_vm_address_t
) kalloc(adjustedSize
);
357 if (allocationAddress
) {
359 address
= (allocationAddress
+ alignMask
360 + (sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
)))
363 if (atop_32(address
) != atop_32(address
+ size
- 1))
364 address
= round_page_32(address
);
366 *((mach_vm_size_t
*)(address
- sizeof(mach_vm_size_t
)
367 - sizeof(mach_vm_address_t
))) = adjustedSize
;
368 *((mach_vm_address_t
*)(address
- sizeof(mach_vm_address_t
)))
376 debug_iomalloc_size
+= size
;
382 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
384 struct _IOMallocContiguousEntry
386 mach_vm_address_t virtualAddr
;
387 IOBufferMemoryDescriptor
* md
;
390 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
392 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
393 IOPhysicalAddress
* physicalAddress
)
395 mach_vm_address_t address
= 0;
402 /* Do we want a physical address? */
403 if (!physicalAddress
)
405 address
= IOKernelAllocateContiguous(size
, alignment
);
409 IOBufferMemoryDescriptor
* bmd
;
410 mach_vm_address_t physicalMask
;
411 vm_offset_t alignMask
;
413 alignMask
= alignment
- 1;
414 physicalMask
= 0xFFFFFFFF ^ (alignMask
& PAGE_MASK
);
415 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
416 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
420 _IOMallocContiguousEntry
*
421 entry
= IONew(_IOMallocContiguousEntry
, 1);
427 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
429 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
430 queue_enter( &gIOMallocContiguousEntries
, entry
,
431 _IOMallocContiguousEntry
*, link
);
432 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
434 address
= (mach_vm_address_t
) entry
->virtualAddr
;
435 *physicalAddress
= bmd
->getPhysicalAddress();
439 return (void *) address
;
442 void IOFreeContiguous(void * _address
, vm_size_t size
)
444 _IOMallocContiguousEntry
* entry
;
445 IOMemoryDescriptor
* md
= NULL
;
447 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
454 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
455 queue_iterate( &gIOMallocContiguousEntries
, entry
,
456 _IOMallocContiguousEntry
*, link
)
458 if( entry
->virtualAddr
== address
) {
460 queue_remove( &gIOMallocContiguousEntries
, entry
,
461 _IOMallocContiguousEntry
*, link
);
465 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
470 IODelete(entry
, _IOMallocContiguousEntry
, 1);
474 IOKernelFreeContiguous((mach_vm_address_t
) address
, size
);
478 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
480 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
481 IOIteratePageableMapsCallback callback
, void * ref
)
483 kern_return_t kr
= kIOReturnNotReady
;
490 if (size
> kIOPageableMaxMapSize
)
491 return( kIOReturnBadArgument
);
494 index
= gIOKitPageableSpace
.hint
;
495 attempts
= gIOKitPageableSpace
.count
;
497 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
498 if( KERN_SUCCESS
== kr
) {
499 gIOKitPageableSpace
.hint
= index
;
505 index
= gIOKitPageableSpace
.count
- 1;
507 if( KERN_SUCCESS
== kr
)
510 lck_mtx_lock( gIOKitPageableSpace
.lock
);
512 index
= gIOKitPageableSpace
.count
;
513 if( index
>= (kIOMaxPageableMaps
- 1)) {
514 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
518 if( size
< kIOPageableMapSize
)
519 segSize
= kIOPageableMapSize
;
524 kr
= kmem_suballoc(kernel_map
,
530 if( KERN_SUCCESS
!= kr
) {
531 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
535 gIOKitPageableSpace
.maps
[index
].map
= map
;
536 gIOKitPageableSpace
.maps
[index
].address
= min
;
537 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
538 gIOKitPageableSpace
.hint
= index
;
539 gIOKitPageableSpace
.count
= index
+ 1;
541 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
548 struct IOMallocPageableRef
550 vm_address_t address
;
554 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
556 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
559 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
564 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
566 kern_return_t kr
= kIOReturnNotReady
;
567 struct IOMallocPageableRef ref
;
569 if (alignment
> page_size
)
571 if (size
> kIOPageableMaxMapSize
)
575 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
576 if( kIOReturnSuccess
!= kr
)
581 debug_iomallocpageable_size
+= round_page_32(size
);
584 return( (void *) ref
.address
);
587 vm_map_t
IOPageableMapForAddress( vm_address_t address
)
592 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
593 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
594 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
595 map
= gIOKitPageableSpace
.maps
[index
].map
;
600 IOPanic("IOPageableMapForAddress: null");
605 void IOFreePageable(void * address
, vm_size_t size
)
609 map
= IOPageableMapForAddress( (vm_address_t
) address
);
611 kmem_free( map
, (vm_offset_t
) address
, size
);
614 debug_iomallocpageable_size
-= round_page_32(size
);
618 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
620 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
621 IOByteCount length
, IOOptionBits cacheMode
)
623 IOReturn ret
= kIOReturnSuccess
;
626 if( task
!= kernel_task
)
627 return( kIOReturnUnsupported
);
629 length
= round_page_32(address
+ length
) - trunc_page_32( address
);
630 address
= trunc_page_32( address
);
633 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
635 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
637 // Get the physical page number
638 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
640 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
641 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
643 ret
= kIOReturnVMError
;
645 address
+= page_size
;
653 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
656 if( task
!= kernel_task
)
657 return( kIOReturnUnsupported
);
660 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
663 return( kIOReturnSuccess
);
666 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
668 SInt32
OSKernelStackRemaining( void )
672 stack
= (((SInt32
) &stack
) & (KERNEL_STACK_SIZE
- 1));
677 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
679 void IOSleep(unsigned milliseconds
)
681 delay_for_interval(milliseconds
, kMillisecondScale
);
685 * Spin for indicated number of microseconds.
687 void IODelay(unsigned microseconds
)
689 delay_for_interval(microseconds
, kMicrosecondScale
);
692 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
694 void IOLog(const char *format
, ...)
697 extern void conslog_putc(char);
698 extern void logwakeup(void);
700 va_start(ap
, format
);
701 _doprnt(format
, &ap
, conslog_putc
, 16);
705 void IOPanic(const char *reason
)
710 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713 * Convert a integer constant (typically a #define or enum) to a string.
715 static char noValue
[80]; // that's pretty
717 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
719 for( ; regValueArray
->name
; regValueArray
++) {
720 if(regValueArray
->value
== value
)
721 return(regValueArray
->name
);
723 sprintf(noValue
, "0x%x (UNDEFINED)", value
);
724 return((const char *)noValue
);
727 IOReturn
IOFindValueForName(const char *string
,
728 const IONamedValue
*regValueArray
,
731 for( ; regValueArray
->name
; regValueArray
++) {
732 if(!strcmp(regValueArray
->name
, string
)) {
733 *value
= regValueArray
->value
;
734 return kIOReturnSuccess
;
737 return kIOReturnBadArgument
;
740 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
742 IOAlignment
IOSizeToAlignment(unsigned int size
)
745 const int intsize
= sizeof(unsigned int) * 8;
747 for (shift
= 1; shift
< intsize
; shift
++) {
748 if (size
& 0x80000000)
749 return (IOAlignment
)(intsize
- shift
);
755 unsigned int IOAlignmentToSize(IOAlignment align
)
759 for (size
= 1; align
; align
--) {