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>
58 #include "libkern/OSAtomic.h"
59 #include <libkern/c++/OSKext.h>
60 #include <IOKit/IOStatisticsPrivate.h>
61 #include <sys/msgbuf.h>
65 #define IOStatisticsAlloc(type, size) \
67 IOStatistics::countAlloc(type, size); \
72 #define IOStatisticsAlloc(type, size)
74 #endif /* IOKITSTATS */
80 mach_timespec_t IOZeroTvalspec
= { 0, 0 };
82 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
88 void (*putc
)(int, void *),
92 extern void cons_putc_locked(char);
93 extern void bsd_log_lock(void);
94 extern void bsd_log_unlock(void);
97 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
99 lck_grp_t
*IOLockGroup
;
102 * Global variables for use by iLogger
103 * These symbols are for use only by Apple diagnostic code.
104 * Binary compatibility is not guaranteed for kexts that reference these symbols.
107 void *_giDebugLogInternal
= NULL
;
108 void *_giDebugLogDataInternal
= NULL
;
109 void *_giDebugReserved1
= NULL
;
110 void *_giDebugReserved2
= NULL
;
114 * Static variables for this module.
117 static queue_head_t gIOMallocContiguousEntries
;
118 static lck_mtx_t
* gIOMallocContiguousEntriesLock
;
120 enum { kIOMaxPageableMaps
= 16 };
121 enum { kIOPageableMapSize
= 96 * 1024 * 1024 };
122 enum { kIOPageableMaxMapSize
= 96 * 1024 * 1024 };
133 IOMapData maps
[ kIOMaxPageableMaps
];
135 } gIOKitPageableSpace
;
137 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
143 static bool libInitialized
;
148 gIOKitPageableSpace
.maps
[0].address
= 0;
149 ret
= kmem_suballoc(kernel_map
,
150 &gIOKitPageableSpace
.maps
[0].address
,
154 &gIOKitPageableSpace
.maps
[0].map
);
155 if (ret
!= KERN_SUCCESS
)
156 panic("failed to allocate iokit pageable map\n");
158 IOLockGroup
= lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL
);
160 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
161 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
162 gIOKitPageableSpace
.hint
= 0;
163 gIOKitPageableSpace
.count
= 1;
165 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
166 queue_init( &gIOMallocContiguousEntries
);
168 libInitialized
= true;
171 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
173 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
175 kern_return_t result
;
178 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
179 if (result
!= KERN_SUCCESS
)
182 thread_deallocate(thread
);
188 void IOExitThread(void)
190 (void) thread_terminate(current_thread());
193 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
196 void * IOMalloc(vm_size_t size
)
200 address
= (void *)kalloc(size
);
203 debug_iomalloc_size
+= size
;
205 IOStatisticsAlloc(kIOStatisticsMalloc
, size
);
211 void IOFree(void * address
, vm_size_t size
)
214 kfree(address
, size
);
216 debug_iomalloc_size
-= size
;
218 IOStatisticsAlloc(kIOStatisticsFree
, size
);
222 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
224 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
228 vm_offset_t allocationAddress
;
229 vm_size_t adjustedSize
;
237 alignMask
= alignment
- 1;
238 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
240 if (adjustedSize
>= page_size
) {
242 kr
= kernel_memory_allocate(kernel_map
, &address
,
244 if (KERN_SUCCESS
!= kr
)
249 adjustedSize
+= alignMask
;
251 if (adjustedSize
>= page_size
) {
253 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
255 if (KERN_SUCCESS
!= kr
)
256 allocationAddress
= 0;
259 allocationAddress
= (vm_address_t
) kalloc(adjustedSize
);
261 if (allocationAddress
) {
262 address
= (allocationAddress
+ alignMask
263 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
266 *((vm_size_t
*)(address
- sizeof(vm_size_t
) - sizeof(vm_address_t
)))
268 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
274 assert(0 == (address
& alignMask
));
278 debug_iomalloc_size
+= size
;
280 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
283 return (void *) address
;
286 void IOFreeAligned(void * address
, vm_size_t size
)
288 vm_address_t allocationAddress
;
289 vm_size_t adjustedSize
;
296 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
297 if (adjustedSize
>= page_size
) {
299 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
302 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
303 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
304 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
305 - sizeof(vm_address_t
) ));
307 if (adjustedSize
>= page_size
)
308 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
310 kfree((void *)allocationAddress
, adjustedSize
);
314 debug_iomalloc_size
-= size
;
317 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
320 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
323 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
325 mach_vm_address_t allocationAddress
;
326 mach_vm_size_t adjustedSize
;
333 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
334 if (adjustedSize
>= page_size
) {
336 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
340 adjustedSize
= *((mach_vm_size_t
*)
341 (address
- sizeof(mach_vm_address_t
) - sizeof(mach_vm_size_t
)));
342 allocationAddress
= *((mach_vm_address_t
*)
343 (address
- sizeof(mach_vm_address_t
) ));
344 kfree((void *)allocationAddress
, adjustedSize
);
348 debug_iomalloc_size
-= size
;
353 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
354 mach_vm_size_t alignment
, bool contiguous
)
357 mach_vm_address_t address
;
358 mach_vm_address_t allocationAddress
;
359 mach_vm_size_t adjustedSize
;
360 mach_vm_address_t alignMask
;
367 alignMask
= alignment
- 1;
368 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
370 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
371 || (alignment
> page_size
);
373 if (contiguous
|| maxPhys
)
379 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
380 || (alignment
> page_size
);
382 if ((!contiguous
) && (maxPhys
<= 0xFFFFFFFF))
385 options
|= KMA_LOMEM
;
388 if (contiguous
|| maxPhys
)
390 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
391 alignMask
, atop(maxPhys
), atop(alignMask
), 0);
395 kr
= kernel_memory_allocate(kernel_map
, &virt
,
396 size
, alignMask
, options
);
398 if (KERN_SUCCESS
== kr
)
405 adjustedSize
+= alignMask
;
406 allocationAddress
= (mach_vm_address_t
) kalloc(adjustedSize
);
408 if (allocationAddress
) {
410 address
= (allocationAddress
+ alignMask
411 + (sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
)))
414 if (atop_32(address
) != atop_32(address
+ size
- 1))
415 address
= round_page(address
);
417 *((mach_vm_size_t
*)(address
- sizeof(mach_vm_size_t
)
418 - sizeof(mach_vm_address_t
))) = adjustedSize
;
419 *((mach_vm_address_t
*)(address
- sizeof(mach_vm_address_t
)))
427 debug_iomalloc_size
+= size
;
435 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
437 struct _IOMallocContiguousEntry
439 mach_vm_address_t virtualAddr
;
440 IOBufferMemoryDescriptor
* md
;
443 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
445 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
446 IOPhysicalAddress
* physicalAddress
)
448 mach_vm_address_t address
= 0;
455 /* Do we want a physical address? */
456 if (!physicalAddress
)
458 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
462 IOBufferMemoryDescriptor
* bmd
;
463 mach_vm_address_t physicalMask
;
464 vm_offset_t alignMask
;
466 alignMask
= alignment
- 1;
467 physicalMask
= (0xFFFFFFFF ^ alignMask
);
469 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
470 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
474 _IOMallocContiguousEntry
*
475 entry
= IONew(_IOMallocContiguousEntry
, 1);
481 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
483 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
484 queue_enter( &gIOMallocContiguousEntries
, entry
,
485 _IOMallocContiguousEntry
*, link
);
486 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
488 address
= (mach_vm_address_t
) entry
->virtualAddr
;
489 *physicalAddress
= bmd
->getPhysicalAddress();
494 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
497 return (void *) address
;
500 void IOFreeContiguous(void * _address
, vm_size_t size
)
502 _IOMallocContiguousEntry
* entry
;
503 IOMemoryDescriptor
* md
= NULL
;
505 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
512 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
513 queue_iterate( &gIOMallocContiguousEntries
, entry
,
514 _IOMallocContiguousEntry
*, link
)
516 if( entry
->virtualAddr
== address
) {
518 queue_remove( &gIOMallocContiguousEntries
, entry
,
519 _IOMallocContiguousEntry
*, link
);
523 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
528 IODelete(entry
, _IOMallocContiguousEntry
, 1);
532 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
535 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
538 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
540 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
541 IOIteratePageableMapsCallback callback
, void * ref
)
543 kern_return_t kr
= kIOReturnNotReady
;
550 if (size
> kIOPageableMaxMapSize
)
551 return( kIOReturnBadArgument
);
554 index
= gIOKitPageableSpace
.hint
;
555 attempts
= gIOKitPageableSpace
.count
;
557 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
558 if( KERN_SUCCESS
== kr
) {
559 gIOKitPageableSpace
.hint
= index
;
565 index
= gIOKitPageableSpace
.count
- 1;
567 if( KERN_SUCCESS
== kr
)
570 lck_mtx_lock( gIOKitPageableSpace
.lock
);
572 index
= gIOKitPageableSpace
.count
;
573 if( index
>= (kIOMaxPageableMaps
- 1)) {
574 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
578 if( size
< kIOPageableMapSize
)
579 segSize
= kIOPageableMapSize
;
584 kr
= kmem_suballoc(kernel_map
,
590 if( KERN_SUCCESS
!= kr
) {
591 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
595 gIOKitPageableSpace
.maps
[index
].map
= map
;
596 gIOKitPageableSpace
.maps
[index
].address
= min
;
597 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
598 gIOKitPageableSpace
.hint
= index
;
599 gIOKitPageableSpace
.count
= index
+ 1;
601 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
608 struct IOMallocPageableRef
614 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
616 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
619 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
624 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
626 kern_return_t kr
= kIOReturnNotReady
;
627 struct IOMallocPageableRef ref
;
629 if (alignment
> page_size
)
631 if (size
> kIOPageableMaxMapSize
)
635 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
636 if( kIOReturnSuccess
!= kr
)
641 debug_iomallocpageable_size
+= round_page(size
);
643 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
646 return( (void *) ref
.address
);
649 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
654 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
655 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
656 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
657 map
= gIOKitPageableSpace
.maps
[index
].map
;
662 panic("IOPageableMapForAddress: null");
667 void IOFreePageable(void * address
, vm_size_t size
)
671 map
= IOPageableMapForAddress( (vm_address_t
) address
);
673 kmem_free( map
, (vm_offset_t
) address
, size
);
676 debug_iomallocpageable_size
-= round_page(size
);
679 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
682 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
684 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
685 IOByteCount length
, IOOptionBits cacheMode
)
687 IOReturn ret
= kIOReturnSuccess
;
690 if( task
!= kernel_task
)
691 return( kIOReturnUnsupported
);
692 if ((address
| length
) & PAGE_MASK
)
694 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
695 return( kIOReturnUnsupported
);
697 length
= round_page(address
+ length
) - trunc_page( address
);
698 address
= trunc_page( address
);
701 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
703 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
705 // Get the physical page number
706 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
708 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
709 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
711 ret
= kIOReturnVMError
;
713 address
+= page_size
;
721 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
724 if( task
!= kernel_task
)
725 return( kIOReturnUnsupported
);
727 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
729 return( kIOReturnSuccess
);
732 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
734 vm_offset_t
OSKernelStackRemaining( void )
736 return (ml_stack_remaining());
739 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
742 * Spin for indicated number of milliseconds.
744 void IOSleep(unsigned milliseconds
)
746 delay_for_interval(milliseconds
, kMillisecondScale
);
750 * Spin for indicated number of microseconds.
752 void IODelay(unsigned microseconds
)
754 delay_for_interval(microseconds
, kMicrosecondScale
);
758 * Spin for indicated number of nanoseconds.
760 void IOPause(unsigned nanoseconds
)
762 delay_for_interval(nanoseconds
, kNanosecondScale
);
765 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
767 static void _iolog_consputc(int ch
, void *arg __unused
)
769 cons_putc_locked(ch
);
772 static void _iolog_logputc(int ch
, void *arg __unused
)
777 void IOLog(const char *format
, ...)
781 va_start(ap
, format
);
786 void IOLogv(const char *format
, va_list ap
)
793 __doprnt(format
, ap
, _iolog_logputc
, NULL
, 16);
796 __doprnt(format
, ap2
, _iolog_consputc
, NULL
, 16);
800 void IOPanic(const char *reason
)
806 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
809 * Convert a integer constant (typically a #define or enum) to a string.
811 static char noValue
[80]; // that's pretty
813 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
815 for( ; regValueArray
->name
; regValueArray
++) {
816 if(regValueArray
->value
== value
)
817 return(regValueArray
->name
);
819 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
820 return((const char *)noValue
);
823 IOReturn
IOFindValueForName(const char *string
,
824 const IONamedValue
*regValueArray
,
827 for( ; regValueArray
->name
; regValueArray
++) {
828 if(!strcmp(regValueArray
->name
, string
)) {
829 *value
= regValueArray
->value
;
830 return kIOReturnSuccess
;
833 return kIOReturnBadArgument
;
836 OSString
* IOCopyLogNameForPID(int pid
)
840 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
842 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
843 return (OSString::withCString(buf
));
846 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
848 IOAlignment
IOSizeToAlignment(unsigned int size
)
851 const int intsize
= sizeof(unsigned int) * 8;
853 for (shift
= 1; shift
< intsize
; shift
++) {
854 if (size
& 0x80000000)
855 return (IOAlignment
)(intsize
- shift
);
861 unsigned int IOAlignmentToSize(IOAlignment align
)
865 for (size
= 1; align
; align
--) {