2 * Copyright (c) 1998-2006 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 <vm/vm_kern.h>
40 #include <libkern/c++/OSCPPDebug.h>
42 #include <IOKit/assert.h>
44 #include <IOKit/IOReturn.h>
45 #include <IOKit/IOLib.h>
46 #include <IOKit/IOLocks.h>
47 #include <IOKit/IOMapper.h>
48 #include <IOKit/IOBufferMemoryDescriptor.h>
49 #include <IOKit/IOKitDebug.h>
51 #include "IOKitKernelInternal.h"
54 #include <libkern/OSDebug.h>
55 #include <sys/sysctl.h>
62 mach_timespec_t IOZeroTvalspec
= { 0, 0 };
64 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
70 void (*putc
)(int, void *),
74 extern void conslog_putc(char);
77 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
79 lck_grp_t
*IOLockGroup
;
82 * Global variables for use by iLogger
83 * These symbols are for use only by Apple diagnostic code.
84 * Binary compatibility is not guaranteed for kexts that reference these symbols.
87 void *_giDebugLogInternal
= NULL
;
88 void *_giDebugLogDataInternal
= NULL
;
89 void *_giDebugReserved1
= NULL
;
90 void *_giDebugReserved2
= NULL
;
94 * Static variables for this module.
97 static queue_head_t gIOMallocContiguousEntries
;
98 static lck_mtx_t
* gIOMallocContiguousEntriesLock
;
100 enum { kIOMaxPageableMaps
= 16 };
101 enum { kIOPageableMapSize
= 96 * 1024 * 1024 };
102 enum { kIOPageableMaxMapSize
= 96 * 1024 * 1024 };
113 IOMapData maps
[ kIOMaxPageableMaps
];
115 } gIOKitPageableSpace
;
117 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
123 static bool libInitialized
;
128 gIOKitPageableSpace
.maps
[0].address
= 0;
129 ret
= kmem_suballoc(kernel_map
,
130 &gIOKitPageableSpace
.maps
[0].address
,
134 &gIOKitPageableSpace
.maps
[0].map
);
135 if (ret
!= KERN_SUCCESS
)
136 panic("failed to allocate iokit pageable map\n");
138 IOLockGroup
= lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL
);
140 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
141 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
142 gIOKitPageableSpace
.hint
= 0;
143 gIOKitPageableSpace
.count
= 1;
145 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
146 queue_init( &gIOMallocContiguousEntries
);
148 libInitialized
= true;
151 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
153 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
155 kern_return_t result
;
158 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
159 if (result
!= KERN_SUCCESS
)
162 thread_deallocate(thread
);
168 void IOExitThread(void)
170 (void) thread_terminate(current_thread());
173 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
176 void * IOMalloc(vm_size_t size
)
180 address
= (void *)kalloc(size
);
183 debug_iomalloc_size
+= size
;
189 void IOFree(void * address
, vm_size_t size
)
192 kfree(address
, size
);
194 debug_iomalloc_size
-= size
;
199 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
201 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
205 vm_offset_t allocationAddress
;
206 vm_size_t adjustedSize
;
214 alignMask
= alignment
- 1;
215 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
217 if (adjustedSize
>= page_size
) {
219 kr
= kernel_memory_allocate(kernel_map
, &address
,
221 if (KERN_SUCCESS
!= kr
)
226 adjustedSize
+= alignMask
;
228 if (adjustedSize
>= page_size
) {
230 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
232 if (KERN_SUCCESS
!= kr
)
233 allocationAddress
= 0;
236 allocationAddress
= (vm_address_t
) kalloc(adjustedSize
);
238 if (allocationAddress
) {
239 address
= (allocationAddress
+ alignMask
240 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
243 *((vm_size_t
*)(address
- sizeof(vm_size_t
) - sizeof(vm_address_t
)))
245 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
251 assert(0 == (address
& alignMask
));
255 debug_iomalloc_size
+= size
;
259 return (void *) address
;
262 void IOFreeAligned(void * address
, vm_size_t size
)
264 vm_address_t allocationAddress
;
265 vm_size_t adjustedSize
;
272 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
273 if (adjustedSize
>= page_size
) {
275 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
278 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
279 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
280 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
281 - sizeof(vm_address_t
) ));
283 if (adjustedSize
>= page_size
)
284 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
286 kfree((void *)allocationAddress
, adjustedSize
);
290 debug_iomalloc_size
-= size
;
294 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
297 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
299 mach_vm_address_t allocationAddress
;
300 mach_vm_size_t adjustedSize
;
307 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
308 if (adjustedSize
>= page_size
) {
310 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
314 adjustedSize
= *((mach_vm_size_t
*)
315 (address
- sizeof(mach_vm_address_t
) - sizeof(mach_vm_size_t
)));
316 allocationAddress
= *((mach_vm_address_t
*)
317 (address
- sizeof(mach_vm_address_t
) ));
318 kfree((void *)allocationAddress
, adjustedSize
);
322 debug_iomalloc_size
-= size
;
327 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
328 mach_vm_size_t alignment
, bool contiguous
)
331 mach_vm_address_t address
;
332 mach_vm_address_t allocationAddress
;
333 mach_vm_size_t adjustedSize
;
334 mach_vm_address_t alignMask
;
341 alignMask
= alignment
- 1;
342 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
344 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
345 || (alignment
> page_size
);
347 if (contiguous
|| maxPhys
)
353 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
354 || (alignment
> page_size
);
356 if ((!contiguous
) && (maxPhys
<= 0xFFFFFFFF))
359 options
|= KMA_LOMEM
;
362 if (contiguous
|| maxPhys
)
364 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
365 alignMask
, atop(maxPhys
), atop(alignMask
), 0);
369 kr
= kernel_memory_allocate(kernel_map
, &virt
,
370 size
, alignMask
, options
);
372 if (KERN_SUCCESS
== kr
)
379 adjustedSize
+= alignMask
;
380 allocationAddress
= (mach_vm_address_t
) kalloc(adjustedSize
);
382 if (allocationAddress
) {
384 address
= (allocationAddress
+ alignMask
385 + (sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
)))
388 if (atop_32(address
) != atop_32(address
+ size
- 1))
389 address
= round_page(address
);
391 *((mach_vm_size_t
*)(address
- sizeof(mach_vm_size_t
)
392 - sizeof(mach_vm_address_t
))) = adjustedSize
;
393 *((mach_vm_address_t
*)(address
- sizeof(mach_vm_address_t
)))
401 debug_iomalloc_size
+= size
;
408 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
410 struct _IOMallocContiguousEntry
412 mach_vm_address_t virtualAddr
;
413 IOBufferMemoryDescriptor
* md
;
416 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
418 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
419 IOPhysicalAddress
* physicalAddress
)
421 mach_vm_address_t address
= 0;
428 /* Do we want a physical address? */
429 if (!physicalAddress
)
431 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
435 IOBufferMemoryDescriptor
* bmd
;
436 mach_vm_address_t physicalMask
;
437 vm_offset_t alignMask
;
439 alignMask
= alignment
- 1;
440 physicalMask
= (0xFFFFFFFF ^ alignMask
);
442 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
443 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
447 _IOMallocContiguousEntry
*
448 entry
= IONew(_IOMallocContiguousEntry
, 1);
454 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
456 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
457 queue_enter( &gIOMallocContiguousEntries
, entry
,
458 _IOMallocContiguousEntry
*, link
);
459 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
461 address
= (mach_vm_address_t
) entry
->virtualAddr
;
462 *physicalAddress
= bmd
->getPhysicalAddress();
466 return (void *) address
;
469 void IOFreeContiguous(void * _address
, vm_size_t size
)
471 _IOMallocContiguousEntry
* entry
;
472 IOMemoryDescriptor
* md
= NULL
;
474 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
481 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
482 queue_iterate( &gIOMallocContiguousEntries
, entry
,
483 _IOMallocContiguousEntry
*, link
)
485 if( entry
->virtualAddr
== address
) {
487 queue_remove( &gIOMallocContiguousEntries
, entry
,
488 _IOMallocContiguousEntry
*, link
);
492 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
497 IODelete(entry
, _IOMallocContiguousEntry
, 1);
501 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
505 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
507 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
508 IOIteratePageableMapsCallback callback
, void * ref
)
510 kern_return_t kr
= kIOReturnNotReady
;
517 if (size
> kIOPageableMaxMapSize
)
518 return( kIOReturnBadArgument
);
521 index
= gIOKitPageableSpace
.hint
;
522 attempts
= gIOKitPageableSpace
.count
;
524 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
525 if( KERN_SUCCESS
== kr
) {
526 gIOKitPageableSpace
.hint
= index
;
532 index
= gIOKitPageableSpace
.count
- 1;
534 if( KERN_SUCCESS
== kr
)
537 lck_mtx_lock( gIOKitPageableSpace
.lock
);
539 index
= gIOKitPageableSpace
.count
;
540 if( index
>= (kIOMaxPageableMaps
- 1)) {
541 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
545 if( size
< kIOPageableMapSize
)
546 segSize
= kIOPageableMapSize
;
551 kr
= kmem_suballoc(kernel_map
,
557 if( KERN_SUCCESS
!= kr
) {
558 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
562 gIOKitPageableSpace
.maps
[index
].map
= map
;
563 gIOKitPageableSpace
.maps
[index
].address
= min
;
564 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
565 gIOKitPageableSpace
.hint
= index
;
566 gIOKitPageableSpace
.count
= index
+ 1;
568 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
575 struct IOMallocPageableRef
581 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
583 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
586 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
591 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
593 kern_return_t kr
= kIOReturnNotReady
;
594 struct IOMallocPageableRef ref
;
596 if (alignment
> page_size
)
598 if (size
> kIOPageableMaxMapSize
)
602 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
603 if( kIOReturnSuccess
!= kr
)
608 debug_iomallocpageable_size
+= round_page(size
);
611 return( (void *) ref
.address
);
614 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
619 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
620 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
621 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
622 map
= gIOKitPageableSpace
.maps
[index
].map
;
627 panic("IOPageableMapForAddress: null");
632 void IOFreePageable(void * address
, vm_size_t size
)
636 map
= IOPageableMapForAddress( (vm_address_t
) address
);
638 kmem_free( map
, (vm_offset_t
) address
, size
);
641 debug_iomallocpageable_size
-= round_page(size
);
645 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
647 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
648 IOByteCount length
, IOOptionBits cacheMode
)
650 IOReturn ret
= kIOReturnSuccess
;
653 if( task
!= kernel_task
)
654 return( kIOReturnUnsupported
);
655 if ((address
| length
) & PAGE_MASK
)
657 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
658 return( kIOReturnUnsupported
);
660 length
= round_page(address
+ length
) - trunc_page( address
);
661 address
= trunc_page( address
);
664 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
666 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
668 // Get the physical page number
669 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
671 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
672 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
674 ret
= kIOReturnVMError
;
676 address
+= page_size
;
684 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
687 if( task
!= kernel_task
)
688 return( kIOReturnUnsupported
);
690 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
692 return( kIOReturnSuccess
);
695 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
697 vm_offset_t
OSKernelStackRemaining( void )
699 return (ml_stack_remaining());
702 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
705 * Spin for indicated number of milliseconds.
707 void IOSleep(unsigned milliseconds
)
709 delay_for_interval(milliseconds
, kMillisecondScale
);
713 * Spin for indicated number of microseconds.
715 void IODelay(unsigned microseconds
)
717 delay_for_interval(microseconds
, kMicrosecondScale
);
721 * Spin for indicated number of nanoseconds.
723 void IOPause(unsigned nanoseconds
)
725 delay_for_interval(nanoseconds
, kNanosecondScale
);
728 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
730 static void _iolog_putc(int ch
, void *arg __unused
)
735 void IOLog(const char *format
, ...)
739 va_start(ap
, format
);
740 __doprnt(format
, ap
, _iolog_putc
, NULL
, 16);
744 void IOLogv(const char *format
, va_list ap
)
746 __doprnt(format
, ap
, _iolog_putc
, NULL
, 16);
750 void IOPanic(const char *reason
)
756 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
759 * Convert a integer constant (typically a #define or enum) to a string.
761 static char noValue
[80]; // that's pretty
763 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
765 for( ; regValueArray
->name
; regValueArray
++) {
766 if(regValueArray
->value
== value
)
767 return(regValueArray
->name
);
769 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
770 return((const char *)noValue
);
773 IOReturn
IOFindValueForName(const char *string
,
774 const IONamedValue
*regValueArray
,
777 for( ; regValueArray
->name
; regValueArray
++) {
778 if(!strcmp(regValueArray
->name
, string
)) {
779 *value
= regValueArray
->value
;
780 return kIOReturnSuccess
;
783 return kIOReturnBadArgument
;
786 OSString
* IOCopyLogNameForPID(int pid
)
790 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
792 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
793 return (OSString::withCString(buf
));
796 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
798 IOAlignment
IOSizeToAlignment(unsigned int size
)
801 const int intsize
= sizeof(unsigned int) * 8;
803 for (shift
= 1; shift
< intsize
; shift
++) {
804 if (size
& 0x80000000)
805 return (IOAlignment
)(intsize
- shift
);
811 unsigned int IOAlignmentToSize(IOAlignment align
)
815 for (size
= 1; align
; align
--) {