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 (size
> adjustedSize
) {
241 address
= 0; /* overflow detected */
243 else if (adjustedSize
>= page_size
) {
245 kr
= kernel_memory_allocate(kernel_map
, &address
,
247 if (KERN_SUCCESS
!= kr
)
252 adjustedSize
+= alignMask
;
254 if (adjustedSize
>= page_size
) {
256 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
258 if (KERN_SUCCESS
!= kr
)
259 allocationAddress
= 0;
262 allocationAddress
= (vm_address_t
) kalloc(adjustedSize
);
264 if (allocationAddress
) {
265 address
= (allocationAddress
+ alignMask
266 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
269 *((vm_size_t
*)(address
- sizeof(vm_size_t
) - sizeof(vm_address_t
)))
271 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
277 assert(0 == (address
& alignMask
));
281 debug_iomalloc_size
+= size
;
283 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
286 return (void *) address
;
289 void IOFreeAligned(void * address
, vm_size_t size
)
291 vm_address_t allocationAddress
;
292 vm_size_t adjustedSize
;
299 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
300 if (adjustedSize
>= page_size
) {
302 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
305 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
306 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
307 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
308 - sizeof(vm_address_t
) ));
310 if (adjustedSize
>= page_size
)
311 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
313 kfree((void *)allocationAddress
, adjustedSize
);
317 debug_iomalloc_size
-= size
;
320 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
323 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
326 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
328 mach_vm_address_t allocationAddress
;
329 mach_vm_size_t adjustedSize
;
336 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
337 if (adjustedSize
>= page_size
) {
339 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
343 adjustedSize
= *((mach_vm_size_t
*)
344 (address
- sizeof(mach_vm_address_t
) - sizeof(mach_vm_size_t
)));
345 allocationAddress
= *((mach_vm_address_t
*)
346 (address
- sizeof(mach_vm_address_t
) ));
347 kfree((void *)allocationAddress
, adjustedSize
);
350 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
352 debug_iomalloc_size
-= size
;
357 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
358 mach_vm_size_t alignment
, bool contiguous
)
361 mach_vm_address_t address
;
362 mach_vm_address_t allocationAddress
;
363 mach_vm_size_t adjustedSize
;
364 mach_vm_address_t alignMask
;
371 alignMask
= alignment
- 1;
372 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
374 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
375 || (alignment
> page_size
);
377 if (contiguous
|| maxPhys
)
383 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
384 || (alignment
> page_size
);
388 if (maxPhys
<= 0xFFFFFFFF)
391 options
|= KMA_LOMEM
;
393 else if (gIOLastPage
&& (atop_64(maxPhys
) > gIOLastPage
))
398 if (contiguous
|| maxPhys
)
400 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
401 alignMask
, atop(maxPhys
), atop(alignMask
), 0);
405 kr
= kernel_memory_allocate(kernel_map
, &virt
,
406 size
, alignMask
, options
);
408 if (KERN_SUCCESS
== kr
)
415 adjustedSize
+= alignMask
;
416 allocationAddress
= (mach_vm_address_t
) kalloc(adjustedSize
);
418 if (allocationAddress
) {
420 address
= (allocationAddress
+ alignMask
421 + (sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
)))
424 if (atop_32(address
) != atop_32(address
+ size
- 1))
425 address
= round_page(address
);
427 *((mach_vm_size_t
*)(address
- sizeof(mach_vm_size_t
)
428 - sizeof(mach_vm_address_t
))) = adjustedSize
;
429 *((mach_vm_address_t
*)(address
- sizeof(mach_vm_address_t
)))
436 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
438 debug_iomalloc_size
+= size
;
446 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
448 struct _IOMallocContiguousEntry
450 mach_vm_address_t virtualAddr
;
451 IOBufferMemoryDescriptor
* md
;
454 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
456 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
457 IOPhysicalAddress
* physicalAddress
)
459 mach_vm_address_t address
= 0;
466 /* Do we want a physical address? */
467 if (!physicalAddress
)
469 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
473 IOBufferMemoryDescriptor
* bmd
;
474 mach_vm_address_t physicalMask
;
475 vm_offset_t alignMask
;
477 alignMask
= alignment
- 1;
478 physicalMask
= (0xFFFFFFFF ^ alignMask
);
480 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
481 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
485 _IOMallocContiguousEntry
*
486 entry
= IONew(_IOMallocContiguousEntry
, 1);
492 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
494 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
495 queue_enter( &gIOMallocContiguousEntries
, entry
,
496 _IOMallocContiguousEntry
*, link
);
497 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
499 address
= (mach_vm_address_t
) entry
->virtualAddr
;
500 *physicalAddress
= bmd
->getPhysicalAddress();
504 return (void *) address
;
507 void IOFreeContiguous(void * _address
, vm_size_t size
)
509 _IOMallocContiguousEntry
* entry
;
510 IOMemoryDescriptor
* md
= NULL
;
512 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
519 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
520 queue_iterate( &gIOMallocContiguousEntries
, entry
,
521 _IOMallocContiguousEntry
*, link
)
523 if( entry
->virtualAddr
== address
) {
525 queue_remove( &gIOMallocContiguousEntries
, entry
,
526 _IOMallocContiguousEntry
*, link
);
530 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
535 IODelete(entry
, _IOMallocContiguousEntry
, 1);
539 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
543 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
545 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
546 IOIteratePageableMapsCallback callback
, void * ref
)
548 kern_return_t kr
= kIOReturnNotReady
;
555 if (size
> kIOPageableMaxMapSize
)
556 return( kIOReturnBadArgument
);
559 index
= gIOKitPageableSpace
.hint
;
560 attempts
= gIOKitPageableSpace
.count
;
562 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
563 if( KERN_SUCCESS
== kr
) {
564 gIOKitPageableSpace
.hint
= index
;
570 index
= gIOKitPageableSpace
.count
- 1;
572 if( KERN_SUCCESS
== kr
)
575 lck_mtx_lock( gIOKitPageableSpace
.lock
);
577 index
= gIOKitPageableSpace
.count
;
578 if( index
>= (kIOMaxPageableMaps
- 1)) {
579 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
583 if( size
< kIOPageableMapSize
)
584 segSize
= kIOPageableMapSize
;
589 kr
= kmem_suballoc(kernel_map
,
595 if( KERN_SUCCESS
!= kr
) {
596 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
600 gIOKitPageableSpace
.maps
[index
].map
= map
;
601 gIOKitPageableSpace
.maps
[index
].address
= min
;
602 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
603 gIOKitPageableSpace
.hint
= index
;
604 gIOKitPageableSpace
.count
= index
+ 1;
606 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
613 struct IOMallocPageableRef
619 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
621 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
624 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
629 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
631 kern_return_t kr
= kIOReturnNotReady
;
632 struct IOMallocPageableRef ref
;
634 if (alignment
> page_size
)
636 if (size
> kIOPageableMaxMapSize
)
640 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
641 if( kIOReturnSuccess
!= kr
)
646 debug_iomallocpageable_size
+= round_page(size
);
648 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
651 return( (void *) ref
.address
);
654 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
659 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
660 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
661 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
662 map
= gIOKitPageableSpace
.maps
[index
].map
;
667 panic("IOPageableMapForAddress: null");
672 void IOFreePageable(void * address
, vm_size_t size
)
676 map
= IOPageableMapForAddress( (vm_address_t
) address
);
678 kmem_free( map
, (vm_offset_t
) address
, size
);
681 debug_iomallocpageable_size
-= round_page(size
);
684 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
687 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
689 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
690 IOByteCount length
, IOOptionBits cacheMode
)
692 IOReturn ret
= kIOReturnSuccess
;
695 if( task
!= kernel_task
)
696 return( kIOReturnUnsupported
);
697 if ((address
| length
) & PAGE_MASK
)
699 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
700 return( kIOReturnUnsupported
);
702 length
= round_page(address
+ length
) - trunc_page( address
);
703 address
= trunc_page( address
);
706 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
708 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
710 // Get the physical page number
711 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
713 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
714 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
716 ret
= kIOReturnVMError
;
718 address
+= page_size
;
726 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
729 if( task
!= kernel_task
)
730 return( kIOReturnUnsupported
);
732 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
734 return( kIOReturnSuccess
);
737 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
739 vm_offset_t
OSKernelStackRemaining( void )
741 return (ml_stack_remaining());
744 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
747 * Spin for indicated number of milliseconds.
749 void IOSleep(unsigned milliseconds
)
751 delay_for_interval(milliseconds
, kMillisecondScale
);
755 * Spin for indicated number of microseconds.
757 void IODelay(unsigned microseconds
)
759 delay_for_interval(microseconds
, kMicrosecondScale
);
763 * Spin for indicated number of nanoseconds.
765 void IOPause(unsigned nanoseconds
)
767 delay_for_interval(nanoseconds
, kNanosecondScale
);
770 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
772 static void _iolog_consputc(int ch
, void *arg __unused
)
774 cons_putc_locked(ch
);
777 static void _iolog_logputc(int ch
, void *arg __unused
)
782 void IOLog(const char *format
, ...)
786 va_start(ap
, format
);
791 void IOLogv(const char *format
, va_list ap
)
798 __doprnt(format
, ap
, _iolog_logputc
, NULL
, 16);
801 __doprnt(format
, ap2
, _iolog_consputc
, NULL
, 16);
805 void IOPanic(const char *reason
)
811 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
814 * Convert a integer constant (typically a #define or enum) to a string.
816 static char noValue
[80]; // that's pretty
818 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
820 for( ; regValueArray
->name
; regValueArray
++) {
821 if(regValueArray
->value
== value
)
822 return(regValueArray
->name
);
824 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
825 return((const char *)noValue
);
828 IOReturn
IOFindValueForName(const char *string
,
829 const IONamedValue
*regValueArray
,
832 for( ; regValueArray
->name
; regValueArray
++) {
833 if(!strcmp(regValueArray
->name
, string
)) {
834 *value
= regValueArray
->value
;
835 return kIOReturnSuccess
;
838 return kIOReturnBadArgument
;
841 OSString
* IOCopyLogNameForPID(int pid
)
845 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
847 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
848 return (OSString::withCString(buf
));
851 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
853 IOAlignment
IOSizeToAlignment(unsigned int size
)
856 const int intsize
= sizeof(unsigned int) * 8;
858 for (shift
= 1; shift
< intsize
; shift
++) {
859 if (size
& 0x80000000)
860 return (IOAlignment
)(intsize
- shift
);
866 unsigned int IOAlignmentToSize(IOAlignment align
)
870 for (size
= 1; align
; align
--) {