]> git.saurik.com Git - apple/xnu.git/blobdiff - iokit/Kernel/IOLib.cpp
xnu-1504.7.4.tar.gz
[apple/xnu.git] / iokit / Kernel / IOLib.cpp
index 4e86365930e518cdf06940572d48e7438f6eb173..e40d20afc2be6dd869de850f1b72c1e24f8c8171 100644 (file)
@@ -1,31 +1,29 @@
 /*
 /*
- * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * 
- * 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.  The rights granted to you under the 
- * License may not be used to create, or enable the creation or 
- * redistribution of, unlawful or unlicensed copies of an Apple operating 
- * system, or to circumvent, violate, or enable the circumvention or 
- * violation of, any terms of an Apple operating system software license 
- * agreement.
- *
- * 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, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
- * Please see the License for the specific language governing rights and 
+ * 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. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * 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, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
  * limitations under the License.
  * limitations under the License.
- *
- * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
+ * 
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * HISTORY
  */
 /*
  * HISTORY
@@ -38,6 +36,7 @@
 #include <IOKit/system.h>
 #include <mach/sync_policy.h>
 #include <machine/machine_routines.h>
 #include <IOKit/system.h>
 #include <mach/sync_policy.h>
 #include <machine/machine_routines.h>
+#include <vm/vm_kern.h>
 #include <libkern/c++/OSCPPDebug.h>
 
 #include <IOKit/assert.h>
 #include <libkern/c++/OSCPPDebug.h>
 
 #include <IOKit/assert.h>
 
 #include "IOKitKernelInternal.h"
 
 
 #include "IOKitKernelInternal.h"
 
+#ifdef IOALLOCDEBUG
+#include <libkern/OSDebug.h>
+#include <sys/sysctl.h>
+#endif
+
 extern "C"
 {
 
 extern "C"
 {
 
@@ -59,13 +63,16 @@ mach_timespec_t IOZeroTvalspec = { 0, 0 };
 
 extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
 
 
 extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
 
-extern kern_return_t   kmem_suballoc(
-                               vm_map_t        parent,
-                               vm_offset_t     *addr,
-                               vm_size_t       size,
-                               boolean_t       pageable,
-                               boolean_t       anywhere,
-                               vm_map_t        *new_map);
+int
+__doprnt(
+       const char              *fmt,
+       va_list                 argp,
+       void                    (*putc)(int, void *),
+       void                    *arg,
+       int                     radix);
+
+extern void conslog_putc(char);
+
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
@@ -94,9 +101,8 @@ enum { kIOMaxPageableMaps = 16 };
 enum { kIOPageableMapSize = 96 * 1024 * 1024 };
 enum { kIOPageableMaxMapSize = 96 * 1024 * 1024 };
 
 enum { kIOPageableMapSize = 96 * 1024 * 1024 };
 enum { kIOPageableMaxMapSize = 96 * 1024 * 1024 };
 
-/* LP64todo - these need to expand */
 typedef struct {
 typedef struct {
-    vm_map_t   map;
+    vm_map_t           map;
     vm_offset_t        address;
     vm_offset_t        end;
 } IOMapData;
     vm_offset_t        address;
     vm_offset_t        end;
 } IOMapData;
@@ -173,8 +179,9 @@ void * IOMalloc(vm_size_t size)
 
     address = (void *)kalloc(size);
 #if IOALLOCDEBUG
 
     address = (void *)kalloc(size);
 #if IOALLOCDEBUG
-    if (address)
-       debug_iomalloc_size += size;
+    if (address) {
+               debug_iomalloc_size += size;
+       }
 #endif
     return address;
 }
 #endif
     return address;
 }
@@ -182,9 +189,9 @@ void * IOMalloc(vm_size_t size)
 void IOFree(void * address, vm_size_t size)
 {
     if (address) {
 void IOFree(void * address, vm_size_t size)
 {
     if (address) {
-       kfree(address, size);
+               kfree(address, size);
 #if IOALLOCDEBUG
 #if IOALLOCDEBUG
-       debug_iomalloc_size -= size;
+               debug_iomalloc_size -= size;
 #endif
     }
 }
 #endif
     }
 }
@@ -194,10 +201,10 @@ void IOFree(void * address, vm_size_t size)
 void * IOMallocAligned(vm_size_t size, vm_size_t alignment)
 {
     kern_return_t      kr;
 void * IOMallocAligned(vm_size_t size, vm_size_t alignment)
 {
     kern_return_t      kr;
-    vm_address_t       address;
-    vm_address_t       allocationAddress;
+    vm_offset_t                address;
+    vm_offset_t                allocationAddress;
     vm_size_t          adjustedSize;
     vm_size_t          adjustedSize;
-    vm_offset_t                alignMask;
+    uintptr_t          alignMask;
 
     if (size == 0)
         return 0;
 
     if (size == 0)
         return 0;
@@ -233,8 +240,8 @@ void * IOMallocAligned(vm_size_t size, vm_size_t alignment)
                     + (sizeof(vm_size_t) + sizeof(vm_address_t)))
                     & (~alignMask);
 
                     + (sizeof(vm_size_t) + sizeof(vm_address_t)))
                     & (~alignMask);
 
-            *((vm_size_t *)(address - sizeof(vm_size_t)
-                            - sizeof(vm_address_t))) = adjustedSize;
+            *((vm_size_t *)(address - sizeof(vm_size_t) - sizeof(vm_address_t))) 
+                           = adjustedSize;
             *((vm_address_t *)(address - sizeof(vm_address_t)))
                             = allocationAddress;
        } else
             *((vm_address_t *)(address - sizeof(vm_address_t)))
                             = allocationAddress;
        } else
@@ -244,8 +251,9 @@ void * IOMallocAligned(vm_size_t size, vm_size_t alignment)
     assert(0 == (address & alignMask));
 
 #if IOALLOCDEBUG
     assert(0 == (address & alignMask));
 
 #if IOALLOCDEBUG
-    if( address)
-       debug_iomalloc_size += size;
+    if( address) {
+               debug_iomalloc_size += size;
+       }
 #endif
 
     return (void *) address;
 #endif
 
     return (void *) address;
@@ -254,7 +262,7 @@ void * IOMallocAligned(vm_size_t size, vm_size_t alignment)
 void IOFreeAligned(void * address, vm_size_t size)
 {
     vm_address_t       allocationAddress;
 void IOFreeAligned(void * address, vm_size_t size)
 {
     vm_address_t       allocationAddress;
-    vm_size_t          adjustedSize;
+    vm_size_t  adjustedSize;
 
     if( !address)
        return;
 
     if( !address)
        return;
@@ -264,10 +272,10 @@ void IOFreeAligned(void * address, vm_size_t size)
     adjustedSize = size + sizeof(vm_size_t) + sizeof(vm_address_t);
     if (adjustedSize >= page_size) {
 
     adjustedSize = size + sizeof(vm_size_t) + sizeof(vm_address_t);
     if (adjustedSize >= page_size) {
 
-        kmem_free( kernel_map, (vm_address_t) address, size);
+        kmem_free( kernel_map, (vm_offset_t) address, size);
 
     } else {
 
     } else {
-        adjustedSize = *((vm_size_t *)( (vm_address_t) address
+       adjustedSize = *((vm_size_t *)( (vm_address_t) address
                                 - sizeof(vm_address_t) - sizeof(vm_size_t)));
         allocationAddress = *((vm_address_t *)( (vm_address_t) address
                                - sizeof(vm_address_t) ));
                                 - sizeof(vm_address_t) - sizeof(vm_size_t)));
         allocationAddress = *((vm_address_t *)( (vm_address_t) address
                                - sizeof(vm_address_t) ));
@@ -299,7 +307,7 @@ IOKernelFreeContiguous(mach_vm_address_t address, mach_vm_size_t size)
     adjustedSize = (2 * size) + sizeof(mach_vm_size_t) + sizeof(mach_vm_address_t);
     if (adjustedSize >= page_size) {
 
     adjustedSize = (2 * size) + sizeof(mach_vm_size_t) + sizeof(mach_vm_address_t);
     if (adjustedSize >= page_size) {
 
-       kmem_free( kernel_map, (vm_address_t) address, size);
+       kmem_free( kernel_map, (vm_offset_t) address, size);
 
     } else {
 
 
     } else {
 
@@ -316,7 +324,8 @@ IOKernelFreeContiguous(mach_vm_address_t address, mach_vm_size_t size)
 }
 
 mach_vm_address_t
 }
 
 mach_vm_address_t
-IOKernelAllocateContiguous(mach_vm_size_t size, mach_vm_size_t alignment)
+IOKernelAllocateContiguous(mach_vm_size_t size, mach_vm_address_t maxPhys, 
+                           mach_vm_size_t alignment)
 {
     kern_return_t      kr;
     mach_vm_address_t  address;
 {
     kern_return_t      kr;
     mach_vm_address_t  address;
@@ -336,10 +345,10 @@ IOKernelAllocateContiguous(mach_vm_size_t size, mach_vm_size_t alignment)
     {
        vm_offset_t virt;
        adjustedSize = size;
     {
        vm_offset_t virt;
        adjustedSize = size;
-       if (adjustedSize > page_size)
+       if ((adjustedSize > page_size) || (alignment > page_size) || maxPhys)
        {
            kr = kmem_alloc_contig(kernel_map, &virt, size,
        {
            kr = kmem_alloc_contig(kernel_map, &virt, size,
-                                   alignMask, 0);
+                                  alignMask, atop(maxPhys), atop(alignMask), 0);
        }
        else
        {
        }
        else
        {
@@ -363,7 +372,7 @@ IOKernelAllocateContiguous(mach_vm_size_t size, mach_vm_size_t alignment)
                     & (~alignMask);
 
             if (atop_32(address) != atop_32(address + size - 1))
                     & (~alignMask);
 
             if (atop_32(address) != atop_32(address + size - 1))
-                address = round_page_32(address);
+                address = round_page(address);
 
             *((mach_vm_size_t *)(address - sizeof(mach_vm_size_t)
                             - sizeof(mach_vm_address_t))) = adjustedSize;
 
             *((mach_vm_size_t *)(address - sizeof(mach_vm_size_t)
                             - sizeof(mach_vm_address_t))) = adjustedSize;
@@ -374,8 +383,9 @@ IOKernelAllocateContiguous(mach_vm_size_t size, mach_vm_size_t alignment)
     }
 
 #if IOALLOCDEBUG
     }
 
 #if IOALLOCDEBUG
-    if (address)
+    if (address) {
        debug_iomalloc_size += size;
        debug_iomalloc_size += size;
+    }
 #endif
 
     return (address);
 #endif
 
     return (address);
@@ -404,16 +414,17 @@ void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
     /* Do we want a physical address? */
     if (!physicalAddress)
     {
     /* Do we want a physical address? */
     if (!physicalAddress)
     {
-       address = IOKernelAllocateContiguous(size, alignment);
+       address = IOKernelAllocateContiguous(size, 0 /*maxPhys*/, alignment);
     }
     else do
     {
        IOBufferMemoryDescriptor * bmd;
        mach_vm_address_t          physicalMask;
     }
     else do
     {
        IOBufferMemoryDescriptor * bmd;
        mach_vm_address_t          physicalMask;
-        vm_offset_t               alignMask;
+       vm_offset_t                alignMask;
 
        alignMask = alignment - 1;
 
        alignMask = alignment - 1;
-       physicalMask = 0xFFFFFFFF ^ (alignMask & PAGE_MASK);
+       physicalMask = (0xFFFFFFFF ^ alignMask);
+
        bmd = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
                kernel_task, kIOMemoryPhysicallyContiguous, size, physicalMask);
        if (!bmd)
        bmd = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
                kernel_task, kIOMemoryPhysicallyContiguous, size, physicalMask);
        if (!bmd)
@@ -549,7 +560,7 @@ kern_return_t IOIteratePageableMaps(vm_size_t size,
 
 struct IOMallocPageableRef
 {
 
 struct IOMallocPageableRef
 {
-    vm_address_t address;
+    vm_offset_t address;
     vm_size_t   size;
 };
 
     vm_size_t   size;
 };
 
@@ -580,13 +591,13 @@ void * IOMallocPageable(vm_size_t size, vm_size_t alignment)
 
 #if IOALLOCDEBUG
     if( ref.address)
 
 #if IOALLOCDEBUG
     if( ref.address)
-       debug_iomallocpageable_size += round_page_32(size);
+       debug_iomallocpageable_size += round_page(size);
 #endif
 
     return( (void *) ref.address );
 }
 
 #endif
 
     return( (void *) ref.address );
 }
 
-vm_map_t IOPageableMapForAddress( vm_address_t address )
+vm_map_t IOPageableMapForAddress( uintptr_t address )
 {
     vm_map_t   map = 0;
     UInt32     index;
 {
     vm_map_t   map = 0;
     UInt32     index;
@@ -599,7 +610,7 @@ vm_map_t IOPageableMapForAddress( vm_address_t address )
         }
     }
     if( !map)
         }
     }
     if( !map)
-        IOPanic("IOPageableMapForAddress: null");
+        panic("IOPageableMapForAddress: null");
 
     return( map );
 }
 
     return( map );
 }
@@ -613,10 +624,10 @@ void IOFreePageable(void * address, vm_size_t size)
         kmem_free( map, (vm_offset_t) address, size);
 
 #if IOALLOCDEBUG
         kmem_free( map, (vm_offset_t) address, size);
 
 #if IOALLOCDEBUG
-    debug_iomallocpageable_size -= round_page_32(size);
+    debug_iomallocpageable_size -= round_page(size);
 #endif
 }
 #endif
 }
-
+    
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
@@ -627,9 +638,13 @@ IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
 
     if( task != kernel_task)
        return( kIOReturnUnsupported );
 
     if( task != kernel_task)
        return( kIOReturnUnsupported );
-
-    length = round_page_32(address + length) - trunc_page_32( address );
-    address = trunc_page_32( address );
+    if ((address | length) & PAGE_MASK)
+    {
+//     OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode);
+       return( kIOReturnUnsupported );
+    }
+    length = round_page(address + length) - trunc_page( address );
+    address = trunc_page( address );
 
     // make map mode
     cacheMode = (cacheMode << kIOMapCacheShift) & kIOMapCacheMask;
 
     // make map mode
     cacheMode = (cacheMode << kIOMapCacheShift) & kIOMapCacheMask;
@@ -658,26 +673,23 @@ IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
     if( task != kernel_task)
        return( kIOReturnUnsupported );
 
     if( task != kernel_task)
        return( kIOReturnUnsupported );
 
-#if __ppc__
     flush_dcache64( (addr64_t) address, (unsigned) length, false );
     flush_dcache64( (addr64_t) address, (unsigned) length, false );
-#endif
 
     return( kIOReturnSuccess );
 }
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 
     return( kIOReturnSuccess );
 }
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
-SInt32 OSKernelStackRemaining( void )
+vm_offset_t OSKernelStackRemaining( void )
 {
 {
-   SInt32 stack;
-
-   stack = (((SInt32) &stack) & (KERNEL_STACK_SIZE - 1));
-
-   return( stack );
+    return (ml_stack_remaining());
 }
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 }
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
+/*
+ * Spin for indicated number of milliseconds.
+ */
 void IOSleep(unsigned milliseconds)
 {
     delay_for_interval(milliseconds, kMillisecondScale);
 void IOSleep(unsigned milliseconds)
 {
     delay_for_interval(milliseconds, kMillisecondScale);
@@ -691,23 +703,41 @@ void IODelay(unsigned microseconds)
     delay_for_interval(microseconds, kMicrosecondScale);
 }
 
     delay_for_interval(microseconds, kMicrosecondScale);
 }
 
+/*
+ * Spin for indicated number of nanoseconds.
+ */
+void IOPause(unsigned nanoseconds)
+{
+    delay_for_interval(nanoseconds, kNanosecondScale);
+}
+
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
+static void _iolog_putc(int ch, void *arg __unused)
+{
+       conslog_putc(ch);
+}
+
 void IOLog(const char *format, ...)
 {
        va_list ap;
 void IOLog(const char *format, ...)
 {
        va_list ap;
-       extern void conslog_putc(char);
-       extern void logwakeup(void);
 
        va_start(ap, format);
 
        va_start(ap, format);
-       _doprnt(format, &ap, conslog_putc, 16);
+       __doprnt(format, ap, _iolog_putc, NULL, 16);
        va_end(ap);
 }
 
        va_end(ap);
 }
 
+void IOLogv(const char *format, va_list ap)
+{
+       __doprnt(format, ap, _iolog_putc, NULL, 16);
+}
+
+#if !__LP64__
 void IOPanic(const char *reason)
 {
 void IOPanic(const char *reason)
 {
-       panic(reason);
+       panic("%s", reason);
 }
 }
+#endif
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
@@ -722,7 +752,7 @@ const char *IOFindNameForValue(int value, const IONamedValue *regValueArray)
                if(regValueArray->value == value)
                        return(regValueArray->name);
        }
                if(regValueArray->value == value)
                        return(regValueArray->name);
        }
-       sprintf(noValue, "0x%x (UNDEFINED)", value);
+       snprintf(noValue, sizeof(noValue), "0x%x (UNDEFINED)", value);
        return((const char *)noValue);
 }
 
        return((const char *)noValue);
 }
 
@@ -739,6 +769,16 @@ IOReturn IOFindValueForName(const char *string,
        return kIOReturnBadArgument;
 }
 
        return kIOReturnBadArgument;
 }
 
+OSString * IOCopyLogNameForPID(int pid)
+{
+    char   buf[128];
+    size_t len;
+    snprintf(buf, sizeof(buf), "pid %d, ", pid);
+    len = strlen(buf);
+    proc_name(pid, buf + len, sizeof(buf) - len);
+    return (OSString::withCString(buf));
+}
+
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 IOAlignment IOSizeToAlignment(unsigned int size)
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 IOAlignment IOSizeToAlignment(unsigned int size)
@@ -765,3 +805,6 @@ unsigned int IOAlignmentToSize(IOAlignment align)
 }
 
 } /* extern "C" */
 }
 
 } /* extern "C" */
+
+
+