]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/ppc/mappings.c
xnu-1228.tar.gz
[apple/xnu.git] / osfmk / ppc / mappings.c
index e92ce2469c66e4198fbd0f58109d87bdbcc4bd3f..b6430b79b2c8e588a2f7a45dada40c6d1bf6ee9a 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_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.
+ * 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.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  *     This file is used to maintain the virtual to real mappings for a PowerPC machine.
@@ -77,7 +83,9 @@ extern unsigned int DebugWork;                                                /* (BRINGUP) */
 void mapping_verify(void);
 void mapping_phys_unused(ppnum_t pa);
 
-int nx_enabled = 0;                    /* enable no-execute protection */
+int nx_enabled = 1;                    /* enable no-execute protection */
+int allow_data_exec  = VM_ABI_32;      /* 32-bit apps may execute data by default, 64-bit apps may not */
+int allow_stack_exec = VM_ABI_32;      /* 32-bit apps may execute from the stack by default, 64-bit apps may not */
 
 /*
  *  ppc_prot translates Mach's representation of protections to that of the PPC hardware.
@@ -229,7 +237,7 @@ addr64_t mapping_remove(pmap_t pmap, addr64_t va) {         /* Remove a single mapping
                case mapRtNotFnd:
                        return (nextva | 1);                                            /* Nothing found to unmap */
                default:
-                       panic("mapping_remove: hw_rem_map failed - pmap = %08X, va = %016llX, code = %08X\n",
+                       panic("mapping_remove: hw_rem_map failed - pmap = %p, va = %016llX, code = %p\n",
                                pmap, va, mp);
                        break;
        }
@@ -255,7 +263,7 @@ addr64_t mapping_remove(pmap_t pmap, addr64_t va) {         /* Remove a single mapping
                                case mapRtEmpty:                                                /* No guest mappings left to scrub */
                                        break;
                                default:
-                                       panic("mapping_remove: hw_scrub_guest failed - physent = %08X, code = %08X\n",
+                                       panic("mapping_remove: hw_scrub_guest failed - physent = %p, code = %p\n",
                                                physent, mp);                                   /* Cry havoc, cry wrack,
                                                                                                                        at least we die with harness on our backs */
                                        break;
@@ -316,17 +324,17 @@ addr64_t mapping_make(pmap_t pmap, addr64_t va, ppnum_t pa, unsigned int flags,
 
        pcf = (flags & mmFlgPcfg) >> 24;                                                        /* Get the physical page config index */
        if(!(pPcfg[pcf].pcfFlags)) {                                                            /* Validate requested physical page configuration */
-               panic("mapping_make: invalid physical page configuration request - pmap = %08X, va = %016llX, cfg = %d\n",
+               panic("mapping_make: invalid physical page configuration request - pmap = %p, va = %016llX, cfg = %d\n",
                        pmap, va, pcf);
        }
        
        psmask = (1ULL << pPcfg[pcf].pcfPSize) - 1;                                     /* Mask to isolate any offset into a page */
        if(va & psmask) {                                                                                       /* Make sure we are page aligned on virtual */
-               panic("mapping_make: attempt to map unaligned vaddr - pmap = %08X, va = %016llX, cfg = %d\n",
+               panic("mapping_make: attempt to map unaligned vaddr - pmap = %p, va = %016llX, cfg = %d\n",
                        pmap, va, pcf);
        }
        if(((addr64_t)pa << 12) & psmask) {                                                     /* Make sure we are page aligned on physical */
-               panic("mapping_make: attempt to map unaligned paddr - pmap = %08X, pa = %016llX, cfg = %d\n",
+               panic("mapping_make: attempt to map unaligned paddr - pmap = %p, pa = %08X, cfg = %d\n",
                        pmap, pa, pcf);
        }
        
@@ -407,7 +415,7 @@ addr64_t mapping_make(pmap_t pmap, addr64_t va, ppnum_t pa, unsigned int flags,
                                return (colladdr | mapRtSmash);                                 /* Return colliding address, with some dirt added to avoid
                                                                                                                                   confusion if effective address is 0 */
                        default:
-                               panic("mapping_make: hw_add_map failed - collision addr = %016llX, code = %02X, pmap = %08X, va = %016llX, mapping = %08X\n",
+                               panic("mapping_make: hw_add_map failed - collision addr = %016llX, code = %02X, pmap = %p, va = %016llX, mapping = %p\n",
                                        colladdr, rc, pmap, va, mp);                            /* Die dead */
                }
                
@@ -449,19 +457,19 @@ mapping_t *mapping_find(pmap_t pmap, addr64_t va, addr64_t *nextva, int full) {
 
                mp = hw_find_map(curpmap, curva, nextva);                               /* Find the mapping for this address */
                if((unsigned int)mp == mapRtBadLk) {                                    /* Did we lock up ok? */
-                       panic("mapping_find: pmap lock failure - rc = %08X, pmap = %08X\n", mp, curpmap);       /* Die... */
+                       panic("mapping_find: pmap lock failure - rc = %p, pmap = %p\n", mp, curpmap);   /* Die... */
                }
                
                if(!mp || ((mp->mpFlags & mpType) < mpMinSpecial) || !full) break;              /* Are we done looking? */
 
                if((mp->mpFlags & mpType) != mpNest) {                                  /* Don't chain through anything other than a nested pmap */
                        mapping_drop_busy(mp);                                                          /* We have everything we need from the mapping */
-                       mp = 0;                                                                                         /* Set not found */
+                       mp = NULL;                                                                                              /* Set not found */
                        break;
                }
 
                if(nestdepth++ > 64) {                                                                  /* Have we nested too far down? */
-                       panic("mapping_find: too many nested pmaps - va = %016llX, curva = %016llX, pmap = %08X, curpmap = %08X\n",
+                       panic("mapping_find: too many nested pmaps - va = %016llX, curva = %016llX, pmap = %p, curpmap = %p\n",
                                va, curva, pmap, curpmap);
                }
                
@@ -507,7 +515,7 @@ mapping_protect(pmap_t pmap, addr64_t va, vm_prot_t prot, addr64_t *nextva) {       /*
                        break;
                        
                default:
-                       panic("mapping_protect: hw_protect failed - rc = %d, pmap = %08X, va = %016llX\n", ret, pmap, va);
+                       panic("mapping_protect: hw_protect failed - rc = %d, pmap = %p, va = %016llX\n", ret, pmap, va);
                
        }
 
@@ -742,9 +750,10 @@ void mapping_clr_refmod(ppnum_t pa, unsigned int mask) {           /* Clears the referenc
  *             the reference bit. 
  */
 
-phys_entry_t *mapping_phys_lookup(ppnum_t pp, unsigned int *pindex) {  /* Finds the physical entry for the page */
-
-       int i;
+phys_entry_t *
+mapping_phys_lookup(ppnum_t pp, unsigned int *pindex)
+{      /* Finds the physical entry for the page */
+       unsigned int i;
        
        for(i = 0; i < pmap_mem_regions_count; i++) {                           /* Walk through the list */
                if(!(unsigned int)pmap_mem_regions[i].mrPhysTab) continue;      /* Skip any empty lists */
@@ -857,7 +866,7 @@ void mapping_adjust(void) {                                                                         /* Adjust free mappings */
        }
 
        mbn = mapCtl.mapcrel;                                                                           /* Get first pending release block */
-       mapCtl.mapcrel = 0;                                                                                     /* Dequeue them */
+       mapCtl.mapcrel = NULL;                                                                                  /* Dequeue them */
        mapCtl.mapcreln = 0;                                                                            /* Set count to 0 */
 
        hw_lock_unlock((hw_lock_t)&mapCtl.mapclock);                            /* Unlock our stuff */
@@ -955,13 +964,13 @@ void mapping_free(struct mapping *mp) {                                                   /* Release a mapping */
 
                        if(mapCtl.mapcnext == mb) {                                                     /* Are we first on the list? */
                                mapCtl.mapcnext = mb->nextblok;                                 /* Unchain us */
-                               if(!((unsigned int)mapCtl.mapcnext)) mapCtl.mapclast = 0;       /* If last, remove last */
+                               if(!((unsigned int)mapCtl.mapcnext)) mapCtl.mapclast = NULL;    /* If last, remove last */
                        }
                        else {                                                                                          /* We're not first */
                                for(mbn = mapCtl.mapcnext; mbn != 0; mbn = mbn->nextblok) {     /* Search for our block */
                                        if(mbn->nextblok == mb) break;                          /* Is the next one our's? */
                                }
-                               if(!mbn) panic("mapping_free: attempt to release mapping block (%08X) not on list\n", mp);
+                               if(!mbn) panic("mapping_free: attempt to release mapping block (%p) not on list\n", mp);
                                mbn->nextblok = mb->nextblok;                                   /* Dequeue us */
                                if(mapCtl.mapclast == mb) mapCtl.mapclast = mbn;        /* If last, make our predecessor last */
                        }
@@ -1087,15 +1096,16 @@ mapping_alloc(int lists) {                                                              /* Obtain a mapping */
                                                        case mapRtNotFnd:
                                                                break;
                                                        default:
-                                                               panic("mapping_alloc: hw_purge_map failed - pmap = %08X, va = %16llX, code = %08X\n", ckpmap, va, mp);
+                                                               panic("mapping_alloc: hw_purge_map failed - pmap = %p, va = %16llX, code = %p\n", ckpmap, va, mp);
                                                                break;
                                                }
 
-                                               if (mapRtNotFnd == ((unsigned int)mp & mapRetCode)) 
+                                               if (mapRtNotFnd == ((unsigned int)mp & mapRetCode)) {
                                                        if (do_rescan)
                                                                do_rescan = FALSE;
                                                        else
                                                                break;
+                                               }
 
                                                va = nextva;
                                        }
@@ -1159,13 +1169,13 @@ rescued:
     
     if ( !big ) {                                                                                              /* if we need a small (64-byte) mapping */
         if(!(mindx = mapalc1(mb)))                                                             /* Allocate a 1-bit slot */
-            panic("mapping_alloc - empty mapping block detected at %08X\n", mb);
+            panic("mapping_alloc - empty mapping block detected at %p\n", mb);
     }
        
        if(mindx < 0) {                                                                                         /* Did we just take the last one */
                mindx = -mindx;                                                                                 /* Make positive */
                mapCtl.mapcnext = mb->nextblok;                                                 /* Remove us from the list */
-               if(!((unsigned int)mapCtl.mapcnext)) mapCtl.mapclast = 0;       /* Removed the last one */
+               if(!((unsigned int)mapCtl.mapcnext)) mapCtl.mapclast = NULL;    /* Removed the last one */
        }
        
        mapCtl.mapcfree--;                                                                                      /* Decrement free count */
@@ -1315,7 +1325,7 @@ void mapping_free_init(vm_offset_t mbl, int perm, boolean_t locked) {
        }
        else {                                                                                                  /* Add to the free list */
                
-               mb->nextblok = 0;                                                                       /* We always add to the end */
+               mb->nextblok = NULL;                                                                    /* We always add to the end */
                mapCtl.mapcfree += MAPPERBLOK;                                          /* Bump count */
                
                if(!((unsigned int)mapCtl.mapcnext)) {                          /* First entry on list? */
@@ -1524,6 +1534,21 @@ addr64_t kvtophys(vm_offset_t va) {
 
 }
 
+/*
+ *     kvtophys64(addr)
+ *
+ *     Convert a kernel virtual address to a 64-bit physical address
+ */
+vm_map_offset_t kvtophys64(vm_map_offset_t va) {
+
+       ppnum_t pa = pmap_find_phys(kernel_pmap, (addr64_t)va);
+
+       if (!pa)
+               return 0;
+       return (((vm_map_offset_t)pa) << 12) | (va & 0xfff);
+
+}
+
 /*
  *             void ignore_zero_fault(boolean_t) - Sets up to ignore or honor any fault on 
  *             page 0 access for the current thread.
@@ -1542,9 +1567,9 @@ void ignore_zero_fault(boolean_t type) {                          /* Sets up to ignore or honor any fa
 }
 
 /*
- * nop in current ppc implementation
+ * no-op in current ppc implementation
  */
-void inval_copy_windows(__unused thread_t t)
+void inval_copy_windows(__unused thread_t th)
 {
 }
 
@@ -1566,18 +1591,19 @@ void inval_copy_windows(__unused thread_t t)
  *
  */
  
-kern_return_t hw_copypv_32(addr64_t source, addr64_t sink, unsigned int size, int which) {
+kern_return_t
+hw_copypv_32(addr64_t source, addr64_t sink, unsigned int size, int which)
+{
        vm_map_t map;
        kern_return_t ret;
-       addr64_t nextva, vaddr, paddr;
-       register mapping_t *mp;
+       addr64_t nextva, vaddr = 0, paddr;
+       mapping_t *mp = NULL;
        spl_t s;
        unsigned int lop, csize;
        int needtran, bothphys;
        unsigned int pindex;
        phys_entry_t *physent;
-       vm_prot_t prot;
+       vm_prot_t prot = 0;
        int orig_which;
 
        orig_which = which;
@@ -1615,7 +1641,7 @@ kern_return_t hw_copypv_32(addr64_t source, addr64_t sink, unsigned int size, in
                                mp = mapping_find(map->pmap, vaddr, &nextva, 1);        /* Find and busy the mapping */
                                if(!mp) {                                                       /* Was it there? */
                                        if(getPerProc()->istackptr == 0)
-                                               panic("copypv: No vaild mapping on memory %s %x", "RD", vaddr);
+                                               panic("copypv: No vaild mapping on memory %s %16llx", "RD", vaddr);
 
                                        splx(s);                                                /* Restore the interrupt level */
                                        ret = vm_fault(map, vm_map_trunc_page(vaddr), prot, FALSE, THREAD_UNINT, NULL, 0);      /* Didn't find it, try to fault it in... */
@@ -1641,7 +1667,7 @@ kern_return_t hw_copypv_32(addr64_t source, addr64_t sink, unsigned int size, in
                        
                                mapping_drop_busy(mp);                          /* Go ahead and release the mapping for now */
                                if(getPerProc()->istackptr == 0)
-                                       panic("copypv: No vaild mapping on memory %s %x", "RDWR", vaddr);
+                                       panic("copypv: No vaild mapping on memory %s %16llx", "RDWR", vaddr);
                                splx(s);                                                        /* Restore the interrupt level */
                                
                                ret = vm_fault(map, vm_map_trunc_page(vaddr), VM_PROT_READ | VM_PROT_WRITE, FALSE, THREAD_UNINT, NULL, 0);      /* check for a COW area */
@@ -1709,16 +1735,16 @@ void mapping_verify(void) {
        
        s = splhigh();                                                                                  /* Don't bother from now on */
 
-       mbn = 0;                                                                                                /* Start with none */
+       mbn = NULL;                                                                                             /* Start with none */
        for(mb = mapCtl.mapcnext; mb; mb = mb->nextblok) {              /* Walk the free chain */
                if((mappingblok_t *)(mb->mapblokflags & 0x7FFFFFFF) != mb) {    /* Is tag ok? */
-                       panic("mapping_verify: flags tag bad, free chain; mb = %08X, tag = %08X\n", mb, mb->mapblokflags);
+                       panic("mapping_verify: flags tag bad, free chain; mb = %p, tag = %08X\n", mb, mb->mapblokflags);
                }
                mbn = mb;                                                                                       /* Remember the last one */
        }
        
        if(mapCtl.mapcnext && (mapCtl.mapclast != mbn)) {               /* Do we point to the last one? */
-               panic("mapping_verify: last pointer bad; mb = %08X, mapclast = %08X\n", mb, mapCtl.mapclast);
+               panic("mapping_verify: last pointer bad; mb = %p, mapclast = %p\n", mb, mapCtl.mapclast);
        }
        
        relncnt = 0;                                                                                    /* Clear count */
@@ -1746,14 +1772,14 @@ void mapping_phys_unused(ppnum_t pa) {
 
        if(!(physent->ppLink & ~(ppLock | ppFlags))) return;    /* No one else is here */
        
-       panic("mapping_phys_unused: physical page (%08X) in use, physent = %08X\n", pa, physent);
+       panic("mapping_phys_unused: physical page (%08X) in use, physent = %p\n", pa, physent);
        
 }
        
-void mapping_hibernate_flush(void)
+void
+mapping_hibernate_flush(void)
 {
-    int bank;
-    unsigned int page;
+    unsigned int page, bank;
     struct phys_entry * entry;
 
     for (bank = 0; bank < pmap_mem_regions_count; bank++)