]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/i386_vm_init.c
xnu-1699.22.81.tar.gz
[apple/xnu.git] / osfmk / i386 / i386_vm_init.c
index 301d02274f77f0851b0353bd7fde9af961f245a1..989895eb0904cde360ee093e239c97088373f031 100644 (file)
 #include <vm/vm_kern.h>
 #include <i386/pmap.h>
 #include <i386/misc_protos.h>
-#include <i386/ipl.h>
 #include <i386/cpuid.h>
 #include <mach/thread_status.h>
 #include <pexpert/i386/efi.h>
 #include <i386/i386_lowmem.h>
 #include <i386/lowglobals.h>
+#include <i386/pal_routines.h>
 
 #include <mach-o/loader.h>
 #include <libkern/kernel_mach_header.h>
 
-#if DEBUG 
-#define DBG(x...)      kprintf("DBG: " x)
-#define PRINT_PMAP_MEMORY_TABLE
-#else
-#define DBG(x...)
-#endif
-
 vm_size_t      mem_size = 0; 
-vm_offset_t    first_avail = 0;/* first after page tables */
+pmap_paddr_t   first_avail = 0;/* first after page tables */
 
 uint64_t       max_mem;        /* Size of physical memory (bytes), adjusted by maxmem */
 uint64_t        mem_actual;
 uint64_t       sane_size = 0;  /* Memory size to use for defaults calculations */
 
-#define MAXLORESERVE   ( 32 * 1024 * 1024)
+#define MAXLORESERVE   (32 * 1024 * 1024)
 
 ppnum_t                max_ppnum = 0;
 ppnum_t                lowest_lo = 0;
@@ -119,11 +112,6 @@ vm_offset_t     static_memory_end = 0;
 
 vm_offset_t    sHIB, eHIB, stext, etext, sdata, edata, end;
 
-boolean_t      kernel_text_ps_4K = TRUE;
-boolean_t      wpkernel = TRUE;
-
-extern void    *KPTphys;
-
 /*
  * _mh_execute_header is the mach_header for the currently executing kernel
  */
@@ -135,6 +123,9 @@ void *sectPRELINKB; unsigned long sectSizePRELINK;
 void *sectHIBB; unsigned long sectSizeHIB;
 void *sectINITPTB; unsigned long sectSizeINITPT;
 
+kernel_segment_command_t *segTEXT;
+kernel_section_t *cursectTEXT, *lastsectTEXT;
+
 extern uint64_t firmware_Conventional_bytes;
 extern uint64_t firmware_RuntimeServices_bytes;
 extern uint64_t firmware_ACPIReclaim_bytes;
@@ -145,6 +136,9 @@ extern uint64_t firmware_Unusable_bytes;
 extern uint64_t firmware_other_bytes;
 uint64_t firmware_MMIO_bytes;
 
+#if    DEBUG
+#define        PRINT_PMAP_MEMORY_TABLE
+#endif /* DEBUG */
 /*
  * Basic VM initialization.
  */
@@ -163,7 +157,19 @@ i386_vm_init(uint64_t      maxmem,
        unsigned int safeboot;
        ppnum_t maxpg = 0;
         uint32_t pmap_type;
+       uint32_t maxloreserve;
        uint32_t maxdmaaddr;
+       uint32_t  mbuf_reserve = 0;
+       boolean_t mbuf_override = FALSE;
+
+#if DEBUG
+       kprintf("Boot args revision: %d version: %d",
+               args->Revision, args->Version);
+       kprintf("  commandline: \"");
+       for(i=0; i<BOOT_LINE_LENGTH; i++)
+               kprintf("%c", args->CommandLine[i]);
+       kprintf("\"\n");
+#endif
 
        /*
         * Now retrieve addresses for end, edata, and etext 
@@ -185,11 +191,18 @@ i386_vm_init(uint64_t     maxmem,
        sectPRELINKB = (void *) getsegdatafromheader(
                &_mh_execute_header, "__PRELINK_TEXT", &sectSizePRELINK);
 
+       segTEXT = getsegbynamefromheader(&_mh_execute_header, "__TEXT");
+       cursectTEXT = lastsectTEXT = firstsect(segTEXT);
+       /* Discover the last TEXT section within the TEXT segment */
+       while ((cursectTEXT = nextsect(segTEXT, cursectTEXT)) != NULL) {
+               lastsectTEXT = cursectTEXT;
+       }
+
        sHIB  = (vm_offset_t) sectHIBB;
        eHIB  = (vm_offset_t) sectHIBB + sectSizeHIB;
        /* Zero-padded from ehib to stext if text is 2M-aligned */
        stext = (vm_offset_t) sectTEXTB;
-       etext = (vm_offset_t) sectTEXTB + sectSizeTEXT;
+       etext = (vm_offset_t) round_page_64(lastsectTEXT->addr + lastsectTEXT->size);
        /* Zero-padded from etext to sdata if text is 2M-aligned */
        sdata = (vm_offset_t) sectDATAB;
        edata = (vm_offset_t) sectDATAB + sectSizeDATA;
@@ -307,8 +320,10 @@ i386_vm_init(uint64_t      maxmem,
                        break;
                }
 
+#if DEBUG
                kprintf("EFI region %d: type %u/%d, base 0x%x, top 0x%x\n",
                        i, mptr->Type, pmap_type, base, top);
+#endif
 
                if (maxpg) {
                        if (base >= maxpg)
@@ -327,7 +342,8 @@ i386_vm_init(uint64_t       maxmem,
                        /*
                         * Usable memory region
                         */
-                       if (top < I386_LOWMEM_RESERVED) {
+                       if (top < I386_LOWMEM_RESERVED ||
+                           !pal_is_usable_memory(base, top)) {
                                prev_pmptr = 0;
                                continue;
                        }
@@ -408,8 +424,11 @@ i386_vm_init(uint64_t      maxmem,
                        if (prev_pmptr &&
                            pmptr->type == prev_pmptr->type &&
                            pmptr->base == pmptr->alloc &&
-                           pmptr->base == (prev_pmptr->end + 1)) {
-                               prev_pmptr->end = pmptr->end;
+                               pmptr->base == (prev_pmptr->end + 1))
+                       {
+                               if(prev_pmptr->end == prev_pmptr->alloc)
+                                       prev_pmptr->alloc = pmptr->base;
+                               prev_pmptr->end = pmptr->end;
                        } else {
                                pmap_memory_region_count++;
                                prev_pmptr = pmptr;
@@ -462,7 +481,7 @@ i386_vm_init(uint64_t       maxmem,
        sane_size = (sane_size + 128 * MB - 1) & ~((uint64_t)(128 * MB - 1));
 
        /*
-        * We cap at KERNEL_MAXMEM bytes (currently 32GB for K32, 64GB for K64).
+        * We cap at KERNEL_MAXMEM bytes (currently 32GB for K32, 96GB for K64).
         * Unless overriden by the maxmem= boot-arg
         * -- which is a non-zero maxmem argument to this function.
         */
@@ -532,9 +551,6 @@ i386_vm_init(uint64_t       maxmem,
                        max_valid_low_ppnum = (ppnum_t)(max_valid_dma_address / PAGE_SIZE);
        }
        if (avail_end >= max_valid_dma_address) {
-               uint32_t  maxloreserve;
-               uint32_t  mbuf_reserve = 0;
-               boolean_t mbuf_override = FALSE;
 
                if (!PE_parse_boot_argn("maxloreserve", &maxloreserve, sizeof (maxloreserve))) {
 
@@ -562,10 +578,12 @@ i386_vm_init(uint64_t     maxmem,
                        vm_lopage_needed = TRUE;
                }
        }
+       
        /*
         *      Initialize kernel physical map.
         *      Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
         */
+       kprintf("avail_remaining = 0x%lx\n", (unsigned long)avail_remaining);
        pmap_bootstrap(0, IA32e);
 }
 
@@ -576,6 +594,7 @@ pmap_free_pages(void)
        return (unsigned int)avail_remaining;
 }
 
+
 boolean_t pmap_next_page_reserved(ppnum_t *);
 
 /*
@@ -685,209 +704,3 @@ pmap_valid_page(
        return FALSE;
 }
 
-/*
- * Called once VM is fully initialized so that we can release unused
- * sections of low memory to the general pool.
- * Also complete the set-up of identity-mapped sections of the kernel:
- *  1) write-protect kernel text
- *  2) map kernel text using large pages if possible
- *  3) read and write-protect page zero (for K32)
- *  4) map the global page at the appropriate virtual address.
- *
- * Use of large pages
- * ------------------
- * To effectively map and write-protect all kernel text pages, the text
- * must be 2M-aligned at the base, and the data section above must also be
- * 2M-aligned. That is, there's padding below and above. This is achieved
- * through linker directives. Large pages are used only if this alignment
- * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
- * memory layout is:
- * 
- *                       :                :
- *                       |     __DATA     |
- *               sdata:  ==================  2Meg
- *                       |                |
- *                       |  zero-padding  |
- *                       |                |
- *               etext:  ------------------ 
- *                       |                |
- *                       :                :
- *                       |                |
- *                       |     __TEXT     |
- *                       |                |
- *                       :                :
- *                       |                |
- *               stext:  ==================  2Meg
- *                       |                |
- *                       |  zero-padding  |
- *                       |                |
- *               eHIB:   ------------------ 
- *                       |     __HIB      |
- *                       :                :
- *
- * Prior to changing the mapping from 4K to 2M, the zero-padding pages
- * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
- * 4K pages covering [stext,etext] are coalesced as 2M large pages.
- * The now unused level-1 PTE pages are also freed.
- */
-void
-pmap_lowmem_finalize(void)
-{
-       spl_t           spl;
-       int             i;
-
-       /* Check the kernel is linked at the expected base address */
-       if (i386_btop(kvtophys((vm_offset_t) &IdlePML4)) !=
-           I386_KERNEL_IMAGE_BASE_PAGE)
-               panic("pmap_lowmem_finalize() unexpected kernel base address");
-
-       /*
-        * Free all pages in pmap regions below the base:
-        * rdar://6332712
-        *      We can't free all the pages to VM that EFI reports available.
-        *      Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
-        *      There's also a size miscalculation here: pend is one page less
-        *      than it should be but this is not fixed to be backwards
-        *      compatible.
-        *      Due to this current EFI limitation, we take only the first
-        *      entry in the memory region table. However, the loop is retained
-        *      (with the intended termination criteria commented out) in the
-        *      hope that some day we can free all low-memory ranges.
-        *      This loop assumes the first range does not span the kernel
-        *      image base & avail_start. We skip this process on systems
-        *      with "kernel reserved" ranges, as the low memory reclamation
-        *      is handled in the initial memory map processing loop on
-        *      such systems.
-        */
-       for (i = 0;
-//          pmap_memory_regions[i].end <= I386_KERNEL_IMAGE_BASE_PAGE;
-            i < 1 && (pmap_reserved_ranges == 0);
-            i++) {
-               vm_offset_t     pbase = (vm_offset_t)i386_ptob(pmap_memory_regions[i].base);
-               vm_offset_t     pend  = (vm_offset_t)i386_ptob(pmap_memory_regions[i].end);
-//             vm_offset_t     pend  = i386_ptob(pmap_memory_regions[i].end+1);
-
-               DBG("ml_static_mfree(%p,%p) for pmap region %d\n",
-                   (void *) ml_static_ptovirt(pbase),
-                   (void *) (pend - pbase), i);
-               ml_static_mfree(ml_static_ptovirt(pbase), pend - pbase);
-       }
-
-       /*
-        * If text and data are both 2MB-aligned,
-        * we can map text with large-pages,
-        * unless the -kernel_text_ps_4K boot-arg overrides.
-        */
-       if ((stext & I386_LPGMASK) == 0 && (sdata & I386_LPGMASK) == 0) {
-               kprintf("Kernel text is 2MB aligned");
-               kernel_text_ps_4K = FALSE;
-               if (PE_parse_boot_argn("-kernel_text_ps_4K",
-                                      &kernel_text_ps_4K,
-                                      sizeof (kernel_text_ps_4K)))
-                       kprintf(" but will be mapped with 4K pages\n");
-               else
-                       kprintf(" and will be mapped with 2M pages\n");
-       }
-
-       (void) PE_parse_boot_argn("wpkernel", &wpkernel, sizeof (wpkernel));
-       if (wpkernel)
-               kprintf("Kernel text %p-%p to be write-protected\n",
-                       (void *) stext, (void *) etext);
-
-       spl = splhigh();
-
-       /*
-        * Scan over text if mappings are to be changed:
-        * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0 
-        * - Change to large-pages if possible and not overriden.
-        */
-       if (kernel_text_ps_4K && wpkernel) {
-               vm_offset_t     myva;
-               for (myva = stext; myva < etext; myva += PAGE_SIZE) {
-                       pt_entry_t     *ptep;
-
-                       ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
-                       if (ptep)
-                               pmap_store_pte(ptep, *ptep & ~INTEL_PTE_RW);
-               }
-       }
-
-       if (!kernel_text_ps_4K) {
-               vm_offset_t     myva;
-
-               /*
-                * Release zero-filled page padding used for 2M-alignment.
-                */
-               DBG("ml_static_mfree(%p,%p) for padding below text\n",
-                       (void *) eHIB, (void *) (stext - eHIB));
-               ml_static_mfree(eHIB, stext - eHIB);
-               DBG("ml_static_mfree(%p,%p) for padding above text\n",
-                       (void *) etext, (void *) (sdata - etext));
-               ml_static_mfree(etext, sdata - etext);
-
-               /*
-                * Coalesce text pages into large pages.
-                */
-               for (myva = stext; myva < sdata; myva += I386_LPGBYTES) {
-                       pt_entry_t      *ptep;
-                       vm_offset_t     pte_phys;
-                       pt_entry_t      *pdep;
-                       pt_entry_t      pde;
-
-                       pdep = pmap_pde(kernel_pmap, (vm_map_offset_t)myva);
-                       ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
-                       DBG("myva: %p pdep: %p ptep: %p\n",
-                               (void *) myva, (void *) pdep, (void *) ptep);
-                       if ((*ptep & INTEL_PTE_VALID) == 0)
-                               continue;
-                       pte_phys = (vm_offset_t)(*ptep & PG_FRAME);
-                       pde = *pdep & PTMASK;   /* page attributes from pde */
-                       pde |= INTEL_PTE_PS;    /* make it a 2M entry */
-                       pde |= pte_phys;        /* take page frame from pte */
-
-                       if (wpkernel)
-                               pde &= ~INTEL_PTE_RW;
-                       DBG("pmap_store_pte(%p,0x%llx)\n",
-                               (void *)pdep, pde);
-                       pmap_store_pte(pdep, pde);
-
-                       /*
-                        * Free the now-unused level-1 pte.
-                        * Note: ptep is a virtual address to the pte in the
-                        *   recursive map. We can't use this address to free
-                        *   the page. Instead we need to compute its address
-                        *   in the Idle PTEs in "low memory".
-                        */
-                       vm_offset_t vm_ptep = (vm_offset_t) KPTphys
-                                               + (pte_phys >> PTPGSHIFT);
-                       DBG("ml_static_mfree(%p,0x%x) for pte\n",
-                               (void *) vm_ptep, PAGE_SIZE);
-                       ml_static_mfree(vm_ptep, PAGE_SIZE);
-               }
-
-               /* Change variable read by sysctl machdep.pmap */
-               pmap_kernel_text_ps = I386_LPGBYTES;
-       }
-
-#if defined(__i386__)
-       /* no matter what,  kernel page zero is not accessible */
-       pmap_store_pte(pmap_pte(kernel_pmap, 0), INTEL_PTE_INVALID);
-#endif
-
-       /* map lowmem global page into fixed addr */
-       pt_entry_t *pte = NULL;
-       if (0 == (pte = pmap_pte(kernel_pmap,
-                                VM_MIN_KERNEL_LOADED_ADDRESS + 0x2000)))
-               panic("lowmem pte");
-       /* make sure it is defined on page boundary */
-       assert(0 == ((vm_offset_t) &lowGlo & PAGE_MASK));
-       pmap_store_pte(pte, kvtophys((vm_offset_t)&lowGlo)
-                               | INTEL_PTE_REF
-                               | INTEL_PTE_MOD
-                               | INTEL_PTE_WIRED
-                               | INTEL_PTE_VALID
-                               | INTEL_PTE_RW);
-       splx(spl);
-       flush_tlb();
-}
-