+ if (queueit == TRUE) {
+ if (m_object->purgable == VM_PURGABLE_EMPTY) {
+ vm_page_deactivate(mem);
+ } else {
+ vm_page_activate(mem);
+ }
+ }
+
+ VM_CHECK_MEMORYSTATUS;
+ }
+ VM_PAGE_CHECK(mem);
+}
+
+/*
+ * vm_page_deactivate:
+ *
+ * Returns the given page to the inactive list,
+ * indicating that no physical maps have access
+ * to this page. [Used by the physical mapping system.]
+ *
+ * The page queues must be locked.
+ */
+void
+vm_page_deactivate(
+ vm_page_t m)
+{
+ vm_page_deactivate_internal(m, TRUE);
+}
+
+
+void
+vm_page_deactivate_internal(
+ vm_page_t m,
+ boolean_t clear_hw_reference)
+{
+ vm_object_t m_object;
+
+ m_object = VM_PAGE_OBJECT(m);
+
+ VM_PAGE_CHECK(m);
+ assert(m_object != kernel_object);
+ assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr);
+
+// dbgLog(VM_PAGE_GET_PHYS_PAGE(m), vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
+ LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
+ /*
+ * This page is no longer very interesting. If it was
+ * interesting (active or inactive/referenced), then we
+ * clear the reference bit and (re)enter it in the
+ * inactive queue. Note wired pages should not have
+ * their reference bit cleared.
+ */
+ assert( !(m->vmp_absent && !m->vmp_unusual));
+
+ if (m->vmp_gobbled) { /* can this happen? */
+ assert( !VM_PAGE_WIRED(m));
+
+ if (!m->vmp_private && !m->vmp_fictitious) {
+ vm_page_wire_count--;
+ }
+ vm_page_gobble_count--;
+ m->vmp_gobbled = FALSE;
+ }
+ /*
+ * if this page is currently on the pageout queue, we can't do the
+ * vm_page_queues_remove (which doesn't handle the pageout queue case)
+ * and we can't remove it manually since we would need the object lock
+ * (which is not required here) to decrement the activity_in_progress
+ * reference which is held on the object while the page is in the pageout queue...
+ * just let the normal laundry processing proceed
+ */
+ if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious ||
+ (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) ||
+ (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) ||
+ VM_PAGE_WIRED(m)) {
+ return;
+ }
+ if (!m->vmp_absent && clear_hw_reference == TRUE) {
+ pmap_clear_reference(VM_PAGE_GET_PHYS_PAGE(m));
+ }
+
+ m->vmp_reference = FALSE;
+ m->vmp_no_cache = FALSE;
+
+ if (!VM_PAGE_INACTIVE(m)) {
+ vm_page_queues_remove(m, FALSE);
+
+ if (!VM_DYNAMIC_PAGING_ENABLED() &&
+ m->vmp_dirty && m_object->internal &&
+ (m_object->purgable == VM_PURGABLE_DENY ||
+ m_object->purgable == VM_PURGABLE_NONVOLATILE ||
+ m_object->purgable == VM_PURGABLE_VOLATILE)) {
+ vm_page_check_pageable_safe(m);
+ vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq);
+ m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q;
+ vm_page_throttled_count++;
+ } else {
+ if (m_object->named && m_object->ref_count == 1) {
+ vm_page_speculate(m, FALSE);
+#if DEVELOPMENT || DEBUG
+ vm_page_speculative_recreated++;
+#endif
+ } else {
+ vm_page_enqueue_inactive(m, FALSE);
+ }
+ }
+ }
+}
+
+/*
+ * vm_page_enqueue_cleaned
+ *
+ * Put the page on the cleaned queue, mark it cleaned, etc.
+ * Being on the cleaned queue (and having m->clean_queue set)
+ * does ** NOT ** guarantee that the page is clean!
+ *
+ * Call with the queues lock held.
+ */
+
+void
+vm_page_enqueue_cleaned(vm_page_t m)
+{
+ vm_object_t m_object;
+
+ m_object = VM_PAGE_OBJECT(m);
+
+ assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr);
+ LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
+ assert( !(m->vmp_absent && !m->vmp_unusual));
+
+ if (VM_PAGE_WIRED(m)) {
+ return;
+ }
+
+ if (m->vmp_gobbled) {
+ if (!m->vmp_private && !m->vmp_fictitious) {
+ vm_page_wire_count--;
+ }
+ vm_page_gobble_count--;
+ m->vmp_gobbled = FALSE;
+ }
+ /*
+ * if this page is currently on the pageout queue, we can't do the
+ * vm_page_queues_remove (which doesn't handle the pageout queue case)
+ * and we can't remove it manually since we would need the object lock
+ * (which is not required here) to decrement the activity_in_progress
+ * reference which is held on the object while the page is in the pageout queue...
+ * just let the normal laundry processing proceed
+ */
+ if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious ||
+ (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) ||
+ (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) {
+ return;
+ }
+ vm_page_queues_remove(m, FALSE);
+
+ vm_page_check_pageable_safe(m);
+ vm_page_queue_enter(&vm_page_queue_cleaned, m, vmp_pageq);
+ m->vmp_q_state = VM_PAGE_ON_INACTIVE_CLEANED_Q;
+ vm_page_cleaned_count++;
+
+ vm_page_inactive_count++;
+ if (m_object->internal) {
+ vm_page_pageable_internal_count++;
+ } else {
+ vm_page_pageable_external_count++;
+ }
+#if CONFIG_BACKGROUND_QUEUE
+ if (m->vmp_in_background) {
+ vm_page_add_to_backgroundq(m, TRUE);
+ }
+#endif
+ VM_PAGEOUT_DEBUG(vm_pageout_enqueued_cleaned, 1);
+}
+
+/*
+ * vm_page_activate:
+ *
+ * Put the specified page on the active list (if appropriate).
+ *
+ * The page queues must be locked.
+ */
+
+void
+vm_page_activate(
+ vm_page_t m)
+{
+ vm_object_t m_object;
+
+ m_object = VM_PAGE_OBJECT(m);
+
+ VM_PAGE_CHECK(m);
+#ifdef FIXME_4778297
+ assert(m_object != kernel_object);
+#endif
+ assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr);
+ LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
+ assert( !(m->vmp_absent && !m->vmp_unusual));
+
+ if (m->vmp_gobbled) {
+ assert( !VM_PAGE_WIRED(m));
+ if (!m->vmp_private && !m->vmp_fictitious) {
+ vm_page_wire_count--;
+ }
+ vm_page_gobble_count--;
+ m->vmp_gobbled = FALSE;
+ }
+ /*
+ * if this page is currently on the pageout queue, we can't do the
+ * vm_page_queues_remove (which doesn't handle the pageout queue case)
+ * and we can't remove it manually since we would need the object lock
+ * (which is not required here) to decrement the activity_in_progress
+ * reference which is held on the object while the page is in the pageout queue...
+ * just let the normal laundry processing proceed
+ */
+ if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious ||
+ (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) ||
+ (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) {
+ return;
+ }
+
+#if DEBUG
+ if (m->vmp_q_state == VM_PAGE_ON_ACTIVE_Q) {
+ panic("vm_page_activate: already active");
+ }
+#endif
+
+ if (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) {
+ DTRACE_VM2(pgrec, int, 1, (uint64_t *), NULL);
+ DTRACE_VM2(pgfrec, int, 1, (uint64_t *), NULL);
+ }
+
+ vm_page_queues_remove(m, FALSE);
+
+ if (!VM_PAGE_WIRED(m)) {
+ vm_page_check_pageable_safe(m);
+ if (!VM_DYNAMIC_PAGING_ENABLED() &&
+ m->vmp_dirty && m_object->internal &&
+ (m_object->purgable == VM_PURGABLE_DENY ||
+ m_object->purgable == VM_PURGABLE_NONVOLATILE ||
+ m_object->purgable == VM_PURGABLE_VOLATILE)) {
+ vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq);
+ m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q;
+ vm_page_throttled_count++;
+ } else {
+#if CONFIG_SECLUDED_MEMORY
+ if (secluded_for_filecache &&
+ vm_page_secluded_target != 0 &&
+ num_tasks_can_use_secluded_mem == 0 &&
+ m_object->eligible_for_secluded) {
+ vm_page_queue_enter(&vm_page_queue_secluded, m, vmp_pageq);
+ m->vmp_q_state = VM_PAGE_ON_SECLUDED_Q;
+ vm_page_secluded_count++;
+ vm_page_secluded_count_inuse++;
+ assert(!m_object->internal);
+// vm_page_pageable_external_count++;
+ } else
+#endif /* CONFIG_SECLUDED_MEMORY */
+ vm_page_enqueue_active(m, FALSE);
+ }
+ m->vmp_reference = TRUE;
+ m->vmp_no_cache = FALSE;
+ }
+ VM_PAGE_CHECK(m);
+}
+
+
+/*
+ * vm_page_speculate:
+ *
+ * Put the specified page on the speculative list (if appropriate).
+ *
+ * The page queues must be locked.
+ */
+void
+vm_page_speculate(
+ vm_page_t m,
+ boolean_t new)
+{
+ struct vm_speculative_age_q *aq;
+ vm_object_t m_object;
+
+ m_object = VM_PAGE_OBJECT(m);
+
+ VM_PAGE_CHECK(m);
+ vm_page_check_pageable_safe(m);
+
+ assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr);
+ LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
+ assert( !(m->vmp_absent && !m->vmp_unusual));
+ assert(m_object->internal == FALSE);
+
+ /*
+ * if this page is currently on the pageout queue, we can't do the
+ * vm_page_queues_remove (which doesn't handle the pageout queue case)
+ * and we can't remove it manually since we would need the object lock
+ * (which is not required here) to decrement the activity_in_progress
+ * reference which is held on the object while the page is in the pageout queue...
+ * just let the normal laundry processing proceed
+ */
+ if (m->vmp_laundry || m->vmp_private || m->vmp_fictitious ||
+ (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) ||
+ (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) {
+ return;
+ }
+
+ vm_page_queues_remove(m, FALSE);
+
+ if (!VM_PAGE_WIRED(m)) {
+ mach_timespec_t ts;
+ clock_sec_t sec;
+ clock_nsec_t nsec;
+
+ clock_get_system_nanotime(&sec, &nsec);
+ ts.tv_sec = (unsigned int) sec;
+ ts.tv_nsec = nsec;
+
+ if (vm_page_speculative_count == 0) {
+ speculative_age_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q;
+ speculative_steal_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q;
+
+ aq = &vm_page_queue_speculative[speculative_age_index];
+
+ /*
+ * set the timer to begin a new group
+ */
+ aq->age_ts.tv_sec = vm_pageout_state.vm_page_speculative_q_age_ms / 1000;
+ aq->age_ts.tv_nsec = (vm_pageout_state.vm_page_speculative_q_age_ms % 1000) * 1000 * NSEC_PER_USEC;
+ ADD_MACH_TIMESPEC(&aq->age_ts, &ts);
+ } else {
+ aq = &vm_page_queue_speculative[speculative_age_index];
+
+ if (CMP_MACH_TIMESPEC(&ts, &aq->age_ts) >= 0) {
+ speculative_age_index++;
+
+ if (speculative_age_index > VM_PAGE_MAX_SPECULATIVE_AGE_Q) {
+ speculative_age_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q;
+ }
+ if (speculative_age_index == speculative_steal_index) {
+ speculative_steal_index = speculative_age_index + 1;
+
+ if (speculative_steal_index > VM_PAGE_MAX_SPECULATIVE_AGE_Q) {
+ speculative_steal_index = VM_PAGE_MIN_SPECULATIVE_AGE_Q;
+ }
+ }
+ aq = &vm_page_queue_speculative[speculative_age_index];
+
+ if (!vm_page_queue_empty(&aq->age_q)) {
+ vm_page_speculate_ageit(aq);
+ }
+
+ aq->age_ts.tv_sec = vm_pageout_state.vm_page_speculative_q_age_ms / 1000;
+ aq->age_ts.tv_nsec = (vm_pageout_state.vm_page_speculative_q_age_ms % 1000) * 1000 * NSEC_PER_USEC;
+ ADD_MACH_TIMESPEC(&aq->age_ts, &ts);
+ }
+ }
+ vm_page_enqueue_tail(&aq->age_q, &m->vmp_pageq);
+ m->vmp_q_state = VM_PAGE_ON_SPECULATIVE_Q;
+ vm_page_speculative_count++;
+ vm_page_pageable_external_count++;
+
+ if (new == TRUE) {
+ vm_object_lock_assert_exclusive(m_object);
+
+ m_object->pages_created++;
+#if DEVELOPMENT || DEBUG
+ vm_page_speculative_created++;
+#endif
+ }
+ }
+ VM_PAGE_CHECK(m);
+}
+
+
+/*
+ * move pages from the specified aging bin to
+ * the speculative bin that pageout_scan claims from
+ *
+ * The page queues must be locked.
+ */
+void
+vm_page_speculate_ageit(struct vm_speculative_age_q *aq)
+{
+ struct vm_speculative_age_q *sq;
+ vm_page_t t;
+
+ sq = &vm_page_queue_speculative[VM_PAGE_SPECULATIVE_AGED_Q];
+
+ if (vm_page_queue_empty(&sq->age_q)) {
+ sq->age_q.next = aq->age_q.next;
+ sq->age_q.prev = aq->age_q.prev;
+
+ t = (vm_page_t)VM_PAGE_UNPACK_PTR(sq->age_q.next);
+ t->vmp_pageq.prev = VM_PAGE_PACK_PTR(&sq->age_q);
+
+ t = (vm_page_t)VM_PAGE_UNPACK_PTR(sq->age_q.prev);
+ t->vmp_pageq.next = VM_PAGE_PACK_PTR(&sq->age_q);
+ } else {
+ t = (vm_page_t)VM_PAGE_UNPACK_PTR(sq->age_q.prev);
+ t->vmp_pageq.next = aq->age_q.next;
+
+ t = (vm_page_t)VM_PAGE_UNPACK_PTR(aq->age_q.next);
+ t->vmp_pageq.prev = sq->age_q.prev;
+
+ t = (vm_page_t)VM_PAGE_UNPACK_PTR(aq->age_q.prev);
+ t->vmp_pageq.next = VM_PAGE_PACK_PTR(&sq->age_q);
+
+ sq->age_q.prev = aq->age_q.prev;
+ }
+ vm_page_queue_init(&aq->age_q);
+}
+
+
+void
+vm_page_lru(
+ vm_page_t m)
+{
+ VM_PAGE_CHECK(m);
+ assert(VM_PAGE_OBJECT(m) != kernel_object);
+ assert(VM_PAGE_GET_PHYS_PAGE(m) != vm_page_guard_addr);
+
+ LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
+
+ if (m->vmp_q_state == VM_PAGE_ON_INACTIVE_EXTERNAL_Q) {
+ /*
+ * we don't need to do all the other work that
+ * vm_page_queues_remove and vm_page_enqueue_inactive
+ * bring along for the ride
+ */
+ assert(!m->vmp_laundry);
+ assert(!m->vmp_private);
+
+ m->vmp_no_cache = FALSE;
+
+ vm_page_queue_remove(&vm_page_queue_inactive, m, vmp_pageq);
+ vm_page_queue_enter(&vm_page_queue_inactive, m, vmp_pageq);
+
+ return;
+ }
+ /*
+ * if this page is currently on the pageout queue, we can't do the
+ * vm_page_queues_remove (which doesn't handle the pageout queue case)
+ * and we can't remove it manually since we would need the object lock
+ * (which is not required here) to decrement the activity_in_progress
+ * reference which is held on the object while the page is in the pageout queue...
+ * just let the normal laundry processing proceed
+ */
+ if (m->vmp_laundry || m->vmp_private ||
+ (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) ||
+ (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) ||
+ VM_PAGE_WIRED(m)) {
+ return;
+ }
+
+ m->vmp_no_cache = FALSE;
+
+ vm_page_queues_remove(m, FALSE);
+
+ vm_page_enqueue_inactive(m, FALSE);
+}
+
+
+void
+vm_page_reactivate_all_throttled(void)
+{
+ vm_page_t first_throttled, last_throttled;
+ vm_page_t first_active;
+ vm_page_t m;
+ int extra_active_count;
+ int extra_internal_count, extra_external_count;
+ vm_object_t m_object;
+
+ if (!VM_DYNAMIC_PAGING_ENABLED()) {
+ return;
+ }
+
+ extra_active_count = 0;
+ extra_internal_count = 0;
+ extra_external_count = 0;
+ vm_page_lock_queues();
+ if (!vm_page_queue_empty(&vm_page_queue_throttled)) {
+ /*
+ * Switch "throttled" pages to "active".
+ */
+ vm_page_queue_iterate(&vm_page_queue_throttled, m, vmp_pageq) {
+ VM_PAGE_CHECK(m);
+ assert(m->vmp_q_state == VM_PAGE_ON_THROTTLED_Q);
+
+ m_object = VM_PAGE_OBJECT(m);
+
+ extra_active_count++;
+ if (m_object->internal) {
+ extra_internal_count++;
+ } else {
+ extra_external_count++;
+ }
+
+ m->vmp_q_state = VM_PAGE_ON_ACTIVE_Q;
+ VM_PAGE_CHECK(m);
+#if CONFIG_BACKGROUND_QUEUE
+ if (m->vmp_in_background) {
+ vm_page_add_to_backgroundq(m, FALSE);
+ }
+#endif
+ }
+
+ /*
+ * Transfer the entire throttled queue to a regular LRU page queues.
+ * We insert it at the head of the active queue, so that these pages
+ * get re-evaluated by the LRU algorithm first, since they've been
+ * completely out of it until now.
+ */
+ first_throttled = (vm_page_t) vm_page_queue_first(&vm_page_queue_throttled);
+ last_throttled = (vm_page_t) vm_page_queue_last(&vm_page_queue_throttled);
+ first_active = (vm_page_t) vm_page_queue_first(&vm_page_queue_active);
+ if (vm_page_queue_empty(&vm_page_queue_active)) {
+ vm_page_queue_active.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_throttled);
+ } else {
+ first_active->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_throttled);
+ }
+ vm_page_queue_active.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_throttled);
+ first_throttled->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(&vm_page_queue_active);
+ last_throttled->vmp_pageq.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_active);
+
+#if DEBUG
+ printf("reactivated %d throttled pages\n", vm_page_throttled_count);
+#endif
+ vm_page_queue_init(&vm_page_queue_throttled);
+ /*
+ * Adjust the global page counts.
+ */
+ vm_page_active_count += extra_active_count;
+ vm_page_pageable_internal_count += extra_internal_count;
+ vm_page_pageable_external_count += extra_external_count;
+ vm_page_throttled_count = 0;
+ }
+ assert(vm_page_throttled_count == 0);
+ assert(vm_page_queue_empty(&vm_page_queue_throttled));
+ vm_page_unlock_queues();
+}
+
+
+/*
+ * move pages from the indicated local queue to the global active queue
+ * its ok to fail if we're below the hard limit and force == FALSE
+ * the nolocks == TRUE case is to allow this function to be run on
+ * the hibernate path
+ */
+
+void
+vm_page_reactivate_local(uint32_t lid, boolean_t force, boolean_t nolocks)
+{
+ struct vpl *lq;
+ vm_page_t first_local, last_local;
+ vm_page_t first_active;
+ vm_page_t m;
+ uint32_t count = 0;
+
+ if (vm_page_local_q == NULL) {
+ return;
+ }
+
+ lq = &vm_page_local_q[lid].vpl_un.vpl;
+
+ if (nolocks == FALSE) {
+ if (lq->vpl_count < vm_page_local_q_hard_limit && force == FALSE) {
+ if (!vm_page_trylockspin_queues()) {
+ return;
+ }
+ } else {
+ vm_page_lockspin_queues();
+ }
+
+ VPL_LOCK(&lq->vpl_lock);
+ }
+ if (lq->vpl_count) {
+ /*
+ * Switch "local" pages to "active".
+ */
+ assert(!vm_page_queue_empty(&lq->vpl_queue));
+
+ vm_page_queue_iterate(&lq->vpl_queue, m, vmp_pageq) {
+ VM_PAGE_CHECK(m);
+ vm_page_check_pageable_safe(m);
+ assert(m->vmp_q_state == VM_PAGE_ON_ACTIVE_LOCAL_Q);
+ assert(!m->vmp_fictitious);
+
+ if (m->vmp_local_id != lid) {
+ panic("vm_page_reactivate_local: found vm_page_t(%p) with wrong cpuid", m);
+ }
+
+ m->vmp_local_id = 0;
+ m->vmp_q_state = VM_PAGE_ON_ACTIVE_Q;
+ VM_PAGE_CHECK(m);
+#if CONFIG_BACKGROUND_QUEUE
+ if (m->vmp_in_background) {
+ vm_page_add_to_backgroundq(m, FALSE);
+ }
+#endif
+ count++;
+ }
+ if (count != lq->vpl_count) {
+ panic("vm_page_reactivate_local: count = %d, vm_page_local_count = %d\n", count, lq->vpl_count);
+ }
+
+ /*
+ * Transfer the entire local queue to a regular LRU page queues.
+ */
+ first_local = (vm_page_t) vm_page_queue_first(&lq->vpl_queue);
+ last_local = (vm_page_t) vm_page_queue_last(&lq->vpl_queue);
+ first_active = (vm_page_t) vm_page_queue_first(&vm_page_queue_active);
+
+ if (vm_page_queue_empty(&vm_page_queue_active)) {
+ vm_page_queue_active.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_local);
+ } else {
+ first_active->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(last_local);
+ }
+ vm_page_queue_active.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_local);
+ first_local->vmp_pageq.prev = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(&vm_page_queue_active);
+ last_local->vmp_pageq.next = VM_PAGE_CONVERT_TO_QUEUE_ENTRY(first_active);
+
+ vm_page_queue_init(&lq->vpl_queue);
+ /*
+ * Adjust the global page counts.
+ */
+ vm_page_active_count += lq->vpl_count;
+ vm_page_pageable_internal_count += lq->vpl_internal_count;
+ vm_page_pageable_external_count += lq->vpl_external_count;
+ lq->vpl_count = 0;
+ lq->vpl_internal_count = 0;
+ lq->vpl_external_count = 0;
+ }
+ assert(vm_page_queue_empty(&lq->vpl_queue));
+
+ if (nolocks == FALSE) {
+ VPL_UNLOCK(&lq->vpl_lock);
+
+ vm_page_balance_inactive(count / 4);
+ vm_page_unlock_queues();
+ }
+}
+
+/*
+ * vm_page_part_zero_fill:
+ *
+ * Zero-fill a part of the page.
+ */
+#define PMAP_ZERO_PART_PAGE_IMPLEMENTED
+void
+vm_page_part_zero_fill(
+ vm_page_t m,
+ vm_offset_t m_pa,
+ vm_size_t len)
+{
+#if 0
+ /*
+ * we don't hold the page queue lock
+ * so this check isn't safe to make
+ */
+ VM_PAGE_CHECK(m);
+#endif
+
+#ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
+ pmap_zero_part_page(VM_PAGE_GET_PHYS_PAGE(m), m_pa, len);
+#else
+ vm_page_t tmp;
+ while (1) {
+ tmp = vm_page_grab();
+ if (tmp == VM_PAGE_NULL) {
+ vm_page_wait(THREAD_UNINT);
+ continue;
+ }
+ break;
+ }
+ vm_page_zero_fill(tmp);
+ if (m_pa != 0) {
+ vm_page_part_copy(m, 0, tmp, 0, m_pa);
+ }
+ if ((m_pa + len) < PAGE_SIZE) {
+ vm_page_part_copy(m, m_pa + len, tmp,
+ m_pa + len, PAGE_SIZE - (m_pa + len));
+ }
+ vm_page_copy(tmp, m);
+ VM_PAGE_FREE(tmp);
+#endif
+}
+
+/*
+ * vm_page_zero_fill:
+ *
+ * Zero-fill the specified page.
+ */
+void
+vm_page_zero_fill(
+ vm_page_t m)
+{
+ XPR(XPR_VM_PAGE,
+ "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
+ VM_PAGE_OBJECT(m), m->vmp_offset, m, 0, 0);
+#if 0
+ /*
+ * we don't hold the page queue lock
+ * so this check isn't safe to make
+ */
+ VM_PAGE_CHECK(m);
+#endif
+
+// dbgTrace(0xAEAEAEAE, VM_PAGE_GET_PHYS_PAGE(m), 0); /* (BRINGUP) */
+ pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(m));
+}
+
+/*
+ * vm_page_part_copy:
+ *
+ * copy part of one page to another
+ */
+
+void
+vm_page_part_copy(
+ vm_page_t src_m,
+ vm_offset_t src_pa,
+ vm_page_t dst_m,
+ vm_offset_t dst_pa,
+ vm_size_t len)
+{
+#if 0
+ /*
+ * we don't hold the page queue lock
+ * so this check isn't safe to make
+ */
+ VM_PAGE_CHECK(src_m);
+ VM_PAGE_CHECK(dst_m);
+#endif
+ pmap_copy_part_page(VM_PAGE_GET_PHYS_PAGE(src_m), src_pa,
+ VM_PAGE_GET_PHYS_PAGE(dst_m), dst_pa, len);
+}
+
+/*
+ * vm_page_copy:
+ *
+ * Copy one page to another
+ */
+
+int vm_page_copy_cs_validations = 0;
+int vm_page_copy_cs_tainted = 0;
+
+void
+vm_page_copy(
+ vm_page_t src_m,
+ vm_page_t dest_m)
+{
+ vm_object_t src_m_object;
+
+ src_m_object = VM_PAGE_OBJECT(src_m);
+
+ XPR(XPR_VM_PAGE,
+ "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
+ src_m_object, src_m->vmp_offset,
+ VM_PAGE_OBJECT(dest_m), dest_m->vmp_offset,
+ 0);
+#if 0
+ /*
+ * we don't hold the page queue lock
+ * so this check isn't safe to make
+ */
+ VM_PAGE_CHECK(src_m);
+ VM_PAGE_CHECK(dest_m);
+#endif
+ vm_object_lock_assert_held(src_m_object);
+
+ if (src_m_object != VM_OBJECT_NULL &&
+ src_m_object->code_signed) {
+ /*
+ * We're copying a page from a code-signed object.
+ * Whoever ends up mapping the copy page might care about
+ * the original page's integrity, so let's validate the
+ * source page now.
+ */
+ vm_page_copy_cs_validations++;
+ vm_page_validate_cs(src_m);
+#if DEVELOPMENT || DEBUG
+ DTRACE_VM4(codesigned_copy,
+ vm_object_t, src_m_object,
+ vm_object_offset_t, src_m->vmp_offset,
+ int, src_m->vmp_cs_validated,
+ int, src_m->vmp_cs_tainted);
+#endif /* DEVELOPMENT || DEBUG */
+ }
+
+ /*
+ * Propagate the cs_tainted bit to the copy page. Do not propagate
+ * the cs_validated bit.
+ */
+ dest_m->vmp_cs_tainted = src_m->vmp_cs_tainted;
+ if (dest_m->vmp_cs_tainted) {
+ vm_page_copy_cs_tainted++;
+ }
+ dest_m->vmp_error = src_m->vmp_error; /* sliding src_m might have failed... */
+ pmap_copy_page(VM_PAGE_GET_PHYS_PAGE(src_m), VM_PAGE_GET_PHYS_PAGE(dest_m));
+}
+
+#if MACH_ASSERT
+static void
+_vm_page_print(
+ vm_page_t p)
+{
+ printf("vm_page %p: \n", p);
+ printf(" pageq: next=%p prev=%p\n",
+ (vm_page_t)VM_PAGE_UNPACK_PTR(p->vmp_pageq.next),
+ (vm_page_t)VM_PAGE_UNPACK_PTR(p->vmp_pageq.prev));
+ printf(" listq: next=%p prev=%p\n",
+ (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_listq.next)),
+ (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_listq.prev)));
+ printf(" next=%p\n", (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_next_m)));
+ printf(" object=%p offset=0x%llx\n", VM_PAGE_OBJECT(p), p->vmp_offset);
+ printf(" wire_count=%u\n", p->vmp_wire_count);
+ printf(" q_state=%u\n", p->vmp_q_state);
+
+ printf(" %slaundry, %sref, %sgobbled, %sprivate\n",
+ (p->vmp_laundry ? "" : "!"),
+ (p->vmp_reference ? "" : "!"),
+ (p->vmp_gobbled ? "" : "!"),
+ (p->vmp_private ? "" : "!"));
+ printf(" %sbusy, %swanted, %stabled, %sfictitious, %spmapped, %swpmapped\n",
+ (p->vmp_busy ? "" : "!"),
+ (p->vmp_wanted ? "" : "!"),
+ (p->vmp_tabled ? "" : "!"),
+ (p->vmp_fictitious ? "" : "!"),
+ (p->vmp_pmapped ? "" : "!"),
+ (p->vmp_wpmapped ? "" : "!"));
+ printf(" %sfree_when_done, %sabsent, %serror, %sdirty, %scleaning, %sprecious, %sclustered\n",
+ (p->vmp_free_when_done ? "" : "!"),
+ (p->vmp_absent ? "" : "!"),
+ (p->vmp_error ? "" : "!"),
+ (p->vmp_dirty ? "" : "!"),
+ (p->vmp_cleaning ? "" : "!"),
+ (p->vmp_precious ? "" : "!"),
+ (p->vmp_clustered ? "" : "!"));
+ printf(" %soverwriting, %srestart, %sunusual\n",
+ (p->vmp_overwriting ? "" : "!"),
+ (p->vmp_restart ? "" : "!"),
+ (p->vmp_unusual ? "" : "!"));
+ printf(" %scs_validated, %scs_tainted, %scs_nx, %sno_cache\n",
+ (p->vmp_cs_validated ? "" : "!"),
+ (p->vmp_cs_tainted ? "" : "!"),
+ (p->vmp_cs_nx ? "" : "!"),
+ (p->vmp_no_cache ? "" : "!"));
+
+ printf("phys_page=0x%x\n", VM_PAGE_GET_PHYS_PAGE(p));
+}
+
+/*
+ * Check that the list of pages is ordered by
+ * ascending physical address and has no holes.
+ */
+static int
+vm_page_verify_contiguous(
+ vm_page_t pages,
+ unsigned int npages)
+{
+ vm_page_t m;
+ unsigned int page_count;
+ vm_offset_t prev_addr;
+
+ prev_addr = VM_PAGE_GET_PHYS_PAGE(pages);
+ page_count = 1;
+ for (m = NEXT_PAGE(pages); m != VM_PAGE_NULL; m = NEXT_PAGE(m)) {
+ if (VM_PAGE_GET_PHYS_PAGE(m) != prev_addr + 1) {
+ printf("m %p prev_addr 0x%lx, current addr 0x%x\n",
+ m, (long)prev_addr, VM_PAGE_GET_PHYS_PAGE(m));
+ printf("pages %p page_count %d npages %d\n", pages, page_count, npages);
+ panic("vm_page_verify_contiguous: not contiguous!");
+ }
+ prev_addr = VM_PAGE_GET_PHYS_PAGE(m);
+ ++page_count;
+ }
+ if (page_count != npages) {
+ printf("pages %p actual count 0x%x but requested 0x%x\n",
+ pages, page_count, npages);
+ panic("vm_page_verify_contiguous: count error");
+ }
+ return 1;
+}
+
+
+/*
+ * Check the free lists for proper length etc.
+ */
+static boolean_t vm_page_verify_this_free_list_enabled = FALSE;
+static unsigned int
+vm_page_verify_free_list(
+ vm_page_queue_head_t *vm_page_queue,
+ unsigned int color,
+ vm_page_t look_for_page,
+ boolean_t expect_page)
+{
+ unsigned int npages;
+ vm_page_t m;
+ vm_page_t prev_m;
+ boolean_t found_page;
+
+ if (!vm_page_verify_this_free_list_enabled) {
+ return 0;
+ }
+
+ found_page = FALSE;
+ npages = 0;
+ prev_m = (vm_page_t)((uintptr_t)vm_page_queue);
+
+ vm_page_queue_iterate(vm_page_queue, m, vmp_pageq) {
+ if (m == look_for_page) {
+ found_page = TRUE;
+ }
+ if ((vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.prev) != prev_m) {
+ panic("vm_page_verify_free_list(color=%u, npages=%u): page %p corrupted prev ptr %p instead of %p\n",
+ color, npages, m, (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.prev), prev_m);
+ }
+ if (!m->vmp_busy) {
+ panic("vm_page_verify_free_list(color=%u, npages=%u): page %p not busy\n",
+ color, npages, m);
+ }
+ if (color != (unsigned int) -1) {
+ if (VM_PAGE_GET_COLOR(m) != color) {
+ panic("vm_page_verify_free_list(color=%u, npages=%u): page %p wrong color %u instead of %u\n",
+ color, npages, m, VM_PAGE_GET_COLOR(m), color);
+ }
+ if (m->vmp_q_state != VM_PAGE_ON_FREE_Q) {
+ panic("vm_page_verify_free_list(color=%u, npages=%u): page %p - expecting q_state == VM_PAGE_ON_FREE_Q, found %d\n",
+ color, npages, m, m->vmp_q_state);
+ }
+ } else {
+ if (m->vmp_q_state != VM_PAGE_ON_FREE_LOCAL_Q) {
+ panic("vm_page_verify_free_list(npages=%u): local page %p - expecting q_state == VM_PAGE_ON_FREE_LOCAL_Q, found %d\n",
+ npages, m, m->vmp_q_state);
+ }
+ }
+ ++npages;
+ prev_m = m;
+ }
+ if (look_for_page != VM_PAGE_NULL) {
+ unsigned int other_color;
+
+ if (expect_page && !found_page) {
+ printf("vm_page_verify_free_list(color=%u, npages=%u): page %p not found phys=%u\n",
+ color, npages, look_for_page, VM_PAGE_GET_PHYS_PAGE(look_for_page));
+ _vm_page_print(look_for_page);
+ for (other_color = 0;
+ other_color < vm_colors;
+ other_color++) {
+ if (other_color == color) {
+ continue;
+ }
+ vm_page_verify_free_list(&vm_page_queue_free[other_color].qhead,
+ other_color, look_for_page, FALSE);
+ }
+ if (color == (unsigned int) -1) {
+ vm_page_verify_free_list(&vm_lopage_queue_free,
+ (unsigned int) -1, look_for_page, FALSE);
+ }
+ panic("vm_page_verify_free_list(color=%u)\n", color);
+ }
+ if (!expect_page && found_page) {
+ printf("vm_page_verify_free_list(color=%u, npages=%u): page %p found phys=%u\n",
+ color, npages, look_for_page, VM_PAGE_GET_PHYS_PAGE(look_for_page));
+ }
+ }
+ return npages;
+}
+
+static boolean_t vm_page_verify_all_free_lists_enabled = FALSE;
+static void
+vm_page_verify_free_lists( void )
+{
+ unsigned int color, npages, nlopages;
+ boolean_t toggle = TRUE;
+
+ if (!vm_page_verify_all_free_lists_enabled) {
+ return;
+ }
+
+ npages = 0;
+
+ lck_mtx_lock(&vm_page_queue_free_lock);
+
+ if (vm_page_verify_this_free_list_enabled == TRUE) {
+ /*
+ * This variable has been set globally for extra checking of
+ * each free list Q. Since we didn't set it, we don't own it
+ * and we shouldn't toggle it.
+ */
+ toggle = FALSE;
+ }
+
+ if (toggle == TRUE) {
+ vm_page_verify_this_free_list_enabled = TRUE;
+ }
+
+ for (color = 0; color < vm_colors; color++) {
+ npages += vm_page_verify_free_list(&vm_page_queue_free[color].qhead,
+ color, VM_PAGE_NULL, FALSE);
+ }
+ nlopages = vm_page_verify_free_list(&vm_lopage_queue_free,
+ (unsigned int) -1,
+ VM_PAGE_NULL, FALSE);
+ if (npages != vm_page_free_count || nlopages != vm_lopage_free_count) {
+ panic("vm_page_verify_free_lists: "
+ "npages %u free_count %d nlopages %u lo_free_count %u",
+ npages, vm_page_free_count, nlopages, vm_lopage_free_count);
+ }
+
+ if (toggle == TRUE) {
+ vm_page_verify_this_free_list_enabled = FALSE;
+ }
+
+ lck_mtx_unlock(&vm_page_queue_free_lock);
+}
+
+#endif /* MACH_ASSERT */
+
+
+extern boolean_t(*volatile consider_buffer_cache_collect)(int);
+
+/*
+ * CONTIGUOUS PAGE ALLOCATION
+ *
+ * Find a region large enough to contain at least n pages
+ * of contiguous physical memory.
+ *
+ * This is done by traversing the vm_page_t array in a linear fashion
+ * we assume that the vm_page_t array has the avaiable physical pages in an
+ * ordered, ascending list... this is currently true of all our implementations
+ * and must remain so... there can be 'holes' in the array... we also can
+ * no longer tolerate the vm_page_t's in the list being 'freed' and reclaimed
+ * which use to happen via 'vm_page_convert'... that function was no longer
+ * being called and was removed...
+ *
+ * The basic flow consists of stabilizing some of the interesting state of
+ * a vm_page_t behind the vm_page_queue and vm_page_free locks... we start our
+ * sweep at the beginning of the array looking for pages that meet our criterea
+ * for a 'stealable' page... currently we are pretty conservative... if the page
+ * meets this criterea and is physically contiguous to the previous page in the 'run'
+ * we keep developing it. If we hit a page that doesn't fit, we reset our state
+ * and start to develop a new run... if at this point we've already considered
+ * at least MAX_CONSIDERED_BEFORE_YIELD pages, we'll drop the 2 locks we hold,
+ * and mutex_pause (which will yield the processor), to keep the latency low w/r
+ * to other threads trying to acquire free pages (or move pages from q to q),
+ * and then continue from the spot we left off... we only make 1 pass through the
+ * array. Once we have a 'run' that is long enough, we'll go into the loop which
+ * which steals the pages from the queues they're currently on... pages on the free
+ * queue can be stolen directly... pages that are on any of the other queues
+ * must be removed from the object they are tabled on... this requires taking the
+ * object lock... we do this as a 'try' to prevent deadlocks... if the 'try' fails
+ * or if the state of the page behind the vm_object lock is no longer viable, we'll
+ * dump the pages we've currently stolen back to the free list, and pick up our
+ * scan from the point where we aborted the 'current' run.
+ *
+ *
+ * Requirements:
+ * - neither vm_page_queue nor vm_free_list lock can be held on entry
+ *
+ * Returns a pointer to a list of gobbled/wired pages or VM_PAGE_NULL.
+ *
+ * Algorithm:
+ */
+
+#define MAX_CONSIDERED_BEFORE_YIELD 1000
+
+
+#define RESET_STATE_OF_RUN() \
+ MACRO_BEGIN \
+ prevcontaddr = -2; \
+ start_pnum = -1; \
+ free_considered = 0; \
+ substitute_needed = 0; \
+ npages = 0; \
+ MACRO_END
+
+/*
+ * Can we steal in-use (i.e. not free) pages when searching for
+ * physically-contiguous pages ?
+ */
+#define VM_PAGE_FIND_CONTIGUOUS_CAN_STEAL 1
+
+static unsigned int vm_page_find_contiguous_last_idx = 0, vm_page_lomem_find_contiguous_last_idx = 0;
+#if DEBUG
+int vm_page_find_contig_debug = 0;
+#endif
+
+static vm_page_t
+vm_page_find_contiguous(
+ unsigned int contig_pages,
+ ppnum_t max_pnum,
+ ppnum_t pnum_mask,
+ boolean_t wire,
+ int flags)
+{
+ vm_page_t m = NULL;
+ ppnum_t prevcontaddr = 0;
+ ppnum_t start_pnum = 0;
+ unsigned int npages = 0, considered = 0, scanned = 0;
+ unsigned int page_idx = 0, start_idx = 0, last_idx = 0, orig_last_idx = 0;
+ unsigned int idx_last_contig_page_found = 0;
+ int free_considered = 0, free_available = 0;
+ int substitute_needed = 0;
+ boolean_t wrapped, zone_gc_called = FALSE;
+ kern_return_t kr;
+#if DEBUG
+ clock_sec_t tv_start_sec = 0, tv_end_sec = 0;
+ clock_usec_t tv_start_usec = 0, tv_end_usec = 0;
+#endif
+
+ int yielded = 0;
+ int dumped_run = 0;
+ int stolen_pages = 0;
+ int compressed_pages = 0;
+
+
+ if (contig_pages == 0) {
+ return VM_PAGE_NULL;
+ }
+
+full_scan_again:
+
+#if MACH_ASSERT
+ vm_page_verify_free_lists();
+#endif
+#if DEBUG
+ clock_get_system_microtime(&tv_start_sec, &tv_start_usec);
+#endif
+ PAGE_REPLACEMENT_ALLOWED(TRUE);
+
+ /*
+ * If there are still delayed pages, try to free up some that match.
+ */
+ if (__improbable(vm_delayed_count != 0 && contig_pages != 0)) {
+ vm_free_delayed_pages_contig(contig_pages, max_pnum, pnum_mask);
+ }
+
+ vm_page_lock_queues();
+ lck_mtx_lock(&vm_page_queue_free_lock);
+
+ RESET_STATE_OF_RUN();
+
+ scanned = 0;
+ considered = 0;
+ free_available = vm_page_free_count - vm_page_free_reserved;
+
+ wrapped = FALSE;
+
+ if (flags & KMA_LOMEM) {
+ idx_last_contig_page_found = vm_page_lomem_find_contiguous_last_idx;
+ } else {
+ idx_last_contig_page_found = vm_page_find_contiguous_last_idx;
+ }
+
+ orig_last_idx = idx_last_contig_page_found;
+ last_idx = orig_last_idx;
+
+ for (page_idx = last_idx, start_idx = last_idx;
+ npages < contig_pages && page_idx < vm_pages_count;
+ page_idx++) {
+retry:
+ if (wrapped &&
+ npages == 0 &&
+ page_idx >= orig_last_idx) {
+ /*
+ * We're back where we started and we haven't
+ * found any suitable contiguous range. Let's
+ * give up.
+ */
+ break;
+ }
+ scanned++;
+ m = &vm_pages[page_idx];
+
+ assert(!m->vmp_fictitious);
+ assert(!m->vmp_private);
+
+ if (max_pnum && VM_PAGE_GET_PHYS_PAGE(m) > max_pnum) {
+ /* no more low pages... */
+ break;
+ }
+ if (!npages & ((VM_PAGE_GET_PHYS_PAGE(m) & pnum_mask) != 0)) {
+ /*
+ * not aligned
+ */
+ RESET_STATE_OF_RUN();
+ } else if (VM_PAGE_WIRED(m) || m->vmp_gobbled ||
+ m->vmp_laundry || m->vmp_wanted ||
+ m->vmp_cleaning || m->vmp_overwriting || m->vmp_free_when_done) {
+ /*
+ * page is in a transient state
+ * or a state we don't want to deal
+ * with, so don't consider it which
+ * means starting a new run
+ */
+ RESET_STATE_OF_RUN();
+ } else if ((m->vmp_q_state == VM_PAGE_NOT_ON_Q) ||
+ (m->vmp_q_state == VM_PAGE_ON_FREE_LOCAL_Q) ||
+ (m->vmp_q_state == VM_PAGE_ON_FREE_LOPAGE_Q) ||
+ (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) {
+ /*
+ * page needs to be on one of our queues (other then the pageout or special free queues)
+ * or it needs to belong to the compressor pool (which is now indicated
+ * by vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR and falls out
+ * from the check for VM_PAGE_NOT_ON_Q)
+ * in order for it to be stable behind the
+ * locks we hold at this point...
+ * if not, don't consider it which
+ * means starting a new run
+ */
+ RESET_STATE_OF_RUN();
+ } else if ((m->vmp_q_state != VM_PAGE_ON_FREE_Q) && (!m->vmp_tabled || m->vmp_busy)) {
+ /*
+ * pages on the free list are always 'busy'
+ * so we couldn't test for 'busy' in the check
+ * for the transient states... pages that are
+ * 'free' are never 'tabled', so we also couldn't
+ * test for 'tabled'. So we check here to make
+ * sure that a non-free page is not busy and is
+ * tabled on an object...
+ * if not, don't consider it which
+ * means starting a new run
+ */
+ RESET_STATE_OF_RUN();
+ } else {
+ if (VM_PAGE_GET_PHYS_PAGE(m) != prevcontaddr + 1) {
+ if ((VM_PAGE_GET_PHYS_PAGE(m) & pnum_mask) != 0) {
+ RESET_STATE_OF_RUN();
+ goto did_consider;
+ } else {
+ npages = 1;
+ start_idx = page_idx;
+ start_pnum = VM_PAGE_GET_PHYS_PAGE(m);
+ }
+ } else {
+ npages++;
+ }
+ prevcontaddr = VM_PAGE_GET_PHYS_PAGE(m);
+
+ VM_PAGE_CHECK(m);
+ if (m->vmp_q_state == VM_PAGE_ON_FREE_Q) {
+ free_considered++;
+ } else {
+ /*
+ * This page is not free.
+ * If we can't steal used pages,
+ * we have to give up this run
+ * and keep looking.
+ * Otherwise, we might need to
+ * move the contents of this page
+ * into a substitute page.
+ */
+#if VM_PAGE_FIND_CONTIGUOUS_CAN_STEAL
+ if (m->vmp_pmapped || m->vmp_dirty || m->vmp_precious) {
+ substitute_needed++;
+ }
+#else
+ RESET_STATE_OF_RUN();
+#endif
+ }
+
+ if ((free_considered + substitute_needed) > free_available) {
+ /*
+ * if we let this run continue
+ * we will end up dropping the vm_page_free_count
+ * below the reserve limit... we need to abort
+ * this run, but we can at least re-consider this
+ * page... thus the jump back to 'retry'
+ */
+ RESET_STATE_OF_RUN();
+
+ if (free_available && considered <= MAX_CONSIDERED_BEFORE_YIELD) {
+ considered++;
+ goto retry;
+ }
+ /*
+ * free_available == 0
+ * so can't consider any free pages... if
+ * we went to retry in this case, we'd
+ * get stuck looking at the same page
+ * w/o making any forward progress
+ * we also want to take this path if we've already
+ * reached our limit that controls the lock latency
+ */
+ }
+ }
+did_consider:
+ if (considered > MAX_CONSIDERED_BEFORE_YIELD && npages <= 1) {
+ PAGE_REPLACEMENT_ALLOWED(FALSE);
+
+ lck_mtx_unlock(&vm_page_queue_free_lock);
+ vm_page_unlock_queues();
+
+ mutex_pause(0);
+
+ PAGE_REPLACEMENT_ALLOWED(TRUE);
+
+ vm_page_lock_queues();
+ lck_mtx_lock(&vm_page_queue_free_lock);
+
+ RESET_STATE_OF_RUN();
+ /*
+ * reset our free page limit since we
+ * dropped the lock protecting the vm_page_free_queue
+ */
+ free_available = vm_page_free_count - vm_page_free_reserved;
+ considered = 0;
+
+ yielded++;
+
+ goto retry;
+ }
+ considered++;
+ }
+ m = VM_PAGE_NULL;
+
+ if (npages != contig_pages) {
+ if (!wrapped) {
+ /*
+ * We didn't find a contiguous range but we didn't
+ * start from the very first page.
+ * Start again from the very first page.
+ */
+ RESET_STATE_OF_RUN();
+ if (flags & KMA_LOMEM) {
+ idx_last_contig_page_found = vm_page_lomem_find_contiguous_last_idx = 0;
+ } else {
+ idx_last_contig_page_found = vm_page_find_contiguous_last_idx = 0;
+ }
+ last_idx = 0;
+ page_idx = last_idx;
+ wrapped = TRUE;
+ goto retry;
+ }
+ lck_mtx_unlock(&vm_page_queue_free_lock);
+ } else {
+ vm_page_t m1;
+ vm_page_t m2;
+ unsigned int cur_idx;
+ unsigned int tmp_start_idx;
+ vm_object_t locked_object = VM_OBJECT_NULL;
+ boolean_t abort_run = FALSE;
+
+ assert(page_idx - start_idx == contig_pages);
+
+ tmp_start_idx = start_idx;
+
+ /*
+ * first pass through to pull the free pages
+ * off of the free queue so that in case we
+ * need substitute pages, we won't grab any
+ * of the free pages in the run... we'll clear
+ * the 'free' bit in the 2nd pass, and even in
+ * an abort_run case, we'll collect all of the
+ * free pages in this run and return them to the free list
+ */
+ while (start_idx < page_idx) {
+ m1 = &vm_pages[start_idx++];
+
+#if !VM_PAGE_FIND_CONTIGUOUS_CAN_STEAL
+ assert(m1->vmp_q_state == VM_PAGE_ON_FREE_Q);
+#endif
+
+ if (m1->vmp_q_state == VM_PAGE_ON_FREE_Q) {
+ unsigned int color;
+
+ color = VM_PAGE_GET_COLOR(m1);
+#if MACH_ASSERT
+ vm_page_verify_free_list(&vm_page_queue_free[color].qhead, color, m1, TRUE);
+#endif
+ vm_page_queue_remove(&vm_page_queue_free[color].qhead, m1, vmp_pageq);
+
+ VM_PAGE_ZERO_PAGEQ_ENTRY(m1);
+#if MACH_ASSERT
+ vm_page_verify_free_list(&vm_page_queue_free[color].qhead, color, VM_PAGE_NULL, FALSE);
+#endif
+ /*
+ * Clear the "free" bit so that this page
+ * does not get considered for another
+ * concurrent physically-contiguous allocation.
+ */
+ m1->vmp_q_state = VM_PAGE_NOT_ON_Q;
+ assert(m1->vmp_busy);
+
+ vm_page_free_count--;
+ }
+ }
+ if (flags & KMA_LOMEM) {
+ vm_page_lomem_find_contiguous_last_idx = page_idx;
+ } else {
+ vm_page_find_contiguous_last_idx = page_idx;
+ }
+
+ /*
+ * we can drop the free queue lock at this point since
+ * we've pulled any 'free' candidates off of the list
+ * we need it dropped so that we can do a vm_page_grab
+ * when substituing for pmapped/dirty pages
+ */
+ lck_mtx_unlock(&vm_page_queue_free_lock);
+
+ start_idx = tmp_start_idx;
+ cur_idx = page_idx - 1;
+
+ while (start_idx++ < page_idx) {
+ /*
+ * must go through the list from back to front
+ * so that the page list is created in the
+ * correct order - low -> high phys addresses
+ */
+ m1 = &vm_pages[cur_idx--];
+
+ if (m1->vmp_object == 0) {
+ /*
+ * page has already been removed from
+ * the free list in the 1st pass
+ */
+ assert(m1->vmp_q_state == VM_PAGE_NOT_ON_Q);
+ assert(m1->vmp_offset == (vm_object_offset_t) -1);
+ assert(m1->vmp_busy);
+ assert(!m1->vmp_wanted);
+ assert(!m1->vmp_laundry);
+ } else {
+ vm_object_t object;
+ int refmod;
+ boolean_t disconnected, reusable;
+
+ if (abort_run == TRUE) {
+ continue;
+ }
+
+ assert(m1->vmp_q_state != VM_PAGE_NOT_ON_Q);
+
+ object = VM_PAGE_OBJECT(m1);
+
+ if (object != locked_object) {
+ if (locked_object) {
+ vm_object_unlock(locked_object);
+ locked_object = VM_OBJECT_NULL;
+ }
+ if (vm_object_lock_try(object)) {
+ locked_object = object;
+ }
+ }
+ if (locked_object == VM_OBJECT_NULL ||
+ (VM_PAGE_WIRED(m1) || m1->vmp_gobbled ||
+ m1->vmp_laundry || m1->vmp_wanted ||
+ m1->vmp_cleaning || m1->vmp_overwriting || m1->vmp_free_when_done || m1->vmp_busy) ||
+ (m1->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q)) {
+ if (locked_object) {
+ vm_object_unlock(locked_object);
+ locked_object = VM_OBJECT_NULL;
+ }
+ tmp_start_idx = cur_idx;
+ abort_run = TRUE;
+ continue;
+ }
+
+ disconnected = FALSE;
+ reusable = FALSE;
+
+ if ((m1->vmp_reusable ||
+ object->all_reusable) &&
+ (m1->vmp_q_state == VM_PAGE_ON_INACTIVE_INTERNAL_Q) &&
+ !m1->vmp_dirty &&
+ !m1->vmp_reference) {
+ /* reusable page... */
+ refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m1));
+ disconnected = TRUE;
+ if (refmod == 0) {
+ /*
+ * ... not reused: can steal
+ * without relocating contents.
+ */
+ reusable = TRUE;
+ }
+ }
+
+ if ((m1->vmp_pmapped &&
+ !reusable) ||
+ m1->vmp_dirty ||
+ m1->vmp_precious) {
+ vm_object_offset_t offset;
+
+ m2 = vm_page_grab_options(VM_PAGE_GRAB_Q_LOCK_HELD);
+
+ if (m2 == VM_PAGE_NULL) {
+ if (locked_object) {
+ vm_object_unlock(locked_object);
+ locked_object = VM_OBJECT_NULL;
+ }
+ tmp_start_idx = cur_idx;
+ abort_run = TRUE;
+ continue;
+ }
+ if (!disconnected) {
+ if (m1->vmp_pmapped) {
+ refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m1));
+ } else {
+ refmod = 0;
+ }
+ }
+
+ /* copy the page's contents */
+ pmap_copy_page(VM_PAGE_GET_PHYS_PAGE(m1), VM_PAGE_GET_PHYS_PAGE(m2));
+ /* copy the page's state */
+ assert(!VM_PAGE_WIRED(m1));
+ assert(m1->vmp_q_state != VM_PAGE_ON_FREE_Q);
+ assert(m1->vmp_q_state != VM_PAGE_ON_PAGEOUT_Q);
+ assert(!m1->vmp_laundry);
+ m2->vmp_reference = m1->vmp_reference;
+ assert(!m1->vmp_gobbled);
+ assert(!m1->vmp_private);
+ m2->vmp_no_cache = m1->vmp_no_cache;
+ m2->vmp_xpmapped = 0;
+ assert(!m1->vmp_busy);
+ assert(!m1->vmp_wanted);
+ assert(!m1->vmp_fictitious);
+ m2->vmp_pmapped = m1->vmp_pmapped; /* should flush cache ? */
+ m2->vmp_wpmapped = m1->vmp_wpmapped;
+ assert(!m1->vmp_free_when_done);
+ m2->vmp_absent = m1->vmp_absent;
+ m2->vmp_error = m1->vmp_error;
+ m2->vmp_dirty = m1->vmp_dirty;
+ assert(!m1->vmp_cleaning);
+ m2->vmp_precious = m1->vmp_precious;
+ m2->vmp_clustered = m1->vmp_clustered;
+ assert(!m1->vmp_overwriting);
+ m2->vmp_restart = m1->vmp_restart;
+ m2->vmp_unusual = m1->vmp_unusual;
+ m2->vmp_cs_validated = m1->vmp_cs_validated;
+ m2->vmp_cs_tainted = m1->vmp_cs_tainted;
+ m2->vmp_cs_nx = m1->vmp_cs_nx;
+
+ /*
+ * If m1 had really been reusable,
+ * we would have just stolen it, so
+ * let's not propagate it's "reusable"
+ * bit and assert that m2 is not
+ * marked as "reusable".
+ */
+ // m2->vmp_reusable = m1->vmp_reusable;
+ assert(!m2->vmp_reusable);
+
+ // assert(!m1->vmp_lopage);
+
+ if (m1->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) {
+ m2->vmp_q_state = VM_PAGE_USED_BY_COMPRESSOR;
+ }
+
+ /*
+ * page may need to be flushed if
+ * it is marshalled into a UPL
+ * that is going to be used by a device
+ * that doesn't support coherency
+ */
+ m2->vmp_written_by_kernel = TRUE;
+
+ /*
+ * make sure we clear the ref/mod state
+ * from the pmap layer... else we risk
+ * inheriting state from the last time
+ * this page was used...
+ */
+ pmap_clear_refmod(VM_PAGE_GET_PHYS_PAGE(m2), VM_MEM_MODIFIED | VM_MEM_REFERENCED);
+
+ if (refmod & VM_MEM_REFERENCED) {
+ m2->vmp_reference = TRUE;
+ }
+ if (refmod & VM_MEM_MODIFIED) {
+ SET_PAGE_DIRTY(m2, TRUE);
+ }
+ offset = m1->vmp_offset;
+
+ /*
+ * completely cleans up the state
+ * of the page so that it is ready
+ * to be put onto the free list, or
+ * for this purpose it looks like it
+ * just came off of the free list
+ */
+ vm_page_free_prepare(m1);
+
+ /*
+ * now put the substitute page
+ * on the object
+ */
+ vm_page_insert_internal(m2, locked_object, offset, VM_KERN_MEMORY_NONE, TRUE, TRUE, FALSE, FALSE, NULL);
+
+ if (m2->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) {
+ m2->vmp_pmapped = TRUE;
+ m2->vmp_wpmapped = TRUE;
+
+ PMAP_ENTER(kernel_pmap, m2->vmp_offset, m2,
+ VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, 0, TRUE, kr);
+
+ assert(kr == KERN_SUCCESS);
+
+ compressed_pages++;
+ } else {
+ if (m2->vmp_reference) {
+ vm_page_activate(m2);
+ } else {
+ vm_page_deactivate(m2);
+ }
+ }
+ PAGE_WAKEUP_DONE(m2);
+ } else {
+ assert(m1->vmp_q_state != VM_PAGE_USED_BY_COMPRESSOR);
+
+ /*
+ * completely cleans up the state
+ * of the page so that it is ready
+ * to be put onto the free list, or
+ * for this purpose it looks like it
+ * just came off of the free list
+ */
+ vm_page_free_prepare(m1);
+ }
+
+ stolen_pages++;
+ }
+#if CONFIG_BACKGROUND_QUEUE
+ vm_page_assign_background_state(m1);
+#endif
+ VM_PAGE_ZERO_PAGEQ_ENTRY(m1);
+ m1->vmp_snext = m;
+ m = m1;
+ }
+ if (locked_object) {
+ vm_object_unlock(locked_object);
+ locked_object = VM_OBJECT_NULL;
+ }
+
+ if (abort_run == TRUE) {
+ /*
+ * want the index of the last
+ * page in this run that was
+ * successfully 'stolen', so back
+ * it up 1 for the auto-decrement on use
+ * and 1 more to bump back over this page
+ */
+ page_idx = tmp_start_idx + 2;
+ if (page_idx >= vm_pages_count) {
+ if (wrapped) {
+ if (m != VM_PAGE_NULL) {
+ vm_page_unlock_queues();
+ vm_page_free_list(m, FALSE);
+ vm_page_lock_queues();
+ m = VM_PAGE_NULL;
+ }
+ dumped_run++;
+ goto done_scanning;
+ }
+ page_idx = last_idx = 0;
+ wrapped = TRUE;
+ }
+ abort_run = FALSE;
+
+ /*
+ * We didn't find a contiguous range but we didn't
+ * start from the very first page.
+ * Start again from the very first page.
+ */
+ RESET_STATE_OF_RUN();
+
+ if (flags & KMA_LOMEM) {
+ idx_last_contig_page_found = vm_page_lomem_find_contiguous_last_idx = page_idx;
+ } else {
+ idx_last_contig_page_found = vm_page_find_contiguous_last_idx = page_idx;
+ }
+
+ last_idx = page_idx;
+
+ if (m != VM_PAGE_NULL) {
+ vm_page_unlock_queues();
+ vm_page_free_list(m, FALSE);
+ vm_page_lock_queues();
+ m = VM_PAGE_NULL;
+ }
+ dumped_run++;
+
+ lck_mtx_lock(&vm_page_queue_free_lock);
+ /*
+ * reset our free page limit since we
+ * dropped the lock protecting the vm_page_free_queue
+ */
+ free_available = vm_page_free_count - vm_page_free_reserved;
+ goto retry;
+ }
+
+ for (m1 = m; m1 != VM_PAGE_NULL; m1 = NEXT_PAGE(m1)) {
+ assert(m1->vmp_q_state == VM_PAGE_NOT_ON_Q);
+ assert(m1->vmp_wire_count == 0);
+
+ if (wire == TRUE) {
+ m1->vmp_wire_count++;
+ m1->vmp_q_state = VM_PAGE_IS_WIRED;
+ } else {
+ m1->vmp_gobbled = TRUE;
+ }
+ }
+ if (wire == FALSE) {
+ vm_page_gobble_count += npages;
+ }
+
+ /*
+ * gobbled pages are also counted as wired pages
+ */
+ vm_page_wire_count += npages;
+
+ assert(vm_page_verify_contiguous(m, npages));
+ }
+done_scanning:
+ PAGE_REPLACEMENT_ALLOWED(FALSE);
+
+ vm_page_unlock_queues();
+
+#if DEBUG
+ clock_get_system_microtime(&tv_end_sec, &tv_end_usec);
+
+ tv_end_sec -= tv_start_sec;
+ if (tv_end_usec < tv_start_usec) {
+ tv_end_sec--;
+ tv_end_usec += 1000000;
+ }
+ tv_end_usec -= tv_start_usec;
+ if (tv_end_usec >= 1000000) {
+ tv_end_sec++;
+ tv_end_sec -= 1000000;
+ }
+ if (vm_page_find_contig_debug) {
+ printf("%s(num=%d,low=%d): found %d pages at 0x%llx in %ld.%06ds... started at %d... scanned %d pages... yielded %d times... dumped run %d times... stole %d pages... stole %d compressed pages\n",
+ __func__, contig_pages, max_pnum, npages, (vm_object_offset_t)start_pnum << PAGE_SHIFT,
+ (long)tv_end_sec, tv_end_usec, orig_last_idx,
+ scanned, yielded, dumped_run, stolen_pages, compressed_pages);
+ }
+
+#endif
+#if MACH_ASSERT
+ vm_page_verify_free_lists();
+#endif
+ if (m == NULL && zone_gc_called == FALSE) {
+ printf("%s(num=%d,low=%d): found %d pages at 0x%llx...scanned %d pages... yielded %d times... dumped run %d times... stole %d pages... stole %d compressed pages... wired count is %d\n",
+ __func__, contig_pages, max_pnum, npages, (vm_object_offset_t)start_pnum << PAGE_SHIFT,
+ scanned, yielded, dumped_run, stolen_pages, compressed_pages, vm_page_wire_count);
+
+ if (consider_buffer_cache_collect != NULL) {
+ (void)(*consider_buffer_cache_collect)(1);
+ }
+
+ consider_zone_gc(FALSE);
+
+ zone_gc_called = TRUE;
+
+ printf("vm_page_find_contiguous: zone_gc called... wired count is %d\n", vm_page_wire_count);
+ goto full_scan_again;
+ }
+
+ return m;
+}
+
+/*
+ * Allocate a list of contiguous, wired pages.
+ */
+kern_return_t
+cpm_allocate(
+ vm_size_t size,
+ vm_page_t *list,
+ ppnum_t max_pnum,
+ ppnum_t pnum_mask,
+ boolean_t wire,
+ int flags)
+{
+ vm_page_t pages;
+ unsigned int npages;
+
+ if (size % PAGE_SIZE != 0) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ npages = (unsigned int) (size / PAGE_SIZE);
+ if (npages != size / PAGE_SIZE) {
+ /* 32-bit overflow */
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ /*
+ * Obtain a pointer to a subset of the free
+ * list large enough to satisfy the request;
+ * the region will be physically contiguous.
+ */
+ pages = vm_page_find_contiguous(npages, max_pnum, pnum_mask, wire, flags);
+
+ if (pages == VM_PAGE_NULL) {
+ return KERN_NO_SPACE;
+ }
+ /*
+ * determine need for wakeups
+ */
+ if (vm_page_free_count < vm_page_free_min) {
+ thread_wakeup((event_t) &vm_page_free_wanted);
+ }
+
+ VM_CHECK_MEMORYSTATUS;
+
+ /*
+ * The CPM pages should now be available and
+ * ordered by ascending physical address.
+ */
+ assert(vm_page_verify_contiguous(pages, npages));
+
+ *list = pages;
+ return KERN_SUCCESS;
+}
+
+
+unsigned int vm_max_delayed_work_limit = DEFAULT_DELAYED_WORK_LIMIT;
+
+/*
+ * when working on a 'run' of pages, it is necessary to hold
+ * the vm_page_queue_lock (a hot global lock) for certain operations
+ * on the page... however, the majority of the work can be done
+ * while merely holding the object lock... in fact there are certain
+ * collections of pages that don't require any work brokered by the
+ * vm_page_queue_lock... to mitigate the time spent behind the global
+ * lock, go to a 2 pass algorithm... collect pages up to DELAYED_WORK_LIMIT
+ * while doing all of the work that doesn't require the vm_page_queue_lock...
+ * then call vm_page_do_delayed_work to acquire the vm_page_queue_lock and do the
+ * necessary work for each page... we will grab the busy bit on the page
+ * if it's not already held so that vm_page_do_delayed_work can drop the object lock
+ * if it can't immediately take the vm_page_queue_lock in order to compete
+ * for the locks in the same order that vm_pageout_scan takes them.
+ * the operation names are modeled after the names of the routines that
+ * need to be called in order to make the changes very obvious in the
+ * original loop
+ */
+
+void
+vm_page_do_delayed_work(
+ vm_object_t object,
+ vm_tag_t tag,
+ struct vm_page_delayed_work *dwp,
+ int dw_count)
+{
+ int j;
+ vm_page_t m;
+ vm_page_t local_free_q = VM_PAGE_NULL;
+
+ /*
+ * pageout_scan takes the vm_page_lock_queues first
+ * then tries for the object lock... to avoid what
+ * is effectively a lock inversion, we'll go to the
+ * trouble of taking them in that same order... otherwise
+ * if this object contains the majority of the pages resident
+ * in the UBC (or a small set of large objects actively being
+ * worked on contain the majority of the pages), we could
+ * cause the pageout_scan thread to 'starve' in its attempt
+ * to find pages to move to the free queue, since it has to
+ * successfully acquire the object lock of any candidate page
+ * before it can steal/clean it.
+ */
+ if (!vm_page_trylockspin_queues()) {
+ vm_object_unlock(object);
+
+ vm_page_lockspin_queues();
+
+ for (j = 0;; j++) {
+ if (!vm_object_lock_avoid(object) &&
+ _vm_object_lock_try(object)) {
+ break;
+ }
+ vm_page_unlock_queues();
+ mutex_pause(j);
+ vm_page_lockspin_queues();
+ }
+ }
+ for (j = 0; j < dw_count; j++, dwp++) {
+ m = dwp->dw_m;
+
+ if (dwp->dw_mask & DW_vm_pageout_throttle_up) {
+ vm_pageout_throttle_up(m);
+ }
+#if CONFIG_PHANTOM_CACHE
+ if (dwp->dw_mask & DW_vm_phantom_cache_update) {
+ vm_phantom_cache_update(m);
+ }
+#endif
+ if (dwp->dw_mask & DW_vm_page_wire) {
+ vm_page_wire(m, tag, FALSE);
+ } else if (dwp->dw_mask & DW_vm_page_unwire) {
+ boolean_t queueit;
+
+ queueit = (dwp->dw_mask & (DW_vm_page_free | DW_vm_page_deactivate_internal)) ? FALSE : TRUE;
+
+ vm_page_unwire(m, queueit);
+ }
+ if (dwp->dw_mask & DW_vm_page_free) {
+ vm_page_free_prepare_queues(m);
+
+ assert(m->vmp_pageq.next == 0 && m->vmp_pageq.prev == 0);
+ /*
+ * Add this page to our list of reclaimed pages,
+ * to be freed later.
+ */
+ m->vmp_snext = local_free_q;
+ local_free_q = m;
+ } else {
+ if (dwp->dw_mask & DW_vm_page_deactivate_internal) {
+ vm_page_deactivate_internal(m, FALSE);
+ } else if (dwp->dw_mask & DW_vm_page_activate) {
+ if (m->vmp_q_state != VM_PAGE_ON_ACTIVE_Q) {
+ vm_page_activate(m);
+ }
+ } else if (dwp->dw_mask & DW_vm_page_speculate) {
+ vm_page_speculate(m, TRUE);
+ } else if (dwp->dw_mask & DW_enqueue_cleaned) {
+ /*
+ * if we didn't hold the object lock and did this,
+ * we might disconnect the page, then someone might
+ * soft fault it back in, then we would put it on the
+ * cleaned queue, and so we would have a referenced (maybe even dirty)
+ * page on that queue, which we don't want
+ */
+ int refmod_state = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
+
+ if ((refmod_state & VM_MEM_REFERENCED)) {
+ /*
+ * this page has been touched since it got cleaned; let's activate it
+ * if it hasn't already been
+ */
+ VM_PAGEOUT_DEBUG(vm_pageout_enqueued_cleaned, 1);
+ VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated, 1);
+
+ if (m->vmp_q_state != VM_PAGE_ON_ACTIVE_Q) {
+ vm_page_activate(m);
+ }
+ } else {
+ m->vmp_reference = FALSE;
+ vm_page_enqueue_cleaned(m);
+ }
+ } else if (dwp->dw_mask & DW_vm_page_lru) {
+ vm_page_lru(m);
+ } else if (dwp->dw_mask & DW_VM_PAGE_QUEUES_REMOVE) {
+ if (m->vmp_q_state != VM_PAGE_ON_PAGEOUT_Q) {
+ vm_page_queues_remove(m, TRUE);
+ }
+ }
+ if (dwp->dw_mask & DW_set_reference) {
+ m->vmp_reference = TRUE;
+ } else if (dwp->dw_mask & DW_clear_reference) {
+ m->vmp_reference = FALSE;
+ }
+
+ if (dwp->dw_mask & DW_move_page) {
+ if (m->vmp_q_state != VM_PAGE_ON_PAGEOUT_Q) {
+ vm_page_queues_remove(m, FALSE);
+
+ assert(VM_PAGE_OBJECT(m) != kernel_object);
+
+ vm_page_enqueue_inactive(m, FALSE);
+ }
+ }
+ if (dwp->dw_mask & DW_clear_busy) {
+ m->vmp_busy = FALSE;
+ }
+
+ if (dwp->dw_mask & DW_PAGE_WAKEUP) {
+ PAGE_WAKEUP(m);
+ }
+ }
+ }
+ vm_page_unlock_queues();
+
+ if (local_free_q) {
+ vm_page_free_list(local_free_q, TRUE);
+ }
+
+ VM_CHECK_MEMORYSTATUS;
+}
+
+kern_return_t
+vm_page_alloc_list(
+ int page_count,
+ int flags,
+ vm_page_t *list)
+{
+ vm_page_t lo_page_list = VM_PAGE_NULL;
+ vm_page_t mem;
+ int i;
+
+ if (!(flags & KMA_LOMEM)) {
+ panic("vm_page_alloc_list: called w/o KMA_LOMEM");
+ }
+
+ for (i = 0; i < page_count; i++) {
+ mem = vm_page_grablo();
+
+ if (mem == VM_PAGE_NULL) {
+ if (lo_page_list) {
+ vm_page_free_list(lo_page_list, FALSE);
+ }
+
+ *list = VM_PAGE_NULL;
+
+ return KERN_RESOURCE_SHORTAGE;
+ }
+ mem->vmp_snext = lo_page_list;
+ lo_page_list = mem;
+ }
+ *list = lo_page_list;
+
+ return KERN_SUCCESS;
+}
+
+void
+vm_page_set_offset(vm_page_t page, vm_object_offset_t offset)
+{
+ page->vmp_offset = offset;
+}
+
+vm_page_t
+vm_page_get_next(vm_page_t page)
+{
+ return page->vmp_snext;
+}
+
+vm_object_offset_t
+vm_page_get_offset(vm_page_t page)
+{
+ return page->vmp_offset;
+}
+
+ppnum_t
+vm_page_get_phys_page(vm_page_t page)
+{
+ return VM_PAGE_GET_PHYS_PAGE(page);
+}
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+#if HIBERNATION
+
+static vm_page_t hibernate_gobble_queue;
+
+static int hibernate_drain_pageout_queue(struct vm_pageout_queue *);
+static int hibernate_flush_dirty_pages(int);
+static int hibernate_flush_queue(vm_page_queue_head_t *, int);
+
+void hibernate_flush_wait(void);
+void hibernate_mark_in_progress(void);
+void hibernate_clear_in_progress(void);
+
+void hibernate_free_range(int, int);
+void hibernate_hash_insert_page(vm_page_t);
+uint32_t hibernate_mark_as_unneeded(addr64_t, addr64_t, hibernate_page_list_t *, hibernate_page_list_t *);
+void hibernate_rebuild_vm_structs(void);
+uint32_t hibernate_teardown_vm_structs(hibernate_page_list_t *, hibernate_page_list_t *);
+ppnum_t hibernate_lookup_paddr(unsigned int);
+
+struct hibernate_statistics {
+ int hibernate_considered;
+ int hibernate_reentered_on_q;
+ int hibernate_found_dirty;
+ int hibernate_skipped_cleaning;
+ int hibernate_skipped_transient;
+ int hibernate_skipped_precious;
+ int hibernate_skipped_external;
+ int hibernate_queue_nolock;
+ int hibernate_queue_paused;
+ int hibernate_throttled;
+ int hibernate_throttle_timeout;
+ int hibernate_drained;
+ int hibernate_drain_timeout;
+ int cd_lock_failed;
+ int cd_found_precious;
+ int cd_found_wired;
+ int cd_found_busy;
+ int cd_found_unusual;
+ int cd_found_cleaning;
+ int cd_found_laundry;
+ int cd_found_dirty;
+ int cd_found_xpmapped;
+ int cd_skipped_xpmapped;
+ int cd_local_free;
+ int cd_total_free;
+ int cd_vm_page_wire_count;
+ int cd_vm_struct_pages_unneeded;
+ int cd_pages;
+ int cd_discarded;
+ int cd_count_wire;
+} hibernate_stats;
+
+
+/*
+ * clamp the number of 'xpmapped' pages we'll sweep into the hibernation image
+ * so that we don't overrun the estimated image size, which would
+ * result in a hibernation failure.
+ */
+#define HIBERNATE_XPMAPPED_LIMIT 40000
+
+
+static int
+hibernate_drain_pageout_queue(struct vm_pageout_queue *q)
+{
+ wait_result_t wait_result;
+
+ vm_page_lock_queues();
+
+ while (!vm_page_queue_empty(&q->pgo_pending)) {
+ q->pgo_draining = TRUE;
+
+ assert_wait_timeout((event_t) (&q->pgo_laundry + 1), THREAD_INTERRUPTIBLE, 5000, 1000 * NSEC_PER_USEC);
+
+ vm_page_unlock_queues();
+
+ wait_result = thread_block(THREAD_CONTINUE_NULL);
+
+ if (wait_result == THREAD_TIMED_OUT && !vm_page_queue_empty(&q->pgo_pending)) {
+ hibernate_stats.hibernate_drain_timeout++;
+
+ if (q == &vm_pageout_queue_external) {
+ return 0;
+ }
+
+ return 1;
+ }
+ vm_page_lock_queues();
+
+ hibernate_stats.hibernate_drained++;
+ }
+ vm_page_unlock_queues();
+
+ return 0;
+}
+
+
+boolean_t hibernate_skip_external = FALSE;
+
+static int
+hibernate_flush_queue(vm_page_queue_head_t *q, int qcount)
+{
+ vm_page_t m;
+ vm_object_t l_object = NULL;
+ vm_object_t m_object = NULL;
+ int refmod_state = 0;
+ int try_failed_count = 0;
+ int retval = 0;
+ int current_run = 0;
+ struct vm_pageout_queue *iq;
+ struct vm_pageout_queue *eq;
+ struct vm_pageout_queue *tq;
+
+ KDBG(IOKDBG_CODE(DBG_HIBERNATE, 4) | DBG_FUNC_START,
+ VM_KERNEL_UNSLIDE_OR_PERM(q), qcount);
+
+ iq = &vm_pageout_queue_internal;
+ eq = &vm_pageout_queue_external;
+
+ vm_page_lock_queues();
+
+ while (qcount && !vm_page_queue_empty(q)) {
+ if (current_run++ == 1000) {
+ if (hibernate_should_abort()) {
+ retval = 1;
+ break;
+ }
+ current_run = 0;
+ }
+
+ m = (vm_page_t) vm_page_queue_first(q);
+ m_object = VM_PAGE_OBJECT(m);
+
+ /*
+ * check to see if we currently are working
+ * with the same object... if so, we've
+ * already got the lock
+ */
+ if (m_object != l_object) {
+ /*
+ * the object associated with candidate page is
+ * different from the one we were just working
+ * with... dump the lock if we still own it
+ */
+ if (l_object != NULL) {
+ vm_object_unlock(l_object);
+ l_object = NULL;
+ }
+ /*
+ * Try to lock object; since we've alread got the
+ * page queues lock, we can only 'try' for this one.
+ * if the 'try' fails, we need to do a mutex_pause
+ * to allow the owner of the object lock a chance to
+ * run...
+ */
+ if (!vm_object_lock_try_scan(m_object)) {
+ if (try_failed_count > 20) {
+ hibernate_stats.hibernate_queue_nolock++;
+
+ goto reenter_pg_on_q;
+ }
+
+ vm_page_unlock_queues();
+ mutex_pause(try_failed_count++);
+ vm_page_lock_queues();
+
+ hibernate_stats.hibernate_queue_paused++;
+ continue;
+ } else {
+ l_object = m_object;
+ }
+ }
+ if (!m_object->alive || m->vmp_cleaning || m->vmp_laundry || m->vmp_busy || m->vmp_absent || m->vmp_error) {
+ /*
+ * page is not to be cleaned
+ * put it back on the head of its queue
+ */
+ if (m->vmp_cleaning) {
+ hibernate_stats.hibernate_skipped_cleaning++;
+ } else {
+ hibernate_stats.hibernate_skipped_transient++;
+ }
+
+ goto reenter_pg_on_q;
+ }
+ if (m_object->copy == VM_OBJECT_NULL) {
+ if (m_object->purgable == VM_PURGABLE_VOLATILE || m_object->purgable == VM_PURGABLE_EMPTY) {
+ /*
+ * let the normal hibernate image path
+ * deal with these
+ */
+ goto reenter_pg_on_q;
+ }
+ }
+ if (!m->vmp_dirty && m->vmp_pmapped) {
+ refmod_state = pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(m));
+
+ if ((refmod_state & VM_MEM_MODIFIED)) {
+ SET_PAGE_DIRTY(m, FALSE);
+ }
+ } else {
+ refmod_state = 0;
+ }
+
+ if (!m->vmp_dirty) {
+ /*
+ * page is not to be cleaned
+ * put it back on the head of its queue
+ */
+ if (m->vmp_precious) {
+ hibernate_stats.hibernate_skipped_precious++;
+ }
+
+ goto reenter_pg_on_q;
+ }
+
+ if (hibernate_skip_external == TRUE && !m_object->internal) {
+ hibernate_stats.hibernate_skipped_external++;
+
+ goto reenter_pg_on_q;
+ }
+ tq = NULL;
+
+ if (m_object->internal) {
+ if (VM_PAGE_Q_THROTTLED(iq)) {
+ tq = iq;
+ }
+ } else if (VM_PAGE_Q_THROTTLED(eq)) {
+ tq = eq;
+ }
+
+ if (tq != NULL) {
+ wait_result_t wait_result;
+ int wait_count = 5;
+
+ if (l_object != NULL) {
+ vm_object_unlock(l_object);
+ l_object = NULL;
+ }
+
+ while (retval == 0) {
+ tq->pgo_throttled = TRUE;
+
+ assert_wait_timeout((event_t) &tq->pgo_laundry, THREAD_INTERRUPTIBLE, 1000, 1000 * NSEC_PER_USEC);
+
+ vm_page_unlock_queues();
+
+ wait_result = thread_block(THREAD_CONTINUE_NULL);
+
+ vm_page_lock_queues();
+
+ if (wait_result != THREAD_TIMED_OUT) {
+ break;
+ }
+ if (!VM_PAGE_Q_THROTTLED(tq)) {
+ break;
+ }
+
+ if (hibernate_should_abort()) {
+ retval = 1;
+ }
+
+ if (--wait_count == 0) {
+ hibernate_stats.hibernate_throttle_timeout++;
+
+ if (tq == eq) {
+ hibernate_skip_external = TRUE;
+ break;
+ }
+ retval = 1;
+ }
+ }
+ if (retval) {
+ break;
+ }
+
+ hibernate_stats.hibernate_throttled++;
+
+ continue;
+ }
+ /*
+ * we've already factored out pages in the laundry which
+ * means this page can't be on the pageout queue so it's
+ * safe to do the vm_page_queues_remove
+ */
+ vm_page_queues_remove(m, TRUE);
+
+ if (m_object->internal == TRUE) {
+ pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(m), PMAP_OPTIONS_COMPRESSOR, NULL);
+ }
+
+ vm_pageout_cluster(m);
+
+ hibernate_stats.hibernate_found_dirty++;
+
+ goto next_pg;
+
+reenter_pg_on_q:
+ vm_page_queue_remove(q, m, vmp_pageq);
+ vm_page_queue_enter(q, m, vmp_pageq);
+
+ hibernate_stats.hibernate_reentered_on_q++;
+next_pg:
+ hibernate_stats.hibernate_considered++;
+
+ qcount--;
+ try_failed_count = 0;
+ }
+ if (l_object != NULL) {
+ vm_object_unlock(l_object);
+ l_object = NULL;
+ }
+
+ vm_page_unlock_queues();
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 4) | DBG_FUNC_END, hibernate_stats.hibernate_found_dirty, retval, 0, 0, 0);
+
+ return retval;
+}
+
+
+static int
+hibernate_flush_dirty_pages(int pass)
+{
+ struct vm_speculative_age_q *aq;
+ uint32_t i;
+
+ if (vm_page_local_q) {
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ vm_page_reactivate_local(i, TRUE, FALSE);
+ }
+ }
+
+ for (i = 0; i <= VM_PAGE_MAX_SPECULATIVE_AGE_Q; i++) {
+ int qcount;
+ vm_page_t m;
+
+ aq = &vm_page_queue_speculative[i];
+
+ if (vm_page_queue_empty(&aq->age_q)) {
+ continue;
+ }
+ qcount = 0;
+
+ vm_page_lockspin_queues();
+
+ vm_page_queue_iterate(&aq->age_q, m, vmp_pageq) {
+ qcount++;
+ }
+ vm_page_unlock_queues();
+
+ if (qcount) {
+ if (hibernate_flush_queue(&aq->age_q, qcount)) {
+ return 1;
+ }
+ }
+ }
+ if (hibernate_flush_queue(&vm_page_queue_inactive, vm_page_inactive_count - vm_page_anonymous_count - vm_page_cleaned_count)) {
+ return 1;
+ }
+ /* XXX FBDP TODO: flush secluded queue */
+ if (hibernate_flush_queue(&vm_page_queue_anonymous, vm_page_anonymous_count)) {
+ return 1;
+ }
+ if (hibernate_flush_queue(&vm_page_queue_cleaned, vm_page_cleaned_count)) {
+ return 1;
+ }
+ if (hibernate_drain_pageout_queue(&vm_pageout_queue_internal)) {
+ return 1;
+ }
+
+ if (pass == 1) {
+ vm_compressor_record_warmup_start();
+ }
+
+ if (hibernate_flush_queue(&vm_page_queue_active, vm_page_active_count)) {
+ if (pass == 1) {
+ vm_compressor_record_warmup_end();
+ }
+ return 1;
+ }
+ if (hibernate_drain_pageout_queue(&vm_pageout_queue_internal)) {
+ if (pass == 1) {
+ vm_compressor_record_warmup_end();
+ }
+ return 1;
+ }
+ if (pass == 1) {
+ vm_compressor_record_warmup_end();
+ }
+
+ if (hibernate_skip_external == FALSE && hibernate_drain_pageout_queue(&vm_pageout_queue_external)) {
+ return 1;
+ }
+
+ return 0;
+}
+
+
+void
+hibernate_reset_stats()
+{
+ bzero(&hibernate_stats, sizeof(struct hibernate_statistics));
+}
+
+
+int
+hibernate_flush_memory()
+{
+ int retval;
+
+ assert(VM_CONFIG_COMPRESSOR_IS_PRESENT);
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 3) | DBG_FUNC_START, vm_page_free_count, 0, 0, 0, 0);
+
+ hibernate_cleaning_in_progress = TRUE;
+ hibernate_skip_external = FALSE;
+
+ if ((retval = hibernate_flush_dirty_pages(1)) == 0) {
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 10) | DBG_FUNC_START, VM_PAGE_COMPRESSOR_COUNT, 0, 0, 0, 0);
+
+ vm_compressor_flush();
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 10) | DBG_FUNC_END, VM_PAGE_COMPRESSOR_COUNT, 0, 0, 0, 0);
+
+ if (consider_buffer_cache_collect != NULL) {
+ unsigned int orig_wire_count;
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 7) | DBG_FUNC_START, 0, 0, 0, 0, 0);
+ orig_wire_count = vm_page_wire_count;
+
+ (void)(*consider_buffer_cache_collect)(1);
+ consider_zone_gc(FALSE);
+
+ HIBLOG("hibernate_flush_memory: buffer_cache_gc freed up %d wired pages\n", orig_wire_count - vm_page_wire_count);
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 7) | DBG_FUNC_END, orig_wire_count - vm_page_wire_count, 0, 0, 0, 0);
+ }
+ }
+ hibernate_cleaning_in_progress = FALSE;
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 3) | DBG_FUNC_END, vm_page_free_count, hibernate_stats.hibernate_found_dirty, retval, 0, 0);
+
+ if (retval) {
+ HIBLOG("hibernate_flush_memory() failed to finish - vm_page_compressor_count(%d)\n", VM_PAGE_COMPRESSOR_COUNT);
+ }
+
+
+ HIBPRINT("hibernate_flush_memory() considered(%d) reentered_on_q(%d) found_dirty(%d)\n",
+ hibernate_stats.hibernate_considered,
+ hibernate_stats.hibernate_reentered_on_q,
+ hibernate_stats.hibernate_found_dirty);
+ HIBPRINT(" skipped_cleaning(%d) skipped_transient(%d) skipped_precious(%d) skipped_external(%d) queue_nolock(%d)\n",
+ hibernate_stats.hibernate_skipped_cleaning,
+ hibernate_stats.hibernate_skipped_transient,
+ hibernate_stats.hibernate_skipped_precious,
+ hibernate_stats.hibernate_skipped_external,
+ hibernate_stats.hibernate_queue_nolock);
+ HIBPRINT(" queue_paused(%d) throttled(%d) throttle_timeout(%d) drained(%d) drain_timeout(%d)\n",
+ hibernate_stats.hibernate_queue_paused,
+ hibernate_stats.hibernate_throttled,
+ hibernate_stats.hibernate_throttle_timeout,
+ hibernate_stats.hibernate_drained,
+ hibernate_stats.hibernate_drain_timeout);
+
+ return retval;
+}
+
+
+static void
+hibernate_page_list_zero(hibernate_page_list_t *list)
+{
+ uint32_t bank;
+ hibernate_bitmap_t * bitmap;
+
+ bitmap = &list->bank_bitmap[0];
+ for (bank = 0; bank < list->bank_count; bank++) {
+ uint32_t last_bit;
+
+ bzero((void *) &bitmap->bitmap[0], bitmap->bitmapwords << 2);
+ // set out-of-bound bits at end of bitmap.
+ last_bit = ((bitmap->last_page - bitmap->first_page + 1) & 31);
+ if (last_bit) {
+ bitmap->bitmap[bitmap->bitmapwords - 1] = (0xFFFFFFFF >> last_bit);
+ }
+
+ bitmap = (hibernate_bitmap_t *) &bitmap->bitmap[bitmap->bitmapwords];
+ }
+}
+
+void
+hibernate_free_gobble_pages(void)
+{
+ vm_page_t m, next;
+ uint32_t count = 0;
+
+ m = (vm_page_t) hibernate_gobble_queue;
+ while (m) {
+ next = m->vmp_snext;
+ vm_page_free(m);
+ count++;
+ m = next;
+ }
+ hibernate_gobble_queue = VM_PAGE_NULL;
+
+ if (count) {
+ HIBLOG("Freed %d pages\n", count);
+ }
+}
+
+static boolean_t
+hibernate_consider_discard(vm_page_t m, boolean_t preflight)
+{
+ vm_object_t object = NULL;
+ int refmod_state;
+ boolean_t discard = FALSE;
+
+ do{
+ if (m->vmp_private) {
+ panic("hibernate_consider_discard: private");
+ }
+
+ object = VM_PAGE_OBJECT(m);
+
+ if (!vm_object_lock_try(object)) {
+ object = NULL;
+ if (!preflight) {
+ hibernate_stats.cd_lock_failed++;
+ }
+ break;
+ }
+ if (VM_PAGE_WIRED(m)) {
+ if (!preflight) {
+ hibernate_stats.cd_found_wired++;
+ }
+ break;
+ }
+ if (m->vmp_precious) {
+ if (!preflight) {
+ hibernate_stats.cd_found_precious++;
+ }
+ break;
+ }
+ if (m->vmp_busy || !object->alive) {
+ /*
+ * Somebody is playing with this page.
+ */
+ if (!preflight) {
+ hibernate_stats.cd_found_busy++;
+ }
+ break;
+ }
+ if (m->vmp_absent || m->vmp_unusual || m->vmp_error) {
+ /*
+ * If it's unusual in anyway, ignore it
+ */
+ if (!preflight) {
+ hibernate_stats.cd_found_unusual++;
+ }
+ break;
+ }
+ if (m->vmp_cleaning) {
+ if (!preflight) {
+ hibernate_stats.cd_found_cleaning++;
+ }
+ break;
+ }
+ if (m->vmp_laundry) {
+ if (!preflight) {
+ hibernate_stats.cd_found_laundry++;
+ }
+ break;
+ }
+ if (!m->vmp_dirty) {
+ refmod_state = pmap_get_refmod(VM_PAGE_GET_PHYS_PAGE(m));
+
+ if (refmod_state & VM_MEM_REFERENCED) {
+ m->vmp_reference = TRUE;
+ }
+ if (refmod_state & VM_MEM_MODIFIED) {
+ SET_PAGE_DIRTY(m, FALSE);
+ }
+ }
+
+ /*
+ * If it's clean or purgeable we can discard the page on wakeup.
+ */
+ discard = (!m->vmp_dirty)
+ || (VM_PURGABLE_VOLATILE == object->purgable)
+ || (VM_PURGABLE_EMPTY == object->purgable);
+
+
+ if (discard == FALSE) {
+ if (!preflight) {
+ hibernate_stats.cd_found_dirty++;
+ }
+ } else if (m->vmp_xpmapped && m->vmp_reference && !object->internal) {
+ if (hibernate_stats.cd_found_xpmapped < HIBERNATE_XPMAPPED_LIMIT) {
+ if (!preflight) {
+ hibernate_stats.cd_found_xpmapped++;
+ }
+ discard = FALSE;
+ } else {
+ if (!preflight) {
+ hibernate_stats.cd_skipped_xpmapped++;
+ }
+ }
+ }
+ }while (FALSE);
+
+ if (object) {
+ vm_object_unlock(object);
+ }
+
+ return discard;
+}
+
+
+static void
+hibernate_discard_page(vm_page_t m)
+{
+ vm_object_t m_object;
+
+ if (m->vmp_absent || m->vmp_unusual || m->vmp_error) {
+ /*
+ * If it's unusual in anyway, ignore
+ */
+ return;
+ }
+
+ m_object = VM_PAGE_OBJECT(m);
+
+#if MACH_ASSERT || DEBUG
+ if (!vm_object_lock_try(m_object)) {
+ panic("hibernate_discard_page(%p) !vm_object_lock_try", m);
+ }
+#else
+ /* No need to lock page queue for token delete, hibernate_vm_unlock()
+ * makes sure these locks are uncontended before sleep */
+#endif /* MACH_ASSERT || DEBUG */
+
+ if (m->vmp_pmapped == TRUE) {
+ __unused int refmod_state = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m));
+ }
+
+ if (m->vmp_laundry) {
+ panic("hibernate_discard_page(%p) laundry", m);
+ }
+ if (m->vmp_private) {
+ panic("hibernate_discard_page(%p) private", m);
+ }
+ if (m->vmp_fictitious) {
+ panic("hibernate_discard_page(%p) fictitious", m);
+ }
+
+ if (VM_PURGABLE_VOLATILE == m_object->purgable) {
+ /* object should be on a queue */
+ assert((m_object->objq.next != NULL) && (m_object->objq.prev != NULL));
+ purgeable_q_t old_queue = vm_purgeable_object_remove(m_object);
+ assert(old_queue);
+ if (m_object->purgeable_when_ripe) {
+ vm_purgeable_token_delete_first(old_queue);
+ }
+ vm_object_lock_assert_exclusive(m_object);
+ m_object->purgable = VM_PURGABLE_EMPTY;
+
+ /*
+ * Purgeable ledgers: pages of VOLATILE and EMPTY objects are
+ * accounted in the "volatile" ledger, so no change here.
+ * We have to update vm_page_purgeable_count, though, since we're
+ * effectively purging this object.
+ */
+ unsigned int delta;
+ assert(m_object->resident_page_count >= m_object->wired_page_count);
+ delta = (m_object->resident_page_count - m_object->wired_page_count);
+ assert(vm_page_purgeable_count >= delta);
+ assert(delta > 0);
+ OSAddAtomic(-delta, (SInt32 *)&vm_page_purgeable_count);
+ }
+
+ vm_page_free(m);
+
+#if MACH_ASSERT || DEBUG
+ vm_object_unlock(m_object);
+#endif /* MACH_ASSERT || DEBUG */
+}
+
+/*
+ * Grab locks for hibernate_page_list_setall()
+ */
+void
+hibernate_vm_lock_queues(void)
+{
+ vm_object_lock(compressor_object);
+ vm_page_lock_queues();
+ lck_mtx_lock(&vm_page_queue_free_lock);
+ lck_mtx_lock(&vm_purgeable_queue_lock);
+
+ if (vm_page_local_q) {
+ uint32_t i;
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ struct vpl *lq;
+ lq = &vm_page_local_q[i].vpl_un.vpl;
+ VPL_LOCK(&lq->vpl_lock);
+ }
+ }
+}
+
+void
+hibernate_vm_unlock_queues(void)
+{
+ if (vm_page_local_q) {
+ uint32_t i;
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ struct vpl *lq;
+ lq = &vm_page_local_q[i].vpl_un.vpl;
+ VPL_UNLOCK(&lq->vpl_lock);
+ }
+ }
+ lck_mtx_unlock(&vm_purgeable_queue_lock);
+ lck_mtx_unlock(&vm_page_queue_free_lock);
+ vm_page_unlock_queues();
+ vm_object_unlock(compressor_object);
+}
+
+/*
+ * Bits zero in the bitmaps => page needs to be saved. All pages default to be saved,
+ * pages known to VM to not need saving are subtracted.
+ * Wired pages to be saved are present in page_list_wired, pageable in page_list.
+ */
+
+void
+hibernate_page_list_setall(hibernate_page_list_t * page_list,
+ hibernate_page_list_t * page_list_wired,
+ hibernate_page_list_t * page_list_pal,
+ boolean_t preflight,
+ boolean_t will_discard,
+ uint32_t * pagesOut)
+{
+ uint64_t start, end, nsec;
+ vm_page_t m;
+ vm_page_t next;
+ uint32_t pages = page_list->page_count;
+ uint32_t count_anonymous = 0, count_throttled = 0, count_compressor = 0;
+ uint32_t count_inactive = 0, count_active = 0, count_speculative = 0, count_cleaned = 0;
+ uint32_t count_wire = pages;
+ uint32_t count_discard_active = 0;
+ uint32_t count_discard_inactive = 0;
+ uint32_t count_discard_cleaned = 0;
+ uint32_t count_discard_purgeable = 0;
+ uint32_t count_discard_speculative = 0;
+ uint32_t count_discard_vm_struct_pages = 0;
+ uint32_t i;
+ uint32_t bank;
+ hibernate_bitmap_t * bitmap;
+ hibernate_bitmap_t * bitmap_wired;
+ boolean_t discard_all;
+ boolean_t discard;
+
+ HIBLOG("hibernate_page_list_setall(preflight %d) start\n", preflight);
+
+ if (preflight) {
+ page_list = NULL;
+ page_list_wired = NULL;
+ page_list_pal = NULL;
+ discard_all = FALSE;
+ } else {
+ discard_all = will_discard;
+ }
+
+#if MACH_ASSERT || DEBUG
+ if (!preflight) {
+ assert(hibernate_vm_locks_are_safe());
+ vm_page_lock_queues();
+ if (vm_page_local_q) {
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ struct vpl *lq;
+ lq = &vm_page_local_q[i].vpl_un.vpl;
+ VPL_LOCK(&lq->vpl_lock);
+ }
+ }
+ }
+#endif /* MACH_ASSERT || DEBUG */
+
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 8) | DBG_FUNC_START, count_wire, 0, 0, 0, 0);
+
+ clock_get_uptime(&start);
+
+ if (!preflight) {
+ hibernate_page_list_zero(page_list);
+ hibernate_page_list_zero(page_list_wired);
+ hibernate_page_list_zero(page_list_pal);
+
+ hibernate_stats.cd_vm_page_wire_count = vm_page_wire_count;
+ hibernate_stats.cd_pages = pages;
+ }
+
+ if (vm_page_local_q) {
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ vm_page_reactivate_local(i, TRUE, !preflight);
+ }
+ }
+
+ if (preflight) {
+ vm_object_lock(compressor_object);
+ vm_page_lock_queues();
+ lck_mtx_lock(&vm_page_queue_free_lock);
+ }
+
+ LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_OWNED);
+
+ hibernation_vmqueues_inspection = TRUE;
+
+ m = (vm_page_t) hibernate_gobble_queue;
+ while (m) {
+ pages--;
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ m = m->vmp_snext;
+ }
+
+ if (!preflight) {
+ for (i = 0; i < real_ncpus; i++) {
+ if (cpu_data_ptr[i] && cpu_data_ptr[i]->cpu_processor) {
+ for (m = PROCESSOR_DATA(cpu_data_ptr[i]->cpu_processor, free_pages); m; m = m->vmp_snext) {
+ assert(m->vmp_q_state == VM_PAGE_ON_FREE_LOCAL_Q);
+
+ pages--;
+ count_wire--;
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+
+ hibernate_stats.cd_local_free++;
+ hibernate_stats.cd_total_free++;
+ }
+ }
+ }
+ }
+
+ for (i = 0; i < vm_colors; i++) {
+ vm_page_queue_iterate(&vm_page_queue_free[i].qhead, m, vmp_pageq) {
+ assert(m->vmp_q_state == VM_PAGE_ON_FREE_Q);
+
+ pages--;
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+
+ hibernate_stats.cd_total_free++;
+ }
+ }
+ }
+
+ vm_page_queue_iterate(&vm_lopage_queue_free, m, vmp_pageq) {
+ assert(m->vmp_q_state == VM_PAGE_ON_FREE_LOPAGE_Q);
+
+ pages--;
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+
+ hibernate_stats.cd_total_free++;
+ }
+ }
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_throttled);
+ while (m && !vm_page_queue_end(&vm_page_queue_throttled, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_THROTTLED_Q);
+
+ next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ discard = FALSE;
+ if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode)
+ && hibernate_consider_discard(m, preflight)) {
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ count_discard_inactive++;
+ discard = discard_all;
+ } else {
+ count_throttled++;
+ }
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+
+ if (discard) {
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+
+ m = (vm_page_t)vm_page_queue_first(&vm_page_queue_anonymous);
+ while (m && !vm_page_queue_end(&vm_page_queue_anonymous, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_INTERNAL_Q);
+
+ next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ discard = FALSE;
+ if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) &&
+ hibernate_consider_discard(m, preflight)) {
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_inactive++;
+ }
+ discard = discard_all;
+ } else {
+ count_anonymous++;
+ }
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (discard) {
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_cleaned);
+ while (m && !vm_page_queue_end(&vm_page_queue_cleaned, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q);
+
+ next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ discard = FALSE;
+ if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) &&
+ hibernate_consider_discard(m, preflight)) {
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_cleaned++;
+ }
+ discard = discard_all;
+ } else {
+ count_cleaned++;
+ }
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (discard) {
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_active);
+ while (m && !vm_page_queue_end(&vm_page_queue_active, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_ACTIVE_Q);
+
+ next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ discard = FALSE;
+ if ((kIOHibernateModeDiscardCleanActive & gIOHibernateMode) &&
+ hibernate_consider_discard(m, preflight)) {
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_active++;
+ }
+ discard = discard_all;
+ } else {
+ count_active++;
+ }
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (discard) {
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_inactive);
+ while (m && !vm_page_queue_end(&vm_page_queue_inactive, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_EXTERNAL_Q);
+
+ next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ discard = FALSE;
+ if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) &&
+ hibernate_consider_discard(m, preflight)) {
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_inactive++;
+ }
+ discard = discard_all;
+ } else {
+ count_inactive++;
+ }
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (discard) {
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+ /* XXX FBDP TODO: secluded queue */
+
+ for (i = 0; i <= VM_PAGE_MAX_SPECULATIVE_AGE_Q; i++) {
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_speculative[i].age_q);
+ while (m && !vm_page_queue_end(&vm_page_queue_speculative[i].age_q, (vm_page_queue_entry_t)m)) {
+ assertf(m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q,
+ "Bad page: %p (0x%x:0x%x) on queue %d has state: %d (Discard: %d, Preflight: %d)",
+ m, m->vmp_pageq.next, m->vmp_pageq.prev, i, m->vmp_q_state, discard, preflight);
+
+ next = (vm_page_t)VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ discard = FALSE;
+ if ((kIOHibernateModeDiscardCleanInactive & gIOHibernateMode) &&
+ hibernate_consider_discard(m, preflight)) {
+ if (!preflight) {
+ hibernate_page_bitset(page_list, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ count_discard_speculative++;
+ discard = discard_all;
+ } else {
+ count_speculative++;
+ }
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ if (discard) {
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+ }
+
+ vm_page_queue_iterate(&compressor_object->memq, m, vmp_listq) {
+ assert(m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR);
+
+ count_compressor++;
+ count_wire--;
+ if (!preflight) {
+ hibernate_page_bitset(page_list_wired, TRUE, VM_PAGE_GET_PHYS_PAGE(m));
+ }
+ }
+
+ if (preflight == FALSE && discard_all == TRUE) {
+ KDBG(IOKDBG_CODE(DBG_HIBERNATE, 12) | DBG_FUNC_START);
+
+ HIBLOG("hibernate_teardown started\n");
+ count_discard_vm_struct_pages = hibernate_teardown_vm_structs(page_list, page_list_wired);
+ HIBLOG("hibernate_teardown completed - discarded %d\n", count_discard_vm_struct_pages);
+
+ pages -= count_discard_vm_struct_pages;
+ count_wire -= count_discard_vm_struct_pages;
+
+ hibernate_stats.cd_vm_struct_pages_unneeded = count_discard_vm_struct_pages;
+
+ KDBG(IOKDBG_CODE(DBG_HIBERNATE, 12) | DBG_FUNC_END);
+ }
+
+ if (!preflight) {
+ // pull wired from hibernate_bitmap
+ bitmap = &page_list->bank_bitmap[0];
+ bitmap_wired = &page_list_wired->bank_bitmap[0];
+ for (bank = 0; bank < page_list->bank_count; bank++) {
+ for (i = 0; i < bitmap->bitmapwords; i++) {
+ bitmap->bitmap[i] = bitmap->bitmap[i] | ~bitmap_wired->bitmap[i];
+ }
+ bitmap = (hibernate_bitmap_t *)&bitmap->bitmap[bitmap->bitmapwords];
+ bitmap_wired = (hibernate_bitmap_t *) &bitmap_wired->bitmap[bitmap_wired->bitmapwords];
+ }
+ }
+
+ // machine dependent adjustments
+ hibernate_page_list_setall_machine(page_list, page_list_wired, preflight, &pages);
+
+ if (!preflight) {
+ hibernate_stats.cd_count_wire = count_wire;
+ hibernate_stats.cd_discarded = count_discard_active + count_discard_inactive + count_discard_purgeable +
+ count_discard_speculative + count_discard_cleaned + count_discard_vm_struct_pages;
+ }
+
+ clock_get_uptime(&end);
+ absolutetime_to_nanoseconds(end - start, &nsec);
+ HIBLOG("hibernate_page_list_setall time: %qd ms\n", nsec / 1000000ULL);
+
+ HIBLOG("pages %d, wire %d, act %d, inact %d, cleaned %d spec %d, zf %d, throt %d, compr %d, xpmapped %d\n %s discard act %d inact %d purgeable %d spec %d cleaned %d\n",
+ pages, count_wire, count_active, count_inactive, count_cleaned, count_speculative, count_anonymous, count_throttled, count_compressor, hibernate_stats.cd_found_xpmapped,
+ discard_all ? "did" : "could",
+ count_discard_active, count_discard_inactive, count_discard_purgeable, count_discard_speculative, count_discard_cleaned);
+
+ if (hibernate_stats.cd_skipped_xpmapped) {
+ HIBLOG("WARNING: hibernate_page_list_setall skipped %d xpmapped pages\n", hibernate_stats.cd_skipped_xpmapped);
+ }
+
+ *pagesOut = pages - count_discard_active - count_discard_inactive - count_discard_purgeable - count_discard_speculative - count_discard_cleaned;
+
+ if (preflight && will_discard) {
+ *pagesOut -= count_compressor + count_throttled + count_anonymous + count_inactive + count_cleaned + count_speculative + count_active;
+ }
+
+ hibernation_vmqueues_inspection = FALSE;
+
+#if MACH_ASSERT || DEBUG
+ if (!preflight) {
+ if (vm_page_local_q) {
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ struct vpl *lq;
+ lq = &vm_page_local_q[i].vpl_un.vpl;
+ VPL_UNLOCK(&lq->vpl_lock);
+ }
+ }
+ vm_page_unlock_queues();
+ }
+#endif /* MACH_ASSERT || DEBUG */
+
+ if (preflight) {
+ lck_mtx_unlock(&vm_page_queue_free_lock);
+ vm_page_unlock_queues();
+ vm_object_unlock(compressor_object);
+ }
+
+ KERNEL_DEBUG_CONSTANT(IOKDBG_CODE(DBG_HIBERNATE, 8) | DBG_FUNC_END, count_wire, *pagesOut, 0, 0, 0);
+}
+
+void
+hibernate_page_list_discard(hibernate_page_list_t * page_list)
+{
+ uint64_t start, end, nsec;
+ vm_page_t m;
+ vm_page_t next;
+ uint32_t i;
+ uint32_t count_discard_active = 0;
+ uint32_t count_discard_inactive = 0;
+ uint32_t count_discard_purgeable = 0;
+ uint32_t count_discard_cleaned = 0;
+ uint32_t count_discard_speculative = 0;
+
+
+#if MACH_ASSERT || DEBUG
+ vm_page_lock_queues();
+ if (vm_page_local_q) {
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ struct vpl *lq;
+ lq = &vm_page_local_q[i].vpl_un.vpl;
+ VPL_LOCK(&lq->vpl_lock);
+ }
+ }
+#endif /* MACH_ASSERT || DEBUG */
+
+ clock_get_uptime(&start);
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_anonymous);
+ while (m && !vm_page_queue_end(&vm_page_queue_anonymous, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_INTERNAL_Q);
+
+ next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) {
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_inactive++;
+ }
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+
+ for (i = 0; i <= VM_PAGE_MAX_SPECULATIVE_AGE_Q; i++) {
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_speculative[i].age_q);
+ while (m && !vm_page_queue_end(&vm_page_queue_speculative[i].age_q, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q);
+
+ next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) {
+ count_discard_speculative++;
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+ }
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_inactive);
+ while (m && !vm_page_queue_end(&vm_page_queue_inactive, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_EXTERNAL_Q);
+
+ next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) {
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_inactive++;
+ }
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+ /* XXX FBDP TODO: secluded queue */
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_active);
+ while (m && !vm_page_queue_end(&vm_page_queue_active, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_ACTIVE_Q);
+
+ next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) {
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_active++;
+ }
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+
+ m = (vm_page_t) vm_page_queue_first(&vm_page_queue_cleaned);
+ while (m && !vm_page_queue_end(&vm_page_queue_cleaned, (vm_page_queue_entry_t)m)) {
+ assert(m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q);
+
+ next = (vm_page_t) VM_PAGE_UNPACK_PTR(m->vmp_pageq.next);
+ if (hibernate_page_bittst(page_list, VM_PAGE_GET_PHYS_PAGE(m))) {
+ if (m->vmp_dirty) {
+ count_discard_purgeable++;
+ } else {
+ count_discard_cleaned++;
+ }
+ hibernate_discard_page(m);
+ }
+ m = next;
+ }
+
+#if MACH_ASSERT || DEBUG
+ if (vm_page_local_q) {
+ for (i = 0; i < vm_page_local_q_count; i++) {
+ struct vpl *lq;
+ lq = &vm_page_local_q[i].vpl_un.vpl;
+ VPL_UNLOCK(&lq->vpl_lock);
+ }
+ }
+ vm_page_unlock_queues();
+#endif /* MACH_ASSERT || DEBUG */
+
+ clock_get_uptime(&end);
+ absolutetime_to_nanoseconds(end - start, &nsec);
+ HIBLOG("hibernate_page_list_discard time: %qd ms, discarded act %d inact %d purgeable %d spec %d cleaned %d\n",
+ nsec / 1000000ULL,
+ count_discard_active, count_discard_inactive, count_discard_purgeable, count_discard_speculative, count_discard_cleaned);
+}
+
+boolean_t hibernate_paddr_map_inited = FALSE;
+unsigned int hibernate_teardown_last_valid_compact_indx = -1;
+vm_page_t hibernate_rebuild_hash_list = NULL;
+
+unsigned int hibernate_teardown_found_tabled_pages = 0;
+unsigned int hibernate_teardown_found_created_pages = 0;
+unsigned int hibernate_teardown_found_free_pages = 0;
+unsigned int hibernate_teardown_vm_page_free_count;
+
+
+struct ppnum_mapping {
+ struct ppnum_mapping *ppnm_next;
+ ppnum_t ppnm_base_paddr;
+ unsigned int ppnm_sindx;
+ unsigned int ppnm_eindx;
+};
+
+struct ppnum_mapping *ppnm_head;
+struct ppnum_mapping *ppnm_last_found = NULL;
+
+
+void
+hibernate_create_paddr_map()
+{
+ unsigned int i;
+ ppnum_t next_ppnum_in_run = 0;
+ struct ppnum_mapping *ppnm = NULL;
+
+ if (hibernate_paddr_map_inited == FALSE) {
+ for (i = 0; i < vm_pages_count; i++) {
+ if (ppnm) {
+ ppnm->ppnm_eindx = i;
+ }
+
+ if (ppnm == NULL || VM_PAGE_GET_PHYS_PAGE(&vm_pages[i]) != next_ppnum_in_run) {
+ ppnm = kalloc(sizeof(struct ppnum_mapping));
+
+ ppnm->ppnm_next = ppnm_head;
+ ppnm_head = ppnm;
+
+ ppnm->ppnm_sindx = i;
+ ppnm->ppnm_base_paddr = VM_PAGE_GET_PHYS_PAGE(&vm_pages[i]);
+ }
+ next_ppnum_in_run = VM_PAGE_GET_PHYS_PAGE(&vm_pages[i]) + 1;
+ }
+ ppnm->ppnm_eindx++;
+
+ hibernate_paddr_map_inited = TRUE;
+ }
+}
+
+ppnum_t
+hibernate_lookup_paddr(unsigned int indx)
+{
+ struct ppnum_mapping *ppnm = NULL;
+
+ ppnm = ppnm_last_found;
+
+ if (ppnm) {
+ if (indx >= ppnm->ppnm_sindx && indx < ppnm->ppnm_eindx) {
+ goto done;
+ }
+ }
+ for (ppnm = ppnm_head; ppnm; ppnm = ppnm->ppnm_next) {
+ if (indx >= ppnm->ppnm_sindx && indx < ppnm->ppnm_eindx) {
+ ppnm_last_found = ppnm;
+ break;
+ }
+ }
+ if (ppnm == NULL) {
+ panic("hibernate_lookup_paddr of %d failed\n", indx);
+ }
+done:
+ return ppnm->ppnm_base_paddr + (indx - ppnm->ppnm_sindx);
+}
+
+
+uint32_t
+hibernate_mark_as_unneeded(addr64_t saddr, addr64_t eaddr, hibernate_page_list_t *page_list, hibernate_page_list_t *page_list_wired)
+{
+ addr64_t saddr_aligned;
+ addr64_t eaddr_aligned;
+ addr64_t addr;
+ ppnum_t paddr;
+ unsigned int mark_as_unneeded_pages = 0;
+
+ saddr_aligned = (saddr + PAGE_MASK_64) & ~PAGE_MASK_64;
+ eaddr_aligned = eaddr & ~PAGE_MASK_64;
+
+ for (addr = saddr_aligned; addr < eaddr_aligned; addr += PAGE_SIZE_64) {
+ paddr = pmap_find_phys(kernel_pmap, addr);
+
+ assert(paddr);
+
+ hibernate_page_bitset(page_list, TRUE, paddr);
+ hibernate_page_bitset(page_list_wired, TRUE, paddr);
+
+ mark_as_unneeded_pages++;
+ }
+ return mark_as_unneeded_pages;
+}
+
+
+void
+hibernate_hash_insert_page(vm_page_t mem)
+{
+ vm_page_bucket_t *bucket;
+ int hash_id;
+ vm_object_t m_object;
+
+ m_object = VM_PAGE_OBJECT(mem);
+
+ assert(mem->vmp_hashed);
+ assert(m_object);
+ assert(mem->vmp_offset != (vm_object_offset_t) -1);
+
+ /*
+ * Insert it into the object_object/offset hash table
+ */
+ hash_id = vm_page_hash(m_object, mem->vmp_offset);
+ bucket = &vm_page_buckets[hash_id];
+
+ mem->vmp_next_m = bucket->page_list;
+ bucket->page_list = VM_PAGE_PACK_PTR(mem);
+}
+
+
+void
+hibernate_free_range(int sindx, int eindx)
+{
+ vm_page_t mem;
+ unsigned int color;
+
+ while (sindx < eindx) {
+ mem = &vm_pages[sindx];
+
+ vm_page_init(mem, hibernate_lookup_paddr(sindx), FALSE);
+
+ mem->vmp_lopage = FALSE;
+ mem->vmp_q_state = VM_PAGE_ON_FREE_Q;
+
+ color = VM_PAGE_GET_COLOR(mem);
+#if defined(__x86_64__)
+ vm_page_queue_enter_clump(&vm_page_queue_free[color].qhead, mem);
+#else
+ vm_page_queue_enter(&vm_page_queue_free[color].qhead, mem, vmp_pageq);
+#endif
+ vm_page_free_count++;
+
+ sindx++;
+ }
+}
+
+
+extern void hibernate_rebuild_pmap_structs(void);
+
+void
+hibernate_rebuild_vm_structs(void)
+{
+ int i, cindx, sindx, eindx;
+ vm_page_t mem, tmem, mem_next;
+ AbsoluteTime startTime, endTime;
+ uint64_t nsec;
+
+ if (hibernate_rebuild_needed == FALSE) {
+ return;
+ }
+
+ KDBG(IOKDBG_CODE(DBG_HIBERNATE, 13) | DBG_FUNC_START);
+ HIBLOG("hibernate_rebuild started\n");
+
+ clock_get_uptime(&startTime);
+
+ hibernate_rebuild_pmap_structs();
+
+ bzero(&vm_page_buckets[0], vm_page_bucket_count * sizeof(vm_page_bucket_t));
+ eindx = vm_pages_count;
+
+ /*
+ * Mark all the vm_pages[] that have not been initialized yet as being
+ * transient. This is needed to ensure that buddy page search is corrrect.
+ * Without this random data in these vm_pages[] can trip the buddy search
+ */
+ for (i = hibernate_teardown_last_valid_compact_indx + 1; i < eindx; ++i) {
+ vm_pages[i].vmp_q_state = VM_PAGE_NOT_ON_Q;
+ }
+
+ for (cindx = hibernate_teardown_last_valid_compact_indx; cindx >= 0; cindx--) {
+ mem = &vm_pages[cindx];
+ assert(mem->vmp_q_state != VM_PAGE_ON_FREE_Q);
+ /*
+ * hibernate_teardown_vm_structs leaves the location where
+ * this vm_page_t must be located in "next".
+ */
+ tmem = (vm_page_t)(VM_PAGE_UNPACK_PTR(mem->vmp_next_m));
+ mem->vmp_next_m = VM_PAGE_PACK_PTR(NULL);
+
+ sindx = (int)(tmem - &vm_pages[0]);
+
+ if (mem != tmem) {
+ /*
+ * this vm_page_t was moved by hibernate_teardown_vm_structs,
+ * so move it back to its real location
+ */
+ *tmem = *mem;
+ mem = tmem;
+ }
+ if (mem->vmp_hashed) {
+ hibernate_hash_insert_page(mem);
+ }
+ /*
+ * the 'hole' between this vm_page_t and the previous
+ * vm_page_t we moved needs to be initialized as
+ * a range of free vm_page_t's
+ */
+ hibernate_free_range(sindx + 1, eindx);
+
+ eindx = sindx;
+ }
+ if (sindx) {
+ hibernate_free_range(0, sindx);
+ }
+
+ assert(vm_page_free_count == hibernate_teardown_vm_page_free_count);
+
+ /*
+ * process the list of vm_page_t's that were entered in the hash,
+ * but were not located in the vm_pages arrary... these are
+ * vm_page_t's that were created on the fly (i.e. fictitious)
+ */
+ for (mem = hibernate_rebuild_hash_list; mem; mem = mem_next) {
+ mem_next = (vm_page_t)(VM_PAGE_UNPACK_PTR(mem->vmp_next_m));
+
+ mem->vmp_next_m = 0;
+ hibernate_hash_insert_page(mem);
+ }
+ hibernate_rebuild_hash_list = NULL;
+
+ clock_get_uptime(&endTime);
+ SUB_ABSOLUTETIME(&endTime, &startTime);
+ absolutetime_to_nanoseconds(endTime, &nsec);
+
+ HIBLOG("hibernate_rebuild completed - took %qd msecs\n", nsec / 1000000ULL);
+
+ hibernate_rebuild_needed = FALSE;
+
+ KDBG(IOKDBG_CODE(DBG_HIBERNATE, 13) | DBG_FUNC_END);
+}
+
+
+extern void hibernate_teardown_pmap_structs(addr64_t *, addr64_t *);
+
+uint32_t
+hibernate_teardown_vm_structs(hibernate_page_list_t *page_list, hibernate_page_list_t *page_list_wired)
+{
+ unsigned int i;
+ unsigned int compact_target_indx;
+ vm_page_t mem, mem_next;
+ vm_page_bucket_t *bucket;
+ unsigned int mark_as_unneeded_pages = 0;
+ unsigned int unneeded_vm_page_bucket_pages = 0;
+ unsigned int unneeded_vm_pages_pages = 0;
+ unsigned int unneeded_pmap_pages = 0;
+ addr64_t start_of_unneeded = 0;
+ addr64_t end_of_unneeded = 0;
+
+
+ if (hibernate_should_abort()) {
+ return 0;
+ }
+
+ hibernate_rebuild_needed = TRUE;
+
+ HIBLOG("hibernate_teardown: wired_pages %d, free_pages %d, active_pages %d, inactive_pages %d, speculative_pages %d, cleaned_pages %d, compressor_pages %d\n",
+ vm_page_wire_count, vm_page_free_count, vm_page_active_count, vm_page_inactive_count, vm_page_speculative_count,
+ vm_page_cleaned_count, compressor_object->resident_page_count);
+
+ for (i = 0; i < vm_page_bucket_count; i++) {
+ bucket = &vm_page_buckets[i];
+
+ for (mem = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list)); mem != VM_PAGE_NULL; mem = mem_next) {
+ assert(mem->vmp_hashed);
+
+ mem_next = (vm_page_t)(VM_PAGE_UNPACK_PTR(mem->vmp_next_m));
+
+ if (mem < &vm_pages[0] || mem >= &vm_pages[vm_pages_count]) {
+ mem->vmp_next_m = VM_PAGE_PACK_PTR(hibernate_rebuild_hash_list);
+ hibernate_rebuild_hash_list = mem;
+ }
+ }
+ }
+ unneeded_vm_page_bucket_pages = hibernate_mark_as_unneeded((addr64_t)&vm_page_buckets[0], (addr64_t)&vm_page_buckets[vm_page_bucket_count], page_list, page_list_wired);
+ mark_as_unneeded_pages += unneeded_vm_page_bucket_pages;
+
+ hibernate_teardown_vm_page_free_count = vm_page_free_count;
+
+ compact_target_indx = 0;
+
+ for (i = 0; i < vm_pages_count; i++) {
+ mem = &vm_pages[i];
+
+ if (mem->vmp_q_state == VM_PAGE_ON_FREE_Q) {
+ unsigned int color;
+
+ assert(mem->vmp_busy);
+ assert(!mem->vmp_lopage);
+
+ color = VM_PAGE_GET_COLOR(mem);
+
+ vm_page_queue_remove(&vm_page_queue_free[color].qhead, mem, vmp_pageq);
+
+ VM_PAGE_ZERO_PAGEQ_ENTRY(mem);
+
+ vm_page_free_count--;
+
+ hibernate_teardown_found_free_pages++;
+
+ if (vm_pages[compact_target_indx].vmp_q_state != VM_PAGE_ON_FREE_Q) {
+ compact_target_indx = i;
+ }
+ } else {
+ /*
+ * record this vm_page_t's original location
+ * we need this even if it doesn't get moved
+ * as an indicator to the rebuild function that
+ * we don't have to move it
+ */
+ mem->vmp_next_m = VM_PAGE_PACK_PTR(mem);
+
+ if (vm_pages[compact_target_indx].vmp_q_state == VM_PAGE_ON_FREE_Q) {
+ /*
+ * we've got a hole to fill, so
+ * move this vm_page_t to it's new home
+ */
+ vm_pages[compact_target_indx] = *mem;
+ mem->vmp_q_state = VM_PAGE_ON_FREE_Q;
+
+ hibernate_teardown_last_valid_compact_indx = compact_target_indx;
+ compact_target_indx++;
+ } else {
+ hibernate_teardown_last_valid_compact_indx = i;
+ }
+ }
+ }
+ unneeded_vm_pages_pages = hibernate_mark_as_unneeded((addr64_t)&vm_pages[hibernate_teardown_last_valid_compact_indx + 1],
+ (addr64_t)&vm_pages[vm_pages_count - 1], page_list, page_list_wired);
+ mark_as_unneeded_pages += unneeded_vm_pages_pages;
+
+ hibernate_teardown_pmap_structs(&start_of_unneeded, &end_of_unneeded);
+
+ if (start_of_unneeded) {
+ unneeded_pmap_pages = hibernate_mark_as_unneeded(start_of_unneeded, end_of_unneeded, page_list, page_list_wired);
+ mark_as_unneeded_pages += unneeded_pmap_pages;
+ }
+ HIBLOG("hibernate_teardown: mark_as_unneeded_pages %d, %d, %d\n", unneeded_vm_page_bucket_pages, unneeded_vm_pages_pages, unneeded_pmap_pages);
+
+ return mark_as_unneeded_pages;
+}
+
+
+#endif /* HIBERNATION */
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+#include <mach_vm_debug.h>
+#if MACH_VM_DEBUG
+
+#include <mach_debug/hash_info.h>
+#include <vm/vm_debug.h>
+
+/*
+ * Routine: vm_page_info
+ * Purpose:
+ * Return information about the global VP table.
+ * Fills the buffer with as much information as possible
+ * and returns the desired size of the buffer.
+ * Conditions:
+ * Nothing locked. The caller should provide
+ * possibly-pageable memory.
+ */
+
+unsigned int
+vm_page_info(
+ hash_info_bucket_t *info,
+ unsigned int count)
+{
+ unsigned int i;
+ lck_spin_t *bucket_lock;
+
+ if (vm_page_bucket_count < count) {
+ count = vm_page_bucket_count;
+ }
+
+ for (i = 0; i < count; i++) {
+ vm_page_bucket_t *bucket = &vm_page_buckets[i];
+ unsigned int bucket_count = 0;
+ vm_page_t m;
+
+ bucket_lock = &vm_page_bucket_locks[i / BUCKETS_PER_LOCK];
+ lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket);
+
+ for (m = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list));
+ m != VM_PAGE_NULL;
+ m = (vm_page_t)(VM_PAGE_UNPACK_PTR(m->vmp_next_m))) {
+ bucket_count++;
+ }
+
+ lck_spin_unlock(bucket_lock);
+
+ /* don't touch pageable memory while holding locks */
+ info[i].hib_count = bucket_count;
+ }
+
+ return vm_page_bucket_count;
+}
+#endif /* MACH_VM_DEBUG */
+
+#if VM_PAGE_BUCKETS_CHECK
+void
+vm_page_buckets_check(void)
+{
+ unsigned int i;
+ vm_page_t p;
+ unsigned int p_hash;
+ vm_page_bucket_t *bucket;
+ lck_spin_t *bucket_lock;
+
+ if (!vm_page_buckets_check_ready) {
+ return;
+ }
+
+#if HIBERNATION
+ if (hibernate_rebuild_needed ||
+ hibernate_rebuild_hash_list) {
+ panic("BUCKET_CHECK: hibernation in progress: "
+ "rebuild_needed=%d rebuild_hash_list=%p\n",
+ hibernate_rebuild_needed,
+ hibernate_rebuild_hash_list);
+ }
+#endif /* HIBERNATION */
+
+#if VM_PAGE_FAKE_BUCKETS
+ char *cp;
+ for (cp = (char *) vm_page_fake_buckets_start;
+ cp < (char *) vm_page_fake_buckets_end;
+ cp++) {
+ if (*cp != 0x5a) {
+ panic("BUCKET_CHECK: corruption at %p in fake buckets "
+ "[0x%llx:0x%llx]\n",
+ cp,
+ (uint64_t) vm_page_fake_buckets_start,
+ (uint64_t) vm_page_fake_buckets_end);
+ }
+ }
+#endif /* VM_PAGE_FAKE_BUCKETS */
+
+ for (i = 0; i < vm_page_bucket_count; i++) {
+ vm_object_t p_object;
+
+ bucket = &vm_page_buckets[i];
+ if (!bucket->page_list) {
+ continue;
+ }
+
+ bucket_lock = &vm_page_bucket_locks[i / BUCKETS_PER_LOCK];
+ lck_spin_lock_grp(bucket_lock, &vm_page_lck_grp_bucket);
+ p = (vm_page_t)(VM_PAGE_UNPACK_PTR(bucket->page_list));
+
+ while (p != VM_PAGE_NULL) {
+ p_object = VM_PAGE_OBJECT(p);
+
+ if (!p->vmp_hashed) {
+ panic("BUCKET_CHECK: page %p (%p,0x%llx) "
+ "hash %d in bucket %d at %p "
+ "is not hashed\n",
+ p, p_object, p->vmp_offset,
+ p_hash, i, bucket);
+ }
+ p_hash = vm_page_hash(p_object, p->vmp_offset);
+ if (p_hash != i) {
+ panic("BUCKET_CHECK: corruption in bucket %d "
+ "at %p: page %p object %p offset 0x%llx "
+ "hash %d\n",
+ i, bucket, p, p_object, p->vmp_offset,
+ p_hash);
+ }
+ p = (vm_page_t)(VM_PAGE_UNPACK_PTR(p->vmp_next_m));
+ }
+ lck_spin_unlock(bucket_lock);
+ }
+
+// printf("BUCKET_CHECK: checked buckets\n");
+}
+#endif /* VM_PAGE_BUCKETS_CHECK */
+
+/*
+ * 'vm_fault_enter' will place newly created pages (zero-fill and COW) onto the
+ * local queues if they exist... its the only spot in the system where we add pages
+ * to those queues... once on those queues, those pages can only move to one of the
+ * global page queues or the free queues... they NEVER move from local q to local q.
+ * the 'local' state is stable when vm_page_queues_remove is called since we're behind
+ * the global vm_page_queue_lock at this point... we still need to take the local lock
+ * in case this operation is being run on a different CPU then the local queue's identity,
+ * but we don't have to worry about the page moving to a global queue or becoming wired
+ * while we're grabbing the local lock since those operations would require the global
+ * vm_page_queue_lock to be held, and we already own it.
+ *
+ * this is why its safe to utilze the wire_count field in the vm_page_t as the local_id...
+ * 'wired' and local are ALWAYS mutually exclusive conditions.
+ */
+
+#if CONFIG_BACKGROUND_QUEUE
+void
+vm_page_queues_remove(vm_page_t mem, boolean_t remove_from_backgroundq)
+#else
+void
+vm_page_queues_remove(vm_page_t mem, boolean_t __unused remove_from_backgroundq)
+#endif
+{
+ boolean_t was_pageable = TRUE;
+ vm_object_t m_object;