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
;
122 enum { kIOMaxPageableMaps
= 16 };
123 enum { kIOPageableMapSize
= 96 * 1024 * 1024 };
124 enum { kIOPageableMaxMapSize
= 96 * 1024 * 1024 };
135 IOMapData maps
[ kIOMaxPageableMaps
];
137 } gIOKitPageableSpace
;
139 static iopa_t gIOPageablePageAllocator
;
141 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
147 static bool libInitialized
;
152 gIOKitPageableSpace
.maps
[0].address
= 0;
153 ret
= kmem_suballoc(kernel_map
,
154 &gIOKitPageableSpace
.maps
[0].address
,
158 &gIOKitPageableSpace
.maps
[0].map
);
159 if (ret
!= KERN_SUCCESS
)
160 panic("failed to allocate iokit pageable map\n");
162 IOLockGroup
= lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL
);
164 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
165 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
166 gIOKitPageableSpace
.hint
= 0;
167 gIOKitPageableSpace
.count
= 1;
169 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
170 queue_init( &gIOMallocContiguousEntries
);
172 iopa_init(&gIOBMDPageAllocator
);
173 iopa_init(&gIOPageablePageAllocator
);
175 libInitialized
= true;
178 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
180 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
182 kern_return_t result
;
185 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
186 if (result
!= KERN_SUCCESS
)
189 thread_deallocate(thread
);
195 void IOExitThread(void)
197 (void) thread_terminate(current_thread());
200 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
203 void * IOMalloc(vm_size_t size
)
207 address
= (void *)kalloc(size
);
210 debug_iomalloc_size
+= size
;
212 IOStatisticsAlloc(kIOStatisticsMalloc
, size
);
218 void IOFree(void * address
, vm_size_t size
)
221 kfree(address
, size
);
223 debug_iomalloc_size
-= size
;
225 IOStatisticsAlloc(kIOStatisticsFree
, size
);
229 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
231 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
235 vm_offset_t allocationAddress
;
236 vm_size_t adjustedSize
;
244 alignMask
= alignment
- 1;
245 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
247 if (size
> adjustedSize
) {
248 address
= 0; /* overflow detected */
250 else if (adjustedSize
>= page_size
) {
252 kr
= kernel_memory_allocate(kernel_map
, &address
,
254 if (KERN_SUCCESS
!= kr
)
259 adjustedSize
+= alignMask
;
261 if (adjustedSize
>= page_size
) {
263 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
265 if (KERN_SUCCESS
!= kr
)
266 allocationAddress
= 0;
269 allocationAddress
= (vm_address_t
) kalloc(adjustedSize
);
271 if (allocationAddress
) {
272 address
= (allocationAddress
+ alignMask
273 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
276 *((vm_size_t
*)(address
- sizeof(vm_size_t
) - sizeof(vm_address_t
)))
278 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
284 assert(0 == (address
& alignMask
));
288 debug_iomalloc_size
+= size
;
290 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
293 return (void *) address
;
296 void IOFreeAligned(void * address
, vm_size_t size
)
298 vm_address_t allocationAddress
;
299 vm_size_t adjustedSize
;
306 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
307 if (adjustedSize
>= page_size
) {
309 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
312 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
313 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
314 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
315 - sizeof(vm_address_t
) ));
317 if (adjustedSize
>= page_size
)
318 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
320 kfree((void *)allocationAddress
, adjustedSize
);
324 debug_iomalloc_size
-= size
;
327 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
330 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
333 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
335 mach_vm_address_t allocationAddress
;
336 mach_vm_size_t adjustedSize
;
343 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
344 if (adjustedSize
>= page_size
) {
346 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
350 adjustedSize
= *((mach_vm_size_t
*)
351 (address
- sizeof(mach_vm_address_t
) - sizeof(mach_vm_size_t
)));
352 allocationAddress
= *((mach_vm_address_t
*)
353 (address
- sizeof(mach_vm_address_t
) ));
354 kfree((void *)allocationAddress
, adjustedSize
);
357 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
359 debug_iomalloc_size
-= size
;
364 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
365 mach_vm_size_t alignment
, bool contiguous
)
368 mach_vm_address_t address
;
369 mach_vm_address_t allocationAddress
;
370 mach_vm_size_t adjustedSize
;
371 mach_vm_address_t alignMask
;
378 alignMask
= alignment
- 1;
379 adjustedSize
= (2 * size
) + sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
);
381 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
382 || (alignment
> page_size
);
384 if (contiguous
|| maxPhys
)
390 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
391 || (alignment
> page_size
);
395 if (maxPhys
<= 0xFFFFFFFF)
398 options
|= KMA_LOMEM
;
400 else if (gIOLastPage
&& (atop_64(maxPhys
) > gIOLastPage
))
405 if (contiguous
|| maxPhys
)
407 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
408 alignMask
, atop(maxPhys
), atop(alignMask
), 0);
412 kr
= kernel_memory_allocate(kernel_map
, &virt
,
413 size
, alignMask
, options
);
415 if (KERN_SUCCESS
== kr
)
422 adjustedSize
+= alignMask
;
423 allocationAddress
= (mach_vm_address_t
) kalloc(adjustedSize
);
425 if (allocationAddress
) {
427 address
= (allocationAddress
+ alignMask
428 + (sizeof(mach_vm_size_t
) + sizeof(mach_vm_address_t
)))
431 if (atop_32(address
) != atop_32(address
+ size
- 1))
432 address
= round_page(address
);
434 *((mach_vm_size_t
*)(address
- sizeof(mach_vm_size_t
)
435 - sizeof(mach_vm_address_t
))) = adjustedSize
;
436 *((mach_vm_address_t
*)(address
- sizeof(mach_vm_address_t
)))
443 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
445 debug_iomalloc_size
+= size
;
453 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
455 struct _IOMallocContiguousEntry
457 mach_vm_address_t virtualAddr
;
458 IOBufferMemoryDescriptor
* md
;
461 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
463 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
464 IOPhysicalAddress
* physicalAddress
)
466 mach_vm_address_t address
= 0;
473 /* Do we want a physical address? */
474 if (!physicalAddress
)
476 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
480 IOBufferMemoryDescriptor
* bmd
;
481 mach_vm_address_t physicalMask
;
482 vm_offset_t alignMask
;
484 alignMask
= alignment
- 1;
485 physicalMask
= (0xFFFFFFFF ^ alignMask
);
487 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
488 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
492 _IOMallocContiguousEntry
*
493 entry
= IONew(_IOMallocContiguousEntry
, 1);
499 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
501 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
502 queue_enter( &gIOMallocContiguousEntries
, entry
,
503 _IOMallocContiguousEntry
*, link
);
504 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
506 address
= (mach_vm_address_t
) entry
->virtualAddr
;
507 *physicalAddress
= bmd
->getPhysicalAddress();
511 return (void *) address
;
514 void IOFreeContiguous(void * _address
, vm_size_t size
)
516 _IOMallocContiguousEntry
* entry
;
517 IOMemoryDescriptor
* md
= NULL
;
519 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
526 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
527 queue_iterate( &gIOMallocContiguousEntries
, entry
,
528 _IOMallocContiguousEntry
*, link
)
530 if( entry
->virtualAddr
== address
) {
532 queue_remove( &gIOMallocContiguousEntries
, entry
,
533 _IOMallocContiguousEntry
*, link
);
537 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
542 IODelete(entry
, _IOMallocContiguousEntry
, 1);
546 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
550 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
552 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
553 IOIteratePageableMapsCallback callback
, void * ref
)
555 kern_return_t kr
= kIOReturnNotReady
;
562 if (size
> kIOPageableMaxMapSize
)
563 return( kIOReturnBadArgument
);
566 index
= gIOKitPageableSpace
.hint
;
567 attempts
= gIOKitPageableSpace
.count
;
569 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
570 if( KERN_SUCCESS
== kr
) {
571 gIOKitPageableSpace
.hint
= index
;
577 index
= gIOKitPageableSpace
.count
- 1;
579 if( KERN_SUCCESS
== kr
)
582 lck_mtx_lock( gIOKitPageableSpace
.lock
);
584 index
= gIOKitPageableSpace
.count
;
585 if( index
>= (kIOMaxPageableMaps
- 1)) {
586 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
590 if( size
< kIOPageableMapSize
)
591 segSize
= kIOPageableMapSize
;
596 kr
= kmem_suballoc(kernel_map
,
602 if( KERN_SUCCESS
!= kr
) {
603 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
607 gIOKitPageableSpace
.maps
[index
].map
= map
;
608 gIOKitPageableSpace
.maps
[index
].address
= min
;
609 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
610 gIOKitPageableSpace
.hint
= index
;
611 gIOKitPageableSpace
.count
= index
+ 1;
613 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
620 struct IOMallocPageableRef
626 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
628 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
631 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
636 static void * IOMallocPageablePages(vm_size_t size
, vm_size_t alignment
)
638 kern_return_t kr
= kIOReturnNotReady
;
639 struct IOMallocPageableRef ref
;
641 if (alignment
> page_size
)
643 if (size
> kIOPageableMaxMapSize
)
647 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
648 if( kIOReturnSuccess
!= kr
)
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 static void IOFreePageablePages(void * address
, vm_size_t size
)
676 map
= IOPageableMapForAddress( (vm_address_t
) address
);
678 kmem_free( map
, (vm_offset_t
) address
, size
);
681 static uintptr_t IOMallocOnePageablePage(iopa_t
* a
)
683 return ((uintptr_t) IOMallocPageablePages(page_size
, page_size
));
686 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
690 if (size
>= (page_size
- 4*kIOPageAllocChunkBytes
)) addr
= IOMallocPageablePages(size
, alignment
);
691 else addr
= ((void * ) iopa_alloc(&gIOPageablePageAllocator
, &IOMallocOnePageablePage
, size
, alignment
));
695 debug_iomallocpageable_size
+= size
;
697 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
703 void IOFreePageable(void * address
, vm_size_t size
)
706 debug_iomallocpageable_size
-= size
;
708 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
710 if (size
< (page_size
- 4*kIOPageAllocChunkBytes
))
712 address
= (void *) iopa_free(&gIOPageablePageAllocator
, (uintptr_t) address
, size
);
715 if (address
) IOFreePageablePages(address
, size
);
718 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
723 ((ex) ? (void)0 : Assert(__FILE__, __LINE__, # ex))
726 typedef char iopa_page_t_assert
[(sizeof(iopa_page_t
) <= kIOPageAllocChunkBytes
) ? 1 : -1];
729 iopa_init(iopa_t
* a
)
731 bzero(a
, sizeof(*a
));
732 a
->lock
= IOLockAlloc();
733 queue_init(&a
->list
);
737 iopa_allocinpage(iopa_page_t
* pa
, uint32_t count
, uint64_t align
)
740 uint64_t avail
= pa
->avail
;
744 // find strings of count 1 bits in avail
745 for (n
= count
; n
> 1; n
-= s
)
748 avail
= avail
& (avail
<< s
);
755 n
= __builtin_clzll(avail
);
756 pa
->avail
&= ~((-1ULL << (64 - count
)) >> n
);
757 if (!pa
->avail
&& pa
->link
.next
)
762 return (n
* kIOPageAllocChunkBytes
+ trunc_page((uintptr_t) pa
));
769 log2up(uint32_t size
)
771 if (size
<= 1) size
= 0;
772 else size
= 32 - __builtin_clz(size
- 1);
777 iopa_alloc(iopa_t
* a
, iopa_proc_t alloc
, vm_size_t bytes
, uint32_t balign
)
779 static const uint64_t align_masks
[] = {
793 if (!bytes
) bytes
= 1;
794 count
= (bytes
+ kIOPageAllocChunkBytes
- 1) / kIOPageAllocChunkBytes
;
795 align
= align_masks
[log2up((balign
+ kIOPageAllocChunkBytes
- 1) / kIOPageAllocChunkBytes
)];
798 pa
= (typeof(pa
)) queue_first(&a
->list
);
799 while (!queue_end(&a
->list
, &pa
->link
))
801 addr
= iopa_allocinpage(pa
, count
, align
);
804 a
->bytecount
+= bytes
;
807 pa
= (typeof(pa
)) queue_next(&pa
->link
);
809 IOLockUnlock(a
->lock
);
816 pa
= (typeof(pa
)) (addr
+ page_size
- kIOPageAllocChunkBytes
);
817 pa
->signature
= kIOPageAllocSignature
;
820 addr
= iopa_allocinpage(pa
, count
, align
);
822 if (pa
->avail
) enqueue_head(&a
->list
, &pa
->link
);
824 if (addr
) a
->bytecount
+= bytes
;
825 IOLockUnlock(a
->lock
);
829 assert((addr
& ((1 << log2up(balign
)) - 1)) == 0);
834 iopa_free(iopa_t
* a
, uintptr_t addr
, vm_size_t bytes
)
840 if (!bytes
) bytes
= 1;
842 chunk
= (addr
& page_mask
);
843 assert(0 == (chunk
& (kIOPageAllocChunkBytes
- 1)));
845 pa
= (typeof(pa
)) (addr
| (page_size
- kIOPageAllocChunkBytes
));
846 assert(kIOPageAllocSignature
== pa
->signature
);
848 count
= (bytes
+ kIOPageAllocChunkBytes
- 1) / kIOPageAllocChunkBytes
;
849 chunk
/= kIOPageAllocChunkBytes
;
854 assert(!pa
->link
.next
);
855 enqueue_tail(&a
->list
, &pa
->link
);
857 pa
->avail
|= ((-1ULL << (64 - count
)) >> chunk
);
858 if (pa
->avail
!= -2ULL) pa
= 0;
866 pa
= (typeof(pa
)) trunc_page(pa
);
868 a
->bytecount
-= bytes
;
869 IOLockUnlock(a
->lock
);
871 return ((uintptr_t) pa
);
874 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
876 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
877 IOByteCount length
, IOOptionBits cacheMode
)
879 IOReturn ret
= kIOReturnSuccess
;
882 if( task
!= kernel_task
)
883 return( kIOReturnUnsupported
);
884 if ((address
| length
) & PAGE_MASK
)
886 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
887 return( kIOReturnUnsupported
);
889 length
= round_page(address
+ length
) - trunc_page( address
);
890 address
= trunc_page( address
);
893 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
895 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
897 // Get the physical page number
898 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
900 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
901 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
903 ret
= kIOReturnVMError
;
905 address
+= page_size
;
913 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
916 if( task
!= kernel_task
)
917 return( kIOReturnUnsupported
);
919 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
921 return( kIOReturnSuccess
);
924 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
926 vm_offset_t
OSKernelStackRemaining( void )
928 return (ml_stack_remaining());
931 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
934 * Spin for indicated number of milliseconds.
936 void IOSleep(unsigned milliseconds
)
938 delay_for_interval(milliseconds
, kMillisecondScale
);
942 * Spin for indicated number of microseconds.
944 void IODelay(unsigned microseconds
)
946 delay_for_interval(microseconds
, kMicrosecondScale
);
950 * Spin for indicated number of nanoseconds.
952 void IOPause(unsigned nanoseconds
)
954 delay_for_interval(nanoseconds
, kNanosecondScale
);
957 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
959 static void _iolog_consputc(int ch
, void *arg __unused
)
961 cons_putc_locked(ch
);
964 static void _iolog_logputc(int ch
, void *arg __unused
)
969 void IOLog(const char *format
, ...)
973 va_start(ap
, format
);
978 void IOLogv(const char *format
, va_list ap
)
985 __doprnt(format
, ap
, _iolog_logputc
, NULL
, 16);
989 __doprnt(format
, ap2
, _iolog_consputc
, NULL
, 16);
993 void IOPanic(const char *reason
)
999 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1002 * Convert a integer constant (typically a #define or enum) to a string.
1004 static char noValue
[80]; // that's pretty
1006 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
1008 for( ; regValueArray
->name
; regValueArray
++) {
1009 if(regValueArray
->value
== value
)
1010 return(regValueArray
->name
);
1012 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
1013 return((const char *)noValue
);
1016 IOReturn
IOFindValueForName(const char *string
,
1017 const IONamedValue
*regValueArray
,
1020 for( ; regValueArray
->name
; regValueArray
++) {
1021 if(!strcmp(regValueArray
->name
, string
)) {
1022 *value
= regValueArray
->value
;
1023 return kIOReturnSuccess
;
1026 return kIOReturnBadArgument
;
1029 OSString
* IOCopyLogNameForPID(int pid
)
1033 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
1035 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
1036 return (OSString::withCString(buf
));
1039 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1041 IOAlignment
IOSizeToAlignment(unsigned int size
)
1044 const int intsize
= sizeof(unsigned int) * 8;
1046 for (shift
= 1; shift
< intsize
; shift
++) {
1047 if (size
& 0x80000000)
1048 return (IOAlignment
)(intsize
- shift
);
1054 unsigned int IOAlignmentToSize(IOAlignment align
)
1058 for (size
= 1; align
; align
--) {