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 IOKernelFreeContiguous(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 IOKernelAllocateContiguous(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
328 mach_vm_size_t alignment
)
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 if (adjustedSize
>= page_size
)
348 if ((adjustedSize
> page_size
) || (alignment
> page_size
) || maxPhys
)
350 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
351 alignMask
, atop(maxPhys
), atop(alignMask
), 0);
355 kr
= kernel_memory_allocate(kernel_map
, &virt
,
358 if (KERN_SUCCESS
== kr
)
365 adjustedSize
+= alignMask
;
366 allocationAddress
= (mach_vm_address_t
) kalloc(adjustedSize
);
368 if (allocationAddress
) {
370 address
= (allocationAddress
+ alignMask
371 + (sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
)))
374 if (atop_32(address
) != atop_32(address
+ size
- 1))
375 address
= round_page(address
);
377 *((mach_vm_size_t
*)(address
- sizeof(mach_vm_size_t
)
378 - sizeof(mach_vm_address_t
))) = adjustedSize
;
379 *((mach_vm_address_t
*)(address
- sizeof(mach_vm_address_t
)))
387 debug_iomalloc_size
+= size
;
394 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
396 struct _IOMallocContiguousEntry
398 mach_vm_address_t virtualAddr
;
399 IOBufferMemoryDescriptor
* md
;
402 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
404 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
405 IOPhysicalAddress
* physicalAddress
)
407 mach_vm_address_t address
= 0;
414 /* Do we want a physical address? */
415 if (!physicalAddress
)
417 address
= IOKernelAllocateContiguous(size
, 0 /*maxPhys*/, alignment
);
421 IOBufferMemoryDescriptor
* bmd
;
422 mach_vm_address_t physicalMask
;
423 vm_offset_t alignMask
;
425 alignMask
= alignment
- 1;
426 physicalMask
= (0xFFFFFFFF ^ alignMask
);
428 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
429 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
433 _IOMallocContiguousEntry
*
434 entry
= IONew(_IOMallocContiguousEntry
, 1);
440 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
442 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
443 queue_enter( &gIOMallocContiguousEntries
, entry
,
444 _IOMallocContiguousEntry
*, link
);
445 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
447 address
= (mach_vm_address_t
) entry
->virtualAddr
;
448 *physicalAddress
= bmd
->getPhysicalAddress();
452 return (void *) address
;
455 void IOFreeContiguous(void * _address
, vm_size_t size
)
457 _IOMallocContiguousEntry
* entry
;
458 IOMemoryDescriptor
* md
= NULL
;
460 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
467 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
468 queue_iterate( &gIOMallocContiguousEntries
, entry
,
469 _IOMallocContiguousEntry
*, link
)
471 if( entry
->virtualAddr
== address
) {
473 queue_remove( &gIOMallocContiguousEntries
, entry
,
474 _IOMallocContiguousEntry
*, link
);
478 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
483 IODelete(entry
, _IOMallocContiguousEntry
, 1);
487 IOKernelFreeContiguous((mach_vm_address_t
) address
, size
);
491 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
493 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
494 IOIteratePageableMapsCallback callback
, void * ref
)
496 kern_return_t kr
= kIOReturnNotReady
;
503 if (size
> kIOPageableMaxMapSize
)
504 return( kIOReturnBadArgument
);
507 index
= gIOKitPageableSpace
.hint
;
508 attempts
= gIOKitPageableSpace
.count
;
510 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
511 if( KERN_SUCCESS
== kr
) {
512 gIOKitPageableSpace
.hint
= index
;
518 index
= gIOKitPageableSpace
.count
- 1;
520 if( KERN_SUCCESS
== kr
)
523 lck_mtx_lock( gIOKitPageableSpace
.lock
);
525 index
= gIOKitPageableSpace
.count
;
526 if( index
>= (kIOMaxPageableMaps
- 1)) {
527 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
531 if( size
< kIOPageableMapSize
)
532 segSize
= kIOPageableMapSize
;
537 kr
= kmem_suballoc(kernel_map
,
543 if( KERN_SUCCESS
!= kr
) {
544 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
548 gIOKitPageableSpace
.maps
[index
].map
= map
;
549 gIOKitPageableSpace
.maps
[index
].address
= min
;
550 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
551 gIOKitPageableSpace
.hint
= index
;
552 gIOKitPageableSpace
.count
= index
+ 1;
554 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
561 struct IOMallocPageableRef
567 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
569 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
572 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
577 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
579 kern_return_t kr
= kIOReturnNotReady
;
580 struct IOMallocPageableRef ref
;
582 if (alignment
> page_size
)
584 if (size
> kIOPageableMaxMapSize
)
588 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
589 if( kIOReturnSuccess
!= kr
)
594 debug_iomallocpageable_size
+= round_page(size
);
597 return( (void *) ref
.address
);
600 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
605 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
606 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
607 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
608 map
= gIOKitPageableSpace
.maps
[index
].map
;
613 panic("IOPageableMapForAddress: null");
618 void IOFreePageable(void * address
, vm_size_t size
)
622 map
= IOPageableMapForAddress( (vm_address_t
) address
);
624 kmem_free( map
, (vm_offset_t
) address
, size
);
627 debug_iomallocpageable_size
-= round_page(size
);
631 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
633 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
634 IOByteCount length
, IOOptionBits cacheMode
)
636 IOReturn ret
= kIOReturnSuccess
;
639 if( task
!= kernel_task
)
640 return( kIOReturnUnsupported
);
641 if ((address
| length
) & PAGE_MASK
)
643 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
644 return( kIOReturnUnsupported
);
646 length
= round_page(address
+ length
) - trunc_page( address
);
647 address
= trunc_page( address
);
650 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
652 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
654 // Get the physical page number
655 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
657 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
658 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
660 ret
= kIOReturnVMError
;
662 address
+= page_size
;
670 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
673 if( task
!= kernel_task
)
674 return( kIOReturnUnsupported
);
676 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
678 return( kIOReturnSuccess
);
681 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
683 vm_offset_t
OSKernelStackRemaining( void )
685 return (ml_stack_remaining());
688 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
691 * Spin for indicated number of milliseconds.
693 void IOSleep(unsigned milliseconds
)
695 delay_for_interval(milliseconds
, kMillisecondScale
);
699 * Spin for indicated number of microseconds.
701 void IODelay(unsigned microseconds
)
703 delay_for_interval(microseconds
, kMicrosecondScale
);
707 * Spin for indicated number of nanoseconds.
709 void IOPause(unsigned nanoseconds
)
711 delay_for_interval(nanoseconds
, kNanosecondScale
);
714 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
716 static void _iolog_putc(int ch
, void *arg __unused
)
721 void IOLog(const char *format
, ...)
725 va_start(ap
, format
);
726 __doprnt(format
, ap
, _iolog_putc
, NULL
, 16);
730 void IOLogv(const char *format
, va_list ap
)
732 __doprnt(format
, ap
, _iolog_putc
, NULL
, 16);
736 void IOPanic(const char *reason
)
742 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
745 * Convert a integer constant (typically a #define or enum) to a string.
747 static char noValue
[80]; // that's pretty
749 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
751 for( ; regValueArray
->name
; regValueArray
++) {
752 if(regValueArray
->value
== value
)
753 return(regValueArray
->name
);
755 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
756 return((const char *)noValue
);
759 IOReturn
IOFindValueForName(const char *string
,
760 const IONamedValue
*regValueArray
,
763 for( ; regValueArray
->name
; regValueArray
++) {
764 if(!strcmp(regValueArray
->name
, string
)) {
765 *value
= regValueArray
->value
;
766 return kIOReturnSuccess
;
769 return kIOReturnBadArgument
;
772 OSString
* IOCopyLogNameForPID(int pid
)
776 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
778 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
779 return (OSString::withCString(buf
));
782 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
784 IOAlignment
IOSizeToAlignment(unsigned int size
)
787 const int intsize
= sizeof(unsigned int) * 8;
789 for (shift
= 1; shift
< intsize
; shift
++) {
790 if (size
& 0x80000000)
791 return (IOAlignment
)(intsize
- shift
);
797 unsigned int IOAlignmentToSize(IOAlignment align
)
801 for (size
= 1; align
; align
--) {