2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr., Michael Wayne Young
56 * Resident memory management module.
61 #include <mach/clock_types.h>
62 #include <mach/vm_prot.h>
63 #include <mach/vm_statistics.h>
64 #include <kern/counters.h>
65 #include <kern/sched_prim.h>
66 #include <kern/task.h>
67 #include <kern/thread.h>
68 #include <kern/zalloc.h>
71 #include <vm/vm_init.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/vm_kern.h> /* kernel_memory_allocate() */
76 #include <kern/misc_protos.h>
77 #include <zone_debug.h>
79 #include <ppc/mappings.h> /* (BRINGUP) */
80 #include <pexpert/pexpert.h> /* (BRINGUP) */
82 #include <vm/vm_protos.h>
84 /* Variables used to indicate the relative age of pages in the
88 unsigned int vm_page_ticket_roll
= 0;
89 unsigned int vm_page_ticket
= 0;
91 * Associated with page of user-allocatable memory is a
96 * These variables record the values returned by vm_page_bootstrap,
97 * for debugging purposes. The implementation of pmap_steal_memory
98 * and pmap_startup here also uses them internally.
101 vm_offset_t virtual_space_start
;
102 vm_offset_t virtual_space_end
;
106 * The vm_page_lookup() routine, which provides for fast
107 * (virtual memory object, offset) to page lookup, employs
108 * the following hash table. The vm_page_{insert,remove}
109 * routines install and remove associations in the table.
110 * [This table is often called the virtual-to-physical,
115 #if MACH_PAGE_HASH_STATS
116 int cur_count
; /* current count */
117 int hi_count
; /* high water mark */
118 #endif /* MACH_PAGE_HASH_STATS */
121 vm_page_bucket_t
*vm_page_buckets
; /* Array of buckets */
122 unsigned int vm_page_bucket_count
= 0; /* How big is array? */
123 unsigned int vm_page_hash_mask
; /* Mask for hash function */
124 unsigned int vm_page_hash_shift
; /* Shift for hash function */
125 uint32_t vm_page_bucket_hash
; /* Basic bucket hash */
126 decl_simple_lock_data(,vm_page_bucket_lock
)
129 vm_page_lookup_nohint(vm_object_t object
, vm_object_offset_t offset
);
132 #if MACH_PAGE_HASH_STATS
133 /* This routine is only for debug. It is intended to be called by
134 * hand by a developer using a kernel debugger. This routine prints
135 * out vm_page_hash table statistics to the kernel debug console.
145 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
146 if (vm_page_buckets
[i
].hi_count
) {
148 highsum
+= vm_page_buckets
[i
].hi_count
;
149 if (vm_page_buckets
[i
].hi_count
> maxdepth
)
150 maxdepth
= vm_page_buckets
[i
].hi_count
;
153 printf("Total number of buckets: %d\n", vm_page_bucket_count
);
154 printf("Number used buckets: %d = %d%%\n",
155 numbuckets
, 100*numbuckets
/vm_page_bucket_count
);
156 printf("Number unused buckets: %d = %d%%\n",
157 vm_page_bucket_count
- numbuckets
,
158 100*(vm_page_bucket_count
-numbuckets
)/vm_page_bucket_count
);
159 printf("Sum of bucket max depth: %d\n", highsum
);
160 printf("Average bucket depth: %d.%2d\n",
161 highsum
/vm_page_bucket_count
,
162 highsum%vm_page_bucket_count
);
163 printf("Maximum bucket depth: %d\n", maxdepth
);
165 #endif /* MACH_PAGE_HASH_STATS */
168 * The virtual page size is currently implemented as a runtime
169 * variable, but is constant once initialized using vm_set_page_size.
170 * This initialization must be done in the machine-dependent
171 * bootstrap sequence, before calling other machine-independent
174 * All references to the virtual page size outside this
175 * module must use the PAGE_SIZE, PAGE_MASK and PAGE_SHIFT
178 vm_size_t page_size
= PAGE_SIZE
;
179 vm_size_t page_mask
= PAGE_MASK
;
180 int page_shift
= PAGE_SHIFT
;
183 * Resident page structures are initialized from
184 * a template (see vm_page_alloc).
186 * When adding a new field to the virtual memory
187 * object structure, be sure to add initialization
188 * (see vm_page_bootstrap).
190 struct vm_page vm_page_template
;
193 * Resident pages that represent real memory
194 * are allocated from a free list.
196 vm_page_t vm_page_queue_free
;
197 vm_page_t vm_page_queue_fictitious
;
198 unsigned int vm_page_free_wanted
;
199 unsigned int vm_page_free_count
;
200 unsigned int vm_page_fictitious_count
;
202 unsigned int vm_page_free_count_minimum
; /* debugging */
205 * Occasionally, the virtual memory system uses
206 * resident page structures that do not refer to
207 * real pages, for example to leave a page with
208 * important state information in the VP table.
210 * These page structures are allocated the way
211 * most other kernel structures are.
214 decl_mutex_data(,vm_page_alloc_lock
)
215 unsigned int io_throttle_zero_fill
;
218 * Fictitious pages don't have a physical address,
219 * but we must initialize phys_page to something.
220 * For debugging, this should be a strange value
221 * that the pmap module can recognize in assertions.
223 vm_offset_t vm_page_fictitious_addr
= (vm_offset_t
) -1;
226 * Resident page structures are also chained on
227 * queues that are used by the page replacement
228 * system (pageout daemon). These queues are
229 * defined here, but are shared by the pageout
230 * module. The inactive queue is broken into
231 * inactive and zf for convenience as the
232 * pageout daemon often assignes a higher
233 * affinity to zf pages
235 queue_head_t vm_page_queue_active
;
236 queue_head_t vm_page_queue_inactive
;
237 unsigned int vm_page_active_count
;
238 unsigned int vm_page_inactive_count
;
239 unsigned int vm_page_wire_count
;
240 unsigned int vm_page_gobble_count
= 0;
241 unsigned int vm_page_wire_count_warning
= 0;
242 unsigned int vm_page_gobble_count_warning
= 0;
244 unsigned int vm_page_purgeable_count
= 0; /* # of pages purgeable now */
245 uint64_t vm_page_purged_count
= 0; /* total count of purged pages */
248 * Several page replacement parameters are also
249 * shared with this module, so that page allocation
250 * (done here in vm_page_alloc) can trigger the
253 unsigned int vm_page_free_target
= 0;
254 unsigned int vm_page_free_min
= 0;
255 unsigned int vm_page_inactive_target
= 0;
256 unsigned int vm_page_free_reserved
= 0;
257 unsigned int vm_page_throttled_count
= 0;
260 * The VM system has a couple of heuristics for deciding
261 * that pages are "uninteresting" and should be placed
262 * on the inactive queue as likely candidates for replacement.
263 * These variables let the heuristics be controlled at run-time
264 * to make experimentation easier.
267 boolean_t vm_page_deactivate_hint
= TRUE
;
272 * Sets the page size, perhaps based upon the memory
273 * size. Must be called before any use of page-size
274 * dependent functions.
276 * Sets page_shift and page_mask from page_size.
279 vm_set_page_size(void)
281 page_mask
= page_size
- 1;
283 if ((page_mask
& page_size
) != 0)
284 panic("vm_set_page_size: page size not a power of two");
286 for (page_shift
= 0; ; page_shift
++)
287 if ((1U << page_shift
) == page_size
)
294 * Initializes the resident memory module.
296 * Allocates memory for the page cells, and
297 * for the object/offset-to-page hash table headers.
298 * Each page cell is initialized and placed on the free list.
299 * Returns the range of available kernel virtual memory.
307 register vm_page_t m
;
314 * Initialize the vm_page template.
317 m
= &vm_page_template
;
318 m
->object
= VM_OBJECT_NULL
; /* reset later */
319 m
->offset
= (vm_object_offset_t
) -1; /* reset later */
322 m
->pageq
.next
= NULL
;
323 m
->pageq
.prev
= NULL
;
324 m
->listq
.next
= NULL
;
325 m
->listq
.prev
= NULL
;
332 m
->reference
= FALSE
;
334 m
->dump_cleaning
= FALSE
;
335 m
->list_req_pending
= FALSE
;
340 m
->fictitious
= FALSE
;
347 m
->clustered
= FALSE
;
348 m
->lock_supplied
= FALSE
;
351 m
->zero_fill
= FALSE
;
352 m
->encrypted
= FALSE
;
354 m
->phys_page
= 0; /* reset later */
356 m
->page_lock
= VM_PROT_NONE
;
357 m
->unlock_request
= VM_PROT_NONE
;
358 m
->page_error
= KERN_SUCCESS
;
361 * Initialize the page queues.
364 mutex_init(&vm_page_queue_free_lock
, 0);
365 mutex_init(&vm_page_queue_lock
, 0);
367 vm_page_queue_free
= VM_PAGE_NULL
;
368 vm_page_queue_fictitious
= VM_PAGE_NULL
;
369 queue_init(&vm_page_queue_active
);
370 queue_init(&vm_page_queue_inactive
);
371 queue_init(&vm_page_queue_zf
);
373 vm_page_free_wanted
= 0;
376 * Steal memory for the map and zone subsystems.
379 vm_map_steal_memory();
383 * Allocate (and initialize) the virtual-to-physical
384 * table hash buckets.
386 * The number of buckets should be a power of two to
387 * get a good hash function. The following computation
388 * chooses the first power of two that is greater
389 * than the number of physical pages in the system.
392 simple_lock_init(&vm_page_bucket_lock
, 0);
394 if (vm_page_bucket_count
== 0) {
395 unsigned int npages
= pmap_free_pages();
397 vm_page_bucket_count
= 1;
398 while (vm_page_bucket_count
< npages
)
399 vm_page_bucket_count
<<= 1;
402 vm_page_hash_mask
= vm_page_bucket_count
- 1;
405 * Calculate object shift value for hashing algorithm:
406 * O = log2(sizeof(struct vm_object))
407 * B = log2(vm_page_bucket_count)
408 * hash shifts the object left by
411 size
= vm_page_bucket_count
;
412 for (log1
= 0; size
> 1; log1
++)
414 size
= sizeof(struct vm_object
);
415 for (log2
= 0; size
> 1; log2
++)
417 vm_page_hash_shift
= log1
/2 - log2
+ 1;
419 vm_page_bucket_hash
= 1 << ((log1
+ 1) >> 1); /* Get (ceiling of sqrt of table size) */
420 vm_page_bucket_hash
|= 1 << ((log1
+ 1) >> 2); /* Get (ceiling of quadroot of table size) */
421 vm_page_bucket_hash
|= 1; /* Set bit and add 1 - always must be 1 to insure unique series */
423 if (vm_page_hash_mask
& vm_page_bucket_count
)
424 printf("vm_page_bootstrap: WARNING -- strange page hash\n");
426 vm_page_buckets
= (vm_page_bucket_t
*)
427 pmap_steal_memory(vm_page_bucket_count
*
428 sizeof(vm_page_bucket_t
));
430 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
431 register vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
433 bucket
->pages
= VM_PAGE_NULL
;
434 #if MACH_PAGE_HASH_STATS
435 bucket
->cur_count
= 0;
436 bucket
->hi_count
= 0;
437 #endif /* MACH_PAGE_HASH_STATS */
441 * Machine-dependent code allocates the resident page table.
442 * It uses vm_page_init to initialize the page frames.
443 * The code also returns to us the virtual space available
444 * to the kernel. We don't trust the pmap module
445 * to get the alignment right.
448 pmap_startup(&virtual_space_start
, &virtual_space_end
);
449 virtual_space_start
= round_page(virtual_space_start
);
450 virtual_space_end
= trunc_page(virtual_space_end
);
452 *startp
= virtual_space_start
;
453 *endp
= virtual_space_end
;
456 * Compute the initial "wire" count.
457 * Up until now, the pages which have been set aside are not under
458 * the VM system's control, so although they aren't explicitly
459 * wired, they nonetheless can't be moved. At this moment,
460 * all VM managed pages are "free", courtesy of pmap_startup.
462 vm_page_wire_count
= atop_64(max_mem
) - vm_page_free_count
; /* initial value */
464 printf("vm_page_bootstrap: %d free pages\n", vm_page_free_count
);
465 vm_page_free_count_minimum
= vm_page_free_count
;
467 simple_lock_init(&vm_paging_lock
, 0);
470 #ifndef MACHINE_PAGES
472 * We implement pmap_steal_memory and pmap_startup with the help
473 * of two simpler functions, pmap_virtual_space and pmap_next_page.
480 vm_offset_t addr
, vaddr
;
484 * We round the size to a round multiple.
487 size
= (size
+ sizeof (void *) - 1) &~ (sizeof (void *) - 1);
490 * If this is the first call to pmap_steal_memory,
491 * we have to initialize ourself.
494 if (virtual_space_start
== virtual_space_end
) {
495 pmap_virtual_space(&virtual_space_start
, &virtual_space_end
);
498 * The initial values must be aligned properly, and
499 * we don't trust the pmap module to do it right.
502 virtual_space_start
= round_page(virtual_space_start
);
503 virtual_space_end
= trunc_page(virtual_space_end
);
507 * Allocate virtual memory for this request.
510 addr
= virtual_space_start
;
511 virtual_space_start
+= size
;
513 kprintf("pmap_steal_memory: %08X - %08X; size=%08X\n", addr
, virtual_space_start
, size
); /* (TEST/DEBUG) */
516 * Allocate and map physical pages to back new virtual pages.
519 for (vaddr
= round_page(addr
);
521 vaddr
+= PAGE_SIZE
) {
522 if (!pmap_next_page(&phys_page
))
523 panic("pmap_steal_memory");
526 * XXX Logically, these mappings should be wired,
527 * but some pmap modules barf if they are.
530 pmap_enter(kernel_pmap
, vaddr
, phys_page
,
531 VM_PROT_READ
|VM_PROT_WRITE
,
532 VM_WIMG_USE_DEFAULT
, FALSE
);
534 * Account for newly stolen memory
536 vm_page_wire_count
++;
540 return (void *) addr
;
548 unsigned int i
, npages
, pages_initialized
, fill
, fillval
;
554 * We calculate how many page frames we will have
555 * and then allocate the page structures in one chunk.
558 tmpaddr
= (addr64_t
)pmap_free_pages() * (addr64_t
)PAGE_SIZE
; /* Get the amount of memory left */
559 tmpaddr
= tmpaddr
+ (addr64_t
)(round_page_32(virtual_space_start
) - virtual_space_start
); /* Account for any slop */
560 npages
= (unsigned int)(tmpaddr
/ (addr64_t
)(PAGE_SIZE
+ sizeof(*pages
))); /* Figure size of all vm_page_ts, including enough to hold the vm_page_ts */
562 pages
= (vm_page_t
) pmap_steal_memory(npages
* sizeof *pages
);
565 * Initialize the page frames.
568 for (i
= 0, pages_initialized
= 0; i
< npages
; i
++) {
569 if (!pmap_next_page(&phys_page
))
572 vm_page_init(&pages
[i
], phys_page
);
578 * Release pages in reverse order so that physical pages
579 * initially get allocated in ascending addresses. This keeps
580 * the devices (which must address physical memory) happy if
581 * they require several consecutive pages.
585 * Check if we want to initialize pages to a known value
588 fill
= 0; /* Assume no fill */
589 if (PE_parse_boot_arg("fill", &fillval
)) fill
= 1; /* Set fill */
591 for (i
= pages_initialized
; i
> 0; i
--) {
592 if(fill
) fillPage(pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
593 vm_page_release(&pages
[i
- 1]);
598 vm_page_t xx
, xxo
, xxl
;
601 j
= 0; /* (BRINGUP) */
604 for(xx
= vm_page_queue_free
; xx
; xxl
= xx
, xx
= xx
->pageq
.next
) { /* (BRINGUP) */
606 if(j
> vm_page_free_count
) { /* (BRINGUP) */
607 panic("pmap_startup: too many pages, xx = %08X, xxl = %08X\n", xx
, xxl
);
610 l
= vm_page_free_count
- j
; /* (BRINGUP) */
611 k
= 0; /* (BRINGUP) */
613 if(((j
- 1) & 0xFFFF) == 0) kprintf("checking number %d of %d\n", j
, vm_page_free_count
);
615 for(xxo
= xx
->pageq
.next
; xxo
; xxo
= xxo
->pageq
.next
) { /* (BRINGUP) */
617 if(k
> l
) panic("pmap_startup: too many in secondary check %d %d\n", k
, l
);
618 if((xx
->phys_page
& 0xFFFFFFFF) == (xxo
->phys_page
& 0xFFFFFFFF)) { /* (BRINGUP) */
619 panic("pmap_startup: duplicate physaddr, xx = %08X, xxo = %08X\n", xx
, xxo
);
624 if(j
!= vm_page_free_count
) { /* (BRINGUP) */
625 panic("pmap_startup: vm_page_free_count does not match, calc = %d, vm_page_free_count = %08X\n", j
, vm_page_free_count
);
632 * We have to re-align virtual_space_start,
633 * because pmap_steal_memory has been using it.
636 virtual_space_start
= round_page_32(virtual_space_start
);
638 *startp
= virtual_space_start
;
639 *endp
= virtual_space_end
;
641 #endif /* MACHINE_PAGES */
644 * Routine: vm_page_module_init
646 * Second initialization pass, to be done after
647 * the basic VM system is ready.
650 vm_page_module_init(void)
652 vm_page_zone
= zinit((vm_size_t
) sizeof(struct vm_page
),
653 0, PAGE_SIZE
, "vm pages");
656 zone_debug_disable(vm_page_zone
);
657 #endif /* ZONE_DEBUG */
659 zone_change(vm_page_zone
, Z_EXPAND
, FALSE
);
660 zone_change(vm_page_zone
, Z_EXHAUST
, TRUE
);
661 zone_change(vm_page_zone
, Z_FOREIGN
, TRUE
);
664 * Adjust zone statistics to account for the real pages allocated
665 * in vm_page_create(). [Q: is this really what we want?]
667 vm_page_zone
->count
+= vm_page_pages
;
668 vm_page_zone
->cur_size
+= vm_page_pages
* vm_page_zone
->elem_size
;
670 mutex_init(&vm_page_alloc_lock
, 0);
674 * Routine: vm_page_create
676 * After the VM system is up, machine-dependent code
677 * may stumble across more physical memory. For example,
678 * memory that it was reserving for a frame buffer.
679 * vm_page_create turns this memory into available pages.
690 for (phys_page
= start
;
693 while ((m
= (vm_page_t
) vm_page_grab_fictitious())
695 vm_page_more_fictitious();
697 vm_page_init(m
, phys_page
);
706 * Distributes the object/offset key pair among hash buckets.
708 * NOTE: The bucket count must be a power of 2
710 #define vm_page_hash(object, offset) (\
711 ( (natural_t)((uint32_t)object * vm_page_bucket_hash) + ((uint32_t)atop_64(offset) ^ vm_page_bucket_hash))\
715 * vm_page_insert: [ internal use only ]
717 * Inserts the given mem entry into the object/object-page
718 * table and object list.
720 * The object must be locked.
725 register vm_page_t mem
,
726 register vm_object_t object
,
727 register vm_object_offset_t offset
)
729 register vm_page_bucket_t
*bucket
;
732 "vm_page_insert, object 0x%X offset 0x%X page 0x%X\n",
733 (integer_t
)object
, (integer_t
)offset
, (integer_t
)mem
, 0,0);
737 _mutex_assert(&object
->Lock
, MA_OWNED
);
739 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
740 panic("vm_page_insert: page %p for (obj=%p,off=0x%llx) "
741 "already in (obj=%p,off=0x%llx)",
742 mem
, object
, offset
, mem
->object
, mem
->offset
);
744 assert(!object
->internal
|| offset
< object
->size
);
746 /* only insert "pageout" pages into "pageout" objects,
747 * and normal pages into normal objects */
748 assert(object
->pageout
== mem
->pageout
);
750 assert(vm_page_lookup(object
, offset
) == VM_PAGE_NULL
);
753 * Record the object/offset pair in this page
756 mem
->object
= object
;
757 mem
->offset
= offset
;
760 * Insert it into the object_object/offset hash table
763 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
764 simple_lock(&vm_page_bucket_lock
);
765 mem
->next
= bucket
->pages
;
767 #if MACH_PAGE_HASH_STATS
768 if (++bucket
->cur_count
> bucket
->hi_count
)
769 bucket
->hi_count
= bucket
->cur_count
;
770 #endif /* MACH_PAGE_HASH_STATS */
771 simple_unlock(&vm_page_bucket_lock
);
774 * Now link into the object's list of backed pages.
777 VM_PAGE_INSERT(mem
, object
);
781 * Show that the object has one more resident page.
784 object
->resident_page_count
++;
786 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
787 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
788 vm_page_lock_queues();
789 vm_page_purgeable_count
++;
790 vm_page_unlock_queues();
797 * Exactly like vm_page_insert, except that we first
798 * remove any existing page at the given offset in object.
800 * The object and page queues must be locked.
805 register vm_page_t mem
,
806 register vm_object_t object
,
807 register vm_object_offset_t offset
)
809 register vm_page_bucket_t
*bucket
;
813 _mutex_assert(&object
->Lock
, MA_OWNED
);
814 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
816 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
817 panic("vm_page_replace: page %p for (obj=%p,off=0x%llx) "
818 "already in (obj=%p,off=0x%llx)",
819 mem
, object
, offset
, mem
->object
, mem
->offset
);
822 * Record the object/offset pair in this page
825 mem
->object
= object
;
826 mem
->offset
= offset
;
829 * Insert it into the object_object/offset hash table,
830 * replacing any page that might have been there.
833 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
834 simple_lock(&vm_page_bucket_lock
);
836 vm_page_t
*mp
= &bucket
->pages
;
837 register vm_page_t m
= *mp
;
839 if (m
->object
== object
&& m
->offset
== offset
) {
841 * Remove page from bucket and from object,
842 * and return it to the free list.
847 m
->object
= VM_OBJECT_NULL
;
848 m
->offset
= (vm_object_offset_t
) -1;
849 object
->resident_page_count
--;
851 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
852 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
853 assert(vm_page_purgeable_count
> 0);
854 vm_page_purgeable_count
--;
858 * Return page to the free list.
859 * Note the page is not tabled now, so this
860 * won't self-deadlock on the bucket lock.
868 mem
->next
= bucket
->pages
;
870 mem
->next
= VM_PAGE_NULL
;
873 simple_unlock(&vm_page_bucket_lock
);
876 * Now link into the object's list of backed pages.
879 VM_PAGE_INSERT(mem
, object
);
883 * And show that the object has one more resident
887 object
->resident_page_count
++;
889 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
890 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
891 vm_page_purgeable_count
++;
896 * vm_page_remove: [ internal use only ]
898 * Removes the given mem entry from the object/offset-page
899 * table and the object page list.
901 * The object and page queues must be locked.
906 register vm_page_t mem
)
908 register vm_page_bucket_t
*bucket
;
909 register vm_page_t
this;
912 "vm_page_remove, object 0x%X offset 0x%X page 0x%X\n",
913 (integer_t
)mem
->object
, (integer_t
)mem
->offset
,
914 (integer_t
)mem
, 0,0);
916 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
917 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
920 assert(!mem
->cleaning
);
925 * Remove from the object_object/offset hash table
928 bucket
= &vm_page_buckets
[vm_page_hash(mem
->object
, mem
->offset
)];
929 simple_lock(&vm_page_bucket_lock
);
930 if ((this = bucket
->pages
) == mem
) {
931 /* optimize for common case */
933 bucket
->pages
= mem
->next
;
935 register vm_page_t
*prev
;
937 for (prev
= &this->next
;
938 (this = *prev
) != mem
;
943 #if MACH_PAGE_HASH_STATS
945 #endif /* MACH_PAGE_HASH_STATS */
946 simple_unlock(&vm_page_bucket_lock
);
949 * Now remove from the object's list of backed pages.
955 * And show that the object has one fewer resident
959 mem
->object
->resident_page_count
--;
961 if (mem
->object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
962 mem
->object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
963 assert(vm_page_purgeable_count
> 0);
964 vm_page_purgeable_count
--;
968 mem
->object
= VM_OBJECT_NULL
;
969 mem
->offset
= (vm_object_offset_t
) -1;
975 * Returns the page associated with the object/offset
976 * pair specified; if none is found, VM_PAGE_NULL is returned.
978 * The object must be locked. No side effects.
981 unsigned long vm_page_lookup_hint
= 0;
982 unsigned long vm_page_lookup_hint_next
= 0;
983 unsigned long vm_page_lookup_hint_prev
= 0;
984 unsigned long vm_page_lookup_hint_miss
= 0;
988 register vm_object_t object
,
989 register vm_object_offset_t offset
)
991 register vm_page_t mem
;
992 register vm_page_bucket_t
*bucket
;
995 _mutex_assert(&object
->Lock
, MA_OWNED
);
998 mem
= object
->memq_hint
;
999 if (mem
!= VM_PAGE_NULL
) {
1000 assert(mem
->object
== object
);
1001 if (mem
->offset
== offset
) {
1002 vm_page_lookup_hint
++;
1005 qe
= queue_next(&mem
->listq
);
1006 if (! queue_end(&object
->memq
, qe
)) {
1007 vm_page_t next_page
;
1009 next_page
= (vm_page_t
) qe
;
1010 assert(next_page
->object
== object
);
1011 if (next_page
->offset
== offset
) {
1012 vm_page_lookup_hint_next
++;
1013 object
->memq_hint
= next_page
; /* new hint */
1017 qe
= queue_prev(&mem
->listq
);
1018 if (! queue_end(&object
->memq
, qe
)) {
1019 vm_page_t prev_page
;
1021 prev_page
= (vm_page_t
) qe
;
1022 assert(prev_page
->object
== object
);
1023 if (prev_page
->offset
== offset
) {
1024 vm_page_lookup_hint_prev
++;
1025 object
->memq_hint
= prev_page
; /* new hint */
1032 * Search the hash table for this object/offset pair
1035 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1037 simple_lock(&vm_page_bucket_lock
);
1038 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
1040 if ((mem
->object
== object
) && (mem
->offset
== offset
))
1043 simple_unlock(&vm_page_bucket_lock
);
1045 if (mem
!= VM_PAGE_NULL
) {
1046 if (object
->memq_hint
!= VM_PAGE_NULL
) {
1047 vm_page_lookup_hint_miss
++;
1049 assert(mem
->object
== object
);
1050 object
->memq_hint
= mem
;
1058 vm_page_lookup_nohint(
1060 vm_object_offset_t offset
)
1062 register vm_page_t mem
;
1063 register vm_page_bucket_t
*bucket
;
1066 _mutex_assert(&object
->Lock
, MA_OWNED
);
1069 * Search the hash table for this object/offset pair
1072 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1074 simple_lock(&vm_page_bucket_lock
);
1075 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
1077 if ((mem
->object
== object
) && (mem
->offset
== offset
))
1080 simple_unlock(&vm_page_bucket_lock
);
1088 * Move the given memory entry from its
1089 * current object to the specified target object/offset.
1091 * The object must be locked.
1095 register vm_page_t mem
,
1096 register vm_object_t new_object
,
1097 vm_object_offset_t new_offset
)
1099 assert(mem
->object
!= new_object
);
1102 * The encryption key is based on the page's memory object
1103 * (aka "pager") and paging offset. Moving the page to
1104 * another VM object changes its "pager" and "paging_offset"
1105 * so it has to be decrypted first.
1107 if (mem
->encrypted
) {
1108 panic("vm_page_rename: page %p is encrypted\n", mem
);
1111 * Changes to mem->object require the page lock because
1112 * the pageout daemon uses that lock to get the object.
1116 "vm_page_rename, new object 0x%X, offset 0x%X page 0x%X\n",
1117 (integer_t
)new_object
, (integer_t
)new_offset
,
1118 (integer_t
)mem
, 0,0);
1120 vm_page_lock_queues();
1121 vm_page_remove(mem
);
1122 vm_page_insert(mem
, new_object
, new_offset
);
1123 vm_page_unlock_queues();
1129 * Initialize the fields in a new page.
1130 * This takes a structure with random values and initializes it
1131 * so that it can be given to vm_page_release or vm_page_insert.
1139 *mem
= vm_page_template
;
1140 mem
->phys_page
= phys_page
;
1144 * vm_page_grab_fictitious:
1146 * Remove a fictitious page from the free list.
1147 * Returns VM_PAGE_NULL if there are no free pages.
1149 int c_vm_page_grab_fictitious
= 0;
1150 int c_vm_page_release_fictitious
= 0;
1151 int c_vm_page_more_fictitious
= 0;
1154 vm_page_grab_fictitious(void)
1156 register vm_page_t m
;
1158 m
= (vm_page_t
)zget(vm_page_zone
);
1160 vm_page_init(m
, vm_page_fictitious_addr
);
1161 m
->fictitious
= TRUE
;
1164 c_vm_page_grab_fictitious
++;
1169 * vm_page_release_fictitious:
1171 * Release a fictitious page to the free list.
1175 vm_page_release_fictitious(
1176 register vm_page_t m
)
1180 assert(m
->fictitious
);
1181 assert(m
->phys_page
== vm_page_fictitious_addr
);
1183 c_vm_page_release_fictitious
++;
1186 panic("vm_page_release_fictitious");
1189 zfree(vm_page_zone
, m
);
1193 * vm_page_more_fictitious:
1195 * Add more fictitious pages to the free list.
1196 * Allowed to block. This routine is way intimate
1197 * with the zones code, for several reasons:
1198 * 1. we need to carve some page structures out of physical
1199 * memory before zones work, so they _cannot_ come from
1201 * 2. the zone needs to be collectable in order to prevent
1202 * growth without bound. These structures are used by
1203 * the device pager (by the hundreds and thousands), as
1204 * private pages for pageout, and as blocking pages for
1205 * pagein. Temporary bursts in demand should not result in
1206 * permanent allocation of a resource.
1207 * 3. To smooth allocation humps, we allocate single pages
1208 * with kernel_memory_allocate(), and cram them into the
1209 * zone. This also allows us to initialize the vm_page_t's
1210 * on the way into the zone, so that zget() always returns
1211 * an initialized structure. The zone free element pointer
1212 * and the free page pointer are both the first item in the
1214 * 4. By having the pages in the zone pre-initialized, we need
1215 * not keep 2 levels of lists. The garbage collector simply
1216 * scans our list, and reduces physical memory usage as it
1220 void vm_page_more_fictitious(void)
1222 register vm_page_t m
;
1224 kern_return_t retval
;
1227 c_vm_page_more_fictitious
++;
1230 * Allocate a single page from the zone_map. Do not wait if no physical
1231 * pages are immediately available, and do not zero the space. We need
1232 * our own blocking lock here to prevent having multiple,
1233 * simultaneous requests from piling up on the zone_map lock. Exactly
1234 * one (of our) threads should be potentially waiting on the map lock.
1235 * If winner is not vm-privileged, then the page allocation will fail,
1236 * and it will temporarily block here in the vm_page_wait().
1238 mutex_lock(&vm_page_alloc_lock
);
1240 * If another thread allocated space, just bail out now.
1242 if (zone_free_count(vm_page_zone
) > 5) {
1244 * The number "5" is a small number that is larger than the
1245 * number of fictitious pages that any single caller will
1246 * attempt to allocate. Otherwise, a thread will attempt to
1247 * acquire a fictitious page (vm_page_grab_fictitious), fail,
1248 * release all of the resources and locks already acquired,
1249 * and then call this routine. This routine finds the pages
1250 * that the caller released, so fails to allocate new space.
1251 * The process repeats infinitely. The largest known number
1252 * of fictitious pages required in this manner is 2. 5 is
1253 * simply a somewhat larger number.
1255 mutex_unlock(&vm_page_alloc_lock
);
1259 retval
= kernel_memory_allocate(zone_map
,
1260 &addr
, PAGE_SIZE
, VM_PROT_ALL
,
1261 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
1262 if (retval
!= KERN_SUCCESS
) {
1264 * No page was available. Tell the pageout daemon, drop the
1265 * lock to give another thread a chance at it, and
1266 * wait for the pageout daemon to make progress.
1268 mutex_unlock(&vm_page_alloc_lock
);
1269 vm_page_wait(THREAD_UNINT
);
1273 * Initialize as many vm_page_t's as will fit on this page. This
1274 * depends on the zone code disturbing ONLY the first item of
1275 * each zone element.
1277 m
= (vm_page_t
)addr
;
1278 for (i
= PAGE_SIZE
/sizeof(struct vm_page
); i
> 0; i
--) {
1279 vm_page_init(m
, vm_page_fictitious_addr
);
1280 m
->fictitious
= TRUE
;
1283 zcram(vm_page_zone
, (void *) addr
, PAGE_SIZE
);
1284 mutex_unlock(&vm_page_alloc_lock
);
1290 * Attempt to convert a fictitious page into a real page.
1295 register vm_page_t m
)
1297 register vm_page_t real_m
;
1300 assert(m
->fictitious
);
1303 real_m
= vm_page_grab();
1304 if (real_m
== VM_PAGE_NULL
)
1307 m
->phys_page
= real_m
->phys_page
;
1308 m
->fictitious
= FALSE
;
1311 vm_page_lock_queues();
1313 vm_page_active_count
++;
1314 else if (m
->inactive
)
1315 vm_page_inactive_count
++;
1316 vm_page_unlock_queues();
1318 real_m
->phys_page
= vm_page_fictitious_addr
;
1319 real_m
->fictitious
= TRUE
;
1321 vm_page_release_fictitious(real_m
);
1328 * Return true if it is not likely that a non-vm_privileged thread
1329 * can get memory without blocking. Advisory only, since the
1330 * situation may change under us.
1335 /* No locking, at worst we will fib. */
1336 return( vm_page_free_count
< vm_page_free_reserved
);
1342 * Remove a page from the free list.
1343 * Returns VM_PAGE_NULL if the free list is too small.
1346 unsigned long vm_page_grab_count
= 0; /* measure demand */
1351 register vm_page_t mem
;
1353 mutex_lock(&vm_page_queue_free_lock
);
1354 vm_page_grab_count
++;
1357 * Optionally produce warnings if the wire or gobble
1358 * counts exceed some threshold.
1360 if (vm_page_wire_count_warning
> 0
1361 && vm_page_wire_count
>= vm_page_wire_count_warning
) {
1362 printf("mk: vm_page_grab(): high wired page count of %d\n",
1363 vm_page_wire_count
);
1364 assert(vm_page_wire_count
< vm_page_wire_count_warning
);
1366 if (vm_page_gobble_count_warning
> 0
1367 && vm_page_gobble_count
>= vm_page_gobble_count_warning
) {
1368 printf("mk: vm_page_grab(): high gobbled page count of %d\n",
1369 vm_page_gobble_count
);
1370 assert(vm_page_gobble_count
< vm_page_gobble_count_warning
);
1374 * Only let privileged threads (involved in pageout)
1375 * dip into the reserved pool.
1378 if ((vm_page_free_count
< vm_page_free_reserved
) &&
1379 !(current_thread()->options
& TH_OPT_VMPRIV
)) {
1380 mutex_unlock(&vm_page_queue_free_lock
);
1382 goto wakeup_pageout
;
1385 while (vm_page_queue_free
== VM_PAGE_NULL
) {
1386 mutex_unlock(&vm_page_queue_free_lock
);
1388 mutex_lock(&vm_page_queue_free_lock
);
1391 if (--vm_page_free_count
< vm_page_free_count_minimum
)
1392 vm_page_free_count_minimum
= vm_page_free_count
;
1393 mem
= vm_page_queue_free
;
1394 vm_page_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1395 mem
->pageq
.next
= NULL
;
1396 mem
->pageq
.prev
= NULL
;
1397 assert(mem
->listq
.next
== NULL
&& mem
->listq
.prev
== NULL
);
1398 assert(mem
->tabled
== FALSE
);
1399 assert(mem
->object
== VM_OBJECT_NULL
);
1400 assert(!mem
->laundry
);
1402 mem
->no_isync
= TRUE
;
1403 mutex_unlock(&vm_page_queue_free_lock
);
1405 assert(pmap_verify_free(mem
->phys_page
));
1408 * Decide if we should poke the pageout daemon.
1409 * We do this if the free count is less than the low
1410 * water mark, or if the free count is less than the high
1411 * water mark (but above the low water mark) and the inactive
1412 * count is less than its target.
1414 * We don't have the counts locked ... if they change a little,
1415 * it doesn't really matter.
1419 if ((vm_page_free_count
< vm_page_free_min
) ||
1420 ((vm_page_free_count
< vm_page_free_target
) &&
1421 (vm_page_inactive_count
< vm_page_inactive_target
)))
1422 thread_wakeup((event_t
) &vm_page_free_wanted
);
1424 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */
1432 * Return a page to the free list.
1437 register vm_page_t mem
)
1441 unsigned int pindex
;
1442 phys_entry
*physent
;
1444 physent
= mapping_phys_lookup(mem
->phys_page
, &pindex
); /* (BRINGUP) */
1445 if(physent
->ppLink
& ppN
) { /* (BRINGUP) */
1446 panic("vm_page_release: already released - %08X %08X\n", mem
, mem
->phys_page
);
1448 physent
->ppLink
= physent
->ppLink
| ppN
; /* (BRINGUP) */
1450 assert(!mem
->private && !mem
->fictitious
);
1452 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */
1454 mutex_lock(&vm_page_queue_free_lock
);
1457 panic("vm_page_release");
1460 assert(!mem
->laundry
);
1461 assert(mem
->object
== VM_OBJECT_NULL
);
1462 assert(mem
->pageq
.next
== NULL
&&
1463 mem
->pageq
.prev
== NULL
);
1464 mem
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1465 vm_page_queue_free
= mem
;
1466 vm_page_free_count
++;
1469 * Check if we should wake up someone waiting for page.
1470 * But don't bother waking them unless they can allocate.
1472 * We wakeup only one thread, to prevent starvation.
1473 * Because the scheduling system handles wait queues FIFO,
1474 * if we wakeup all waiting threads, one greedy thread
1475 * can starve multiple niceguy threads. When the threads
1476 * all wakeup, the greedy threads runs first, grabs the page,
1477 * and waits for another page. It will be the first to run
1478 * when the next page is freed.
1480 * However, there is a slight danger here.
1481 * The thread we wake might not use the free page.
1482 * Then the other threads could wait indefinitely
1483 * while the page goes unused. To forestall this,
1484 * the pageout daemon will keep making free pages
1485 * as long as vm_page_free_wanted is non-zero.
1488 if ((vm_page_free_wanted
> 0) &&
1489 (vm_page_free_count
>= vm_page_free_reserved
)) {
1490 vm_page_free_wanted
--;
1491 thread_wakeup_one((event_t
) &vm_page_free_count
);
1494 mutex_unlock(&vm_page_queue_free_lock
);
1500 * Wait for a page to become available.
1501 * If there are plenty of free pages, then we don't sleep.
1504 * TRUE: There may be another page, try again
1505 * FALSE: We were interrupted out of our wait, don't try again
1513 * We can't use vm_page_free_reserved to make this
1514 * determination. Consider: some thread might
1515 * need to allocate two pages. The first allocation
1516 * succeeds, the second fails. After the first page is freed,
1517 * a call to vm_page_wait must really block.
1519 kern_return_t wait_result
;
1520 int need_wakeup
= 0;
1522 mutex_lock(&vm_page_queue_free_lock
);
1523 if (vm_page_free_count
< vm_page_free_target
) {
1524 if (vm_page_free_wanted
++ == 0)
1526 wait_result
= assert_wait((event_t
)&vm_page_free_count
, interruptible
);
1527 mutex_unlock(&vm_page_queue_free_lock
);
1528 counter(c_vm_page_wait_block
++);
1531 thread_wakeup((event_t
)&vm_page_free_wanted
);
1533 if (wait_result
== THREAD_WAITING
)
1534 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1536 return(wait_result
== THREAD_AWAKENED
);
1538 mutex_unlock(&vm_page_queue_free_lock
);
1546 * Allocate and return a memory cell associated
1547 * with this VM object/offset pair.
1549 * Object must be locked.
1555 vm_object_offset_t offset
)
1557 register vm_page_t mem
;
1560 _mutex_assert(&object
->Lock
, MA_OWNED
);
1562 mem
= vm_page_grab();
1563 if (mem
== VM_PAGE_NULL
)
1564 return VM_PAGE_NULL
;
1566 vm_page_insert(mem
, object
, offset
);
1571 counter(unsigned int c_laundry_pages_freed
= 0;)
1573 int vm_pagein_cluster_unused
= 0;
1574 boolean_t vm_page_free_verify
= TRUE
;
1578 * Returns the given page to the free list,
1579 * disassociating it with any VM object.
1581 * Object and page queues must be locked prior to entry.
1585 register vm_page_t mem
)
1587 vm_object_t object
= mem
->object
;
1590 assert(!mem
->cleaning
);
1591 assert(!mem
->pageout
);
1592 if (vm_page_free_verify
&& !mem
->fictitious
&& !mem
->private) {
1593 assert(pmap_verify_free(mem
->phys_page
));
1598 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
1599 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1602 panic("vm_page_free: freeing page on free list\n");
1605 vm_page_remove(mem
); /* clears tabled, object, offset */
1606 VM_PAGE_QUEUES_REMOVE(mem
); /* clears active or inactive */
1608 if (mem
->clustered
) {
1609 mem
->clustered
= FALSE
;
1610 vm_pagein_cluster_unused
++;
1613 if (mem
->wire_count
) {
1614 if (!mem
->private && !mem
->fictitious
)
1615 vm_page_wire_count
--;
1616 mem
->wire_count
= 0;
1617 assert(!mem
->gobbled
);
1618 } else if (mem
->gobbled
) {
1619 if (!mem
->private && !mem
->fictitious
)
1620 vm_page_wire_count
--;
1621 vm_page_gobble_count
--;
1623 mem
->gobbled
= FALSE
;
1626 vm_pageout_throttle_up(mem
);
1627 counter(++c_laundry_pages_freed
);
1630 PAGE_WAKEUP(mem
); /* clears wanted */
1633 vm_object_absent_release(object
);
1635 /* Some of these may be unnecessary */
1637 mem
->unlock_request
= 0;
1639 mem
->absent
= FALSE
;
1642 mem
->precious
= FALSE
;
1643 mem
->reference
= FALSE
;
1644 mem
->encrypted
= FALSE
;
1646 mem
->page_error
= KERN_SUCCESS
;
1649 mem
->private = FALSE
;
1650 mem
->fictitious
= TRUE
;
1651 mem
->phys_page
= vm_page_fictitious_addr
;
1653 if (mem
->fictitious
) {
1654 vm_page_release_fictitious(mem
);
1656 /* depends on the queues lock */
1657 if(mem
->zero_fill
) {
1659 mem
->zero_fill
= FALSE
;
1661 vm_page_init(mem
, mem
->phys_page
);
1662 vm_page_release(mem
);
1669 register vm_page_t mem
)
1671 register vm_page_t nxt
;
1672 register vm_page_t first
= NULL
;
1673 register vm_page_t last
= VM_PAGE_NULL
;
1674 register int pg_count
= 0;
1677 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1681 if (mem
->tabled
|| mem
->object
)
1682 panic("vm_page_free_list: freeing tabled page\n");
1683 if (mem
->inactive
|| mem
->active
|| mem
->free
)
1684 panic("vm_page_free_list: freeing page on list\n");
1686 assert(mem
->pageq
.prev
== NULL
);
1687 nxt
= (vm_page_t
)(mem
->pageq
.next
);
1690 vm_pagein_cluster_unused
++;
1693 vm_pageout_throttle_up(mem
);
1694 counter(++c_laundry_pages_freed
);
1698 PAGE_WAKEUP(mem
); /* clears wanted */
1701 mem
->fictitious
= TRUE
;
1703 if (!mem
->fictitious
) {
1704 /* depends on the queues lock */
1707 assert(!mem
->laundry
);
1708 vm_page_init(mem
, mem
->phys_page
);
1714 mem
->pageq
.next
= (queue_t
) first
;
1719 mem
->phys_page
= vm_page_fictitious_addr
;
1720 vm_page_release_fictitious(mem
);
1726 mutex_lock(&vm_page_queue_free_lock
);
1728 last
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1729 vm_page_queue_free
= first
;
1731 vm_page_free_count
+= pg_count
;
1733 if ((vm_page_free_wanted
> 0) &&
1734 (vm_page_free_count
>= vm_page_free_reserved
)) {
1735 unsigned int available_pages
;
1737 if (vm_page_free_count
>= vm_page_free_reserved
) {
1738 available_pages
= (vm_page_free_count
1739 - vm_page_free_reserved
);
1741 available_pages
= 0;
1744 if (available_pages
>= vm_page_free_wanted
) {
1745 vm_page_free_wanted
= 0;
1746 thread_wakeup((event_t
) &vm_page_free_count
);
1748 while (available_pages
--) {
1749 vm_page_free_wanted
--;
1750 thread_wakeup_one((event_t
) &vm_page_free_count
);
1754 mutex_unlock(&vm_page_queue_free_lock
);
1762 * Mark this page as wired down by yet
1763 * another map, removing it from paging queues
1766 * The page's object and the page queues must be locked.
1770 register vm_page_t mem
)
1773 // dbgLog(current_thread(), mem->offset, mem->object, 1); /* (TEST/DEBUG) */
1778 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
1779 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1781 if (mem
->wire_count
== 0) {
1782 VM_PAGE_QUEUES_REMOVE(mem
);
1783 if (!mem
->private && !mem
->fictitious
&& !mem
->gobbled
)
1784 vm_page_wire_count
++;
1786 vm_page_gobble_count
--;
1787 mem
->gobbled
= FALSE
;
1788 if(mem
->zero_fill
) {
1789 /* depends on the queues lock */
1791 mem
->zero_fill
= FALSE
;
1795 * The page could be encrypted, but
1796 * We don't have to decrypt it here
1797 * because we don't guarantee that the
1798 * data is actually valid at this point.
1799 * The page will get decrypted in
1800 * vm_fault_wire() if needed.
1803 assert(!mem
->gobbled
);
1810 * Mark this page as consumed by the vm/ipc/xmm subsystems.
1812 * Called only for freshly vm_page_grab()ed pages - w/ nothing locked.
1816 register vm_page_t mem
)
1818 vm_page_lock_queues();
1821 assert(!mem
->gobbled
);
1822 assert(mem
->wire_count
== 0);
1824 if (!mem
->gobbled
&& mem
->wire_count
== 0) {
1825 if (!mem
->private && !mem
->fictitious
)
1826 vm_page_wire_count
++;
1828 vm_page_gobble_count
++;
1829 mem
->gobbled
= TRUE
;
1830 vm_page_unlock_queues();
1836 * Release one wiring of this page, potentially
1837 * enabling it to be paged again.
1839 * The page's object and the page queues must be locked.
1843 register vm_page_t mem
)
1846 // dbgLog(current_thread(), mem->offset, mem->object, 0); /* (TEST/DEBUG) */
1849 assert(mem
->wire_count
> 0);
1852 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
1853 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1855 if (--mem
->wire_count
== 0) {
1856 assert(!mem
->private && !mem
->fictitious
);
1857 vm_page_wire_count
--;
1858 assert(!mem
->laundry
);
1859 assert(mem
->object
!= kernel_object
);
1860 assert(mem
->pageq
.next
== NULL
&& mem
->pageq
.prev
== NULL
);
1861 queue_enter(&vm_page_queue_active
, mem
, vm_page_t
, pageq
);
1862 vm_page_active_count
++;
1864 mem
->reference
= TRUE
;
1869 * vm_page_deactivate:
1871 * Returns the given page to the inactive list,
1872 * indicating that no physical maps have access
1873 * to this page. [Used by the physical mapping system.]
1875 * The page queues must be locked.
1879 register vm_page_t m
)
1882 assert(m
->object
!= kernel_object
);
1884 // dbgLog(m->phys_page, vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
1886 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1889 * This page is no longer very interesting. If it was
1890 * interesting (active or inactive/referenced), then we
1891 * clear the reference bit and (re)enter it in the
1892 * inactive queue. Note wired pages should not have
1893 * their reference bit cleared.
1895 if (m
->gobbled
) { /* can this happen? */
1896 assert(m
->wire_count
== 0);
1897 if (!m
->private && !m
->fictitious
)
1898 vm_page_wire_count
--;
1899 vm_page_gobble_count
--;
1902 if (m
->private || (m
->wire_count
!= 0))
1904 if (m
->active
|| (m
->inactive
&& m
->reference
)) {
1905 if (!m
->fictitious
&& !m
->absent
)
1906 pmap_clear_reference(m
->phys_page
);
1907 m
->reference
= FALSE
;
1908 VM_PAGE_QUEUES_REMOVE(m
);
1910 if (m
->wire_count
== 0 && !m
->inactive
) {
1911 m
->page_ticket
= vm_page_ticket
;
1912 vm_page_ticket_roll
++;
1914 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
1915 vm_page_ticket_roll
= 0;
1916 if(vm_page_ticket
== VM_PAGE_TICKET_ROLL_IDS
)
1922 assert(!m
->laundry
);
1923 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
1925 queue_enter(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1927 queue_enter(&vm_page_queue_inactive
,
1928 m
, vm_page_t
, pageq
);
1933 vm_page_inactive_count
++;
1940 * Put the specified page on the active list (if appropriate).
1942 * The page queues must be locked.
1947 register vm_page_t m
)
1950 assert(m
->object
!= kernel_object
);
1952 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1955 assert(m
->wire_count
== 0);
1956 if (!m
->private && !m
->fictitious
)
1957 vm_page_wire_count
--;
1958 vm_page_gobble_count
--;
1965 assert(!m
->laundry
);
1967 queue_remove(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1969 queue_remove(&vm_page_queue_inactive
,
1970 m
, vm_page_t
, pageq
);
1972 m
->pageq
.next
= NULL
;
1973 m
->pageq
.prev
= NULL
;
1975 vm_page_inactive_count
--;
1976 m
->inactive
= FALSE
;
1978 if (m
->wire_count
== 0) {
1981 panic("vm_page_activate: already active");
1983 assert(!m
->laundry
);
1984 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
1985 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
1987 m
->reference
= TRUE
;
1989 vm_page_active_count
++;
1994 * vm_page_part_zero_fill:
1996 * Zero-fill a part of the page.
1999 vm_page_part_zero_fill(
2007 #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
2008 pmap_zero_part_page(m
->phys_page
, m_pa
, len
);
2011 tmp
= vm_page_grab();
2012 if (tmp
== VM_PAGE_NULL
) {
2013 vm_page_wait(THREAD_UNINT
);
2018 vm_page_zero_fill(tmp
);
2020 vm_page_part_copy(m
, 0, tmp
, 0, m_pa
);
2022 if((m_pa
+ len
) < PAGE_SIZE
) {
2023 vm_page_part_copy(m
, m_pa
+ len
, tmp
,
2024 m_pa
+ len
, PAGE_SIZE
- (m_pa
+ len
));
2026 vm_page_copy(tmp
,m
);
2027 vm_page_lock_queues();
2029 vm_page_unlock_queues();
2035 * vm_page_zero_fill:
2037 * Zero-fill the specified page.
2044 "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
2045 (integer_t
)m
->object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
2049 // dbgTrace(0xAEAEAEAE, m->phys_page, 0); /* (BRINGUP) */
2050 pmap_zero_page(m
->phys_page
);
2054 * vm_page_part_copy:
2056 * copy part of one page to another
2067 VM_PAGE_CHECK(src_m
);
2068 VM_PAGE_CHECK(dst_m
);
2070 pmap_copy_part_page(src_m
->phys_page
, src_pa
,
2071 dst_m
->phys_page
, dst_pa
, len
);
2077 * Copy one page to another
2080 * The source page should not be encrypted. The caller should
2081 * make sure the page is decrypted first, if necessary.
2090 "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
2091 (integer_t
)src_m
->object
, src_m
->offset
,
2092 (integer_t
)dest_m
->object
, dest_m
->offset
,
2095 VM_PAGE_CHECK(src_m
);
2096 VM_PAGE_CHECK(dest_m
);
2100 * The source page should not be encrypted at this point.
2101 * The destination page will therefore not contain encrypted
2102 * data after the copy.
2104 if (src_m
->encrypted
) {
2105 panic("vm_page_copy: source page %p is encrypted\n", src_m
);
2107 dest_m
->encrypted
= FALSE
;
2109 pmap_copy_page(src_m
->phys_page
, dest_m
->phys_page
);
2113 * Currently, this is a primitive allocator that grabs
2114 * free pages from the system, sorts them by physical
2115 * address, then searches for a region large enough to
2116 * satisfy the user's request.
2118 * Additional levels of effort:
2119 * + steal clean active/inactive pages
2120 * + force pageouts of dirty pages
2121 * + maintain a map of available physical
2127 * Check that the list of pages is ordered by
2128 * ascending physical address and has no holes.
2130 int vm_page_verify_contiguous(
2132 unsigned int npages
);
2135 vm_page_verify_contiguous(
2137 unsigned int npages
)
2139 register vm_page_t m
;
2140 unsigned int page_count
;
2141 vm_offset_t prev_addr
;
2143 prev_addr
= pages
->phys_page
;
2145 for (m
= NEXT_PAGE(pages
); m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2146 if (m
->phys_page
!= prev_addr
+ 1) {
2147 printf("m 0x%x prev_addr 0x%x, current addr 0x%x\n",
2148 m
, prev_addr
, m
->phys_page
);
2149 printf("pages 0x%x page_count %d\n", pages
, page_count
);
2150 panic("vm_page_verify_contiguous: not contiguous!");
2152 prev_addr
= m
->phys_page
;
2155 if (page_count
!= npages
) {
2156 printf("pages 0x%x actual count 0x%x but requested 0x%x\n",
2157 pages
, page_count
, npages
);
2158 panic("vm_page_verify_contiguous: count error");
2162 #endif /* MACH_ASSERT */
2165 cpm_counter(unsigned int vpfls_pages_handled
= 0;)
2166 cpm_counter(unsigned int vpfls_head_insertions
= 0;)
2167 cpm_counter(unsigned int vpfls_tail_insertions
= 0;)
2168 cpm_counter(unsigned int vpfls_general_insertions
= 0;)
2169 cpm_counter(unsigned int vpfc_failed
= 0;)
2170 cpm_counter(unsigned int vpfc_satisfied
= 0;)
2173 * Find a region large enough to contain at least npages
2174 * of contiguous physical memory.
2177 * - Called while holding vm_page_queue_free_lock.
2178 * - Doesn't respect vm_page_free_reserved; caller
2179 * must not ask for more pages than are legal to grab.
2181 * Returns a pointer to a list of gobbled pages or VM_PAGE_NULL.
2184 * Loop over the free list, extracting one page at a time and
2185 * inserting those into a sorted sub-list. We stop as soon as
2186 * there's a contiguous range within the sorted list that can
2187 * satisfy the contiguous memory request. This contiguous sub-
2188 * list is chopped out of the sorted sub-list and the remainder
2189 * of the sorted sub-list is put back onto the beginning of the
2193 vm_page_find_contiguous(
2194 unsigned int contig_pages
)
2196 vm_page_t sort_list
;
2197 vm_page_t
*contfirstprev
, contlast
;
2199 ppnum_t prevcontaddr
;
2200 ppnum_t nextcontaddr
;
2201 unsigned int npages
;
2205 _mutex_assert(&vm_page_queue_free_lock
, MA_OWNED
);
2209 * Verify pages in the free list..
2212 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
))
2214 if (npages
!= vm_page_free_count
)
2215 panic("vm_sort_free_list: prelim: npages %u free_count %d",
2216 npages
, vm_page_free_count
);
2217 #endif /* MACH_ASSERT */
2219 if (contig_pages
== 0 || vm_page_queue_free
== VM_PAGE_NULL
)
2220 return VM_PAGE_NULL
;
2222 #define PPNUM_PREV(x) (((x) > 0) ? ((x) - 1) : 0)
2223 #define PPNUM_NEXT(x) (((x) < PPNUM_MAX) ? ((x) + 1) : PPNUM_MAX)
2224 #define SET_NEXT_PAGE(m,n) ((m)->pageq.next = (struct queue_entry *) (n))
2227 contfirstprev
= &sort_list
;
2228 contlast
= sort_list
= vm_page_queue_free
;
2229 vm_page_queue_free
= NEXT_PAGE(sort_list
);
2230 SET_NEXT_PAGE(sort_list
, VM_PAGE_NULL
);
2231 prevcontaddr
= PPNUM_PREV(sort_list
->phys_page
);
2232 nextcontaddr
= PPNUM_NEXT(sort_list
->phys_page
);
2234 while (npages
< contig_pages
&&
2235 (m
= vm_page_queue_free
) != VM_PAGE_NULL
)
2237 cpm_counter(++vpfls_pages_handled
);
2239 /* prepend to existing run? */
2240 if (m
->phys_page
== prevcontaddr
)
2242 vm_page_queue_free
= NEXT_PAGE(m
);
2243 cpm_counter(++vpfls_head_insertions
);
2244 prevcontaddr
= PPNUM_PREV(prevcontaddr
);
2245 SET_NEXT_PAGE(m
, *contfirstprev
);
2248 continue; /* no tail expansion check needed */
2251 /* append to tail of existing run? */
2252 else if (m
->phys_page
== nextcontaddr
)
2254 vm_page_queue_free
= NEXT_PAGE(m
);
2255 cpm_counter(++vpfls_tail_insertions
);
2256 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2257 SET_NEXT_PAGE(m
, NEXT_PAGE(contlast
));
2258 SET_NEXT_PAGE(contlast
, m
);
2263 /* prepend to the very front of sorted list? */
2264 else if (m
->phys_page
< sort_list
->phys_page
)
2266 vm_page_queue_free
= NEXT_PAGE(m
);
2267 cpm_counter(++vpfls_general_insertions
);
2268 prevcontaddr
= PPNUM_PREV(m
->phys_page
);
2269 nextcontaddr
= PPNUM_NEXT(m
->phys_page
);
2270 SET_NEXT_PAGE(m
, sort_list
);
2271 contfirstprev
= &sort_list
;
2272 contlast
= sort_list
= m
;
2276 else /* get to proper place for insertion */
2278 if (m
->phys_page
< nextcontaddr
)
2280 prevcontaddr
= PPNUM_PREV(sort_list
->phys_page
);
2281 nextcontaddr
= PPNUM_NEXT(sort_list
->phys_page
);
2282 contfirstprev
= &sort_list
;
2283 contlast
= sort_list
;
2286 for (m1
= NEXT_PAGE(contlast
);
2287 npages
< contig_pages
&&
2288 m1
!= VM_PAGE_NULL
&& m1
->phys_page
< m
->phys_page
;
2291 if (m1
->phys_page
!= nextcontaddr
) {
2292 prevcontaddr
= PPNUM_PREV(m1
->phys_page
);
2293 contfirstprev
= NEXT_PAGE_PTR(contlast
);
2298 nextcontaddr
= PPNUM_NEXT(m1
->phys_page
);
2303 * We may actually already have enough.
2304 * This could happen if a previous prepend
2305 * joined up two runs to meet our needs.
2306 * If so, bail before we take the current
2307 * page off the free queue.
2309 if (npages
== contig_pages
)
2312 if (m
->phys_page
!= nextcontaddr
)
2314 contfirstprev
= NEXT_PAGE_PTR(contlast
);
2315 prevcontaddr
= PPNUM_PREV(m
->phys_page
);
2316 nextcontaddr
= PPNUM_NEXT(m
->phys_page
);
2319 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2322 vm_page_queue_free
= NEXT_PAGE(m
);
2323 cpm_counter(++vpfls_general_insertions
);
2324 SET_NEXT_PAGE(m
, NEXT_PAGE(contlast
));
2325 SET_NEXT_PAGE(contlast
, m
);
2329 /* See how many pages are now contiguous after the insertion */
2330 for (m1
= NEXT_PAGE(m
);
2331 npages
< contig_pages
&&
2332 m1
!= VM_PAGE_NULL
&& m1
->phys_page
== nextcontaddr
;
2335 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2341 /* how did we do? */
2342 if (npages
== contig_pages
)
2344 cpm_counter(++vpfc_satisfied
);
2346 /* remove the contiguous range from the sorted list */
2348 *contfirstprev
= NEXT_PAGE(contlast
);
2349 SET_NEXT_PAGE(contlast
, VM_PAGE_NULL
);
2350 assert(vm_page_verify_contiguous(m
, npages
));
2352 /* inline vm_page_gobble() for each returned page */
2353 for (m1
= m
; m1
!= VM_PAGE_NULL
; m1
= NEXT_PAGE(m1
)) {
2355 assert(!m1
->wanted
);
2356 assert(!m1
->laundry
);
2358 m1
->no_isync
= TRUE
;
2361 vm_page_wire_count
+= npages
;
2362 vm_page_gobble_count
+= npages
;
2363 vm_page_free_count
-= npages
;
2365 /* stick free list at the tail of the sorted list */
2366 while ((m1
= *contfirstprev
) != VM_PAGE_NULL
)
2367 contfirstprev
= (vm_page_t
*)&m1
->pageq
.next
;
2368 *contfirstprev
= vm_page_queue_free
;
2371 vm_page_queue_free
= sort_list
;
2376 * Allocate a list of contiguous, wired pages.
2384 register vm_page_t m
;
2386 unsigned int npages
;
2387 unsigned int vm_pages_available
;
2390 if (size
% page_size
!= 0)
2391 return KERN_INVALID_ARGUMENT
;
2393 vm_page_lock_queues();
2394 mutex_lock(&vm_page_queue_free_lock
);
2397 * Should also take active and inactive pages
2398 * into account... One day...
2400 npages
= size
/ page_size
;
2401 vm_pages_available
= vm_page_free_count
- vm_page_free_reserved
;
2403 if (npages
> vm_pages_available
) {
2404 mutex_unlock(&vm_page_queue_free_lock
);
2405 vm_page_unlock_queues();
2406 return KERN_RESOURCE_SHORTAGE
;
2410 * Obtain a pointer to a subset of the free
2411 * list large enough to satisfy the request;
2412 * the region will be physically contiguous.
2414 pages
= vm_page_find_contiguous(npages
);
2416 /* adjust global freelist counts and determine need for wakeups */
2417 if (vm_page_free_count
< vm_page_free_count_minimum
)
2418 vm_page_free_count_minimum
= vm_page_free_count
;
2420 wakeup
= ((vm_page_free_count
< vm_page_free_min
) ||
2421 ((vm_page_free_count
< vm_page_free_target
) &&
2422 (vm_page_inactive_count
< vm_page_inactive_target
)));
2424 mutex_unlock(&vm_page_queue_free_lock
);
2426 if (pages
== VM_PAGE_NULL
) {
2427 vm_page_unlock_queues();
2428 return KERN_NO_SPACE
;
2432 * Walk the returned list, wiring the pages.
2435 for (m
= pages
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2437 * Essentially inlined vm_page_wire.
2440 assert(!m
->inactive
);
2441 assert(!m
->private);
2442 assert(!m
->fictitious
);
2443 assert(m
->wire_count
== 0);
2447 --vm_page_gobble_count
;
2449 vm_page_unlock_queues();
2452 thread_wakeup((event_t
) &vm_page_free_wanted
);
2455 * The CPM pages should now be available and
2456 * ordered by ascending physical address.
2458 assert(vm_page_verify_contiguous(pages
, npages
));
2461 return KERN_SUCCESS
;
2465 #include <mach_vm_debug.h>
2468 #include <mach_debug/hash_info.h>
2469 #include <vm/vm_debug.h>
2472 * Routine: vm_page_info
2474 * Return information about the global VP table.
2475 * Fills the buffer with as much information as possible
2476 * and returns the desired size of the buffer.
2478 * Nothing locked. The caller should provide
2479 * possibly-pageable memory.
2484 hash_info_bucket_t
*info
,
2489 if (vm_page_bucket_count
< count
)
2490 count
= vm_page_bucket_count
;
2492 for (i
= 0; i
< count
; i
++) {
2493 vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
2494 unsigned int bucket_count
= 0;
2497 simple_lock(&vm_page_bucket_lock
);
2498 for (m
= bucket
->pages
; m
!= VM_PAGE_NULL
; m
= m
->next
)
2500 simple_unlock(&vm_page_bucket_lock
);
2502 /* don't touch pageable memory while holding locks */
2503 info
[i
].hib_count
= bucket_count
;
2506 return vm_page_bucket_count
;
2508 #endif /* MACH_VM_DEBUG */
2510 #include <mach_kdb.h>
2513 #include <ddb/db_output.h>
2514 #include <vm/vm_print.h>
2515 #define printf kdbprintf
2518 * Routine: vm_page_print [exported]
2526 p
= (vm_page_t
) (long) db_addr
;
2528 iprintf("page 0x%x\n", p
);
2532 iprintf("object=0x%x", p
->object
);
2533 printf(", offset=0x%x", p
->offset
);
2534 printf(", wire_count=%d", p
->wire_count
);
2536 iprintf("%sinactive, %sactive, %sgobbled, %slaundry, %sfree, %sref, %sencrypted\n",
2537 (p
->inactive
? "" : "!"),
2538 (p
->active
? "" : "!"),
2539 (p
->gobbled
? "" : "!"),
2540 (p
->laundry
? "" : "!"),
2541 (p
->free
? "" : "!"),
2542 (p
->reference
? "" : "!"),
2543 (p
->encrypted
? "" : "!"));
2544 iprintf("%sbusy, %swanted, %stabled, %sfictitious, %sprivate, %sprecious\n",
2545 (p
->busy
? "" : "!"),
2546 (p
->wanted
? "" : "!"),
2547 (p
->tabled
? "" : "!"),
2548 (p
->fictitious
? "" : "!"),
2549 (p
->private ? "" : "!"),
2550 (p
->precious
? "" : "!"));
2551 iprintf("%sabsent, %serror, %sdirty, %scleaning, %spageout, %sclustered\n",
2552 (p
->absent
? "" : "!"),
2553 (p
->error
? "" : "!"),
2554 (p
->dirty
? "" : "!"),
2555 (p
->cleaning
? "" : "!"),
2556 (p
->pageout
? "" : "!"),
2557 (p
->clustered
? "" : "!"));
2558 iprintf("%slock_supplied, %soverwriting, %srestart, %sunusual\n",
2559 (p
->lock_supplied
? "" : "!"),
2560 (p
->overwriting
? "" : "!"),
2561 (p
->restart
? "" : "!"),
2562 (p
->unusual
? "" : "!"));
2564 iprintf("phys_page=0x%x", p
->phys_page
);
2565 printf(", page_error=0x%x", p
->page_error
);
2566 printf(", page_lock=0x%x", p
->page_lock
);
2567 printf(", unlock_request=%d\n", p
->unlock_request
);
2571 #endif /* MACH_KDB */