]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/vm/vm_pageout.c
xnu-2782.30.5.tar.gz
[apple/xnu.git] / osfmk / vm / vm_pageout.c
index 2bfd6e7a5967f705aeed9fd2cb99f4db0185eb9a..080ffb5e732cadf5cbecaae6a6ae64e12d7dbfe2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
@@ -68,7 +68,6 @@
 #include <debug.h>
 #include <mach_pagemap.h>
 #include <mach_cluster_stats.h>
-#include <advisory_pageout.h>
 
 #include <mach/mach_types.h>
 #include <mach/memory_object.h>
@@ -95,6 +94,7 @@
 #include <machine/commpage.h>
 
 #include <vm/pmap.h>
+#include <vm/vm_compressor_pager.h>
 #include <vm/vm_fault.h>
 #include <vm/vm_map.h>
 #include <vm/vm_object.h>
 #include <vm/memory_object.h>
 #include <vm/vm_purgeable_internal.h>
 #include <vm/vm_shared_region.h>
+#include <vm/vm_compressor.h>
+
+#if CONFIG_PHANTOM_CACHE
+#include <vm/vm_phantom_cache.h>
+#endif
 /*
  * ENCRYPTED SWAP:
  */
@@ -116,21 +121,36 @@ extern int cs_debug;
 #include <libkern/OSDebug.h>
 #endif
 
+extern void m_drain(void);
+
 #if VM_PRESSURE_EVENTS
+extern unsigned int memorystatus_available_pages;
+extern unsigned int memorystatus_available_pages_pressure;
+extern unsigned int memorystatus_available_pages_critical;
+extern unsigned int memorystatus_frozen_count;
+extern unsigned int memorystatus_suspended_count;
+
+extern vm_pressure_level_t memorystatus_vm_pressure_level;
+int memorystatus_purge_on_warning = 2;
+int memorystatus_purge_on_urgent = 5;
+int memorystatus_purge_on_critical = 8;
+
+void vm_pressure_response(void);
+boolean_t vm_pressure_thread_running = FALSE;
 extern void consider_vm_pressure_events(void);
-#endif
+
+#define MEMORYSTATUS_SUSPENDED_THRESHOLD  4
+#endif /* VM_PRESSURE_EVENTS */
+
+boolean_t      vm_pressure_changed = FALSE;
 
 #ifndef VM_PAGEOUT_BURST_ACTIVE_THROTTLE   /* maximum iterations of the active queue to move pages to inactive */
 #define VM_PAGEOUT_BURST_ACTIVE_THROTTLE  100
 #endif
 
 #ifndef VM_PAGEOUT_BURST_INACTIVE_THROTTLE  /* maximum iterations of the inactive queue w/o stealing/cleaning a page */
-#ifdef CONFIG_EMBEDDED
-#define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 1024
-#else
 #define VM_PAGEOUT_BURST_INACTIVE_THROTTLE 4096
 #endif
-#endif
 
 #ifndef VM_PAGEOUT_DEADLOCK_RELIEF
 #define VM_PAGEOUT_DEADLOCK_RELIEF 100 /* number of pages to move to break deadlock */
@@ -145,7 +165,7 @@ extern void consider_vm_pressure_events(void);
 #endif /* VM_PAGEOUT_LAUNDRY_MAX */
 
 #ifndef        VM_PAGEOUT_BURST_WAIT
-#define        VM_PAGEOUT_BURST_WAIT   30      /* milliseconds */
+#define        VM_PAGEOUT_BURST_WAIT   10      /* milliseconds */
 #endif /* VM_PAGEOUT_BURST_WAIT */
 
 #ifndef        VM_PAGEOUT_EMPTY_WAIT
@@ -160,6 +180,10 @@ extern void consider_vm_pressure_events(void);
 #define VM_PAGEOUT_IDLE_WAIT   10      /* milliseconds */
 #endif /* VM_PAGEOUT_IDLE_WAIT */
 
+#ifndef        VM_PAGEOUT_SWAP_WAIT
+#define VM_PAGEOUT_SWAP_WAIT   50      /* milliseconds */
+#endif /* VM_PAGEOUT_SWAP_WAIT */
+
 #ifndef VM_PAGEOUT_PRESSURE_PAGES_CONSIDERED
 #define VM_PAGEOUT_PRESSURE_PAGES_CONSIDERED           1000    /* maximum pages considered before we issue a pressure event */
 #endif /* VM_PAGEOUT_PRESSURE_PAGES_CONSIDERED */
@@ -203,30 +227,22 @@ unsigned int      vm_page_speculative_percentage = 5;
  */
 
 #ifndef        VM_PAGE_FREE_TARGET
-#ifdef CONFIG_EMBEDDED
-#define        VM_PAGE_FREE_TARGET(free)       (15 + (free) / 100)
-#else
 #define        VM_PAGE_FREE_TARGET(free)       (15 + (free) / 80)
-#endif
 #endif /* VM_PAGE_FREE_TARGET */
 
+
 /*
  *     The pageout daemon always starts running once vm_page_free_count
  *     falls below vm_page_free_min.
  */
 
 #ifndef        VM_PAGE_FREE_MIN
-#ifdef CONFIG_EMBEDDED
-#define        VM_PAGE_FREE_MIN(free)          (10 + (free) / 200)
-#else
 #define        VM_PAGE_FREE_MIN(free)          (10 + (free) / 100)
-#endif
 #endif /* VM_PAGE_FREE_MIN */
 
-#define VM_PAGE_FREE_RESERVED_LIMIT    100
-#define VM_PAGE_FREE_MIN_LIMIT         1500
-#define VM_PAGE_FREE_TARGET_LIMIT      2000
-
+#define VM_PAGE_FREE_RESERVED_LIMIT    1700
+#define VM_PAGE_FREE_MIN_LIMIT         3500
+#define VM_PAGE_FREE_TARGET_LIMIT      4000
 
 /*
  *     When vm_page_free_count falls below vm_page_free_reserved,
@@ -251,11 +267,7 @@ unsigned int       vm_page_speculative_percentage = 5;
  */
 #define VM_PAGE_REACTIVATE_LIMIT_MAX 20000
 #ifndef        VM_PAGE_REACTIVATE_LIMIT
-#ifdef CONFIG_EMBEDDED
-#define        VM_PAGE_REACTIVATE_LIMIT(avail) (VM_PAGE_INACTIVE_TARGET(avail) / 2)
-#else
 #define        VM_PAGE_REACTIVATE_LIMIT(avail) (MAX((avail) * 1 / 20,VM_PAGE_REACTIVATE_LIMIT_MAX))
-#endif
 #endif /* VM_PAGE_REACTIVATE_LIMIT */
 #define VM_PAGEOUT_INACTIVE_FORCE_RECLAIM      100
 
@@ -274,12 +286,26 @@ unsigned int      vm_pageout_scan_event_counter = 0;
 /*
  * Forward declarations for internal routines.
  */
+struct cq {
+       struct vm_pageout_queue *q;
+       void                    *current_chead;
+       char                    *scratch_buf;
+};
+
 
-static void vm_pressure_thread(void);
+#if VM_PRESSURE_EVENTS
+void vm_pressure_thread(void);
+
+boolean_t VM_PRESSURE_NORMAL_TO_WARNING(void);
+boolean_t VM_PRESSURE_WARNING_TO_CRITICAL(void);
+
+boolean_t VM_PRESSURE_WARNING_TO_NORMAL(void);
+boolean_t VM_PRESSURE_CRITICAL_TO_WARNING(void);
+#endif
 static void vm_pageout_garbage_collect(int);
 static void vm_pageout_iothread_continue(struct vm_pageout_queue *);
 static void vm_pageout_iothread_external(void);
-static void vm_pageout_iothread_internal(void);
+static void vm_pageout_iothread_internal(struct cq *cq);
 static void vm_pageout_adjust_io_throttles(struct vm_pageout_queue *, struct vm_pageout_queue *, boolean_t);
 
 extern void vm_pageout_continue(void);
@@ -291,6 +317,7 @@ static thread_t     vm_pageout_internal_iothread = THREAD_NULL;
 unsigned int vm_pageout_reserved_internal = 0;
 unsigned int vm_pageout_reserved_really = 0;
 
+unsigned int vm_pageout_swap_wait = 0;
 unsigned int vm_pageout_idle_wait = 0;         /* milliseconds */
 unsigned int vm_pageout_empty_wait = 0;                /* milliseconds */
 unsigned int vm_pageout_burst_wait = 0;                /* milliseconds */
@@ -362,20 +389,15 @@ unsigned int vm_pageout_scan_inactive_throttled_external = 0;
 unsigned int vm_pageout_scan_throttle = 0;                     /* debugging */
 unsigned int vm_pageout_scan_burst_throttle = 0;               /* debugging */
 unsigned int vm_pageout_scan_empty_throttle = 0;               /* debugging */
+unsigned int vm_pageout_scan_swap_throttle = 0;                /* debugging */
 unsigned int vm_pageout_scan_deadlock_detected = 0;            /* debugging */
 unsigned int vm_pageout_scan_active_throttle_success = 0;      /* debugging */
 unsigned int vm_pageout_scan_inactive_throttle_success = 0;    /* debugging */
-unsigned int vm_pageout_inactive_external_forced_reactivate_count = 0; /* debugging */
 unsigned int vm_pageout_inactive_external_forced_jetsam_count = 0;     /* debugging */
 unsigned int vm_page_speculative_count_drifts = 0;
 unsigned int vm_page_speculative_count_drift_max = 0;
 
 
-unsigned int vm_precleaning_aborted = 0;
-
-static boolean_t vm_pageout_need_to_refill_clean_queue = FALSE;
-static boolean_t vm_pageout_precleaning_delayed = FALSE;
-
 /*
  * Backing store throttle when BS is exhausted
  */
@@ -413,12 +435,15 @@ unsigned long vm_cs_validated_resets = 0;
 int    vm_debug_events = 0;
 
 #if CONFIG_MEMORYSTATUS
-extern int memorystatus_wakeup;
+#if !CONFIG_JETSAM
+extern boolean_t memorystatus_idle_exit_from_VM(void);
 #endif
-#if CONFIG_JETSAM
-extern int memorystatus_kill_top_proc_from_VM(void);
+extern boolean_t memorystatus_kill_on_VM_page_shortage(boolean_t async);
+extern void memorystatus_on_pageout_scan_end(void);
 #endif
 
+boolean_t      vm_page_compressions_failing = FALSE;
+
 /*
  *     Routine:        vm_backing_store_disable
  *     Purpose:
@@ -510,7 +535,7 @@ vm_pageout_object_terminate(
                 * Also decrement the burst throttle (if external).
                 */
                vm_page_lock_queues();
-               if (m->laundry)
+               if (m->pageout_queue)
                        vm_pageout_throttle_up(m);
 
                /*
@@ -810,7 +835,7 @@ struct {
  * which will page it out and attempt to clean adjacent pages
  * in the same operation.
  *
- * The page must be busy, and the object and queues locked. We will take a
+ * The object and queues must be locked. We will take a
  * paging reference to prevent deallocation or collapse when we
  * release the object lock back at the call site.  The I/O thread
  * is responsible for consuming this reference
@@ -852,12 +877,15 @@ vm_pageout_cluster(vm_page_t m, boolean_t pageout)
 
        m->pageout = pageout;
 
-       if (object->internal == TRUE)
+       if (object->internal == TRUE) {
+               if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE)
+                       m->busy = TRUE;
+
                q = &vm_pageout_queue_internal;
-       else
+       else
                q = &vm_pageout_queue_external;
 
-        /* 
+       /* 
         * pgo_laundry count is tied to the laundry bit
         */
        m->laundry = TRUE;
@@ -867,10 +895,9 @@ vm_pageout_cluster(vm_page_t m, boolean_t pageout)
        queue_enter(&q->pgo_pending, m, vm_page_t, pageq);
        
        if (q->pgo_idle == TRUE) {
-               q->pgo_idle = FALSE;
-               thread_wakeup((event_t) &q->pgo_pending);
+               q->pgo_idle = FALSE;
+               thread_wakeup((event_t) &q->pgo_pending);
        }
-
        VM_PAGE_CHECK(m);
 }
 
@@ -928,20 +955,35 @@ vm_pageout_throttle_up(
                       q->pgo_draining = FALSE;
                       thread_wakeup((event_t) (&q->pgo_laundry+1));
               }
-              if (vm_pageout_precleaning_delayed == TRUE) {
-                      /*
-                       * since the pageout scan can return on laundry congestion, wake it up this way
-                       * don't depend on pgo_throttled == TRUE to indicate that the pageout scan thread
-                       * is blocked on &q->pgo_laundry since the hibernation mechanism utilizes both
-                       * pgo_throttled and pgo_draining
-                       */
-                      vm_pageout_precleaning_delayed = FALSE;
-                      thread_wakeup((event_t)(&vm_page_free_wanted));
-              }
        }
 }
 
 
+static void
+vm_pageout_throttle_up_batch(
+       struct vm_pageout_queue *q,
+       int             batch_cnt)
+{
+#if DEBUG
+       lck_mtx_assert(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
+#endif
+
+       vm_pageout_throttle_up_count += batch_cnt;
+
+       q->pgo_laundry -= batch_cnt;
+
+       if (q->pgo_throttled == TRUE) {
+              q->pgo_throttled = FALSE;
+              thread_wakeup((event_t) &q->pgo_laundry);
+       }
+       if (q->pgo_draining == TRUE && q->pgo_laundry == 0) {
+              q->pgo_draining = FALSE;
+              thread_wakeup((event_t) (&q->pgo_laundry+1));
+       }
+}
+
+
+
 /*
  * VM memory pressure monitoring.
  *
@@ -972,6 +1014,10 @@ unsigned int vm_memory_pressure = 0;
 #define VM_PAGEOUT_STAT_AFTER(i) \
        (((i) == VM_PAGEOUT_STAT_SIZE - 1) ? 0 : (i) + 1)
 
+#if VM_PAGE_BUCKETS_CHECK
+int vm_page_buckets_check_interval = 10; /* in seconds */
+#endif /* VM_PAGE_BUCKETS_CHECK */
+
 /*
  * Called from compute_averages().
  */
@@ -981,6 +1027,14 @@ compute_memory_pressure(
 {
        unsigned int vm_pageout_next;
 
+#if VM_PAGE_BUCKETS_CHECK
+       /* check the consistency of VM page buckets at regular interval */
+       static int counter = 0;
+       if ((++counter % vm_page_buckets_check_interval) == 0) {
+               vm_page_buckets_check();
+       }
+#endif /* VM_PAGE_BUCKETS_CHECK */
+
        vm_memory_pressure =
                vm_pageout_stats[VM_PAGEOUT_STAT_BEFORE(vm_pageout_stat_now)].reclaimed;
 
@@ -1101,7 +1155,6 @@ mach_vm_pressure_monitor(
  */
 extern void vm_pageout_io_throttle(void);
 
-
 /*
  * Page States: Used below to maintain the page state
  * before it's removed from it's Q. This saved state
@@ -1113,27 +1166,28 @@ extern void vm_pageout_io_throttle(void);
 #define PAGE_STATE_INACTIVE_FIRST      4
 #define PAGE_STATE_CLEAN      5
 
-#define VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m)                                \
-       MACRO_BEGIN                                                     \
-       /*                                                              \
-        * If a "reusable" page somehow made it back into               \
-        * the active queue, it's been re-used and is not               \
-        * quite re-usable.                                             \
-        * If the VM object was "all_reusable", consider it             \
-        * as "all re-used" instead of converting it to                 \
-        * "partially re-used", which could be expensive.               \
-        */                                                             \
-       if ((m)->reusable ||                                            \
-           (m)->object->all_reusable) {                                \
-               vm_object_reuse_pages((m)->object,                      \
-                                     (m)->offset,                      \
-                                     (m)->offset + PAGE_SIZE_64,       \
-                                     FALSE);                           \
-       }                                                               \
-       MACRO_END
-
-
-#define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT        128
+
+#define VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m)                         \
+        MACRO_BEGIN                                                     \
+        /*                                                              \
+         * If a "reusable" page somehow made it back into               \
+         * the active queue, it's been re-used and is not               \
+         * quite re-usable.                                             \
+         * If the VM object was "all_reusable", consider it             \
+         * as "all re-used" instead of converting it to                 \
+         * "partially re-used", which could be expensive.               \
+         */                                                             \
+        if ((m)->reusable ||                                            \
+            (m)->object->all_reusable) {                                \
+                vm_object_reuse_pages((m)->object,                      \
+                                      (m)->offset,                      \
+                                      (m)->offset + PAGE_SIZE_64,       \
+                                      FALSE);                           \
+        }                                                               \
+        MACRO_END
+
+
+#define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT        64
 #define VM_PAGEOUT_DELAYED_UNLOCK_LIMIT_MAX    1024
 
 #define        FCS_IDLE                0
@@ -1146,7 +1200,9 @@ struct flow_control {
 };
 
 uint32_t vm_pageout_considered_page = 0;
+uint32_t vm_page_filecache_min = 0;
 
+#define ANONS_GRABBED_LIMIT    2
 
 /*
  *     vm_pageout_scan does the dirty work for the pageout daemon.
@@ -1182,9 +1238,16 @@ vm_pageout_scan(void)
        boolean_t       forced_reclaim;
        boolean_t       exceeded_burst_throttle;
        boolean_t       grab_anonymous = FALSE;
+       boolean_t       force_anonymous = FALSE;
+       int             anons_grabbed = 0;
        int             page_prev_state = 0;
        int             cache_evict_throttle = 0;
        uint32_t        vm_pageout_inactive_external_forced_reactivate_limit = 0;
+       int             force_purge = 0;
+
+#if VM_PRESSURE_EVENTS
+       vm_pressure_level_t pressure_level;
+#endif /* VM_PRESSURE_EVENTS */
 
        VM_DEBUG_EVENT(vm_pageout_scan, VM_PAGEOUT_SCAN, DBG_FUNC_START,
                       vm_pageout_speculative_clean, vm_pageout_inactive_clean,
@@ -1233,7 +1296,7 @@ vm_pageout_scan(void)
 
 Restart:
        assert(delayed_unlock!=0);
-
+       
        /*
         *      Recalculate vm_page_inactivate_target.
         */
@@ -1241,7 +1304,8 @@ Restart:
                                                          vm_page_inactive_count +
                                                          vm_page_speculative_count);
 
-       vm_page_anonymous_min = vm_page_inactive_target / 3;
+       vm_page_anonymous_min = vm_page_inactive_target / 20;
+
 
        /*
         * don't want to wake the pageout_scan thread up everytime we fall below
@@ -1265,7 +1329,7 @@ Restart:
                catch_up_count = vm_page_inactive_count + vm_page_speculative_count;
        else
                catch_up_count = 0;
-
+                   
        for (;;) {
                vm_page_t m;
 
@@ -1299,8 +1363,7 @@ Restart:
                 * Don't sweep through active queue more than the throttle
                 * which should be kept relatively low
                 */
-               active_burst_count = MIN(vm_pageout_burst_active_throttle,
-                                        vm_page_active_count);
+               active_burst_count = MIN(vm_pageout_burst_active_throttle, vm_page_active_count);
 
                VM_DEBUG_EVENT(vm_pageout_balance, VM_PAGEOUT_BALANCE, DBG_FUNC_START,
                               vm_pageout_inactive, vm_pageout_inactive_used, vm_page_free_count, local_freed);
@@ -1308,6 +1371,8 @@ Restart:
                VM_DEBUG_EVENT(vm_pageout_balance, VM_PAGEOUT_BALANCE, DBG_FUNC_NONE,
                               vm_pageout_speculative_clean, vm_pageout_inactive_clean,
                               vm_pageout_inactive_dirty_internal, vm_pageout_inactive_dirty_external);
+               memoryshot(VM_PAGEOUT_BALANCE, DBG_FUNC_START);
+
 
                while (!queue_empty(&vm_page_queue_active) && active_burst_count--) {
 
@@ -1322,31 +1387,46 @@ Restart:
 
                        DTRACE_VM2(scan, int, 1, (uint64_t *), NULL);
 
+                       /*
+                        * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise...
+                        *
+                        * a TLB flush isn't really needed here since at worst we'll miss the reference bit being
+                        * updated in the PTE if a remote processor still has this mapping cached in its TLB when the
+                        * new reference happens. If no futher references happen on the page after that remote TLB flushes
+                        * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue
+                        * by pageout_scan, which is just fine since the last reference would have happened quite far
+                        * in the past (TLB caches don't hang around for very long), and of course could just as easily
+                        * have happened before we moved the page
+                        */
+                       pmap_clear_refmod_options(m->phys_page, VM_MEM_REFERENCED, PMAP_OPTIONS_NOFLUSH, (void *)NULL);
+
                        /*
                         * The page might be absent or busy,
                         * but vm_page_deactivate can handle that.
+                        * FALSE indicates that we don't want a H/W clear reference
                         */
-                       vm_page_deactivate(m);
+                       vm_page_deactivate_internal(m, FALSE);
 
                        if (delayed_unlock++ > delayed_unlock_limit) {
 
-                               if (local_freeq) {
+                               if (local_freeq) {
                                        vm_page_unlock_queues();
                                        
                                        VM_DEBUG_EVENT(vm_pageout_freelist, VM_PAGEOUT_FREELIST, DBG_FUNC_START,
                                                       vm_page_free_count, local_freed, delayed_unlock_limit, 1);
 
-                                       vm_page_free_list(local_freeq, TRUE);
-                                       
+                                       vm_page_free_list(local_freeq, TRUE);
+                                               
                                        VM_DEBUG_EVENT(vm_pageout_freelist, VM_PAGEOUT_FREELIST, DBG_FUNC_END,
                                                       vm_page_free_count, 0, 0, 1);
 
                                        local_freeq = NULL;
                                        local_freed = 0;
                                        vm_page_lock_queues();
-                               } else
+                               } else {
                                        lck_mtx_yield(&vm_page_queue_lock);
-
+                               }
+                               
                                delayed_unlock = 1;
 
                                /*
@@ -1359,7 +1439,7 @@ Restart:
 
                VM_DEBUG_EVENT(vm_pageout_balance, VM_PAGEOUT_BALANCE, DBG_FUNC_END,
                               vm_page_active_count, vm_page_inactive_count, vm_page_speculative_count, vm_page_inactive_target);
-
+               memoryshot(VM_PAGEOUT_BALANCE, DBG_FUNC_END);
 
                /**********************************************************************
                 * above this point we're playing with the active queue
@@ -1369,9 +1449,6 @@ Restart:
 
 done_moving_active_pages:
 
-               if (vm_page_cleaned_count < VM_PAGE_CLEANED_MIN && vm_page_anonymous_count > vm_page_anonymous_min)
-                       vm_pageout_need_to_refill_clean_queue = TRUE;
-
                if (vm_page_free_count + local_freed >= vm_page_free_target) {
                        if (object != NULL) {
                                vm_object_unlock(object);
@@ -1410,7 +1487,6 @@ done_moving_active_pages:
                        vm_page_inactive_target = VM_PAGE_INACTIVE_TARGET(vm_page_active_count +
                                                                          vm_page_inactive_count +
                                                                          vm_page_speculative_count);
-#ifndef        CONFIG_EMBEDDED
                        if (((vm_page_inactive_count + vm_page_speculative_count) < vm_page_inactive_target) &&
                            !queue_empty(&vm_page_queue_active)) {
                                /*
@@ -1419,22 +1495,19 @@ done_moving_active_pages:
                                 */
                                continue;
                        }
-#endif
                        lck_mtx_lock(&vm_page_queue_free_lock);
 
                        if ((vm_page_free_count >= vm_page_free_target) &&
-                           (vm_page_cleaned_count >= VM_PAGE_CLEANED_TARGET || vm_pageout_need_to_refill_clean_queue == FALSE) &&
                            (vm_page_free_wanted == 0) && (vm_page_free_wanted_privileged == 0)) {
                                /*
                                 * done - we have met our target *and*
                                 * there is no one waiting for a page.
                                 */
-                               vm_pageout_need_to_refill_clean_queue = FALSE;
 return_from_scan:
                                assert(vm_pageout_scan_wants_object == VM_OBJECT_NULL);
 
                                VM_DEBUG_EVENT(vm_pageout_scan, VM_PAGEOUT_SCAN, DBG_FUNC_NONE,
-                                              vm_pageout_inactive, vm_pageout_inactive_used, vm_pageout_need_to_refill_clean_queue, 0);
+                                              vm_pageout_inactive, vm_pageout_inactive_used, 0, 0);
                                VM_DEBUG_EVENT(vm_pageout_scan, VM_PAGEOUT_SCAN, DBG_FUNC_END,
                                               vm_pageout_speculative_clean, vm_pageout_inactive_clean,
                                               vm_pageout_inactive_dirty_internal, vm_pageout_inactive_dirty_external);
@@ -1451,24 +1524,45 @@ return_from_scan:
                 * If the purge succeeds, go back to the top and reevalute
                 * the new memory situation.
                 */
+               
                assert (available_for_purge>=0);
-               if (available_for_purge)
-               {
+               force_purge = 0; /* no force-purging */
+
+#if VM_PRESSURE_EVENTS
+               pressure_level = memorystatus_vm_pressure_level;
+
+               if (pressure_level > kVMPressureNormal) {
+
+                       if (pressure_level >= kVMPressureCritical) {
+                               force_purge = memorystatus_purge_on_critical;
+                       } else if (pressure_level >= kVMPressureUrgent) {
+                               force_purge = memorystatus_purge_on_urgent;
+                       } else if (pressure_level >= kVMPressureWarning) {
+                               force_purge = memorystatus_purge_on_warning;
+                       }
+               }
+#endif /* VM_PRESSURE_EVENTS */
+
+               if (available_for_purge || force_purge) {
+
                        if (object != NULL) {
                                vm_object_unlock(object);
                                object = NULL;
                        }
 
-                       VM_DEBUG_EVENT(vm_pageout_purgeone, VM_PAGEOUT_PURGEONE, DBG_FUNC_START, vm_page_free_count, 0, 0, 0);
+                       memoryshot(VM_PAGEOUT_PURGEONE, DBG_FUNC_START);
 
-                       if (TRUE == vm_purgeable_object_purge_one()) {
+                       VM_DEBUG_EVENT(vm_pageout_purgeone, VM_PAGEOUT_PURGEONE, DBG_FUNC_START, vm_page_free_count, 0, 0, 0);
+                       if (vm_purgeable_object_purge_one(force_purge, C_DONT_BLOCK)) {
 
                                VM_DEBUG_EVENT(vm_pageout_purgeone, VM_PAGEOUT_PURGEONE, DBG_FUNC_END, vm_page_free_count, 0, 0, 0);
-
+                               memoryshot(VM_PAGEOUT_PURGEONE, DBG_FUNC_END);
                                continue;
                        }
                        VM_DEBUG_EVENT(vm_pageout_purgeone, VM_PAGEOUT_PURGEONE, DBG_FUNC_END, 0, 0, 0, -1);
+                       memoryshot(VM_PAGEOUT_PURGEONE, DBG_FUNC_END);
                }
+
                if (queue_empty(&sq->age_q) && vm_page_speculative_count) {
                        /*
                         * try to pull pages from the aging bins...
@@ -1555,6 +1649,7 @@ return_from_scan:
 
                                VM_DEBUG_EVENT(vm_pageout_cache_evict, VM_PAGEOUT_CACHE_EVICT, DBG_FUNC_NONE,
                                               vm_page_free_count, pages_evicted, vm_pageout_cache_evicted, 0);
+                               memoryshot(VM_PAGEOUT_CACHE_EVICT, DBG_FUNC_NONE);
 
                                /*
                                 * we just freed up to 100 pages,
@@ -1568,6 +1663,16 @@ return_from_scan:
                if  (cache_evict_throttle)
                        cache_evict_throttle--;
 
+               /*
+                * don't let the filecache_min fall below 33% of available memory...
+                *
+                * on systems w/o the compressor/swapper, the filecache is always
+                * a very large percentage of the AVAILABLE_NON_COMPRESSED_MEMORY
+                * since most (if not all) of the anonymous pages are in the
+                * throttled queue (which isn't counted as available) which
+                * effectively disables this filter
+                */
+               vm_page_filecache_min = (AVAILABLE_NON_COMPRESSED_MEMORY / 3);
 
                exceeded_burst_throttle = FALSE;
                /*
@@ -1592,6 +1697,12 @@ return_from_scan:
                        exceeded_burst_throttle = TRUE;
                        goto vm_pageout_scan_delay;
 
+               } else if (vm_page_free_count > (vm_page_free_reserved / 4) &&
+                          VM_PAGEOUT_SCAN_NEEDS_TO_THROTTLE()) {
+                       vm_pageout_scan_swap_throttle++;
+                       msecs = vm_pageout_swap_wait;
+                       goto vm_pageout_scan_delay;
+
                } else if (VM_PAGE_Q_THROTTLED(iq) && 
                                  VM_DYNAMIC_PAGING_ENABLED(memory_manager_default)) {
                        clock_sec_t sec;
@@ -1601,11 +1712,12 @@ return_from_scan:
 
                        case FCS_IDLE:
                                if ((vm_page_free_count + local_freed) < vm_page_free_target) {
-                                       if (vm_page_inactive_count - vm_page_anonymous_count > 0) {
-                                               grab_anonymous = FALSE;
+
+                                       if (vm_page_pageable_external_count > vm_page_filecache_min && !queue_empty(&vm_page_queue_inactive)) {
+                                               anons_grabbed = ANONS_GRABBED_LIMIT;
                                                goto consider_inactive;
                                        }
-                                       if ((vm_page_inactive_count + vm_page_speculative_count) < vm_page_inactive_target)
+                                       if (((vm_page_inactive_count + vm_page_speculative_count) < vm_page_inactive_target) && vm_page_active_count)
                                                continue;
                                }
 reset_deadlock_timer:
@@ -1671,8 +1783,9 @@ vm_pageout_scan_delay:
                        }
                        vm_pageout_scan_wants_object = VM_OBJECT_NULL;
 
+                       vm_page_unlock_queues();
+
                        if (local_freeq) {
-                               vm_page_unlock_queues();
 
                                VM_DEBUG_EVENT(vm_pageout_freelist, VM_PAGEOUT_FREELIST, DBG_FUNC_START,
                                               vm_page_free_count, local_freed, delayed_unlock_limit, 3);
@@ -1684,22 +1797,23 @@ vm_pageout_scan_delay:
 
                                local_freeq = NULL;
                                local_freed = 0;
-                               vm_page_lock_queues();
+                       }
+                       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE)
+                               vm_consider_waking_compactor_swapper();
 
-                               if (flow_control.state == FCS_DELAYED &&
-                                   !VM_PAGE_Q_THROTTLED(iq)) {
-                                       flow_control.state = FCS_IDLE;
-                                       goto consider_inactive;
-                               }
+                       vm_page_lock_queues();
+
+                       if (flow_control.state == FCS_DELAYED &&
+                           !VM_PAGE_Q_THROTTLED(iq)) {
+                               flow_control.state = FCS_IDLE;
+                               goto consider_inactive;
                        }
                        
                        if (vm_page_free_count >= vm_page_free_target) {
                                /*
-                                * we're here because either
+                                * we're here because
                                 *  1) someone else freed up some pages while we had
-                                *     the queues unlocked above or 
-                                *  2) we're precleaning and we haven't yet met
-                                *     our cleaned target
+                                *     the queues unlocked above
                                 * and we've hit one of the 3 conditions that 
                                 * cause us to pause the pageout scan thread
                                 *
@@ -1725,11 +1839,8 @@ vm_pageout_scan_delay:
                        }
                        lck_mtx_lock(&vm_page_queue_free_lock);
 
-                       if (vm_page_free_count >= vm_page_free_target) {
-                               if (vm_page_cleaned_count < VM_PAGE_CLEANED_TARGET) {
-                                       vm_precleaning_aborted++;
-                                       vm_pageout_precleaning_delayed = TRUE;
-                               }
+                       if (vm_page_free_count >= vm_page_free_target &&
+                           (vm_page_free_wanted == 0) && (vm_page_free_wanted_privileged == 0)) {
                                goto return_from_scan;
                        }
                        lck_mtx_unlock(&vm_page_queue_free_lock);
@@ -1769,6 +1880,9 @@ vm_pageout_scan_delay:
                                vm_pageout_scan_throttle++;
                        iq->pgo_throttled = TRUE;
 
+                       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE)
+                               vm_consider_waking_compactor_swapper();
+
                        assert_wait_timeout((event_t) &iq->pgo_laundry, THREAD_INTERRUPTIBLE, msecs, 1000*NSEC_PER_USEC);
                        counter(c_vm_pageout_scan_block++);
 
@@ -1778,11 +1892,13 @@ vm_pageout_scan_delay:
 
                        VM_DEBUG_EVENT(vm_pageout_thread_block, VM_PAGEOUT_THREAD_BLOCK, DBG_FUNC_START, 
                                       iq->pgo_laundry, iq->pgo_maxlaundry, msecs, 0);
+                       memoryshot(VM_PAGEOUT_THREAD_BLOCK, DBG_FUNC_START);
 
                        thread_block(THREAD_CONTINUE_NULL);
 
                        VM_DEBUG_EVENT(vm_pageout_thread_block, VM_PAGEOUT_THREAD_BLOCK, DBG_FUNC_END,
                                       iq->pgo_laundry, iq->pgo_maxlaundry, msecs, 0);
+                       memoryshot(VM_PAGEOUT_THREAD_BLOCK, DBG_FUNC_END);
 
                        vm_page_lock_queues();
                        delayed_unlock = 1;
@@ -1805,68 +1921,74 @@ consider_inactive:
                loop_count++;
                inactive_burst_count++;
                vm_pageout_inactive++;
-               
-               boolean_t pageout_making_free = ((vm_page_free_count + local_freed) < vm_page_free_target); /* TRUE if making free, FALSE if making clean */
+
 
                /*
                 * Choose a victim.
                 */
-               while (1) {     
+               while (1) {
                        m = NULL;
                        
                        if (VM_DYNAMIC_PAGING_ENABLED(memory_manager_default)) {
                                assert(vm_page_throttled_count == 0);
                                assert(queue_empty(&vm_page_queue_throttled));
                        }
-                       
                        /*
-                        * If we are still below the free target, try speculative
-                        * and clean queue pages.
+                        * The most eligible pages are ones we paged in speculatively,
+                        * but which have not yet been touched.
                         */
-                       if (pageout_making_free) {
-                               /*
-                                * The most eligible pages are ones we paged in speculatively,
-                                * but which have not yet been touched.
-                                */
-                               if ( !queue_empty(&sq->age_q) ) {
-                                       m = (vm_page_t) queue_first(&sq->age_q);
+                       if (!queue_empty(&sq->age_q) && force_anonymous == FALSE) {
+                               m = (vm_page_t) queue_first(&sq->age_q);
 
-                                       page_prev_state = PAGE_STATE_SPECULATIVE;
-                                       
-                                       break;
-                               }
+                               page_prev_state = PAGE_STATE_SPECULATIVE;
 
-                               /*
-                                * Try a clean-queue inactive page, if we are still trying to fill the free list.
-                                */
-                               if ( !queue_empty(&vm_page_queue_cleaned) ) {
-                                       m = (vm_page_t) queue_first(&vm_page_queue_cleaned);
+                               break;
+                       }
+                       /*
+                        * Try a clean-queue inactive page.
+                        */
+                       if (!queue_empty(&vm_page_queue_cleaned)) {
+                               m = (vm_page_t) queue_first(&vm_page_queue_cleaned);
                     
-                                       page_prev_state = PAGE_STATE_CLEAN;
+                               page_prev_state = PAGE_STATE_CLEAN;
                     
-                                       break;
-                               }
+                               break;
+                       }
+
+                       grab_anonymous = (vm_page_anonymous_count > vm_page_anonymous_min);
+
+                       if (vm_page_pageable_external_count < vm_page_filecache_min || force_anonymous == TRUE) {
+                               grab_anonymous = TRUE;
+                               anons_grabbed = 0;
+                       }
 
-                               if (grab_anonymous == FALSE || queue_empty(&vm_page_queue_anonymous)) {
+                       if (grab_anonymous == FALSE || anons_grabbed >= ANONS_GRABBED_LIMIT || queue_empty(&vm_page_queue_anonymous)) {
 
-                                       if ( !queue_empty(&vm_page_queue_inactive) ) {
-                                               m = (vm_page_t) queue_first(&vm_page_queue_inactive);
+                               if ( !queue_empty(&vm_page_queue_inactive) ) {
+                                       m = (vm_page_t) queue_first(&vm_page_queue_inactive);
                                
-                                               page_prev_state = PAGE_STATE_INACTIVE;
-                                               if (vm_pageout_need_to_refill_clean_queue == TRUE)
-                                                       grab_anonymous = TRUE;
-                                               break;
+                                       page_prev_state = PAGE_STATE_INACTIVE;
+                                       anons_grabbed = 0;
+
+                                       if (vm_page_pageable_external_count < vm_page_filecache_min) {
+                                               if ((++reactivated_this_call % 100))
+                                                       goto must_activate_page;
+                                               /*
+                                                * steal 1% of the file backed pages even if
+                                                * we are under the limit that has been set
+                                                * for a healthy filecache
+                                                */
                                        }
+                                       break;
                                }
                        }
-                       if (vm_pageout_need_to_refill_clean_queue == TRUE) {
-                               if ( !queue_empty(&vm_page_queue_anonymous) ) {
-                                       m = (vm_page_t) queue_first(&vm_page_queue_anonymous);
+                       if ( !queue_empty(&vm_page_queue_anonymous) ) {
+                               m = (vm_page_t) queue_first(&vm_page_queue_anonymous);
 
-                                       page_prev_state = PAGE_STATE_ANONYMOUS;
-                                       grab_anonymous = FALSE;
-                                       break;
-                               }
+                               page_prev_state = PAGE_STATE_ANONYMOUS;
+                               anons_grabbed++;
+
+                               break;
                        }
 
                        /*
@@ -1898,21 +2020,19 @@ consider_inactive:
                        vm_page_lock_queues();
                        delayed_unlock = 1;
 
-                       if (pageout_making_free == FALSE) {
-                               if (vm_pageout_need_to_refill_clean_queue == TRUE)
-                                       DTRACE_VM(novictimforclean);
-
-                               lck_mtx_lock(&vm_page_queue_free_lock);
-                               goto return_from_scan;
+                       force_anonymous = FALSE;
 
-                       }
                        if ((vm_page_inactive_count + vm_page_speculative_count) < vm_page_inactive_target)
                                goto Restart;
 
+                       if (!queue_empty(&sq->age_q))
+                               goto Restart;
+
                        panic("vm_pageout: no victim");
                        
                        /* NOTREACHED */
                }
+               force_anonymous = FALSE;
                
                /*
                 * we just found this page on one of our queues...
@@ -1986,18 +2106,15 @@ consider_inactive:
                                 */
                                m->object->scan_collisions++;
 
-                               if (pageout_making_free) {
-                                       if ( !queue_empty(&sq->age_q) )
-                                               m_want = (vm_page_t) queue_first(&sq->age_q);
-                                       else if (!queue_empty(&vm_page_queue_cleaned))
-                                               m_want = (vm_page_t) queue_first(&vm_page_queue_cleaned);
-                                       else if (grab_anonymous == FALSE || queue_empty(&vm_page_queue_anonymous))
-                                               m_want = (vm_page_t) queue_first(&vm_page_queue_inactive);
-                               }
-                               if (m_want == NULL && vm_pageout_need_to_refill_clean_queue == TRUE) {
-                                       if ( !queue_empty(&vm_page_queue_anonymous) )
-                                               m_want = (vm_page_t) queue_first(&vm_page_queue_anonymous);
-                               }
+                               if ( !queue_empty(&sq->age_q) )
+                                       m_want = (vm_page_t) queue_first(&sq->age_q);
+                               else if ( !queue_empty(&vm_page_queue_cleaned))
+                                       m_want = (vm_page_t) queue_first(&vm_page_queue_cleaned);
+                               else if (anons_grabbed >= ANONS_GRABBED_LIMIT || queue_empty(&vm_page_queue_anonymous))
+                                       m_want = (vm_page_t) queue_first(&vm_page_queue_inactive);
+                               else if ( !queue_empty(&vm_page_queue_anonymous))
+                                       m_want = (vm_page_t) queue_first(&vm_page_queue_anonymous);
+
                                /*
                                 * this is the next object we're going to be interested in
                                 * try to make sure its available after the mutex_yield
@@ -2131,11 +2248,10 @@ reclaim_page:
                        else
                                vm_pageout_freed_from_inactive_clean++;
 
-                       inactive_burst_count = 0;
-
                        if (page_prev_state != PAGE_STATE_SPECULATIVE)
                                vm_pageout_stats[vm_pageout_stat_now].reclaimed++;
 
+                       inactive_burst_count = 0;
                        goto done_with_inactivepage;
                }
                /*
@@ -2159,7 +2275,22 @@ reclaim_page:
                                }
                                goto reclaim_page;
                        }
-                       if (object->purgable == VM_PURGABLE_VOLATILE) {
+
+                       if (COMPRESSED_PAGER_IS_ACTIVE) {
+                               /*
+                                * With the VM compressor, the cost of
+                                * reclaiming a page is much lower (no I/O),
+                                * so if we find a "volatile" page, it's better
+                                * to let it get compressed rather than letting
+                                * it occupy a full page until it gets purged.
+                                * So no need to check for "volatile" here.
+                                */
+                       } else if (object->purgable == VM_PURGABLE_VOLATILE) {
+                               /*
+                                * Avoid cleaning a "volatile" page which might
+                                * be purged soon.
+                                */
+
                                /* if it's wired, we can't put it on our queue */
                                assert(!VM_PAGE_WIRED(m));
 
@@ -2214,11 +2345,11 @@ consider_inactive_page:
                }
                
                /*
-                *   if (m->cleaning)
+                *   if (m->cleaning && !m->pageout)
                 *      If already cleaning this page in place and it hasn't
-                *  been recently referenced, just pull off the queue.
-                *  We can leave the page mapped, and upl_commit_range
-                *  will put it on the clean queue.
+                *      been recently referenced, just pull off the queue.
+                *      We can leave the page mapped, and upl_commit_range
+                *      will put it on the clean queue.
                 *
                 *      note: if m->encrypted_cleaning == TRUE, then
                 *              m->cleaning == TRUE
@@ -2240,10 +2371,9 @@ consider_inactive_page:
                
                /*
                 * page with m->pageout and still on the queues means that an
-                * MS_INVALIDATE in progress on this page... leave it alone
+                * MS_INVALIDATE is in progress on this page... leave it alone
                 */
                if (m->pageout) {
-                       inactive_burst_count = 0;
                        goto done_with_inactivepage;
                }
                
@@ -2253,17 +2383,18 @@ consider_inactive_page:
                                reactivated_this_call++;
                                goto reactivate_page;
                        } else {
-                               inactive_burst_count = 0;
                                goto done_with_inactivepage;
                        }
                }
 
-               if (m->reference || m->dirty) {
-                       /* deal with a rogue "reusable" page */
-                       VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m);
-               }
+                if (m->reference || m->dirty) {
+                        /* deal with a rogue "reusable" page */
+                        VM_PAGEOUT_SCAN_HANDLE_REUSABLE_PAGE(m);
+                }
 
-               if (m->reference && !m->no_cache) {
+               if (!m->no_cache &&
+                   (m->reference ||
+                    (m->xpmapped && !object->internal && (vm_page_xpmapped_external_count < (vm_page_external_count / 4))))) {
                        /*
                         * The page we pulled off the inactive list has
                         * been referenced.  It is possible for other
@@ -2294,18 +2425,19 @@ reactivate_page:
                                        vm_page_deactivate(m);
                                        vm_pageout_inactive_deactivated++;
                                } else {
+must_activate_page:
                                        /*
                                         * The page was/is being used, so put back on active list.
                                         */
                                        vm_page_activate(m);
                                        VM_STAT_INCR(reactivations);
+                                       inactive_burst_count = 0;
                                }
                                
                                if (page_prev_state == PAGE_STATE_CLEAN)
                                        vm_pageout_cleaned_reactivated++;
 
                                vm_pageout_inactive_used++;
-                               inactive_burst_count = 0;
 
                                 goto done_with_inactivepage;
                        }
@@ -2369,45 +2501,51 @@ throttle_inactive:
 
                        vm_pageout_scan_reclaimed_throttled++;
 
+                       inactive_burst_count = 0;
                        goto done_with_inactivepage;
                }
                if (inactive_throttled == TRUE) {
 
-                       if (object->internal)
-                               vm_pageout_scan_inactive_throttled_internal++;
-                       else
-                               vm_pageout_scan_inactive_throttled_external++;                  
-
-                       if (page_prev_state == PAGE_STATE_SPECULATIVE)
-                               page_prev_state = PAGE_STATE_INACTIVE;
-
-                       if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default) && object->internal == FALSE) {
-                               /*
-                                * a) The external pageout queue is throttled
-                                * b) We're done with the active queue and moved on to the inactive queue
-                                * c) We start noticing dirty pages and usually we would put them at the end of the inactive queue, but,
-                                * d) We don't have a default pager, and so,
-                                * e) We push these onto the active queue in an effort to cause a re-evaluation of the active queue 
-                                *    and get back some, possibly clean, pages.
+                       if (object->internal == FALSE) {
+                                /*
+                                * we need to break up the following potential deadlock case...
+                                *  a) The external pageout thread is stuck on the truncate lock for a file that is being extended i.e. written.
+                                *  b) The thread doing the writing is waiting for pages while holding the truncate lock
+                                *  c) Most of the pages in the inactive queue belong to this file.
+                                *
+                                * we are potentially in this deadlock because...
+                                *  a) the external pageout queue is throttled
+                                *  b) we're done with the active queue and moved on to the inactive queue
+                                *  c) we've got a dirty external page
+                                *
+                                * since we don't know the reason for the external pageout queue being throttled we
+                                * must suspect that we are deadlocked, so move the current page onto the active queue
+                                * in an effort to cause a page from the active queue to 'age' to the inactive queue
                                 *
-                                * We also keep a count of the pages of this kind, since, these will be a good indicator of us being in a deadlock
-                                * on systems without a dynamic pager, where: 
-                                * a) The external pageout thread is stuck on the truncate lock for a file that is being extended i.e. written.
-                                * b) The thread doing the writing is waiting for pages while holding the truncate lock
-                                * c) Most of the pages in the inactive queue belong to this file.
+                                * if we don't have jetsam configured (i.e. we have a dynamic pager), set
+                                * 'force_anonymous' to TRUE to cause us to grab a page from the cleaned/anonymous
+                                * pool the next time we select a victim page... if we can make enough new free pages,
+                                * the deadlock will break, the external pageout queue will empty and it will no longer
+                                * be throttled
+                                *
+                                * if we have jestam configured, keep a count of the pages reactivated this way so
+                                * that we can try to find clean pages in the active/inactive queues before
+                                * deciding to jetsam a process
                                 */
+                               vm_pageout_scan_inactive_throttled_external++;                  
+
                                queue_enter(&vm_page_queue_active, m, vm_page_t, pageq);
                                m->active = TRUE;
                                vm_page_active_count++;
+                               vm_page_pageable_external_count++;
 
                                vm_pageout_adjust_io_throttles(iq, eq, FALSE);
 
-                               vm_pageout_inactive_external_forced_reactivate_count++;
+#if CONFIG_MEMORYSTATUS && CONFIG_JETSAM
                                vm_pageout_inactive_external_forced_reactivate_limit--;
 
-                               if (vm_pageout_inactive_external_forced_reactivate_limit <= 0){
+                               if (vm_pageout_inactive_external_forced_reactivate_limit <= 0) {
                                        vm_pageout_inactive_external_forced_reactivate_limit = vm_page_active_count + vm_page_inactive_count;
-#if CONFIG_JETSAM
                                        /*
                                         * Possible deadlock scenario so request jetsam action
                                         */
@@ -2415,19 +2553,32 @@ throttle_inactive:
                                        vm_object_unlock(object);
                                        object = VM_OBJECT_NULL;
                                        vm_page_unlock_queues();
+                                       
+                                       VM_DEBUG_EVENT(vm_pageout_jetsam, VM_PAGEOUT_JETSAM, DBG_FUNC_START,
+                                              vm_page_active_count, vm_page_inactive_count, vm_page_free_count, vm_page_free_count);
 
-                                       if (memorystatus_kill_top_proc_from_VM() < 0){
+                                        /* Kill first suitable process */
+                                       if (memorystatus_kill_on_VM_page_shortage(FALSE) == FALSE) {
                                                panic("vm_pageout_scan: Jetsam request failed\n");      
                                        }
+                                       
+                                       VM_DEBUG_EVENT(vm_pageout_jetsam, VM_PAGEOUT_JETSAM, DBG_FUNC_END, 0, 0, 0, 0);
 
                                        vm_pageout_inactive_external_forced_jetsam_count++;
                                        vm_page_lock_queues();  
                                        delayed_unlock = 1;
-#endif
                                }
+#else /* CONFIG_MEMORYSTATUS && CONFIG_JETSAM */
+                               force_anonymous = TRUE;
+#endif
                                inactive_burst_count = 0;
                                goto done_with_inactivepage;
                        } else {
+                               if (page_prev_state == PAGE_STATE_SPECULATIVE)
+                                       page_prev_state = PAGE_STATE_INACTIVE;
+
+                               vm_pageout_scan_inactive_throttled_internal++;
+
                                goto requeue_page;
                        }
                }
@@ -2443,37 +2594,30 @@ throttle_inactive:
                 * page was still mapped up to the pmap_disconnect
                 * and may have been dirtied at the last microsecond
                 *
-                * we also check for the page being referenced 'late'
-                * and reactivate it for that case
-                *
                 * Note that if 'pmapped' is FALSE then the page is not
                 * and has not been in any map, so there is no point calling
-                * pmap_disconnect().  m->dirty and/or m->reference could
-                * have been set in anticipation of likely usage of the page.
+                * pmap_disconnect().  m->dirty could have been set in anticipation
+                * of likely usage of the page.
                 */
                if (m->pmapped == TRUE) {
-                       refmod_state = pmap_disconnect(m->phys_page);
 
-                       if (refmod_state & VM_MEM_MODIFIED) {
-                               SET_PAGE_DIRTY(m, FALSE);
-                       }
-                       if (refmod_state & VM_MEM_REFERENCED) {
-                               
-                               /* If m->reference is already set, this page must have
-                                * already failed the reactivate_limit test, so don't
-                                * bump the counts twice.
+                       if (DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE || object->internal == FALSE) {
+                               /*
+                                * Don't count this page as going into the compressor if any of these are true:
+                                * 1) We have the dynamic pager i.e. no compressed pager
+                                * 2) Freezer enabled device with a freezer file to hold the app data i.e. no compressed pager
+                                * 3) Freezer enabled device with compressed pager backend (exclusive use) i.e. most of the VM system
+                                     (including vm_pageout_scan) has no knowledge of the compressor
+                                * 4) This page belongs to a file and hence will not be sent into the compressor
                                 */
-                               if ( ! m->reference ) {
-                                       m->reference = TRUE;
-                                       if (forced_reclaim ||
-                                           ++reactivated_this_call >= reactivate_limit)
-                                               vm_pageout_reactivation_limit_exceeded++;
-                                       else {
-                                               if (page_prev_state == PAGE_STATE_CLEAN)
-                                                       vm_pageout_cleaned_reference_reactivated++;
-                                               goto reactivate_page;
-                                       }
-                               }
+
+                               refmod_state = pmap_disconnect_options(m->phys_page, 0, NULL);
+                       } else {
+                               refmod_state = pmap_disconnect_options(m->phys_page, PMAP_OPTIONS_COMPRESSOR, NULL);
+                       }
+
+                       if (refmod_state & VM_MEM_MODIFIED) {
+                               SET_PAGE_DIRTY(m, FALSE);
                        }
                }
                /*
@@ -2495,66 +2639,16 @@ throttle_inactive:
                                else if (page_prev_state == PAGE_STATE_CLEAN)
                                        vm_pageout_cleaned_reclaimed++;
 
-                               if (m->was_dirty) {
-                                       /* page on clean queue used to be dirty; we should increment the vm_stat pageout count here */
-                                       VM_STAT_INCR(pageouts);
-                                       DTRACE_VM2(pgout, int, 1, (uint64_t *), NULL);
-                               }
                                vm_pageout_inactive_clean++;
                        }
-                       /* FYI: (!pageout_making_free) == (!m->clean_queue && !m->speculative) */
-                       if (((vm_page_free_count + local_freed) >= vm_page_free_target) && !pageout_making_free) {
-                               
-                               assert(!m->clean_queue);
-                               assert(!m->speculative);
-                               
-                               /*
-                                * we have met our free page target and this page wasn't just pulled
-                                * from the clean or speculative queues, so put it on the clean queue
-                                */
-                               if (m->reference == TRUE) {
-                                       /*
-                                        * must have come through the forced reclaim path.
-                                        * we need to clear out the reference state in this case
-                                        * so that we don't just reactivate the page when we
-                                        * find it in the clean queue based on an old reference.
-                                        * if it gets re-referenced while on the queue, then
-                                        * the reactivation is justified
-                                        */
-                                       m->reference = FALSE;
-                                       pmap_clear_reference(m->phys_page);
-                               }
-                               
-                               vm_pageout_enqueued_cleaned_from_inactive_clean++;
-                               vm_page_enqueue_cleaned(m);
-                               
-                               inactive_burst_count = 0; /* we found a usable page on the inactive queue, hooray */
-                               
-                               goto done_with_inactivepage;
 
-                       }
                        /*
                         * OK, at this point we have found a page we are going to free.
                         */
-
-#ifndef CONFIG_EMBEDDED
-
-#define VM_PRESSURE_INTERVAL_NS                250000000       /* nanoseconds; == .25 seconds */
-                       if (vm_pageout_need_to_refill_clean_queue == TRUE || page_prev_state == PAGE_STATE_CLEAN) {
-                               static uint64_t vm_pressure_last_time_ns = 0;
-                               uint64_t cur_time_ns = 0;
-                               absolutetime_to_nanoseconds(mach_absolute_time(), &cur_time_ns);
-                               if (cur_time_ns >= vm_pressure_last_time_ns + VM_PRESSURE_INTERVAL_NS) {
-                                       vm_pressure_last_time_ns = cur_time_ns;
-                                       thread_wakeup(&vm_pressure_thread);
-#if CONFIG_MEMORYSTATUS
-                                       /* Wake up idle-exit thread */
-                                       thread_wakeup((event_t)&memorystatus_wakeup);
+#if CONFIG_PHANTOM_CACHE
+                       if (!object->internal)
+                               vm_phantom_cache_add_ghost(m);
 #endif
-                               }
-                       }
-#endif /* !CONFIG_EMBEDDED */
-
                        goto reclaim_page;
                }
 
@@ -2573,6 +2667,23 @@ throttle_inactive:
 
                if (inactive_throttled == TRUE)
                        goto throttle_inactive;
+       
+#if VM_PRESSURE_EVENTS
+#if CONFIG_JETSAM
+
+               /*
+                * If Jetsam is enabled, then the sending
+                * of memory pressure notifications is handled
+                * from the same thread that takes care of high-water
+                * and other jetsams i.e. the memorystatus_thread.
+                */
+
+#else /* CONFIG_JETSAM */
+               
+               vm_pressure_response();
+
+#endif /* CONFIG_JETSAM */
+#endif /* VM_PRESSURE_EVENTS */
                
                /*
                 * do NOT set the pageout bit!
@@ -2588,19 +2699,21 @@ throttle_inactive:
                        vm_pageout_inactive_dirty_internal++;
                else
                        vm_pageout_inactive_dirty_external++;
-       
-               inactive_burst_count = 0;
+
 
 done_with_inactivepage:
+
                if (delayed_unlock++ > delayed_unlock_limit || try_failed == TRUE) {
+                       boolean_t       need_delay = TRUE;
 
                        if (object != NULL) {
                                vm_pageout_scan_wants_object = VM_OBJECT_NULL;
                                vm_object_unlock(object);
                                object = NULL;
                        }
+                       vm_page_unlock_queues();
+
                        if (local_freeq) {
-                               vm_page_unlock_queues();
 
                                VM_DEBUG_EVENT(vm_pageout_freelist, VM_PAGEOUT_FREELIST, DBG_FUNC_START,
                                               vm_page_free_count, local_freed, delayed_unlock_limit, 4);
@@ -2612,14 +2725,21 @@ done_with_inactivepage:
 
                                local_freeq = NULL;
                                local_freed = 0;
-                               vm_page_lock_queues();
-                       } else
+                               need_delay = FALSE;
+                       }
+                       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
+                               vm_consider_waking_compactor_swapper();
+                               need_delay = FALSE;
+                       }
+                       vm_page_lock_queues();
+
+                       if (need_delay == TRUE)
                                lck_mtx_yield(&vm_page_queue_lock);
 
                        delayed_unlock = 1;
                }
                vm_pageout_considered_page++;
-               
+
                /*
                 * back to top of pageout scan loop
                 */
@@ -2635,11 +2755,19 @@ vm_page_free_reserve(
 {
        int             free_after_reserve;
 
-       vm_page_free_reserved += pages;
+       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
 
-       if (vm_page_free_reserved > VM_PAGE_FREE_RESERVED_LIMIT)
-               vm_page_free_reserved = VM_PAGE_FREE_RESERVED_LIMIT;
+               if ((vm_page_free_reserved + pages + COMPRESSOR_FREE_RESERVED_LIMIT) >= (VM_PAGE_FREE_RESERVED_LIMIT + COMPRESSOR_FREE_RESERVED_LIMIT))
+                       vm_page_free_reserved = VM_PAGE_FREE_RESERVED_LIMIT + COMPRESSOR_FREE_RESERVED_LIMIT;
+               else
+                       vm_page_free_reserved += (pages + COMPRESSOR_FREE_RESERVED_LIMIT);
 
+       } else {
+               if ((vm_page_free_reserved + pages) >= VM_PAGE_FREE_RESERVED_LIMIT)
+                       vm_page_free_reserved = VM_PAGE_FREE_RESERVED_LIMIT;
+               else
+                       vm_page_free_reserved += pages;
+       }
        free_after_reserve = vm_page_free_count_init - vm_page_free_reserved;
 
        vm_page_free_min = vm_page_free_reserved +
@@ -2658,7 +2786,6 @@ vm_page_free_reserve(
                vm_page_free_target = vm_page_free_min + 5;
 
        vm_page_throttle_limit = vm_page_free_target - (vm_page_free_target / 3);
-       vm_page_creation_throttle = vm_page_free_target * 3;
 }
 
 /*
@@ -2718,7 +2845,7 @@ vm_pageout_iothread_continue(struct vm_pageout_queue *q)
 
                   q->pgo_busy = TRUE;
                   queue_remove_first(&q->pgo_pending, m, vm_page_t, pageq);
-                  if (m->object == slide_info.slide_object) {
+                  if (m->object->object_slid) {
                           panic("slid page %p not allowed on this path\n", m);
                   }
                   VM_PAGE_CHECK(m);
@@ -2896,28 +3023,452 @@ vm_pageout_iothread_continue(struct vm_pageout_queue *q)
        q->pgo_busy = FALSE;
        q->pgo_idle = TRUE;
 
-       assert_wait((event_t) q, THREAD_UNINT);
+       assert_wait((event_t) &q->pgo_pending, THREAD_UNINT);
        vm_page_unlock_queues();
 
-       thread_block_parameter((thread_continue_t)vm_pageout_iothread_continue, (void *) &q->pgo_pending);
+       thread_block_parameter((thread_continue_t)vm_pageout_iothread_continue, (void *) q);
        /*NOTREACHED*/
 }
 
 
-
 static void
-vm_pageout_adjust_io_throttles(struct vm_pageout_queue *iq, struct vm_pageout_queue *eq, boolean_t req_lowpriority)
+vm_pageout_iothread_external_continue(struct vm_pageout_queue *q)
 {
-       uint32_t        policy;
-       boolean_t       set_iq = FALSE;
-       boolean_t       set_eq = FALSE;
-       
-       if (hibernate_cleaning_in_progress == TRUE)
-               req_lowpriority = FALSE;
-
-       if (iq->pgo_inited == TRUE && iq->pgo_lowpriority != req_lowpriority)
-               set_iq = TRUE;
-
+       vm_page_t       m = NULL;
+       vm_object_t     object;
+       vm_object_offset_t offset;
+       memory_object_t pager;
+
+
+       if (vm_pageout_internal_iothread != THREAD_NULL)
+               current_thread()->options &= ~TH_OPT_VMPRIV;
+
+       vm_page_lockspin_queues();
+
+        while ( !queue_empty(&q->pgo_pending) ) {
+
+                  q->pgo_busy = TRUE;
+                  queue_remove_first(&q->pgo_pending, m, vm_page_t, pageq);
+                  if (m->object->object_slid) {
+                          panic("slid page %p not allowed on this path\n", m);
+                  }
+                  VM_PAGE_CHECK(m);
+                  m->pageout_queue = FALSE;
+                  m->pageq.next = NULL;
+                  m->pageq.prev = NULL;
+
+                  /*
+                   * grab a snapshot of the object and offset this
+                   * page is tabled in so that we can relookup this
+                   * page after we've taken the object lock - these
+                   * fields are stable while we hold the page queues lock
+                   * but as soon as we drop it, there is nothing to keep
+                   * this page in this object... we hold an activity_in_progress
+                   * on this object which will keep it from terminating
+                   */
+                  object = m->object;
+                  offset = m->offset;
+
+                  vm_page_unlock_queues();
+
+                  vm_object_lock(object);
+
+                  m = vm_page_lookup(object, offset);
+
+                  if (m == NULL ||
+                      m->busy || m->cleaning || m->pageout_queue || !m->laundry) {
+                          /*
+                           * it's either the same page that someone else has
+                           * started cleaning (or it's finished cleaning or
+                           * been put back on the pageout queue), or
+                           * the page has been freed or we have found a
+                           * new page at this offset... in all of these cases
+                           * we merely need to release the activity_in_progress
+                           * we took when we put the page on the pageout queue
+                           */
+                          vm_object_activity_end(object);
+                          vm_object_unlock(object);
+
+                          vm_page_lockspin_queues();
+                          continue;
+                  }
+                  pager = object->pager;
+
+                  if (pager == MEMORY_OBJECT_NULL) {
+                          /*
+                           * This pager has been destroyed by either
+                           * memory_object_destroy or vm_object_destroy, and
+                           * so there is nowhere for the page to go.
+                           */
+                          if (m->pageout) {
+                                  /*
+                                   * Just free the page... VM_PAGE_FREE takes
+                                   * care of cleaning up all the state...
+                                   * including doing the vm_pageout_throttle_up
+                                   */
+                                  VM_PAGE_FREE(m);
+                          } else {
+                                  vm_page_lockspin_queues();
+
+                                  vm_pageout_throttle_up(m);
+                                  vm_page_activate(m);
+                                  
+                                  vm_page_unlock_queues();
+
+                                  /*
+                                   *   And we are done with it.
+                                   */
+                          }
+                          vm_object_activity_end(object);
+                          vm_object_unlock(object);
+
+                          vm_page_lockspin_queues();
+                          continue;
+                  }
+#if 0
+                  /*
+                   * we don't hold the page queue lock
+                   * so this check isn't safe to make
+                   */
+                  VM_PAGE_CHECK(m);
+#endif
+                  /*
+                   * give back the activity_in_progress reference we
+                   * took when we queued up this page and replace it
+                   * it with a paging_in_progress reference that will
+                    * also hold the paging offset from changing and
+                    * prevent the object from terminating
+                   */
+                  vm_object_activity_end(object);
+                  vm_object_paging_begin(object);
+                  vm_object_unlock(object);
+
+                   /*
+                   * Send the data to the pager.
+                   * any pageout clustering happens there
+                   */
+                  memory_object_data_return(pager,
+                                            m->offset + object->paging_offset,
+                                            PAGE_SIZE,
+                                            NULL,
+                                            NULL,
+                                            FALSE,
+                                            FALSE,
+                                            0);
+
+                  vm_object_lock(object);
+                  vm_object_paging_end(object);
+                  vm_object_unlock(object);
+
+                  vm_pageout_io_throttle();
+
+                  vm_page_lockspin_queues();
+       }
+       q->pgo_busy = FALSE;
+       q->pgo_idle = TRUE;
+
+       assert_wait((event_t) &q->pgo_pending, THREAD_UNINT);
+       vm_page_unlock_queues();
+
+       thread_block_parameter((thread_continue_t)vm_pageout_iothread_external_continue, (void *) q);
+       /*NOTREACHED*/
+}
+
+
+uint32_t       vm_compressor_failed;
+
+static void
+vm_pageout_iothread_internal_continue(struct cq *cq)
+{
+       struct vm_pageout_queue *q;
+       vm_page_t       m = NULL;
+       vm_object_t     object;
+       memory_object_t pager;
+       boolean_t       pgo_draining;
+       vm_page_t   local_q;
+       int         local_cnt;
+       vm_page_t   local_freeq = NULL;
+       int         local_freed = 0;
+       int         local_batch_size;
+       kern_return_t   retval;
+       int             compressed_count_delta;
+
+
+       KERNEL_DEBUG(0xe040000c | DBG_FUNC_END, 0, 0, 0, 0, 0);
+
+       q = cq->q;
+       local_batch_size = q->pgo_maxlaundry / (vm_compressor_thread_count * 4);
+
+       while (TRUE) {
+
+               local_cnt = 0;
+               local_q = NULL;
+
+               KERNEL_DEBUG(0xe0400014 | DBG_FUNC_START, 0, 0, 0, 0, 0);
+       
+               vm_page_lock_queues();
+
+               KERNEL_DEBUG(0xe0400014 | DBG_FUNC_END, 0, 0, 0, 0, 0);
+
+               KERNEL_DEBUG(0xe0400018 | DBG_FUNC_START, 0, 0, 0, 0, 0);
+
+               while ( !queue_empty(&q->pgo_pending) && local_cnt <  local_batch_size) {
+
+                       queue_remove_first(&q->pgo_pending, m, vm_page_t, pageq);
+
+                       VM_PAGE_CHECK(m);
+
+                       m->pageout_queue = FALSE;
+                       m->pageq.prev = NULL;
+
+                       m->pageq.next = (queue_entry_t)local_q;
+                       local_q = m;
+                       local_cnt++;
+               }
+               if (local_q == NULL)
+                       break;
+
+               q->pgo_busy = TRUE;
+
+               if ((pgo_draining = q->pgo_draining) == FALSE) 
+                       vm_pageout_throttle_up_batch(q, local_cnt);
+
+               vm_page_unlock_queues();
+
+               KERNEL_DEBUG(0xe0400018 | DBG_FUNC_END, 0, 0, 0, 0, 0);
+
+               while (local_q) {
+               
+                       m = local_q;
+                       local_q = (vm_page_t)m->pageq.next;
+                       m->pageq.next = NULL;
+
+                       if (m->object->object_slid) {
+                               panic("slid page %p not allowed on this path\n", m);
+                       }
+
+                       object = m->object;
+                       pager = object->pager;
+
+                       if (!object->pager_initialized || pager == MEMORY_OBJECT_NULL)  {
+                               
+                               KERNEL_DEBUG(0xe0400010 | DBG_FUNC_START, object, pager, 0, 0, 0);
+
+                               vm_object_lock(object);
+
+                               /*
+                                * If there is no memory object for the page, create
+                                * one and hand it to the compression pager.
+                                */
+
+                               if (!object->pager_initialized)
+                                       vm_object_collapse(object, (vm_object_offset_t) 0, TRUE);
+                               if (!object->pager_initialized)
+                                       vm_object_compressor_pager_create(object);
+
+                               if (!object->pager_initialized) {
+                                       /*
+                                        * Still no pager for the object.
+                                        * Reactivate the page.
+                                        *
+                                        * Should only happen if there is no
+                                        * compression pager
+                                        */
+                                       m->pageout = FALSE;
+                                       m->laundry = FALSE;
+                                       PAGE_WAKEUP_DONE(m);
+
+                                       vm_page_lockspin_queues();
+                                       vm_page_activate(m);
+                                       vm_pageout_dirty_no_pager++;
+                                       vm_page_unlock_queues();
+                                       
+                                       /*
+                                        *      And we are done with it.
+                                        */
+                                       vm_object_activity_end(object);
+                                       vm_object_unlock(object);
+
+                                       continue;
+                               }
+                               pager = object->pager;
+
+                               if (pager == MEMORY_OBJECT_NULL) {
+                                       /*
+                                        * This pager has been destroyed by either
+                                        * memory_object_destroy or vm_object_destroy, and
+                                        * so there is nowhere for the page to go.
+                                        */
+                                       if (m->pageout) {
+                                               /*
+                                                * Just free the page... VM_PAGE_FREE takes
+                                                * care of cleaning up all the state...
+                                                * including doing the vm_pageout_throttle_up
+                                                */
+                                               VM_PAGE_FREE(m);
+                                       } else {
+                                               m->laundry = FALSE;
+                                               PAGE_WAKEUP_DONE(m);
+
+                                               vm_page_lockspin_queues();
+                                               vm_page_activate(m);
+                                               vm_page_unlock_queues();
+
+                                               /*
+                                                *      And we are done with it.
+                                                */
+                                       }
+                                       vm_object_activity_end(object);
+                                       vm_object_unlock(object);
+
+                                       continue;
+                               }
+                               vm_object_unlock(object);
+
+                               KERNEL_DEBUG(0xe0400010 | DBG_FUNC_END, object, pager, 0, 0, 0);
+                       }
+                       while (vm_page_free_count < (vm_page_free_reserved - COMPRESSOR_FREE_RESERVED_LIMIT)) {
+                               kern_return_t   wait_result;
+                               int             need_wakeup = 0;
+
+                               if (local_freeq) {
+                                       vm_page_free_list(local_freeq, TRUE);
+
+                                       local_freeq = NULL;
+                                       local_freed = 0;
+
+                                       continue;
+                               }
+                               lck_mtx_lock_spin(&vm_page_queue_free_lock);
+
+                               if (vm_page_free_count < (vm_page_free_reserved - COMPRESSOR_FREE_RESERVED_LIMIT)) {
+                               
+                                       if (vm_page_free_wanted_privileged++ == 0)
+                                               need_wakeup = 1;
+                                       wait_result = assert_wait((event_t)&vm_page_free_wanted_privileged, THREAD_UNINT);
+
+                                       lck_mtx_unlock(&vm_page_queue_free_lock);
+
+                                       if (need_wakeup)
+                                               thread_wakeup((event_t)&vm_page_free_wanted);
+
+                                       if (wait_result == THREAD_WAITING)
+                                               thread_block(THREAD_CONTINUE_NULL);
+                               } else
+                                       lck_mtx_unlock(&vm_page_queue_free_lock);
+                       }
+
+                       assert(object->activity_in_progress > 0);
+
+                       retval = vm_compressor_pager_put(
+                               pager,
+                               m->offset + object->paging_offset,
+                               m->phys_page,
+                               &cq->current_chead,
+                               cq->scratch_buf,
+                               &compressed_count_delta);
+
+                       vm_object_lock(object);
+                       assert(object->activity_in_progress > 0);
+
+                       assert(m->object == object);
+
+                       vm_compressor_pager_count(pager,
+                                                 compressed_count_delta,
+                                                 FALSE, /* shared_lock */
+                                                 object);
+
+                       m->laundry = FALSE;
+                       m->pageout = FALSE;
+
+                       if (retval == KERN_SUCCESS) {
+                               /*
+                                * If the object is purgeable, its owner's
+                                * purgeable ledgers will be updated in
+                                * vm_page_remove() but the page still
+                                * contributes to the owner's memory footprint,
+                                * so account for it as such.
+                                */
+                               if (object->purgable != VM_PURGABLE_DENY &&
+                                   object->vo_purgeable_owner != NULL) {
+                                       /* one more compressed purgeable page */
+                                       vm_purgeable_compressed_update(object,
+                                                                      +1);
+                               }
+
+                               vm_page_compressions_failing = FALSE;
+                               
+                               VM_STAT_INCR(compressions);
+                       
+                               if (m->tabled)
+                                       vm_page_remove(m, TRUE);
+                               vm_object_activity_end(object);
+                               vm_object_unlock(object);
+
+                               m->pageq.next = (queue_entry_t)local_freeq;
+                               local_freeq = m;
+                               local_freed++;
+
+                       } else {
+                               PAGE_WAKEUP_DONE(m);
+
+                               vm_page_lockspin_queues();
+
+                               vm_page_activate(m);
+                               vm_compressor_failed++;
+
+                               vm_page_compressions_failing = TRUE;
+
+                               vm_page_unlock_queues();
+
+                               vm_object_activity_end(object);
+                               vm_object_unlock(object);
+                       }
+               }
+               if (local_freeq) {
+                       vm_page_free_list(local_freeq, TRUE);
+                               
+                       local_freeq = NULL;
+                       local_freed = 0;
+               }
+               if (pgo_draining == TRUE) {
+                       vm_page_lockspin_queues();
+                       vm_pageout_throttle_up_batch(q, local_cnt);
+                       vm_page_unlock_queues();
+               }
+       }
+       KERNEL_DEBUG(0xe040000c | DBG_FUNC_START, 0, 0, 0, 0, 0);
+
+       /*
+        * queue lock is held and our q is empty
+        */
+       q->pgo_busy = FALSE;
+       q->pgo_idle = TRUE;
+
+       assert_wait((event_t) &q->pgo_pending, THREAD_UNINT);
+       vm_page_unlock_queues();
+
+       KERNEL_DEBUG(0xe0400018 | DBG_FUNC_END, 0, 0, 0, 0, 0);
+
+       thread_block_parameter((thread_continue_t)vm_pageout_iothread_internal_continue, (void *) cq);
+       /*NOTREACHED*/
+}
+
+
+
+static void
+vm_pageout_adjust_io_throttles(struct vm_pageout_queue *iq, struct vm_pageout_queue *eq, boolean_t req_lowpriority)
+{
+       uint32_t        policy;
+       boolean_t       set_iq = FALSE;
+       boolean_t       set_eq = FALSE;
+       
+       if (hibernate_cleaning_in_progress == TRUE)
+               req_lowpriority = FALSE;
+
+       if ((DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE) && iq->pgo_inited == TRUE && iq->pgo_lowpriority != req_lowpriority)
+               set_iq = TRUE;
+
        if (eq->pgo_inited == TRUE && eq->pgo_lowpriority != req_lowpriority)
                set_eq = TRUE;
        
@@ -2926,18 +3477,20 @@ vm_pageout_adjust_io_throttles(struct vm_pageout_queue *iq, struct vm_pageout_qu
                vm_page_unlock_queues();
 
                if (req_lowpriority == TRUE) {
-                       policy = TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_THROTTLE;
+                       policy = THROTTLE_LEVEL_PAGEOUT_THROTTLED;
                        DTRACE_VM(laundrythrottle);
                } else {
-                       policy = TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_NORMAL;
+                       policy = THROTTLE_LEVEL_PAGEOUT_UNTHROTTLED;
                        DTRACE_VM(laundryunthrottle);
                }
                if (set_iq == TRUE) {
-                       proc_apply_thread_diskacc(kernel_task, iq->pgo_tid, policy);
+                       proc_set_task_policy_thread(kernel_task, iq->pgo_tid, TASK_POLICY_EXTERNAL, TASK_POLICY_IO, policy);
+
                        iq->pgo_lowpriority = req_lowpriority;
                }
                if (set_eq == TRUE) {
-                       proc_apply_thread_diskacc(kernel_task, eq->pgo_tid, policy);
+                       proc_set_task_policy_thread(kernel_task, eq->pgo_tid, TASK_POLICY_EXTERNAL, TASK_POLICY_IO, policy);
+
                        eq->pgo_lowpriority = req_lowpriority;
                }
                vm_page_lock_queues();
@@ -2945,76 +3498,229 @@ vm_pageout_adjust_io_throttles(struct vm_pageout_queue *iq, struct vm_pageout_qu
 }
 
 
-static void
-vm_pageout_iothread_external(void)
+static void
+vm_pageout_iothread_external(void)
+{
+       thread_t        self = current_thread();
+
+       self->options |= TH_OPT_VMPRIV;
+
+       DTRACE_VM2(laundrythrottle, int, 1, (uint64_t *), NULL);        
+
+       proc_set_task_policy_thread(kernel_task, self->thread_id, TASK_POLICY_EXTERNAL,
+                                   TASK_POLICY_IO, THROTTLE_LEVEL_PAGEOUT_THROTTLED);
+
+       vm_page_lock_queues();
+
+       vm_pageout_queue_external.pgo_tid = self->thread_id;
+       vm_pageout_queue_external.pgo_lowpriority = TRUE;
+       vm_pageout_queue_external.pgo_inited = TRUE;
+
+       vm_page_unlock_queues();
+
+       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE)
+               vm_pageout_iothread_external_continue(&vm_pageout_queue_external);
+       else
+               vm_pageout_iothread_continue(&vm_pageout_queue_external);
+
+       /*NOTREACHED*/
+}
+
+
+static void
+vm_pageout_iothread_internal(struct cq *cq)
+{
+       thread_t        self = current_thread();
+
+       self->options |= TH_OPT_VMPRIV;
+
+       if (DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE) {
+               DTRACE_VM2(laundrythrottle, int, 1, (uint64_t *), NULL);        
+
+               proc_set_task_policy_thread(kernel_task, self->thread_id, TASK_POLICY_EXTERNAL,
+                                           TASK_POLICY_IO, THROTTLE_LEVEL_PAGEOUT_THROTTLED);
+       }
+       vm_page_lock_queues();
+
+       vm_pageout_queue_internal.pgo_tid = self->thread_id;
+       vm_pageout_queue_internal.pgo_lowpriority = TRUE;
+       vm_pageout_queue_internal.pgo_inited = TRUE;
+
+       vm_page_unlock_queues();
+
+       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
+               cq->q = &vm_pageout_queue_internal;
+               cq->current_chead = NULL;
+               cq->scratch_buf = kalloc(COMPRESSOR_SCRATCH_BUF_SIZE);
+
+               vm_pageout_iothread_internal_continue(cq);
+       } else
+               vm_pageout_iothread_continue(&vm_pageout_queue_internal);
+
+       /*NOTREACHED*/
+}
+
+kern_return_t
+vm_set_buffer_cleanup_callout(boolean_t (*func)(int)) 
+{
+       if (OSCompareAndSwapPtr(NULL, func, (void * volatile *) &consider_buffer_cache_collect)) {
+               return KERN_SUCCESS;
+       } else {
+               return KERN_FAILURE; /* Already set */
+       }
+}
+
+extern boolean_t       memorystatus_manual_testing_on;
+extern unsigned int    memorystatus_level;
+
+
+#if VM_PRESSURE_EVENTS
+
+boolean_t vm_pressure_events_enabled = FALSE;
+
+void
+vm_pressure_response(void)
 {
-       thread_t        self = current_thread();
 
-       self->options |= TH_OPT_VMPRIV;
+       vm_pressure_level_t     old_level = kVMPressureNormal;
+       int                     new_level = -1;
 
-       DTRACE_VM2(laundrythrottle, int, 1, (uint64_t *), NULL);        
-       proc_apply_thread_diskacc(kernel_task, self->thread_id, TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_THROTTLE);
+       uint64_t                available_memory = 0;
 
-       vm_page_lock_queues();
+       if (vm_pressure_events_enabled == FALSE)
+               return;
 
-       vm_pageout_queue_external.pgo_tid = self->thread_id;
-       vm_pageout_queue_external.pgo_lowpriority = TRUE;
-       vm_pageout_queue_external.pgo_inited = TRUE;
 
-       vm_page_unlock_queues();
+       available_memory = (((uint64_t) AVAILABLE_NON_COMPRESSED_MEMORY) * 100);
 
-       vm_pageout_iothread_continue(&vm_pageout_queue_external);
 
-       /*NOTREACHED*/
-}
+       memorystatus_level = (unsigned int) (available_memory / atop_64(max_mem));
 
-static void
-vm_pageout_iothread_internal(void)
-{
-       thread_t        self = current_thread();
+       if (memorystatus_manual_testing_on) {
+               return;
+       }
+       
+       old_level = memorystatus_vm_pressure_level;
 
-       self->options |= TH_OPT_VMPRIV;
+       switch (memorystatus_vm_pressure_level) {
 
-       DTRACE_VM2(laundrythrottle, int, 1, (uint64_t *), NULL);
-       proc_apply_thread_diskacc(kernel_task, self->thread_id, TASK_POLICY_HWACCESS_DISK_ATTRIBUTE_THROTTLE);
+               case kVMPressureNormal:
+               {
+                       if (VM_PRESSURE_WARNING_TO_CRITICAL()) {
+                               new_level = kVMPressureCritical;
+                       }  else if (VM_PRESSURE_NORMAL_TO_WARNING()) {
+                               new_level = kVMPressureWarning;
+                       }
+                       break;
+               }
 
-       vm_page_lock_queues();
+               case kVMPressureWarning:
+               case kVMPressureUrgent:
+               {
+                       if (VM_PRESSURE_WARNING_TO_NORMAL()) {
+                               new_level = kVMPressureNormal;
+                       }  else if (VM_PRESSURE_WARNING_TO_CRITICAL()) {
+                               new_level = kVMPressureCritical;
+                       }
+                       break;
+               }
 
-       vm_pageout_queue_internal.pgo_tid = self->thread_id;
-       vm_pageout_queue_internal.pgo_lowpriority = TRUE;
-       vm_pageout_queue_internal.pgo_inited = TRUE;
+               case kVMPressureCritical:
+               {
+                       if (VM_PRESSURE_WARNING_TO_NORMAL()) {
+                               new_level = kVMPressureNormal;
+                       }  else if (VM_PRESSURE_CRITICAL_TO_WARNING()) {
+                               new_level = kVMPressureWarning;
+                       }
+                       break;
+               }
 
-       vm_page_unlock_queues();
+               default:
+                       return;
+       }
+               
+       if (new_level != -1) {
+               memorystatus_vm_pressure_level = (vm_pressure_level_t) new_level;
+
+               if ((memorystatus_vm_pressure_level != kVMPressureNormal) || (old_level != new_level)) {
+                       if (vm_pressure_thread_running == FALSE) {
+                               thread_wakeup(&vm_pressure_thread);
+                       }
 
-       vm_pageout_iothread_continue(&vm_pageout_queue_internal);
+                       if (old_level != new_level) {
+                               thread_wakeup(&vm_pressure_changed);
+                       }
+               }
+       }
 
-       /*NOTREACHED*/
 }
+#endif /* VM_PRESSURE_EVENTS */
 
 kern_return_t
-vm_set_buffer_cleanup_callout(boolean_t (*func)(int)) 
-{
-       if (OSCompareAndSwapPtr(NULL, func, (void * volatile *) &consider_buffer_cache_collect)) {
-               return KERN_SUCCESS;
+mach_vm_pressure_level_monitor(__unused boolean_t wait_for_pressure, __unused unsigned int *pressure_level) {
+
+#if   !VM_PRESSURE_EVENTS
+       
+       return KERN_FAILURE;
+
+#else /* VM_PRESSURE_EVENTS */
+
+       kern_return_t   kr = KERN_SUCCESS;
+
+       if (pressure_level != NULL) {
+
+               vm_pressure_level_t     old_level = memorystatus_vm_pressure_level;
+
+               if (wait_for_pressure == TRUE) {
+                       wait_result_t           wr = 0;
+
+                       while (old_level == *pressure_level) {
+                               wr = assert_wait((event_t) &vm_pressure_changed,
+                                                THREAD_INTERRUPTIBLE);
+                               if (wr == THREAD_WAITING) {
+                                       wr = thread_block(THREAD_CONTINUE_NULL);
+                               }
+                               if (wr == THREAD_INTERRUPTED) {
+                                       return KERN_ABORTED;
+                               }
+                               if (wr == THREAD_AWAKENED) {
+                                       
+                                       old_level = memorystatus_vm_pressure_level;
+
+                                       if (old_level != *pressure_level) {
+                                               break;
+                                       }
+                               }
+                       }
+               }
+
+               *pressure_level = old_level;
+               kr = KERN_SUCCESS;
        } else {
-               return KERN_FAILURE; /* Already set */
+               kr = KERN_INVALID_ARGUMENT;
        }
+
+       return kr;
+#endif /* VM_PRESSURE_EVENTS */
 }
 
-static void
+#if VM_PRESSURE_EVENTS
+void
 vm_pressure_thread(void) {
-       static boolean_t set_up_thread = FALSE;
+       static boolean_t thread_initialized = FALSE;
 
-       if (set_up_thread) {
-#if VM_PRESSURE_EVENTS
+       if (thread_initialized == TRUE) {
+               vm_pressure_thread_running = TRUE;
                consider_vm_pressure_events();
-#endif /* VM_PRESSURE_EVENTS */
+               vm_pressure_thread_running = FALSE;
        }
 
-       set_up_thread = TRUE;
+       thread_initialized = TRUE;
        assert_wait((event_t) &vm_pressure_thread, THREAD_UNINT);
        thread_block((thread_continue_t)vm_pressure_thread);
 }
+#endif /* VM_PRESSURE_EVENTS */
+
 
 uint32_t vm_pageout_considered_page_last = 0;
 
@@ -3044,6 +3750,7 @@ vm_pageout_garbage_collect(int collect)
                stack_collect();
 
                consider_machine_collect();
+               m_drain();
 
                do {
                        if (consider_buffer_cache_collect != NULL) {
@@ -3069,6 +3776,36 @@ vm_pageout_garbage_collect(int collect)
 }
 
 
+void   vm_pageout_reinit_tuneables(void);
+
+void
+vm_pageout_reinit_tuneables(void)
+{
+       vm_compressor_minorcompact_threshold_divisor = 18;
+       vm_compressor_majorcompact_threshold_divisor = 22;
+       vm_compressor_unthrottle_threshold_divisor = 32;
+}
+
+
+#if VM_PAGE_BUCKETS_CHECK
+#if VM_PAGE_FAKE_BUCKETS
+extern vm_map_offset_t vm_page_fake_buckets_start, vm_page_fake_buckets_end;
+#endif /* VM_PAGE_FAKE_BUCKETS */
+#endif /* VM_PAGE_BUCKETS_CHECK */
+
+#define FBDP_TEST_COLLAPSE_COMPRESSOR 0
+#if FBDP_TEST_COLLAPSE_COMPRESSOR
+extern boolean_t vm_object_collapse_compressor_allowed;
+#include <IOKit/IOLib.h>
+#endif /* FBDP_TEST_COLLAPSE_COMPRESSOR */
+
+#define FBDP_TEST_WIRE_AND_EXTRACT 0
+#if FBDP_TEST_WIRE_AND_EXTRACT
+extern ledger_template_t       task_ledger_template;
+#include <mach/mach_vm.h>
+extern ppnum_t vm_map_get_phys_page(vm_map_t map,
+                                   vm_offset_t offset);
+#endif /* FBDP_TEST_WIRE_AND_EXTRACT */
 
 void
 vm_pageout(void)
@@ -3096,6 +3833,9 @@ vm_pageout(void)
         *      Initialize some paging parameters.
         */
 
+       if (vm_pageout_swap_wait == 0)
+               vm_pageout_swap_wait = VM_PAGEOUT_SWAP_WAIT;
+
        if (vm_pageout_idle_wait == 0)
                vm_pageout_idle_wait = VM_PAGEOUT_IDLE_WAIT;
 
@@ -3185,6 +3925,7 @@ vm_pageout(void)
 
        thread_deallocate(thread);
 
+#if VM_PRESSURE_EVENTS
        result = kernel_thread_start_priority((thread_continue_t)vm_pressure_thread, NULL,
                                                BASEPRI_DEFAULT,
                                                &thread);
@@ -3193,8 +3934,287 @@ vm_pageout(void)
                panic("vm_pressure_thread: create failed");
 
        thread_deallocate(thread);
+#endif
 
        vm_object_reaper_init();
+       
+       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE)
+               vm_compressor_pager_init();
+
+#if VM_PRESSURE_EVENTS
+       vm_pressure_events_enabled = TRUE;
+#endif /* VM_PRESSURE_EVENTS */
+
+#if CONFIG_PHANTOM_CACHE
+       vm_phantom_cache_init();
+#endif
+#if VM_PAGE_BUCKETS_CHECK
+#if VM_PAGE_FAKE_BUCKETS
+       printf("**** DEBUG: protecting fake buckets [0x%llx:0x%llx]\n",
+              (uint64_t) vm_page_fake_buckets_start,
+              (uint64_t) vm_page_fake_buckets_end);
+       pmap_protect(kernel_pmap,
+                    vm_page_fake_buckets_start,
+                    vm_page_fake_buckets_end,
+                    VM_PROT_READ);
+//     *(char *) vm_page_fake_buckets_start = 'x';     /* panic! */
+#endif /* VM_PAGE_FAKE_BUCKETS */
+#endif /* VM_PAGE_BUCKETS_CHECK */
+
+#if VM_OBJECT_TRACKING
+       vm_object_tracking_init();
+#endif /* VM_OBJECT_TRACKING */
+
+
+#if FBDP_TEST_COLLAPSE_COMPRESSOR
+       vm_object_size_t        backing_size, top_size;
+       vm_object_t             backing_object, top_object;
+       vm_map_offset_t         backing_offset, top_offset;
+       unsigned char           *backing_address, *top_address;
+       kern_return_t           kr;
+
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR:\n");
+
+       /* create backing object */
+       backing_size = 15 * PAGE_SIZE;
+       backing_object = vm_object_allocate(backing_size);
+       assert(backing_object != VM_OBJECT_NULL);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: created backing object %p\n",
+               backing_object);
+       /* map backing object */
+       backing_offset = 0;
+       kr = vm_map_enter(kernel_map, &backing_offset, backing_size, 0,
+                         VM_FLAGS_ANYWHERE, backing_object, 0, FALSE,
+                         VM_PROT_DEFAULT, VM_PROT_DEFAULT, VM_INHERIT_DEFAULT);
+       assert(kr == KERN_SUCCESS);
+       backing_address = (unsigned char *) backing_offset;
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+              "mapped backing object %p at 0x%llx\n",
+              backing_object, (uint64_t) backing_offset);
+       /* populate with pages to be compressed in backing object */
+       backing_address[0x1*PAGE_SIZE] = 0xB1;
+       backing_address[0x4*PAGE_SIZE] = 0xB4;
+       backing_address[0x7*PAGE_SIZE] = 0xB7;
+       backing_address[0xa*PAGE_SIZE] = 0xBA;
+       backing_address[0xd*PAGE_SIZE] = 0xBD;
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+              "populated pages to be compressed in "
+              "backing_object %p\n", backing_object);
+       /* compress backing object */
+       vm_object_pageout(backing_object);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: compressing backing_object %p\n",
+              backing_object);
+       /* wait for all the pages to be gone */
+       while (*(volatile int *)&backing_object->resident_page_count != 0)
+               IODelay(10);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: backing_object %p compressed\n",
+              backing_object);
+       /* populate with pages to be resident in backing object */
+       backing_address[0x0*PAGE_SIZE] = 0xB0;
+       backing_address[0x3*PAGE_SIZE] = 0xB3;
+       backing_address[0x6*PAGE_SIZE] = 0xB6;
+       backing_address[0x9*PAGE_SIZE] = 0xB9;
+       backing_address[0xc*PAGE_SIZE] = 0xBC;
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+              "populated pages to be resident in "
+              "backing_object %p\n", backing_object);
+       /* leave the other pages absent */
+       /* mess with the paging_offset of the backing_object */
+       assert(backing_object->paging_offset == 0);
+       backing_object->paging_offset = 0x3000;
+
+       /* create top object */
+       top_size = 9 * PAGE_SIZE;
+       top_object = vm_object_allocate(top_size);
+       assert(top_object != VM_OBJECT_NULL);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: created top object %p\n",
+               top_object);
+       /* map top object */
+       top_offset = 0;
+       kr = vm_map_enter(kernel_map, &top_offset, top_size, 0,
+                         VM_FLAGS_ANYWHERE, top_object, 0, FALSE,
+                         VM_PROT_DEFAULT, VM_PROT_DEFAULT, VM_INHERIT_DEFAULT);
+       assert(kr == KERN_SUCCESS);
+       top_address = (unsigned char *) top_offset;
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+              "mapped top object %p at 0x%llx\n",
+              top_object, (uint64_t) top_offset);
+       /* populate with pages to be compressed in top object */
+       top_address[0x3*PAGE_SIZE] = 0xA3;
+       top_address[0x4*PAGE_SIZE] = 0xA4;
+       top_address[0x5*PAGE_SIZE] = 0xA5;
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+              "populated pages to be compressed in "
+              "top_object %p\n", top_object);
+       /* compress top object */
+       vm_object_pageout(top_object);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: compressing top_object %p\n",
+              top_object);
+       /* wait for all the pages to be gone */
+       while (top_object->resident_page_count != 0);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: top_object %p compressed\n",
+              top_object);
+       /* populate with pages to be resident in top object */
+       top_address[0x0*PAGE_SIZE] = 0xA0;
+       top_address[0x1*PAGE_SIZE] = 0xA1;
+       top_address[0x2*PAGE_SIZE] = 0xA2;
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+              "populated pages to be resident in "
+              "top_object %p\n", top_object);
+       /* leave the other pages absent */
+       
+       /* link the 2 objects */
+       vm_object_reference(backing_object);
+       top_object->shadow = backing_object;
+       top_object->vo_shadow_offset = 0x3000;
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: linked %p and %p\n",
+              top_object, backing_object);
+
+       /* unmap backing object */
+       vm_map_remove(kernel_map,
+                     backing_offset,
+                     backing_offset + backing_size,
+                     0);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+              "unmapped backing_object %p [0x%llx:0x%llx]\n",
+              backing_object,
+              (uint64_t) backing_offset,
+              (uint64_t) (backing_offset + backing_size));
+
+       /* collapse */
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: collapsing %p\n", top_object);
+       vm_object_lock(top_object);
+       vm_object_collapse(top_object, 0, FALSE);
+       vm_object_unlock(top_object);
+       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: collapsed %p\n", top_object);
+
+       /* did it work? */
+       if (top_object->shadow != VM_OBJECT_NULL) {
+               printf("FBDP_TEST_COLLAPSE_COMPRESSOR: not collapsed\n");
+               printf("FBDP_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
+               if (vm_object_collapse_compressor_allowed) {
+                       panic("FBDP_TEST_COLLAPSE_COMPRESSOR: FAIL\n");
+               }
+       } else {
+               /* check the contents of the mapping */
+               unsigned char expect[9] =
+                       { 0xA0, 0xA1, 0xA2,     /* resident in top */
+                         0xA3, 0xA4, 0xA5,     /* compressed in top */
+                         0xB9, /* resident in backing + shadow_offset */
+                         0xBD, /* compressed in backing + shadow_offset + paging_offset */
+                         0x00 };               /* absent in both */
+               unsigned char actual[9];
+               unsigned int i, errors;
+
+               errors = 0;
+               for (i = 0; i < sizeof (actual); i++) {
+                       actual[i] = (unsigned char) top_address[i*PAGE_SIZE];
+                       if (actual[i] != expect[i]) {
+                               errors++;
+                       }
+               }
+               printf("FBDP_TEST_COLLAPSE_COMPRESSOR: "
+                      "actual [%x %x %x %x %x %x %x %x %x] "
+                      "expect [%x %x %x %x %x %x %x %x %x] "
+                      "%d errors\n",
+                      actual[0], actual[1], actual[2], actual[3],
+                      actual[4], actual[5], actual[6], actual[7],
+                      actual[8],
+                      expect[0], expect[1], expect[2], expect[3],
+                      expect[4], expect[5], expect[6], expect[7],
+                      expect[8],
+                      errors);
+               if (errors) {
+                       panic("FBDP_TEST_COLLAPSE_COMPRESSOR: FAIL\n"); 
+               } else {
+                       printf("FBDP_TEST_COLLAPSE_COMPRESSOR: PASS\n");
+               }
+       }
+#endif /* FBDP_TEST_COLLAPSE_COMPRESSOR */
+
+#if FBDP_TEST_WIRE_AND_EXTRACT
+       ledger_t                ledger;
+       vm_map_t                user_map, wire_map;
+       mach_vm_address_t       user_addr, wire_addr;
+       mach_vm_size_t          user_size, wire_size;
+       mach_vm_offset_t        cur_offset;
+       vm_prot_t               cur_prot, max_prot;
+       ppnum_t                 user_ppnum, wire_ppnum;
+       kern_return_t           kr;
+
+       ledger = ledger_instantiate(task_ledger_template,
+                                   LEDGER_CREATE_ACTIVE_ENTRIES);
+       user_map = vm_map_create(pmap_create(ledger, 0, TRUE),
+                                0x100000000ULL,
+                                0x200000000ULL,
+                                TRUE);
+       wire_map = vm_map_create(NULL,
+                                0x100000000ULL,
+                                0x200000000ULL,
+                                TRUE);
+       user_addr = 0;
+       user_size = 0x10000;
+       kr = mach_vm_allocate(user_map,
+                             &user_addr,
+                             user_size,
+                             VM_FLAGS_ANYWHERE);
+       assert(kr == KERN_SUCCESS);
+       wire_addr = 0;
+       wire_size = user_size;
+       kr = mach_vm_remap(wire_map,
+                          &wire_addr,
+                          wire_size,
+                          0,
+                          VM_FLAGS_ANYWHERE,
+                          user_map,
+                          user_addr,
+                          FALSE,
+                          &cur_prot,
+                          &max_prot,
+                          VM_INHERIT_NONE);
+       assert(kr == KERN_SUCCESS);
+       for (cur_offset = 0;
+            cur_offset < wire_size;
+            cur_offset += PAGE_SIZE) {
+               kr = vm_map_wire_and_extract(wire_map,
+                                            wire_addr + cur_offset,
+                                            VM_PROT_DEFAULT,
+                                            TRUE,
+                                            &wire_ppnum);
+               assert(kr == KERN_SUCCESS);
+               user_ppnum = vm_map_get_phys_page(user_map,
+                                                 user_addr + cur_offset);
+               printf("FBDP_TEST_WIRE_AND_EXTRACT: kr=0x%x "
+                      "user[%p:0x%llx:0x%x] wire[%p:0x%llx:0x%x]\n",
+                      kr,
+                      user_map, user_addr + cur_offset, user_ppnum,
+                      wire_map, wire_addr + cur_offset, wire_ppnum);
+               if (kr != KERN_SUCCESS ||
+                   wire_ppnum == 0 ||
+                   wire_ppnum != user_ppnum) {
+                       panic("FBDP_TEST_WIRE_AND_EXTRACT: FAIL\n");
+               }
+       }
+       cur_offset -= PAGE_SIZE;
+       kr = vm_map_wire_and_extract(wire_map,
+                                    wire_addr + cur_offset,
+                                    VM_PROT_DEFAULT,
+                                    TRUE,
+                                    &wire_ppnum);
+       assert(kr == KERN_SUCCESS);
+       printf("FBDP_TEST_WIRE_AND_EXTRACT: re-wire kr=0x%x "
+              "user[%p:0x%llx:0x%x] wire[%p:0x%llx:0x%x]\n",
+              kr,
+              user_map, user_addr + cur_offset, user_ppnum,
+              wire_map, wire_addr + cur_offset, wire_ppnum);
+       if (kr != KERN_SUCCESS ||
+           wire_ppnum == 0 ||
+           wire_ppnum != user_ppnum) {
+               panic("FBDP_TEST_WIRE_AND_EXTRACT: FAIL\n");
+       }
+       
+       printf("FBDP_TEST_WIRE_AND_EXTRACT: PASS\n");
+#endif /* FBDP_TEST_WIRE_AND_EXTRACT */
 
 
        vm_pageout_continue();
@@ -3223,26 +4243,104 @@ vm_pageout(void)
        /*NOTREACHED*/
 }
 
+
+
+#define MAX_COMRPESSOR_THREAD_COUNT    8
+
+struct cq ciq[MAX_COMRPESSOR_THREAD_COUNT];
+
+int vm_compressor_thread_count = 2;
+
 kern_return_t
 vm_pageout_internal_start(void)
 {
-       kern_return_t result;
+       kern_return_t   result;
+       int             i;
+       host_basic_info_data_t hinfo;
+
+       if (COMPRESSED_PAGER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_ACTIVE) {
+               mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
+#define BSD_HOST 1
+               host_info((host_t)BSD_HOST, HOST_BASIC_INFO, (host_info_t)&hinfo, &count);
+
+               assert(hinfo.max_cpus > 0);
+
+               if (vm_compressor_thread_count >= hinfo.max_cpus)
+                       vm_compressor_thread_count = hinfo.max_cpus - 1;
+               if (vm_compressor_thread_count <= 0)
+                       vm_compressor_thread_count = 1;
+               else if (vm_compressor_thread_count > MAX_COMRPESSOR_THREAD_COUNT)
+                       vm_compressor_thread_count = MAX_COMRPESSOR_THREAD_COUNT;
+
+               vm_pageout_queue_internal.pgo_maxlaundry = (vm_compressor_thread_count * 4) * VM_PAGE_LAUNDRY_MAX;
+       } else {
+               vm_compressor_thread_count = 1;
+               vm_pageout_queue_internal.pgo_maxlaundry = VM_PAGE_LAUNDRY_MAX;
+       }
+
+       for (i = 0; i < vm_compressor_thread_count; i++) {
 
-       vm_pageout_queue_internal.pgo_maxlaundry = VM_PAGE_LAUNDRY_MAX;
-       result = kernel_thread_start_priority((thread_continue_t)vm_pageout_iothread_internal, NULL, BASEPRI_PREEMPT - 1, &vm_pageout_internal_iothread);
-       if (result == KERN_SUCCESS)
-               thread_deallocate(vm_pageout_internal_iothread);
+               result = kernel_thread_start_priority((thread_continue_t)vm_pageout_iothread_internal, (void *)&ciq[i], BASEPRI_PREEMPT - 1, &vm_pageout_internal_iothread);
+               if (result == KERN_SUCCESS)
+                       thread_deallocate(vm_pageout_internal_iothread);
+               else
+                       break;
+       }
        return result;
 }
 
+#if CONFIG_IOSCHED
+/*
+ * To support I/O Expedite for compressed files we mark the upls with special flags.
+ * The way decmpfs works is that we create a big upl which marks all the pages needed to
+ * represent the compressed file as busy. We tag this upl with the flag UPL_DECMP_REQ. Decmpfs
+ * then issues smaller I/Os for compressed I/Os, deflates them and puts the data into the pages
+ * being held in the big original UPL. We mark each of these smaller UPLs with the flag
+ * UPL_DECMP_REAL_IO. Any outstanding real I/O UPL is tracked by the big req upl using the
+ * decmp_io_upl field (in the upl structure). This link is protected in the forward direction
+ * by the req upl lock (the reverse link doesnt need synch. since we never inspect this link
+ * unless the real I/O upl is being destroyed).
+ */
+
+
+static void
+upl_set_decmp_info(upl_t upl, upl_t src_upl)
+{
+        assert((src_upl->flags & UPL_DECMP_REQ) != 0);
+
+        upl_lock(src_upl);
+        if (src_upl->decmp_io_upl) {
+                /*
+                 * If there is already an alive real I/O UPL, ignore this new UPL.
+                 * This case should rarely happen and even if it does, it just means
+                 * that we might issue a spurious expedite which the driver is expected
+                 * to handle.
+                 */ 
+                upl_unlock(src_upl);
+                return;
+        }
+        src_upl->decmp_io_upl = (void *)upl;
+        src_upl->ref_count++;
+
+        upl->flags |= UPL_DECMP_REAL_IO;
+        upl->decmp_io_upl = (void *)src_upl;
+       upl_unlock(src_upl);
+}
+#endif /* CONFIG_IOSCHED */  
+
+#if UPL_DEBUG
+int    upl_debug_enabled = 1;
+#else
+int    upl_debug_enabled = 0;
+#endif
 
 static upl_t
 upl_create(int type, int flags, upl_size_t size)
 {
        upl_t   upl;
-       int     page_field_size = 0;
+       vm_size_t       page_field_size = 0;
        int     upl_flags = 0;
-       int     upl_size  = sizeof(struct upl);
+       vm_size_t       upl_size  = sizeof(struct upl);
 
        size = round_page_32(size);
 
@@ -3253,7 +4351,7 @@ upl_create(int type, int flags, upl_size_t size)
                upl_flags |= UPL_LITE;
        }
        if (type & UPL_CREATE_INTERNAL) {
-               upl_size += (int) sizeof(struct upl_page_info) * atop(size);
+               upl_size += sizeof(struct upl_page_info) * atop(size);
 
                upl_flags |= UPL_INTERNAL;
        }
@@ -3272,18 +4370,40 @@ upl_create(int type, int flags, upl_size_t size)
        upl->highest_page = 0;
        upl_lock_init(upl);
        upl->vector_upl = NULL;
+#if CONFIG_IOSCHED
+       if (type & UPL_CREATE_IO_TRACKING) {
+               upl->upl_priority = proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO);
+       }
+       
+       upl->upl_reprio_info = 0;
+       upl->decmp_io_upl = 0;
+       if ((type & UPL_CREATE_INTERNAL) && (type & UPL_CREATE_EXPEDITE_SUP)) {
+               /* Only support expedite on internal UPLs */
+               thread_t        curthread = current_thread();
+               upl->upl_reprio_info = (uint64_t *)kalloc(sizeof(uint64_t) * atop(size));
+               bzero(upl->upl_reprio_info, (sizeof(uint64_t) * atop(size)));
+               upl->flags |= UPL_EXPEDITE_SUPPORTED;
+               if (curthread->decmp_upl != NULL) 
+                       upl_set_decmp_info(upl, curthread->decmp_upl);
+       }
+#endif
+#if CONFIG_IOSCHED || UPL_DEBUG
+       if ((type & UPL_CREATE_IO_TRACKING) || upl_debug_enabled) {
+               upl->upl_creator = current_thread();
+               upl->uplq.next = 0;
+               upl->uplq.prev = 0;
+               upl->flags |= UPL_TRACKED_BY_OBJECT;
+       }
+#endif
+
 #if UPL_DEBUG
        upl->ubc_alias1 = 0;
        upl->ubc_alias2 = 0;
 
-       upl->upl_creator = current_thread();
        upl->upl_state = 0;
        upl->upl_commit_index = 0;
        bzero(&upl->upl_commit_records[0], sizeof(upl->upl_commit_records));
 
-       upl->uplq.next = 0;
-       upl->uplq.prev = 0;
-
        (void) OSBacktrace(&upl->upl_create_retaddr[0], UPL_DEBUG_STACK_FRAMES);
 #endif /* UPL_DEBUG */
 
@@ -3300,8 +4420,20 @@ upl_destroy(upl_t upl)
                panic("upl(%p) ext_ref_count", upl);
        }
 
-#if UPL_DEBUG
-       if ( !(upl->flags & UPL_VECTOR)) {
+#if CONFIG_IOSCHED
+        if ((upl->flags & UPL_DECMP_REAL_IO) && upl->decmp_io_upl) {
+                upl_t src_upl;
+                src_upl = upl->decmp_io_upl;
+                assert((src_upl->flags & UPL_DECMP_REQ) != 0);
+                upl_lock(src_upl);
+                src_upl->decmp_io_upl = NULL;
+                upl_unlock(src_upl);
+                upl_deallocate(src_upl);
+        }
+#endif /* CONFIG_IOSCHED */
+
+#if CONFIG_IOSCHED || UPL_DEBUG
+       if ((upl->flags & UPL_TRACKED_BY_OBJECT) && !(upl->flags & UPL_VECTOR)) {
                vm_object_t     object;
 
                if (upl->flags & UPL_SHADOWED) {
@@ -3309,13 +4441,14 @@ upl_destroy(upl_t upl)
                } else {
                        object = upl->map_object;
                }
+
                vm_object_lock(object);
                queue_remove(&object->uplq, upl, upl_t, uplq);
                vm_object_activity_end(object);
                vm_object_collapse(object, 0, TRUE);
                vm_object_unlock(object);
        }
-#endif /* UPL_DEBUG */
+#endif
        /*
         * drop a reference on the map_object whether or
         * not a pageout object is inserted
@@ -3336,6 +4469,11 @@ upl_destroy(upl_t upl)
        upl_lock_destroy(upl);
        upl->vector_upl = (vector_upl_t) 0xfeedbeef;
 
+#if CONFIG_IOSCHED
+       if (upl->flags & UPL_EXPEDITE_SUPPORTED)
+               kfree(upl->upl_reprio_info, sizeof(uint64_t) * (size/PAGE_SIZE));
+#endif
+
        if (upl->flags & UPL_INTERNAL) {
                kfree(upl,
                      sizeof(struct upl) + 
@@ -3349,13 +4487,53 @@ upl_destroy(upl_t upl)
 void
 upl_deallocate(upl_t upl)
 {
+       upl_lock(upl);
        if (--upl->ref_count == 0) {
                if(vector_upl_is_valid(upl))
                        vector_upl_deallocate(upl);
+               upl_unlock(upl);        
                upl_destroy(upl);
        }
+       else
+               upl_unlock(upl);
+}
+
+#if CONFIG_IOSCHED
+void
+upl_mark_decmp(upl_t upl)
+{
+       if (upl->flags & UPL_TRACKED_BY_OBJECT) {
+               upl->flags |= UPL_DECMP_REQ;
+               upl->upl_creator->decmp_upl = (void *)upl;
+       }       
+}
+
+void
+upl_unmark_decmp(upl_t upl)
+{
+       if(upl && (upl->flags & UPL_DECMP_REQ)) {
+               upl->upl_creator->decmp_upl = NULL;
+       }
+} 
+
+#endif /* CONFIG_IOSCHED */
+
+#define VM_PAGE_Q_BACKING_UP(q)                \
+        ((q)->pgo_laundry >= (((q)->pgo_maxlaundry * 8) / 10))
+
+boolean_t must_throttle_writes(void);
+
+boolean_t
+must_throttle_writes()
+{
+       if (VM_PAGE_Q_BACKING_UP(&vm_pageout_queue_external) &&
+           vm_page_pageable_external_count > (AVAILABLE_NON_COMPRESSED_MEMORY * 6) / 10)
+               return (TRUE);
+
+       return (FALSE);
 }
 
+
 #if DEVELOPMENT || DEBUG
 /*/*
  * Statistics about UPL enforcement of copy-on-write obligations.
@@ -3442,6 +4620,7 @@ vm_object_upl_request(
        struct  vm_page_delayed_work    *dwp;
        int                     dw_count;
        int                     dw_limit;
+       int                     io_tracking_flag = 0;
 
        if (cntrl_flags & ~UPL_VALID_FLAGS) {
                /*
@@ -3456,16 +4635,25 @@ vm_object_upl_request(
                panic("vm_object_upl_request: contiguous object specified\n");
 
 
-       if ((size / PAGE_SIZE) > MAX_UPL_SIZE)
-               size = MAX_UPL_SIZE * PAGE_SIZE;
+       if (size > MAX_UPL_SIZE_BYTES)
+               size = MAX_UPL_SIZE_BYTES;
 
        if ( (cntrl_flags & UPL_SET_INTERNAL) && page_list_count != NULL)
-               *page_list_count = MAX_UPL_SIZE;
+               *page_list_count = MAX_UPL_SIZE_BYTES >> PAGE_SHIFT;
+
+#if CONFIG_IOSCHED || UPL_DEBUG
+       if (object->io_tracking || upl_debug_enabled)
+               io_tracking_flag |= UPL_CREATE_IO_TRACKING;
+#endif
+#if CONFIG_IOSCHED
+       if (object->io_tracking)
+               io_tracking_flag |= UPL_CREATE_EXPEDITE_SUP;
+#endif
 
        if (cntrl_flags & UPL_SET_INTERNAL) {
                if (cntrl_flags & UPL_SET_LITE) {
 
-                       upl = upl_create(UPL_CREATE_INTERNAL | UPL_CREATE_LITE, 0, size);
+                       upl = upl_create(UPL_CREATE_INTERNAL | UPL_CREATE_LITE | io_tracking_flag, 0, size);
 
                        user_page_list = (upl_page_info_t *) (((uintptr_t)upl) + sizeof(struct upl));
                        lite_list = (wpl_array_t)
@@ -3476,7 +4664,7 @@ vm_object_upl_request(
                                lite_list = NULL;
                        }
                } else {
-                       upl = upl_create(UPL_CREATE_INTERNAL, 0, size);
+                       upl = upl_create(UPL_CREATE_INTERNAL | io_tracking_flag, 0, size);
 
                        user_page_list = (upl_page_info_t *) (((uintptr_t)upl) + sizeof(struct upl));
                        if (size == 0) {
@@ -3486,14 +4674,14 @@ vm_object_upl_request(
        } else {
                if (cntrl_flags & UPL_SET_LITE) {
 
-                       upl = upl_create(UPL_CREATE_EXTERNAL | UPL_CREATE_LITE, 0, size);
+                       upl = upl_create(UPL_CREATE_EXTERNAL | UPL_CREATE_LITE | io_tracking_flag, 0, size);
 
                        lite_list = (wpl_array_t) (((uintptr_t)upl) + sizeof(struct upl));
                        if (size == 0) {
                                lite_list = NULL;
                        }
                } else {
-                       upl = upl_create(UPL_CREATE_EXTERNAL, 0, size);
+                       upl = upl_create(UPL_CREATE_EXTERNAL | io_tracking_flag, 0, size);
                }
        }
        *upl_ptr = upl;
@@ -3542,11 +4730,12 @@ vm_object_upl_request(
        upl->size = size;
        upl->offset = offset + object->paging_offset;
 
-#if UPL_DEBUG
-       vm_object_activity_begin(object);
-       queue_enter(&object->uplq, upl, upl_t, uplq);
-#endif /* UPL_DEBUG */
-
+#if CONFIG_IOSCHED || UPL_DEBUG
+       if (object->io_tracking || upl_debug_enabled) {
+               vm_object_activity_begin(object);
+               queue_enter(&object->uplq, upl, upl_t, uplq);
+       }
+#endif
        if ((cntrl_flags & UPL_WILL_MODIFY) && object->copy != VM_OBJECT_NULL) {
                /*
                 * Honor copy-on-write obligations
@@ -3585,9 +4774,26 @@ vm_object_upl_request(
        dw_limit = DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT);
 
        if (vm_page_free_count > (vm_page_free_target + size_in_pages) ||
-           object->resident_page_count < (MAX_UPL_SIZE * 2))
+           object->resident_page_count < ((MAX_UPL_SIZE_BYTES * 2) >> PAGE_SHIFT))
                object->scan_collisions = 0;
 
+       if ((cntrl_flags & UPL_WILL_MODIFY) && must_throttle_writes() == TRUE) {
+               boolean_t       isSSD = FALSE;
+
+               vnode_pager_get_isSSD(object->pager, &isSSD);
+               vm_object_unlock(object);
+               
+               OSAddAtomic(size_in_pages, &vm_upl_wait_for_pages);
+
+               if (isSSD == TRUE)
+                       delay(1000 * size_in_pages);
+               else
+                       delay(5000 * size_in_pages);
+               OSAddAtomic(-size_in_pages, &vm_upl_wait_for_pages);
+
+               vm_object_lock(object);
+       }
+
        while (xfer_size) {
 
                dwp->dw_mask = 0;
@@ -3637,7 +4843,7 @@ vm_object_upl_request(
                                /*
                                 * we're only asking for DIRTY pages to be returned
                                 */
-                               if (dst_page->pageout || !(cntrl_flags & UPL_FOR_PAGEOUT)) {
+                               if (dst_page->laundry || !(cntrl_flags & UPL_FOR_PAGEOUT)) {
                                        /*
                                         * if we were the page stolen by vm_pageout_scan to be
                                         * cleaned (as opposed to a buddy being clustered in 
@@ -3994,6 +5200,9 @@ check_busy:
                                         * speculative list
                                         */
                                        dst_page->clustered = TRUE;
+
+                                       if ( !(cntrl_flags & UPL_FILE_IO))
+                                               VM_STAT_INCR(pageins);
                                }
                        }
                        /*
@@ -4144,7 +5353,8 @@ check_busy:
                         * update clustered and speculative state
                         * 
                         */
-                       VM_PAGE_CONSUME_CLUSTERED(dst_page);
+                       if (dst_page->clustered)
+                               VM_PAGE_CONSUME_CLUSTERED(dst_page);
                }
 try_next_page:
                if (dwp->dw_mask) {
@@ -4381,8 +5591,8 @@ REDISCOVER_ENTRY:
                }
 
                if (entry->object.vm_object == VM_OBJECT_NULL || !entry->object.vm_object->phys_contiguous) {
-                       if ((*upl_size/PAGE_SIZE) > MAX_UPL_SIZE)
-                                       *upl_size = MAX_UPL_SIZE * PAGE_SIZE;
+                       if (*upl_size > MAX_UPL_SIZE_BYTES)
+                                       *upl_size = MAX_UPL_SIZE_BYTES;
                }
                /*
                 *      Create an object if necessary.
@@ -4402,76 +5612,99 @@ REDISCOVER_ENTRY:
                                vm_map_unlock_read(map);
                                return KERN_PROTECTION_FAILURE;
                        }
+               }
 
-#if !CONFIG_EMBEDDED
-                       local_object = entry->object.vm_object;
-                       if (vm_map_entry_should_cow_for_true_share(entry) &&
-                           local_object->vo_size > *upl_size &&
-                           *upl_size != 0) {
-                               vm_prot_t       prot;
+               local_object = entry->object.vm_object;
+               if (vm_map_entry_should_cow_for_true_share(entry) &&
+                   local_object->vo_size > *upl_size &&
+                   *upl_size != 0) {
+                       vm_prot_t       prot;
 
-                               /*
-                                * Set up the targeted range for copy-on-write to avoid
-                                * applying true_share/copy_delay to the entire object.
-                                */
+                       /*
+                        * Set up the targeted range for copy-on-write to avoid
+                        * applying true_share/copy_delay to the entire object.
+                        */
 
-                               if (vm_map_lock_read_to_write(map)) {
-                                       goto REDISCOVER_ENTRY;
-                               }
+                       if (vm_map_lock_read_to_write(map)) {
+                               goto REDISCOVER_ENTRY;
+                       }
 
-                               vm_map_clip_start(map, entry, vm_map_trunc_page(offset));
-                               vm_map_clip_end(map, entry, vm_map_round_page(offset + *upl_size));
-                               prot = entry->protection & ~VM_PROT_WRITE;
-                               if (override_nx(map, entry->alias) && prot)
-                                       prot |= VM_PROT_EXECUTE;
-                               vm_object_pmap_protect(local_object,
-                                                      entry->offset,
-                                                      entry->vme_end - entry->vme_start,
-                                                      ((entry->is_shared || map->mapped_in_other_pmaps)
-                                                       ? PMAP_NULL
-                                                       : map->pmap),
-                                                      entry->vme_start,
-                                                      prot);
-                               entry->needs_copy = TRUE;
-
-                               vm_map_lock_write_to_read(map);
-                       }
-#endif /* !CONFIG_EMBEDDED */
-
-                       if (entry->needs_copy)  {
-                               /*
-                                * Honor copy-on-write for COPY_SYMMETRIC
-                                * strategy.
-                                */
-                               vm_map_t                local_map;
-                               vm_object_t             object;
-                               vm_object_offset_t      new_offset;
-                               vm_prot_t               prot;
-                               boolean_t               wired;
-                               vm_map_version_t        version;
-                               vm_map_t                real_map;
-
-                               local_map = map;
-
-                               if (vm_map_lookup_locked(&local_map,
-                                                        offset, VM_PROT_WRITE,
-                                                        OBJECT_LOCK_EXCLUSIVE,
-                                                        &version, &object,
-                                                        &new_offset, &prot, &wired,
-                                                        NULL,
-                                                        &real_map) != KERN_SUCCESS) {
-                                       vm_map_unlock_read(local_map);
-                                       return KERN_FAILURE;
+                       vm_map_clip_start(map,
+                                         entry,
+                                         vm_map_trunc_page(offset,
+                                                           VM_MAP_PAGE_MASK(map)));
+                       vm_map_clip_end(map,
+                                       entry,
+                                       vm_map_round_page(offset + *upl_size,
+                                                         VM_MAP_PAGE_MASK(map)));
+                       if ((entry->vme_end - offset) < *upl_size) {
+                               *upl_size = (upl_size_t) (entry->vme_end - offset);
+                               assert(*upl_size == entry->vme_end - offset);
+                       }
+
+                       prot = entry->protection & ~VM_PROT_WRITE;
+                       if (override_nx(map, entry->alias) && prot)
+                               prot |= VM_PROT_EXECUTE;
+                       vm_object_pmap_protect(local_object,
+                                              entry->offset,
+                                              entry->vme_end - entry->vme_start,
+                                              ((entry->is_shared || map->mapped_in_other_pmaps)
+                                               ? PMAP_NULL
+                                               : map->pmap),
+                                              entry->vme_start,
+                                              prot);
+                       entry->needs_copy = TRUE;
+
+                       vm_map_lock_write_to_read(map);
+               }
+
+               if (entry->needs_copy)  {
+                       /*
+                        * Honor copy-on-write for COPY_SYMMETRIC
+                        * strategy.
+                        */
+                       vm_map_t                local_map;
+                       vm_object_t             object;
+                       vm_object_offset_t      new_offset;
+                       vm_prot_t               prot;
+                       boolean_t               wired;
+                       vm_map_version_t        version;
+                       vm_map_t                real_map;
+                       vm_prot_t               fault_type;
+
+                       local_map = map;
+
+                       if (caller_flags & UPL_COPYOUT_FROM) {
+                               fault_type = VM_PROT_READ | VM_PROT_COPY;
+                               vm_counters.create_upl_extra_cow++;
+                               vm_counters.create_upl_extra_cow_pages += (entry->vme_end - entry->vme_start) / PAGE_SIZE;
+                       } else {
+                               fault_type = VM_PROT_WRITE;
+                       }
+                       if (vm_map_lookup_locked(&local_map,
+                                                offset, fault_type,
+                                                OBJECT_LOCK_EXCLUSIVE,
+                                                &version, &object,
+                                                &new_offset, &prot, &wired,
+                                                NULL,
+                                                &real_map) != KERN_SUCCESS) {
+                               if (fault_type == VM_PROT_WRITE) {
+                                       vm_counters.create_upl_lookup_failure_write++;
+                               } else {
+                                       vm_counters.create_upl_lookup_failure_copy++;
                                }
-                               if (real_map != map)
-                                       vm_map_unlock(real_map);
                                vm_map_unlock_read(local_map);
+                               return KERN_FAILURE;
+                       }
+                       if (real_map != map)
+                               vm_map_unlock(real_map);
+                       vm_map_unlock_read(local_map);
 
-                               vm_object_unlock(object);
+                       vm_object_unlock(object);
 
-                               goto REDISCOVER_ENTRY;
-                       }
+                       goto REDISCOVER_ENTRY;
                }
+
                if (sync_cow_data) {
                        if (entry->object.vm_object->shadow || entry->object.vm_object->copy) {
                                local_object = entry->object.vm_object;
@@ -4773,9 +6006,9 @@ process_upl_to_enter:
                         * but only in kernel space. If this was on a user map,
                         * we'd have to set the wpmapped bit. */
                        /* m->wpmapped = TRUE; */
-                       assert(map==kernel_map);
+                       assert(map->pmap == kernel_pmap);
        
-                       PMAP_ENTER(map->pmap, addr, m, VM_PROT_ALL, VM_PROT_NONE, 0, TRUE);
+                       PMAP_ENTER(map->pmap, addr, m, VM_PROT_DEFAULT, VM_PROT_NONE, 0, TRUE);
                }
                offset += PAGE_SIZE_64;
        }
@@ -4877,9 +6110,12 @@ process_upl_to_remove:
                if(!isVectorUPL) {
                        upl_unlock(upl);
                
-                       vm_map_remove(map,
-                               vm_map_trunc_page(addr),
-                               vm_map_round_page(addr + size),
+                       vm_map_remove(
+                               map,
+                               vm_map_trunc_page(addr,
+                                                 VM_MAP_PAGE_MASK(map)),
+                               vm_map_round_page(addr + size,
+                                                 VM_MAP_PAGE_MASK(map)),
                                VM_MAP_NO_FLAGS);
                
                        return KERN_SUCCESS;
@@ -4898,7 +6134,6 @@ process_upl_to_remove:
        return KERN_FAILURE;
 }
 
-
 kern_return_t
 upl_commit_range(
        upl_t                   upl, 
@@ -4927,6 +6162,14 @@ upl_commit_range(
        upl_t                   vector_upl = NULL;
        boolean_t               should_be_throttled = FALSE;
 
+       vm_page_t               nxt_page = VM_PAGE_NULL;
+       int                     fast_path_possible = 0;
+       int                     fast_path_full_commit = 0;
+       int                     throttle_page = 0;
+       int                     unwired_count = 0;
+       int                     local_queue_count = 0;
+       queue_head_t            local_queue;
+
        *empty = FALSE;
 
        if (upl == UPL_NULL)
@@ -5039,6 +6282,29 @@ process_upl_to_commit:
        dw_count = 0;
        dw_limit = DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT);
 
+       if ((upl->flags & UPL_IO_WIRE) &&
+           !(flags & UPL_COMMIT_FREE_ABSENT) &&
+           !isVectorUPL &&
+           shadow_object->purgable != VM_PURGABLE_VOLATILE &&
+           shadow_object->purgable != VM_PURGABLE_EMPTY) {
+
+               if (!queue_empty(&shadow_object->memq)) {
+                       queue_init(&local_queue);
+                       if (size == shadow_object->vo_size) {
+                               nxt_page = (vm_page_t)queue_first(&shadow_object->memq);
+                               fast_path_full_commit = 1;
+                       }
+                       fast_path_possible = 1;
+
+                       if (!VM_DYNAMIC_PAGING_ENABLED(memory_manager_default) && shadow_object->internal &&
+                           (shadow_object->purgable == VM_PURGABLE_DENY ||
+                            shadow_object->purgable == VM_PURGABLE_NONVOLATILE ||
+                            shadow_object->purgable == VM_PURGABLE_VOLATILE)) {
+                               throttle_page = 1;
+                       }
+               }
+       }
+
        while (xfer_size) {
                vm_page_t       t, m;
 
@@ -5050,15 +6316,21 @@ process_upl_to_commit:
                if (upl->flags & UPL_LITE) {
                        unsigned int    pg_num;
 
+                       if (nxt_page != VM_PAGE_NULL) {
+                               m = nxt_page;
+                               nxt_page = (vm_page_t)queue_next(&nxt_page->listq);
+                               target_offset = m->offset;
+                       }
                        pg_num = (unsigned int) (target_offset/PAGE_SIZE);
                        assert(pg_num == target_offset/PAGE_SIZE);
 
                        if (lite_list[pg_num>>5] & (1 << (pg_num & 31))) {
                                lite_list[pg_num>>5] &= ~(1 << (pg_num & 31));
 
-                               if (!(upl->flags & UPL_KERNEL_OBJECT))
+                               if (!(upl->flags & UPL_KERNEL_OBJECT) && m == VM_PAGE_NULL)
                                        m = vm_page_lookup(shadow_object, target_offset + (upl->offset - shadow_object->paging_offset));
-                       }
+                       } else
+                               m = NULL;
                }
                if (upl->flags & UPL_SHADOWED) {
                        if ((t = vm_page_lookup(object, target_offset)) != VM_PAGE_NULL) {
@@ -5067,13 +6339,20 @@ process_upl_to_commit:
 
                                VM_PAGE_FREE(t);
 
-                               if (m == VM_PAGE_NULL)
+                               if (!(upl->flags & UPL_KERNEL_OBJECT) && m == VM_PAGE_NULL)
                                        m = vm_page_lookup(shadow_object, target_offset + object->vo_shadow_offset);
                        }
                }
-               if ((upl->flags & UPL_KERNEL_OBJECT) || m == VM_PAGE_NULL)
+               if (m == VM_PAGE_NULL)
                        goto commit_next_page;
 
+               if (m->compressor) {
+                       assert(m->busy);
+
+                       dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP);
+                       goto commit_next_page;
+               }
+
                if (flags & UPL_COMMIT_CS_VALIDATED) {
                        /*
                         * CODE SIGNING:
@@ -5083,6 +6362,9 @@ process_upl_to_commit:
                        m->cs_validated = page_list[entry].cs_validated;
                        m->cs_tainted = page_list[entry].cs_tainted;
                }
+               if (flags & UPL_COMMIT_WRITTEN_BY_KERNEL)
+                       m->written_by_kernel = TRUE;
+
                if (upl->flags & UPL_IO_WIRE) {
 
                        if (page_list)
@@ -5102,6 +6384,11 @@ process_upl_to_commit:
                                         * so it will need to be
                                         * re-validated.
                                         */
+                                       if (m->slid) {
+                                               panic("upl_commit_range(%p): page %p was slid\n",
+                                                     upl, m);
+                                       }
+                                       assert(!m->slid);
                                        m->cs_validated = FALSE;
 #if DEVELOPMENT || DEBUG
                                        vm_cs_validated_resets++;
@@ -5110,10 +6397,6 @@ process_upl_to_commit:
                                }
                                clear_refmod |= VM_MEM_MODIFIED;
                        }
-                       if (flags & UPL_COMMIT_INACTIVATE) {
-                               dwp->dw_mask |= DW_vm_page_deactivate_internal;
-                               clear_refmod |= VM_MEM_REFERENCED;
-                       }
                        if (upl->flags & UPL_ACCESS_BLOCKED) {
                                /*
                                 * We blocked access to the pages in this UPL.
@@ -5122,21 +6405,65 @@ process_upl_to_commit:
                                 */
                                dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP);
                        }
-                       if (m->absent) {
-                               if (flags & UPL_COMMIT_FREE_ABSENT)
-                                       dwp->dw_mask |= DW_vm_page_free;
-                               else {
+                       if (fast_path_possible) {
+                               assert(m->object->purgable != VM_PURGABLE_EMPTY);
+                               assert(m->object->purgable != VM_PURGABLE_VOLATILE);
+                               if (m->absent) {
+                                       assert(m->wire_count == 0);
+                                       assert(m->busy);
+
                                        m->absent = FALSE;
                                        dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP);
+                               } else {
+                                       if (m->wire_count == 0)
+                                               panic("wire_count == 0, m = %p, obj = %p\n", m, shadow_object);
+
+                                       /*
+                                        * XXX FBDP need to update some other
+                                        * counters here (purgeable_wired_count)
+                                        * (ledgers), ...
+                                        */
+                                       assert(m->wire_count);
+                                       m->wire_count--;
 
-                                       if ( !(dwp->dw_mask & DW_vm_page_deactivate_internal))
-                                               dwp->dw_mask |= DW_vm_page_activate;
+                                       if (m->wire_count == 0)
+                                               unwired_count++;
                                }
-                       } else
-                               dwp->dw_mask |= DW_vm_page_unwire;
+                               if (m->wire_count == 0) {
+                                       queue_enter(&local_queue, m, vm_page_t, pageq);
+                                       local_queue_count++;
+
+                                       if (throttle_page) {
+                                               m->throttled = TRUE;
+                                       } else {
+                                               if (flags & UPL_COMMIT_INACTIVATE)
+                                                       m->inactive = TRUE;
+                                               else
+                                                       m->active = TRUE;
+                                       }
+                               }
+                       } else {
+                               if (flags & UPL_COMMIT_INACTIVATE) {
+                                       dwp->dw_mask |= DW_vm_page_deactivate_internal;
+                                       clear_refmod |= VM_MEM_REFERENCED;
+                               }
+                               if (m->absent) {
+                                       if (flags & UPL_COMMIT_FREE_ABSENT)
+                                               dwp->dw_mask |= DW_vm_page_free;
+                                       else {
+                                               m->absent = FALSE;
+                                               dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP);
 
+                                               if ( !(dwp->dw_mask & DW_vm_page_deactivate_internal))
+                                                       dwp->dw_mask |= DW_vm_page_activate;
+                                       }
+                               } else
+                                       dwp->dw_mask |= DW_vm_page_unwire;
+                       }
                        goto commit_next_page;
                }
+               assert(!m->compressor);
+
                if (page_list)
                        page_list[entry].phys_addr = 0;
 
@@ -5167,6 +6494,11 @@ process_upl_to_commit:
                         * so it will need to be
                         * re-validated.
                         */
+                       if (m->slid) {
+                               panic("upl_commit_range(%p): page %p was slid\n",
+                                     upl, m);
+                       }
+                       assert(!m->slid);
                        m->cs_validated = FALSE;
 #if DEVELOPMENT || DEBUG
                        vm_cs_validated_resets++;
@@ -5178,6 +6510,10 @@ process_upl_to_commit:
                         * the (COPY_OUT_FROM == FALSE) request_page_list case
                         */
                        if (m->busy) {
+#if CONFIG_PHANTOM_CACHE
+                               if (m->absent && !m->object->internal)
+                                       dwp->dw_mask |= DW_vm_phantom_cache_update;
+#endif
                                m->absent = FALSE;
 
                                dwp->dw_mask |= DW_clear_busy;
@@ -5274,8 +6610,8 @@ process_upl_to_commit:
                if (hibernate_cleaning_in_progress == FALSE && !m->dirty && (upl->flags & UPL_PAGEOUT)) {
                        pgpgout_count++;
 
-                       /* this page used to be dirty; now it's on the clean queue. */
-                       m->was_dirty = TRUE;
+                       VM_STAT_INCR(pageouts);
+                       DTRACE_VM2(pgout, int, 1, (uint64_t *), NULL);
 
                        dwp->dw_mask |= DW_enqueue_cleaned;
                        vm_pageout_enqueued_cleaned_from_inactive_dirty++;
@@ -5348,6 +6684,80 @@ commit_next_page:
        if (dw_count)
                vm_page_do_delayed_work(shadow_object, &dw_array[0], dw_count);
 
+       if (fast_path_possible) {
+
+               assert(shadow_object->purgable != VM_PURGABLE_VOLATILE);
+               assert(shadow_object->purgable != VM_PURGABLE_EMPTY);
+
+               if (local_queue_count || unwired_count) {
+
+                       if (local_queue_count) {
+                               vm_page_t       first_local, last_local;
+                               vm_page_t       first_target;
+                               queue_head_t    *target_queue;
+
+                               if (throttle_page)
+                                       target_queue = &vm_page_queue_throttled;
+                               else {
+                                       if (flags & UPL_COMMIT_INACTIVATE) {
+                                               if (shadow_object->internal)
+                                                       target_queue = &vm_page_queue_anonymous;
+                                               else
+                                                       target_queue = &vm_page_queue_inactive;
+                                       } else
+                                               target_queue = &vm_page_queue_active;
+                               }
+                               /*
+                                * Transfer the entire local queue to a regular LRU page queues.
+                                */
+                               first_local = (vm_page_t) queue_first(&local_queue);
+                               last_local = (vm_page_t) queue_last(&local_queue);
+
+                               vm_page_lockspin_queues();
+
+                               first_target = (vm_page_t) queue_first(target_queue);
+
+                               if (queue_empty(target_queue))
+                                       queue_last(target_queue) = (queue_entry_t) last_local;
+                               else
+                                       queue_prev(&first_target->pageq) = (queue_entry_t) last_local;
+
+                               queue_first(target_queue) = (queue_entry_t) first_local;
+                               queue_prev(&first_local->pageq) = (queue_entry_t) target_queue;
+                               queue_next(&last_local->pageq) = (queue_entry_t) first_target;
+
+                               /*
+                                * Adjust the global page counts.
+                                */
+                               if (throttle_page) {
+                                       vm_page_throttled_count += local_queue_count;
+                               } else {
+                                       if (flags & UPL_COMMIT_INACTIVATE) {
+                                               if (shadow_object->internal)
+                                                       vm_page_anonymous_count += local_queue_count;
+                                               vm_page_inactive_count += local_queue_count;
+
+                                               token_new_pagecount += local_queue_count;
+                                       } else
+                                               vm_page_active_count += local_queue_count;
+
+                                       if (shadow_object->internal)
+                                               vm_page_pageable_internal_count += local_queue_count;
+                                       else
+                                               vm_page_pageable_external_count += local_queue_count;
+                               }
+                       } else {
+                               vm_page_lockspin_queues();
+                       }
+                       if (unwired_count) {    
+                               vm_page_wire_count -= unwired_count;
+                               VM_CHECK_MEMORYSTATUS;
+                       }
+                       vm_page_unlock_queues();
+
+                       shadow_object->wired_page_count -= unwired_count;
+               }
+       }
        occupied = 1;
 
        if (upl->flags & UPL_DEVICE_MEMORY)  {
@@ -5356,14 +6766,17 @@ commit_next_page:
                int     pg_num;
                int     i;
 
-               pg_num = upl->size/PAGE_SIZE;
-               pg_num = (pg_num + 31) >> 5;
                occupied = 0;
 
-               for (i = 0; i < pg_num; i++) {
-                       if (lite_list[i] != 0) {
-                               occupied = 1;
-                               break;
+               if (!fast_path_full_commit) {
+                       pg_num = upl->size/PAGE_SIZE;
+                       pg_num = (pg_num + 31) >> 5;
+
+                       for (i = 0; i < pg_num; i++) {
+                               if (lite_list[i] != 0) {
+                                       occupied = 1;
+                                       break;
+                               }
                        }
                }
        } else {
@@ -5591,6 +7004,8 @@ process_upl_to_abort:
 
                if (m != VM_PAGE_NULL) {
 
+                       assert(!m->compressor);
+
                        if (m->absent) {
                                boolean_t must_free = TRUE;
 
@@ -5854,6 +7269,82 @@ upl_commit(
        return upl_commit_range(upl, 0, upl->size, 0, page_list, count, &empty);
 }
 
+
+void
+iopl_valid_data(
+       upl_t   upl)
+{
+       vm_object_t     object;
+       vm_offset_t     offset;
+       vm_page_t       m, nxt_page = VM_PAGE_NULL;
+       upl_size_t      size;
+       int             wired_count = 0;
+
+       if (upl == NULL)
+               panic("iopl_valid_data: NULL upl");
+       if (vector_upl_is_valid(upl))
+               panic("iopl_valid_data: vector upl");
+       if ((upl->flags & (UPL_DEVICE_MEMORY|UPL_SHADOWED|UPL_ACCESS_BLOCKED|UPL_IO_WIRE|UPL_INTERNAL)) != UPL_IO_WIRE)
+               panic("iopl_valid_data: unsupported upl, flags = %x", upl->flags);
+
+       object = upl->map_object;
+
+       if (object == kernel_object || object == compressor_object)
+               panic("iopl_valid_data: object == kernel or compressor");
+
+       if (object->purgable == VM_PURGABLE_VOLATILE)
+               panic("iopl_valid_data: object == VM_PURGABLE_VOLATILE");
+
+       size = upl->size;
+
+       vm_object_lock(object);
+
+       if (object->vo_size == size && object->resident_page_count == (size / PAGE_SIZE))
+               nxt_page = (vm_page_t)queue_first(&object->memq);
+       else
+               offset = 0 + upl->offset - object->paging_offset;
+
+       while (size) {
+
+               if (nxt_page != VM_PAGE_NULL) {
+                       m = nxt_page;
+                       nxt_page = (vm_page_t)queue_next(&nxt_page->listq);
+               } else {
+                       m = vm_page_lookup(object, offset);
+                       offset += PAGE_SIZE;
+
+                       if (m == VM_PAGE_NULL)
+                               panic("iopl_valid_data: missing expected page at offset %lx", (long)offset);
+               }
+               if (m->busy) {
+                       if (!m->absent)
+                               panic("iopl_valid_data: busy page w/o absent");
+
+                       if (m->pageq.next || m->pageq.prev)
+                               panic("iopl_valid_data: busy+absent page on page queue");
+
+                       m->absent = FALSE;
+                       m->dirty = TRUE;
+                       m->wire_count++;
+                       wired_count++;
+                       
+                       PAGE_WAKEUP_DONE(m);
+               }
+               size -= PAGE_SIZE;
+       }
+       if (wired_count) {
+               object->wired_page_count += wired_count;
+
+               vm_page_lockspin_queues();
+               vm_page_wire_count += wired_count;
+               vm_page_unlock_queues();
+       }
+       vm_object_unlock(object);
+}
+
+
+
+
 void
 vm_object_set_pmap_cache_attr(
                vm_object_t             object,
@@ -5899,6 +7390,14 @@ vm_object_iopl_request(
        int                     dw_count;
        int                     dw_limit;
        int                     dw_index;
+       boolean_t               caller_lookup;
+       int                     io_tracking_flag = 0;
+       int                     interruptible;
+
+       boolean_t               set_cache_attr_needed = FALSE;
+       boolean_t               free_wired_pages = FALSE;
+       int                     fast_path_possible = 0;
+       
 
        if (cntrl_flags & ~UPL_VALID_FLAGS) {
                /*
@@ -5932,7 +7431,7 @@ vm_object_iopl_request(
                 */
                assert(! (cntrl_flags & UPL_ENCRYPT));
        }
-       if (cntrl_flags & UPL_NOZEROFILL)
+       if (cntrl_flags & (UPL_NOZEROFILL | UPL_NOZEROFILLIO))
                no_zero_fill = TRUE;
 
        if (cntrl_flags & UPL_COPYOUT_FROM)
@@ -5940,20 +7439,21 @@ vm_object_iopl_request(
        else
                prot = VM_PROT_READ | VM_PROT_WRITE;
 
-       if (((size/PAGE_SIZE) > MAX_UPL_SIZE) && !object->phys_contiguous)
-               size = MAX_UPL_SIZE * PAGE_SIZE;
-
-       if (cntrl_flags & UPL_SET_INTERNAL) {
-               if (page_list_count != NULL)
-                       *page_list_count = MAX_UPL_SIZE;
-       }
-       if (((cntrl_flags & UPL_SET_INTERNAL) && !(object->phys_contiguous)) &&
-           ((page_list_count != NULL) && (*page_list_count != 0) && *page_list_count < (size/page_size)))
-               return KERN_INVALID_ARGUMENT;
-
        if ((!object->internal) && (object->paging_offset != 0))
                panic("vm_object_iopl_request: external object with non-zero paging offset\n");
 
+#if CONFIG_IOSCHED || UPL_DEBUG
+       if ((object->io_tracking && object != kernel_object) || upl_debug_enabled)
+               io_tracking_flag |= UPL_CREATE_IO_TRACKING;
+#endif
+
+#if CONFIG_IOSCHED
+       if (object->io_tracking) {
+               /* Check if we're dealing with the kernel object. We do not support expedite on kernel object UPLs */
+               if (object != kernel_object)
+                       io_tracking_flag |= UPL_CREATE_EXPEDITE_SUP;
+       }
+#endif
 
        if (object->phys_contiguous)
                psize = PAGE_SIZE;
@@ -5961,7 +7461,7 @@ vm_object_iopl_request(
                psize = size;
 
        if (cntrl_flags & UPL_SET_INTERNAL) {
-               upl = upl_create(UPL_CREATE_INTERNAL | UPL_CREATE_LITE, UPL_IO_WIRE, psize);
+               upl = upl_create(UPL_CREATE_INTERNAL | UPL_CREATE_LITE | io_tracking_flag, UPL_IO_WIRE, psize);
 
                user_page_list = (upl_page_info_t *) (((uintptr_t)upl) + sizeof(struct upl));
                lite_list = (wpl_array_t) (((uintptr_t)user_page_list) +
@@ -5971,7 +7471,7 @@ vm_object_iopl_request(
                        lite_list = NULL;
                }
        } else {
-               upl = upl_create(UPL_CREATE_LITE, UPL_IO_WIRE, psize);
+               upl = upl_create(UPL_CREATE_LITE | io_tracking_flag, UPL_IO_WIRE, psize);
 
                lite_list = (wpl_array_t) (((uintptr_t)upl) + sizeof(struct upl));
                if (size == 0) {
@@ -6012,11 +7512,28 @@ vm_object_iopl_request(
                upl->flags |= UPL_ACCESS_BLOCKED;
        }
 
-       if (object->phys_contiguous) {
-#if UPL_DEBUG
+       if (!(cntrl_flags & (UPL_NEED_32BIT_ADDR | UPL_BLOCK_ACCESS)) &&
+           object->purgable != VM_PURGABLE_VOLATILE &&
+           object->purgable != VM_PURGABLE_EMPTY &&
+           object->copy == NULL &&
+           size == object->vo_size &&
+           offset == 0 &&
+           object->resident_page_count == 0 &&
+           object->shadow == NULL &&
+           object->pager == NULL)
+       {
+               fast_path_possible = 1;
+               set_cache_attr_needed = TRUE;
+       }
+
+#if CONFIG_IOSCHED || UPL_DEBUG
+       if (upl->flags & UPL_TRACKED_BY_OBJECT) {
                vm_object_activity_begin(object);
                queue_enter(&object->uplq, upl, upl_t, uplq);
-#endif /* UPL_DEBUG */
+       }
+#endif
+
+       if (object->phys_contiguous) {
 
                if (upl->flags & UPL_ACCESS_BLOCKED) {
                        assert(!object->blocked_access);
@@ -6045,20 +7562,31 @@ vm_object_iopl_request(
                }
                return KERN_SUCCESS;
        }
-       if (object != kernel_object) {
+       if (object != kernel_object && object != compressor_object) {
                /*
                 * Protect user space from future COW operations
                 */
+#if VM_OBJECT_TRACKING_OP_TRUESHARE
+               if (!object->true_share &&
+                   vm_object_tracking_inited) {
+                       void *bt[VM_OBJECT_TRACKING_BTDEPTH];
+                       int num = 0;
+
+                       num = OSBacktrace(bt,
+                                         VM_OBJECT_TRACKING_BTDEPTH);
+                       btlog_add_entry(vm_object_tracking_btlog,
+                                       object,
+                                       VM_OBJECT_TRACKING_OP_TRUESHARE,
+                                       bt,
+                                       num);
+               }
+#endif /* VM_OBJECT_TRACKING_OP_TRUESHARE */
+
                object->true_share = TRUE;
 
                if (object->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC)
                        object->copy_strategy = MEMORY_OBJECT_COPY_DELAY;
        }
-#if UPL_DEBUG
-       vm_object_activity_begin(object);
-       queue_enter(&object->uplq, upl, upl_t, uplq);
-#endif /* UPL_DEBUG */
 
        if (!(cntrl_flags & UPL_COPYOUT_FROM) &&
            object->copy != VM_OBJECT_NULL) {
@@ -6092,12 +7620,99 @@ vm_object_iopl_request(
                iopl_cow_pages += size >> PAGE_SHIFT;
 #endif
        }
-
+       if (cntrl_flags & UPL_SET_INTERRUPTIBLE)
+               interruptible = THREAD_ABORTSAFE;
+       else
+               interruptible = THREAD_UNINT;
 
        entry = 0;
 
        xfer_size = size;
        dst_offset = offset;
+       dw_count = 0;
+
+       if (fast_path_possible) {
+               int     wired_count = 0;
+
+               while (xfer_size) {
+                       
+                       while ( (dst_page = vm_page_grab()) == VM_PAGE_NULL) {
+                               OSAddAtomic(size_in_pages, &vm_upl_wait_for_pages);
+
+                               VM_DEBUG_EVENT(vm_iopl_page_wait, VM_IOPL_PAGE_WAIT, DBG_FUNC_START, vm_upl_wait_for_pages, 0, 0, 0);
+
+                               if (vm_page_wait(interruptible) == FALSE) {
+                                       /*
+                                        * interrupted case
+                                        */
+                                       OSAddAtomic(-size_in_pages, &vm_upl_wait_for_pages);
+
+                                       VM_DEBUG_EVENT(vm_iopl_page_wait, VM_IOPL_PAGE_WAIT, DBG_FUNC_END, vm_upl_wait_for_pages, 0, 0, -1);
+
+                                       if (wired_count) {
+                                               vm_page_lockspin_queues();
+                                               vm_page_wire_count += wired_count;
+                                               vm_page_unlock_queues();
+
+                                               free_wired_pages = TRUE;
+                                       }
+                                       ret = MACH_SEND_INTERRUPTED;
+
+                                       goto return_err;
+                               }
+                               OSAddAtomic(-size_in_pages, &vm_upl_wait_for_pages);
+
+                               VM_DEBUG_EVENT(vm_iopl_page_wait, VM_IOPL_PAGE_WAIT, DBG_FUNC_END, vm_upl_wait_for_pages, 0, 0, 0);
+                       }
+                       if (no_zero_fill == FALSE)
+                               vm_page_zero_fill(dst_page);
+                       else
+                               dst_page->absent = TRUE;
+
+                       dst_page->reference = TRUE;
+
+                       if (!(cntrl_flags & UPL_COPYOUT_FROM)) {
+                               SET_PAGE_DIRTY(dst_page, FALSE);        
+                       }
+                       if (dst_page->absent == FALSE) {
+                               assert(object->purgable != VM_PURGABLE_VOLATILE);
+                               assert(object->purgable != VM_PURGABLE_EMPTY);
+                               dst_page->wire_count++;
+                               wired_count++;
+
+                               PAGE_WAKEUP_DONE(dst_page);
+                       }
+                       vm_page_insert_internal(dst_page, object, dst_offset, FALSE, TRUE, TRUE);
+                       
+                       lite_list[entry>>5] |= 1 << (entry & 31);
+               
+                       if (dst_page->phys_page > upl->highest_page)
+                               upl->highest_page = dst_page->phys_page;
+
+                       if (user_page_list) {
+                               user_page_list[entry].phys_addr = dst_page->phys_page;
+                               user_page_list[entry].absent    = dst_page->absent;
+                               user_page_list[entry].dirty     = dst_page->dirty;
+                               user_page_list[entry].precious  = FALSE;
+                               user_page_list[entry].pageout   = FALSE;
+                               user_page_list[entry].device    = FALSE;
+                               user_page_list[entry].needed    = FALSE;
+                               user_page_list[entry].speculative = FALSE;
+                               user_page_list[entry].cs_validated = FALSE;
+                               user_page_list[entry].cs_tainted = FALSE;
+                       }
+                       entry++;
+                       dst_offset += PAGE_SIZE_64;
+                       xfer_size -= PAGE_SIZE;
+                       size_in_pages--;
+               }
+               if (wired_count) {
+                       vm_page_lockspin_queues();
+                       vm_page_wire_count += wired_count;
+                       vm_page_unlock_queues();
+               }
+               goto finish;
+       }
 
        fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL;
        fault_info.user_tag  = 0;
@@ -6108,9 +7723,10 @@ vm_object_iopl_request(
        fault_info.io_sync = FALSE;
        fault_info.cs_bypass = FALSE;
        fault_info.mark_zf_absent = TRUE;
+       fault_info.interruptible = interruptible;
+       fault_info.batch_pmap_op = TRUE;
 
        dwp = &dw_array[0];
-       dw_count = 0;
        dw_limit = DELAYED_WORK_LIMIT(DEFAULT_DELAYED_WORK_LIMIT);
 
        while (xfer_size) {
@@ -6136,30 +7752,42 @@ vm_object_iopl_request(
 
                   if (object == kernel_object)
                           panic("vm_object_iopl_request: missing/bad page in kernel object\n");
+                  if (object == compressor_object)
+                          panic("vm_object_iopl_request: missing/bad page in compressor object\n");
+
+                  if (cntrl_flags & UPL_REQUEST_NO_FAULT) {
+                          ret = KERN_MEMORY_ERROR;
+                          goto return_err;
+                  }
+                  set_cache_attr_needed = TRUE;
+
+                   /*
+                   * We just looked up the page and the result remains valid
+                   * until the object lock is release, so send it to
+                   * vm_fault_page() (as "dst_page"), to avoid having to
+                   * look it up again there.
+                   */
+                  caller_lookup = TRUE;
 
                   do {
                        vm_page_t       top_page;
                        kern_return_t   error_code;
-                       int             interruptible;
 
-                       if (cntrl_flags & UPL_SET_INTERRUPTIBLE)
-                               interruptible = THREAD_ABORTSAFE;
-                       else
-                               interruptible = THREAD_UNINT;
-
-                       fault_info.interruptible = interruptible;
                        fault_info.cluster_size = xfer_size;
-                       fault_info.batch_pmap_op = TRUE;
 
                        vm_object_paging_begin(object);
 
                        result = vm_fault_page(object, dst_offset,
-                                              prot | VM_PROT_WRITE, FALSE, 
+                                              prot | VM_PROT_WRITE, FALSE,
+                                              caller_lookup,
                                               &prot, &dst_page, &top_page,
                                               (int *)0,
                                               &error_code, no_zero_fill,
                                               FALSE, &fault_info);
 
+                        /* our lookup is no longer valid at this point */
+                       caller_lookup = FALSE;
+
                        switch (result) {
 
                        case VM_FAULT_SUCCESS:
@@ -6250,6 +7878,11 @@ vm_object_iopl_request(
                if (upl->flags & UPL_KERNEL_OBJECT)
                        goto record_phys_addr;
 
+               if (dst_page->compressor) {
+                       dst_page->busy = TRUE;
+                       goto record_phys_addr;
+               }
+
                if (dst_page->cleaning) {
                        /*
                         * Someone else is cleaning this page in place.
@@ -6351,6 +7984,11 @@ vm_object_iopl_request(
                if (!(cntrl_flags & UPL_COPYOUT_FROM)) {
                        SET_PAGE_DIRTY(dst_page, TRUE); 
                }
+               if ((cntrl_flags & UPL_REQUEST_FORCE_COHERENCY) && dst_page->written_by_kernel == TRUE) {
+                       pmap_sync_page_attributes_phys(dst_page->phys_page);
+                       dst_page->written_by_kernel = FALSE;
+               }
+
 record_phys_addr:
                if (dst_page->busy)
                        upl->flags |= UPL_HAS_BUSY;
@@ -6377,17 +8015,19 @@ record_phys_addr:
                        user_page_list[entry].cs_validated = dst_page->cs_validated;
                        user_page_list[entry].cs_tainted = dst_page->cs_tainted;
                }
-               if (object != kernel_object) {
+               if (object != kernel_object && object != compressor_object) {
                        /*
                         * someone is explicitly grabbing this page...
                         * update clustered and speculative state
                         * 
                         */
-                       VM_PAGE_CONSUME_CLUSTERED(dst_page);
+                       if (dst_page->clustered)
+                               VM_PAGE_CONSUME_CLUSTERED(dst_page);
                }
                entry++;
                dst_offset += PAGE_SIZE_64;
                xfer_size -= PAGE_SIZE;
+               size_in_pages--;
 
                if (dwp->dw_mask) {
                        VM_PAGE_ADD_DELAYED_WORK(dwp, dst_page, dw_count);
@@ -6403,7 +8043,9 @@ record_phys_addr:
        if (dw_count)
                vm_page_do_delayed_work(object, &dw_array[0], dw_count);
 
-       vm_object_set_pmap_cache_attr(object, user_page_list, entry, TRUE);
+finish:
+       if (user_page_list && set_cache_attr_needed == TRUE)
+               vm_object_set_pmap_cache_attr(object, user_page_list, entry, TRUE);
 
        if (page_list_count != NULL) {
                if (upl->flags & UPL_INTERNAL)
@@ -6465,7 +8107,7 @@ return_err:
                }
                vm_page_lock_queues();
 
-               if (dst_page->absent) {
+               if (dst_page->absent || free_wired_pages == TRUE) {
                        vm_page_free(dst_page);
 
                        need_unwire = FALSE;
@@ -6547,15 +8189,28 @@ upl_transpose(
                 * Make each UPL point to the correct VM object, i.e. the
                 * object holding the pages that the UPL refers to...
                 */
-#if UPL_DEBUG
-               queue_remove(&object1->uplq, upl1, upl_t, uplq);
-               queue_remove(&object2->uplq, upl2, upl_t, uplq);
+#if CONFIG_IOSCHED || UPL_DEBUG
+               if ((upl1->flags & UPL_TRACKED_BY_OBJECT) || (upl2->flags & UPL_TRACKED_BY_OBJECT)) {
+                       vm_object_lock(object1);
+                       vm_object_lock(object2);
+               }
+               if (upl1->flags & UPL_TRACKED_BY_OBJECT)
+                       queue_remove(&object1->uplq, upl1, upl_t, uplq);
+               if (upl2->flags & UPL_TRACKED_BY_OBJECT)
+                       queue_remove(&object2->uplq, upl2, upl_t, uplq);
 #endif
                upl1->map_object = object2;
                upl2->map_object = object1;
-#if UPL_DEBUG
-               queue_enter(&object1->uplq, upl2, upl_t, uplq);
-               queue_enter(&object2->uplq, upl1, upl_t, uplq);
+
+#if CONFIG_IOSCHED || UPL_DEBUG
+               if (upl1->flags & UPL_TRACKED_BY_OBJECT)
+                       queue_enter(&object2->uplq, upl1, upl_t, uplq);
+               if (upl2->flags & UPL_TRACKED_BY_OBJECT)
+                       queue_enter(&object1->uplq, upl2, upl_t, uplq);
+               if ((upl1->flags & UPL_TRACKED_BY_OBJECT) || (upl2->flags & UPL_TRACKED_BY_OBJECT)) {
+                       vm_object_unlock(object2);
+                       vm_object_unlock(object1);
+               }
 #endif
        }
 
@@ -6701,13 +8356,14 @@ vm_paging_map_init(void)
  */
 kern_return_t
 vm_paging_map_object(
-       vm_map_offset_t         *address,
        vm_page_t               page,
        vm_object_t             object,
        vm_object_offset_t      offset,
-       vm_map_size_t           *size,
        vm_prot_t               protection,
-       boolean_t               can_unlock_object)
+       boolean_t               can_unlock_object,
+       vm_map_size_t           *size,          /* IN/OUT */
+       vm_map_offset_t         *address,       /* OUT */
+       boolean_t               *need_unmap)    /* OUT */
 {
        kern_return_t           kr;
        vm_map_offset_t         page_map_offset;
@@ -6715,8 +8371,18 @@ vm_paging_map_object(
        vm_object_offset_t      object_offset;
        int                     i;
 
-       
        if (page != VM_PAGE_NULL && *size == PAGE_SIZE) {
+               /* use permanent 1-to-1 kernel mapping of physical memory ? */
+#if __x86_64__
+               *address = (vm_map_offset_t)
+                       PHYSMAP_PTOV((pmap_paddr_t)page->phys_page <<
+                                    PAGE_SHIFT);
+               *need_unmap = FALSE;
+               return KERN_SUCCESS;
+#else
+#warn "vm_paging_map_object: no 1-to-1 kernel mapping of physical memory..."
+#endif
+
                assert(page->busy);
                /*
                 * Use one of the pre-allocated kernel virtual addresses
@@ -6757,9 +8423,12 @@ vm_paging_map_object(
                         */
                        vm_paging_page_waiter_total++;
                        vm_paging_page_waiter++;
-                       thread_sleep_fast_usimple_lock(&vm_paging_page_waiter,
-                                                      &vm_paging_lock,
-                                                      THREAD_UNINT);
+                       kr = assert_wait((event_t)&vm_paging_page_waiter, THREAD_UNINT);
+                       if (kr == THREAD_WAITING) {
+                               simple_unlock(&vm_paging_lock);
+                               kr = thread_block(THREAD_CONTINUE_NULL);
+                               simple_lock(&vm_paging_lock);
+                       }
                        vm_paging_page_waiter--;
                        /* ... and try again */
                }
@@ -6793,6 +8462,7 @@ vm_paging_map_object(
                        vm_paging_objects_mapped++;
                        vm_paging_pages_mapped++; 
                        *address = page_map_offset;
+                       *need_unmap = TRUE;
 
                        /* all done and mapped, ready to use ! */
                        return KERN_SUCCESS;
@@ -6808,11 +8478,15 @@ vm_paging_map_object(
        }
 
        if (! can_unlock_object) {
+               *address = 0;
+               *size = 0;
+               *need_unmap = FALSE;
                return KERN_NOT_SUPPORTED;
        }
 
        object_offset = vm_object_trunc_page(offset);
-       map_size = vm_map_round_page(*size);
+       map_size = vm_map_round_page(*size,
+                                    VM_MAP_PAGE_MASK(kernel_map));
 
        /*
         * Try and map the required range of the object
@@ -6836,6 +8510,7 @@ vm_paging_map_object(
        if (kr != KERN_SUCCESS) {
                *address = 0;
                *size = 0;
+               *need_unmap = FALSE;
                vm_object_deallocate(object);   /* for the map entry */
                vm_object_lock(object);
                return kr;
@@ -6867,6 +8542,7 @@ vm_paging_map_object(
                        assert(kr == KERN_SUCCESS);
                        *address = 0;
                        *size = 0;
+                       *need_unmap = FALSE;
                        vm_object_lock(object);
                        return KERN_MEMORY_ERROR;
                }
@@ -6885,6 +8561,8 @@ vm_paging_map_object(
        vm_paging_objects_mapped_slow++;
        vm_paging_pages_mapped_slow += (unsigned long) (map_size / PAGE_SIZE_64);
 
+       *need_unmap = TRUE;
+
        return KERN_SUCCESS;
 }
 
@@ -6945,7 +8623,7 @@ vm_paging_unmap_object(
        }
 }
 
-#if CRYPTO
+#if ENCRYPTED_SWAP
 /*
  * Encryption data.
  * "iv" is the "initial vector".  Ideally, we want to
@@ -7069,6 +8747,7 @@ vm_page_encrypt(
 {
        kern_return_t           kr;
        vm_map_size_t           kernel_mapping_size;
+       boolean_t               kernel_mapping_needs_unmap;
        vm_offset_t             kernel_vaddr;
        union {
                unsigned char   aes_iv[AES_BLOCK_SIZE];
@@ -7109,13 +8788,15 @@ vm_page_encrypt(
                 * its contents and encrypt them.
                 */
                kernel_mapping_size = PAGE_SIZE;
-               kr = vm_paging_map_object(&kernel_mapping_offset,
-                                         page,
+               kernel_mapping_needs_unmap = FALSE;
+               kr = vm_paging_map_object(page,
                                          page->object,
                                          page->offset,
-                                         &kernel_mapping_size,
                                          VM_PROT_READ | VM_PROT_WRITE,
-                                         FALSE);
+                                         FALSE,
+                                         &kernel_mapping_size,
+                                         &kernel_mapping_offset,
+                                         &kernel_mapping_needs_unmap);
                if (kr != KERN_SUCCESS) {
                        panic("vm_page_encrypt: "
                              "could not map page in kernel: 0x%x\n",
@@ -7123,6 +8804,7 @@ vm_page_encrypt(
                }
        } else {
                kernel_mapping_size = 0;
+               kernel_mapping_needs_unmap = FALSE;
        }
        kernel_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping_offset);
 
@@ -7166,7 +8848,7 @@ vm_page_encrypt(
         * if we had to map it ourselves.  Otherwise, let
         * the caller undo the mapping if needed.
         */
-       if (kernel_mapping_size != 0) {
+       if (kernel_mapping_needs_unmap) {
                vm_paging_unmap_object(page->object,
                                       kernel_mapping_offset,
                                       kernel_mapping_offset + kernel_mapping_size);
@@ -7209,6 +8891,7 @@ vm_page_decrypt(
        kern_return_t           kr;
        vm_map_size_t           kernel_mapping_size;
        vm_offset_t             kernel_vaddr;
+       boolean_t               kernel_mapping_needs_unmap;
        union {
                unsigned char   aes_iv[AES_BLOCK_SIZE];
                struct {
@@ -7237,13 +8920,15 @@ vm_page_decrypt(
                 * its contents and decrypt them.
                 */
                kernel_mapping_size = PAGE_SIZE;
-               kr = vm_paging_map_object(&kernel_mapping_offset,
-                                         page,
+               kernel_mapping_needs_unmap = FALSE;
+               kr = vm_paging_map_object(page,
                                          page->object,
                                          page->offset,
-                                         &kernel_mapping_size,
                                          VM_PROT_READ | VM_PROT_WRITE,
-                                         FALSE);
+                                         FALSE,
+                                         &kernel_mapping_size,
+                                         &kernel_mapping_offset,
+                                         &kernel_mapping_needs_unmap);
                if (kr != KERN_SUCCESS) {
                        panic("vm_page_decrypt: "
                              "could not map page in kernel: 0x%x\n",
@@ -7251,6 +8936,7 @@ vm_page_decrypt(
                }
        } else {
                kernel_mapping_size = 0;
+               kernel_mapping_needs_unmap = FALSE;
        }
        kernel_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping_offset);
 
@@ -7288,7 +8974,7 @@ vm_page_decrypt(
         * if we had to map it ourselves.  Otherwise, let
         * the caller undo the mapping if needed.
         */
-       if (kernel_mapping_size != 0) {
+       if (kernel_mapping_needs_unmap) {
                vm_paging_unmap_object(page->object,
                                       kernel_vaddr,
                                       kernel_vaddr + PAGE_SIZE);
@@ -7464,7 +9150,7 @@ process_upl_to_encrypt:
                goto process_upl_to_encrypt;
 }
 
-#else /* CRYPTO */
+#else /* ENCRYPTED_SWAP */
 void
 upl_encrypt(
        __unused upl_t                  upl,
@@ -7487,7 +9173,7 @@ vm_page_decrypt(
 {
 }
 
-#endif /* CRYPTO */
+#endif /* ENCRYPTED_SWAP */
 
 /*
  * page->object must be locked
@@ -7862,27 +9548,48 @@ upl_set_referenced(
        upl_unlock(upl);
 }
 
+#if CONFIG_IOSCHED
+void
+upl_set_blkno(
+       upl_t           upl,
+       vm_offset_t     upl_offset,
+       int             io_size,
+       int64_t         blkno)
+{
+               int i,j;
+               if ((upl->flags & UPL_EXPEDITE_SUPPORTED) == 0)
+                       return;
+                       
+               assert(upl->upl_reprio_info != 0);      
+               for(i = (int)(upl_offset / PAGE_SIZE), j = 0; j < io_size; i++, j += PAGE_SIZE) {
+                       UPL_SET_REPRIO_INFO(upl, i, blkno, io_size);
+               }
+}
+#endif
+
 boolean_t
 vm_page_is_slideable(vm_page_t m)
 {
        boolean_t result = FALSE;
-       vm_object_t slide_object = slide_info.slide_object;
-       mach_vm_offset_t start = slide_info.start;
-       mach_vm_offset_t end = slide_info.end;
+       vm_shared_region_slide_info_t si;
+
+       vm_object_lock_assert_held(m->object);
 
        /* make sure our page belongs to the one object allowed to do this */
-       if (slide_object == VM_OBJECT_NULL) {
-               return result;
+       if (!m->object->object_slid) {
+               goto done;
        }
 
-       /*Should we traverse down the chain?*/
-       if (m->object != slide_object) {
-               return result;
+       si = m->object->vo_slide_info;
+       if (si == NULL) {
+               goto done;
        }
 
-       if(!m->slid && (start <= m->offset && end > m->offset)) {
+       if(!m->slid && (si->start <= m->offset && si->end > m->offset)) {
                result = TRUE;
        }
+
+done:
        return result;
 }
 
@@ -7895,10 +9602,13 @@ vm_page_slide(
 {
        kern_return_t           kr;
        vm_map_size_t           kernel_mapping_size;
+       boolean_t               kernel_mapping_needs_unmap;
        vm_offset_t             kernel_vaddr;
        uint32_t                pageIndex = 0;
 
        assert(!page->slid);
+       assert(page->object->object_slid);
+       vm_object_lock_assert_exclusive(page->object);
 
        if (page->error)
                return KERN_FAILURE;
@@ -7917,13 +9627,15 @@ vm_page_slide(
                 * its contents and decrypt them.
                 */
                kernel_mapping_size = PAGE_SIZE;
-               kr = vm_paging_map_object(&kernel_mapping_offset,
-                                         page,
+               kernel_mapping_needs_unmap = FALSE;
+               kr = vm_paging_map_object(page,
                                          page->object,
                                          page->offset,
-                                         &kernel_mapping_size,
                                          VM_PROT_READ | VM_PROT_WRITE,
-                                         FALSE);
+                                         FALSE,
+                                         &kernel_mapping_size,
+                                         &kernel_mapping_offset,
+                                         &kernel_mapping_needs_unmap);
                if (kr != KERN_SUCCESS) {
                        panic("vm_page_slide: "
                              "could not map page in kernel: 0x%x\n",
@@ -7931,6 +9643,7 @@ vm_page_slide(
                }
        } else {
                kernel_mapping_size = 0;
+               kernel_mapping_needs_unmap = FALSE;
        }
        kernel_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping_offset);
 
@@ -7940,14 +9653,29 @@ vm_page_slide(
 
        /*assert that slide_file_info.start/end are page-aligned?*/
 
-       pageIndex = (uint32_t)((page->offset - slide_info.start)/PAGE_SIZE);
-       kr = vm_shared_region_slide(kernel_vaddr, pageIndex);
+       assert(!page->slid);
+       assert(page->object->object_slid);
+
+       /* on some platforms this is an extern int, on others it's a cpp macro */
+       __unreachable_ok_push
+        /* TODO: Consider this */
+       if (!TEST_PAGE_SIZE_4K) {
+               for (int i = 0; i < 4; i++) {
+                       pageIndex = (uint32_t)((page->offset - page->object->vo_slide_info->start)/0x1000);
+                       kr = vm_shared_region_slide_page(page->object->vo_slide_info, kernel_vaddr + (0x1000*i), pageIndex + i);
+               }
+       } else {
+               pageIndex = (uint32_t)((page->offset - page->object->vo_slide_info->start)/PAGE_SIZE);
+               kr = vm_shared_region_slide_page(page->object->vo_slide_info, kernel_vaddr, pageIndex);
+       }
+       __unreachable_ok_pop
+
        vm_page_slide_counter++;
 
        /*
         * Unmap the page from the kernel's address space,
         */
-       if (kernel_mapping_size != 0) {
+       if (kernel_mapping_needs_unmap) {
                vm_paging_unmap_object(page->object,
                                       kernel_vaddr,
                                       kernel_vaddr + PAGE_SIZE);
@@ -7977,6 +9705,19 @@ vm_page_slide(
        return kr;
 }
 
+void inline memoryshot(unsigned int event, unsigned int control)
+{
+       if (vm_debug_events) {
+               KERNEL_DEBUG_CONSTANT1((MACHDBG_CODE(DBG_MACH_VM_PRESSURE, event)) | control,
+                                       vm_page_active_count, vm_page_inactive_count,
+                                       vm_page_free_count, vm_page_speculative_count,
+                                       vm_page_throttled_count);
+       } else {
+               (void) event;
+               (void) control;
+       }
+
+}
 
 #ifdef MACH_BSD
 
@@ -8005,7 +9746,6 @@ ppnum_t  upl_phys_page(upl_page_info_t *upl, int index)
        return(UPL_PHYS_PAGE(upl, index));
 }
 
-
 void
 vm_countdirtypages(void)
 {
@@ -8121,3 +9861,80 @@ int  upl_ubc_alias_get(upl_t upl, uintptr_t * al, uintptr_t * al2)
        return KERN_SUCCESS;
 }
 #endif /* UPL_DEBUG */
+
+#if VM_PRESSURE_EVENTS
+/*
+ * Upward trajectory.
+ */
+extern boolean_t vm_compressor_low_on_space(void);
+
+boolean_t
+VM_PRESSURE_NORMAL_TO_WARNING(void)    {
+
+       if (DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS) {
+               
+               /* Available pages below our threshold */
+               if (memorystatus_available_pages < memorystatus_available_pages_pressure) {
+                       /* No frozen processes to kill */
+                       if (memorystatus_frozen_count == 0) {
+                               /* Not enough suspended processes available. */
+                               if (memorystatus_suspended_count < MEMORYSTATUS_SUSPENDED_THRESHOLD) {
+                                       return TRUE;
+                               }
+                       }
+               }
+               return FALSE;
+
+       } else {
+               return ((AVAILABLE_NON_COMPRESSED_MEMORY < VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD) ? 1 : 0);
+       }
+}
+
+boolean_t
+VM_PRESSURE_WARNING_TO_CRITICAL(void) {
+
+       if (DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS) {
+               /* Available pages below our threshold */
+               if (memorystatus_available_pages < memorystatus_available_pages_critical) {
+                       return TRUE;
+               }
+               return FALSE;
+       } else {
+               return (vm_compressor_low_on_space() || (AVAILABLE_NON_COMPRESSED_MEMORY < ((12 * VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD) / 10)) ? 1 : 0);
+       }
+}
+
+/*
+ * Downward trajectory.
+ */
+boolean_t
+VM_PRESSURE_WARNING_TO_NORMAL(void) {
+
+       if (DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS) {
+               /* Available pages above our threshold */
+               unsigned int target_threshold = memorystatus_available_pages_pressure + ((15 * memorystatus_available_pages_pressure) / 100);
+               if (memorystatus_available_pages > target_threshold) {
+                       return TRUE;
+               }
+               return FALSE;
+       } else {
+               return ((AVAILABLE_NON_COMPRESSED_MEMORY > ((12 * VM_PAGE_COMPRESSOR_COMPACT_THRESHOLD) / 10)) ? 1 : 0);
+       }
+}
+
+boolean_t
+VM_PRESSURE_CRITICAL_TO_WARNING(void) {
+
+       if (DEFAULT_PAGER_IS_ACTIVE || DEFAULT_FREEZER_IS_ACTIVE || DEFAULT_FREEZER_COMPRESSED_PAGER_IS_SWAPLESS) {
+               /* Available pages above our threshold */
+               unsigned int target_threshold = memorystatus_available_pages_critical + ((15 * memorystatus_available_pages_critical) / 100);
+               if (memorystatus_available_pages > target_threshold) {
+                       return TRUE;
+               }
+               return FALSE;
+       } else {
+               return ((AVAILABLE_NON_COMPRESSED_MEMORY > ((14 * VM_PAGE_COMPRESSOR_SWAP_UNTHROTTLE_THRESHOLD) / 10)) ? 1 : 0);
+       }
+}
+#endif /* VM_PRESSURE_EVENTS */
+