X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/b4c24cb9d3df001f2892dc4ed451bc769ff28a9f..d7e50217d7adf6e52786a38bcaa4cd698cb9a79e:/iokit/Kernel/IOLib.c diff --git a/iokit/Kernel/IOLib.c b/iokit/Kernel/IOLib.c index 4eb08077e..f89aa4fbb 100644 --- a/iokit/Kernel/IOLib.c +++ b/iokit/Kernel/IOLib.c @@ -3,19 +3,22 @@ * * @APPLE_LICENSE_HEADER_START@ * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. + * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ @@ -36,10 +39,14 @@ #include #include +#include #include mach_timespec_t IOZeroTvalspec = { 0, 0 }; +extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va); + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * Global variables for use by iLogger @@ -58,9 +65,11 @@ void *_giDebugReserved2 = NULL; */ static IOThreadFunc threadArgFcn; -static void * threadArgArg; -static lock_t * threadArgLock; +static void * threadArgArg; +static lock_t * threadArgLock; +static queue_head_t gIOMallocContiguousEntries; +static mutex_t * gIOMallocContiguousEntriesLock; enum { kIOMaxPageableMaps = 16 }; enum { kIOPageableMapSize = 16 * 1024 * 1024 }; @@ -79,6 +88,7 @@ static struct { mutex_t * lock; } gIOKitPageableSpace; +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void IOLibInit(void) { @@ -106,6 +116,9 @@ void IOLibInit(void) gIOKitPageableSpace.hint = 0; gIOKitPageableSpace.count = 1; + gIOMallocContiguousEntriesLock = mutex_alloc( 0 ); + queue_init( &gIOMallocContiguousEntries ); + libInitialized = true; } @@ -272,6 +285,14 @@ void IOFreeAligned(void * address, vm_size_t size) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +struct _IOMallocContiguousEntry +{ + void * virtual; + ppnum_t ioBase; + queue_chain_t link; +}; +typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry; + void * IOMallocContiguous(vm_size_t size, vm_size_t alignment, IOPhysicalAddress * physicalAddress) { @@ -280,6 +301,7 @@ void * IOMallocContiguous(vm_size_t size, vm_size_t alignment, vm_address_t allocationAddress; vm_size_t adjustedSize; vm_offset_t alignMask; + ppnum_t pagenum; if (size == 0) return 0; @@ -289,15 +311,24 @@ void * IOMallocContiguous(vm_size_t size, vm_size_t alignment, alignMask = alignment - 1; adjustedSize = (2 * size) + sizeof(vm_size_t) + sizeof(vm_address_t); - if (adjustedSize >= page_size) { - - kr = kmem_alloc_contig(kernel_map, &address, size, - alignMask, 0); + if (adjustedSize >= page_size) + { + adjustedSize = size; + if (adjustedSize > page_size) + { + kr = kmem_alloc_contig(kernel_map, &address, size, + alignMask, 0); + } + else + { + kr = kernel_memory_allocate(kernel_map, &address, + size, alignMask, 0); + } if (KERN_SUCCESS != kr) address = 0; - - } else { - + } + else + { adjustedSize += alignMask; allocationAddress = (vm_address_t) kalloc(adjustedSize); @@ -307,8 +338,8 @@ void * IOMallocContiguous(vm_size_t size, vm_size_t alignment, + (sizeof(vm_size_t) + sizeof(vm_address_t))) & (~alignMask); - if (atop(address) != atop(address + size - 1)) - address = round_page(address); + if (atop_32(address) != atop_32(address + size - 1)) + address = round_page_32(address); *((vm_size_t *)(address - sizeof(vm_size_t) - sizeof(vm_address_t))) = adjustedSize; @@ -318,9 +349,49 @@ void * IOMallocContiguous(vm_size_t size, vm_size_t alignment, address = 0; } - if( address && physicalAddress) - *physicalAddress = (IOPhysicalAddress) pmap_extract( kernel_pmap, - address ); + /* Do we want a physical address? */ + if (address && physicalAddress) + { + do + { + /* Get the physical page */ + pagenum = pmap_find_phys(kernel_pmap, (addr64_t) address); + if(pagenum) + { + IOByteCount offset; + ppnum_t base; + + base = IOMapperIOVMAlloc((size + PAGE_MASK) >> PAGE_SHIFT); + if (base) + { + _IOMallocContiguousEntry * + entry = IONew(_IOMallocContiguousEntry, 1); + if (!entry) + { + IOFreeContiguous((void *) address, size); + address = 0; + break; + } + entry->virtual = (void *) address; + entry->ioBase = base; + mutex_lock(gIOMallocContiguousEntriesLock); + queue_enter( &gIOMallocContiguousEntries, entry, + _IOMallocContiguousEntry *, link ); + mutex_unlock(gIOMallocContiguousEntriesLock); + + *physicalAddress = (IOPhysicalAddress)((base << PAGE_SHIFT) | (address & PAGE_MASK)); + for (offset = 0; offset < ((size + PAGE_MASK) >> PAGE_SHIFT); offset++, pagenum++) + IOMapperInsertPage( base, offset, pagenum ); + } + else + *physicalAddress = (IOPhysicalAddress)((pagenum << PAGE_SHIFT) | (address & PAGE_MASK)); + } + else + /* Did not find, return 0 */ + *physicalAddress = (IOPhysicalAddress) 0; + } + while (false); + } assert(0 == (address & alignMask)); @@ -334,14 +405,35 @@ void * IOMallocContiguous(vm_size_t size, vm_size_t alignment, void IOFreeContiguous(void * address, vm_size_t size) { - vm_address_t allocationAddress; - vm_size_t adjustedSize; + vm_address_t allocationAddress; + vm_size_t adjustedSize; + _IOMallocContiguousEntry * entry; + ppnum_t base = 0; if( !address) return; assert(size); + mutex_lock(gIOMallocContiguousEntriesLock); + queue_iterate( &gIOMallocContiguousEntries, entry, + _IOMallocContiguousEntry *, link ) + { + if( entry->virtual == address ) { + base = entry->ioBase; + queue_remove( &gIOMallocContiguousEntries, entry, + _IOMallocContiguousEntry *, link ); + break; + } + } + mutex_unlock(gIOMallocContiguousEntriesLock); + + if (base) + { + IOMapperIOVMFree(base, (size + PAGE_MASK) >> PAGE_SHIFT); + IODelete(entry, _IOMallocContiguousEntry, 1); + } + adjustedSize = (2 * size) + sizeof(vm_size_t) + sizeof(vm_address_t); if (adjustedSize >= page_size) { @@ -466,7 +558,7 @@ void * IOMallocPageable(vm_size_t size, vm_size_t alignment) #if IOALLOCDEBUG if( ref.address) - debug_iomalloc_size += round_page(size); + debug_iomalloc_size += round_page_32(size); #endif return( (void *) ref.address ); @@ -499,7 +591,7 @@ void IOFreePageable(void * address, vm_size_t size) kmem_free( map, (vm_offset_t) address, size); #if IOALLOCDEBUG - debug_iomalloc_size -= round_page(size); + debug_iomalloc_size -= round_page_32(size); #endif } @@ -507,30 +599,34 @@ void IOFreePageable(void * address, vm_size_t size) extern kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa, vm_size_t length, unsigned int options); +extern kern_return_t IOUnmapPages(vm_map_t map, vm_offset_t va, vm_size_t length); IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address, IOByteCount length, IOOptionBits cacheMode ) { IOReturn ret = kIOReturnSuccess; - vm_offset_t physAddr; + ppnum_t pagenum; if( task != kernel_task) return( kIOReturnUnsupported ); - length = round_page(address + length) - trunc_page( address ); - address = trunc_page( address ); + length = round_page_32(address + length) - trunc_page_32( address ); + address = trunc_page_32( address ); // make map mode cacheMode = (cacheMode << kIOMapCacheShift) & kIOMapCacheMask; while( (kIOReturnSuccess == ret) && (length > 0) ) { - physAddr = pmap_extract( kernel_pmap, address ); - if( physAddr) - ret = IOMapPages( get_task_map(task), address, physAddr, page_size, cacheMode ); - else + // Get the physical page number + pagenum = pmap_find_phys(kernel_pmap, (addr64_t)address); + if( pagenum) { + ret = IOUnmapPages( get_task_map(task), address, page_size ); + ret = IOMapPages( get_task_map(task), address, pagenum << PAGE_SHIFT, page_size, cacheMode ); + } else ret = kIOReturnVMError; + address += page_size; length -= page_size; } @@ -545,7 +641,7 @@ IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address, return( kIOReturnUnsupported ); #if __ppc__ - flush_dcache( (vm_offset_t) address, (unsigned) length, false ); + flush_dcache64( (addr64_t) address, (unsigned) length, false ); #endif return( kIOReturnSuccess );