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 <os/log_private.h>
62 #include <sys/msgbuf.h>
63 #include <console/serial_protos.h>
67 #define IOStatisticsAlloc(type, size) \
69 IOStatistics::countAlloc(type, size); \
74 #define IOStatisticsAlloc(type, size)
76 #endif /* IOKITSTATS */
79 #define TRACK_ALLOC (IOTRACKING && (kIOTracking & gIOKitDebug))
86 mach_timespec_t IOZeroTvalspec
= { 0, 0 };
88 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
94 void (*putc
)(int, void *),
99 extern void cons_putc_locked(char);
100 extern void bsd_log_lock(void);
101 extern void bsd_log_unlock(void);
104 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
106 lck_grp_t
*IOLockGroup
;
109 * Global variables for use by iLogger
110 * These symbols are for use only by Apple diagnostic code.
111 * Binary compatibility is not guaranteed for kexts that reference these symbols.
114 void *_giDebugLogInternal
= NULL
;
115 void *_giDebugLogDataInternal
= NULL
;
116 void *_giDebugReserved1
= NULL
;
117 void *_giDebugReserved2
= NULL
;
119 iopa_t gIOBMDPageAllocator
;
122 * Static variables for this module.
125 static queue_head_t gIOMallocContiguousEntries
;
126 static lck_mtx_t
* gIOMallocContiguousEntriesLock
;
129 enum { kIOMaxPageableMaps
= 8 };
130 enum { kIOPageableMapSize
= 512 * 1024 * 1024 };
131 enum { kIOPageableMaxMapSize
= 512 * 1024 * 1024 };
133 enum { kIOMaxPageableMaps
= 16 };
134 enum { kIOPageableMapSize
= 96 * 1024 * 1024 };
135 enum { kIOPageableMaxMapSize
= 96 * 1024 * 1024 };
147 IOMapData maps
[ kIOMaxPageableMaps
];
149 } gIOKitPageableSpace
;
151 static iopa_t gIOPageablePageAllocator
;
153 uint32_t gIOPageAllocChunkBytes
;
156 IOTrackingQueue
* gIOMallocTracking
;
157 IOTrackingQueue
* gIOWireTracking
;
158 IOTrackingQueue
* gIOMapTracking
;
159 #endif /* IOTRACKING */
161 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
167 static bool libInitialized
;
172 IOLockGroup
= lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL
);
176 gIOMallocTracking
= IOTrackingQueueAlloc(kIOMallocTrackingName
, 0, 0, 0,
177 kIOTrackingQueueTypeAlloc
,
179 gIOWireTracking
= IOTrackingQueueAlloc(kIOWireTrackingName
, 0, 0, page_size
, 0, 0);
181 size_t mapCaptureSize
= (kIOTracking
& gIOKitDebug
) ? page_size
: (1024*1024);
182 gIOMapTracking
= IOTrackingQueueAlloc(kIOMapTrackingName
, 0, 0, mapCaptureSize
,
183 kIOTrackingQueueTypeDefaultOn
184 | kIOTrackingQueueTypeMap
185 | kIOTrackingQueueTypeUser
,
189 gIOKitPageableSpace
.maps
[0].address
= 0;
190 ret
= kmem_suballoc(kernel_map
,
191 &gIOKitPageableSpace
.maps
[0].address
,
195 VM_MAP_KERNEL_FLAGS_NONE
,
196 VM_KERN_MEMORY_IOKIT
,
197 &gIOKitPageableSpace
.maps
[0].map
);
198 if (ret
!= KERN_SUCCESS
)
199 panic("failed to allocate iokit pageable map\n");
201 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
202 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
203 gIOKitPageableSpace
.hint
= 0;
204 gIOKitPageableSpace
.count
= 1;
206 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
207 queue_init( &gIOMallocContiguousEntries
);
209 gIOPageAllocChunkBytes
= PAGE_SIZE
/64;
210 assert(sizeof(iopa_page_t
) <= gIOPageAllocChunkBytes
);
211 iopa_init(&gIOBMDPageAllocator
);
212 iopa_init(&gIOPageablePageAllocator
);
215 libInitialized
= true;
218 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
221 log2up(uint32_t size
)
223 if (size
<= 1) size
= 0;
224 else size
= 32 - __builtin_clz(size
- 1);
228 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
230 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
232 kern_return_t result
;
235 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
236 if (result
!= KERN_SUCCESS
)
239 thread_deallocate(thread
);
245 void IOExitThread(void)
247 (void) thread_terminate(current_thread());
250 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
253 struct IOLibMallocHeader
255 IOTrackingAddress tracking
;
260 #define sizeofIOLibMallocHeader (sizeof(IOLibMallocHeader) - (TRACK_ALLOC ? 0 : sizeof(IOTrackingAddress)))
262 #define sizeofIOLibMallocHeader (0)
265 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
267 void * IOMalloc(vm_size_t size
)
272 allocSize
= size
+ sizeofIOLibMallocHeader
;
274 if (sizeofIOLibMallocHeader
&& (allocSize
<= size
)) return (NULL
); // overflow
276 address
= kalloc_tag_bt(allocSize
, VM_KERN_MEMORY_IOKIT
);
281 IOLibMallocHeader
* hdr
;
282 hdr
= (typeof(hdr
)) address
;
283 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
284 hdr
->tracking
.address
= ~(((uintptr_t) address
) + sizeofIOLibMallocHeader
);
285 hdr
->tracking
.size
= size
;
286 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true, VM_KERN_MEMORY_NONE
);
289 address
= (typeof(address
)) (((uintptr_t) address
) + sizeofIOLibMallocHeader
);
292 OSAddAtomic(size
, &debug_iomalloc_size
);
294 IOStatisticsAlloc(kIOStatisticsMalloc
, size
);
300 void IOFree(void * inAddress
, vm_size_t size
)
304 if ((address
= inAddress
))
306 address
= (typeof(address
)) (((uintptr_t) address
) - sizeofIOLibMallocHeader
);
311 IOLibMallocHeader
* hdr
;
312 struct ptr_reference
{ void * ptr
; };
313 volatile struct ptr_reference ptr
;
315 // we're about to block in IOTrackingRemove(), make sure the original pointer
316 // exists in memory or a register for leak scanning to find
319 hdr
= (typeof(hdr
)) address
;
320 if (size
!= hdr
->tracking
.size
)
322 OSReportWithBacktrace("bad IOFree size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
323 size
= hdr
->tracking
.size
;
325 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
330 kfree(address
, size
+ sizeofIOLibMallocHeader
);
332 OSAddAtomic(-size
, &debug_iomalloc_size
);
334 IOStatisticsAlloc(kIOStatisticsFree
, size
);
338 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
341 IOMemoryTag(vm_map_t map
)
345 if (!vm_kernel_map_is_kernel(map
)) return (VM_MEMORY_IOKIT
);
348 if (tag
== VM_KERN_MEMORY_NONE
) tag
= VM_KERN_MEMORY_IOKIT
;
353 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
355 struct IOLibPageMallocHeader
357 mach_vm_size_t allocationSize
;
358 mach_vm_address_t allocationAddress
;
360 IOTrackingAddress tracking
;
365 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader) - (TRACK_ALLOC ? 0 : sizeof(IOTrackingAddress)))
367 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader))
370 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
372 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
376 vm_offset_t allocationAddress
;
377 vm_size_t adjustedSize
;
379 IOLibPageMallocHeader
* hdr
;
384 alignment
= (1UL << log2up(alignment
));
385 alignMask
= alignment
- 1;
386 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
388 if (size
> adjustedSize
) {
389 address
= 0; /* overflow detected */
391 else if (adjustedSize
>= page_size
) {
393 kr
= kernel_memory_allocate(kernel_map
, &address
,
394 size
, alignMask
, 0, IOMemoryTag(kernel_map
));
395 if (KERN_SUCCESS
!= kr
) address
= 0;
397 else if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
402 adjustedSize
+= alignMask
;
404 if (adjustedSize
>= page_size
) {
406 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
407 adjustedSize
, 0, 0, IOMemoryTag(kernel_map
));
408 if (KERN_SUCCESS
!= kr
) allocationAddress
= 0;
411 allocationAddress
= (vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
413 if (allocationAddress
) {
414 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
417 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
418 hdr
->allocationSize
= adjustedSize
;
419 hdr
->allocationAddress
= allocationAddress
;
422 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
423 hdr
->tracking
.address
= ~address
;
424 hdr
->tracking
.size
= size
;
425 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true, VM_KERN_MEMORY_NONE
);
432 assert(0 == (address
& alignMask
));
436 OSAddAtomic(size
, &debug_iomalloc_size
);
438 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
441 return (void *) address
;
444 void IOFreeAligned(void * address
, vm_size_t size
)
446 vm_address_t allocationAddress
;
447 vm_size_t adjustedSize
;
448 IOLibPageMallocHeader
* hdr
;
455 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
456 if (adjustedSize
>= page_size
) {
458 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, (uintptr_t) address
, size
);
460 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
463 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
464 adjustedSize
= hdr
->allocationSize
;
465 allocationAddress
= hdr
->allocationAddress
;
470 if (size
!= hdr
->tracking
.size
)
472 OSReportWithBacktrace("bad IOFreeAligned size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
473 size
= hdr
->tracking
.size
;
475 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
478 if (adjustedSize
>= page_size
) {
479 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
481 kfree((void *)allocationAddress
, adjustedSize
);
486 OSAddAtomic(-size
, &debug_iomalloc_size
);
489 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
492 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
495 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
497 mach_vm_address_t allocationAddress
;
498 mach_vm_size_t adjustedSize
;
499 IOLibPageMallocHeader
* hdr
;
506 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
507 if (adjustedSize
>= page_size
) {
509 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, address
, size
);
511 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
515 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
516 adjustedSize
= hdr
->allocationSize
;
517 allocationAddress
= hdr
->allocationAddress
;
519 if (TRACK_ALLOC
) IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
521 kfree((void *)allocationAddress
, adjustedSize
);
524 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
526 OSAddAtomic(-size
, &debug_iomalloc_size
);
530 #if __arm__ || __arm64__
531 extern unsigned long gPhysBase
, gPhysSize
;
535 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
536 mach_vm_size_t alignment
, bool contiguous
)
539 mach_vm_address_t address
;
540 mach_vm_address_t allocationAddress
;
541 mach_vm_size_t adjustedSize
;
542 mach_vm_address_t alignMask
;
543 IOLibPageMallocHeader
* hdr
;
550 alignMask
= alignment
- 1;
551 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
552 if (adjustedSize
< size
) return (0);
554 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
555 || (alignment
> page_size
);
557 if (contiguous
|| maxPhys
)
563 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
564 || (alignment
> page_size
);
568 #if __arm__ || __arm64__
569 if (maxPhys
>= (mach_vm_address_t
)(gPhysBase
+ gPhysSize
))
575 if (maxPhys
<= 0xFFFFFFFF)
578 options
|= KMA_LOMEM
;
580 else if (gIOLastPage
&& (atop_64(maxPhys
) > gIOLastPage
))
585 if (contiguous
|| maxPhys
)
587 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
588 alignMask
, atop(maxPhys
), atop(alignMask
), 0, IOMemoryTag(kernel_map
));
592 kr
= kernel_memory_allocate(kernel_map
, &virt
,
593 size
, alignMask
, options
, IOMemoryTag(kernel_map
));
595 if (KERN_SUCCESS
== kr
)
599 if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
607 adjustedSize
+= alignMask
;
608 if (adjustedSize
< size
) return (0);
609 allocationAddress
= (mach_vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
611 if (allocationAddress
) {
614 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
617 if (atop_32(address
) != atop_32(address
+ size
- 1))
618 address
= round_page(address
);
620 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
621 hdr
->allocationSize
= adjustedSize
;
622 hdr
->allocationAddress
= allocationAddress
;
625 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
626 hdr
->tracking
.address
= ~address
;
627 hdr
->tracking
.size
= size
;
628 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true, VM_KERN_MEMORY_NONE
);
636 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
638 OSAddAtomic(size
, &debug_iomalloc_size
);
646 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
648 struct _IOMallocContiguousEntry
650 mach_vm_address_t virtualAddr
;
651 IOBufferMemoryDescriptor
* md
;
654 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
656 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
657 IOPhysicalAddress
* physicalAddress
)
659 mach_vm_address_t address
= 0;
666 /* Do we want a physical address? */
667 if (!physicalAddress
)
669 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
673 IOBufferMemoryDescriptor
* bmd
;
674 mach_vm_address_t physicalMask
;
675 vm_offset_t alignMask
;
677 alignMask
= alignment
- 1;
678 physicalMask
= (0xFFFFFFFF ^ alignMask
);
680 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
681 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
685 _IOMallocContiguousEntry
*
686 entry
= IONew(_IOMallocContiguousEntry
, 1);
692 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
694 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
695 queue_enter( &gIOMallocContiguousEntries
, entry
,
696 _IOMallocContiguousEntry
*, link
);
697 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
699 address
= (mach_vm_address_t
) entry
->virtualAddr
;
700 *physicalAddress
= bmd
->getPhysicalAddress();
704 return (void *) address
;
707 void IOFreeContiguous(void * _address
, vm_size_t size
)
709 _IOMallocContiguousEntry
* entry
;
710 IOMemoryDescriptor
* md
= NULL
;
712 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
719 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
720 queue_iterate( &gIOMallocContiguousEntries
, entry
,
721 _IOMallocContiguousEntry
*, link
)
723 if( entry
->virtualAddr
== address
) {
725 queue_remove( &gIOMallocContiguousEntries
, entry
,
726 _IOMallocContiguousEntry
*, link
);
730 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
735 IODelete(entry
, _IOMallocContiguousEntry
, 1);
739 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
743 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
745 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
746 IOIteratePageableMapsCallback callback
, void * ref
)
748 kern_return_t kr
= kIOReturnNotReady
;
755 if (size
> kIOPageableMaxMapSize
)
756 return( kIOReturnBadArgument
);
759 index
= gIOKitPageableSpace
.hint
;
760 attempts
= gIOKitPageableSpace
.count
;
762 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
763 if( KERN_SUCCESS
== kr
) {
764 gIOKitPageableSpace
.hint
= index
;
770 index
= gIOKitPageableSpace
.count
- 1;
772 if (KERN_NO_SPACE
!= kr
)
775 lck_mtx_lock( gIOKitPageableSpace
.lock
);
777 index
= gIOKitPageableSpace
.count
;
778 if( index
>= (kIOMaxPageableMaps
- 1)) {
779 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
783 if( size
< kIOPageableMapSize
)
784 segSize
= kIOPageableMapSize
;
789 kr
= kmem_suballoc(kernel_map
,
794 VM_MAP_KERNEL_FLAGS_NONE
,
795 VM_KERN_MEMORY_IOKIT
,
797 if( KERN_SUCCESS
!= kr
) {
798 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
802 gIOKitPageableSpace
.maps
[index
].map
= map
;
803 gIOKitPageableSpace
.maps
[index
].address
= min
;
804 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
805 gIOKitPageableSpace
.hint
= index
;
806 gIOKitPageableSpace
.count
= index
+ 1;
808 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
815 struct IOMallocPageableRef
822 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
824 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
827 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
, ref
->tag
);
832 static void * IOMallocPageablePages(vm_size_t size
, vm_size_t alignment
, vm_tag_t tag
)
834 kern_return_t kr
= kIOReturnNotReady
;
835 struct IOMallocPageableRef ref
;
837 if (alignment
> page_size
)
839 if (size
> kIOPageableMaxMapSize
)
844 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
845 if( kIOReturnSuccess
!= kr
)
848 return( (void *) ref
.address
);
851 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
856 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
857 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
858 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
859 map
= gIOKitPageableSpace
.maps
[index
].map
;
864 panic("IOPageableMapForAddress: null");
869 static void IOFreePageablePages(void * address
, vm_size_t size
)
873 map
= IOPageableMapForAddress( (vm_address_t
) address
);
875 kmem_free( map
, (vm_offset_t
) address
, size
);
878 static uintptr_t IOMallocOnePageablePage(iopa_t
* a
)
880 return ((uintptr_t) IOMallocPageablePages(page_size
, page_size
, VM_KERN_MEMORY_IOKIT
));
883 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
887 if (size
>= (page_size
- 4*gIOPageAllocChunkBytes
)) addr
= IOMallocPageablePages(size
, alignment
, IOMemoryTag(kernel_map
));
888 else addr
= ((void * ) iopa_alloc(&gIOPageablePageAllocator
, &IOMallocOnePageablePage
, size
, alignment
));
892 OSAddAtomicLong(size
, &debug_iomallocpageable_size
);
894 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
900 void IOFreePageable(void * address
, vm_size_t size
)
903 OSAddAtomicLong(-size
, &debug_iomallocpageable_size
);
905 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
907 if (size
< (page_size
- 4*gIOPageAllocChunkBytes
))
909 address
= (void *) iopa_free(&gIOPageablePageAllocator
, (uintptr_t) address
, size
);
912 if (address
) IOFreePageablePages(address
, size
);
915 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
918 iopa_init(iopa_t
* a
)
920 bzero(a
, sizeof(*a
));
921 a
->lock
= IOLockAlloc();
922 queue_init(&a
->list
);
926 iopa_allocinpage(iopa_page_t
* pa
, uint32_t count
, uint64_t align
)
929 uint64_t avail
= pa
->avail
;
933 // find strings of count 1 bits in avail
934 for (n
= count
; n
> 1; n
-= s
)
937 avail
= avail
& (avail
<< s
);
944 n
= __builtin_clzll(avail
);
945 pa
->avail
&= ~((-1ULL << (64 - count
)) >> n
);
946 if (!pa
->avail
&& pa
->link
.next
)
951 return (n
* gIOPageAllocChunkBytes
+ trunc_page((uintptr_t) pa
));
958 iopa_alloc(iopa_t
* a
, iopa_proc_t alloc
, vm_size_t bytes
, uint32_t balign
)
960 static const uint64_t align_masks
[] = {
974 if (!bytes
) bytes
= 1;
975 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
976 align
= align_masks
[log2up((balign
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
)];
979 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_first(&a
->list
));
980 while (!queue_end(&a
->list
, &pa
->link
))
982 addr
= iopa_allocinpage(pa
, count
, align
);
985 a
->bytecount
+= bytes
;
988 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_next(&pa
->link
));
990 IOLockUnlock(a
->lock
);
997 pa
= (typeof(pa
)) (addr
+ page_size
- gIOPageAllocChunkBytes
);
998 pa
->signature
= kIOPageAllocSignature
;
1001 addr
= iopa_allocinpage(pa
, count
, align
);
1002 IOLockLock(a
->lock
);
1003 if (pa
->avail
) enqueue_head(&a
->list
, &pa
->link
);
1005 if (addr
) a
->bytecount
+= bytes
;
1006 IOLockUnlock(a
->lock
);
1010 assert((addr
& ((1 << log2up(balign
)) - 1)) == 0);
1015 iopa_free(iopa_t
* a
, uintptr_t addr
, vm_size_t bytes
)
1021 if (!bytes
) bytes
= 1;
1023 chunk
= (addr
& page_mask
);
1024 assert(0 == (chunk
& (gIOPageAllocChunkBytes
- 1)));
1026 pa
= (typeof(pa
)) (addr
| (page_size
- gIOPageAllocChunkBytes
));
1027 assert(kIOPageAllocSignature
== pa
->signature
);
1029 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
1030 chunk
/= gIOPageAllocChunkBytes
;
1032 IOLockLock(a
->lock
);
1035 assert(!pa
->link
.next
);
1036 enqueue_tail(&a
->list
, &pa
->link
);
1038 pa
->avail
|= ((-1ULL << (64 - count
)) >> chunk
);
1039 if (pa
->avail
!= -2ULL) pa
= 0;
1047 pa
= (typeof(pa
)) trunc_page(pa
);
1049 a
->bytecount
-= bytes
;
1050 IOLockUnlock(a
->lock
);
1052 return ((uintptr_t) pa
);
1055 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1057 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
1058 IOByteCount length
, IOOptionBits cacheMode
)
1060 IOReturn ret
= kIOReturnSuccess
;
1063 if( task
!= kernel_task
)
1064 return( kIOReturnUnsupported
);
1065 if ((address
| length
) & PAGE_MASK
)
1067 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
1068 return( kIOReturnUnsupported
);
1070 length
= round_page(address
+ length
) - trunc_page( address
);
1071 address
= trunc_page( address
);
1074 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
1076 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
1078 // Get the physical page number
1079 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
1081 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
1082 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
1084 ret
= kIOReturnVMError
;
1086 address
+= page_size
;
1087 length
-= page_size
;
1094 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
1095 IOByteCount length
)
1097 if( task
!= kernel_task
)
1098 return( kIOReturnUnsupported
);
1100 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
1102 return( kIOReturnSuccess
);
1105 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1107 vm_offset_t
OSKernelStackRemaining( void )
1109 return (ml_stack_remaining());
1112 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1115 * Spin for indicated number of milliseconds.
1117 void IOSleep(unsigned milliseconds
)
1119 delay_for_interval(milliseconds
, kMillisecondScale
);
1123 * Spin for indicated number of milliseconds, and potentially an
1124 * additional number of milliseconds up to the leeway values.
1126 void IOSleepWithLeeway(unsigned intervalMilliseconds
, unsigned leewayMilliseconds
)
1128 delay_for_interval_with_leeway(intervalMilliseconds
, leewayMilliseconds
, kMillisecondScale
);
1132 * Spin for indicated number of microseconds.
1134 void IODelay(unsigned microseconds
)
1136 delay_for_interval(microseconds
, kMicrosecondScale
);
1140 * Spin for indicated number of nanoseconds.
1142 void IOPause(unsigned nanoseconds
)
1144 delay_for_interval(nanoseconds
, kNanosecondScale
);
1147 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1149 static void _IOLogv(const char *format
, va_list ap
, void *caller
) __printflike(1,0);
1151 __attribute__((noinline
,not_tail_called
))
1152 void IOLog(const char *format
, ...)
1154 void *caller
= __builtin_return_address(0);
1157 va_start(ap
, format
);
1158 _IOLogv(format
, ap
, caller
);
1162 __attribute__((noinline
,not_tail_called
))
1163 void IOLogv(const char *format
, va_list ap
)
1165 void *caller
= __builtin_return_address(0);
1166 _IOLogv(format
, ap
, caller
);
1169 void _IOLogv(const char *format
, va_list ap
, void *caller
)
1172 struct console_printbuf_state info_data
;
1173 console_printbuf_state_init(&info_data
, TRUE
, TRUE
);
1177 os_log_with_args(OS_LOG_DEFAULT
, OS_LOG_TYPE_DEFAULT
, format
, ap
, caller
);
1179 __doprnt(format
, ap2
, console_printbuf_putc
, &info_data
, 16, TRUE
);
1180 console_printbuf_clear(&info_data
);
1183 assertf(ml_get_interrupts_enabled() || ml_is_quiescing() || debug_mode_active() || !gCPUsRunning
, "IOLog called with interrupts disabled");
1187 void IOPanic(const char *reason
)
1189 panic("%s", reason
);
1193 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1196 * Convert a integer constant (typically a #define or enum) to a string.
1198 static char noValue
[80]; // that's pretty
1200 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
1202 for( ; regValueArray
->name
; regValueArray
++) {
1203 if(regValueArray
->value
== value
)
1204 return(regValueArray
->name
);
1206 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
1207 return((const char *)noValue
);
1210 IOReturn
IOFindValueForName(const char *string
,
1211 const IONamedValue
*regValueArray
,
1214 for( ; regValueArray
->name
; regValueArray
++) {
1215 if(!strcmp(regValueArray
->name
, string
)) {
1216 *value
= regValueArray
->value
;
1217 return kIOReturnSuccess
;
1220 return kIOReturnBadArgument
;
1223 OSString
* IOCopyLogNameForPID(int pid
)
1227 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
1229 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
1230 return (OSString::withCString(buf
));
1233 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1235 IOAlignment
IOSizeToAlignment(unsigned int size
)
1238 const int intsize
= sizeof(unsigned int) * 8;
1240 for (shift
= 1; shift
< intsize
; shift
++) {
1241 if (size
& 0x80000000)
1242 return (IOAlignment
)(intsize
- shift
);
1248 unsigned int IOAlignmentToSize(IOAlignment align
)
1252 for (size
= 1; align
; align
--) {