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;
271 * The VM system has a couple of heuristics for deciding
272 * that pages are "uninteresting" and should be placed
273 * on the inactive queue as likely candidates for replacement.
274 * These variables let the heuristics be controlled at run-time
275 * to make experimentation easier.
278 boolean_t vm_page_deactivate_hint
= TRUE
;
283 * Sets the page size, perhaps based upon the memory
284 * size. Must be called before any use of page-size
285 * dependent functions.
287 * Sets page_shift and page_mask from page_size.
290 vm_set_page_size(void)
292 #ifndef PAGE_SIZE_FIXED
293 page_mask
= page_size
- 1;
295 if ((page_mask
& page_size
) != 0)
296 panic("vm_set_page_size: page size not a power of two");
298 for (page_shift
= 0; ; page_shift
++)
299 if ((1 << page_shift
) == page_size
)
301 #endif /* PAGE_SIZE_FIXED */
307 * Initializes the resident memory module.
309 * Allocates memory for the page cells, and
310 * for the object/offset-to-page hash table headers.
311 * Each page cell is initialized and placed on the free list.
312 * Returns the range of available kernel virtual memory.
320 register vm_page_t m
;
327 * Initialize the vm_page template.
330 m
= &vm_page_template
;
331 m
->object
= VM_OBJECT_NULL
; /* reset later */
332 m
->offset
= 0; /* reset later */
340 m
->reference
= FALSE
;
342 m
->dump_cleaning
= FALSE
;
343 m
->list_req_pending
= FALSE
;
348 m
->fictitious
= FALSE
;
355 m
->clustered
= FALSE
;
356 m
->lock_supplied
= FALSE
;
359 m
->zero_fill
= FALSE
;
361 m
->phys_page
= 0; /* reset later */
363 m
->page_lock
= VM_PROT_NONE
;
364 m
->unlock_request
= VM_PROT_NONE
;
365 m
->page_error
= KERN_SUCCESS
;
368 * Initialize the page queues.
371 mutex_init(&vm_page_queue_free_lock
, ETAP_VM_PAGEQ_FREE
);
372 mutex_init(&vm_page_queue_lock
, ETAP_VM_PAGEQ
);
373 simple_lock_init(&vm_page_preppin_lock
, ETAP_VM_PREPPIN
);
375 vm_page_queue_free
= VM_PAGE_NULL
;
376 vm_page_queue_fictitious
= VM_PAGE_NULL
;
377 queue_init(&vm_page_queue_active
);
378 queue_init(&vm_page_queue_inactive
);
379 queue_init(&vm_page_queue_zf
);
380 queue_init(&vm_page_queue_limbo
);
382 vm_page_free_wanted
= 0;
385 * Steal memory for the map and zone subsystems.
388 vm_map_steal_memory();
392 * Allocate (and initialize) the virtual-to-physical
393 * table hash buckets.
395 * The number of buckets should be a power of two to
396 * get a good hash function. The following computation
397 * chooses the first power of two that is greater
398 * than the number of physical pages in the system.
401 simple_lock_init(&vm_page_bucket_lock
, ETAP_VM_BUCKET
);
403 if (vm_page_bucket_count
== 0) {
404 unsigned int npages
= pmap_free_pages();
406 vm_page_bucket_count
= 1;
407 while (vm_page_bucket_count
< npages
)
408 vm_page_bucket_count
<<= 1;
411 vm_page_hash_mask
= vm_page_bucket_count
- 1;
414 * Calculate object shift value for hashing algorithm:
415 * O = log2(sizeof(struct vm_object))
416 * B = log2(vm_page_bucket_count)
417 * hash shifts the object left by
420 size
= vm_page_bucket_count
;
421 for (log1
= 0; size
> 1; log1
++)
423 size
= sizeof(struct vm_object
);
424 for (log2
= 0; size
> 1; log2
++)
426 vm_page_hash_shift
= log1
/2 - log2
+ 1;
428 vm_page_bucket_hash
= 1 << ((log1
+ 1) >> 1); /* Get (ceiling of sqrt of table size) */
429 vm_page_bucket_hash
|= 1 << ((log1
+ 1) >> 2); /* Get (ceiling of quadroot of table size) */
430 vm_page_bucket_hash
|= 1; /* Set bit and add 1 - always must be 1 to insure unique series */
432 if (vm_page_hash_mask
& vm_page_bucket_count
)
433 printf("vm_page_bootstrap: WARNING -- strange page hash\n");
435 vm_page_buckets
= (vm_page_bucket_t
*)
436 pmap_steal_memory(vm_page_bucket_count
*
437 sizeof(vm_page_bucket_t
));
439 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
440 register vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
442 bucket
->pages
= VM_PAGE_NULL
;
443 #if MACH_PAGE_HASH_STATS
444 bucket
->cur_count
= 0;
445 bucket
->hi_count
= 0;
446 #endif /* MACH_PAGE_HASH_STATS */
450 * Machine-dependent code allocates the resident page table.
451 * It uses vm_page_init to initialize the page frames.
452 * The code also returns to us the virtual space available
453 * to the kernel. We don't trust the pmap module
454 * to get the alignment right.
457 pmap_startup(&virtual_space_start
, &virtual_space_end
);
458 virtual_space_start
= round_page_32(virtual_space_start
);
459 virtual_space_end
= trunc_page_32(virtual_space_end
);
461 *startp
= virtual_space_start
;
462 *endp
= virtual_space_end
;
465 * Compute the initial "wire" count.
466 * Up until now, the pages which have been set aside are not under
467 * the VM system's control, so although they aren't explicitly
468 * wired, they nonetheless can't be moved. At this moment,
469 * all VM managed pages are "free", courtesy of pmap_startup.
471 vm_page_wire_count
= atop_64(max_mem
) - vm_page_free_count
; /* initial value */
473 printf("vm_page_bootstrap: %d free pages\n", vm_page_free_count
);
474 vm_page_free_count_minimum
= vm_page_free_count
;
477 #ifndef MACHINE_PAGES
479 * We implement pmap_steal_memory and pmap_startup with the help
480 * of two simpler functions, pmap_virtual_space and pmap_next_page.
487 vm_offset_t addr
, vaddr
;
491 * We round the size to a round multiple.
494 size
= (size
+ sizeof (void *) - 1) &~ (sizeof (void *) - 1);
497 * If this is the first call to pmap_steal_memory,
498 * we have to initialize ourself.
501 if (virtual_space_start
== virtual_space_end
) {
502 pmap_virtual_space(&virtual_space_start
, &virtual_space_end
);
505 * The initial values must be aligned properly, and
506 * we don't trust the pmap module to do it right.
509 virtual_space_start
= round_page_32(virtual_space_start
);
510 virtual_space_end
= trunc_page_32(virtual_space_end
);
514 * Allocate virtual memory for this request.
517 addr
= virtual_space_start
;
518 virtual_space_start
+= size
;
520 kprintf("pmap_steal_memory: %08X - %08X; size=%08X\n", addr
, virtual_space_start
, size
); /* (TEST/DEBUG) */
523 * Allocate and map physical pages to back new virtual pages.
526 for (vaddr
= round_page_32(addr
);
528 vaddr
+= PAGE_SIZE
) {
529 if (!pmap_next_page(&phys_page
))
530 panic("pmap_steal_memory");
533 * XXX Logically, these mappings should be wired,
534 * but some pmap modules barf if they are.
537 pmap_enter(kernel_pmap
, vaddr
, phys_page
,
538 VM_PROT_READ
|VM_PROT_WRITE
,
539 VM_WIMG_USE_DEFAULT
, FALSE
);
541 * Account for newly stolen memory
543 vm_page_wire_count
++;
555 unsigned int i
, npages
, pages_initialized
, fill
, fillval
;
561 * We calculate how many page frames we will have
562 * and then allocate the page structures in one chunk.
565 tmpaddr
= (addr64_t
)pmap_free_pages() * (addr64_t
)PAGE_SIZE
; /* Get the amount of memory left */
566 tmpaddr
= tmpaddr
+ (addr64_t
)(round_page_32(virtual_space_start
) - virtual_space_start
); /* Account for any slop */
567 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 */
569 pages
= (vm_page_t
) pmap_steal_memory(npages
* sizeof *pages
);
572 * Initialize the page frames.
575 for (i
= 0, pages_initialized
= 0; i
< npages
; i
++) {
576 if (!pmap_next_page(&phys_page
))
579 vm_page_init(&pages
[i
], phys_page
);
585 * Release pages in reverse order so that physical pages
586 * initially get allocated in ascending addresses. This keeps
587 * the devices (which must address physical memory) happy if
588 * they require several consecutive pages.
592 * Check if we want to initialize pages to a known value
595 fill
= 0; /* Assume no fill */
596 if (PE_parse_boot_arg("fill", &fillval
)) fill
= 1; /* Set fill */
598 for (i
= pages_initialized
; i
> 0; i
--) {
599 extern void fillPage(ppnum_t phys_page
, unsigned int fillval
);
600 if(fill
) fillPage(pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
601 vm_page_release(&pages
[i
- 1]);
606 vm_page_t xx
, xxo
, xxl
;
609 j
= 0; /* (BRINGUP) */
612 for(xx
= vm_page_queue_free
; xx
; xxl
= xx
, xx
= xx
->pageq
.next
) { /* (BRINGUP) */
614 if(j
> vm_page_free_count
) { /* (BRINGUP) */
615 panic("pmap_startup: too many pages, xx = %08X, xxl = %08X\n", xx
, xxl
);
618 l
= vm_page_free_count
- j
; /* (BRINGUP) */
619 k
= 0; /* (BRINGUP) */
621 if(((j
- 1) & 0xFFFF) == 0) kprintf("checking number %d of %d\n", j
, vm_page_free_count
);
623 for(xxo
= xx
->pageq
.next
; xxo
; xxo
= xxo
->pageq
.next
) { /* (BRINGUP) */
625 if(k
> l
) panic("pmap_startup: too many in secondary check %d %d\n", k
, l
);
626 if((xx
->phys_page
& 0xFFFFFFFF) == (xxo
->phys_page
& 0xFFFFFFFF)) { /* (BRINGUP) */
627 panic("pmap_startup: duplicate physaddr, xx = %08X, xxo = %08X\n", xx
, xxo
);
632 if(j
!= vm_page_free_count
) { /* (BRINGUP) */
633 panic("pmap_startup: vm_page_free_count does not match, calc = %d, vm_page_free_count = %08X\n", j
, vm_page_free_count
);
640 * We have to re-align virtual_space_start,
641 * because pmap_steal_memory has been using it.
644 virtual_space_start
= round_page_32(virtual_space_start
);
646 *startp
= virtual_space_start
;
647 *endp
= virtual_space_end
;
649 #endif /* MACHINE_PAGES */
652 * Routine: vm_page_module_init
654 * Second initialization pass, to be done after
655 * the basic VM system is ready.
658 vm_page_module_init(void)
660 vm_page_zone
= zinit((vm_size_t
) sizeof(struct vm_page
),
661 0, PAGE_SIZE
, "vm pages");
664 zone_debug_disable(vm_page_zone
);
665 #endif /* ZONE_DEBUG */
667 zone_change(vm_page_zone
, Z_EXPAND
, FALSE
);
668 zone_change(vm_page_zone
, Z_EXHAUST
, TRUE
);
669 zone_change(vm_page_zone
, Z_FOREIGN
, TRUE
);
672 * Adjust zone statistics to account for the real pages allocated
673 * in vm_page_create(). [Q: is this really what we want?]
675 vm_page_zone
->count
+= vm_page_pages
;
676 vm_page_zone
->cur_size
+= vm_page_pages
* vm_page_zone
->elem_size
;
678 mutex_init(&vm_page_alloc_lock
, ETAP_VM_PAGE_ALLOC
);
679 mutex_init(&vm_page_zero_fill_lock
, ETAP_VM_PAGE_ALLOC
);
683 * Routine: vm_page_create
685 * After the VM system is up, machine-dependent code
686 * may stumble across more physical memory. For example,
687 * memory that it was reserving for a frame buffer.
688 * vm_page_create turns this memory into available pages.
699 for (phys_page
= start
;
702 while ((m
= (vm_page_t
) vm_page_grab_fictitious())
704 vm_page_more_fictitious();
706 vm_page_init(m
, phys_page
);
715 * Distributes the object/offset key pair among hash buckets.
717 * NOTE: The bucket count must be a power of 2
719 #define vm_page_hash(object, offset) (\
720 ( (natural_t)((uint32_t)object * vm_page_bucket_hash) + ((uint32_t)atop_64(offset) ^ vm_page_bucket_hash))\
724 * vm_page_insert: [ internal use only ]
726 * Inserts the given mem entry into the object/object-page
727 * table and object list.
729 * The object must be locked.
734 register vm_page_t mem
,
735 register vm_object_t object
,
736 register vm_object_offset_t offset
)
738 register vm_page_bucket_t
*bucket
;
741 "vm_page_insert, object 0x%X offset 0x%X page 0x%X\n",
742 (integer_t
)object
, (integer_t
)offset
, (integer_t
)mem
, 0,0);
747 panic("vm_page_insert");
749 assert(!object
->internal
|| offset
< object
->size
);
751 /* only insert "pageout" pages into "pageout" objects,
752 * and normal pages into normal objects */
753 assert(object
->pageout
== mem
->pageout
);
756 * Record the object/offset pair in this page
759 mem
->object
= object
;
760 mem
->offset
= offset
;
763 * Insert it into the object_object/offset hash table
766 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
767 simple_lock(&vm_page_bucket_lock
);
768 mem
->next
= bucket
->pages
;
770 #if MACH_PAGE_HASH_STATS
771 if (++bucket
->cur_count
> bucket
->hi_count
)
772 bucket
->hi_count
= bucket
->cur_count
;
773 #endif /* MACH_PAGE_HASH_STATS */
774 simple_unlock(&vm_page_bucket_lock
);
777 * Now link into the object's list of backed pages.
780 queue_enter(&object
->memq
, mem
, vm_page_t
, listq
);
784 * Show that the object has one more resident page.
787 object
->resident_page_count
++;
793 * Exactly like vm_page_insert, except that we first
794 * remove any existing page at the given offset in object.
796 * The object and page queues must be locked.
801 register vm_page_t mem
,
802 register vm_object_t object
,
803 register vm_object_offset_t offset
)
805 register vm_page_bucket_t
*bucket
;
810 panic("vm_page_replace");
813 * Record the object/offset pair in this page
816 mem
->object
= object
;
817 mem
->offset
= offset
;
820 * Insert it into the object_object/offset hash table,
821 * replacing any page that might have been there.
824 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
825 simple_lock(&vm_page_bucket_lock
);
827 vm_page_t
*mp
= &bucket
->pages
;
828 register vm_page_t m
= *mp
;
830 if (m
->object
== object
&& m
->offset
== offset
) {
832 * Remove page from bucket and from object,
833 * and return it to the free list.
836 queue_remove(&object
->memq
, m
, vm_page_t
,
839 object
->resident_page_count
--;
842 * Return page to the free list.
843 * Note the page is not tabled now, so this
844 * won't self-deadlock on the bucket lock.
852 mem
->next
= bucket
->pages
;
854 mem
->next
= VM_PAGE_NULL
;
857 simple_unlock(&vm_page_bucket_lock
);
860 * Now link into the object's list of backed pages.
863 queue_enter(&object
->memq
, mem
, vm_page_t
, listq
);
867 * And show that the object has one more resident
871 object
->resident_page_count
++;
875 * vm_page_remove: [ internal use only ]
877 * Removes the given mem entry from the object/offset-page
878 * table and the object page list.
880 * The object and page must be locked.
885 register vm_page_t mem
)
887 register vm_page_bucket_t
*bucket
;
888 register vm_page_t
this;
891 "vm_page_remove, object 0x%X offset 0x%X page 0x%X\n",
892 (integer_t
)mem
->object
, (integer_t
)mem
->offset
,
893 (integer_t
)mem
, 0,0);
896 assert(!mem
->cleaning
);
900 * Remove from the object_object/offset hash table
903 bucket
= &vm_page_buckets
[vm_page_hash(mem
->object
, mem
->offset
)];
904 simple_lock(&vm_page_bucket_lock
);
905 if ((this = bucket
->pages
) == mem
) {
906 /* optimize for common case */
908 bucket
->pages
= mem
->next
;
910 register vm_page_t
*prev
;
912 for (prev
= &this->next
;
913 (this = *prev
) != mem
;
918 #if MACH_PAGE_HASH_STATS
920 #endif /* MACH_PAGE_HASH_STATS */
921 simple_unlock(&vm_page_bucket_lock
);
924 * Now remove from the object's list of backed pages.
927 queue_remove(&mem
->object
->memq
, mem
, vm_page_t
, listq
);
930 * And show that the object has one fewer resident
934 mem
->object
->resident_page_count
--;
937 mem
->object
= VM_OBJECT_NULL
;
944 * Returns the page associated with the object/offset
945 * pair specified; if none is found, VM_PAGE_NULL is returned.
947 * The object must be locked. No side effects.
952 register vm_object_t object
,
953 register vm_object_offset_t offset
)
955 register vm_page_t mem
;
956 register vm_page_bucket_t
*bucket
;
959 * Search the hash table for this object/offset pair
962 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
964 simple_lock(&vm_page_bucket_lock
);
965 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
967 if ((mem
->object
== object
) && (mem
->offset
== offset
))
970 simple_unlock(&vm_page_bucket_lock
);
978 * Move the given memory entry from its
979 * current object to the specified target object/offset.
981 * The object must be locked.
985 register vm_page_t mem
,
986 register vm_object_t new_object
,
987 vm_object_offset_t new_offset
)
989 assert(mem
->object
!= new_object
);
991 * Changes to mem->object require the page lock because
992 * the pageout daemon uses that lock to get the object.
996 "vm_page_rename, new object 0x%X, offset 0x%X page 0x%X\n",
997 (integer_t
)new_object
, (integer_t
)new_offset
,
998 (integer_t
)mem
, 0,0);
1000 vm_page_lock_queues();
1001 vm_page_remove(mem
);
1002 vm_page_insert(mem
, new_object
, new_offset
);
1003 vm_page_unlock_queues();
1009 * Initialize the fields in a new page.
1010 * This takes a structure with random values and initializes it
1011 * so that it can be given to vm_page_release or vm_page_insert.
1018 *mem
= vm_page_template
;
1019 mem
->phys_page
= phys_page
;
1023 * vm_page_grab_fictitious:
1025 * Remove a fictitious page from the free list.
1026 * Returns VM_PAGE_NULL if there are no free pages.
1028 int c_vm_page_grab_fictitious
= 0;
1029 int c_vm_page_release_fictitious
= 0;
1030 int c_vm_page_more_fictitious
= 0;
1033 vm_page_grab_fictitious(void)
1035 register vm_page_t m
;
1037 m
= (vm_page_t
)zget(vm_page_zone
);
1039 vm_page_init(m
, vm_page_fictitious_addr
);
1040 m
->fictitious
= TRUE
;
1043 c_vm_page_grab_fictitious
++;
1048 * vm_page_release_fictitious:
1050 * Release a fictitious page to the free list.
1054 vm_page_release_fictitious(
1055 register vm_page_t m
)
1059 assert(m
->fictitious
);
1060 assert(m
->phys_page
== vm_page_fictitious_addr
);
1062 c_vm_page_release_fictitious
++;
1065 panic("vm_page_release_fictitious");
1067 zfree(vm_page_zone
, (vm_offset_t
)m
);
1071 * vm_page_more_fictitious:
1073 * Add more fictitious pages to the free list.
1074 * Allowed to block. This routine is way intimate
1075 * with the zones code, for several reasons:
1076 * 1. we need to carve some page structures out of physical
1077 * memory before zones work, so they _cannot_ come from
1079 * 2. the zone needs to be collectable in order to prevent
1080 * growth without bound. These structures are used by
1081 * the device pager (by the hundreds and thousands), as
1082 * private pages for pageout, and as blocking pages for
1083 * pagein. Temporary bursts in demand should not result in
1084 * permanent allocation of a resource.
1085 * 3. To smooth allocation humps, we allocate single pages
1086 * with kernel_memory_allocate(), and cram them into the
1087 * zone. This also allows us to initialize the vm_page_t's
1088 * on the way into the zone, so that zget() always returns
1089 * an initialized structure. The zone free element pointer
1090 * and the free page pointer are both the first item in the
1092 * 4. By having the pages in the zone pre-initialized, we need
1093 * not keep 2 levels of lists. The garbage collector simply
1094 * scans our list, and reduces physical memory usage as it
1098 void vm_page_more_fictitious(void)
1100 extern vm_map_t zone_map
;
1101 register vm_page_t m
;
1103 kern_return_t retval
;
1106 c_vm_page_more_fictitious
++;
1109 * Allocate a single page from the zone_map. Do not wait if no physical
1110 * pages are immediately available, and do not zero the space. We need
1111 * our own blocking lock here to prevent having multiple,
1112 * simultaneous requests from piling up on the zone_map lock. Exactly
1113 * one (of our) threads should be potentially waiting on the map lock.
1114 * If winner is not vm-privileged, then the page allocation will fail,
1115 * and it will temporarily block here in the vm_page_wait().
1117 mutex_lock(&vm_page_alloc_lock
);
1119 * If another thread allocated space, just bail out now.
1121 if (zone_free_count(vm_page_zone
) > 5) {
1123 * The number "5" is a small number that is larger than the
1124 * number of fictitious pages that any single caller will
1125 * attempt to allocate. Otherwise, a thread will attempt to
1126 * acquire a fictitious page (vm_page_grab_fictitious), fail,
1127 * release all of the resources and locks already acquired,
1128 * and then call this routine. This routine finds the pages
1129 * that the caller released, so fails to allocate new space.
1130 * The process repeats infinitely. The largest known number
1131 * of fictitious pages required in this manner is 2. 5 is
1132 * simply a somewhat larger number.
1134 mutex_unlock(&vm_page_alloc_lock
);
1138 if ((retval
= kernel_memory_allocate(zone_map
,
1139 &addr
, PAGE_SIZE
, VM_PROT_ALL
,
1140 KMA_KOBJECT
|KMA_NOPAGEWAIT
)) != KERN_SUCCESS
) {
1142 * No page was available. Tell the pageout daemon, drop the
1143 * lock to give another thread a chance at it, and
1144 * wait for the pageout daemon to make progress.
1146 mutex_unlock(&vm_page_alloc_lock
);
1147 vm_page_wait(THREAD_UNINT
);
1151 * Initialize as many vm_page_t's as will fit on this page. This
1152 * depends on the zone code disturbing ONLY the first item of
1153 * each zone element.
1155 m
= (vm_page_t
)addr
;
1156 for (i
= PAGE_SIZE
/sizeof(struct vm_page
); i
> 0; i
--) {
1157 vm_page_init(m
, vm_page_fictitious_addr
);
1158 m
->fictitious
= TRUE
;
1161 zcram(vm_page_zone
, addr
, PAGE_SIZE
);
1162 mutex_unlock(&vm_page_alloc_lock
);
1168 * Attempt to convert a fictitious page into a real page.
1173 register vm_page_t m
)
1175 register vm_page_t real_m
;
1178 assert(m
->fictitious
);
1181 real_m
= vm_page_grab();
1182 if (real_m
== VM_PAGE_NULL
)
1185 m
->phys_page
= real_m
->phys_page
;
1186 m
->fictitious
= FALSE
;
1189 vm_page_lock_queues();
1191 vm_page_active_count
++;
1192 else if (m
->inactive
)
1193 vm_page_inactive_count
++;
1194 vm_page_unlock_queues();
1196 real_m
->phys_page
= vm_page_fictitious_addr
;
1197 real_m
->fictitious
= TRUE
;
1199 vm_page_release_fictitious(real_m
);
1206 * Return true if it is not likely that a non-vm_privileged thread
1207 * can get memory without blocking. Advisory only, since the
1208 * situation may change under us.
1213 /* No locking, at worst we will fib. */
1214 return( vm_page_free_count
< vm_page_free_reserved
);
1220 * Remove a page from the free list.
1221 * Returns VM_PAGE_NULL if the free list is too small.
1224 unsigned long vm_page_grab_count
= 0; /* measure demand */
1229 register vm_page_t mem
;
1231 mutex_lock(&vm_page_queue_free_lock
);
1232 vm_page_grab_count
++;
1235 * Optionally produce warnings if the wire or gobble
1236 * counts exceed some threshold.
1238 if (vm_page_wire_count_warning
> 0
1239 && vm_page_wire_count
>= vm_page_wire_count_warning
) {
1240 printf("mk: vm_page_grab(): high wired page count of %d\n",
1241 vm_page_wire_count
);
1242 assert(vm_page_wire_count
< vm_page_wire_count_warning
);
1244 if (vm_page_gobble_count_warning
> 0
1245 && vm_page_gobble_count
>= vm_page_gobble_count_warning
) {
1246 printf("mk: vm_page_grab(): high gobbled page count of %d\n",
1247 vm_page_gobble_count
);
1248 assert(vm_page_gobble_count
< vm_page_gobble_count_warning
);
1252 * Only let privileged threads (involved in pageout)
1253 * dip into the reserved pool.
1256 if ((vm_page_free_count
< vm_page_free_reserved
) &&
1257 !current_thread()->vm_privilege
) {
1258 mutex_unlock(&vm_page_queue_free_lock
);
1260 goto wakeup_pageout
;
1263 while (vm_page_queue_free
== VM_PAGE_NULL
) {
1264 printf("vm_page_grab: no free pages, trouble expected...\n");
1265 mutex_unlock(&vm_page_queue_free_lock
);
1267 mutex_lock(&vm_page_queue_free_lock
);
1270 if (--vm_page_free_count
< vm_page_free_count_minimum
)
1271 vm_page_free_count_minimum
= vm_page_free_count
;
1272 mem
= vm_page_queue_free
;
1273 vm_page_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1275 mem
->no_isync
= TRUE
;
1276 mutex_unlock(&vm_page_queue_free_lock
);
1279 * Decide if we should poke the pageout daemon.
1280 * We do this if the free count is less than the low
1281 * water mark, or if the free count is less than the high
1282 * water mark (but above the low water mark) and the inactive
1283 * count is less than its target.
1285 * We don't have the counts locked ... if they change a little,
1286 * it doesn't really matter.
1290 if ((vm_page_free_count
< vm_page_free_min
) ||
1291 ((vm_page_free_count
< vm_page_free_target
) &&
1292 (vm_page_inactive_count
< vm_page_inactive_target
)))
1293 thread_wakeup((event_t
) &vm_page_free_wanted
);
1295 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */
1303 * Return a page to the free list.
1308 register vm_page_t mem
)
1312 unsigned int pindex
;
1313 phys_entry
*physent
;
1315 physent
= mapping_phys_lookup(mem
->phys_page
, &pindex
); /* (BRINGUP) */
1316 if(physent
->ppLink
& ppN
) { /* (BRINGUP) */
1317 panic("vm_page_release: already released - %08X %08X\n", mem
, mem
->phys_page
);
1319 physent
->ppLink
= physent
->ppLink
| ppN
; /* (BRINGUP) */
1322 assert(!mem
->private && !mem
->fictitious
);
1324 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */
1326 mutex_lock(&vm_page_queue_free_lock
);
1328 panic("vm_page_release");
1330 mem
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1331 vm_page_queue_free
= mem
;
1332 vm_page_free_count
++;
1335 * Check if we should wake up someone waiting for page.
1336 * But don't bother waking them unless they can allocate.
1338 * We wakeup only one thread, to prevent starvation.
1339 * Because the scheduling system handles wait queues FIFO,
1340 * if we wakeup all waiting threads, one greedy thread
1341 * can starve multiple niceguy threads. When the threads
1342 * all wakeup, the greedy threads runs first, grabs the page,
1343 * and waits for another page. It will be the first to run
1344 * when the next page is freed.
1346 * However, there is a slight danger here.
1347 * The thread we wake might not use the free page.
1348 * Then the other threads could wait indefinitely
1349 * while the page goes unused. To forestall this,
1350 * the pageout daemon will keep making free pages
1351 * as long as vm_page_free_wanted is non-zero.
1354 if ((vm_page_free_wanted
> 0) &&
1355 (vm_page_free_count
>= vm_page_free_reserved
)) {
1356 vm_page_free_wanted
--;
1357 thread_wakeup_one((event_t
) &vm_page_free_count
);
1360 mutex_unlock(&vm_page_queue_free_lock
);
1363 #define VM_PAGEOUT_DEADLOCK_TIMEOUT 3
1368 * Wait for a page to become available.
1369 * If there are plenty of free pages, then we don't sleep.
1372 * TRUE: There may be another page, try again
1373 * FALSE: We were interrupted out of our wait, don't try again
1381 * We can't use vm_page_free_reserved to make this
1382 * determination. Consider: some thread might
1383 * need to allocate two pages. The first allocation
1384 * succeeds, the second fails. After the first page is freed,
1385 * a call to vm_page_wait must really block.
1388 kern_return_t wait_result
;
1390 int need_wakeup
= 0;
1392 mutex_lock(&vm_page_queue_free_lock
);
1393 if (vm_page_free_count
< vm_page_free_target
) {
1394 if (vm_page_free_wanted
++ == 0)
1396 wait_result
= assert_wait((event_t
)&vm_page_free_count
,
1398 mutex_unlock(&vm_page_queue_free_lock
);
1399 counter(c_vm_page_wait_block
++);
1402 thread_wakeup((event_t
)&vm_page_free_wanted
);
1404 if (wait_result
== THREAD_WAITING
) {
1405 clock_interval_to_absolutetime_interval(
1406 VM_PAGEOUT_DEADLOCK_TIMEOUT
,
1407 NSEC_PER_SEC
, &abstime
);
1408 clock_absolutetime_interval_to_deadline(
1410 thread_set_timer_deadline(abstime
);
1411 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1413 if(wait_result
== THREAD_TIMED_OUT
) {
1414 kr
= vm_pageout_emergency_availability_request();
1417 thread_cancel_timer();
1421 return(wait_result
== THREAD_AWAKENED
);
1423 mutex_unlock(&vm_page_queue_free_lock
);
1431 * Allocate and return a memory cell associated
1432 * with this VM object/offset pair.
1434 * Object must be locked.
1440 vm_object_offset_t offset
)
1442 register vm_page_t mem
;
1444 mem
= vm_page_grab();
1445 if (mem
== VM_PAGE_NULL
)
1446 return VM_PAGE_NULL
;
1448 vm_page_insert(mem
, object
, offset
);
1453 counter(unsigned int c_laundry_pages_freed
= 0;)
1455 int vm_pagein_cluster_unused
= 0;
1456 boolean_t vm_page_free_verify
= FALSE
;
1460 * Returns the given page to the free list,
1461 * disassociating it with any VM object.
1463 * Object and page queues must be locked prior to entry.
1467 register vm_page_t mem
)
1469 vm_object_t object
= mem
->object
;
1472 assert(!mem
->cleaning
);
1473 assert(!mem
->pageout
);
1474 assert(!vm_page_free_verify
|| pmap_verify_free(mem
->phys_page
));
1477 vm_page_remove(mem
); /* clears tabled, object, offset */
1478 VM_PAGE_QUEUES_REMOVE(mem
); /* clears active or inactive */
1480 if (mem
->clustered
) {
1481 mem
->clustered
= FALSE
;
1482 vm_pagein_cluster_unused
++;
1485 if (mem
->wire_count
) {
1486 if (!mem
->private && !mem
->fictitious
)
1487 vm_page_wire_count
--;
1488 mem
->wire_count
= 0;
1489 assert(!mem
->gobbled
);
1490 } else if (mem
->gobbled
) {
1491 if (!mem
->private && !mem
->fictitious
)
1492 vm_page_wire_count
--;
1493 vm_page_gobble_count
--;
1495 mem
->gobbled
= FALSE
;
1498 extern int vm_page_laundry_min
;
1499 vm_page_laundry_count
--;
1500 mem
->laundry
= FALSE
; /* laundry is now clear */
1501 counter(++c_laundry_pages_freed
);
1502 if (vm_page_laundry_count
< vm_page_laundry_min
) {
1503 vm_page_laundry_min
= 0;
1504 thread_wakeup((event_t
) &vm_page_laundry_count
);
1508 mem
->discard_request
= FALSE
;
1510 PAGE_WAKEUP(mem
); /* clears wanted */
1513 vm_object_absent_release(object
);
1515 /* Some of these may be unnecessary */
1517 mem
->unlock_request
= 0;
1519 mem
->absent
= FALSE
;
1522 mem
->precious
= FALSE
;
1523 mem
->reference
= FALSE
;
1525 mem
->page_error
= KERN_SUCCESS
;
1528 mem
->private = FALSE
;
1529 mem
->fictitious
= TRUE
;
1530 mem
->phys_page
= vm_page_fictitious_addr
;
1532 if (mem
->fictitious
) {
1533 vm_page_release_fictitious(mem
);
1535 /* depends on the queues lock */
1536 if(mem
->zero_fill
) {
1538 mem
->zero_fill
= FALSE
;
1540 vm_page_init(mem
, mem
->phys_page
);
1541 vm_page_release(mem
);
1548 * Mark this page as wired down by yet
1549 * another map, removing it from paging queues
1552 * The page's object and the page queues must be locked.
1556 register vm_page_t mem
)
1559 // dbgLog(current_act(), mem->offset, mem->object, 1); /* (TEST/DEBUG) */
1563 if (mem
->wire_count
== 0) {
1564 VM_PAGE_QUEUES_REMOVE(mem
);
1565 if (!mem
->private && !mem
->fictitious
&& !mem
->gobbled
)
1566 vm_page_wire_count
++;
1568 vm_page_gobble_count
--;
1569 mem
->gobbled
= FALSE
;
1570 if(mem
->zero_fill
) {
1571 /* depends on the queues lock */
1573 mem
->zero_fill
= FALSE
;
1576 assert(!mem
->gobbled
);
1583 * Mark this page as consumed by the vm/ipc/xmm subsystems.
1585 * Called only for freshly vm_page_grab()ed pages - w/ nothing locked.
1589 register vm_page_t mem
)
1591 vm_page_lock_queues();
1594 assert(!mem
->gobbled
);
1595 assert(mem
->wire_count
== 0);
1597 if (!mem
->gobbled
&& mem
->wire_count
== 0) {
1598 if (!mem
->private && !mem
->fictitious
)
1599 vm_page_wire_count
++;
1601 vm_page_gobble_count
++;
1602 mem
->gobbled
= TRUE
;
1603 vm_page_unlock_queues();
1609 * Release one wiring of this page, potentially
1610 * enabling it to be paged again.
1612 * The page's object and the page queues must be locked.
1616 register vm_page_t mem
)
1619 // dbgLog(current_act(), mem->offset, mem->object, 0); /* (TEST/DEBUG) */
1622 assert(mem
->wire_count
> 0);
1624 if (--mem
->wire_count
== 0) {
1625 assert(!mem
->private && !mem
->fictitious
);
1626 vm_page_wire_count
--;
1627 queue_enter(&vm_page_queue_active
, mem
, vm_page_t
, pageq
);
1628 vm_page_active_count
++;
1630 mem
->reference
= TRUE
;
1635 * vm_page_deactivate:
1637 * Returns the given page to the inactive list,
1638 * indicating that no physical maps have access
1639 * to this page. [Used by the physical mapping system.]
1641 * The page queues must be locked.
1645 register vm_page_t m
)
1649 // dbgLog(m->phys_page, vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
1652 * This page is no longer very interesting. If it was
1653 * interesting (active or inactive/referenced), then we
1654 * clear the reference bit and (re)enter it in the
1655 * inactive queue. Note wired pages should not have
1656 * their reference bit cleared.
1658 if (m
->gobbled
) { /* can this happen? */
1659 assert(m
->wire_count
== 0);
1660 if (!m
->private && !m
->fictitious
)
1661 vm_page_wire_count
--;
1662 vm_page_gobble_count
--;
1665 if (m
->private || (m
->wire_count
!= 0))
1667 if (m
->active
|| (m
->inactive
&& m
->reference
)) {
1668 if (!m
->fictitious
&& !m
->absent
)
1669 pmap_clear_reference(m
->phys_page
);
1670 m
->reference
= FALSE
;
1671 VM_PAGE_QUEUES_REMOVE(m
);
1673 if (m
->wire_count
== 0 && !m
->inactive
) {
1674 m
->page_ticket
= vm_page_ticket
;
1675 vm_page_ticket_roll
++;
1677 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
1678 vm_page_ticket_roll
= 0;
1679 if(vm_page_ticket
== VM_PAGE_TICKET_ROLL_IDS
)
1686 queue_enter(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1688 queue_enter(&vm_page_queue_inactive
,
1689 m
, vm_page_t
, pageq
);
1694 vm_page_inactive_count
++;
1701 * Put the specified page on the active list (if appropriate).
1703 * The page queues must be locked.
1708 register vm_page_t m
)
1713 assert(m
->wire_count
== 0);
1714 if (!m
->private && !m
->fictitious
)
1715 vm_page_wire_count
--;
1716 vm_page_gobble_count
--;
1724 queue_remove(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
1726 queue_remove(&vm_page_queue_inactive
,
1727 m
, vm_page_t
, pageq
);
1730 vm_page_inactive_count
--;
1731 m
->inactive
= FALSE
;
1733 if (m
->wire_count
== 0) {
1735 panic("vm_page_activate: already active");
1737 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
1739 m
->reference
= TRUE
;
1741 vm_page_active_count
++;
1746 * vm_page_part_zero_fill:
1748 * Zero-fill a part of the page.
1751 vm_page_part_zero_fill(
1759 #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
1760 pmap_zero_part_page(m
->phys_page
, m_pa
, len
);
1763 tmp
= vm_page_grab();
1764 if (tmp
== VM_PAGE_NULL
) {
1765 vm_page_wait(THREAD_UNINT
);
1770 vm_page_zero_fill(tmp
);
1772 vm_page_part_copy(m
, 0, tmp
, 0, m_pa
);
1774 if((m_pa
+ len
) < PAGE_SIZE
) {
1775 vm_page_part_copy(m
, m_pa
+ len
, tmp
,
1776 m_pa
+ len
, PAGE_SIZE
- (m_pa
+ len
));
1778 vm_page_copy(tmp
,m
);
1779 vm_page_lock_queues();
1781 vm_page_unlock_queues();
1787 * vm_page_zero_fill:
1789 * Zero-fill the specified page.
1796 "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
1797 (integer_t
)m
->object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
1801 // dbgTrace(0xAEAEAEAE, m->phys_page, 0); /* (BRINGUP) */
1802 pmap_zero_page(m
->phys_page
);
1806 * vm_page_part_copy:
1808 * copy part of one page to another
1819 VM_PAGE_CHECK(src_m
);
1820 VM_PAGE_CHECK(dst_m
);
1822 pmap_copy_part_page(src_m
->phys_page
, src_pa
,
1823 dst_m
->phys_page
, dst_pa
, len
);
1829 * Copy one page to another
1838 "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
1839 (integer_t
)src_m
->object
, src_m
->offset
,
1840 (integer_t
)dest_m
->object
, dest_m
->offset
,
1843 VM_PAGE_CHECK(src_m
);
1844 VM_PAGE_CHECK(dest_m
);
1846 pmap_copy_page(src_m
->phys_page
, dest_m
->phys_page
);
1850 * Currently, this is a primitive allocator that grabs
1851 * free pages from the system, sorts them by physical
1852 * address, then searches for a region large enough to
1853 * satisfy the user's request.
1855 * Additional levels of effort:
1856 * + steal clean active/inactive pages
1857 * + force pageouts of dirty pages
1858 * + maintain a map of available physical
1862 #define SET_NEXT_PAGE(m,n) ((m)->pageq.next = (struct queue_entry *) (n))
1865 int vm_page_verify_contiguous(
1867 unsigned int npages
);
1868 #endif /* MACH_ASSERT */
1870 cpm_counter(unsigned int vpfls_pages_handled
= 0;)
1871 cpm_counter(unsigned int vpfls_head_insertions
= 0;)
1872 cpm_counter(unsigned int vpfls_tail_insertions
= 0;)
1873 cpm_counter(unsigned int vpfls_general_insertions
= 0;)
1874 cpm_counter(unsigned int vpfc_failed
= 0;)
1875 cpm_counter(unsigned int vpfc_satisfied
= 0;)
1878 * Sort free list by ascending physical address,
1879 * using a not-particularly-bright sort algorithm.
1880 * Caller holds vm_page_queue_free_lock.
1883 vm_page_free_list_sort(void)
1885 vm_page_t sort_list
;
1886 vm_page_t sort_list_end
;
1887 vm_page_t m
, m1
, *prev
, next_m
;
1890 unsigned int npages
;
1892 #endif /* MACH_ASSERT */
1896 * Verify pages in the free list..
1899 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
))
1901 if (npages
!= vm_page_free_count
)
1902 panic("vm_sort_free_list: prelim: npages %d free_count %d",
1903 npages
, vm_page_free_count
);
1904 old_free_count
= vm_page_free_count
;
1905 #endif /* MACH_ASSERT */
1907 sort_list
= sort_list_end
= vm_page_queue_free
;
1908 m
= NEXT_PAGE(vm_page_queue_free
);
1909 SET_NEXT_PAGE(vm_page_queue_free
, VM_PAGE_NULL
);
1910 cpm_counter(vpfls_pages_handled
= 0);
1911 while (m
!= VM_PAGE_NULL
) {
1912 cpm_counter(++vpfls_pages_handled
);
1913 next_m
= NEXT_PAGE(m
);
1914 if (m
->phys_page
< sort_list
->phys_page
) {
1915 cpm_counter(++vpfls_head_insertions
);
1916 SET_NEXT_PAGE(m
, sort_list
);
1918 } else if (m
->phys_page
> sort_list_end
->phys_page
) {
1919 cpm_counter(++vpfls_tail_insertions
);
1920 SET_NEXT_PAGE(sort_list_end
, m
);
1921 SET_NEXT_PAGE(m
, VM_PAGE_NULL
);
1924 cpm_counter(++vpfls_general_insertions
);
1925 /* general sorted list insertion */
1927 for (m1
=sort_list
; m1
!=VM_PAGE_NULL
; m1
=NEXT_PAGE(m1
)) {
1928 if (m1
->phys_page
> m
->phys_page
) {
1930 panic("vm_sort_free_list: ugh");
1931 SET_NEXT_PAGE(m
, *prev
);
1935 prev
= (vm_page_t
*) &m1
->pageq
.next
;
1943 * Verify that pages are sorted into ascending order.
1945 for (m
= sort_list
, npages
= 0; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
1946 if (m
!= sort_list
&&
1947 m
->phys_page
<= addr
) {
1948 printf("m 0x%x addr 0x%x\n", m
, addr
);
1949 panic("vm_sort_free_list");
1951 addr
= m
->phys_page
;
1954 if (old_free_count
!= vm_page_free_count
)
1955 panic("vm_sort_free_list: old_free %d free_count %d",
1956 old_free_count
, vm_page_free_count
);
1957 if (npages
!= vm_page_free_count
)
1958 panic("vm_sort_free_list: npages %d free_count %d",
1959 npages
, vm_page_free_count
);
1960 #endif /* MACH_ASSERT */
1962 vm_page_queue_free
= sort_list
;
1968 * Check that the list of pages is ordered by
1969 * ascending physical address and has no holes.
1972 vm_page_verify_contiguous(
1974 unsigned int npages
)
1976 register vm_page_t m
;
1977 unsigned int page_count
;
1978 vm_offset_t prev_addr
;
1980 prev_addr
= pages
->phys_page
;
1982 for (m
= NEXT_PAGE(pages
); m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
1983 if (m
->phys_page
!= prev_addr
+ 1) {
1984 printf("m 0x%x prev_addr 0x%x, current addr 0x%x\n",
1985 m
, prev_addr
, m
->phys_page
);
1986 printf("pages 0x%x page_count %d\n", pages
, page_count
);
1987 panic("vm_page_verify_contiguous: not contiguous!");
1989 prev_addr
= m
->phys_page
;
1992 if (page_count
!= npages
) {
1993 printf("pages 0x%x actual count 0x%x but requested 0x%x\n",
1994 pages
, page_count
, npages
);
1995 panic("vm_page_verify_contiguous: count error");
1999 #endif /* MACH_ASSERT */
2003 * Find a region large enough to contain at least npages
2004 * of contiguous physical memory.
2007 * - Called while holding vm_page_queue_free_lock.
2008 * - Doesn't respect vm_page_free_reserved; caller
2009 * must not ask for more pages than are legal to grab.
2011 * Returns a pointer to a list of gobbled pages or VM_PAGE_NULL.
2015 vm_page_find_contiguous(
2018 vm_page_t m
, *contig_prev
, *prev_ptr
;
2020 unsigned int contig_npages
;
2024 return VM_PAGE_NULL
;
2026 prev_page
= vm_page_queue_free
->phys_page
- 2;
2027 prev_ptr
= &vm_page_queue_free
;
2028 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2030 if (m
->phys_page
!= prev_page
+ 1) {
2032 * Whoops! Pages aren't contiguous. Start over.
2035 contig_prev
= prev_ptr
;
2038 if (++contig_npages
== npages
) {
2040 * Chop these pages out of the free list.
2041 * Mark them all as gobbled.
2043 list
= *contig_prev
;
2044 *contig_prev
= NEXT_PAGE(m
);
2045 SET_NEXT_PAGE(m
, VM_PAGE_NULL
);
2046 for (m
= list
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2053 vm_page_free_count
-= npages
;
2054 if (vm_page_free_count
< vm_page_free_count_minimum
)
2055 vm_page_free_count_minimum
= vm_page_free_count
;
2056 vm_page_wire_count
+= npages
;
2057 vm_page_gobble_count
+= npages
;
2058 cpm_counter(++vpfc_satisfied
);
2059 assert(vm_page_verify_contiguous(list
, contig_npages
));
2063 assert(contig_npages
< npages
);
2064 prev_ptr
= (vm_page_t
*) &m
->pageq
.next
;
2065 prev_page
= m
->phys_page
;
2067 cpm_counter(++vpfc_failed
);
2068 return VM_PAGE_NULL
;
2072 * Allocate a list of contiguous, wired pages.
2080 register vm_page_t m
;
2081 vm_page_t
*first_contig
;
2082 vm_page_t free_list
, pages
;
2083 unsigned int npages
, n1pages
;
2084 int vm_pages_available
;
2086 if (size
% page_size
!= 0)
2087 return KERN_INVALID_ARGUMENT
;
2089 vm_page_lock_queues();
2090 mutex_lock(&vm_page_queue_free_lock
);
2093 * Should also take active and inactive pages
2094 * into account... One day...
2096 vm_pages_available
= vm_page_free_count
- vm_page_free_reserved
;
2098 if (size
> vm_pages_available
* page_size
) {
2099 mutex_unlock(&vm_page_queue_free_lock
);
2100 return KERN_RESOURCE_SHORTAGE
;
2103 vm_page_free_list_sort();
2105 npages
= size
/ page_size
;
2108 * Obtain a pointer to a subset of the free
2109 * list large enough to satisfy the request;
2110 * the region will be physically contiguous.
2112 pages
= vm_page_find_contiguous(npages
);
2113 if (pages
== VM_PAGE_NULL
) {
2114 mutex_unlock(&vm_page_queue_free_lock
);
2115 vm_page_unlock_queues();
2116 return KERN_NO_SPACE
;
2119 mutex_unlock(&vm_page_queue_free_lock
);
2122 * Walk the returned list, wiring the pages.
2125 for (m
= pages
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2127 * Essentially inlined vm_page_wire.
2130 assert(!m
->inactive
);
2131 assert(!m
->private);
2132 assert(!m
->fictitious
);
2133 assert(m
->wire_count
== 0);
2137 --vm_page_gobble_count
;
2139 vm_page_unlock_queues();
2142 * The CPM pages should now be available and
2143 * ordered by ascending physical address.
2145 assert(vm_page_verify_contiguous(pages
, npages
));
2148 return KERN_SUCCESS
;
2152 #include <mach_vm_debug.h>
2155 #include <mach_debug/hash_info.h>
2156 #include <vm/vm_debug.h>
2159 * Routine: vm_page_info
2161 * Return information about the global VP table.
2162 * Fills the buffer with as much information as possible
2163 * and returns the desired size of the buffer.
2165 * Nothing locked. The caller should provide
2166 * possibly-pageable memory.
2171 hash_info_bucket_t
*info
,
2176 if (vm_page_bucket_count
< count
)
2177 count
= vm_page_bucket_count
;
2179 for (i
= 0; i
< count
; i
++) {
2180 vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
2181 unsigned int bucket_count
= 0;
2184 simple_lock(&vm_page_bucket_lock
);
2185 for (m
= bucket
->pages
; m
!= VM_PAGE_NULL
; m
= m
->next
)
2187 simple_unlock(&vm_page_bucket_lock
);
2189 /* don't touch pageable memory while holding locks */
2190 info
[i
].hib_count
= bucket_count
;
2193 return vm_page_bucket_count
;
2195 #endif /* MACH_VM_DEBUG */
2197 #include <mach_kdb.h>
2200 #include <ddb/db_output.h>
2201 #include <vm/vm_print.h>
2202 #define printf kdbprintf
2205 * Routine: vm_page_print [exported]
2213 iprintf("page 0x%x\n", p
);
2217 iprintf("object=0x%x", p
->object
);
2218 printf(", offset=0x%x", p
->offset
);
2219 printf(", wire_count=%d", p
->wire_count
);
2221 iprintf("%sinactive, %sactive, %sgobbled, %slaundry, %sfree, %sref, %sdiscard\n",
2222 (p
->inactive
? "" : "!"),
2223 (p
->active
? "" : "!"),
2224 (p
->gobbled
? "" : "!"),
2225 (p
->laundry
? "" : "!"),
2226 (p
->free
? "" : "!"),
2227 (p
->reference
? "" : "!"),
2228 (p
->discard_request
? "" : "!"));
2229 iprintf("%sbusy, %swanted, %stabled, %sfictitious, %sprivate, %sprecious\n",
2230 (p
->busy
? "" : "!"),
2231 (p
->wanted
? "" : "!"),
2232 (p
->tabled
? "" : "!"),
2233 (p
->fictitious
? "" : "!"),
2234 (p
->private ? "" : "!"),
2235 (p
->precious
? "" : "!"));
2236 iprintf("%sabsent, %serror, %sdirty, %scleaning, %spageout, %sclustered\n",
2237 (p
->absent
? "" : "!"),
2238 (p
->error
? "" : "!"),
2239 (p
->dirty
? "" : "!"),
2240 (p
->cleaning
? "" : "!"),
2241 (p
->pageout
? "" : "!"),
2242 (p
->clustered
? "" : "!"));
2243 iprintf("%slock_supplied, %soverwriting, %srestart, %sunusual\n",
2244 (p
->lock_supplied
? "" : "!"),
2245 (p
->overwriting
? "" : "!"),
2246 (p
->restart
? "" : "!"),
2247 (p
->unusual
? "" : "!"));
2249 iprintf("phys_page=0x%x", p
->phys_page
);
2250 printf(", page_error=0x%x", p
->page_error
);
2251 printf(", page_lock=0x%x", p
->page_lock
);
2252 printf(", unlock_request=%d\n", p
->unlock_request
);
2256 #endif /* MACH_KDB */