2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 * Author: Avadis Tevanian, Jr., Michael Wayne Young
59 * Resident memory management module.
62 #include <mach/clock_types.h>
63 #include <mach/vm_prot.h>
64 #include <mach/vm_statistics.h>
65 #include <kern/counters.h>
66 #include <kern/sched_prim.h>
67 #include <kern/task.h>
68 #include <kern/thread.h>
69 #include <kern/zalloc.h>
72 #include <vm/vm_init.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pageout.h>
76 #include <vm/vm_kern.h> /* kernel_memory_allocate() */
77 #include <kern/misc_protos.h>
78 #include <zone_debug.h>
80 #include <ppc/mappings.h> /* (BRINGUP) */
81 #include <pexpert/pexpert.h> /* (BRINGUP) */
84 /* Variables used to indicate the relative age of pages in the
88 int vm_page_ticket_roll
= 0;
89 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
)
128 #if MACH_PAGE_HASH_STATS
129 /* This routine is only for debug. It is intended to be called by
130 * hand by a developer using a kernel debugger. This routine prints
131 * out vm_page_hash table statistics to the kernel debug console.
141 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
142 if (vm_page_buckets
[i
].hi_count
) {
144 highsum
+= vm_page_buckets
[i
].hi_count
;
145 if (vm_page_buckets
[i
].hi_count
> maxdepth
)
146 maxdepth
= vm_page_buckets
[i
].hi_count
;
149 printf("Total number of buckets: %d\n", vm_page_bucket_count
);
150 printf("Number used buckets: %d = %d%%\n",
151 numbuckets
, 100*numbuckets
/vm_page_bucket_count
);
152 printf("Number unused buckets: %d = %d%%\n",
153 vm_page_bucket_count
- numbuckets
,
154 100*(vm_page_bucket_count
-numbuckets
)/vm_page_bucket_count
);
155 printf("Sum of bucket max depth: %d\n", highsum
);
156 printf("Average bucket depth: %d.%2d\n",
157 highsum
/vm_page_bucket_count
,
158 highsum%vm_page_bucket_count
);
159 printf("Maximum bucket depth: %d\n", maxdepth
);
161 #endif /* MACH_PAGE_HASH_STATS */
164 * The virtual page size is currently implemented as a runtime
165 * variable, but is constant once initialized using vm_set_page_size.
166 * This initialization must be done in the machine-dependent
167 * bootstrap sequence, before calling other machine-independent
170 * All references to the virtual page size outside this
171 * module must use the PAGE_SIZE, PAGE_MASK and PAGE_SHIFT
174 #ifndef PAGE_SIZE_FIXED
175 vm_size_t page_size
= 4096;
176 vm_size_t page_mask
= 4095;
179 vm_size_t page_size
= PAGE_SIZE
;
180 vm_size_t page_mask
= PAGE_MASK
;
181 int page_shift
= PAGE_SHIFT
;
182 #endif /* PAGE_SIZE_FIXED */
185 * Resident page structures are initialized from
186 * a template (see vm_page_alloc).
188 * When adding a new field to the virtual memory
189 * object structure, be sure to add initialization
190 * (see vm_page_bootstrap).
192 struct vm_page vm_page_template
;
195 * Resident pages that represent real memory
196 * are allocated from a free list.
198 vm_page_t vm_page_queue_free
;
199 vm_page_t vm_page_queue_fictitious
;
200 decl_mutex_data(,vm_page_queue_free_lock
)
201 unsigned int vm_page_free_wanted
;
202 int vm_page_free_count
;
203 int vm_page_fictitious_count
;
205 unsigned int vm_page_free_count_minimum
; /* debugging */
208 * Occasionally, the virtual memory system uses
209 * resident page structures that do not refer to
210 * real pages, for example to leave a page with
211 * important state information in the VP table.
213 * These page structures are allocated the way
214 * most other kernel structures are.
217 decl_mutex_data(,vm_page_alloc_lock
)
218 unsigned int io_throttle_zero_fill
;
219 decl_mutex_data(,vm_page_zero_fill_lock
)
222 * Fictitious pages don't have a physical address,
223 * but we must initialize phys_page to something.
224 * For debugging, this should be a strange value
225 * that the pmap module can recognize in assertions.
227 vm_offset_t vm_page_fictitious_addr
= (vm_offset_t
) -1;
230 * Resident page structures are also chained on
231 * queues that are used by the page replacement
232 * system (pageout daemon). These queues are
233 * defined here, but are shared by the pageout
234 * module. The inactive queue is broken into
235 * inactive and zf for convenience as the
236 * pageout daemon often assignes a higher
237 * affinity to zf pages
239 queue_head_t vm_page_queue_active
;
240 queue_head_t vm_page_queue_inactive
;
241 queue_head_t vm_page_queue_zf
;
242 decl_mutex_data(,vm_page_queue_lock
)
243 int vm_page_active_count
;
244 int vm_page_inactive_count
;
245 int vm_page_wire_count
;
246 int vm_page_gobble_count
= 0;
247 int vm_page_wire_count_warning
= 0;
248 int vm_page_gobble_count_warning
= 0;
250 /* the following fields are protected by the vm_page_queue_lock */
251 queue_head_t vm_page_queue_limbo
;
252 int vm_page_limbo_count
= 0; /* total pages in limbo */
253 int vm_page_limbo_real_count
= 0; /* real pages in limbo */
254 int vm_page_pin_count
= 0; /* number of pinned pages */
256 decl_simple_lock_data(,vm_page_preppin_lock
)
259 * Several page replacement parameters are also
260 * shared with this module, so that page allocation
261 * (done here in vm_page_alloc) can trigger the
264 int vm_page_free_target
= 0;
265 int vm_page_free_min
= 0;
266 int vm_page_inactive_target
= 0;
267 int vm_page_free_reserved
= 0;
268 int vm_page_laundry_count
= 0;
269 int vm_page_burst_count
= 0;
270 int vm_page_throttled_count
= 0;
273 * The VM system has a couple of heuristics for deciding
274 * that pages are "uninteresting" and should be placed
275 * on the inactive queue as likely candidates for replacement.
276 * These variables let the heuristics be controlled at run-time
277 * to make experimentation easier.
280 boolean_t vm_page_deactivate_hint
= TRUE
;
285 * Sets the page size, perhaps based upon the memory
286 * size. Must be called before any use of page-size
287 * dependent functions.
289 * Sets page_shift and page_mask from page_size.
292 vm_set_page_size(void)
294 #ifndef PAGE_SIZE_FIXED
295 page_mask
= page_size
- 1;
297 if ((page_mask
& page_size
) != 0)
298 panic("vm_set_page_size: page size not a power of two");
300 for (page_shift
= 0; ; page_shift
++)
301 if ((1 << page_shift
) == page_size
)
303 #endif /* PAGE_SIZE_FIXED */
309 * Initializes the resident memory module.
311 * Allocates memory for the page cells, and
312 * for the object/offset-to-page hash table headers.
313 * Each page cell is initialized and placed on the free list.
314 * Returns the range of available kernel virtual memory.
322 register vm_page_t m
;
329 * Initialize the vm_page template.
332 m
= &vm_page_template
;
333 m
->object
= VM_OBJECT_NULL
; /* reset later */
334 m
->offset
= 0; /* reset later */
342 m
->reference
= FALSE
;
344 m
->dump_cleaning
= FALSE
;
345 m
->list_req_pending
= FALSE
;
350 m
->fictitious
= FALSE
;
357 m
->clustered
= FALSE
;
358 m
->lock_supplied
= FALSE
;
361 m
->zero_fill
= FALSE
;
363 m
->phys_page
= 0; /* reset later */
365 m
->page_lock
= VM_PROT_NONE
;
366 m
->unlock_request
= VM_PROT_NONE
;
367 m
->page_error
= KERN_SUCCESS
;
370 * Initialize the page queues.
373 mutex_init(&vm_page_queue_free_lock
, ETAP_VM_PAGEQ_FREE
);
374 mutex_init(&vm_page_queue_lock
, ETAP_VM_PAGEQ
);
375 simple_lock_init(&vm_page_preppin_lock
, ETAP_VM_PREPPIN
);
377 vm_page_queue_free
= VM_PAGE_NULL
;
378 vm_page_queue_fictitious
= VM_PAGE_NULL
;
379 queue_init(&vm_page_queue_active
);
380 queue_init(&vm_page_queue_inactive
);
381 queue_init(&vm_page_queue_zf
);
382 queue_init(&vm_page_queue_limbo
);
384 vm_page_free_wanted
= 0;
387 * Steal memory for the map and zone subsystems.
390 vm_map_steal_memory();
394 * Allocate (and initialize) the virtual-to-physical
395 * table hash buckets.
397 * The number of buckets should be a power of two to
398 * get a good hash function. The following computation
399 * chooses the first power of two that is greater
400 * than the number of physical pages in the system.
403 simple_lock_init(&vm_page_bucket_lock
, ETAP_VM_BUCKET
);
405 if (vm_page_bucket_count
== 0) {
406 unsigned int npages
= pmap_free_pages();
408 vm_page_bucket_count
= 1;
409 while (vm_page_bucket_count
< npages
)
410 vm_page_bucket_count
<<= 1;
413 vm_page_hash_mask
= vm_page_bucket_count
- 1;
416 * Calculate object shift value for hashing algorithm:
417 * O = log2(sizeof(struct vm_object))
418 * B = log2(vm_page_bucket_count)
419 * hash shifts the object left by
422 size
= vm_page_bucket_count
;
423 for (log1
= 0; size
> 1; log1
++)
425 size
= sizeof(struct vm_object
);
426 for (log2
= 0; size
> 1; log2
++)
428 vm_page_hash_shift
= log1
/2 - log2
+ 1;
430 vm_page_bucket_hash
= 1 << ((log1
+ 1) >> 1); /* Get (ceiling of sqrt of table size) */
431 vm_page_bucket_hash
|= 1 << ((log1
+ 1) >> 2); /* Get (ceiling of quadroot of table size) */
432 vm_page_bucket_hash
|= 1; /* Set bit and add 1 - always must be 1 to insure unique series */
434 if (vm_page_hash_mask
& vm_page_bucket_count
)
435 printf("vm_page_bootstrap: WARNING -- strange page hash\n");
437 vm_page_buckets
= (vm_page_bucket_t
*)
438 pmap_steal_memory(vm_page_bucket_count
*
439 sizeof(vm_page_bucket_t
));
441 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
442 register vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
444 bucket
->pages
= VM_PAGE_NULL
;
445 #if MACH_PAGE_HASH_STATS
446 bucket
->cur_count
= 0;
447 bucket
->hi_count
= 0;
448 #endif /* MACH_PAGE_HASH_STATS */
452 * Machine-dependent code allocates the resident page table.
453 * It uses vm_page_init to initialize the page frames.
454 * The code also returns to us the virtual space available
455 * to the kernel. We don't trust the pmap module
456 * to get the alignment right.
459 pmap_startup(&virtual_space_start
, &virtual_space_end
);
460 virtual_space_start
= round_page_32(virtual_space_start
);
461 virtual_space_end
= trunc_page_32(virtual_space_end
);
463 *startp
= virtual_space_start
;
464 *endp
= virtual_space_end
;
467 * Compute the initial "wire" count.
468 * Up until now, the pages which have been set aside are not under
469 * the VM system's control, so although they aren't explicitly
470 * wired, they nonetheless can't be moved. At this moment,
471 * all VM managed pages are "free", courtesy of pmap_startup.
473 vm_page_wire_count
= atop_64(max_mem
) - vm_page_free_count
; /* initial value */
475 printf("vm_page_bootstrap: %d free pages\n", vm_page_free_count
);
476 vm_page_free_count_minimum
= vm_page_free_count
;
479 #ifndef MACHINE_PAGES
481 * We implement pmap_steal_memory and pmap_startup with the help
482 * of two simpler functions, pmap_virtual_space and pmap_next_page.
489 vm_offset_t addr
, vaddr
;
493 * We round the size to a round multiple.
496 size
= (size
+ sizeof (void *) - 1) &~ (sizeof (void *) - 1);
499 * If this is the first call to pmap_steal_memory,
500 * we have to initialize ourself.
503 if (virtual_space_start
== virtual_space_end
) {
504 pmap_virtual_space(&virtual_space_start
, &virtual_space_end
);
507 * The initial values must be aligned properly, and
508 * we don't trust the pmap module to do it right.
511 virtual_space_start
= round_page_32(virtual_space_start
);
512 virtual_space_end
= trunc_page_32(virtual_space_end
);
516 * Allocate virtual memory for this request.
519 addr
= virtual_space_start
;
520 virtual_space_start
+= size
;
522 kprintf("pmap_steal_memory: %08X - %08X; size=%08X\n", addr
, virtual_space_start
, size
); /* (TEST/DEBUG) */
525 * Allocate and map physical pages to back new virtual pages.
528 for (vaddr
= round_page_32(addr
);
530 vaddr
+= PAGE_SIZE
) {
531 if (!pmap_next_page(&phys_page
))
532 panic("pmap_steal_memory");
535 * XXX Logically, these mappings should be wired,
536 * but some pmap modules barf if they are.
539 pmap_enter(kernel_pmap
, vaddr
, phys_page
,
540 VM_PROT_READ
|VM_PROT_WRITE
,
541 VM_WIMG_USE_DEFAULT
, FALSE
);
543 * Account for newly stolen memory
545 vm_page_wire_count
++;
557 unsigned int i
, npages
, pages_initialized
, fill
, fillval
;
563 * We calculate how many page frames we will have
564 * and then allocate the page structures in one chunk.
567 tmpaddr
= (addr64_t
)pmap_free_pages() * (addr64_t
)PAGE_SIZE
; /* Get the amount of memory left */
568 tmpaddr
= tmpaddr
+ (addr64_t
)(round_page_32(virtual_space_start
) - virtual_space_start
); /* Account for any slop */
569 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 */
571 pages
= (vm_page_t
) pmap_steal_memory(npages
* sizeof *pages
);
574 * Initialize the page frames.
577 for (i
= 0, pages_initialized
= 0; i
< npages
; i
++) {
578 if (!pmap_next_page(&phys_page
))
581 vm_page_init(&pages
[i
], phys_page
);
587 * Release pages in reverse order so that physical pages
588 * initially get allocated in ascending addresses. This keeps
589 * the devices (which must address physical memory) happy if
590 * they require several consecutive pages.
594 * Check if we want to initialize pages to a known value
597 fill
= 0; /* Assume no fill */
598 if (PE_parse_boot_arg("fill", &fillval
)) fill
= 1; /* Set fill */
600 for (i
= pages_initialized
; i
> 0; i
--) {
601 extern void fillPage(ppnum_t phys_page
, unsigned int fillval
);
602 if(fill
) fillPage(pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
603 vm_page_release(&pages
[i
- 1]);
608 vm_page_t xx
, xxo
, xxl
;
611 j
= 0; /* (BRINGUP) */
614 for(xx
= vm_page_queue_free
; xx
; xxl
= xx
, xx
= xx
->pageq
.next
) { /* (BRINGUP) */
616 if(j
> vm_page_free_count
) { /* (BRINGUP) */
617 panic("pmap_startup: too many pages, xx = %08X, xxl = %08X\n", xx
, xxl
);
620 l
= vm_page_free_count
- j
; /* (BRINGUP) */
621 k
= 0; /* (BRINGUP) */
623 if(((j
- 1) & 0xFFFF) == 0) kprintf("checking number %d of %d\n", j
, vm_page_free_count
);
625 for(xxo
= xx
->pageq
.next
; xxo
; xxo
= xxo
->pageq
.next
) { /* (BRINGUP) */
627 if(k
> l
) panic("pmap_startup: too many in secondary check %d %d\n", k
, l
);
628 if((xx
->phys_page
& 0xFFFFFFFF) == (xxo
->phys_page
& 0xFFFFFFFF)) { /* (BRINGUP) */
629 panic("pmap_startup: duplicate physaddr, xx = %08X, xxo = %08X\n", xx
, xxo
);
634 if(j
!= vm_page_free_count
) { /* (BRINGUP) */
635 panic("pmap_startup: vm_page_free_count does not match, calc = %d, vm_page_free_count = %08X\n", j
, vm_page_free_count
);
642 * We have to re-align virtual_space_start,
643 * because pmap_steal_memory has been using it.
646 virtual_space_start
= round_page_32(virtual_space_start
);
648 *startp
= virtual_space_start
;
649 *endp
= virtual_space_end
;
651 #endif /* MACHINE_PAGES */
654 * Routine: vm_page_module_init
656 * Second initialization pass, to be done after
657 * the basic VM system is ready.
660 vm_page_module_init(void)
662 vm_page_zone
= zinit((vm_size_t
) sizeof(struct vm_page
),
663 0, PAGE_SIZE
, "vm pages");
666 zone_debug_disable(vm_page_zone
);
667 #endif /* ZONE_DEBUG */
669 zone_change(vm_page_zone
, Z_EXPAND
, FALSE
);
670 zone_change(vm_page_zone
, Z_EXHAUST
, TRUE
);
671 zone_change(vm_page_zone
, Z_FOREIGN
, TRUE
);
674 * Adjust zone statistics to account for the real pages allocated
675 * in vm_page_create(). [Q: is this really what we want?]
677 vm_page_zone
->count
+= vm_page_pages
;
678 vm_page_zone
->cur_size
+= vm_page_pages
* vm_page_zone
->elem_size
;
680 mutex_init(&vm_page_alloc_lock
, ETAP_VM_PAGE_ALLOC
);
681 mutex_init(&vm_page_zero_fill_lock
, ETAP_VM_PAGE_ALLOC
);
685 * Routine: vm_page_create
687 * After the VM system is up, machine-dependent code
688 * may stumble across more physical memory. For example,
689 * memory that it was reserving for a frame buffer.
690 * vm_page_create turns this memory into available pages.
701 for (phys_page
= start
;
704 while ((m
= (vm_page_t
) vm_page_grab_fictitious())
706 vm_page_more_fictitious();
708 vm_page_init(m
, phys_page
);
717 * Distributes the object/offset key pair among hash buckets.
719 * NOTE: The bucket count must be a power of 2
721 #define vm_page_hash(object, offset) (\
722 ( (natural_t)((uint32_t)object * vm_page_bucket_hash) + ((uint32_t)atop_64(offset) ^ vm_page_bucket_hash))\
726 * vm_page_insert: [ internal use only ]
728 * Inserts the given mem entry into the object/object-page
729 * table and object list.
731 * The object must be locked.
736 register vm_page_t mem
,
737 register vm_object_t object
,
738 register vm_object_offset_t offset
)
740 register vm_page_bucket_t
*bucket
;
743 "vm_page_insert, object 0x%X offset 0x%X page 0x%X\n",
744 (integer_t
)object
, (integer_t
)offset
, (integer_t
)mem
, 0,0);
749 panic("vm_page_insert");
751 assert(!object
->internal
|| offset
< object
->size
);
753 /* only insert "pageout" pages into "pageout" objects,
754 * and normal pages into normal objects */
755 assert(object
->pageout
== mem
->pageout
);
758 * Record the object/offset pair in this page
761 mem
->object
= object
;
762 mem
->offset
= offset
;
765 * Insert it into the object_object/offset hash table
768 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
769 simple_lock(&vm_page_bucket_lock
);
770 mem
->next
= bucket
->pages
;
772 #if MACH_PAGE_HASH_STATS
773 if (++bucket
->cur_count
> bucket
->hi_count
)
774 bucket
->hi_count
= bucket
->cur_count
;
775 #endif /* MACH_PAGE_HASH_STATS */
776 simple_unlock(&vm_page_bucket_lock
);
779 * Now link into the object's list of backed pages.
782 queue_enter(&object
->memq
, mem
, vm_page_t
, listq
);
786 * Show that the object has one more resident page.
789 object
->resident_page_count
++;
795 * Exactly like vm_page_insert, except that we first
796 * remove any existing page at the given offset in object.
798 * The object and page queues must be locked.
803 register vm_page_t mem
,
804 register vm_object_t object
,
805 register vm_object_offset_t offset
)
807 register vm_page_bucket_t
*bucket
;
812 panic("vm_page_replace");
815 * Record the object/offset pair in this page
818 mem
->object
= object
;
819 mem
->offset
= offset
;
822 * Insert it into the object_object/offset hash table,
823 * replacing any page that might have been there.
826 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
827 simple_lock(&vm_page_bucket_lock
);
829 vm_page_t
*mp
= &bucket
->pages
;
830 register vm_page_t m
= *mp
;
832 if (m
->object
== object
&& m
->offset
== offset
) {
834 * Remove page from bucket and from object,
835 * and return it to the free list.
838 queue_remove(&object
->memq
, m
, vm_page_t
,
841 object
->resident_page_count
--;
844 * Return page to the free list.
845 * Note the page is not tabled now, so this
846 * won't self-deadlock on the bucket lock.
854 mem
->next
= bucket
->pages
;
856 mem
->next
= VM_PAGE_NULL
;
859 simple_unlock(&vm_page_bucket_lock
);
862 * Now link into the object's list of backed pages.
865 queue_enter(&object
->memq
, mem
, vm_page_t
, listq
);
869 * And show that the object has one more resident
873 object
->resident_page_count
++;
877 * vm_page_remove: [ internal use only ]
879 * Removes the given mem entry from the object/offset-page
880 * table and the object page list.
882 * The object and page must be locked.
887 register vm_page_t mem
)
889 register vm_page_bucket_t
*bucket
;
890 register vm_page_t
this;
893 "vm_page_remove, object 0x%X offset 0x%X page 0x%X\n",
894 (integer_t
)mem
->object
, (integer_t
)mem
->offset
,
895 (integer_t
)mem
, 0,0);
898 assert(!mem
->cleaning
);
902 * Remove from the object_object/offset hash table
905 bucket
= &vm_page_buckets
[vm_page_hash(mem
->object
, mem
->offset
)];
906 simple_lock(&vm_page_bucket_lock
);
907 if ((this = bucket
->pages
) == mem
) {
908 /* optimize for common case */
910 bucket
->pages
= mem
->next
;
912 register vm_page_t
*prev
;
914 for (prev
= &this->next
;
915 (this = *prev
) != mem
;
920 #if MACH_PAGE_HASH_STATS
922 #endif /* MACH_PAGE_HASH_STATS */
923 simple_unlock(&vm_page_bucket_lock
);
926 * Now remove from the object's list of backed pages.
929 queue_remove(&mem
->object
->memq
, mem
, vm_page_t
, listq
);
932 * And show that the object has one fewer resident
936 mem
->object
->resident_page_count
--;
939 mem
->object
= VM_OBJECT_NULL
;
946 * Returns the page associated with the object/offset
947 * pair specified; if none is found, VM_PAGE_NULL is returned.
949 * The object must be locked. No side effects.
954 register vm_object_t object
,
955 register vm_object_offset_t offset
)
957 register vm_page_t mem
;
958 register vm_page_bucket_t
*bucket
;
961 * Search the hash table for this object/offset pair
964 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
966 simple_lock(&vm_page_bucket_lock
);
967 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
969 if ((mem
->object
== object
) && (mem
->offset
== offset
))
972 simple_unlock(&vm_page_bucket_lock
);
980 * Move the given memory entry from its
981 * current object to the specified target object/offset.
983 * The object must be locked.
987 register vm_page_t mem
,
988 register vm_object_t new_object
,
989 vm_object_offset_t new_offset
)
991 assert(mem
->object
!= new_object
);
993 * Changes to mem->object require the page lock because
994 * the pageout daemon uses that lock to get the object.
998 "vm_page_rename, new object 0x%X, offset 0x%X page 0x%X\n",
999 (integer_t
)new_object
, (integer_t
)new_offset
,
1000 (integer_t
)mem
, 0,0);
1002 vm_page_lock_queues();
1003 vm_page_remove(mem
);
1004 vm_page_insert(mem
, new_object
, new_offset
);
1005 vm_page_unlock_queues();
1011 * Initialize the fields in a new page.
1012 * This takes a structure with random values and initializes it
1013 * so that it can be given to vm_page_release or vm_page_insert.
1020 *mem
= vm_page_template
;
1021 mem
->phys_page
= phys_page
;
1025 * vm_page_grab_fictitious:
1027 * Remove a fictitious page from the free list.
1028 * Returns VM_PAGE_NULL if there are no free pages.
1030 int c_vm_page_grab_fictitious
= 0;
1031 int c_vm_page_release_fictitious
= 0;
1032 int c_vm_page_more_fictitious
= 0;
1035 vm_page_grab_fictitious(void)
1037 register vm_page_t m
;
1039 m
= (vm_page_t
)zget(vm_page_zone
);
1041 vm_page_init(m
, vm_page_fictitious_addr
);
1042 m
->fictitious
= TRUE
;
1045 c_vm_page_grab_fictitious
++;
1050 * vm_page_release_fictitious:
1052 * Release a fictitious page to the free list.
1056 vm_page_release_fictitious(
1057 register vm_page_t m
)
1061 assert(m
->fictitious
);
1062 assert(m
->phys_page
== vm_page_fictitious_addr
);
1064 c_vm_page_release_fictitious
++;
1067 panic("vm_page_release_fictitious");
1069 zfree(vm_page_zone
, (vm_offset_t
)m
);
1073 * vm_page_more_fictitious:
1075 * Add more fictitious pages to the free list.
1076 * Allowed to block. This routine is way intimate
1077 * with the zones code, for several reasons:
1078 * 1. we need to carve some page structures out of physical
1079 * memory before zones work, so they _cannot_ come from
1081 * 2. the zone needs to be collectable in order to prevent
1082 * growth without bound. These structures are used by
1083 * the device pager (by the hundreds and thousands), as
1084 * private pages for pageout, and as blocking pages for
1085 * pagein. Temporary bursts in demand should not result in
1086 * permanent allocation of a resource.
1087 * 3. To smooth allocation humps, we allocate single pages
1088 * with kernel_memory_allocate(), and cram them into the
1089 * zone. This also allows us to initialize the vm_page_t's
1090 * on the way into the zone, so that zget() always returns
1091 * an initialized structure. The zone free element pointer
1092 * and the free page pointer are both the first item in the
1094 * 4. By having the pages in the zone pre-initialized, we need
1095 * not keep 2 levels of lists. The garbage collector simply
1096 * scans our list, and reduces physical memory usage as it
1100 void vm_page_more_fictitious(void)
1102 extern vm_map_t zone_map
;
1103 register vm_page_t m
;
1105 kern_return_t retval
;
1108 c_vm_page_more_fictitious
++;
1111 * Allocate a single page from the zone_map. Do not wait if no physical
1112 * pages are immediately available, and do not zero the space. We need
1113 * our own blocking lock here to prevent having multiple,
1114 * simultaneous requests from piling up on the zone_map lock. Exactly
1115 * one (of our) threads should be potentially waiting on the map lock.
1116 * If winner is not vm-privileged, then the page allocation will fail,
1117 * and it will temporarily block here in the vm_page_wait().
1119 mutex_lock(&vm_page_alloc_lock
);
1121 * If another thread allocated space, just bail out now.
1123 if (zone_free_count(vm_page_zone
) > 5) {
1125 * The number "5" is a small number that is larger than the
1126 * number of fictitious pages that any single caller will
1127 * attempt to allocate. Otherwise, a thread will attempt to
1128 * acquire a fictitious page (vm_page_grab_fictitious), fail,
1129 * release all of the resources and locks already acquired,
1130 * and then call this routine. This routine finds the pages
1131 * that the caller released, so fails to allocate new space.
1132 * The process repeats infinitely. The largest known number
1133 * of fictitious pages required in this manner is 2. 5 is
1134 * simply a somewhat larger number.
1136 mutex_unlock(&vm_page_alloc_lock
);
1140 if ((retval
= kernel_memory_allocate(zone_map
,
1141 &addr
, PAGE_SIZE
, VM_PROT_ALL
,
1142 KMA_KOBJECT
|KMA_NOPAGEWAIT
)) != KERN_SUCCESS
) {
1144 * No page was available. Tell the pageout daemon, drop the
1145 * lock to give another thread a chance at it, and
1146 * wait for the pageout daemon to make progress.
1148 mutex_unlock(&vm_page_alloc_lock
);
1149 vm_page_wait(THREAD_UNINT
);
1153 * Initialize as many vm_page_t's as will fit on this page. This
1154 * depends on the zone code disturbing ONLY the first item of
1155 * each zone element.
1157 m
= (vm_page_t
)addr
;
1158 for (i
= PAGE_SIZE
/sizeof(struct vm_page
); i
> 0; i
--) {
1159 vm_page_init(m
, vm_page_fictitious_addr
);
1160 m
->fictitious
= TRUE
;
1163 zcram(vm_page_zone
, addr
, PAGE_SIZE
);
1164 mutex_unlock(&vm_page_alloc_lock
);
1170 * Attempt to convert a fictitious page into a real page.
1175 register vm_page_t m
)
1177 register vm_page_t real_m
;
1180 assert(m
->fictitious
);
1183 real_m
= vm_page_grab();
1184 if (real_m
== VM_PAGE_NULL
)
1187 m
->phys_page
= real_m
->phys_page
;
1188 m
->fictitious
= FALSE
;
1191 vm_page_lock_queues();
1193 vm_page_active_count
++;
1194 else if (m
->inactive
)
1195 vm_page_inactive_count
++;
1196 vm_page_unlock_queues();
1198 real_m
->phys_page
= vm_page_fictitious_addr
;
1199 real_m
->fictitious
= TRUE
;
1201 vm_page_release_fictitious(real_m
);
1208 * Return true if it is not likely that a non-vm_privileged thread
1209 * can get memory without blocking. Advisory only, since the
1210 * situation may change under us.
1215 /* No locking, at worst we will fib. */
1216 return( vm_page_free_count
< vm_page_free_reserved
);
1222 * Remove a page from the free list.
1223 * Returns VM_PAGE_NULL if the free list is too small.
1226 unsigned long vm_page_grab_count
= 0; /* measure demand */
1231 register vm_page_t mem
;
1233 mutex_lock(&vm_page_queue_free_lock
);
1234 vm_page_grab_count
++;
1237 * Optionally produce warnings if the wire or gobble
1238 * counts exceed some threshold.
1240 if (vm_page_wire_count_warning
> 0
1241 && vm_page_wire_count
>= vm_page_wire_count_warning
) {
1242 printf("mk: vm_page_grab(): high wired page count of %d\n",
1243 vm_page_wire_count
);
1244 assert(vm_page_wire_count
< vm_page_wire_count_warning
);
1246 if (vm_page_gobble_count_warning
> 0
1247 && vm_page_gobble_count
>= vm_page_gobble_count_warning
) {
1248 printf("mk: vm_page_grab(): high gobbled page count of %d\n",
1249 vm_page_gobble_count
);
1250 assert(vm_page_gobble_count
< vm_page_gobble_count_warning
);
1254 * Only let privileged threads (involved in pageout)
1255 * dip into the reserved pool.
1258 if ((vm_page_free_count
< vm_page_free_reserved
) &&
1259 !current_thread()->vm_privilege
) {
1260 mutex_unlock(&vm_page_queue_free_lock
);
1262 goto wakeup_pageout
;
1265 while (vm_page_queue_free
== VM_PAGE_NULL
) {
1266 printf("vm_page_grab: no free pages, trouble expected...\n");
1267 mutex_unlock(&vm_page_queue_free_lock
);
1269 mutex_lock(&vm_page_queue_free_lock
);
1272 if (--vm_page_free_count
< vm_page_free_count_minimum
)
1273 vm_page_free_count_minimum
= vm_page_free_count
;
1274 mem
= vm_page_queue_free
;
1275 vm_page_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1277 mem
->no_isync
= TRUE
;
1278 mutex_unlock(&vm_page_queue_free_lock
);
1281 * Decide if we should poke the pageout daemon.
1282 * We do this if the free count is less than the low
1283 * water mark, or if the free count is less than the high
1284 * water mark (but above the low water mark) and the inactive
1285 * count is less than its target.
1287 * We don't have the counts locked ... if they change a little,
1288 * it doesn't really matter.
1292 if ((vm_page_free_count
< vm_page_free_min
) ||
1293 ((vm_page_free_count
< vm_page_free_target
) &&
1294 (vm_page_inactive_count
< vm_page_inactive_target
)))
1295 thread_wakeup((event_t
) &vm_page_free_wanted
);
1297 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */
1305 * Return a page to the free list.
1310 register vm_page_t mem
)
1314 unsigned int pindex
;
1315 phys_entry
*physent
;
1317 physent
= mapping_phys_lookup(mem
->phys_page
, &pindex
); /* (BRINGUP) */
1318 if(physent
->ppLink
& ppN
) { /* (BRINGUP) */
1319 panic("vm_page_release: already released - %08X %08X\n", mem
, mem
->phys_page
);
1321 physent
->ppLink
= physent
->ppLink
| ppN
; /* (BRINGUP) */
1324 assert(!mem
->private && !mem
->fictitious
);
1326 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */
1328 mutex_lock(&vm_page_queue_free_lock
);
1330 panic("vm_page_release");
1332 mem
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1333 vm_page_queue_free
= mem
;
1334 vm_page_free_count
++;
1337 * Check if we should wake up someone waiting for page.
1338 * But don't bother waking them unless they can allocate.
1340 * We wakeup only one thread, to prevent starvation.
1341 * Because the scheduling system handles wait queues FIFO,
1342 * if we wakeup all waiting threads, one greedy thread
1343 * can starve multiple niceguy threads. When the threads
1344 * all wakeup, the greedy threads runs first, grabs the page,
1345 * and waits for another page. It will be the first to run
1346 * when the next page is freed.
1348 * However, there is a slight danger here.
1349 * The thread we wake might not use the free page.
1350 * Then the other threads could wait indefinitely
1351 * while the page goes unused. To forestall this,
1352 * the pageout daemon will keep making free pages
1353 * as long as vm_page_free_wanted is non-zero.
1356 if ((vm_page_free_wanted
> 0) &&
1357 (vm_page_free_count
>= vm_page_free_reserved
)) {
1358 vm_page_free_wanted
--;
1359 thread_wakeup_one((event_t
) &vm_page_free_count
);
1362 mutex_unlock(&vm_page_queue_free_lock
);
1365 #define VM_PAGEOUT_DEADLOCK_TIMEOUT 3
1370 * Wait for a page to become available.
1371 * If there are plenty of free pages, then we don't sleep.
1374 * TRUE: There may be another page, try again
1375 * FALSE: We were interrupted out of our wait, don't try again
1383 * We can't use vm_page_free_reserved to make this
1384 * determination. Consider: some thread might
1385 * need to allocate two pages. The first allocation
1386 * succeeds, the second fails. After the first page is freed,
1387 * a call to vm_page_wait must really block.
1390 kern_return_t wait_result
;
1392 int need_wakeup
= 0;
1394 mutex_lock(&vm_page_queue_free_lock
);
1395 if (vm_page_free_count
< vm_page_free_target
) {
1396 if (vm_page_free_wanted
++ == 0)
1398 wait_result
= assert_wait((event_t
)&vm_page_free_count
,
1400 mutex_unlock(&vm_page_queue_free_lock
);
1401 counter(c_vm_page_wait_block
++);
1404 thread_wakeup((event_t
)&vm_page_free_wanted
);
1406 if (wait_result
== THREAD_WAITING
) {
1407 clock_interval_to_absolutetime_interval(
1408 VM_PAGEOUT_DEADLOCK_TIMEOUT
,
1409 NSEC_PER_SEC
, &abstime
);
1410 clock_absolutetime_interval_to_deadline(
1412 thread_set_timer_deadline(abstime
);
1413 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1415 if(wait_result
== THREAD_TIMED_OUT
) {
1416 kr
= vm_pageout_emergency_availability_request();
1419 thread_cancel_timer();
1423 return(wait_result
== THREAD_AWAKENED
);
1425 mutex_unlock(&vm_page_queue_free_lock
);
1433 * Allocate and return a memory cell associated
1434 * with this VM object/offset pair.
1436 * Object must be locked.
1442 vm_object_offset_t offset
)
1444 register vm_page_t mem
;
1446 mem
= vm_page_grab();
1447 if (mem
== VM_PAGE_NULL
)
1448 return VM_PAGE_NULL
;
1450 vm_page_insert(mem
, object
, offset
);
1455 counter(unsigned int c_laundry_pages_freed
= 0;)
1457 int vm_pagein_cluster_unused
= 0;
1458 boolean_t vm_page_free_verify
= FALSE
;
1462 * Returns the given page to the free list,
1463 * disassociating it with any VM object.
1465 * Object and page queues must be locked prior to entry.
1469 register vm_page_t mem
)
1471 vm_object_t object
= mem
->object
;
1474 assert(!mem
->cleaning
);
1475 assert(!mem
->pageout
);
1476 assert(!vm_page_free_verify
|| pmap_verify_free(mem
->phys_page
));
1479 vm_page_remove(mem
); /* clears tabled, object, offset */
1480 VM_PAGE_QUEUES_REMOVE(mem
); /* clears active or inactive */
1482 if (mem
->clustered
) {
1483 mem
->clustered
= FALSE
;
1484 vm_pagein_cluster_unused
++;
1487 if (mem
->wire_count
) {
1488 if (!mem
->private && !mem
->fictitious
)
1489 vm_page_wire_count
--;
1490 mem
->wire_count
= 0;
1491 assert(!mem
->gobbled
);
1492 } else if (mem
->gobbled
) {
1493 if (!mem
->private && !mem
->fictitious
)
1494 vm_page_wire_count
--;
1495 vm_page_gobble_count
--;
1497 mem
->gobbled
= FALSE
;
1500 extern int vm_page_laundry_min
;
1501 if (!object
->internal
)
1502 vm_page_burst_count
--;
1503 vm_page_laundry_count
--;
1504 mem
->laundry
= FALSE
; /* laundry is now clear */
1505 counter(++c_laundry_pages_freed
);
1506 if (vm_page_laundry_count
< vm_page_laundry_min
) {
1507 vm_page_laundry_min
= 0;
1508 thread_wakeup((event_t
) &vm_page_laundry_count
);
1512 mem
->discard_request
= FALSE
;
1514 PAGE_WAKEUP(mem
); /* clears wanted */
1517 vm_object_absent_release(object
);
1519 /* Some of these may be unnecessary */
1521 mem
->unlock_request
= 0;
1523 mem
->absent
= FALSE
;
1526 mem
->precious
= FALSE
;
1527 mem
->reference
= FALSE
;
1529 mem
->page_error
= KERN_SUCCESS
;
1532 mem
->private = FALSE
;
1533 mem
->fictitious
= TRUE
;
1534 mem
->phys_page
= vm_page_fictitious_addr
;
1536 if (mem
->fictitious
) {
1537 vm_page_release_fictitious(mem
);
1539 /* depends on the queues lock */
1540 if(mem
->zero_fill
) {
1542 mem
->zero_fill
= FALSE
;
1544 vm_page_init(mem
, mem
->phys_page
);
1545 vm_page_release(mem
);
1552 register vm_page_t mem
)
1554 register vm_page_t nxt
;
1555 register vm_page_t first
= NULL
;
1556 register vm_page_t last
;
1557 register int pg_count
= 0;
1561 nxt
= (vm_page_t
)(mem
->pageq
.next
);
1564 vm_pagein_cluster_unused
++;
1567 extern int vm_page_laundry_min
;
1569 if (!mem
->object
->internal
)
1570 vm_page_burst_count
--;
1571 vm_page_laundry_count
--;
1572 counter(++c_laundry_pages_freed
);
1574 if (vm_page_laundry_count
< vm_page_laundry_min
) {
1575 vm_page_laundry_min
= 0;
1576 thread_wakeup((event_t
) &vm_page_laundry_count
);
1581 PAGE_WAKEUP(mem
); /* clears wanted */
1584 mem
->fictitious
= TRUE
;
1586 if (!mem
->fictitious
) {
1587 /* depends on the queues lock */
1590 vm_page_init(mem
, mem
->phys_page
);
1596 mem
->pageq
.next
= (queue_t
) first
;
1601 mem
->phys_page
= vm_page_fictitious_addr
;
1602 vm_page_release_fictitious(mem
);
1608 mutex_lock(&vm_page_queue_free_lock
);
1610 last
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1611 vm_page_queue_free
= first
;
1613 vm_page_free_count
+= pg_count
;
1615 if ((vm_page_free_wanted
> 0) &&
1616 (vm_page_free_count
>= vm_page_free_reserved
)) {
1617 int available_pages
;
1619 available_pages
= vm_page_free_count
- vm_page_free_reserved
;
1621 if (available_pages
>= vm_page_free_wanted
) {
1622 vm_page_free_wanted
= 0;
1623 thread_wakeup((event_t
) &vm_page_free_count
);
1625 while (available_pages
--) {
1626 vm_page_free_wanted
--;
1627 thread_wakeup_one((event_t
) &vm_page_free_count
);
1631 mutex_unlock(&vm_page_queue_free_lock
);
1639 * Mark this page as wired down by yet
1640 * another map, removing it from paging queues
1643 * The page's object and the page queues must be locked.
1647 register vm_page_t mem
)
1650 // dbgLog(current_act(), mem->offset, mem->object, 1); /* (TEST/DEBUG) */
1654 if (mem
->wire_count
== 0) {
1655 VM_PAGE_QUEUES_REMOVE(mem
);
1656 if (!mem
->private && !mem
->fictitious
&& !mem
->gobbled
)
1657 vm_page_wire_count
++;
1659 vm_page_gobble_count
--;
1660 mem
->gobbled
= FALSE
;
1661 if(mem
->zero_fill
) {
1662 /* depends on the queues lock */
1664 mem
->zero_fill
= FALSE
;
1667 assert(!mem
->gobbled
);
1674 * Mark this page as consumed by the vm/ipc/xmm subsystems.
1676 * Called only for freshly vm_page_grab()ed pages - w/ nothing locked.
1680 register vm_page_t mem
)
1682 vm_page_lock_queues();
1685 assert(!mem
->gobbled
);
1686 assert(mem
->wire_count
== 0);
1688 if (!mem
->gobbled
&& mem
->wire_count
== 0) {
1689 if (!mem
->private && !mem
->fictitious
)
1690 vm_page_wire_count
++;
1692 vm_page_gobble_count
++;
1693 mem
->gobbled
= TRUE
;
1694 vm_page_unlock_queues();
1700 * Release one wiring of this page, potentially
1701 * enabling it to be paged again.
1703 * The page's object and the page queues must be locked.
1707 register vm_page_t mem
)
1710 // dbgLog(current_act(), mem->offset, mem->object, 0); /* (TEST/DEBUG) */
1713 assert(mem
->wire_count
> 0);
1715 if (--mem
->wire_count
== 0) {
1716 assert(!mem
->private && !mem
->fictitious
);
1717 vm_page_wire_count
--;
1718 queue_enter(&vm_page_queue_active
, mem
, vm_page_t
, pageq
);
1719 vm_page_active_count
++;
1721 mem
->reference
= TRUE
;
1726 * vm_page_deactivate:
1728 * Returns the given page to the inactive list,
1729 * indicating that no physical maps have access
1730 * to this page. [Used by the physical mapping system.]
1732 * The page queues must be locked.
1736 register vm_page_t m
)
1740 // dbgLog(m->phys_page, vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
1743 * This page is no longer very interesting. If it was
1744 * interesting (active or inactive/referenced), then we
1745 * clear the reference bit and (re)enter it in the
1746 * inactive queue. Note wired pages should not have
1747 * their reference bit cleared.
1749 if (m
->gobbled
) { /* can this happen? */
1750 assert(m
->wire_count
== 0);
1751 if (!m
->private && !m
->fictitious
)
1752 vm_page_wire_count
--;
1753 vm_page_gobble_count
--;
1756 if (m
->private || (m
->wire_count
!= 0))
1758 if (m
->active
|| (m
->inactive
&& m
->reference
)) {
1759 if (!m
->fictitious
&& !m
->absent
)
1760 pmap_clear_reference(m
->phys_page
);
1761 m
->reference
= FALSE
;
1762 VM_PAGE_QUEUES_REMOVE(m
);
1764 if (m
->wire_count
== 0 && !m
->inactive
) {
1765 m
->page_ticket
= vm_page_ticket
;
1766 vm_page_ticket_roll
++;
1768 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
1769 vm_page_ticket_roll
= 0;
1770 if(vm_page_ticket
== VM_PAGE_TICKET_ROLL_IDS
)
1777 queue_enter(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1779 queue_enter(&vm_page_queue_inactive
,
1780 m
, vm_page_t
, pageq
);
1785 vm_page_inactive_count
++;
1792 * Put the specified page on the active list (if appropriate).
1794 * The page queues must be locked.
1799 register vm_page_t m
)
1804 assert(m
->wire_count
== 0);
1805 if (!m
->private && !m
->fictitious
)
1806 vm_page_wire_count
--;
1807 vm_page_gobble_count
--;
1815 queue_remove(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1817 queue_remove(&vm_page_queue_inactive
,
1818 m
, vm_page_t
, pageq
);
1821 vm_page_inactive_count
--;
1822 m
->inactive
= FALSE
;
1824 if (m
->wire_count
== 0) {
1826 panic("vm_page_activate: already active");
1828 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
1830 m
->reference
= TRUE
;
1832 vm_page_active_count
++;
1837 * vm_page_part_zero_fill:
1839 * Zero-fill a part of the page.
1842 vm_page_part_zero_fill(
1850 #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
1851 pmap_zero_part_page(m
->phys_page
, m_pa
, len
);
1854 tmp
= vm_page_grab();
1855 if (tmp
== VM_PAGE_NULL
) {
1856 vm_page_wait(THREAD_UNINT
);
1861 vm_page_zero_fill(tmp
);
1863 vm_page_part_copy(m
, 0, tmp
, 0, m_pa
);
1865 if((m_pa
+ len
) < PAGE_SIZE
) {
1866 vm_page_part_copy(m
, m_pa
+ len
, tmp
,
1867 m_pa
+ len
, PAGE_SIZE
- (m_pa
+ len
));
1869 vm_page_copy(tmp
,m
);
1870 vm_page_lock_queues();
1872 vm_page_unlock_queues();
1878 * vm_page_zero_fill:
1880 * Zero-fill the specified page.
1887 "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
1888 (integer_t
)m
->object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
1892 // dbgTrace(0xAEAEAEAE, m->phys_page, 0); /* (BRINGUP) */
1893 pmap_zero_page(m
->phys_page
);
1897 * vm_page_part_copy:
1899 * copy part of one page to another
1910 VM_PAGE_CHECK(src_m
);
1911 VM_PAGE_CHECK(dst_m
);
1913 pmap_copy_part_page(src_m
->phys_page
, src_pa
,
1914 dst_m
->phys_page
, dst_pa
, len
);
1920 * Copy one page to another
1929 "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
1930 (integer_t
)src_m
->object
, src_m
->offset
,
1931 (integer_t
)dest_m
->object
, dest_m
->offset
,
1934 VM_PAGE_CHECK(src_m
);
1935 VM_PAGE_CHECK(dest_m
);
1937 pmap_copy_page(src_m
->phys_page
, dest_m
->phys_page
);
1941 * Currently, this is a primitive allocator that grabs
1942 * free pages from the system, sorts them by physical
1943 * address, then searches for a region large enough to
1944 * satisfy the user's request.
1946 * Additional levels of effort:
1947 * + steal clean active/inactive pages
1948 * + force pageouts of dirty pages
1949 * + maintain a map of available physical
1953 #define SET_NEXT_PAGE(m,n) ((m)->pageq.next = (struct queue_entry *) (n))
1956 int vm_page_verify_contiguous(
1958 unsigned int npages
);
1959 #endif /* MACH_ASSERT */
1961 cpm_counter(unsigned int vpfls_pages_handled
= 0;)
1962 cpm_counter(unsigned int vpfls_head_insertions
= 0;)
1963 cpm_counter(unsigned int vpfls_tail_insertions
= 0;)
1964 cpm_counter(unsigned int vpfls_general_insertions
= 0;)
1965 cpm_counter(unsigned int vpfc_failed
= 0;)
1966 cpm_counter(unsigned int vpfc_satisfied
= 0;)
1969 * Sort free list by ascending physical address,
1970 * using a not-particularly-bright sort algorithm.
1971 * Caller holds vm_page_queue_free_lock.
1974 vm_page_free_list_sort(void)
1976 vm_page_t sort_list
;
1977 vm_page_t sort_list_end
;
1978 vm_page_t m
, m1
, *prev
, next_m
;
1981 unsigned int npages
;
1983 #endif /* MACH_ASSERT */
1987 * Verify pages in the free list..
1990 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
))
1992 if (npages
!= vm_page_free_count
)
1993 panic("vm_sort_free_list: prelim: npages %d free_count %d",
1994 npages
, vm_page_free_count
);
1995 old_free_count
= vm_page_free_count
;
1996 #endif /* MACH_ASSERT */
1998 sort_list
= sort_list_end
= vm_page_queue_free
;
1999 m
= NEXT_PAGE(vm_page_queue_free
);
2000 SET_NEXT_PAGE(vm_page_queue_free
, VM_PAGE_NULL
);
2001 cpm_counter(vpfls_pages_handled
= 0);
2002 while (m
!= VM_PAGE_NULL
) {
2003 cpm_counter(++vpfls_pages_handled
);
2004 next_m
= NEXT_PAGE(m
);
2005 if (m
->phys_page
< sort_list
->phys_page
) {
2006 cpm_counter(++vpfls_head_insertions
);
2007 SET_NEXT_PAGE(m
, sort_list
);
2009 } else if (m
->phys_page
> sort_list_end
->phys_page
) {
2010 cpm_counter(++vpfls_tail_insertions
);
2011 SET_NEXT_PAGE(sort_list_end
, m
);
2012 SET_NEXT_PAGE(m
, VM_PAGE_NULL
);
2015 cpm_counter(++vpfls_general_insertions
);
2016 /* general sorted list insertion */
2018 for (m1
=sort_list
; m1
!=VM_PAGE_NULL
; m1
=NEXT_PAGE(m1
)) {
2019 if (m1
->phys_page
> m
->phys_page
) {
2021 panic("vm_sort_free_list: ugh");
2022 SET_NEXT_PAGE(m
, *prev
);
2026 prev
= (vm_page_t
*) &m1
->pageq
.next
;
2034 * Verify that pages are sorted into ascending order.
2036 for (m
= sort_list
, npages
= 0; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2037 if (m
!= sort_list
&&
2038 m
->phys_page
<= addr
) {
2039 printf("m 0x%x addr 0x%x\n", m
, addr
);
2040 panic("vm_sort_free_list");
2042 addr
= m
->phys_page
;
2045 if (old_free_count
!= vm_page_free_count
)
2046 panic("vm_sort_free_list: old_free %d free_count %d",
2047 old_free_count
, vm_page_free_count
);
2048 if (npages
!= vm_page_free_count
)
2049 panic("vm_sort_free_list: npages %d free_count %d",
2050 npages
, vm_page_free_count
);
2051 #endif /* MACH_ASSERT */
2053 vm_page_queue_free
= sort_list
;
2059 * Check that the list of pages is ordered by
2060 * ascending physical address and has no holes.
2063 vm_page_verify_contiguous(
2065 unsigned int npages
)
2067 register vm_page_t m
;
2068 unsigned int page_count
;
2069 vm_offset_t prev_addr
;
2071 prev_addr
= pages
->phys_page
;
2073 for (m
= NEXT_PAGE(pages
); m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2074 if (m
->phys_page
!= prev_addr
+ 1) {
2075 printf("m 0x%x prev_addr 0x%x, current addr 0x%x\n",
2076 m
, prev_addr
, m
->phys_page
);
2077 printf("pages 0x%x page_count %d\n", pages
, page_count
);
2078 panic("vm_page_verify_contiguous: not contiguous!");
2080 prev_addr
= m
->phys_page
;
2083 if (page_count
!= npages
) {
2084 printf("pages 0x%x actual count 0x%x but requested 0x%x\n",
2085 pages
, page_count
, npages
);
2086 panic("vm_page_verify_contiguous: count error");
2090 #endif /* MACH_ASSERT */
2094 * Find a region large enough to contain at least npages
2095 * of contiguous physical memory.
2098 * - Called while holding vm_page_queue_free_lock.
2099 * - Doesn't respect vm_page_free_reserved; caller
2100 * must not ask for more pages than are legal to grab.
2102 * Returns a pointer to a list of gobbled pages or VM_PAGE_NULL.
2106 vm_page_find_contiguous(
2109 vm_page_t m
, *contig_prev
, *prev_ptr
;
2111 unsigned int contig_npages
;
2115 return VM_PAGE_NULL
;
2117 prev_page
= vm_page_queue_free
->phys_page
- 2;
2118 prev_ptr
= &vm_page_queue_free
;
2119 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2121 if (m
->phys_page
!= prev_page
+ 1) {
2123 * Whoops! Pages aren't contiguous. Start over.
2126 contig_prev
= prev_ptr
;
2129 if (++contig_npages
== npages
) {
2131 * Chop these pages out of the free list.
2132 * Mark them all as gobbled.
2134 list
= *contig_prev
;
2135 *contig_prev
= NEXT_PAGE(m
);
2136 SET_NEXT_PAGE(m
, VM_PAGE_NULL
);
2137 for (m
= list
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2144 vm_page_free_count
-= npages
;
2145 if (vm_page_free_count
< vm_page_free_count_minimum
)
2146 vm_page_free_count_minimum
= vm_page_free_count
;
2147 vm_page_wire_count
+= npages
;
2148 vm_page_gobble_count
+= npages
;
2149 cpm_counter(++vpfc_satisfied
);
2150 assert(vm_page_verify_contiguous(list
, contig_npages
));
2154 assert(contig_npages
< npages
);
2155 prev_ptr
= (vm_page_t
*) &m
->pageq
.next
;
2156 prev_page
= m
->phys_page
;
2158 cpm_counter(++vpfc_failed
);
2159 return VM_PAGE_NULL
;
2163 * Allocate a list of contiguous, wired pages.
2171 register vm_page_t m
;
2172 vm_page_t
*first_contig
;
2173 vm_page_t free_list
, pages
;
2174 unsigned int npages
, n1pages
;
2175 int vm_pages_available
;
2177 if (size
% page_size
!= 0)
2178 return KERN_INVALID_ARGUMENT
;
2180 vm_page_lock_queues();
2181 mutex_lock(&vm_page_queue_free_lock
);
2184 * Should also take active and inactive pages
2185 * into account... One day...
2187 vm_pages_available
= vm_page_free_count
- vm_page_free_reserved
;
2189 if (size
> vm_pages_available
* page_size
) {
2190 mutex_unlock(&vm_page_queue_free_lock
);
2191 return KERN_RESOURCE_SHORTAGE
;
2194 vm_page_free_list_sort();
2196 npages
= size
/ page_size
;
2199 * Obtain a pointer to a subset of the free
2200 * list large enough to satisfy the request;
2201 * the region will be physically contiguous.
2203 pages
= vm_page_find_contiguous(npages
);
2204 if (pages
== VM_PAGE_NULL
) {
2205 mutex_unlock(&vm_page_queue_free_lock
);
2206 vm_page_unlock_queues();
2207 return KERN_NO_SPACE
;
2210 mutex_unlock(&vm_page_queue_free_lock
);
2213 * Walk the returned list, wiring the pages.
2216 for (m
= pages
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2218 * Essentially inlined vm_page_wire.
2221 assert(!m
->inactive
);
2222 assert(!m
->private);
2223 assert(!m
->fictitious
);
2224 assert(m
->wire_count
== 0);
2228 --vm_page_gobble_count
;
2230 vm_page_unlock_queues();
2233 * The CPM pages should now be available and
2234 * ordered by ascending physical address.
2236 assert(vm_page_verify_contiguous(pages
, npages
));
2239 return KERN_SUCCESS
;
2243 #include <mach_vm_debug.h>
2246 #include <mach_debug/hash_info.h>
2247 #include <vm/vm_debug.h>
2250 * Routine: vm_page_info
2252 * Return information about the global VP table.
2253 * Fills the buffer with as much information as possible
2254 * and returns the desired size of the buffer.
2256 * Nothing locked. The caller should provide
2257 * possibly-pageable memory.
2262 hash_info_bucket_t
*info
,
2267 if (vm_page_bucket_count
< count
)
2268 count
= vm_page_bucket_count
;
2270 for (i
= 0; i
< count
; i
++) {
2271 vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
2272 unsigned int bucket_count
= 0;
2275 simple_lock(&vm_page_bucket_lock
);
2276 for (m
= bucket
->pages
; m
!= VM_PAGE_NULL
; m
= m
->next
)
2278 simple_unlock(&vm_page_bucket_lock
);
2280 /* don't touch pageable memory while holding locks */
2281 info
[i
].hib_count
= bucket_count
;
2284 return vm_page_bucket_count
;
2286 #endif /* MACH_VM_DEBUG */
2288 #include <mach_kdb.h>
2291 #include <ddb/db_output.h>
2292 #include <vm/vm_print.h>
2293 #define printf kdbprintf
2296 * Routine: vm_page_print [exported]
2304 iprintf("page 0x%x\n", p
);
2308 iprintf("object=0x%x", p
->object
);
2309 printf(", offset=0x%x", p
->offset
);
2310 printf(", wire_count=%d", p
->wire_count
);
2312 iprintf("%sinactive, %sactive, %sgobbled, %slaundry, %sfree, %sref, %sdiscard\n",
2313 (p
->inactive
? "" : "!"),
2314 (p
->active
? "" : "!"),
2315 (p
->gobbled
? "" : "!"),
2316 (p
->laundry
? "" : "!"),
2317 (p
->free
? "" : "!"),
2318 (p
->reference
? "" : "!"),
2319 (p
->discard_request
? "" : "!"));
2320 iprintf("%sbusy, %swanted, %stabled, %sfictitious, %sprivate, %sprecious\n",
2321 (p
->busy
? "" : "!"),
2322 (p
->wanted
? "" : "!"),
2323 (p
->tabled
? "" : "!"),
2324 (p
->fictitious
? "" : "!"),
2325 (p
->private ? "" : "!"),
2326 (p
->precious
? "" : "!"));
2327 iprintf("%sabsent, %serror, %sdirty, %scleaning, %spageout, %sclustered\n",
2328 (p
->absent
? "" : "!"),
2329 (p
->error
? "" : "!"),
2330 (p
->dirty
? "" : "!"),
2331 (p
->cleaning
? "" : "!"),
2332 (p
->pageout
? "" : "!"),
2333 (p
->clustered
? "" : "!"));
2334 iprintf("%slock_supplied, %soverwriting, %srestart, %sunusual\n",
2335 (p
->lock_supplied
? "" : "!"),
2336 (p
->overwriting
? "" : "!"),
2337 (p
->restart
? "" : "!"),
2338 (p
->unusual
? "" : "!"));
2340 iprintf("phys_page=0x%x", p
->phys_page
);
2341 printf(", page_error=0x%x", p
->page_error
);
2342 printf(", page_lock=0x%x", p
->page_lock
);
2343 printf(", unlock_request=%d\n", p
->unlock_request
);
2347 #endif /* MACH_KDB */