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);
95 extern void logwakeup();
98 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
100 lck_grp_t
*IOLockGroup
;
103 * Global variables for use by iLogger
104 * These symbols are for use only by Apple diagnostic code.
105 * Binary compatibility is not guaranteed for kexts that reference these symbols.
108 void *_giDebugLogInternal
= NULL
;
109 void *_giDebugLogDataInternal
= NULL
;
110 void *_giDebugReserved1
= NULL
;
111 void *_giDebugReserved2
= NULL
;
113 iopa_t gIOBMDPageAllocator
;
116 * Static variables for this module.
119 static queue_head_t gIOMallocContiguousEntries
;
120 static lck_mtx_t
* gIOMallocContiguousEntriesLock
;
123 enum { kIOMaxPageableMaps
= 8 };
124 enum { kIOPageableMapSize
= 512 * 1024 * 1024 };
125 enum { kIOPageableMaxMapSize
= 512 * 1024 * 1024 };
127 enum { kIOMaxPageableMaps
= 16 };
128 enum { kIOPageableMapSize
= 96 * 1024 * 1024 };
129 enum { kIOPageableMaxMapSize
= 96 * 1024 * 1024 };
141 IOMapData maps
[ kIOMaxPageableMaps
];
143 } gIOKitPageableSpace
;
145 static iopa_t gIOPageablePageAllocator
;
147 uint32_t gIOPageAllocChunkBytes
;
149 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
155 static bool libInitialized
;
160 gIOKitPageableSpace
.maps
[0].address
= 0;
161 ret
= kmem_suballoc(kernel_map
,
162 &gIOKitPageableSpace
.maps
[0].address
,
166 &gIOKitPageableSpace
.maps
[0].map
);
167 if (ret
!= KERN_SUCCESS
)
168 panic("failed to allocate iokit pageable map\n");
170 IOLockGroup
= lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL
);
172 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
173 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
174 gIOKitPageableSpace
.hint
= 0;
175 gIOKitPageableSpace
.count
= 1;
177 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
178 queue_init( &gIOMallocContiguousEntries
);
180 gIOPageAllocChunkBytes
= PAGE_SIZE
/64;
181 assert(sizeof(iopa_page_t
) <= gIOPageAllocChunkBytes
);
182 iopa_init(&gIOBMDPageAllocator
);
183 iopa_init(&gIOPageablePageAllocator
);
185 libInitialized
= true;
188 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
190 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
192 kern_return_t result
;
195 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
196 if (result
!= KERN_SUCCESS
)
199 thread_deallocate(thread
);
205 void IOExitThread(void)
207 (void) thread_terminate(current_thread());
210 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
213 void * IOMalloc(vm_size_t size
)
217 address
= (void *)kalloc(size
);
220 debug_iomalloc_size
+= size
;
222 IOStatisticsAlloc(kIOStatisticsMalloc
, size
);
228 void IOFree(void * address
, vm_size_t size
)
231 kfree(address
, size
);
233 debug_iomalloc_size
-= size
;
235 IOStatisticsAlloc(kIOStatisticsFree
, size
);
239 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
241 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
245 vm_offset_t allocationAddress
;
246 vm_size_t adjustedSize
;
254 alignMask
= alignment
- 1;
255 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
257 if (size
> adjustedSize
) {
258 address
= 0; /* overflow detected */
260 else if (adjustedSize
>= page_size
) {
262 kr
= kernel_memory_allocate(kernel_map
, &address
,
264 if (KERN_SUCCESS
!= kr
)
269 adjustedSize
+= alignMask
;
271 if (adjustedSize
>= page_size
) {
273 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
275 if (KERN_SUCCESS
!= kr
)
276 allocationAddress
= 0;
279 allocationAddress
= (vm_address_t
) kalloc(adjustedSize
);
281 if (allocationAddress
) {
282 address
= (allocationAddress
+ alignMask
283 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
286 *((vm_size_t
*)(address
- sizeof(vm_size_t
) - sizeof(vm_address_t
)))
288 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
294 assert(0 == (address
& alignMask
));
298 debug_iomalloc_size
+= size
;
300 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
303 return (void *) address
;
306 void IOFreeAligned(void * address
, vm_size_t size
)
308 vm_address_t allocationAddress
;
309 vm_size_t adjustedSize
;
316 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
317 if (adjustedSize
>= page_size
) {
319 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
322 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
323 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
324 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
325 - sizeof(vm_address_t
) ));
327 if (adjustedSize
>= page_size
)
328 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
330 kfree((void *)allocationAddress
, adjustedSize
);
334 debug_iomalloc_size
-= size
;
337 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
340 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
343 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
345 mach_vm_address_t allocationAddress
;
346 mach_vm_size_t adjustedSize
;
353 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
354 if (adjustedSize
>= page_size
) {
356 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
360 adjustedSize
= *((mach_vm_size_t
*)
361 (address
- sizeof(mach_vm_address_t
) - sizeof(mach_vm_size_t
)));
362 allocationAddress
= *((mach_vm_address_t
*)
363 (address
- sizeof(mach_vm_address_t
) ));
364 kfree((void *)allocationAddress
, adjustedSize
);
367 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
369 debug_iomalloc_size
-= size
;
375 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
376 mach_vm_size_t alignment
, bool contiguous
)
379 mach_vm_address_t address
;
380 mach_vm_address_t allocationAddress
;
381 mach_vm_size_t adjustedSize
;
382 mach_vm_address_t alignMask
;
389 alignMask
= alignment
- 1;
390 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
392 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
393 || (alignment
> page_size
);
395 if (contiguous
|| maxPhys
)
401 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
402 || (alignment
> page_size
);
406 if (maxPhys
<= 0xFFFFFFFF)
409 options
|= KMA_LOMEM
;
411 else if (gIOLastPage
&& (atop_64(maxPhys
) > gIOLastPage
))
416 if (contiguous
|| maxPhys
)
418 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
419 alignMask
, atop(maxPhys
), atop(alignMask
), 0);
423 kr
= kernel_memory_allocate(kernel_map
, &virt
,
424 size
, alignMask
, options
);
426 if (KERN_SUCCESS
== kr
)
433 adjustedSize
+= alignMask
;
434 allocationAddress
= (mach_vm_address_t
) kalloc(adjustedSize
);
436 if (allocationAddress
) {
438 address
= (allocationAddress
+ alignMask
439 + (sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
)))
442 if (atop_32(address
) != atop_32(address
+ size
- 1))
443 address
= round_page(address
);
445 *((mach_vm_size_t
*)(address
- sizeof(mach_vm_size_t
)
446 - sizeof(mach_vm_address_t
))) = adjustedSize
;
447 *((mach_vm_address_t
*)(address
- sizeof(mach_vm_address_t
)))
454 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
456 debug_iomalloc_size
+= size
;
464 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
466 struct _IOMallocContiguousEntry
468 mach_vm_address_t virtualAddr
;
469 IOBufferMemoryDescriptor
* md
;
472 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
474 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
475 IOPhysicalAddress
* physicalAddress
)
477 mach_vm_address_t address
= 0;
484 /* Do we want a physical address? */
485 if (!physicalAddress
)
487 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
491 IOBufferMemoryDescriptor
* bmd
;
492 mach_vm_address_t physicalMask
;
493 vm_offset_t alignMask
;
495 alignMask
= alignment
- 1;
496 physicalMask
= (0xFFFFFFFF ^ alignMask
);
498 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
499 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
503 _IOMallocContiguousEntry
*
504 entry
= IONew(_IOMallocContiguousEntry
, 1);
510 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
512 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
513 queue_enter( &gIOMallocContiguousEntries
, entry
,
514 _IOMallocContiguousEntry
*, link
);
515 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
517 address
= (mach_vm_address_t
) entry
->virtualAddr
;
518 *physicalAddress
= bmd
->getPhysicalAddress();
522 return (void *) address
;
525 void IOFreeContiguous(void * _address
, vm_size_t size
)
527 _IOMallocContiguousEntry
* entry
;
528 IOMemoryDescriptor
* md
= NULL
;
530 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
537 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
538 queue_iterate( &gIOMallocContiguousEntries
, entry
,
539 _IOMallocContiguousEntry
*, link
)
541 if( entry
->virtualAddr
== address
) {
543 queue_remove( &gIOMallocContiguousEntries
, entry
,
544 _IOMallocContiguousEntry
*, link
);
548 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
553 IODelete(entry
, _IOMallocContiguousEntry
, 1);
557 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
561 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
563 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
564 IOIteratePageableMapsCallback callback
, void * ref
)
566 kern_return_t kr
= kIOReturnNotReady
;
573 if (size
> kIOPageableMaxMapSize
)
574 return( kIOReturnBadArgument
);
577 index
= gIOKitPageableSpace
.hint
;
578 attempts
= gIOKitPageableSpace
.count
;
580 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
581 if( KERN_SUCCESS
== kr
) {
582 gIOKitPageableSpace
.hint
= index
;
588 index
= gIOKitPageableSpace
.count
- 1;
590 if( KERN_SUCCESS
== kr
)
593 lck_mtx_lock( gIOKitPageableSpace
.lock
);
595 index
= gIOKitPageableSpace
.count
;
596 if( index
>= (kIOMaxPageableMaps
- 1)) {
597 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
601 if( size
< kIOPageableMapSize
)
602 segSize
= kIOPageableMapSize
;
607 kr
= kmem_suballoc(kernel_map
,
613 if( KERN_SUCCESS
!= kr
) {
614 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
618 gIOKitPageableSpace
.maps
[index
].map
= map
;
619 gIOKitPageableSpace
.maps
[index
].address
= min
;
620 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
621 gIOKitPageableSpace
.hint
= index
;
622 gIOKitPageableSpace
.count
= index
+ 1;
624 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
631 struct IOMallocPageableRef
637 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
639 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
642 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
647 static void * IOMallocPageablePages(vm_size_t size
, vm_size_t alignment
)
649 kern_return_t kr
= kIOReturnNotReady
;
650 struct IOMallocPageableRef ref
;
652 if (alignment
> page_size
)
654 if (size
> kIOPageableMaxMapSize
)
658 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
659 if( kIOReturnSuccess
!= kr
)
662 return( (void *) ref
.address
);
665 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
670 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
671 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
672 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
673 map
= gIOKitPageableSpace
.maps
[index
].map
;
678 panic("IOPageableMapForAddress: null");
683 static void IOFreePageablePages(void * address
, vm_size_t size
)
687 map
= IOPageableMapForAddress( (vm_address_t
) address
);
689 kmem_free( map
, (vm_offset_t
) address
, size
);
692 static uintptr_t IOMallocOnePageablePage(iopa_t
* a
)
694 return ((uintptr_t) IOMallocPageablePages(page_size
, page_size
));
697 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
701 if (size
>= (page_size
- 4*gIOPageAllocChunkBytes
)) addr
= IOMallocPageablePages(size
, alignment
);
702 else addr
= ((void * ) iopa_alloc(&gIOPageablePageAllocator
, &IOMallocOnePageablePage
, size
, alignment
));
706 debug_iomallocpageable_size
+= size
;
708 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
714 void IOFreePageable(void * address
, vm_size_t size
)
717 debug_iomallocpageable_size
-= size
;
719 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
721 if (size
< (page_size
- 4*gIOPageAllocChunkBytes
))
723 address
= (void *) iopa_free(&gIOPageablePageAllocator
, (uintptr_t) address
, size
);
726 if (address
) IOFreePageablePages(address
, size
);
729 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
732 iopa_init(iopa_t
* a
)
734 bzero(a
, sizeof(*a
));
735 a
->lock
= IOLockAlloc();
736 queue_init(&a
->list
);
740 iopa_allocinpage(iopa_page_t
* pa
, uint32_t count
, uint64_t align
)
743 uint64_t avail
= pa
->avail
;
747 // find strings of count 1 bits in avail
748 for (n
= count
; n
> 1; n
-= s
)
751 avail
= avail
& (avail
<< s
);
758 n
= __builtin_clzll(avail
);
759 pa
->avail
&= ~((-1ULL << (64 - count
)) >> n
);
760 if (!pa
->avail
&& pa
->link
.next
)
765 return (n
* gIOPageAllocChunkBytes
+ trunc_page((uintptr_t) pa
));
772 log2up(uint32_t size
)
774 if (size
<= 1) size
= 0;
775 else size
= 32 - __builtin_clz(size
- 1);
780 iopa_alloc(iopa_t
* a
, iopa_proc_t alloc
, vm_size_t bytes
, uint32_t balign
)
782 static const uint64_t align_masks
[] = {
796 if (!bytes
) bytes
= 1;
797 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
798 align
= align_masks
[log2up((balign
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
)];
801 pa
= (typeof(pa
)) queue_first(&a
->list
);
802 while (!queue_end(&a
->list
, &pa
->link
))
804 addr
= iopa_allocinpage(pa
, count
, align
);
807 a
->bytecount
+= bytes
;
810 pa
= (typeof(pa
)) queue_next(&pa
->link
);
812 IOLockUnlock(a
->lock
);
819 pa
= (typeof(pa
)) (addr
+ page_size
- gIOPageAllocChunkBytes
);
820 pa
->signature
= kIOPageAllocSignature
;
823 addr
= iopa_allocinpage(pa
, count
, align
);
825 if (pa
->avail
) enqueue_head(&a
->list
, &pa
->link
);
827 if (addr
) a
->bytecount
+= bytes
;
828 IOLockUnlock(a
->lock
);
832 assert((addr
& ((1 << log2up(balign
)) - 1)) == 0);
837 iopa_free(iopa_t
* a
, uintptr_t addr
, vm_size_t bytes
)
843 if (!bytes
) bytes
= 1;
845 chunk
= (addr
& page_mask
);
846 assert(0 == (chunk
& (gIOPageAllocChunkBytes
- 1)));
848 pa
= (typeof(pa
)) (addr
| (page_size
- gIOPageAllocChunkBytes
));
849 assert(kIOPageAllocSignature
== pa
->signature
);
851 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
852 chunk
/= gIOPageAllocChunkBytes
;
857 assert(!pa
->link
.next
);
858 enqueue_tail(&a
->list
, &pa
->link
);
860 pa
->avail
|= ((-1ULL << (64 - count
)) >> chunk
);
861 if (pa
->avail
!= -2ULL) pa
= 0;
869 pa
= (typeof(pa
)) trunc_page(pa
);
871 a
->bytecount
-= bytes
;
872 IOLockUnlock(a
->lock
);
874 return ((uintptr_t) pa
);
877 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
879 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
880 IOByteCount length
, IOOptionBits cacheMode
)
882 IOReturn ret
= kIOReturnSuccess
;
885 if( task
!= kernel_task
)
886 return( kIOReturnUnsupported
);
887 if ((address
| length
) & PAGE_MASK
)
889 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
890 return( kIOReturnUnsupported
);
892 length
= round_page(address
+ length
) - trunc_page( address
);
893 address
= trunc_page( address
);
896 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
898 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
900 // Get the physical page number
901 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
903 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
904 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
906 ret
= kIOReturnVMError
;
908 address
+= page_size
;
916 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
919 if( task
!= kernel_task
)
920 return( kIOReturnUnsupported
);
922 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
924 return( kIOReturnSuccess
);
927 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
929 vm_offset_t
OSKernelStackRemaining( void )
931 return (ml_stack_remaining());
934 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
937 * Spin for indicated number of milliseconds.
939 void IOSleep(unsigned milliseconds
)
941 delay_for_interval(milliseconds
, kMillisecondScale
);
945 * Spin for indicated number of microseconds.
947 void IODelay(unsigned microseconds
)
949 delay_for_interval(microseconds
, kMicrosecondScale
);
953 * Spin for indicated number of nanoseconds.
955 void IOPause(unsigned nanoseconds
)
957 delay_for_interval(nanoseconds
, kNanosecondScale
);
960 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
962 static void _iolog_consputc(int ch
, void *arg __unused
)
964 cons_putc_locked(ch
);
967 static void _iolog_logputc(int ch
, void *arg __unused
)
972 void IOLog(const char *format
, ...)
976 va_start(ap
, format
);
981 void IOLogv(const char *format
, va_list ap
)
988 __doprnt(format
, ap
, _iolog_logputc
, NULL
, 16);
992 __doprnt(format
, ap2
, _iolog_consputc
, NULL
, 16);
996 void IOPanic(const char *reason
)
1002 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1005 * Convert a integer constant (typically a #define or enum) to a string.
1007 static char noValue
[80]; // that's pretty
1009 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
1011 for( ; regValueArray
->name
; regValueArray
++) {
1012 if(regValueArray
->value
== value
)
1013 return(regValueArray
->name
);
1015 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
1016 return((const char *)noValue
);
1019 IOReturn
IOFindValueForName(const char *string
,
1020 const IONamedValue
*regValueArray
,
1023 for( ; regValueArray
->name
; regValueArray
++) {
1024 if(!strcmp(regValueArray
->name
, string
)) {
1025 *value
= regValueArray
->value
;
1026 return kIOReturnSuccess
;
1029 return kIOReturnBadArgument
;
1032 OSString
* IOCopyLogNameForPID(int pid
)
1036 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
1038 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
1039 return (OSString::withCString(buf
));
1042 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1044 IOAlignment
IOSizeToAlignment(unsigned int size
)
1047 const int intsize
= sizeof(unsigned int) * 8;
1049 for (shift
= 1; shift
< intsize
; shift
++) {
1050 if (size
& 0x80000000)
1051 return (IOAlignment
)(intsize
- shift
);
1057 unsigned int IOAlignmentToSize(IOAlignment align
)
1061 for (size
= 1; align
; align
--) {