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>
66 #define IOStatisticsAlloc(type, size) \
68 IOStatistics::countAlloc(type, size); \
73 #define IOStatisticsAlloc(type, size)
75 #endif /* IOKITSTATS */
78 #define TRACK_ALLOC (IOTRACKING && (kIOTracking & gIOKitDebug))
85 mach_timespec_t IOZeroTvalspec
= { 0, 0 };
87 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
93 void (*putc
)(int, void *),
98 extern void cons_putc_locked(char);
99 extern void bsd_log_lock(void);
100 extern void bsd_log_unlock(void);
101 extern void logwakeup();
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
,
194 VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_IOKIT
),
195 &gIOKitPageableSpace
.maps
[0].map
);
196 if (ret
!= KERN_SUCCESS
)
197 panic("failed to allocate iokit pageable map\n");
199 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
200 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
201 gIOKitPageableSpace
.hint
= 0;
202 gIOKitPageableSpace
.count
= 1;
204 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
205 queue_init( &gIOMallocContiguousEntries
);
207 gIOPageAllocChunkBytes
= PAGE_SIZE
/64;
208 assert(sizeof(iopa_page_t
) <= gIOPageAllocChunkBytes
);
209 iopa_init(&gIOBMDPageAllocator
);
210 iopa_init(&gIOPageablePageAllocator
);
213 libInitialized
= true;
216 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
219 log2up(uint32_t size
)
221 if (size
<= 1) size
= 0;
222 else size
= 32 - __builtin_clz(size
- 1);
226 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
228 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
230 kern_return_t result
;
233 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
234 if (result
!= KERN_SUCCESS
)
237 thread_deallocate(thread
);
243 void IOExitThread(void)
245 (void) thread_terminate(current_thread());
248 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
251 struct IOLibMallocHeader
253 IOTrackingAddress tracking
;
258 #define sizeofIOLibMallocHeader (sizeof(IOLibMallocHeader) - (TRACK_ALLOC ? 0 : sizeof(IOTrackingAddress)))
260 #define sizeofIOLibMallocHeader (0)
263 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
265 void * IOMalloc(vm_size_t size
)
270 allocSize
= size
+ sizeofIOLibMallocHeader
;
272 if (sizeofIOLibMallocHeader
&& (allocSize
<= size
)) return (NULL
); // overflow
274 address
= kalloc_tag_bt(allocSize
, VM_KERN_MEMORY_IOKIT
);
279 IOLibMallocHeader
* hdr
;
280 hdr
= (typeof(hdr
)) address
;
281 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
282 hdr
->tracking
.address
= ~(((uintptr_t) address
) + sizeofIOLibMallocHeader
);
283 hdr
->tracking
.size
= size
;
284 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
287 address
= (typeof(address
)) (((uintptr_t) address
) + sizeofIOLibMallocHeader
);
290 OSAddAtomic(size
, &debug_iomalloc_size
);
292 IOStatisticsAlloc(kIOStatisticsMalloc
, size
);
298 void IOFree(void * inAddress
, vm_size_t size
)
302 if ((address
= inAddress
))
304 address
= (typeof(address
)) (((uintptr_t) address
) - sizeofIOLibMallocHeader
);
309 IOLibMallocHeader
* hdr
;
310 struct ptr_reference
{ void * ptr
; };
311 volatile struct ptr_reference ptr
;
313 // we're about to block in IOTrackingRemove(), make sure the original pointer
314 // exists in memory or a register for leak scanning to find
317 hdr
= (typeof(hdr
)) address
;
318 if (size
!= hdr
->tracking
.size
)
320 OSReportWithBacktrace("bad IOFree size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
321 size
= hdr
->tracking
.size
;
323 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
328 kfree(address
, size
+ sizeofIOLibMallocHeader
);
330 OSAddAtomic(-size
, &debug_iomalloc_size
);
332 IOStatisticsAlloc(kIOStatisticsFree
, size
);
336 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
339 IOMemoryTag(vm_map_t map
)
343 if (!vm_kernel_map_is_kernel(map
)) return (VM_MEMORY_IOKIT
);
346 if (tag
== VM_KERN_MEMORY_NONE
) tag
= VM_KERN_MEMORY_IOKIT
;
351 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
353 struct IOLibPageMallocHeader
355 mach_vm_size_t allocationSize
;
356 mach_vm_address_t allocationAddress
;
358 IOTrackingAddress tracking
;
363 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader) - (TRACK_ALLOC ? 0 : sizeof(IOTrackingAddress)))
365 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader))
368 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
370 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
374 vm_offset_t allocationAddress
;
375 vm_size_t adjustedSize
;
377 IOLibPageMallocHeader
* hdr
;
382 alignment
= (1UL << log2up(alignment
));
383 alignMask
= alignment
- 1;
384 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
386 if (size
> adjustedSize
) {
387 address
= 0; /* overflow detected */
389 else if (adjustedSize
>= page_size
) {
391 kr
= kernel_memory_allocate(kernel_map
, &address
,
392 size
, alignMask
, 0, IOMemoryTag(kernel_map
));
393 if (KERN_SUCCESS
!= kr
) address
= 0;
395 else if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
400 adjustedSize
+= alignMask
;
402 if (adjustedSize
>= page_size
) {
404 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
405 adjustedSize
, 0, 0, IOMemoryTag(kernel_map
));
406 if (KERN_SUCCESS
!= kr
) allocationAddress
= 0;
409 allocationAddress
= (vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
411 if (allocationAddress
) {
412 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
415 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
416 hdr
->allocationSize
= adjustedSize
;
417 hdr
->allocationAddress
= allocationAddress
;
420 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
421 hdr
->tracking
.address
= ~address
;
422 hdr
->tracking
.size
= size
;
423 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
430 assert(0 == (address
& alignMask
));
434 OSAddAtomic(size
, &debug_iomalloc_size
);
436 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
439 return (void *) address
;
442 void IOFreeAligned(void * address
, vm_size_t size
)
444 vm_address_t allocationAddress
;
445 vm_size_t adjustedSize
;
446 IOLibPageMallocHeader
* hdr
;
453 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
454 if (adjustedSize
>= page_size
) {
456 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, (uintptr_t) address
, size
);
458 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
461 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
462 adjustedSize
= hdr
->allocationSize
;
463 allocationAddress
= hdr
->allocationAddress
;
468 if (size
!= hdr
->tracking
.size
)
470 OSReportWithBacktrace("bad IOFreeAligned size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
471 size
= hdr
->tracking
.size
;
473 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
476 if (adjustedSize
>= page_size
) {
477 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
479 kfree((void *)allocationAddress
, adjustedSize
);
484 OSAddAtomic(-size
, &debug_iomalloc_size
);
487 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
490 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
493 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
495 mach_vm_address_t allocationAddress
;
496 mach_vm_size_t adjustedSize
;
497 IOLibPageMallocHeader
* hdr
;
504 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
505 if (adjustedSize
>= page_size
) {
507 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, address
, size
);
509 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
513 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
514 adjustedSize
= hdr
->allocationSize
;
515 allocationAddress
= hdr
->allocationAddress
;
517 if (TRACK_ALLOC
) IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
519 kfree((void *)allocationAddress
, adjustedSize
);
522 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
524 OSAddAtomic(-size
, &debug_iomalloc_size
);
530 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
531 mach_vm_size_t alignment
, bool contiguous
)
534 mach_vm_address_t address
;
535 mach_vm_address_t allocationAddress
;
536 mach_vm_size_t adjustedSize
;
537 mach_vm_address_t alignMask
;
538 IOLibPageMallocHeader
* hdr
;
545 alignMask
= alignment
- 1;
546 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
547 if (adjustedSize
< size
) return (0);
549 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
550 || (alignment
> page_size
);
552 if (contiguous
|| maxPhys
)
558 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
559 || (alignment
> page_size
);
563 if (maxPhys
<= 0xFFFFFFFF)
566 options
|= KMA_LOMEM
;
568 else if (gIOLastPage
&& (atop_64(maxPhys
) > gIOLastPage
))
573 if (contiguous
|| maxPhys
)
575 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
576 alignMask
, atop(maxPhys
), atop(alignMask
), 0, IOMemoryTag(kernel_map
));
580 kr
= kernel_memory_allocate(kernel_map
, &virt
,
581 size
, alignMask
, options
, IOMemoryTag(kernel_map
));
583 if (KERN_SUCCESS
== kr
)
587 if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
595 adjustedSize
+= alignMask
;
596 if (adjustedSize
< size
) return (0);
597 allocationAddress
= (mach_vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
599 if (allocationAddress
) {
602 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
605 if (atop_32(address
) != atop_32(address
+ size
- 1))
606 address
= round_page(address
);
608 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
609 hdr
->allocationSize
= adjustedSize
;
610 hdr
->allocationAddress
= allocationAddress
;
613 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
614 hdr
->tracking
.address
= ~address
;
615 hdr
->tracking
.size
= size
;
616 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
624 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
626 OSAddAtomic(size
, &debug_iomalloc_size
);
634 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
636 struct _IOMallocContiguousEntry
638 mach_vm_address_t virtualAddr
;
639 IOBufferMemoryDescriptor
* md
;
642 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
644 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
645 IOPhysicalAddress
* physicalAddress
)
647 mach_vm_address_t address
= 0;
654 /* Do we want a physical address? */
655 if (!physicalAddress
)
657 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
661 IOBufferMemoryDescriptor
* bmd
;
662 mach_vm_address_t physicalMask
;
663 vm_offset_t alignMask
;
665 alignMask
= alignment
- 1;
666 physicalMask
= (0xFFFFFFFF ^ alignMask
);
668 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
669 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
673 _IOMallocContiguousEntry
*
674 entry
= IONew(_IOMallocContiguousEntry
, 1);
680 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
682 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
683 queue_enter( &gIOMallocContiguousEntries
, entry
,
684 _IOMallocContiguousEntry
*, link
);
685 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
687 address
= (mach_vm_address_t
) entry
->virtualAddr
;
688 *physicalAddress
= bmd
->getPhysicalAddress();
692 return (void *) address
;
695 void IOFreeContiguous(void * _address
, vm_size_t size
)
697 _IOMallocContiguousEntry
* entry
;
698 IOMemoryDescriptor
* md
= NULL
;
700 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
707 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
708 queue_iterate( &gIOMallocContiguousEntries
, entry
,
709 _IOMallocContiguousEntry
*, link
)
711 if( entry
->virtualAddr
== address
) {
713 queue_remove( &gIOMallocContiguousEntries
, entry
,
714 _IOMallocContiguousEntry
*, link
);
718 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
723 IODelete(entry
, _IOMallocContiguousEntry
, 1);
727 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
731 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
733 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
734 IOIteratePageableMapsCallback callback
, void * ref
)
736 kern_return_t kr
= kIOReturnNotReady
;
743 if (size
> kIOPageableMaxMapSize
)
744 return( kIOReturnBadArgument
);
747 index
= gIOKitPageableSpace
.hint
;
748 attempts
= gIOKitPageableSpace
.count
;
750 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
751 if( KERN_SUCCESS
== kr
) {
752 gIOKitPageableSpace
.hint
= index
;
758 index
= gIOKitPageableSpace
.count
- 1;
760 if (KERN_NO_SPACE
!= kr
)
763 lck_mtx_lock( gIOKitPageableSpace
.lock
);
765 index
= gIOKitPageableSpace
.count
;
766 if( index
>= (kIOMaxPageableMaps
- 1)) {
767 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
771 if( size
< kIOPageableMapSize
)
772 segSize
= kIOPageableMapSize
;
777 kr
= kmem_suballoc(kernel_map
,
781 VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_IOKIT
),
783 if( KERN_SUCCESS
!= kr
) {
784 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
788 gIOKitPageableSpace
.maps
[index
].map
= map
;
789 gIOKitPageableSpace
.maps
[index
].address
= min
;
790 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
791 gIOKitPageableSpace
.hint
= index
;
792 gIOKitPageableSpace
.count
= index
+ 1;
794 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
801 struct IOMallocPageableRef
808 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
810 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
813 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
, ref
->tag
);
818 static void * IOMallocPageablePages(vm_size_t size
, vm_size_t alignment
, vm_tag_t tag
)
820 kern_return_t kr
= kIOReturnNotReady
;
821 struct IOMallocPageableRef ref
;
823 if (alignment
> page_size
)
825 if (size
> kIOPageableMaxMapSize
)
830 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
831 if( kIOReturnSuccess
!= kr
)
834 return( (void *) ref
.address
);
837 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
842 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
843 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
844 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
845 map
= gIOKitPageableSpace
.maps
[index
].map
;
850 panic("IOPageableMapForAddress: null");
855 static void IOFreePageablePages(void * address
, vm_size_t size
)
859 map
= IOPageableMapForAddress( (vm_address_t
) address
);
861 kmem_free( map
, (vm_offset_t
) address
, size
);
864 static uintptr_t IOMallocOnePageablePage(iopa_t
* a
)
866 return ((uintptr_t) IOMallocPageablePages(page_size
, page_size
, VM_KERN_MEMORY_IOKIT
));
869 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
873 if (size
>= (page_size
- 4*gIOPageAllocChunkBytes
)) addr
= IOMallocPageablePages(size
, alignment
, IOMemoryTag(kernel_map
));
874 else addr
= ((void * ) iopa_alloc(&gIOPageablePageAllocator
, &IOMallocOnePageablePage
, size
, alignment
));
878 OSAddAtomicLong(size
, &debug_iomallocpageable_size
);
880 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
886 void IOFreePageable(void * address
, vm_size_t size
)
889 OSAddAtomicLong(-size
, &debug_iomallocpageable_size
);
891 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
893 if (size
< (page_size
- 4*gIOPageAllocChunkBytes
))
895 address
= (void *) iopa_free(&gIOPageablePageAllocator
, (uintptr_t) address
, size
);
898 if (address
) IOFreePageablePages(address
, size
);
901 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
904 iopa_init(iopa_t
* a
)
906 bzero(a
, sizeof(*a
));
907 a
->lock
= IOLockAlloc();
908 queue_init(&a
->list
);
912 iopa_allocinpage(iopa_page_t
* pa
, uint32_t count
, uint64_t align
)
915 uint64_t avail
= pa
->avail
;
919 // find strings of count 1 bits in avail
920 for (n
= count
; n
> 1; n
-= s
)
923 avail
= avail
& (avail
<< s
);
930 n
= __builtin_clzll(avail
);
931 pa
->avail
&= ~((-1ULL << (64 - count
)) >> n
);
932 if (!pa
->avail
&& pa
->link
.next
)
937 return (n
* gIOPageAllocChunkBytes
+ trunc_page((uintptr_t) pa
));
944 iopa_alloc(iopa_t
* a
, iopa_proc_t alloc
, vm_size_t bytes
, uint32_t balign
)
946 static const uint64_t align_masks
[] = {
960 if (!bytes
) bytes
= 1;
961 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
962 align
= align_masks
[log2up((balign
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
)];
965 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_first(&a
->list
));
966 while (!queue_end(&a
->list
, &pa
->link
))
968 addr
= iopa_allocinpage(pa
, count
, align
);
971 a
->bytecount
+= bytes
;
974 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_next(&pa
->link
));
976 IOLockUnlock(a
->lock
);
983 pa
= (typeof(pa
)) (addr
+ page_size
- gIOPageAllocChunkBytes
);
984 pa
->signature
= kIOPageAllocSignature
;
987 addr
= iopa_allocinpage(pa
, count
, align
);
989 if (pa
->avail
) enqueue_head(&a
->list
, &pa
->link
);
991 if (addr
) a
->bytecount
+= bytes
;
992 IOLockUnlock(a
->lock
);
996 assert((addr
& ((1 << log2up(balign
)) - 1)) == 0);
1001 iopa_free(iopa_t
* a
, uintptr_t addr
, vm_size_t bytes
)
1007 if (!bytes
) bytes
= 1;
1009 chunk
= (addr
& page_mask
);
1010 assert(0 == (chunk
& (gIOPageAllocChunkBytes
- 1)));
1012 pa
= (typeof(pa
)) (addr
| (page_size
- gIOPageAllocChunkBytes
));
1013 assert(kIOPageAllocSignature
== pa
->signature
);
1015 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
1016 chunk
/= gIOPageAllocChunkBytes
;
1018 IOLockLock(a
->lock
);
1021 assert(!pa
->link
.next
);
1022 enqueue_tail(&a
->list
, &pa
->link
);
1024 pa
->avail
|= ((-1ULL << (64 - count
)) >> chunk
);
1025 if (pa
->avail
!= -2ULL) pa
= 0;
1033 pa
= (typeof(pa
)) trunc_page(pa
);
1035 a
->bytecount
-= bytes
;
1036 IOLockUnlock(a
->lock
);
1038 return ((uintptr_t) pa
);
1041 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1043 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
1044 IOByteCount length
, IOOptionBits cacheMode
)
1046 IOReturn ret
= kIOReturnSuccess
;
1049 if( task
!= kernel_task
)
1050 return( kIOReturnUnsupported
);
1051 if ((address
| length
) & PAGE_MASK
)
1053 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
1054 return( kIOReturnUnsupported
);
1056 length
= round_page(address
+ length
) - trunc_page( address
);
1057 address
= trunc_page( address
);
1060 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
1062 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
1064 // Get the physical page number
1065 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
1067 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
1068 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
1070 ret
= kIOReturnVMError
;
1072 address
+= page_size
;
1073 length
-= page_size
;
1080 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
1081 IOByteCount length
)
1083 if( task
!= kernel_task
)
1084 return( kIOReturnUnsupported
);
1086 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
1088 return( kIOReturnSuccess
);
1091 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1093 vm_offset_t
OSKernelStackRemaining( void )
1095 return (ml_stack_remaining());
1098 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1101 * Spin for indicated number of milliseconds.
1103 void IOSleep(unsigned milliseconds
)
1105 delay_for_interval(milliseconds
, kMillisecondScale
);
1109 * Spin for indicated number of milliseconds, and potentially an
1110 * additional number of milliseconds up to the leeway values.
1112 void IOSleepWithLeeway(unsigned intervalMilliseconds
, unsigned leewayMilliseconds
)
1114 delay_for_interval_with_leeway(intervalMilliseconds
, leewayMilliseconds
, kMillisecondScale
);
1118 * Spin for indicated number of microseconds.
1120 void IODelay(unsigned microseconds
)
1122 delay_for_interval(microseconds
, kMicrosecondScale
);
1126 * Spin for indicated number of nanoseconds.
1128 void IOPause(unsigned nanoseconds
)
1130 delay_for_interval(nanoseconds
, kNanosecondScale
);
1133 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1135 static void _iolog_consputc(int ch
, void *arg __unused
)
1137 cons_putc_locked(ch
);
1140 static void _IOLogv(const char *format
, va_list ap
, void *caller
);
1142 __attribute__((noinline
,not_tail_called
))
1143 void IOLog(const char *format
, ...)
1145 void *caller
= __builtin_return_address(0);
1148 va_start(ap
, format
);
1149 _IOLogv(format
, ap
, caller
);
1153 __attribute__((noinline
,not_tail_called
))
1154 void IOLogv(const char *format
, va_list ap
)
1156 void *caller
= __builtin_return_address(0);
1157 _IOLogv(format
, ap
, caller
);
1160 void _IOLogv(const char *format
, va_list ap
, void *caller
)
1164 /* Ideally not called at interrupt context or with interrupts disabled. Needs further validate */
1165 /* assert(TRUE == ml_get_interrupts_enabled()); */
1169 os_log_with_args(OS_LOG_DEFAULT
, OS_LOG_TYPE_DEFAULT
, format
, ap
, caller
);
1171 __doprnt(format
, ap2
, _iolog_consputc
, NULL
, 16, TRUE
);
1176 void IOPanic(const char *reason
)
1178 panic("%s", reason
);
1182 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1185 * Convert a integer constant (typically a #define or enum) to a string.
1187 static char noValue
[80]; // that's pretty
1189 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
1191 for( ; regValueArray
->name
; regValueArray
++) {
1192 if(regValueArray
->value
== value
)
1193 return(regValueArray
->name
);
1195 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
1196 return((const char *)noValue
);
1199 IOReturn
IOFindValueForName(const char *string
,
1200 const IONamedValue
*regValueArray
,
1203 for( ; regValueArray
->name
; regValueArray
++) {
1204 if(!strcmp(regValueArray
->name
, string
)) {
1205 *value
= regValueArray
->value
;
1206 return kIOReturnSuccess
;
1209 return kIOReturnBadArgument
;
1212 OSString
* IOCopyLogNameForPID(int pid
)
1216 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
1218 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
1219 return (OSString::withCString(buf
));
1222 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1224 IOAlignment
IOSizeToAlignment(unsigned int size
)
1227 const int intsize
= sizeof(unsigned int) * 8;
1229 for (shift
= 1; shift
< intsize
; shift
++) {
1230 if (size
& 0x80000000)
1231 return (IOAlignment
)(intsize
- shift
);
1237 unsigned int IOAlignmentToSize(IOAlignment align
)
1241 for (size
= 1; align
; align
--) {