Loading...
iokit/Kernel/IOMapper.cpp xnu-1228 xnu-792
--- xnu/xnu-1228/iokit/Kernel/IOMapper.cpp
+++ xnu/xnu-792/iokit/Kernel/IOMapper.cpp
@@ -1,44 +1,32 @@
 /*
  * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
+ * @APPLE_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.
+ * 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.
  * 
- * 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
+ * This 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.
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
+ * License for the specific language governing rights and limitations
+ * under the License.
  * 
- * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
+ * @APPLE_LICENSE_HEADER_END@
  */
 #include <IOKit/IOLib.h>
 #include <IOKit/IOMapper.h>
 #include <libkern/c++/OSData.h>
 
-#include "IOCopyMapper.h"
-
-__BEGIN_DECLS
-extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
-__END_DECLS
-
 #define super IOService
 OSDefineMetaClassAndAbstractStructors(IOMapper, IOService);
 
-OSMetaClassDefineReservedUsed(IOMapper, 0);
+OSMetaClassDefineReservedUnused(IOMapper, 0);
 OSMetaClassDefineReservedUnused(IOMapper, 1);
 OSMetaClassDefineReservedUnused(IOMapper, 2);
 OSMetaClassDefineReservedUnused(IOMapper, 3);
@@ -142,44 +130,98 @@
         iovmInsert(addr, offset + i, pageList[i].phys_addr);
 }
 
-OSData * IOMapper::
-NewARTTable(IOByteCount size, void ** virtAddrP, ppnum_t *physAddrP)
-{
-    if (!virtAddrP || !physAddrP)
-	return 0;
-
+struct ARTTableData {
+    void *v;
+    upl_t u[0];
+};
+#define getARTDataP(data) ((ARTTableData *) (data)->getBytesNoCopy())
+
+OSData *
+IOMapper::NewARTTable(IOByteCount size,
+                      void ** virtAddrP, ppnum_t *physAddrP)
+{
+    OSData *ret;
     kern_return_t kr;
-    vm_address_t address;
-
+    vm_address_t startUpl;
+    ARTTableData *dataP;
+    unsigned int dataSize;
+    upl_page_info_t *pl = 0;
+
+    // Each UPL can deal with about one meg at the moment
     size = round_page_32(size);
-    kr = kmem_alloc_contig(kernel_map, &address, size, PAGE_MASK, 0, 0);
+    dataSize = sizeof(ARTTableData) + sizeof(upl_t) * size / (1024 * 1024);
+    ret = OSData::withCapacity(dataSize);
+    if (!ret)
+        return 0;
+
+    // Append 0's to the buffer, in-other-words reset to nulls.
+    ret->appendBytes(NULL, sizeof(ARTTableData));
+    dataP = getARTDataP(ret);
+
+    kr = kmem_alloc_contig(kernel_map, &startUpl, size, PAGE_MASK, 0);
     if (kr)
         return 0;
 
-    ppnum_t pagenum = pmap_find_phys(kernel_pmap, (addr64_t) address);
-    if (pagenum)
-	*physAddrP = pagenum;
-    else {
-	FreeARTTable((OSData *) address, size);
-	address = 0;
-    }
-
-    *virtAddrP = (void *) address;
-
-    return (OSData *) address;
+    dataP->v = (void *) startUpl;
+
+    do {
+        upl_t iopl;
+        int upl_flags = UPL_SET_INTERNAL | UPL_SET_LITE
+                      | UPL_SET_IO_WIRE | UPL_COPYOUT_FROM;
+        vm_size_t iopl_size = size;
+
+        kr = vm_map_get_upl(kernel_map,
+                            startUpl,
+                            &iopl_size,
+                            &iopl,
+                            0,
+                            0,
+                            &upl_flags,
+                            0);
+        if (kr) {
+            panic("IOMapper:vm_map_get_upl returned 0x%x\n");
+            goto bail;
+        }
+
+        if (!ret->appendBytes(&iopl, sizeof(upl_t)))
+            goto bail;
+            
+        startUpl += iopl_size;
+        size -= iopl_size;
+    } while(size);
+
+    // Need to re-establish the dataP as the OSData may have grown.
+    dataP = getARTDataP(ret);
+
+    // Now grab the page entry of the first page and get its phys addr
+    pl = UPL_GET_INTERNAL_PAGE_LIST(dataP->u[0]);
+    *physAddrP = pl->phys_addr;
+    *virtAddrP = dataP->v;
+
+    return ret;
+
+bail:
+    FreeARTTable(ret, size);
+    return 0;
 }
 
 void IOMapper::FreeARTTable(OSData *artHandle, IOByteCount size)
 {
-    vm_address_t address = (vm_address_t) artHandle;
-
-    size = round_page_32(size);
-    kmem_free(kernel_map, address, size);	// Just panic if address is 0
-}
-
-bool IOMapper::getBypassMask(addr64_t *maskP) const
-{
-    return false;
+    assert(artHandle);
+
+    ARTTableData *dataP = getARTDataP(artHandle);
+
+    int numupls = ((artHandle->getLength() - sizeof(*dataP)) / sizeof(upl_t));
+    for (int i = 0; i < numupls; i++) {
+        upl_abort(dataP->u[i], 0);
+        upl_deallocate(dataP->u[i]);
+    }
+
+    if (dataP->v) {
+        size = round_page_32(size);
+        kmem_free(kernel_map, (vm_address_t) dataP->v, size);
+    }
+    artHandle->release();
 }
 
 __BEGIN_DECLS
@@ -343,23 +385,4 @@
         ml_phys_write_double((vm_offset_t) address, value);
 }
 
-mach_vm_address_t IOMallocPhysical(mach_vm_size_t size, mach_vm_address_t mask)
-{
-    mach_vm_address_t address = 0;
-    if (gIOCopyMapper)
-    {
-	address = ptoa_64(gIOCopyMapper->iovmAlloc(atop_64(round_page(size))));
-    }
-    return (address);
-}
-
-void IOFreePhysical(mach_vm_address_t address, mach_vm_size_t size)
-{
-    if (gIOCopyMapper)
-    {
-	gIOCopyMapper->iovmFree(atop_64(address), atop_64(round_page(size)));
-    }
-}
-
-
 __END_DECLS