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 * address
, vm_size_t size
)
302 address
= (typeof(address
)) (((uintptr_t) address
) - sizeofIOLibMallocHeader
);
306 IOLibMallocHeader
* hdr
;
307 hdr
= (typeof(hdr
)) address
;
308 if (size
!= hdr
->tracking
.size
)
310 OSReportWithBacktrace("bad IOFree size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
311 size
= hdr
->tracking
.size
;
313 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
317 kfree(address
, size
+ sizeofIOLibMallocHeader
);
319 OSAddAtomic(-size
, &debug_iomalloc_size
);
321 IOStatisticsAlloc(kIOStatisticsFree
, size
);
325 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
328 IOMemoryTag(vm_map_t map
)
332 if (!vm_kernel_map_is_kernel(map
)) return (VM_MEMORY_IOKIT
);
335 if (tag
== VM_KERN_MEMORY_NONE
) tag
= VM_KERN_MEMORY_IOKIT
;
340 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
342 struct IOLibPageMallocHeader
344 mach_vm_size_t allocationSize
;
345 mach_vm_address_t allocationAddress
;
347 IOTrackingAddress tracking
;
352 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader) - (TRACK_ALLOC ? 0 : sizeof(IOTrackingAddress)))
354 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader))
357 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
359 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
363 vm_offset_t allocationAddress
;
364 vm_size_t adjustedSize
;
366 IOLibPageMallocHeader
* hdr
;
371 alignment
= (1UL << log2up(alignment
));
372 alignMask
= alignment
- 1;
373 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
375 if (size
> adjustedSize
) {
376 address
= 0; /* overflow detected */
378 else if (adjustedSize
>= page_size
) {
380 kr
= kernel_memory_allocate(kernel_map
, &address
,
381 size
, alignMask
, 0, IOMemoryTag(kernel_map
));
382 if (KERN_SUCCESS
!= kr
) address
= 0;
384 else if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
389 adjustedSize
+= alignMask
;
391 if (adjustedSize
>= page_size
) {
393 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
394 adjustedSize
, 0, 0, IOMemoryTag(kernel_map
));
395 if (KERN_SUCCESS
!= kr
) allocationAddress
= 0;
398 allocationAddress
= (vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
400 if (allocationAddress
) {
401 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
404 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
405 hdr
->allocationSize
= adjustedSize
;
406 hdr
->allocationAddress
= allocationAddress
;
409 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
410 hdr
->tracking
.address
= ~address
;
411 hdr
->tracking
.size
= size
;
412 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
419 assert(0 == (address
& alignMask
));
423 OSAddAtomic(size
, &debug_iomalloc_size
);
425 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
428 return (void *) address
;
431 void IOFreeAligned(void * address
, vm_size_t size
)
433 vm_address_t allocationAddress
;
434 vm_size_t adjustedSize
;
435 IOLibPageMallocHeader
* hdr
;
442 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
443 if (adjustedSize
>= page_size
) {
445 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, (uintptr_t) address
, size
);
447 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
450 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
451 adjustedSize
= hdr
->allocationSize
;
452 allocationAddress
= hdr
->allocationAddress
;
457 if (size
!= hdr
->tracking
.size
)
459 OSReportWithBacktrace("bad IOFreeAligned size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
460 size
= hdr
->tracking
.size
;
462 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
465 if (adjustedSize
>= page_size
) {
466 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
468 kfree((void *)allocationAddress
, adjustedSize
);
473 OSAddAtomic(-size
, &debug_iomalloc_size
);
476 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
479 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
482 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
484 mach_vm_address_t allocationAddress
;
485 mach_vm_size_t adjustedSize
;
486 IOLibPageMallocHeader
* hdr
;
493 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
494 if (adjustedSize
>= page_size
) {
496 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, address
, size
);
498 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
502 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
503 adjustedSize
= hdr
->allocationSize
;
504 allocationAddress
= hdr
->allocationAddress
;
506 if (TRACK_ALLOC
) IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
508 kfree((void *)allocationAddress
, adjustedSize
);
511 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
513 OSAddAtomic(-size
, &debug_iomalloc_size
);
519 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
520 mach_vm_size_t alignment
, bool contiguous
)
523 mach_vm_address_t address
;
524 mach_vm_address_t allocationAddress
;
525 mach_vm_size_t adjustedSize
;
526 mach_vm_address_t alignMask
;
527 IOLibPageMallocHeader
* hdr
;
534 alignMask
= alignment
- 1;
535 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
536 if (adjustedSize
< size
) return (0);
538 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
539 || (alignment
> page_size
);
541 if (contiguous
|| maxPhys
)
547 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
548 || (alignment
> page_size
);
552 if (maxPhys
<= 0xFFFFFFFF)
555 options
|= KMA_LOMEM
;
557 else if (gIOLastPage
&& (atop_64(maxPhys
) > gIOLastPage
))
562 if (contiguous
|| maxPhys
)
564 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
565 alignMask
, atop(maxPhys
), atop(alignMask
), 0, IOMemoryTag(kernel_map
));
569 kr
= kernel_memory_allocate(kernel_map
, &virt
,
570 size
, alignMask
, options
, IOMemoryTag(kernel_map
));
572 if (KERN_SUCCESS
== kr
)
576 if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
584 adjustedSize
+= alignMask
;
585 if (adjustedSize
< size
) return (0);
586 allocationAddress
= (mach_vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
588 if (allocationAddress
) {
591 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
594 if (atop_32(address
) != atop_32(address
+ size
- 1))
595 address
= round_page(address
);
597 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
598 hdr
->allocationSize
= adjustedSize
;
599 hdr
->allocationAddress
= allocationAddress
;
602 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
603 hdr
->tracking
.address
= ~address
;
604 hdr
->tracking
.size
= size
;
605 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
613 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
615 OSAddAtomic(size
, &debug_iomalloc_size
);
623 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
625 struct _IOMallocContiguousEntry
627 mach_vm_address_t virtualAddr
;
628 IOBufferMemoryDescriptor
* md
;
631 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
633 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
634 IOPhysicalAddress
* physicalAddress
)
636 mach_vm_address_t address
= 0;
643 /* Do we want a physical address? */
644 if (!physicalAddress
)
646 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
650 IOBufferMemoryDescriptor
* bmd
;
651 mach_vm_address_t physicalMask
;
652 vm_offset_t alignMask
;
654 alignMask
= alignment
- 1;
655 physicalMask
= (0xFFFFFFFF ^ alignMask
);
657 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
658 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
662 _IOMallocContiguousEntry
*
663 entry
= IONew(_IOMallocContiguousEntry
, 1);
669 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
671 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
672 queue_enter( &gIOMallocContiguousEntries
, entry
,
673 _IOMallocContiguousEntry
*, link
);
674 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
676 address
= (mach_vm_address_t
) entry
->virtualAddr
;
677 *physicalAddress
= bmd
->getPhysicalAddress();
681 return (void *) address
;
684 void IOFreeContiguous(void * _address
, vm_size_t size
)
686 _IOMallocContiguousEntry
* entry
;
687 IOMemoryDescriptor
* md
= NULL
;
689 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
696 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
697 queue_iterate( &gIOMallocContiguousEntries
, entry
,
698 _IOMallocContiguousEntry
*, link
)
700 if( entry
->virtualAddr
== address
) {
702 queue_remove( &gIOMallocContiguousEntries
, entry
,
703 _IOMallocContiguousEntry
*, link
);
707 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
712 IODelete(entry
, _IOMallocContiguousEntry
, 1);
716 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
720 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
722 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
723 IOIteratePageableMapsCallback callback
, void * ref
)
725 kern_return_t kr
= kIOReturnNotReady
;
732 if (size
> kIOPageableMaxMapSize
)
733 return( kIOReturnBadArgument
);
736 index
= gIOKitPageableSpace
.hint
;
737 attempts
= gIOKitPageableSpace
.count
;
739 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
740 if( KERN_SUCCESS
== kr
) {
741 gIOKitPageableSpace
.hint
= index
;
747 index
= gIOKitPageableSpace
.count
- 1;
749 if( KERN_SUCCESS
== kr
)
752 lck_mtx_lock( gIOKitPageableSpace
.lock
);
754 index
= gIOKitPageableSpace
.count
;
755 if( index
>= (kIOMaxPageableMaps
- 1)) {
756 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
760 if( size
< kIOPageableMapSize
)
761 segSize
= kIOPageableMapSize
;
766 kr
= kmem_suballoc(kernel_map
,
770 VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_IOKIT
),
772 if( KERN_SUCCESS
!= kr
) {
773 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
777 gIOKitPageableSpace
.maps
[index
].map
= map
;
778 gIOKitPageableSpace
.maps
[index
].address
= min
;
779 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
780 gIOKitPageableSpace
.hint
= index
;
781 gIOKitPageableSpace
.count
= index
+ 1;
783 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
790 struct IOMallocPageableRef
797 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
799 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
802 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
, ref
->tag
);
807 static void * IOMallocPageablePages(vm_size_t size
, vm_size_t alignment
, vm_tag_t tag
)
809 kern_return_t kr
= kIOReturnNotReady
;
810 struct IOMallocPageableRef ref
;
812 if (alignment
> page_size
)
814 if (size
> kIOPageableMaxMapSize
)
819 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
820 if( kIOReturnSuccess
!= kr
)
823 return( (void *) ref
.address
);
826 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
831 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
832 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
833 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
834 map
= gIOKitPageableSpace
.maps
[index
].map
;
839 panic("IOPageableMapForAddress: null");
844 static void IOFreePageablePages(void * address
, vm_size_t size
)
848 map
= IOPageableMapForAddress( (vm_address_t
) address
);
850 kmem_free( map
, (vm_offset_t
) address
, size
);
853 static uintptr_t IOMallocOnePageablePage(iopa_t
* a
)
855 return ((uintptr_t) IOMallocPageablePages(page_size
, page_size
, VM_KERN_MEMORY_IOKIT
));
858 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
862 if (size
>= (page_size
- 4*gIOPageAllocChunkBytes
)) addr
= IOMallocPageablePages(size
, alignment
, IOMemoryTag(kernel_map
));
863 else addr
= ((void * ) iopa_alloc(&gIOPageablePageAllocator
, &IOMallocOnePageablePage
, size
, alignment
));
867 OSAddAtomicLong(size
, &debug_iomallocpageable_size
);
869 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
875 void IOFreePageable(void * address
, vm_size_t size
)
878 OSAddAtomicLong(-size
, &debug_iomallocpageable_size
);
880 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
882 if (size
< (page_size
- 4*gIOPageAllocChunkBytes
))
884 address
= (void *) iopa_free(&gIOPageablePageAllocator
, (uintptr_t) address
, size
);
887 if (address
) IOFreePageablePages(address
, size
);
890 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
893 iopa_init(iopa_t
* a
)
895 bzero(a
, sizeof(*a
));
896 a
->lock
= IOLockAlloc();
897 queue_init(&a
->list
);
901 iopa_allocinpage(iopa_page_t
* pa
, uint32_t count
, uint64_t align
)
904 uint64_t avail
= pa
->avail
;
908 // find strings of count 1 bits in avail
909 for (n
= count
; n
> 1; n
-= s
)
912 avail
= avail
& (avail
<< s
);
919 n
= __builtin_clzll(avail
);
920 pa
->avail
&= ~((-1ULL << (64 - count
)) >> n
);
921 if (!pa
->avail
&& pa
->link
.next
)
926 return (n
* gIOPageAllocChunkBytes
+ trunc_page((uintptr_t) pa
));
933 iopa_alloc(iopa_t
* a
, iopa_proc_t alloc
, vm_size_t bytes
, uint32_t balign
)
935 static const uint64_t align_masks
[] = {
949 if (!bytes
) bytes
= 1;
950 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
951 align
= align_masks
[log2up((balign
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
)];
954 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_first(&a
->list
));
955 while (!queue_end(&a
->list
, &pa
->link
))
957 addr
= iopa_allocinpage(pa
, count
, align
);
960 a
->bytecount
+= bytes
;
963 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_next(&pa
->link
));
965 IOLockUnlock(a
->lock
);
972 pa
= (typeof(pa
)) (addr
+ page_size
- gIOPageAllocChunkBytes
);
973 pa
->signature
= kIOPageAllocSignature
;
976 addr
= iopa_allocinpage(pa
, count
, align
);
978 if (pa
->avail
) enqueue_head(&a
->list
, &pa
->link
);
980 if (addr
) a
->bytecount
+= bytes
;
981 IOLockUnlock(a
->lock
);
985 assert((addr
& ((1 << log2up(balign
)) - 1)) == 0);
990 iopa_free(iopa_t
* a
, uintptr_t addr
, vm_size_t bytes
)
996 if (!bytes
) bytes
= 1;
998 chunk
= (addr
& page_mask
);
999 assert(0 == (chunk
& (gIOPageAllocChunkBytes
- 1)));
1001 pa
= (typeof(pa
)) (addr
| (page_size
- gIOPageAllocChunkBytes
));
1002 assert(kIOPageAllocSignature
== pa
->signature
);
1004 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
1005 chunk
/= gIOPageAllocChunkBytes
;
1007 IOLockLock(a
->lock
);
1010 assert(!pa
->link
.next
);
1011 enqueue_tail(&a
->list
, &pa
->link
);
1013 pa
->avail
|= ((-1ULL << (64 - count
)) >> chunk
);
1014 if (pa
->avail
!= -2ULL) pa
= 0;
1022 pa
= (typeof(pa
)) trunc_page(pa
);
1024 a
->bytecount
-= bytes
;
1025 IOLockUnlock(a
->lock
);
1027 return ((uintptr_t) pa
);
1030 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1032 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
1033 IOByteCount length
, IOOptionBits cacheMode
)
1035 IOReturn ret
= kIOReturnSuccess
;
1038 if( task
!= kernel_task
)
1039 return( kIOReturnUnsupported
);
1040 if ((address
| length
) & PAGE_MASK
)
1042 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
1043 return( kIOReturnUnsupported
);
1045 length
= round_page(address
+ length
) - trunc_page( address
);
1046 address
= trunc_page( address
);
1049 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
1051 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
1053 // Get the physical page number
1054 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
1056 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
1057 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
1059 ret
= kIOReturnVMError
;
1061 address
+= page_size
;
1062 length
-= page_size
;
1069 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
1070 IOByteCount length
)
1072 if( task
!= kernel_task
)
1073 return( kIOReturnUnsupported
);
1075 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
1077 return( kIOReturnSuccess
);
1080 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1082 vm_offset_t
OSKernelStackRemaining( void )
1084 return (ml_stack_remaining());
1087 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1090 * Spin for indicated number of milliseconds.
1092 void IOSleep(unsigned milliseconds
)
1094 delay_for_interval(milliseconds
, kMillisecondScale
);
1098 * Spin for indicated number of milliseconds, and potentially an
1099 * additional number of milliseconds up to the leeway values.
1101 void IOSleepWithLeeway(unsigned intervalMilliseconds
, unsigned leewayMilliseconds
)
1103 delay_for_interval_with_leeway(intervalMilliseconds
, leewayMilliseconds
, kMillisecondScale
);
1107 * Spin for indicated number of microseconds.
1109 void IODelay(unsigned microseconds
)
1111 delay_for_interval(microseconds
, kMicrosecondScale
);
1115 * Spin for indicated number of nanoseconds.
1117 void IOPause(unsigned nanoseconds
)
1119 delay_for_interval(nanoseconds
, kNanosecondScale
);
1122 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1124 static void _iolog_consputc(int ch
, void *arg __unused
)
1126 cons_putc_locked(ch
);
1129 static void _IOLogv(const char *format
, va_list ap
, void *caller
);
1131 __attribute__((noinline
,not_tail_called
))
1132 void IOLog(const char *format
, ...)
1134 void *caller
= __builtin_return_address(0);
1137 va_start(ap
, format
);
1138 _IOLogv(format
, ap
, caller
);
1142 __attribute__((noinline
,not_tail_called
))
1143 void IOLogv(const char *format
, va_list ap
)
1145 void *caller
= __builtin_return_address(0);
1146 _IOLogv(format
, ap
, caller
);
1149 void _IOLogv(const char *format
, va_list ap
, void *caller
)
1153 /* Ideally not called at interrupt context or with interrupts disabled. Needs further validate */
1154 /* assert(TRUE == ml_get_interrupts_enabled()); */
1158 os_log_with_args(OS_LOG_DEFAULT
, OS_LOG_TYPE_DEFAULT
, format
, ap
, caller
);
1160 __doprnt(format
, ap2
, _iolog_consputc
, NULL
, 16, TRUE
);
1165 void IOPanic(const char *reason
)
1167 panic("%s", reason
);
1171 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1174 * Convert a integer constant (typically a #define or enum) to a string.
1176 static char noValue
[80]; // that's pretty
1178 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
1180 for( ; regValueArray
->name
; regValueArray
++) {
1181 if(regValueArray
->value
== value
)
1182 return(regValueArray
->name
);
1184 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
1185 return((const char *)noValue
);
1188 IOReturn
IOFindValueForName(const char *string
,
1189 const IONamedValue
*regValueArray
,
1192 for( ; regValueArray
->name
; regValueArray
++) {
1193 if(!strcmp(regValueArray
->name
, string
)) {
1194 *value
= regValueArray
->value
;
1195 return kIOReturnSuccess
;
1198 return kIOReturnBadArgument
;
1201 OSString
* IOCopyLogNameForPID(int pid
)
1205 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
1207 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
1208 return (OSString::withCString(buf
));
1211 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1213 IOAlignment
IOSizeToAlignment(unsigned int size
)
1216 const int intsize
= sizeof(unsigned int) * 8;
1218 for (shift
= 1; shift
< intsize
; shift
++) {
1219 if (size
& 0x80000000)
1220 return (IOAlignment
)(intsize
- shift
);
1226 unsigned int IOAlignmentToSize(IOAlignment align
)
1230 for (size
= 1; align
; align
--) {