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 */
77 #define TRACK_ALLOC (IOTRACKING && (kIOTracking & gIOKitDebug))
84 mach_timespec_t IOZeroTvalspec
= { 0, 0 };
86 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
92 void (*putc
)(int, void *),
97 extern void cons_putc_locked(char);
98 extern void bsd_log_lock(void);
99 extern void bsd_log_unlock(void);
100 extern void logwakeup();
103 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
105 lck_grp_t
*IOLockGroup
;
108 * Global variables for use by iLogger
109 * These symbols are for use only by Apple diagnostic code.
110 * Binary compatibility is not guaranteed for kexts that reference these symbols.
113 void *_giDebugLogInternal
= NULL
;
114 void *_giDebugLogDataInternal
= NULL
;
115 void *_giDebugReserved1
= NULL
;
116 void *_giDebugReserved2
= NULL
;
118 iopa_t gIOBMDPageAllocator
;
121 * Static variables for this module.
124 static queue_head_t gIOMallocContiguousEntries
;
125 static lck_mtx_t
* gIOMallocContiguousEntriesLock
;
128 enum { kIOMaxPageableMaps
= 8 };
129 enum { kIOPageableMapSize
= 512 * 1024 * 1024 };
130 enum { kIOPageableMaxMapSize
= 512 * 1024 * 1024 };
132 enum { kIOMaxPageableMaps
= 16 };
133 enum { kIOPageableMapSize
= 96 * 1024 * 1024 };
134 enum { kIOPageableMaxMapSize
= 96 * 1024 * 1024 };
146 IOMapData maps
[ kIOMaxPageableMaps
];
148 } gIOKitPageableSpace
;
150 static iopa_t gIOPageablePageAllocator
;
152 uint32_t gIOPageAllocChunkBytes
;
155 IOTrackingQueue
* gIOMallocTracking
;
156 IOTrackingQueue
* gIOWireTracking
;
157 IOTrackingQueue
* gIOMapTracking
;
158 #endif /* IOTRACKING */
160 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
166 static bool libInitialized
;
171 IOLockGroup
= lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL
);
175 gIOMallocTracking
= IOTrackingQueueAlloc(kIOMallocTrackingName
, 0, 0, true);
176 gIOWireTracking
= IOTrackingQueueAlloc(kIOWireTrackingName
, 0, page_size
, false);
177 gIOMapTracking
= IOTrackingQueueAlloc(kIOMapTrackingName
, 0, page_size
, false);
180 gIOKitPageableSpace
.maps
[0].address
= 0;
181 ret
= kmem_suballoc(kernel_map
,
182 &gIOKitPageableSpace
.maps
[0].address
,
185 VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_IOKIT
),
186 &gIOKitPageableSpace
.maps
[0].map
);
187 if (ret
!= KERN_SUCCESS
)
188 panic("failed to allocate iokit pageable map\n");
190 gIOKitPageableSpace
.lock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
191 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
192 gIOKitPageableSpace
.hint
= 0;
193 gIOKitPageableSpace
.count
= 1;
195 gIOMallocContiguousEntriesLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
196 queue_init( &gIOMallocContiguousEntries
);
198 gIOPageAllocChunkBytes
= PAGE_SIZE
/64;
199 assert(sizeof(iopa_page_t
) <= gIOPageAllocChunkBytes
);
200 iopa_init(&gIOBMDPageAllocator
);
201 iopa_init(&gIOPageablePageAllocator
);
204 libInitialized
= true;
207 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
210 log2up(uint32_t size
)
212 if (size
<= 1) size
= 0;
213 else size
= 32 - __builtin_clz(size
- 1);
217 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
219 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
221 kern_return_t result
;
224 result
= kernel_thread_start((thread_continue_t
)fcn
, arg
, &thread
);
225 if (result
!= KERN_SUCCESS
)
228 thread_deallocate(thread
);
234 void IOExitThread(void)
236 (void) thread_terminate(current_thread());
239 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
242 struct IOLibMallocHeader
244 IOTrackingAddress tracking
;
249 #define sizeofIOLibMallocHeader (sizeof(IOLibMallocHeader) - (TRACK_ALLOC ? 0 : sizeof(IOTrackingAddress)))
251 #define sizeofIOLibMallocHeader (0)
254 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
256 void * IOMalloc(vm_size_t size
)
261 allocSize
= size
+ sizeofIOLibMallocHeader
;
263 if (sizeofIOLibMallocHeader
&& (allocSize
<= size
)) return (NULL
); // overflow
265 address
= kalloc_tag_bt(allocSize
, VM_KERN_MEMORY_IOKIT
);
270 IOLibMallocHeader
* hdr
;
271 hdr
= (typeof(hdr
)) address
;
272 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
273 hdr
->tracking
.address
= ~(((uintptr_t) address
) + sizeofIOLibMallocHeader
);
274 hdr
->tracking
.size
= size
;
275 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
278 address
= (typeof(address
)) (((uintptr_t) address
) + sizeofIOLibMallocHeader
);
281 OSAddAtomic(size
, &debug_iomalloc_size
);
283 IOStatisticsAlloc(kIOStatisticsMalloc
, size
);
289 void IOFree(void * address
, vm_size_t size
)
293 address
= (typeof(address
)) (((uintptr_t) address
) - sizeofIOLibMallocHeader
);
297 IOLibMallocHeader
* hdr
;
298 hdr
= (typeof(hdr
)) address
;
299 if (size
!= hdr
->tracking
.size
)
301 OSReportWithBacktrace("bad IOFree size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
302 size
= hdr
->tracking
.size
;
304 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
308 kfree(address
, size
+ sizeofIOLibMallocHeader
);
310 OSAddAtomic(-size
, &debug_iomalloc_size
);
312 IOStatisticsAlloc(kIOStatisticsFree
, size
);
316 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
319 IOMemoryTag(vm_map_t map
)
323 if (!vm_kernel_map_is_kernel(map
)) return (VM_MEMORY_IOKIT
);
326 if (tag
== VM_KERN_MEMORY_NONE
) tag
= VM_KERN_MEMORY_IOKIT
;
331 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
333 struct IOLibPageMallocHeader
335 mach_vm_size_t allocationSize
;
336 mach_vm_address_t allocationAddress
;
338 IOTrackingAddress tracking
;
343 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader) - (TRACK_ALLOC ? 0 : sizeof(IOTrackingAddress)))
345 #define sizeofIOLibPageMallocHeader (sizeof(IOLibPageMallocHeader))
348 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
350 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
354 vm_offset_t allocationAddress
;
355 vm_size_t adjustedSize
;
357 IOLibPageMallocHeader
* hdr
;
362 alignment
= (1UL << log2up(alignment
));
363 alignMask
= alignment
- 1;
364 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
366 if (size
> adjustedSize
) {
367 address
= 0; /* overflow detected */
369 else if (adjustedSize
>= page_size
) {
371 kr
= kernel_memory_allocate(kernel_map
, &address
,
372 size
, alignMask
, 0, IOMemoryTag(kernel_map
));
373 if (KERN_SUCCESS
!= kr
) address
= 0;
375 else if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
380 adjustedSize
+= alignMask
;
382 if (adjustedSize
>= page_size
) {
384 kr
= kernel_memory_allocate(kernel_map
, &allocationAddress
,
385 adjustedSize
, 0, 0, IOMemoryTag(kernel_map
));
386 if (KERN_SUCCESS
!= kr
) allocationAddress
= 0;
389 allocationAddress
= (vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
391 if (allocationAddress
) {
392 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
395 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
396 hdr
->allocationSize
= adjustedSize
;
397 hdr
->allocationAddress
= allocationAddress
;
400 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
401 hdr
->tracking
.address
= ~address
;
402 hdr
->tracking
.size
= size
;
403 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
410 assert(0 == (address
& alignMask
));
414 OSAddAtomic(size
, &debug_iomalloc_size
);
416 IOStatisticsAlloc(kIOStatisticsMallocAligned
, size
);
419 return (void *) address
;
422 void IOFreeAligned(void * address
, vm_size_t size
)
424 vm_address_t allocationAddress
;
425 vm_size_t adjustedSize
;
426 IOLibPageMallocHeader
* hdr
;
433 adjustedSize
= size
+ sizeofIOLibPageMallocHeader
;
434 if (adjustedSize
>= page_size
) {
436 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, (uintptr_t) address
, size
);
438 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
441 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
442 adjustedSize
= hdr
->allocationSize
;
443 allocationAddress
= hdr
->allocationAddress
;
448 if (size
!= hdr
->tracking
.size
)
450 OSReportWithBacktrace("bad IOFreeAligned size 0x%lx should be 0x%lx", size
, hdr
->tracking
.size
);
451 size
= hdr
->tracking
.size
;
453 IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
456 if (adjustedSize
>= page_size
) {
457 kmem_free( kernel_map
, allocationAddress
, adjustedSize
);
459 kfree((void *)allocationAddress
, adjustedSize
);
464 OSAddAtomic(-size
, &debug_iomalloc_size
);
467 IOStatisticsAlloc(kIOStatisticsFreeAligned
, size
);
470 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
473 IOKernelFreePhysical(mach_vm_address_t address
, mach_vm_size_t size
)
475 mach_vm_address_t allocationAddress
;
476 mach_vm_size_t adjustedSize
;
477 IOLibPageMallocHeader
* hdr
;
484 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
485 if (adjustedSize
>= page_size
) {
487 if (TRACK_ALLOC
) IOTrackingFree(gIOMallocTracking
, address
, size
);
489 kmem_free( kernel_map
, (vm_offset_t
) address
, size
);
493 hdr
= (typeof(hdr
)) (((uintptr_t)address
) - sizeofIOLibPageMallocHeader
);
494 adjustedSize
= hdr
->allocationSize
;
495 allocationAddress
= hdr
->allocationAddress
;
497 if (TRACK_ALLOC
) IOTrackingRemove(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
);
499 kfree((void *)allocationAddress
, adjustedSize
);
502 IOStatisticsAlloc(kIOStatisticsFreeContiguous
, size
);
504 OSAddAtomic(-size
, &debug_iomalloc_size
);
510 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size
, mach_vm_address_t maxPhys
,
511 mach_vm_size_t alignment
, bool contiguous
)
514 mach_vm_address_t address
;
515 mach_vm_address_t allocationAddress
;
516 mach_vm_size_t adjustedSize
;
517 mach_vm_address_t alignMask
;
518 IOLibPageMallocHeader
* hdr
;
525 alignMask
= alignment
- 1;
526 adjustedSize
= (2 * size
) + sizeofIOLibPageMallocHeader
;
527 if (adjustedSize
< size
) return (0);
529 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
530 || (alignment
> page_size
);
532 if (contiguous
|| maxPhys
)
538 contiguous
= (contiguous
&& (adjustedSize
> page_size
))
539 || (alignment
> page_size
);
543 if (maxPhys
<= 0xFFFFFFFF)
546 options
|= KMA_LOMEM
;
548 else if (gIOLastPage
&& (atop_64(maxPhys
) > gIOLastPage
))
553 if (contiguous
|| maxPhys
)
555 kr
= kmem_alloc_contig(kernel_map
, &virt
, size
,
556 alignMask
, atop(maxPhys
), atop(alignMask
), 0, IOMemoryTag(kernel_map
));
560 kr
= kernel_memory_allocate(kernel_map
, &virt
,
561 size
, alignMask
, options
, IOMemoryTag(kernel_map
));
563 if (KERN_SUCCESS
== kr
)
567 if (TRACK_ALLOC
) IOTrackingAlloc(gIOMallocTracking
, address
, size
);
575 adjustedSize
+= alignMask
;
576 if (adjustedSize
< size
) return (0);
577 allocationAddress
= (mach_vm_address_t
) kalloc_tag_bt(adjustedSize
, VM_KERN_MEMORY_IOKIT
);
579 if (allocationAddress
) {
582 address
= (allocationAddress
+ alignMask
+ sizeofIOLibPageMallocHeader
)
585 if (atop_32(address
) != atop_32(address
+ size
- 1))
586 address
= round_page(address
);
588 hdr
= (typeof(hdr
))(address
- sizeofIOLibPageMallocHeader
);
589 hdr
->allocationSize
= adjustedSize
;
590 hdr
->allocationAddress
= allocationAddress
;
593 bzero(&hdr
->tracking
, sizeof(hdr
->tracking
));
594 hdr
->tracking
.address
= ~address
;
595 hdr
->tracking
.size
= size
;
596 IOTrackingAdd(gIOMallocTracking
, &hdr
->tracking
.tracking
, size
, true);
604 IOStatisticsAlloc(kIOStatisticsMallocContiguous
, size
);
606 OSAddAtomic(size
, &debug_iomalloc_size
);
614 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
616 struct _IOMallocContiguousEntry
618 mach_vm_address_t virtualAddr
;
619 IOBufferMemoryDescriptor
* md
;
622 typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry
;
624 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
625 IOPhysicalAddress
* physicalAddress
)
627 mach_vm_address_t address
= 0;
634 /* Do we want a physical address? */
635 if (!physicalAddress
)
637 address
= IOKernelAllocateWithPhysicalRestrict(size
, 0 /*maxPhys*/, alignment
, true);
641 IOBufferMemoryDescriptor
* bmd
;
642 mach_vm_address_t physicalMask
;
643 vm_offset_t alignMask
;
645 alignMask
= alignment
- 1;
646 physicalMask
= (0xFFFFFFFF ^ alignMask
);
648 bmd
= IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
649 kernel_task
, kIOMemoryPhysicallyContiguous
, size
, physicalMask
);
653 _IOMallocContiguousEntry
*
654 entry
= IONew(_IOMallocContiguousEntry
, 1);
660 entry
->virtualAddr
= (mach_vm_address_t
) bmd
->getBytesNoCopy();
662 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
663 queue_enter( &gIOMallocContiguousEntries
, entry
,
664 _IOMallocContiguousEntry
*, link
);
665 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
667 address
= (mach_vm_address_t
) entry
->virtualAddr
;
668 *physicalAddress
= bmd
->getPhysicalAddress();
672 return (void *) address
;
675 void IOFreeContiguous(void * _address
, vm_size_t size
)
677 _IOMallocContiguousEntry
* entry
;
678 IOMemoryDescriptor
* md
= NULL
;
680 mach_vm_address_t address
= (mach_vm_address_t
) _address
;
687 lck_mtx_lock(gIOMallocContiguousEntriesLock
);
688 queue_iterate( &gIOMallocContiguousEntries
, entry
,
689 _IOMallocContiguousEntry
*, link
)
691 if( entry
->virtualAddr
== address
) {
693 queue_remove( &gIOMallocContiguousEntries
, entry
,
694 _IOMallocContiguousEntry
*, link
);
698 lck_mtx_unlock(gIOMallocContiguousEntriesLock
);
703 IODelete(entry
, _IOMallocContiguousEntry
, 1);
707 IOKernelFreePhysical((mach_vm_address_t
) address
, size
);
711 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
714 IOIteratePageableMapsCallback callback
, void * ref
)
716 kern_return_t kr
= kIOReturnNotReady
;
723 if (size
> kIOPageableMaxMapSize
)
724 return( kIOReturnBadArgument
);
727 index
= gIOKitPageableSpace
.hint
;
728 attempts
= gIOKitPageableSpace
.count
;
730 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
731 if( KERN_SUCCESS
== kr
) {
732 gIOKitPageableSpace
.hint
= index
;
738 index
= gIOKitPageableSpace
.count
- 1;
740 if( KERN_SUCCESS
== kr
)
743 lck_mtx_lock( gIOKitPageableSpace
.lock
);
745 index
= gIOKitPageableSpace
.count
;
746 if( index
>= (kIOMaxPageableMaps
- 1)) {
747 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
751 if( size
< kIOPageableMapSize
)
752 segSize
= kIOPageableMapSize
;
757 kr
= kmem_suballoc(kernel_map
,
761 VM_FLAGS_ANYWHERE
| VM_MAKE_TAG(VM_KERN_MEMORY_IOKIT
),
763 if( KERN_SUCCESS
!= kr
) {
764 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
768 gIOKitPageableSpace
.maps
[index
].map
= map
;
769 gIOKitPageableSpace
.maps
[index
].address
= min
;
770 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
771 gIOKitPageableSpace
.hint
= index
;
772 gIOKitPageableSpace
.count
= index
+ 1;
774 lck_mtx_unlock( gIOKitPageableSpace
.lock
);
781 struct IOMallocPageableRef
788 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
790 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
793 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
, ref
->tag
);
798 static void * IOMallocPageablePages(vm_size_t size
, vm_size_t alignment
, vm_tag_t tag
)
800 kern_return_t kr
= kIOReturnNotReady
;
801 struct IOMallocPageableRef ref
;
803 if (alignment
> page_size
)
805 if (size
> kIOPageableMaxMapSize
)
810 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
811 if( kIOReturnSuccess
!= kr
)
814 return( (void *) ref
.address
);
817 vm_map_t
IOPageableMapForAddress( uintptr_t address
)
822 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
823 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
824 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
825 map
= gIOKitPageableSpace
.maps
[index
].map
;
830 panic("IOPageableMapForAddress: null");
835 static void IOFreePageablePages(void * address
, vm_size_t size
)
839 map
= IOPageableMapForAddress( (vm_address_t
) address
);
841 kmem_free( map
, (vm_offset_t
) address
, size
);
844 static uintptr_t IOMallocOnePageablePage(iopa_t
* a
)
846 return ((uintptr_t) IOMallocPageablePages(page_size
, page_size
, VM_KERN_MEMORY_IOKIT
));
849 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
853 if (size
>= (page_size
- 4*gIOPageAllocChunkBytes
)) addr
= IOMallocPageablePages(size
, alignment
, IOMemoryTag(kernel_map
));
854 else addr
= ((void * ) iopa_alloc(&gIOPageablePageAllocator
, &IOMallocOnePageablePage
, size
, alignment
));
858 OSAddAtomicLong(size
, &debug_iomallocpageable_size
);
860 IOStatisticsAlloc(kIOStatisticsMallocPageable
, size
);
866 void IOFreePageable(void * address
, vm_size_t size
)
869 OSAddAtomicLong(-size
, &debug_iomallocpageable_size
);
871 IOStatisticsAlloc(kIOStatisticsFreePageable
, size
);
873 if (size
< (page_size
- 4*gIOPageAllocChunkBytes
))
875 address
= (void *) iopa_free(&gIOPageablePageAllocator
, (uintptr_t) address
, size
);
878 if (address
) IOFreePageablePages(address
, size
);
881 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
884 iopa_init(iopa_t
* a
)
886 bzero(a
, sizeof(*a
));
887 a
->lock
= IOLockAlloc();
888 queue_init(&a
->list
);
892 iopa_allocinpage(iopa_page_t
* pa
, uint32_t count
, uint64_t align
)
895 uint64_t avail
= pa
->avail
;
899 // find strings of count 1 bits in avail
900 for (n
= count
; n
> 1; n
-= s
)
903 avail
= avail
& (avail
<< s
);
910 n
= __builtin_clzll(avail
);
911 pa
->avail
&= ~((-1ULL << (64 - count
)) >> n
);
912 if (!pa
->avail
&& pa
->link
.next
)
917 return (n
* gIOPageAllocChunkBytes
+ trunc_page((uintptr_t) pa
));
924 iopa_alloc(iopa_t
* a
, iopa_proc_t alloc
, vm_size_t bytes
, uint32_t balign
)
926 static const uint64_t align_masks
[] = {
940 if (!bytes
) bytes
= 1;
941 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
942 align
= align_masks
[log2up((balign
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
)];
945 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_first(&a
->list
));
946 while (!queue_end(&a
->list
, &pa
->link
))
948 addr
= iopa_allocinpage(pa
, count
, align
);
951 a
->bytecount
+= bytes
;
954 __IGNORE_WCASTALIGN(pa
= (typeof(pa
)) queue_next(&pa
->link
));
956 IOLockUnlock(a
->lock
);
963 pa
= (typeof(pa
)) (addr
+ page_size
- gIOPageAllocChunkBytes
);
964 pa
->signature
= kIOPageAllocSignature
;
967 addr
= iopa_allocinpage(pa
, count
, align
);
969 if (pa
->avail
) enqueue_head(&a
->list
, &pa
->link
);
971 if (addr
) a
->bytecount
+= bytes
;
972 IOLockUnlock(a
->lock
);
976 assert((addr
& ((1 << log2up(balign
)) - 1)) == 0);
981 iopa_free(iopa_t
* a
, uintptr_t addr
, vm_size_t bytes
)
987 if (!bytes
) bytes
= 1;
989 chunk
= (addr
& page_mask
);
990 assert(0 == (chunk
& (gIOPageAllocChunkBytes
- 1)));
992 pa
= (typeof(pa
)) (addr
| (page_size
- gIOPageAllocChunkBytes
));
993 assert(kIOPageAllocSignature
== pa
->signature
);
995 count
= (bytes
+ gIOPageAllocChunkBytes
- 1) / gIOPageAllocChunkBytes
;
996 chunk
/= gIOPageAllocChunkBytes
;
1001 assert(!pa
->link
.next
);
1002 enqueue_tail(&a
->list
, &pa
->link
);
1004 pa
->avail
|= ((-1ULL << (64 - count
)) >> chunk
);
1005 if (pa
->avail
!= -2ULL) pa
= 0;
1013 pa
= (typeof(pa
)) trunc_page(pa
);
1015 a
->bytecount
-= bytes
;
1016 IOLockUnlock(a
->lock
);
1018 return ((uintptr_t) pa
);
1021 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1023 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
1024 IOByteCount length
, IOOptionBits cacheMode
)
1026 IOReturn ret
= kIOReturnSuccess
;
1029 if( task
!= kernel_task
)
1030 return( kIOReturnUnsupported
);
1031 if ((address
| length
) & PAGE_MASK
)
1033 // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
1034 return( kIOReturnUnsupported
);
1036 length
= round_page(address
+ length
) - trunc_page( address
);
1037 address
= trunc_page( address
);
1040 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
1042 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
1044 // Get the physical page number
1045 pagenum
= pmap_find_phys(kernel_pmap
, (addr64_t
)address
);
1047 ret
= IOUnmapPages( get_task_map(task
), address
, page_size
);
1048 ret
= IOMapPages( get_task_map(task
), address
, ptoa_64(pagenum
), page_size
, cacheMode
);
1050 ret
= kIOReturnVMError
;
1052 address
+= page_size
;
1053 length
-= page_size
;
1060 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
1061 IOByteCount length
)
1063 if( task
!= kernel_task
)
1064 return( kIOReturnUnsupported
);
1066 flush_dcache64( (addr64_t
) address
, (unsigned) length
, false );
1068 return( kIOReturnSuccess
);
1071 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1073 vm_offset_t
OSKernelStackRemaining( void )
1075 return (ml_stack_remaining());
1078 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1081 * Spin for indicated number of milliseconds.
1083 void IOSleep(unsigned milliseconds
)
1085 delay_for_interval(milliseconds
, kMillisecondScale
);
1089 * Spin for indicated number of milliseconds, and potentially an
1090 * additional number of milliseconds up to the leeway values.
1092 void IOSleepWithLeeway(unsigned intervalMilliseconds
, unsigned leewayMilliseconds
)
1094 delay_for_interval_with_leeway(intervalMilliseconds
, leewayMilliseconds
, kMillisecondScale
);
1098 * Spin for indicated number of microseconds.
1100 void IODelay(unsigned microseconds
)
1102 delay_for_interval(microseconds
, kMicrosecondScale
);
1106 * Spin for indicated number of nanoseconds.
1108 void IOPause(unsigned nanoseconds
)
1110 delay_for_interval(nanoseconds
, kNanosecondScale
);
1113 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1115 static void _iolog_consputc(int ch
, void *arg __unused
)
1117 cons_putc_locked(ch
);
1120 static void _iolog_logputc(int ch
, void *arg __unused
)
1122 log_putc_locked(ch
);
1125 void IOLog(const char *format
, ...)
1129 va_start(ap
, format
);
1134 void IOLogv(const char *format
, va_list ap
)
1141 __doprnt(format
, ap
, _iolog_logputc
, NULL
, 16, TRUE
);
1145 __doprnt(format
, ap2
, _iolog_consputc
, NULL
, 16, TRUE
);
1150 void IOPanic(const char *reason
)
1152 panic("%s", reason
);
1156 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1159 * Convert a integer constant (typically a #define or enum) to a string.
1161 static char noValue
[80]; // that's pretty
1163 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
1165 for( ; regValueArray
->name
; regValueArray
++) {
1166 if(regValueArray
->value
== value
)
1167 return(regValueArray
->name
);
1169 snprintf(noValue
, sizeof(noValue
), "0x%x (UNDEFINED)", value
);
1170 return((const char *)noValue
);
1173 IOReturn
IOFindValueForName(const char *string
,
1174 const IONamedValue
*regValueArray
,
1177 for( ; regValueArray
->name
; regValueArray
++) {
1178 if(!strcmp(regValueArray
->name
, string
)) {
1179 *value
= regValueArray
->value
;
1180 return kIOReturnSuccess
;
1183 return kIOReturnBadArgument
;
1186 OSString
* IOCopyLogNameForPID(int pid
)
1190 snprintf(buf
, sizeof(buf
), "pid %d, ", pid
);
1192 proc_name(pid
, buf
+ len
, sizeof(buf
) - len
);
1193 return (OSString::withCString(buf
));
1196 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1198 IOAlignment
IOSizeToAlignment(unsigned int size
)
1201 const int intsize
= sizeof(unsigned int) * 8;
1203 for (shift
= 1; shift
< intsize
; shift
++) {
1204 if (size
& 0x80000000)
1205 return (IOAlignment
)(intsize
- shift
);
1211 unsigned int IOAlignmentToSize(IOAlignment align
)
1215 for (size
= 1; align
; align
--) {