]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/pmap_x86_common.c
xnu-3789.60.24.tar.gz
[apple/xnu.git] / osfmk / i386 / pmap_x86_common.c
index af15634240c6baa7beb208394e29607505d2bb94..1bfecd7cb12bc3de9ef6c47cc47fca556a210c59 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <vm/pmap.h>
 #include <vm/vm_map.h>
+#include <vm/vm_kern.h>
 #include <kern/ledger.h>
 #include <i386/pmap_internal.h>
 
@@ -178,11 +179,10 @@ kern_return_t pmap_nest(pmap_t grand, pmap_t subord, addr64_t va_start, addr64_t
                        i += (uint32_t) NPDEPG;
                }
                else {
-                       npde = pmap_pde(subord, nstart);
+                       npde = pmap_pde(subord, vaddr);
                        if (npde == 0)
-                               panic("pmap_nest: no npde, subord %p nstart 0x%llx", subord, nstart);
+                               panic("pmap_nest: no npde, subord %p vaddr 0x%llx", subord, vaddr);
                        tpde = *npde;
-                       nstart += NBPDE;
                        pde = pmap_pde(grand, vaddr);
                        if ((0 == pde) && cpu_64bit) {
                                PMAP_UNLOCK(grand);
@@ -405,7 +405,7 @@ pmap_update_cache_attributes_locked(ppnum_t pn, unsigned attributes) {
 
                do {
                        pmap = pv_e->pmap;
-                       vaddr = pv_e->va;
+                       vaddr = PVE_VA(pv_e);
                        ptep = pmap_pte(pmap, vaddr);
                        
                        if (0 == ptep)
@@ -454,7 +454,7 @@ void x86_filter_TLB_coherency_interrupts(boolean_t dofilter) {
 
 void
 pmap_enter(
-       register pmap_t         pmap,
+       pmap_t          pmap,
        vm_map_offset_t         vaddr,
        ppnum_t                 pn,
        vm_prot_t               prot,
@@ -468,7 +468,7 @@ pmap_enter(
 
 kern_return_t
 pmap_enter_options(
-       register pmap_t         pmap,
+       pmap_t          pmap,
        vm_map_offset_t         vaddr,
        ppnum_t                 pn,
        vm_prot_t               prot,
@@ -497,6 +497,7 @@ pmap_enter_options(
        pt_entry_t              old_pte;
        kern_return_t           kr_expand;
        boolean_t               is_ept;
+       boolean_t               is_altacct;
 
        pmap_intr_assert();
 
@@ -546,7 +547,7 @@ Retry:
         *      pmap is always expanded to include enough hardware
         *      pages to map one VM page.
         */
-        if(superpage) {
+        if (superpage) {
                while ((pte = pmap64_pde(pmap, vaddr)) == PD_ENTRY_NULL) {
                        /* need room for another pde entry */
                        PMAP_UNLOCK(pmap);
@@ -590,9 +591,27 @@ Retry:
        old_pa_locked = FALSE;
 
        if (old_pa == 0 &&
-           (*pte & PTE_COMPRESSED)) {
+           PTE_IS_COMPRESSED(*pte)) {
+               /*
+                * "pmap" should be locked at this point, so this should
+                * not race with another pmap_enter() or pmap_remove_range().
+                */
+               assert(pmap != kernel_pmap);
+
                /* one less "compressed" */
                OSAddAtomic64(-1, &pmap->stats.compressed);
+               pmap_ledger_debit(pmap, task_ledgers.internal_compressed,
+                                 PAGE_SIZE);
+               if (*pte & PTE_COMPRESSED_ALT) {
+                       pmap_ledger_debit(
+                               pmap,
+                               task_ledgers.alternate_accounting_compressed,
+                               PAGE_SIZE);
+               } else {
+                       /* was part of the footprint */
+                       pmap_ledger_debit(pmap, task_ledgers.phys_footprint,
+                                         PAGE_SIZE);
+               }
                /* marker will be cleared below */
        }
 
@@ -733,6 +752,7 @@ dont_update_pte:
         */
 
        if (old_pa != (pmap_paddr_t) 0) {
+               boolean_t       was_altacct;
 
                /*
                 *      Don't do anything to pages outside valid memory here.
@@ -750,23 +770,60 @@ dont_update_pte:
                /* completely invalidate the PTE */
                pmap_store_pte(pte, 0);
 
+               if (IS_MANAGED_PAGE(pai)) {
+                       /*
+                        *      Remove the mapping from the pvlist for
+                        *      this physical page.
+                        *      We'll end up with either a rooted pv or a
+                        *      hashed pv
+                        */
+                       pvh_e = pmap_pv_remove(pmap, vaddr, (ppnum_t *) &pai, &old_pte, &was_altacct);
+               }
+
                if (IS_MANAGED_PAGE(pai)) {
                        pmap_assert(old_pa_locked == TRUE);
                        pmap_ledger_debit(pmap, task_ledgers.phys_mem, PAGE_SIZE);
-                       pmap_ledger_debit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);                        
                        assert(pmap->stats.resident_count >= 1);
                        OSAddAtomic(-1, &pmap->stats.resident_count);
                        if (pmap != kernel_pmap) {
+                               /* update pmap stats */
                                if (IS_REUSABLE_PAGE(pai)) {
-                                       assert(pmap->stats.reusable > 0);
+                                       PMAP_STATS_ASSERTF(
+                                               (pmap->stats.reusable > 0,
+                                                "reusable %d",
+                                                pmap->stats.reusable));
                                        OSAddAtomic(-1, &pmap->stats.reusable);
                                } else if (IS_INTERNAL_PAGE(pai)) {
-                                       assert(pmap->stats.internal > 0);
+                                       PMAP_STATS_ASSERTF(
+                                               (pmap->stats.internal > 0,
+                                                "internal %d",
+                                                pmap->stats.internal));
                                        OSAddAtomic(-1, &pmap->stats.internal);
                                } else {
-                                       assert(pmap->stats.external > 0);
+                                       PMAP_STATS_ASSERTF(
+                                               (pmap->stats.external > 0,
+                                                "external %d",
+                                                pmap->stats.external));
                                        OSAddAtomic(-1, &pmap->stats.external);
                                }
+
+                               /* update ledgers */
+                               if (was_altacct) {
+                                       assert(IS_INTERNAL_PAGE(pai));
+                                       pmap_ledger_debit(pmap, task_ledgers.internal, PAGE_SIZE);
+                                       pmap_ledger_debit(pmap, task_ledgers.alternate_accounting, PAGE_SIZE);
+                               } else if (IS_REUSABLE_PAGE(pai)) {
+                                       assert(!was_altacct);
+                                       assert(IS_INTERNAL_PAGE(pai));
+                                       /* was already not in phys_footprint */
+                               } else if (IS_INTERNAL_PAGE(pai)) {
+                                       assert(!was_altacct);
+                                       assert(!IS_REUSABLE_PAGE(pai));
+                                       pmap_ledger_debit(pmap, task_ledgers.internal, PAGE_SIZE);
+                                       pmap_ledger_debit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);
+                               } else {
+                                       /* not an internal page */
+                               }
                        }
                        if (iswired(*pte)) {
                                assert(pmap->stats.wired_count >= 1);
@@ -781,14 +838,6 @@ dont_update_pte:
                                pmap_phys_attributes[pai] |= ept_refmod_to_physmap(oattr);
                        }
 
-                       /*
-                        *      Remove the mapping from the pvlist for
-                        *      this physical page.
-                        *      We'll end up with either a rooted pv or a
-                        *      hashed pv
-                        */
-                       pvh_e = pmap_pv_remove(pmap, vaddr, (ppnum_t *) &pai, &old_pte);
-
                } else {
 
                        /*
@@ -833,7 +882,7 @@ dont_update_pte:
                        /*
                         *      No mappings yet, use rooted pv
                         */
-                       pv_h->va = vaddr;
+                       pv_h->va_and_flags = vaddr;
                        pv_h->pmap = pmap;
                        queue_init(&pv_h->qlink);
 
@@ -847,6 +896,14 @@ dont_update_pte:
                        } else {
                                pmap_phys_attributes[pai] &= ~PHYS_REUSABLE;
                        }
+                       if ((options & PMAP_OPTIONS_ALT_ACCT) &&
+                           IS_INTERNAL_PAGE(pai)) {
+                               pv_h->va_and_flags |= PVE_IS_ALTACCT;
+                               is_altacct = TRUE;
+                       } else {
+                               pv_h->va_and_flags &= ~PVE_IS_ALTACCT;
+                               is_altacct = FALSE;
+                       }
                } else {
                        /*
                         *      Add new pv_hashed_entry after header.
@@ -881,9 +938,17 @@ dont_update_pte:
                        if (PV_HASHED_ENTRY_NULL == pvh_e)
                                panic("Mapping alias chain exhaustion, possibly induced by numerous kernel virtual double mappings");
 
-                       pvh_e->va = vaddr;
+                       pvh_e->va_and_flags = vaddr;
                        pvh_e->pmap = pmap;
                        pvh_e->ppn = pn;
+                       if ((options & PMAP_OPTIONS_ALT_ACCT) &&
+                           IS_INTERNAL_PAGE(pai)) {
+                               pvh_e->va_and_flags |= PVE_IS_ALTACCT;
+                               is_altacct = TRUE;
+                       } else {
+                               pvh_e->va_and_flags &= ~PVE_IS_ALTACCT;
+                               is_altacct = FALSE;
+                       }
                        pv_hash_add(pvh_e, pv_h);
 
                        /*
@@ -897,12 +962,12 @@ dont_update_pte:
                 * for 'managed memory'
                 */
                pmap_ledger_credit(pmap, task_ledgers.phys_mem, PAGE_SIZE);
-               pmap_ledger_credit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);               
                OSAddAtomic(+1,  &pmap->stats.resident_count);
                if (pmap->stats.resident_count > pmap->stats.resident_max) {
                        pmap->stats.resident_max = pmap->stats.resident_count;
                }
                if (pmap != kernel_pmap) {
+                       /* update pmap stats */
                        if (IS_REUSABLE_PAGE(pai)) {
                                OSAddAtomic(+1, &pmap->stats.reusable);
                                PMAP_STATS_PEAK(pmap->stats.reusable);
@@ -913,13 +978,33 @@ dont_update_pte:
                                OSAddAtomic(+1, &pmap->stats.external);
                                PMAP_STATS_PEAK(pmap->stats.external);
                        }
+
+                       /* update ledgers */
+                       if (is_altacct) {
+                               /* internal but also alternate accounting */
+                               assert(IS_INTERNAL_PAGE(pai));
+                               pmap_ledger_credit(pmap, task_ledgers.internal, PAGE_SIZE);
+                               pmap_ledger_credit(pmap, task_ledgers.alternate_accounting, PAGE_SIZE);
+                               /* alternate accounting, so not in footprint */
+                       } else if (IS_REUSABLE_PAGE(pai)) {
+                               assert(!is_altacct);
+                               assert(IS_INTERNAL_PAGE(pai));
+                               /* internal but reusable: not in footprint */
+                       } else if (IS_INTERNAL_PAGE(pai)) {
+                               assert(!is_altacct);
+                               assert(!IS_REUSABLE_PAGE(pai));
+                               /* internal: add to footprint */
+                               pmap_ledger_credit(pmap, task_ledgers.internal, PAGE_SIZE);
+                               pmap_ledger_credit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);
+                       } else {
+                               /* not internal: not in footprint */
+                       }
                }
        } else if (last_managed_page == 0) {
                /* Account for early mappings created before "managed pages"
                 * are determined. Consider consulting the available DRAM map.
                 */
                pmap_ledger_credit(pmap, task_ledgers.phys_mem, PAGE_SIZE);
-               pmap_ledger_credit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);               
                OSAddAtomic(+1,  &pmap->stats.resident_count);
                if (pmap != kernel_pmap) {
 #if 00
@@ -1071,22 +1156,28 @@ pmap_remove_range_options(
        pv_hashed_entry_t       pvh_e;
        int                     pvh_cnt = 0;
        int                     num_removed, num_unwired, num_found, num_invalid;
-       int                     num_device, num_external, num_internal, num_reusable;
-       uint64_t                num_compressed;
+       int                     stats_external, stats_internal, stats_reusable;
+       uint64_t                stats_compressed;
+       int                     ledgers_internal, ledgers_alt_internal;
+       uint64_t                ledgers_compressed, ledgers_alt_compressed;
        ppnum_t                 pai;
        pmap_paddr_t            pa;
        vm_map_offset_t         vaddr;
        boolean_t               is_ept = is_ept_pmap(pmap);
+       boolean_t               was_altacct;
 
        num_removed = 0;
        num_unwired = 0;
        num_found   = 0;
        num_invalid = 0;
-       num_device  = 0;
-       num_external = 0;
-       num_internal = 0;
-       num_reusable = 0;
-       num_compressed = 0;
+       stats_external = 0;
+       stats_internal = 0;
+       stats_reusable = 0;
+       stats_compressed = 0;
+       ledgers_internal = 0;
+       ledgers_compressed = 0;
+       ledgers_alt_internal = 0;
+       ledgers_alt_compressed = 0;
        /* invalidate the PTEs first to "freeze" them */
        for (cpte = spte, vaddr = start_vaddr;
             cpte < epte;
@@ -1095,14 +1186,19 @@ pmap_remove_range_options(
 
                pa = pte_to_pa(p);
                if (pa == 0) {
-                       if (pmap != kernel_pmap &&
-                           (options & PMAP_OPTIONS_REMOVE) &&
-                           (p & PTE_COMPRESSED)) {
-                               /* one less "compressed" */
-                               num_compressed++;
-                               /* clear marker */
+                       if ((options & PMAP_OPTIONS_REMOVE) &&
+                           (PTE_IS_COMPRESSED(p))) {
+                               assert(pmap != kernel_pmap);
+                               /* one less "compressed"... */
+                               stats_compressed++;
+                               ledgers_compressed++;
+                               if (p & PTE_COMPRESSED_ALT) {
+                                       /* ... but it used to be "ALTACCT" */
+                                       ledgers_alt_compressed++;
+                               }
+                               /* clear marker(s) */
                                /* XXX probably does not need to be atomic! */
-                               pmap_update_pte(cpte, PTE_COMPRESSED, 0);
+                               pmap_update_pte(cpte, INTEL_PTE_COMPRESSED_MASK, 0);
                        }
                        continue;
                }
@@ -1119,7 +1215,6 @@ pmap_remove_range_options(
                         *      Just remove the mappings.
                         */
                        pmap_store_pte(cpte, 0);
-                       num_device++;
                        continue;
                }
 
@@ -1144,8 +1239,27 @@ pmap_remove_range_options(
             cpte++, vaddr += PAGE_SIZE_64) {
 
                pa = pte_to_pa(*cpte);
-               if (pa == 0)
+               if (pa == 0) {
+               check_pte_for_compressed_marker:
+                       /*
+                        * This PTE could have been replaced with a
+                        * "compressed" marker after our first "freeze"
+                        * loop above, so check again.
+                        */
+                       if ((options & PMAP_OPTIONS_REMOVE) &&
+                           (PTE_IS_COMPRESSED(*cpte))) {
+                               assert(pmap != kernel_pmap);
+                               /* one less "compressed"... */
+                               stats_compressed++;
+                               ledgers_compressed++;
+                               if (*cpte & PTE_COMPRESSED_ALT) {
+                                       /* ... but it used to be "ALTACCT" */
+                                       ledgers_alt_compressed++;
+                               }
+                               pmap_store_pte(cpte, 0);
+                       }
                        continue;
+               }
 
                pai = pa_index(pa);
 
@@ -1154,15 +1268,40 @@ pmap_remove_range_options(
                pa = pte_to_pa(*cpte);
                if (pa == 0) {
                        UNLOCK_PVH(pai);
-                       continue;
+                       goto check_pte_for_compressed_marker;
                }
+
+               /*
+                * Remove the mapping from the pvlist for this physical page.
+                */
+               pvh_e = pmap_pv_remove(pmap, vaddr, (ppnum_t *) &pai, cpte, &was_altacct);
+
                num_removed++;
+               /* update pmap stats */
                if (IS_REUSABLE_PAGE(pai)) {
-                       num_reusable++;
+                       stats_reusable++;
+               } else if (IS_INTERNAL_PAGE(pai)) {
+                       stats_internal++;
+               } else {
+                       stats_external++;
+               }
+               /* update ledgers */
+               if (was_altacct) {
+                       /* internal and alternate accounting */
+                       assert(IS_INTERNAL_PAGE(pai));
+                       ledgers_internal++;
+                       ledgers_alt_internal++;
+               } else if (IS_REUSABLE_PAGE(pai)) {
+                       /* internal but reusable */
+                       assert(!was_altacct);
+                       assert(IS_INTERNAL_PAGE(pai));
                } else if (IS_INTERNAL_PAGE(pai)) {
-                       num_internal++;
+                       /* internal */
+                       assert(!was_altacct);
+                       assert(!IS_REUSABLE_PAGE(pai));
+                       ledgers_internal++;
                } else {
-                       num_external++;
+                       /* not internal */
                }
 
                /*
@@ -1178,11 +1317,6 @@ pmap_remove_range_options(
                                ept_refmod_to_physmap((*cpte & (INTEL_EPT_REF | INTEL_EPT_MOD))) & (PHYS_MODIFIED | PHYS_REFERENCED);
                }
 
-               /*
-                * Remove the mapping from the pvlist for this physical page.
-                */
-               pvh_e = pmap_pv_remove(pmap, vaddr, (ppnum_t *) &pai, cpte);
-
                /* completely invalidate the PTE */
                pmap_store_pte(cpte, 0);
 
@@ -1211,35 +1345,72 @@ update_counts:
                panic("pmap_remove_range: resident_count");
 #endif
        pmap_ledger_debit(pmap, task_ledgers.phys_mem, machine_ptob(num_removed));
-       pmap_ledger_debit(pmap, task_ledgers.phys_footprint, machine_ptob(num_removed));        
-       assert(pmap->stats.resident_count >= num_removed);
+       PMAP_STATS_ASSERTF((pmap->stats.resident_count >= num_removed,
+                           "pmap=%p num_removed=%d stats.resident_count=%d",
+                           pmap, num_removed, pmap->stats.resident_count));
        OSAddAtomic(-num_removed,  &pmap->stats.resident_count);
 
        if (pmap != kernel_pmap) {
-#if 00
-               assert(pmap->stats.device >= num_device);
-               if (num_device)
-                       OSAddAtomic(-num_device, &pmap->stats.device);
-#endif /* 00 */
-               assert(pmap->stats.external >= num_external);
-               if (num_external)
-                       OSAddAtomic(-num_external, &pmap->stats.external);
-               assert(pmap->stats.internal >= num_internal);
-               if (num_internal)
-                       OSAddAtomic(-num_internal, &pmap->stats.internal);
-               assert(pmap->stats.reusable >= num_reusable);
-               if (num_reusable)
-                       OSAddAtomic(-num_reusable, &pmap->stats.reusable);
-               assert(pmap->stats.compressed >= num_compressed);
-               if (num_compressed)
-                       OSAddAtomic64(-num_compressed, &pmap->stats.compressed);
+               PMAP_STATS_ASSERTF((pmap->stats.external >= stats_external,
+                                   "pmap=%p stats_external=%d stats.external=%d",
+                                   pmap, stats_external, pmap->stats.external));
+               PMAP_STATS_ASSERTF((pmap->stats.internal >= stats_internal,
+                                   "pmap=%p stats_internal=%d stats.internal=%d",
+                                   pmap, stats_internal, pmap->stats.internal));
+               PMAP_STATS_ASSERTF((pmap->stats.reusable >= stats_reusable,
+                                   "pmap=%p stats_reusable=%d stats.reusable=%d",
+                                   pmap, stats_reusable, pmap->stats.reusable));
+               PMAP_STATS_ASSERTF((pmap->stats.compressed >= stats_compressed,
+                                   "pmap=%p stats_compressed=%lld, stats.compressed=%lld",
+                                   pmap, stats_compressed, pmap->stats.compressed));
+
+               /* update pmap stats */
+               if (stats_external) {
+                       OSAddAtomic(-stats_external, &pmap->stats.external);
+               }
+               if (stats_internal) {
+                       OSAddAtomic(-stats_internal, &pmap->stats.internal);
+               }
+               if (stats_reusable)
+                       OSAddAtomic(-stats_reusable, &pmap->stats.reusable);
+               if (stats_compressed)
+                       OSAddAtomic64(-stats_compressed, &pmap->stats.compressed);
+               /* update ledgers */
+               if (ledgers_internal) {
+                       pmap_ledger_debit(pmap,
+                                         task_ledgers.internal,
+                                         machine_ptob(ledgers_internal));
+               }
+               if (ledgers_compressed) {
+                       pmap_ledger_debit(pmap,
+                                         task_ledgers.internal_compressed,
+                                         machine_ptob(ledgers_compressed));
+               }
+               if (ledgers_alt_internal) {
+                       pmap_ledger_debit(pmap,
+                                         task_ledgers.alternate_accounting,
+                                         machine_ptob(ledgers_alt_internal));
+               }
+               if (ledgers_alt_compressed) {
+                       pmap_ledger_debit(pmap,
+                                         task_ledgers.alternate_accounting_compressed,
+                                         machine_ptob(ledgers_alt_compressed));
+               }
+               pmap_ledger_debit(pmap,
+                                 task_ledgers.phys_footprint,
+                                 machine_ptob((ledgers_internal -
+                                               ledgers_alt_internal) +
+                                              (ledgers_compressed -
+                                               ledgers_alt_compressed)));
        }
 
 #if TESTING
        if (pmap->stats.wired_count < num_unwired)
                panic("pmap_remove_range: wired_count");
 #endif
-       assert(pmap->stats.wired_count >= num_unwired);
+       PMAP_STATS_ASSERTF((pmap->stats.wired_count >= num_unwired,
+                           "pmap=%p num_unwired=%d stats.wired_count=%d",
+                           pmap, num_unwired, pmap->stats.wired_count));
        OSAddAtomic(-num_unwired,  &pmap->stats.wired_count);
        pmap_ledger_debit(pmap, task_ledgers.wired_mem, machine_ptob(num_unwired));
 
@@ -1466,7 +1637,7 @@ pmap_page_protect_options(
 
                pmap = pv_e->pmap;
                is_ept = is_ept_pmap(pmap);
-               vaddr = pv_e->va;
+               vaddr = PVE_VA(pv_e);
                pte = pmap_pte(pmap, vaddr);
 
                pmap_assert2((pa_index(pte_to_pa(*pte)) == pn),
@@ -1493,8 +1664,12 @@ pmap_page_protect_options(
                        if (pmap != kernel_pmap &&
                            (options & PMAP_OPTIONS_COMPRESSOR) &&
                            IS_INTERNAL_PAGE(pai)) {
-                               /* mark this PTE as having been "reclaimed" */
+                               assert(!PTE_IS_COMPRESSED(*pte));
+                               /* mark this PTE as having been "compressed" */
                                new_pte_value = PTE_COMPRESSED;
+                               if (IS_ALTACCT_PAGE(pai, pv_e)) {
+                                       new_pte_value |= PTE_COMPRESSED_ALT;
+                               }
                        } else {
                                new_pte_value = 0;
                        }
@@ -1513,37 +1688,37 @@ pmap_page_protect_options(
                                pmap_update_pte(pte, PTE_VALID_MASK(is_ept), 0);
 
                                PMAP_UPDATE_TLBS(pmap, vaddr, vaddr+PAGE_SIZE);
+                               if (!is_ept) {
+                                       pmap_phys_attributes[pai] |=
+                                               *pte & (PHYS_MODIFIED|PHYS_REFERENCED);
+                               } else {
+                                       pmap_phys_attributes[pai] |=
+                                               ept_refmod_to_physmap((*pte & (INTEL_EPT_REF | INTEL_EPT_MOD))) & (PHYS_MODIFIED | PHYS_REFERENCED);
+                               }
                                if ((options &
                                     PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED) &&
-                                   ! (pmap_phys_attributes[pai] &
-                                      PHYS_MODIFIED) &&
-                                   (*pte & PHYS_MODIFIED)) {
+                                   IS_INTERNAL_PAGE(pai) &&
+                                   (pmap_phys_attributes[pai] &
+                                    PHYS_MODIFIED)) {
                                        /*
                                         * Page is actually "modified" and
                                         * will be compressed.  Start
                                         * accounting for it as "compressed".
                                         */
+                                       assert(!(options & PMAP_OPTIONS_COMPRESSOR));
                                        options &= ~PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED;
                                        options |= PMAP_OPTIONS_COMPRESSOR;
-                                       new_pte_value = PTE_COMPRESSED;
-                               }
-                               if (!is_ept) {
-                                       pmap_phys_attributes[pai] |=
-                                               *pte & (PHYS_MODIFIED|PHYS_REFERENCED);
-                               } else {
-                                       pmap_phys_attributes[pai] |=
-                                               ept_refmod_to_physmap((*pte & (INTEL_EPT_REF | INTEL_EPT_MOD))) & (PHYS_MODIFIED | PHYS_REFERENCED);
+                                       assert(new_pte_value == 0);
+                                       if (pmap != kernel_pmap) {
+                                               new_pte_value = PTE_COMPRESSED;
+                                               if (IS_ALTACCT_PAGE(pai, pv_e)) {
+                                                       new_pte_value |= PTE_COMPRESSED_ALT;
+                                               }
+                                       }
                                }
                                pmap_store_pte(pte, new_pte_value);
                        }
 
-                       if (new_pte_value == PTE_COMPRESSED) {
-                               /* one more "compressed" page */
-                               OSAddAtomic64(+1, &pmap->stats.compressed);
-                               PMAP_STATS_PEAK(pmap->stats.compressed);
-                               pmap->stats.compressed_lifetime++;
-                       }
-
 #if TESTING
                        if (pmap->stats.resident_count < 1)
                                panic("pmap_page_protect: resident_count");
@@ -1551,17 +1726,15 @@ pmap_page_protect_options(
                        pmap_ledger_debit(pmap, task_ledgers.phys_mem, PAGE_SIZE);
                        assert(pmap->stats.resident_count >= 1);
                        OSAddAtomic(-1,  &pmap->stats.resident_count);
+
+                       /*
+                        * We only ever compress internal pages.
+                        */
                        if (options & PMAP_OPTIONS_COMPRESSOR) {
-                               /*
-                                * This removal is only being done so we can send this page to
-                                * the compressor; therefore it mustn't affect total task footprint.
-                                */
-                               pmap_ledger_credit(pmap, task_ledgers.internal_compressed, PAGE_SIZE);
-                       } else {
-                               pmap_ledger_debit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);
+                               assert(IS_INTERNAL_PAGE(pai));
                        }
-
                        if (pmap != kernel_pmap) {
+                               /* update pmap stats */
                                if (IS_REUSABLE_PAGE(pai)) {
                                        assert(pmap->stats.reusable > 0);
                                        OSAddAtomic(-1, &pmap->stats.reusable);
@@ -1572,6 +1745,59 @@ pmap_page_protect_options(
                                        assert(pmap->stats.external > 0);
                                        OSAddAtomic(-1, &pmap->stats.external);
                                }
+                               if ((options & PMAP_OPTIONS_COMPRESSOR) &&
+                                   IS_INTERNAL_PAGE(pai)) {
+                                       /* adjust "compressed" stats */
+                                       OSAddAtomic64(+1, &pmap->stats.compressed);
+                                       PMAP_STATS_PEAK(pmap->stats.compressed);
+                                       pmap->stats.compressed_lifetime++;
+                               }
+
+                               /* update ledgers */
+                               if (IS_ALTACCT_PAGE(pai, pv_e)) {
+                                       assert(IS_INTERNAL_PAGE(pai));
+                                       pmap_ledger_debit(pmap, task_ledgers.internal, PAGE_SIZE);
+                                       pmap_ledger_debit(pmap, task_ledgers.alternate_accounting, PAGE_SIZE);
+                                       if (options & PMAP_OPTIONS_COMPRESSOR) {
+                                               pmap_ledger_credit(pmap, task_ledgers.internal_compressed, PAGE_SIZE);
+                                               pmap_ledger_credit(pmap, task_ledgers.alternate_accounting_compressed, PAGE_SIZE);
+                                       }
+                               } else if (IS_REUSABLE_PAGE(pai)) {
+                                       assert(!IS_ALTACCT_PAGE(pai, pv_e));
+                                       assert(IS_INTERNAL_PAGE(pai));
+                                       if (options & PMAP_OPTIONS_COMPRESSOR) {
+                                               pmap_ledger_credit(pmap, task_ledgers.internal_compressed, PAGE_SIZE);
+                                               /* was not in footprint, but is now */
+                                               pmap_ledger_credit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);
+                                       }
+                               } else if (IS_INTERNAL_PAGE(pai)) {
+                                       assert(!IS_ALTACCT_PAGE(pai, pv_e));
+                                       assert(!IS_REUSABLE_PAGE(pai));
+                                       pmap_ledger_debit(pmap, task_ledgers.internal, PAGE_SIZE);
+                                       /*
+                                        * Update all stats related to physical
+                                        * footprint, which only deals with
+                                        * internal pages.
+                                        */
+                                       if (options & PMAP_OPTIONS_COMPRESSOR) {
+                                               /*
+                                                * This removal is only being
+                                                * done so we can send this page
+                                                * to the compressor;  therefore
+                                                * it mustn't affect total task
+                                                * footprint.
+                                                */
+                                               pmap_ledger_credit(pmap, task_ledgers.internal_compressed, PAGE_SIZE);
+                                       } else {
+                                               /*
+                                                * This internal page isn't
+                                                * going to the compressor,
+                                                * so adjust stats to keep 
+                                                * phys_footprint up to date.
+                                                */
+                                               pmap_ledger_debit(pmap, task_ledgers.phys_footprint, PAGE_SIZE);
+                                       }
+                               }
                        }
 
                        /*
@@ -1626,7 +1852,7 @@ pmap_page_protect_options(
                if (pvh_e != (pv_hashed_entry_t) pv_h) {
                        pv_hash_remove(pvh_e);
                        pv_h->pmap = pvh_e->pmap;
-                       pv_h->va = pvh_e->va;
+                       pv_h->va_and_flags = pvh_e->va_and_flags;
                        pvh_e->qlink.next = (queue_entry_t) pvh_eh;
                        pvh_eh = pvh_e;
 
@@ -1662,7 +1888,7 @@ phys_attribute_clear(
        int                     pai;
        pmap_t                  pmap;
        char                    attributes = 0;
-       boolean_t               is_internal, is_reusable, is_ept;
+       boolean_t               is_internal, is_reusable, is_altacct, is_ept;
        int                     ept_bits_to_clear;
        boolean_t               ept_keep_global_mod = FALSE;
 
@@ -1722,7 +1948,8 @@ phys_attribute_clear(
 
                        pmap = pv_e->pmap;
                        is_ept = is_ept_pmap(pmap);
-                       va = pv_e->va;
+                       is_altacct = IS_ALTACCT_PAGE(pai, pv_e);
+                       va = PVE_VA(pv_e);
                        pte_bits = 0;
 
                        if (bits) {
@@ -1786,10 +2013,23 @@ phys_attribute_clear(
                                        /* one more "internal" */
                                        OSAddAtomic(+1, &pmap->stats.internal);
                                        PMAP_STATS_PEAK(pmap->stats.internal);
+                                       assert(pmap->stats.internal > 0);
+                                       if (is_altacct) {
+                                               /* no impact on ledgers */
+                                       } else {
+                                               pmap_ledger_credit(pmap,
+                                                                  task_ledgers.internal,
+                                                                  PAGE_SIZE);
+                                               pmap_ledger_credit(
+                                                       pmap,
+                                                       task_ledgers.phys_footprint,
+                                                       PAGE_SIZE);
+                                       }
                                } else {
                                        /* one more "external" */
                                        OSAddAtomic(+1, &pmap->stats.external);
                                        PMAP_STATS_PEAK(pmap->stats.external);
+                                       assert(pmap->stats.external > 0);
                                }
                        } else if ((options & PMAP_OPTIONS_SET_REUSABLE) &&
                                   !is_reusable &&
@@ -1797,10 +2037,22 @@ phys_attribute_clear(
                                /* one more "reusable" */
                                OSAddAtomic(+1, &pmap->stats.reusable);
                                PMAP_STATS_PEAK(pmap->stats.reusable);
+                               assert(pmap->stats.reusable > 0);
                                if (is_internal) {
                                        /* one less "internal" */
                                        assert(pmap->stats.internal > 0);
                                        OSAddAtomic(-1, &pmap->stats.internal);
+                                       if (is_altacct) {
+                                               /* no impact on footprint */
+                                       } else {
+                                               pmap_ledger_debit(pmap,
+                                                                 task_ledgers.internal,
+                                                                 PAGE_SIZE);
+                                               pmap_ledger_debit(
+                                                       pmap,
+                                                       task_ledgers.phys_footprint,
+                                                       PAGE_SIZE);
+                                       }
                                } else {
                                        /* one less "external" */
                                        assert(pmap->stats.external > 0);
@@ -1905,7 +2157,7 @@ phys_attribute_test(
 
                        pmap = pv_e->pmap;
                        is_ept = is_ept_pmap(pmap);
-                       va = pv_e->va;
+                       va = PVE_VA(pv_e);
                        /*
                         * pick up modify and/or reference bits from mapping
                         */
@@ -2109,12 +2361,81 @@ pmap_query_resident(
        return resident_bytes;
 }
 
-#if MACH_ASSERT
+kern_return_t
+pmap_query_page_info(
+       pmap_t          pmap,
+       vm_map_offset_t va,
+       int             *disp_p)
+{
+       int             disp;
+       boolean_t       is_ept;
+       pmap_paddr_t    pa;
+       ppnum_t         pai;
+       pd_entry_t      *pde;
+       pt_entry_t      *pte;
+
+       pmap_intr_assert();
+       if (pmap == PMAP_NULL || pmap == kernel_pmap) {
+               *disp_p = 0;
+               return KERN_INVALID_ARGUMENT;
+       }
+
+       disp = 0;
+       is_ept = is_ept_pmap(pmap);
+
+       PMAP_LOCK(pmap);
+
+       pde = pmap_pde(pmap, va);
+       if (!pde ||
+           !(*pde & PTE_VALID_MASK(is_ept)) ||
+           (*pde & PTE_PS)) {
+               goto done;
+       }
+
+       pte = pmap_pte(pmap, va);
+       if (pte == PT_ENTRY_NULL) {
+               goto done;
+       }
+
+       pa = pte_to_pa(*pte);
+       if (pa == 0) {
+               if (PTE_IS_COMPRESSED(*pte)) {
+                       disp |= PMAP_QUERY_PAGE_COMPRESSED;
+                       if (*pte & PTE_COMPRESSED_ALT) {
+                               disp |= PMAP_QUERY_PAGE_COMPRESSED_ALTACCT;
+                       }
+               }
+       } else {
+               disp |= PMAP_QUERY_PAGE_PRESENT;
+               pai = pa_index(pa);
+               if (!IS_MANAGED_PAGE(pai)) {
+               } else if (pmap_pv_is_altacct(pmap, va, pai)) {
+                       assert(IS_INTERNAL_PAGE(pai));
+                       disp |= PMAP_QUERY_PAGE_INTERNAL;
+                       disp |= PMAP_QUERY_PAGE_ALTACCT;
+               } else if (IS_REUSABLE_PAGE(pai)) {
+                       disp |= PMAP_QUERY_PAGE_REUSABLE;
+               } else if (IS_INTERNAL_PAGE(pai)) {
+                       disp |= PMAP_QUERY_PAGE_INTERNAL;
+               }
+       }
+
+done:
+       PMAP_UNLOCK(pmap);
+       *disp_p = disp;
+       return KERN_SUCCESS;
+}
+
+#if DEBUG || DEVELOPMENT
+void
+kernel_pmap_lock(void)
+{
+    PMAP_LOCK(kernel_pmap);
+}
+
 void
-pmap_set_process(
-       __unused pmap_t pmap,
-       __unused int pid,
-       __unused char *procname)
+kernel_pmap_unlock(void)
 {
+    PMAP_UNLOCK(kernel_pmap);
 }
-#endif /* MACH_ASSERT */
+#endif /* DEBUG || DEVELOPMENT */