2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
62 * Author: Avadis Tevanian, Jr., Michael Wayne Young
64 * Resident memory management module.
69 #include <mach/clock_types.h>
70 #include <mach/vm_prot.h>
71 #include <mach/vm_statistics.h>
72 #include <kern/counters.h>
73 #include <kern/sched_prim.h>
74 #include <kern/task.h>
75 #include <kern/thread.h>
76 #include <kern/zalloc.h>
79 #include <vm/vm_init.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pageout.h>
83 #include <vm/vm_kern.h> /* kernel_memory_allocate() */
84 #include <kern/misc_protos.h>
85 #include <zone_debug.h>
87 #include <ppc/mappings.h> /* (BRINGUP) */
88 #include <pexpert/pexpert.h> /* (BRINGUP) */
90 #include <vm/vm_protos.h>
92 /* Variables used to indicate the relative age of pages in the
96 unsigned int vm_page_ticket_roll
= 0;
97 unsigned int vm_page_ticket
= 0;
99 * Associated with page of user-allocatable memory is a
104 * These variables record the values returned by vm_page_bootstrap,
105 * for debugging purposes. The implementation of pmap_steal_memory
106 * and pmap_startup here also uses them internally.
109 vm_offset_t virtual_space_start
;
110 vm_offset_t virtual_space_end
;
114 * The vm_page_lookup() routine, which provides for fast
115 * (virtual memory object, offset) to page lookup, employs
116 * the following hash table. The vm_page_{insert,remove}
117 * routines install and remove associations in the table.
118 * [This table is often called the virtual-to-physical,
123 #if MACH_PAGE_HASH_STATS
124 int cur_count
; /* current count */
125 int hi_count
; /* high water mark */
126 #endif /* MACH_PAGE_HASH_STATS */
129 vm_page_bucket_t
*vm_page_buckets
; /* Array of buckets */
130 unsigned int vm_page_bucket_count
= 0; /* How big is array? */
131 unsigned int vm_page_hash_mask
; /* Mask for hash function */
132 unsigned int vm_page_hash_shift
; /* Shift for hash function */
133 uint32_t vm_page_bucket_hash
; /* Basic bucket hash */
134 decl_simple_lock_data(,vm_page_bucket_lock
)
137 vm_page_lookup_nohint(vm_object_t object
, vm_object_offset_t offset
);
140 #if MACH_PAGE_HASH_STATS
141 /* This routine is only for debug. It is intended to be called by
142 * hand by a developer using a kernel debugger. This routine prints
143 * out vm_page_hash table statistics to the kernel debug console.
153 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
154 if (vm_page_buckets
[i
].hi_count
) {
156 highsum
+= vm_page_buckets
[i
].hi_count
;
157 if (vm_page_buckets
[i
].hi_count
> maxdepth
)
158 maxdepth
= vm_page_buckets
[i
].hi_count
;
161 printf("Total number of buckets: %d\n", vm_page_bucket_count
);
162 printf("Number used buckets: %d = %d%%\n",
163 numbuckets
, 100*numbuckets
/vm_page_bucket_count
);
164 printf("Number unused buckets: %d = %d%%\n",
165 vm_page_bucket_count
- numbuckets
,
166 100*(vm_page_bucket_count
-numbuckets
)/vm_page_bucket_count
);
167 printf("Sum of bucket max depth: %d\n", highsum
);
168 printf("Average bucket depth: %d.%2d\n",
169 highsum
/vm_page_bucket_count
,
170 highsum%vm_page_bucket_count
);
171 printf("Maximum bucket depth: %d\n", maxdepth
);
173 #endif /* MACH_PAGE_HASH_STATS */
176 * The virtual page size is currently implemented as a runtime
177 * variable, but is constant once initialized using vm_set_page_size.
178 * This initialization must be done in the machine-dependent
179 * bootstrap sequence, before calling other machine-independent
182 * All references to the virtual page size outside this
183 * module must use the PAGE_SIZE, PAGE_MASK and PAGE_SHIFT
186 vm_size_t page_size
= PAGE_SIZE
;
187 vm_size_t page_mask
= PAGE_MASK
;
188 int page_shift
= PAGE_SHIFT
;
191 * Resident page structures are initialized from
192 * a template (see vm_page_alloc).
194 * When adding a new field to the virtual memory
195 * object structure, be sure to add initialization
196 * (see vm_page_bootstrap).
198 struct vm_page vm_page_template
;
201 * Resident pages that represent real memory
202 * are allocated from a free list.
204 vm_page_t vm_page_queue_free
;
205 vm_page_t vm_page_queue_fictitious
;
206 unsigned int vm_page_free_wanted
;
207 unsigned int vm_page_free_count
;
208 unsigned int vm_page_fictitious_count
;
210 unsigned int vm_page_free_count_minimum
; /* debugging */
213 * Occasionally, the virtual memory system uses
214 * resident page structures that do not refer to
215 * real pages, for example to leave a page with
216 * important state information in the VP table.
218 * These page structures are allocated the way
219 * most other kernel structures are.
222 decl_mutex_data(,vm_page_alloc_lock
)
223 unsigned int io_throttle_zero_fill
;
226 * Fictitious pages don't have a physical address,
227 * but we must initialize phys_page to something.
228 * For debugging, this should be a strange value
229 * that the pmap module can recognize in assertions.
231 vm_offset_t vm_page_fictitious_addr
= (vm_offset_t
) -1;
234 * Resident page structures are also chained on
235 * queues that are used by the page replacement
236 * system (pageout daemon). These queues are
237 * defined here, but are shared by the pageout
238 * module. The inactive queue is broken into
239 * inactive and zf for convenience as the
240 * pageout daemon often assignes a higher
241 * affinity to zf pages
243 queue_head_t vm_page_queue_active
;
244 queue_head_t vm_page_queue_inactive
;
245 unsigned int vm_page_active_count
;
246 unsigned int vm_page_inactive_count
;
247 unsigned int vm_page_wire_count
;
248 unsigned int vm_page_gobble_count
= 0;
249 unsigned int vm_page_wire_count_warning
= 0;
250 unsigned int vm_page_gobble_count_warning
= 0;
252 unsigned int vm_page_purgeable_count
= 0; /* # of pages purgeable now */
253 uint64_t vm_page_purged_count
= 0; /* total count of purged pages */
255 ppnum_t vm_lopage_poolstart
= 0;
256 ppnum_t vm_lopage_poolend
= 0;
257 int vm_lopage_poolsize
= 0;
258 uint64_t max_valid_dma_address
= 0xffffffffffffffffULL
;
262 * Several page replacement parameters are also
263 * shared with this module, so that page allocation
264 * (done here in vm_page_alloc) can trigger the
267 unsigned int vm_page_free_target
= 0;
268 unsigned int vm_page_free_min
= 0;
269 unsigned int vm_page_inactive_target
= 0;
270 unsigned int vm_page_free_reserved
= 0;
271 unsigned int vm_page_throttled_count
= 0;
274 * The VM system has a couple of heuristics for deciding
275 * that pages are "uninteresting" and should be placed
276 * on the inactive queue as likely candidates for replacement.
277 * These variables let the heuristics be controlled at run-time
278 * to make experimentation easier.
281 boolean_t vm_page_deactivate_hint
= TRUE
;
286 * Sets the page size, perhaps based upon the memory
287 * size. Must be called before any use of page-size
288 * dependent functions.
290 * Sets page_shift and page_mask from page_size.
293 vm_set_page_size(void)
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 ((1U << page_shift
) == page_size
)
308 * Initializes the resident memory module.
310 * Allocates memory for the page cells, and
311 * for the object/offset-to-page hash table headers.
312 * Each page cell is initialized and placed on the free list.
313 * Returns the range of available kernel virtual memory.
321 register vm_page_t m
;
328 * Initialize the vm_page template.
331 m
= &vm_page_template
;
332 m
->object
= VM_OBJECT_NULL
; /* reset later */
333 m
->offset
= (vm_object_offset_t
) -1; /* reset later */
336 m
->pageq
.next
= NULL
;
337 m
->pageq
.prev
= NULL
;
338 m
->listq
.next
= NULL
;
339 m
->listq
.prev
= NULL
;
346 m
->reference
= FALSE
;
348 m
->dump_cleaning
= FALSE
;
349 m
->list_req_pending
= FALSE
;
354 m
->fictitious
= FALSE
;
361 m
->clustered
= FALSE
;
362 m
->lock_supplied
= FALSE
;
365 m
->zero_fill
= FALSE
;
366 m
->encrypted
= FALSE
;
368 m
->phys_page
= 0; /* reset later */
370 m
->page_lock
= VM_PROT_NONE
;
371 m
->unlock_request
= VM_PROT_NONE
;
372 m
->page_error
= KERN_SUCCESS
;
375 * Initialize the page queues.
378 mutex_init(&vm_page_queue_free_lock
, 0);
379 mutex_init(&vm_page_queue_lock
, 0);
381 vm_page_queue_free
= VM_PAGE_NULL
;
382 vm_page_queue_fictitious
= VM_PAGE_NULL
;
383 queue_init(&vm_page_queue_active
);
384 queue_init(&vm_page_queue_inactive
);
385 queue_init(&vm_page_queue_zf
);
387 vm_page_free_wanted
= 0;
390 * Steal memory for the map and zone subsystems.
393 vm_map_steal_memory();
397 * Allocate (and initialize) the virtual-to-physical
398 * table hash buckets.
400 * The number of buckets should be a power of two to
401 * get a good hash function. The following computation
402 * chooses the first power of two that is greater
403 * than the number of physical pages in the system.
406 simple_lock_init(&vm_page_bucket_lock
, 0);
408 if (vm_page_bucket_count
== 0) {
409 unsigned int npages
= pmap_free_pages();
411 vm_page_bucket_count
= 1;
412 while (vm_page_bucket_count
< npages
)
413 vm_page_bucket_count
<<= 1;
416 vm_page_hash_mask
= vm_page_bucket_count
- 1;
419 * Calculate object shift value for hashing algorithm:
420 * O = log2(sizeof(struct vm_object))
421 * B = log2(vm_page_bucket_count)
422 * hash shifts the object left by
425 size
= vm_page_bucket_count
;
426 for (log1
= 0; size
> 1; log1
++)
428 size
= sizeof(struct vm_object
);
429 for (log2
= 0; size
> 1; log2
++)
431 vm_page_hash_shift
= log1
/2 - log2
+ 1;
433 vm_page_bucket_hash
= 1 << ((log1
+ 1) >> 1); /* Get (ceiling of sqrt of table size) */
434 vm_page_bucket_hash
|= 1 << ((log1
+ 1) >> 2); /* Get (ceiling of quadroot of table size) */
435 vm_page_bucket_hash
|= 1; /* Set bit and add 1 - always must be 1 to insure unique series */
437 if (vm_page_hash_mask
& vm_page_bucket_count
)
438 printf("vm_page_bootstrap: WARNING -- strange page hash\n");
440 vm_page_buckets
= (vm_page_bucket_t
*)
441 pmap_steal_memory(vm_page_bucket_count
*
442 sizeof(vm_page_bucket_t
));
444 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
445 register vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
447 bucket
->pages
= VM_PAGE_NULL
;
448 #if MACH_PAGE_HASH_STATS
449 bucket
->cur_count
= 0;
450 bucket
->hi_count
= 0;
451 #endif /* MACH_PAGE_HASH_STATS */
455 * Machine-dependent code allocates the resident page table.
456 * It uses vm_page_init to initialize the page frames.
457 * The code also returns to us the virtual space available
458 * to the kernel. We don't trust the pmap module
459 * to get the alignment right.
462 pmap_startup(&virtual_space_start
, &virtual_space_end
);
463 virtual_space_start
= round_page(virtual_space_start
);
464 virtual_space_end
= trunc_page(virtual_space_end
);
466 *startp
= virtual_space_start
;
467 *endp
= virtual_space_end
;
470 * Compute the initial "wire" count.
471 * Up until now, the pages which have been set aside are not under
472 * the VM system's control, so although they aren't explicitly
473 * wired, they nonetheless can't be moved. At this moment,
474 * all VM managed pages are "free", courtesy of pmap_startup.
476 vm_page_wire_count
= atop_64(max_mem
) - vm_page_free_count
; /* initial value */
478 printf("vm_page_bootstrap: %d free pages\n", vm_page_free_count
);
479 vm_page_free_count_minimum
= vm_page_free_count
;
481 simple_lock_init(&vm_paging_lock
, 0);
484 #ifndef MACHINE_PAGES
486 * We implement pmap_steal_memory and pmap_startup with the help
487 * of two simpler functions, pmap_virtual_space and pmap_next_page.
494 vm_offset_t addr
, vaddr
;
498 * We round the size to a round multiple.
501 size
= (size
+ sizeof (void *) - 1) &~ (sizeof (void *) - 1);
504 * If this is the first call to pmap_steal_memory,
505 * we have to initialize ourself.
508 if (virtual_space_start
== virtual_space_end
) {
509 pmap_virtual_space(&virtual_space_start
, &virtual_space_end
);
512 * The initial values must be aligned properly, and
513 * we don't trust the pmap module to do it right.
516 virtual_space_start
= round_page(virtual_space_start
);
517 virtual_space_end
= trunc_page(virtual_space_end
);
521 * Allocate virtual memory for this request.
524 addr
= virtual_space_start
;
525 virtual_space_start
+= size
;
527 kprintf("pmap_steal_memory: %08X - %08X; size=%08X\n", addr
, virtual_space_start
, size
); /* (TEST/DEBUG) */
530 * Allocate and map physical pages to back new virtual pages.
533 for (vaddr
= round_page(addr
);
535 vaddr
+= PAGE_SIZE
) {
536 if (!pmap_next_page(&phys_page
))
537 panic("pmap_steal_memory");
540 * XXX Logically, these mappings should be wired,
541 * but some pmap modules barf if they are.
544 pmap_enter(kernel_pmap
, vaddr
, phys_page
,
545 VM_PROT_READ
|VM_PROT_WRITE
,
546 VM_WIMG_USE_DEFAULT
, FALSE
);
548 * Account for newly stolen memory
550 vm_page_wire_count
++;
554 return (void *) addr
;
562 unsigned int i
, npages
, pages_initialized
, fill
, fillval
;
566 unsigned int num_of_lopages
= 0;
567 unsigned int last_index
;
570 * We calculate how many page frames we will have
571 * and then allocate the page structures in one chunk.
574 tmpaddr
= (addr64_t
)pmap_free_pages() * (addr64_t
)PAGE_SIZE
; /* Get the amount of memory left */
575 tmpaddr
= tmpaddr
+ (addr64_t
)(round_page_32(virtual_space_start
) - virtual_space_start
); /* Account for any slop */
576 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 */
578 pages
= (vm_page_t
) pmap_steal_memory(npages
* sizeof *pages
);
581 * Initialize the page frames.
583 for (i
= 0, pages_initialized
= 0; i
< npages
; i
++) {
584 if (!pmap_next_page(&phys_page
))
587 vm_page_init(&pages
[i
], phys_page
);
593 * 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 */
599 * if vm_lopage_poolsize is non-zero, than we need to reserve
600 * a pool of pages whose addresess are less than 4G... this pool
601 * is used by drivers whose hardware can't DMA beyond 32 bits...
603 * note that I'm assuming that the page list is ascending and
604 * ordered w/r to the physical address
606 for (i
= 0, num_of_lopages
= vm_lopage_poolsize
; num_of_lopages
&& i
< pages_initialized
; num_of_lopages
--, i
++) {
611 if (m
->phys_page
>= (1 << (32 - PAGE_SHIFT
)))
612 panic("couldn't reserve the lopage pool: not enough lo pages\n");
614 if (m
->phys_page
< vm_lopage_poolend
)
615 panic("couldn't reserve the lopage pool: page list out of order\n");
617 vm_lopage_poolend
= m
->phys_page
;
619 if (vm_lopage_poolstart
== 0)
620 vm_lopage_poolstart
= m
->phys_page
;
622 if (m
->phys_page
< vm_lopage_poolstart
)
623 panic("couldn't reserve the lopage pool: page list out of order\n");
627 fillPage(m
->phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
633 // -debug code remove
634 if (2 == vm_himemory_mode
) {
635 // free low -> high so high is preferred
636 for (i
= last_index
+ 1; i
<= pages_initialized
; i
++) {
637 if(fill
) fillPage(pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
638 vm_page_release(&pages
[i
- 1]);
642 // debug code remove-
645 * Release pages in reverse order so that physical pages
646 * initially get allocated in ascending addresses. This keeps
647 * the devices (which must address physical memory) happy if
648 * they require several consecutive pages.
650 for (i
= pages_initialized
; i
> last_index
; i
--) {
651 if(fill
) fillPage(pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
652 vm_page_release(&pages
[i
- 1]);
657 vm_page_t xx
, xxo
, xxl
;
660 j
= 0; /* (BRINGUP) */
663 for(xx
= vm_page_queue_free
; xx
; xxl
= xx
, xx
= xx
->pageq
.next
) { /* (BRINGUP) */
665 if(j
> vm_page_free_count
) { /* (BRINGUP) */
666 panic("pmap_startup: too many pages, xx = %08X, xxl = %08X\n", xx
, xxl
);
669 l
= vm_page_free_count
- j
; /* (BRINGUP) */
670 k
= 0; /* (BRINGUP) */
672 if(((j
- 1) & 0xFFFF) == 0) kprintf("checking number %d of %d\n", j
, vm_page_free_count
);
674 for(xxo
= xx
->pageq
.next
; xxo
; xxo
= xxo
->pageq
.next
) { /* (BRINGUP) */
676 if(k
> l
) panic("pmap_startup: too many in secondary check %d %d\n", k
, l
);
677 if((xx
->phys_page
& 0xFFFFFFFF) == (xxo
->phys_page
& 0xFFFFFFFF)) { /* (BRINGUP) */
678 panic("pmap_startup: duplicate physaddr, xx = %08X, xxo = %08X\n", xx
, xxo
);
683 if(j
!= vm_page_free_count
) { /* (BRINGUP) */
684 panic("pmap_startup: vm_page_free_count does not match, calc = %d, vm_page_free_count = %08X\n", j
, vm_page_free_count
);
691 * We have to re-align virtual_space_start,
692 * because pmap_steal_memory has been using it.
695 virtual_space_start
= round_page_32(virtual_space_start
);
697 *startp
= virtual_space_start
;
698 *endp
= virtual_space_end
;
700 #endif /* MACHINE_PAGES */
703 * Routine: vm_page_module_init
705 * Second initialization pass, to be done after
706 * the basic VM system is ready.
709 vm_page_module_init(void)
711 vm_page_zone
= zinit((vm_size_t
) sizeof(struct vm_page
),
712 0, PAGE_SIZE
, "vm pages");
715 zone_debug_disable(vm_page_zone
);
716 #endif /* ZONE_DEBUG */
718 zone_change(vm_page_zone
, Z_EXPAND
, FALSE
);
719 zone_change(vm_page_zone
, Z_EXHAUST
, TRUE
);
720 zone_change(vm_page_zone
, Z_FOREIGN
, TRUE
);
723 * Adjust zone statistics to account for the real pages allocated
724 * in vm_page_create(). [Q: is this really what we want?]
726 vm_page_zone
->count
+= vm_page_pages
;
727 vm_page_zone
->cur_size
+= vm_page_pages
* vm_page_zone
->elem_size
;
729 mutex_init(&vm_page_alloc_lock
, 0);
733 * Routine: vm_page_create
735 * After the VM system is up, machine-dependent code
736 * may stumble across more physical memory. For example,
737 * memory that it was reserving for a frame buffer.
738 * vm_page_create turns this memory into available pages.
749 for (phys_page
= start
;
752 while ((m
= (vm_page_t
) vm_page_grab_fictitious())
754 vm_page_more_fictitious();
756 vm_page_init(m
, phys_page
);
765 * Distributes the object/offset key pair among hash buckets.
767 * NOTE: The bucket count must be a power of 2
769 #define vm_page_hash(object, offset) (\
770 ( (natural_t)((uint32_t)object * vm_page_bucket_hash) + ((uint32_t)atop_64(offset) ^ vm_page_bucket_hash))\
774 * vm_page_insert: [ internal use only ]
776 * Inserts the given mem entry into the object/object-page
777 * table and object list.
779 * The object must be locked.
784 register vm_page_t mem
,
785 register vm_object_t object
,
786 register vm_object_offset_t offset
)
788 register vm_page_bucket_t
*bucket
;
791 "vm_page_insert, object 0x%X offset 0x%X page 0x%X\n",
792 (integer_t
)object
, (integer_t
)offset
, (integer_t
)mem
, 0,0);
796 _mutex_assert(&object
->Lock
, MA_OWNED
);
798 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
799 panic("vm_page_insert: page %p for (obj=%p,off=0x%llx) "
800 "already in (obj=%p,off=0x%llx)",
801 mem
, object
, offset
, mem
->object
, mem
->offset
);
803 assert(!object
->internal
|| offset
< object
->size
);
805 /* only insert "pageout" pages into "pageout" objects,
806 * and normal pages into normal objects */
807 assert(object
->pageout
== mem
->pageout
);
809 assert(vm_page_lookup(object
, offset
) == VM_PAGE_NULL
);
812 * Record the object/offset pair in this page
815 mem
->object
= object
;
816 mem
->offset
= offset
;
819 * Insert it into the object_object/offset hash table
822 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
823 simple_lock(&vm_page_bucket_lock
);
824 mem
->next
= bucket
->pages
;
826 #if MACH_PAGE_HASH_STATS
827 if (++bucket
->cur_count
> bucket
->hi_count
)
828 bucket
->hi_count
= bucket
->cur_count
;
829 #endif /* MACH_PAGE_HASH_STATS */
830 simple_unlock(&vm_page_bucket_lock
);
833 * Now link into the object's list of backed pages.
836 VM_PAGE_INSERT(mem
, object
);
840 * Show that the object has one more resident page.
843 object
->resident_page_count
++;
845 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
846 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
847 vm_page_lock_queues();
848 vm_page_purgeable_count
++;
849 vm_page_unlock_queues();
856 * Exactly like vm_page_insert, except that we first
857 * remove any existing page at the given offset in object.
859 * The object and page queues must be locked.
864 register vm_page_t mem
,
865 register vm_object_t object
,
866 register vm_object_offset_t offset
)
868 vm_page_bucket_t
*bucket
;
869 vm_page_t found_m
= VM_PAGE_NULL
;
873 _mutex_assert(&object
->Lock
, MA_OWNED
);
874 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
876 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
877 panic("vm_page_replace: page %p for (obj=%p,off=0x%llx) "
878 "already in (obj=%p,off=0x%llx)",
879 mem
, object
, offset
, mem
->object
, mem
->offset
);
882 * Record the object/offset pair in this page
885 mem
->object
= object
;
886 mem
->offset
= offset
;
889 * Insert it into the object_object/offset hash table,
890 * replacing any page that might have been there.
893 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
894 simple_lock(&vm_page_bucket_lock
);
897 vm_page_t
*mp
= &bucket
->pages
;
898 register vm_page_t m
= *mp
;
901 if (m
->object
== object
&& m
->offset
== offset
) {
903 * Remove old page from hash list
913 mem
->next
= bucket
->pages
;
915 mem
->next
= VM_PAGE_NULL
;
918 * insert new page at head of hash list
922 simple_unlock(&vm_page_bucket_lock
);
926 * there was already a page at the specified
927 * offset for this object... remove it from
928 * the object and free it back to the free list
930 VM_PAGE_REMOVE(found_m
);
931 found_m
->tabled
= FALSE
;
933 found_m
->object
= VM_OBJECT_NULL
;
934 found_m
->offset
= (vm_object_offset_t
) -1;
935 object
->resident_page_count
--;
937 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
938 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
939 assert(vm_page_purgeable_count
> 0);
940 vm_page_purgeable_count
--;
944 * Return page to the free list.
945 * Note the page is not tabled now
947 vm_page_free(found_m
);
950 * Now link into the object's list of backed pages.
953 VM_PAGE_INSERT(mem
, object
);
957 * And show that the object has one more resident
961 object
->resident_page_count
++;
963 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
964 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
965 vm_page_purgeable_count
++;
970 * vm_page_remove: [ internal use only ]
972 * Removes the given mem entry from the object/offset-page
973 * table and the object page list.
975 * The object and page queues must be locked.
980 register vm_page_t mem
)
982 register vm_page_bucket_t
*bucket
;
983 register vm_page_t
this;
986 "vm_page_remove, object 0x%X offset 0x%X page 0x%X\n",
987 (integer_t
)mem
->object
, (integer_t
)mem
->offset
,
988 (integer_t
)mem
, 0,0);
990 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
991 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
994 assert(!mem
->cleaning
);
999 * Remove from the object_object/offset hash table
1002 bucket
= &vm_page_buckets
[vm_page_hash(mem
->object
, mem
->offset
)];
1003 simple_lock(&vm_page_bucket_lock
);
1004 if ((this = bucket
->pages
) == mem
) {
1005 /* optimize for common case */
1007 bucket
->pages
= mem
->next
;
1009 register vm_page_t
*prev
;
1011 for (prev
= &this->next
;
1012 (this = *prev
) != mem
;
1017 #if MACH_PAGE_HASH_STATS
1018 bucket
->cur_count
--;
1019 #endif /* MACH_PAGE_HASH_STATS */
1020 simple_unlock(&vm_page_bucket_lock
);
1023 * Now remove from the object's list of backed pages.
1026 VM_PAGE_REMOVE(mem
);
1029 * And show that the object has one fewer resident
1033 mem
->object
->resident_page_count
--;
1035 if (mem
->object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
1036 mem
->object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
1037 assert(vm_page_purgeable_count
> 0);
1038 vm_page_purgeable_count
--;
1041 mem
->tabled
= FALSE
;
1042 mem
->object
= VM_OBJECT_NULL
;
1043 mem
->offset
= (vm_object_offset_t
) -1;
1049 * Returns the page associated with the object/offset
1050 * pair specified; if none is found, VM_PAGE_NULL is returned.
1052 * The object must be locked. No side effects.
1055 unsigned long vm_page_lookup_hint
= 0;
1056 unsigned long vm_page_lookup_hint_next
= 0;
1057 unsigned long vm_page_lookup_hint_prev
= 0;
1058 unsigned long vm_page_lookup_hint_miss
= 0;
1062 register vm_object_t object
,
1063 register vm_object_offset_t offset
)
1065 register vm_page_t mem
;
1066 register vm_page_bucket_t
*bucket
;
1069 _mutex_assert(&object
->Lock
, MA_OWNED
);
1072 mem
= object
->memq_hint
;
1073 if (mem
!= VM_PAGE_NULL
) {
1074 assert(mem
->object
== object
);
1075 if (mem
->offset
== offset
) {
1076 vm_page_lookup_hint
++;
1079 qe
= queue_next(&mem
->listq
);
1080 if (! queue_end(&object
->memq
, qe
)) {
1081 vm_page_t next_page
;
1083 next_page
= (vm_page_t
) qe
;
1084 assert(next_page
->object
== object
);
1085 if (next_page
->offset
== offset
) {
1086 vm_page_lookup_hint_next
++;
1087 object
->memq_hint
= next_page
; /* new hint */
1091 qe
= queue_prev(&mem
->listq
);
1092 if (! queue_end(&object
->memq
, qe
)) {
1093 vm_page_t prev_page
;
1095 prev_page
= (vm_page_t
) qe
;
1096 assert(prev_page
->object
== object
);
1097 if (prev_page
->offset
== offset
) {
1098 vm_page_lookup_hint_prev
++;
1099 object
->memq_hint
= prev_page
; /* new hint */
1106 * Search the hash table for this object/offset pair
1109 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1112 * since we hold the object lock, we are guaranteed that no
1113 * new pages can be inserted into this object... this in turn
1114 * guarantess that the page we're looking for can't exist
1115 * if the bucket it hashes to is currently NULL even when looked
1116 * at outside the scope of the hash bucket lock... this is a
1117 * really cheap optimiztion to avoid taking the lock
1119 if (bucket
->pages
== VM_PAGE_NULL
) {
1120 return (VM_PAGE_NULL
);
1122 simple_lock(&vm_page_bucket_lock
);
1124 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
1126 if ((mem
->object
== object
) && (mem
->offset
== offset
))
1129 simple_unlock(&vm_page_bucket_lock
);
1131 if (mem
!= VM_PAGE_NULL
) {
1132 if (object
->memq_hint
!= VM_PAGE_NULL
) {
1133 vm_page_lookup_hint_miss
++;
1135 assert(mem
->object
== object
);
1136 object
->memq_hint
= mem
;
1144 vm_page_lookup_nohint(
1146 vm_object_offset_t offset
)
1148 register vm_page_t mem
;
1149 register vm_page_bucket_t
*bucket
;
1152 _mutex_assert(&object
->Lock
, MA_OWNED
);
1155 * Search the hash table for this object/offset pair
1158 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1160 simple_lock(&vm_page_bucket_lock
);
1161 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
1163 if ((mem
->object
== object
) && (mem
->offset
== offset
))
1166 simple_unlock(&vm_page_bucket_lock
);
1174 * Move the given memory entry from its
1175 * current object to the specified target object/offset.
1177 * The object must be locked.
1181 register vm_page_t mem
,
1182 register vm_object_t new_object
,
1183 vm_object_offset_t new_offset
)
1185 assert(mem
->object
!= new_object
);
1188 * The encryption key is based on the page's memory object
1189 * (aka "pager") and paging offset. Moving the page to
1190 * another VM object changes its "pager" and "paging_offset"
1191 * so it has to be decrypted first.
1193 if (mem
->encrypted
) {
1194 panic("vm_page_rename: page %p is encrypted\n", mem
);
1197 * Changes to mem->object require the page lock because
1198 * the pageout daemon uses that lock to get the object.
1202 "vm_page_rename, new object 0x%X, offset 0x%X page 0x%X\n",
1203 (integer_t
)new_object
, (integer_t
)new_offset
,
1204 (integer_t
)mem
, 0,0);
1206 vm_page_lock_queues();
1207 vm_page_remove(mem
);
1208 vm_page_insert(mem
, new_object
, new_offset
);
1209 vm_page_unlock_queues();
1215 * Initialize the fields in a new page.
1216 * This takes a structure with random values and initializes it
1217 * so that it can be given to vm_page_release or vm_page_insert.
1225 *mem
= vm_page_template
;
1226 mem
->phys_page
= phys_page
;
1230 * vm_page_grab_fictitious:
1232 * Remove a fictitious page from the free list.
1233 * Returns VM_PAGE_NULL if there are no free pages.
1235 int c_vm_page_grab_fictitious
= 0;
1236 int c_vm_page_release_fictitious
= 0;
1237 int c_vm_page_more_fictitious
= 0;
1240 vm_page_grab_fictitious(void)
1242 register vm_page_t m
;
1244 m
= (vm_page_t
)zget(vm_page_zone
);
1246 vm_page_init(m
, vm_page_fictitious_addr
);
1247 m
->fictitious
= TRUE
;
1250 c_vm_page_grab_fictitious
++;
1255 * vm_page_release_fictitious:
1257 * Release a fictitious page to the free list.
1261 vm_page_release_fictitious(
1262 register vm_page_t m
)
1266 assert(m
->fictitious
);
1267 assert(m
->phys_page
== vm_page_fictitious_addr
);
1269 c_vm_page_release_fictitious
++;
1272 panic("vm_page_release_fictitious");
1275 zfree(vm_page_zone
, m
);
1279 * vm_page_more_fictitious:
1281 * Add more fictitious pages to the free list.
1282 * Allowed to block. This routine is way intimate
1283 * with the zones code, for several reasons:
1284 * 1. we need to carve some page structures out of physical
1285 * memory before zones work, so they _cannot_ come from
1287 * 2. the zone needs to be collectable in order to prevent
1288 * growth without bound. These structures are used by
1289 * the device pager (by the hundreds and thousands), as
1290 * private pages for pageout, and as blocking pages for
1291 * pagein. Temporary bursts in demand should not result in
1292 * permanent allocation of a resource.
1293 * 3. To smooth allocation humps, we allocate single pages
1294 * with kernel_memory_allocate(), and cram them into the
1295 * zone. This also allows us to initialize the vm_page_t's
1296 * on the way into the zone, so that zget() always returns
1297 * an initialized structure. The zone free element pointer
1298 * and the free page pointer are both the first item in the
1300 * 4. By having the pages in the zone pre-initialized, we need
1301 * not keep 2 levels of lists. The garbage collector simply
1302 * scans our list, and reduces physical memory usage as it
1306 void vm_page_more_fictitious(void)
1308 register vm_page_t m
;
1310 kern_return_t retval
;
1313 c_vm_page_more_fictitious
++;
1316 * Allocate a single page from the zone_map. Do not wait if no physical
1317 * pages are immediately available, and do not zero the space. We need
1318 * our own blocking lock here to prevent having multiple,
1319 * simultaneous requests from piling up on the zone_map lock. Exactly
1320 * one (of our) threads should be potentially waiting on the map lock.
1321 * If winner is not vm-privileged, then the page allocation will fail,
1322 * and it will temporarily block here in the vm_page_wait().
1324 mutex_lock(&vm_page_alloc_lock
);
1326 * If another thread allocated space, just bail out now.
1328 if (zone_free_count(vm_page_zone
) > 5) {
1330 * The number "5" is a small number that is larger than the
1331 * number of fictitious pages that any single caller will
1332 * attempt to allocate. Otherwise, a thread will attempt to
1333 * acquire a fictitious page (vm_page_grab_fictitious), fail,
1334 * release all of the resources and locks already acquired,
1335 * and then call this routine. This routine finds the pages
1336 * that the caller released, so fails to allocate new space.
1337 * The process repeats infinitely. The largest known number
1338 * of fictitious pages required in this manner is 2. 5 is
1339 * simply a somewhat larger number.
1341 mutex_unlock(&vm_page_alloc_lock
);
1345 retval
= kernel_memory_allocate(zone_map
,
1346 &addr
, PAGE_SIZE
, VM_PROT_ALL
,
1347 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
1348 if (retval
!= KERN_SUCCESS
) {
1350 * No page was available. Tell the pageout daemon, drop the
1351 * lock to give another thread a chance at it, and
1352 * wait for the pageout daemon to make progress.
1354 mutex_unlock(&vm_page_alloc_lock
);
1355 vm_page_wait(THREAD_UNINT
);
1359 * Initialize as many vm_page_t's as will fit on this page. This
1360 * depends on the zone code disturbing ONLY the first item of
1361 * each zone element.
1363 m
= (vm_page_t
)addr
;
1364 for (i
= PAGE_SIZE
/sizeof(struct vm_page
); i
> 0; i
--) {
1365 vm_page_init(m
, vm_page_fictitious_addr
);
1366 m
->fictitious
= TRUE
;
1369 zcram(vm_page_zone
, (void *) addr
, PAGE_SIZE
);
1370 mutex_unlock(&vm_page_alloc_lock
);
1376 * Attempt to convert a fictitious page into a real page.
1381 register vm_page_t m
)
1383 register vm_page_t real_m
;
1386 assert(m
->fictitious
);
1389 real_m
= vm_page_grab();
1390 if (real_m
== VM_PAGE_NULL
)
1393 m
->phys_page
= real_m
->phys_page
;
1394 m
->fictitious
= FALSE
;
1397 vm_page_lock_queues();
1399 vm_page_active_count
++;
1400 else if (m
->inactive
)
1401 vm_page_inactive_count
++;
1402 vm_page_unlock_queues();
1404 real_m
->phys_page
= vm_page_fictitious_addr
;
1405 real_m
->fictitious
= TRUE
;
1407 vm_page_release_fictitious(real_m
);
1414 * Return true if it is not likely that a non-vm_privileged thread
1415 * can get memory without blocking. Advisory only, since the
1416 * situation may change under us.
1421 /* No locking, at worst we will fib. */
1422 return( vm_page_free_count
< vm_page_free_reserved
);
1428 * this is an interface to support bring-up of drivers
1429 * on platforms with physical memory > 4G...
1431 int vm_himemory_mode
= 0;
1435 * this interface exists to support hardware controllers
1436 * incapable of generating DMAs with more than 32 bits
1437 * of address on platforms with physical memory > 4G...
1439 unsigned int vm_lopage_free_count
= 0;
1440 unsigned int vm_lopage_max_count
= 0;
1441 vm_page_t vm_lopage_queue_free
= VM_PAGE_NULL
;
1444 vm_page_grablo(void)
1446 register vm_page_t mem
;
1447 unsigned int vm_lopage_alloc_count
;
1449 if (vm_lopage_poolsize
== 0)
1450 return (vm_page_grab());
1452 mutex_lock(&vm_page_queue_free_lock
);
1454 if ((mem
= vm_lopage_queue_free
) != VM_PAGE_NULL
) {
1456 vm_lopage_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1457 mem
->pageq
.next
= NULL
;
1458 mem
->pageq
.prev
= NULL
;
1460 mem
->no_isync
= TRUE
;
1462 vm_lopage_free_count
--;
1463 vm_lopage_alloc_count
= (vm_lopage_poolend
- vm_lopage_poolstart
) - vm_lopage_free_count
;
1464 if (vm_lopage_alloc_count
> vm_lopage_max_count
)
1465 vm_lopage_max_count
= vm_lopage_alloc_count
;
1467 mutex_unlock(&vm_page_queue_free_lock
);
1477 * Remove a page from the free list.
1478 * Returns VM_PAGE_NULL if the free list is too small.
1481 unsigned long vm_page_grab_count
= 0; /* measure demand */
1486 register vm_page_t mem
;
1488 mutex_lock(&vm_page_queue_free_lock
);
1489 vm_page_grab_count
++;
1492 * Optionally produce warnings if the wire or gobble
1493 * counts exceed some threshold.
1495 if (vm_page_wire_count_warning
> 0
1496 && vm_page_wire_count
>= vm_page_wire_count_warning
) {
1497 printf("mk: vm_page_grab(): high wired page count of %d\n",
1498 vm_page_wire_count
);
1499 assert(vm_page_wire_count
< vm_page_wire_count_warning
);
1501 if (vm_page_gobble_count_warning
> 0
1502 && vm_page_gobble_count
>= vm_page_gobble_count_warning
) {
1503 printf("mk: vm_page_grab(): high gobbled page count of %d\n",
1504 vm_page_gobble_count
);
1505 assert(vm_page_gobble_count
< vm_page_gobble_count_warning
);
1509 * Only let privileged threads (involved in pageout)
1510 * dip into the reserved pool.
1513 if ((vm_page_free_count
< vm_page_free_reserved
) &&
1514 !(current_thread()->options
& TH_OPT_VMPRIV
)) {
1515 mutex_unlock(&vm_page_queue_free_lock
);
1517 goto wakeup_pageout
;
1520 while (vm_page_queue_free
== VM_PAGE_NULL
) {
1521 mutex_unlock(&vm_page_queue_free_lock
);
1523 mutex_lock(&vm_page_queue_free_lock
);
1526 if (--vm_page_free_count
< vm_page_free_count_minimum
)
1527 vm_page_free_count_minimum
= vm_page_free_count
;
1528 mem
= vm_page_queue_free
;
1529 vm_page_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1530 mem
->pageq
.next
= NULL
;
1531 mem
->pageq
.prev
= NULL
;
1532 assert(mem
->listq
.next
== NULL
&& mem
->listq
.prev
== NULL
);
1533 assert(mem
->tabled
== FALSE
);
1534 assert(mem
->object
== VM_OBJECT_NULL
);
1535 assert(!mem
->laundry
);
1537 mem
->no_isync
= TRUE
;
1538 mutex_unlock(&vm_page_queue_free_lock
);
1540 assert(pmap_verify_free(mem
->phys_page
));
1543 * Decide if we should poke the pageout daemon.
1544 * We do this if the free count is less than the low
1545 * water mark, or if the free count is less than the high
1546 * water mark (but above the low water mark) and the inactive
1547 * count is less than its target.
1549 * We don't have the counts locked ... if they change a little,
1550 * it doesn't really matter.
1554 if ((vm_page_free_count
< vm_page_free_min
) ||
1555 ((vm_page_free_count
< vm_page_free_target
) &&
1556 (vm_page_inactive_count
< vm_page_inactive_target
)))
1557 thread_wakeup((event_t
) &vm_page_free_wanted
);
1559 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */
1567 * Return a page to the free list.
1572 register vm_page_t mem
)
1576 unsigned int pindex
;
1577 phys_entry
*physent
;
1579 physent
= mapping_phys_lookup(mem
->phys_page
, &pindex
); /* (BRINGUP) */
1580 if(physent
->ppLink
& ppN
) { /* (BRINGUP) */
1581 panic("vm_page_release: already released - %08X %08X\n", mem
, mem
->phys_page
);
1583 physent
->ppLink
= physent
->ppLink
| ppN
; /* (BRINGUP) */
1585 assert(!mem
->private && !mem
->fictitious
);
1587 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */
1589 mutex_lock(&vm_page_queue_free_lock
);
1592 panic("vm_page_release");
1595 assert(!mem
->laundry
);
1596 assert(mem
->object
== VM_OBJECT_NULL
);
1597 assert(mem
->pageq
.next
== NULL
&&
1598 mem
->pageq
.prev
== NULL
);
1600 if (mem
->phys_page
<= vm_lopage_poolend
&& mem
->phys_page
>= vm_lopage_poolstart
) {
1602 * this exists to support hardware controllers
1603 * incapable of generating DMAs with more than 32 bits
1604 * of address on platforms with physical memory > 4G...
1606 mem
->pageq
.next
= (queue_entry_t
) vm_lopage_queue_free
;
1607 vm_lopage_queue_free
= mem
;
1608 vm_lopage_free_count
++;
1610 mem
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1611 vm_page_queue_free
= mem
;
1612 vm_page_free_count
++;
1614 * Check if we should wake up someone waiting for page.
1615 * But don't bother waking them unless they can allocate.
1617 * We wakeup only one thread, to prevent starvation.
1618 * Because the scheduling system handles wait queues FIFO,
1619 * if we wakeup all waiting threads, one greedy thread
1620 * can starve multiple niceguy threads. When the threads
1621 * all wakeup, the greedy threads runs first, grabs the page,
1622 * and waits for another page. It will be the first to run
1623 * when the next page is freed.
1625 * However, there is a slight danger here.
1626 * The thread we wake might not use the free page.
1627 * Then the other threads could wait indefinitely
1628 * while the page goes unused. To forestall this,
1629 * the pageout daemon will keep making free pages
1630 * as long as vm_page_free_wanted is non-zero.
1633 if ((vm_page_free_wanted
> 0) &&
1634 (vm_page_free_count
>= vm_page_free_reserved
)) {
1635 vm_page_free_wanted
--;
1636 thread_wakeup_one((event_t
) &vm_page_free_count
);
1639 mutex_unlock(&vm_page_queue_free_lock
);
1645 * Wait for a page to become available.
1646 * If there are plenty of free pages, then we don't sleep.
1649 * TRUE: There may be another page, try again
1650 * FALSE: We were interrupted out of our wait, don't try again
1658 * We can't use vm_page_free_reserved to make this
1659 * determination. Consider: some thread might
1660 * need to allocate two pages. The first allocation
1661 * succeeds, the second fails. After the first page is freed,
1662 * a call to vm_page_wait must really block.
1664 kern_return_t wait_result
;
1665 int need_wakeup
= 0;
1667 mutex_lock(&vm_page_queue_free_lock
);
1668 if (vm_page_free_count
< vm_page_free_target
) {
1669 if (vm_page_free_wanted
++ == 0)
1671 wait_result
= assert_wait((event_t
)&vm_page_free_count
, interruptible
);
1672 mutex_unlock(&vm_page_queue_free_lock
);
1673 counter(c_vm_page_wait_block
++);
1676 thread_wakeup((event_t
)&vm_page_free_wanted
);
1678 if (wait_result
== THREAD_WAITING
)
1679 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1681 return(wait_result
== THREAD_AWAKENED
);
1683 mutex_unlock(&vm_page_queue_free_lock
);
1691 * Allocate and return a memory cell associated
1692 * with this VM object/offset pair.
1694 * Object must be locked.
1700 vm_object_offset_t offset
)
1702 register vm_page_t mem
;
1705 _mutex_assert(&object
->Lock
, MA_OWNED
);
1707 mem
= vm_page_grab();
1708 if (mem
== VM_PAGE_NULL
)
1709 return VM_PAGE_NULL
;
1711 vm_page_insert(mem
, object
, offset
);
1720 vm_object_offset_t offset
)
1722 register vm_page_t mem
;
1725 _mutex_assert(&object
->Lock
, MA_OWNED
);
1727 mem
= vm_page_grablo();
1728 if (mem
== VM_PAGE_NULL
)
1729 return VM_PAGE_NULL
;
1731 vm_page_insert(mem
, object
, offset
);
1737 counter(unsigned int c_laundry_pages_freed
= 0;)
1739 int vm_pagein_cluster_unused
= 0;
1740 boolean_t vm_page_free_verify
= TRUE
;
1744 * Returns the given page to the free list,
1745 * disassociating it with any VM object.
1747 * Object and page queues must be locked prior to entry.
1751 register vm_page_t mem
)
1753 vm_object_t object
= mem
->object
;
1756 assert(!mem
->cleaning
);
1757 assert(!mem
->pageout
);
1758 if (vm_page_free_verify
&& !mem
->fictitious
&& !mem
->private) {
1759 assert(pmap_verify_free(mem
->phys_page
));
1764 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
1765 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1768 panic("vm_page_free: freeing page on free list\n");
1771 vm_page_remove(mem
); /* clears tabled, object, offset */
1772 VM_PAGE_QUEUES_REMOVE(mem
); /* clears active or inactive */
1774 if (mem
->clustered
) {
1775 mem
->clustered
= FALSE
;
1776 vm_pagein_cluster_unused
++;
1779 if (mem
->wire_count
) {
1780 if (!mem
->private && !mem
->fictitious
)
1781 vm_page_wire_count
--;
1782 mem
->wire_count
= 0;
1783 assert(!mem
->gobbled
);
1784 } else if (mem
->gobbled
) {
1785 if (!mem
->private && !mem
->fictitious
)
1786 vm_page_wire_count
--;
1787 vm_page_gobble_count
--;
1789 mem
->gobbled
= FALSE
;
1792 vm_pageout_throttle_up(mem
);
1793 counter(++c_laundry_pages_freed
);
1796 PAGE_WAKEUP(mem
); /* clears wanted */
1799 vm_object_absent_release(object
);
1801 /* Some of these may be unnecessary */
1803 mem
->unlock_request
= 0;
1805 mem
->absent
= FALSE
;
1808 mem
->precious
= FALSE
;
1809 mem
->reference
= FALSE
;
1810 mem
->encrypted
= FALSE
;
1812 mem
->page_error
= KERN_SUCCESS
;
1815 mem
->private = FALSE
;
1816 mem
->fictitious
= TRUE
;
1817 mem
->phys_page
= vm_page_fictitious_addr
;
1819 if (mem
->fictitious
) {
1820 vm_page_release_fictitious(mem
);
1822 /* depends on the queues lock */
1823 if(mem
->zero_fill
) {
1825 mem
->zero_fill
= FALSE
;
1827 vm_page_init(mem
, mem
->phys_page
);
1828 vm_page_release(mem
);
1835 register vm_page_t mem
)
1837 register vm_page_t nxt
;
1838 register vm_page_t first
= NULL
;
1839 register vm_page_t last
= VM_PAGE_NULL
;
1840 register int pg_count
= 0;
1843 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1847 if (mem
->tabled
|| mem
->object
)
1848 panic("vm_page_free_list: freeing tabled page\n");
1849 if (mem
->inactive
|| mem
->active
|| mem
->free
)
1850 panic("vm_page_free_list: freeing page on list\n");
1852 assert(mem
->pageq
.prev
== NULL
);
1853 nxt
= (vm_page_t
)(mem
->pageq
.next
);
1856 vm_pagein_cluster_unused
++;
1859 vm_pageout_throttle_up(mem
);
1860 counter(++c_laundry_pages_freed
);
1864 PAGE_WAKEUP(mem
); /* clears wanted */
1867 mem
->fictitious
= TRUE
;
1869 if (!mem
->fictitious
) {
1870 /* depends on the queues lock */
1873 assert(!mem
->laundry
);
1874 vm_page_init(mem
, mem
->phys_page
);
1880 mem
->pageq
.next
= (queue_t
) first
;
1885 mem
->phys_page
= vm_page_fictitious_addr
;
1886 vm_page_release_fictitious(mem
);
1892 mutex_lock(&vm_page_queue_free_lock
);
1894 last
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1895 vm_page_queue_free
= first
;
1897 vm_page_free_count
+= pg_count
;
1899 if ((vm_page_free_wanted
> 0) &&
1900 (vm_page_free_count
>= vm_page_free_reserved
)) {
1901 unsigned int available_pages
;
1903 if (vm_page_free_count
>= vm_page_free_reserved
) {
1904 available_pages
= (vm_page_free_count
1905 - vm_page_free_reserved
);
1907 available_pages
= 0;
1910 if (available_pages
>= vm_page_free_wanted
) {
1911 vm_page_free_wanted
= 0;
1912 thread_wakeup((event_t
) &vm_page_free_count
);
1914 while (available_pages
--) {
1915 vm_page_free_wanted
--;
1916 thread_wakeup_one((event_t
) &vm_page_free_count
);
1920 mutex_unlock(&vm_page_queue_free_lock
);
1928 * Mark this page as wired down by yet
1929 * another map, removing it from paging queues
1932 * The page's object and the page queues must be locked.
1936 register vm_page_t mem
)
1939 // dbgLog(current_thread(), mem->offset, mem->object, 1); /* (TEST/DEBUG) */
1944 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
1945 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1947 if (mem
->wire_count
== 0) {
1948 VM_PAGE_QUEUES_REMOVE(mem
);
1949 if (!mem
->private && !mem
->fictitious
&& !mem
->gobbled
)
1950 vm_page_wire_count
++;
1952 vm_page_gobble_count
--;
1953 mem
->gobbled
= FALSE
;
1954 if(mem
->zero_fill
) {
1955 /* depends on the queues lock */
1957 mem
->zero_fill
= FALSE
;
1961 * The page could be encrypted, but
1962 * We don't have to decrypt it here
1963 * because we don't guarantee that the
1964 * data is actually valid at this point.
1965 * The page will get decrypted in
1966 * vm_fault_wire() if needed.
1969 assert(!mem
->gobbled
);
1976 * Mark this page as consumed by the vm/ipc/xmm subsystems.
1978 * Called only for freshly vm_page_grab()ed pages - w/ nothing locked.
1982 register vm_page_t mem
)
1984 vm_page_lock_queues();
1987 assert(!mem
->gobbled
);
1988 assert(mem
->wire_count
== 0);
1990 if (!mem
->gobbled
&& mem
->wire_count
== 0) {
1991 if (!mem
->private && !mem
->fictitious
)
1992 vm_page_wire_count
++;
1994 vm_page_gobble_count
++;
1995 mem
->gobbled
= TRUE
;
1996 vm_page_unlock_queues();
2002 * Release one wiring of this page, potentially
2003 * enabling it to be paged again.
2005 * The page's object and the page queues must be locked.
2009 register vm_page_t mem
)
2012 // dbgLog(current_thread(), mem->offset, mem->object, 0); /* (TEST/DEBUG) */
2015 assert(mem
->wire_count
> 0);
2018 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
2019 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2021 if (--mem
->wire_count
== 0) {
2022 assert(!mem
->private && !mem
->fictitious
);
2023 vm_page_wire_count
--;
2024 assert(!mem
->laundry
);
2025 assert(mem
->object
!= kernel_object
);
2026 assert(mem
->pageq
.next
== NULL
&& mem
->pageq
.prev
== NULL
);
2027 queue_enter(&vm_page_queue_active
, mem
, vm_page_t
, pageq
);
2028 vm_page_active_count
++;
2030 mem
->reference
= TRUE
;
2035 * vm_page_deactivate:
2037 * Returns the given page to the inactive list,
2038 * indicating that no physical maps have access
2039 * to this page. [Used by the physical mapping system.]
2041 * The page queues must be locked.
2045 register vm_page_t m
)
2048 assert(m
->object
!= kernel_object
);
2050 // dbgLog(m->phys_page, vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
2052 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2055 * This page is no longer very interesting. If it was
2056 * interesting (active or inactive/referenced), then we
2057 * clear the reference bit and (re)enter it in the
2058 * inactive queue. Note wired pages should not have
2059 * their reference bit cleared.
2061 if (m
->gobbled
) { /* can this happen? */
2062 assert(m
->wire_count
== 0);
2063 if (!m
->private && !m
->fictitious
)
2064 vm_page_wire_count
--;
2065 vm_page_gobble_count
--;
2068 if (m
->private || (m
->wire_count
!= 0))
2070 if (m
->active
|| (m
->inactive
&& m
->reference
)) {
2071 if (!m
->fictitious
&& !m
->absent
)
2072 pmap_clear_reference(m
->phys_page
);
2073 m
->reference
= FALSE
;
2074 VM_PAGE_QUEUES_REMOVE(m
);
2076 if (m
->wire_count
== 0 && !m
->inactive
) {
2077 m
->page_ticket
= vm_page_ticket
;
2078 vm_page_ticket_roll
++;
2080 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
2081 vm_page_ticket_roll
= 0;
2082 if(vm_page_ticket
== VM_PAGE_TICKET_ROLL_IDS
)
2088 assert(!m
->laundry
);
2089 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2091 queue_enter(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
2093 queue_enter(&vm_page_queue_inactive
,
2094 m
, vm_page_t
, pageq
);
2099 vm_page_inactive_count
++;
2106 * Put the specified page on the active list (if appropriate).
2108 * The page queues must be locked.
2113 register vm_page_t m
)
2116 assert(m
->object
!= kernel_object
);
2118 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2121 assert(m
->wire_count
== 0);
2122 if (!m
->private && !m
->fictitious
)
2123 vm_page_wire_count
--;
2124 vm_page_gobble_count
--;
2131 assert(!m
->laundry
);
2133 queue_remove(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
2135 queue_remove(&vm_page_queue_inactive
,
2136 m
, vm_page_t
, pageq
);
2138 m
->pageq
.next
= NULL
;
2139 m
->pageq
.prev
= NULL
;
2141 vm_page_inactive_count
--;
2142 m
->inactive
= FALSE
;
2144 if (m
->wire_count
== 0) {
2147 panic("vm_page_activate: already active");
2149 assert(!m
->laundry
);
2150 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2151 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
2153 m
->reference
= TRUE
;
2155 vm_page_active_count
++;
2160 * vm_page_part_zero_fill:
2162 * Zero-fill a part of the page.
2165 vm_page_part_zero_fill(
2173 #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
2174 pmap_zero_part_page(m
->phys_page
, m_pa
, len
);
2177 tmp
= vm_page_grab();
2178 if (tmp
== VM_PAGE_NULL
) {
2179 vm_page_wait(THREAD_UNINT
);
2184 vm_page_zero_fill(tmp
);
2186 vm_page_part_copy(m
, 0, tmp
, 0, m_pa
);
2188 if((m_pa
+ len
) < PAGE_SIZE
) {
2189 vm_page_part_copy(m
, m_pa
+ len
, tmp
,
2190 m_pa
+ len
, PAGE_SIZE
- (m_pa
+ len
));
2192 vm_page_copy(tmp
,m
);
2193 vm_page_lock_queues();
2195 vm_page_unlock_queues();
2201 * vm_page_zero_fill:
2203 * Zero-fill the specified page.
2210 "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
2211 (integer_t
)m
->object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
2215 // dbgTrace(0xAEAEAEAE, m->phys_page, 0); /* (BRINGUP) */
2216 pmap_zero_page(m
->phys_page
);
2220 * vm_page_part_copy:
2222 * copy part of one page to another
2233 VM_PAGE_CHECK(src_m
);
2234 VM_PAGE_CHECK(dst_m
);
2236 pmap_copy_part_page(src_m
->phys_page
, src_pa
,
2237 dst_m
->phys_page
, dst_pa
, len
);
2243 * Copy one page to another
2246 * The source page should not be encrypted. The caller should
2247 * make sure the page is decrypted first, if necessary.
2256 "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
2257 (integer_t
)src_m
->object
, src_m
->offset
,
2258 (integer_t
)dest_m
->object
, dest_m
->offset
,
2261 VM_PAGE_CHECK(src_m
);
2262 VM_PAGE_CHECK(dest_m
);
2266 * The source page should not be encrypted at this point.
2267 * The destination page will therefore not contain encrypted
2268 * data after the copy.
2270 if (src_m
->encrypted
) {
2271 panic("vm_page_copy: source page %p is encrypted\n", src_m
);
2273 dest_m
->encrypted
= FALSE
;
2275 pmap_copy_page(src_m
->phys_page
, dest_m
->phys_page
);
2279 * Currently, this is a primitive allocator that grabs
2280 * free pages from the system, sorts them by physical
2281 * address, then searches for a region large enough to
2282 * satisfy the user's request.
2284 * Additional levels of effort:
2285 * + steal clean active/inactive pages
2286 * + force pageouts of dirty pages
2287 * + maintain a map of available physical
2293 * Check that the list of pages is ordered by
2294 * ascending physical address and has no holes.
2296 int vm_page_verify_contiguous(
2298 unsigned int npages
);
2301 vm_page_verify_contiguous(
2303 unsigned int npages
)
2305 register vm_page_t m
;
2306 unsigned int page_count
;
2307 vm_offset_t prev_addr
;
2309 prev_addr
= pages
->phys_page
;
2311 for (m
= NEXT_PAGE(pages
); m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2312 if (m
->phys_page
!= prev_addr
+ 1) {
2313 printf("m 0x%x prev_addr 0x%x, current addr 0x%x\n",
2314 m
, prev_addr
, m
->phys_page
);
2315 printf("pages 0x%x page_count %d\n", pages
, page_count
);
2316 panic("vm_page_verify_contiguous: not contiguous!");
2318 prev_addr
= m
->phys_page
;
2321 if (page_count
!= npages
) {
2322 printf("pages 0x%x actual count 0x%x but requested 0x%x\n",
2323 pages
, page_count
, npages
);
2324 panic("vm_page_verify_contiguous: count error");
2328 #endif /* MACH_ASSERT */
2331 cpm_counter(unsigned int vpfls_pages_handled
= 0;)
2332 cpm_counter(unsigned int vpfls_head_insertions
= 0;)
2333 cpm_counter(unsigned int vpfls_tail_insertions
= 0;)
2334 cpm_counter(unsigned int vpfls_general_insertions
= 0;)
2335 cpm_counter(unsigned int vpfc_failed
= 0;)
2336 cpm_counter(unsigned int vpfc_satisfied
= 0;)
2339 * Find a region large enough to contain at least npages
2340 * of contiguous physical memory.
2343 * - Called while holding vm_page_queue_free_lock.
2344 * - Doesn't respect vm_page_free_reserved; caller
2345 * must not ask for more pages than are legal to grab.
2347 * Returns a pointer to a list of gobbled pages or VM_PAGE_NULL.
2350 * Loop over the free list, extracting one page at a time and
2351 * inserting those into a sorted sub-list. We stop as soon as
2352 * there's a contiguous range within the sorted list that can
2353 * satisfy the contiguous memory request. This contiguous sub-
2354 * list is chopped out of the sorted sub-list and the remainder
2355 * of the sorted sub-list is put back onto the beginning of the
2359 vm_page_find_contiguous(
2360 unsigned int contig_pages
)
2362 vm_page_t sort_list
;
2363 vm_page_t
*contfirstprev
, contlast
;
2365 ppnum_t prevcontaddr
;
2366 ppnum_t nextcontaddr
;
2367 unsigned int npages
;
2371 _mutex_assert(&vm_page_queue_free_lock
, MA_OWNED
);
2375 * Verify pages in the free list..
2378 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
))
2380 if (npages
!= vm_page_free_count
)
2381 panic("vm_sort_free_list: prelim: npages %u free_count %d",
2382 npages
, vm_page_free_count
);
2383 #endif /* MACH_ASSERT */
2385 if (contig_pages
== 0 || vm_page_queue_free
== VM_PAGE_NULL
)
2386 return VM_PAGE_NULL
;
2388 #define PPNUM_PREV(x) (((x) > 0) ? ((x) - 1) : 0)
2389 #define PPNUM_NEXT(x) (((x) < PPNUM_MAX) ? ((x) + 1) : PPNUM_MAX)
2390 #define SET_NEXT_PAGE(m,n) ((m)->pageq.next = (struct queue_entry *) (n))
2393 contfirstprev
= &sort_list
;
2394 contlast
= sort_list
= vm_page_queue_free
;
2395 vm_page_queue_free
= NEXT_PAGE(sort_list
);
2396 SET_NEXT_PAGE(sort_list
, VM_PAGE_NULL
);
2397 prevcontaddr
= PPNUM_PREV(sort_list
->phys_page
);
2398 nextcontaddr
= PPNUM_NEXT(sort_list
->phys_page
);
2400 while (npages
< contig_pages
&&
2401 (m
= vm_page_queue_free
) != VM_PAGE_NULL
)
2403 cpm_counter(++vpfls_pages_handled
);
2405 /* prepend to existing run? */
2406 if (m
->phys_page
== prevcontaddr
)
2408 vm_page_queue_free
= NEXT_PAGE(m
);
2409 cpm_counter(++vpfls_head_insertions
);
2410 prevcontaddr
= PPNUM_PREV(prevcontaddr
);
2411 SET_NEXT_PAGE(m
, *contfirstprev
);
2414 continue; /* no tail expansion check needed */
2417 /* append to tail of existing run? */
2418 else if (m
->phys_page
== nextcontaddr
)
2420 vm_page_queue_free
= NEXT_PAGE(m
);
2421 cpm_counter(++vpfls_tail_insertions
);
2422 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2423 SET_NEXT_PAGE(m
, NEXT_PAGE(contlast
));
2424 SET_NEXT_PAGE(contlast
, m
);
2429 /* prepend to the very front of sorted list? */
2430 else if (m
->phys_page
< sort_list
->phys_page
)
2432 vm_page_queue_free
= NEXT_PAGE(m
);
2433 cpm_counter(++vpfls_general_insertions
);
2434 prevcontaddr
= PPNUM_PREV(m
->phys_page
);
2435 nextcontaddr
= PPNUM_NEXT(m
->phys_page
);
2436 SET_NEXT_PAGE(m
, sort_list
);
2437 contfirstprev
= &sort_list
;
2438 contlast
= sort_list
= m
;
2442 else /* get to proper place for insertion */
2444 if (m
->phys_page
< nextcontaddr
)
2446 prevcontaddr
= PPNUM_PREV(sort_list
->phys_page
);
2447 nextcontaddr
= PPNUM_NEXT(sort_list
->phys_page
);
2448 contfirstprev
= &sort_list
;
2449 contlast
= sort_list
;
2452 for (m1
= NEXT_PAGE(contlast
);
2453 npages
< contig_pages
&&
2454 m1
!= VM_PAGE_NULL
&& m1
->phys_page
< m
->phys_page
;
2457 if (m1
->phys_page
!= nextcontaddr
) {
2458 prevcontaddr
= PPNUM_PREV(m1
->phys_page
);
2459 contfirstprev
= NEXT_PAGE_PTR(contlast
);
2464 nextcontaddr
= PPNUM_NEXT(m1
->phys_page
);
2469 * We may actually already have enough.
2470 * This could happen if a previous prepend
2471 * joined up two runs to meet our needs.
2472 * If so, bail before we take the current
2473 * page off the free queue.
2475 if (npages
== contig_pages
)
2478 if (m
->phys_page
!= nextcontaddr
)
2480 contfirstprev
= NEXT_PAGE_PTR(contlast
);
2481 prevcontaddr
= PPNUM_PREV(m
->phys_page
);
2482 nextcontaddr
= PPNUM_NEXT(m
->phys_page
);
2485 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2488 vm_page_queue_free
= NEXT_PAGE(m
);
2489 cpm_counter(++vpfls_general_insertions
);
2490 SET_NEXT_PAGE(m
, NEXT_PAGE(contlast
));
2491 SET_NEXT_PAGE(contlast
, m
);
2495 /* See how many pages are now contiguous after the insertion */
2496 for (m1
= NEXT_PAGE(m
);
2497 npages
< contig_pages
&&
2498 m1
!= VM_PAGE_NULL
&& m1
->phys_page
== nextcontaddr
;
2501 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2507 /* how did we do? */
2508 if (npages
== contig_pages
)
2510 cpm_counter(++vpfc_satisfied
);
2512 /* remove the contiguous range from the sorted list */
2514 *contfirstprev
= NEXT_PAGE(contlast
);
2515 SET_NEXT_PAGE(contlast
, VM_PAGE_NULL
);
2516 assert(vm_page_verify_contiguous(m
, npages
));
2518 /* inline vm_page_gobble() for each returned page */
2519 for (m1
= m
; m1
!= VM_PAGE_NULL
; m1
= NEXT_PAGE(m1
)) {
2521 assert(!m1
->wanted
);
2522 assert(!m1
->laundry
);
2524 m1
->no_isync
= TRUE
;
2527 vm_page_wire_count
+= npages
;
2528 vm_page_gobble_count
+= npages
;
2529 vm_page_free_count
-= npages
;
2531 /* stick free list at the tail of the sorted list */
2532 while ((m1
= *contfirstprev
) != VM_PAGE_NULL
)
2533 contfirstprev
= (vm_page_t
*)&m1
->pageq
.next
;
2534 *contfirstprev
= vm_page_queue_free
;
2537 vm_page_queue_free
= sort_list
;
2542 * Allocate a list of contiguous, wired pages.
2550 register vm_page_t m
;
2552 unsigned int npages
;
2553 unsigned int vm_pages_available
;
2556 if (size
% page_size
!= 0)
2557 return KERN_INVALID_ARGUMENT
;
2559 vm_page_lock_queues();
2560 mutex_lock(&vm_page_queue_free_lock
);
2563 * Should also take active and inactive pages
2564 * into account... One day...
2566 npages
= size
/ page_size
;
2567 vm_pages_available
= vm_page_free_count
- vm_page_free_reserved
;
2569 if (npages
> vm_pages_available
) {
2570 mutex_unlock(&vm_page_queue_free_lock
);
2571 vm_page_unlock_queues();
2572 return KERN_RESOURCE_SHORTAGE
;
2576 * Obtain a pointer to a subset of the free
2577 * list large enough to satisfy the request;
2578 * the region will be physically contiguous.
2580 pages
= vm_page_find_contiguous(npages
);
2582 /* adjust global freelist counts and determine need for wakeups */
2583 if (vm_page_free_count
< vm_page_free_count_minimum
)
2584 vm_page_free_count_minimum
= vm_page_free_count
;
2586 wakeup
= ((vm_page_free_count
< vm_page_free_min
) ||
2587 ((vm_page_free_count
< vm_page_free_target
) &&
2588 (vm_page_inactive_count
< vm_page_inactive_target
)));
2590 mutex_unlock(&vm_page_queue_free_lock
);
2592 if (pages
== VM_PAGE_NULL
) {
2593 vm_page_unlock_queues();
2594 return KERN_NO_SPACE
;
2598 * Walk the returned list, wiring the pages.
2601 for (m
= pages
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2603 * Essentially inlined vm_page_wire.
2606 assert(!m
->inactive
);
2607 assert(!m
->private);
2608 assert(!m
->fictitious
);
2609 assert(m
->wire_count
== 0);
2613 --vm_page_gobble_count
;
2615 vm_page_unlock_queues();
2618 thread_wakeup((event_t
) &vm_page_free_wanted
);
2621 * The CPM pages should now be available and
2622 * ordered by ascending physical address.
2624 assert(vm_page_verify_contiguous(pages
, npages
));
2627 return KERN_SUCCESS
;
2631 #include <mach_vm_debug.h>
2634 #include <mach_debug/hash_info.h>
2635 #include <vm/vm_debug.h>
2638 * Routine: vm_page_info
2640 * Return information about the global VP table.
2641 * Fills the buffer with as much information as possible
2642 * and returns the desired size of the buffer.
2644 * Nothing locked. The caller should provide
2645 * possibly-pageable memory.
2650 hash_info_bucket_t
*info
,
2655 if (vm_page_bucket_count
< count
)
2656 count
= vm_page_bucket_count
;
2658 for (i
= 0; i
< count
; i
++) {
2659 vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
2660 unsigned int bucket_count
= 0;
2663 simple_lock(&vm_page_bucket_lock
);
2664 for (m
= bucket
->pages
; m
!= VM_PAGE_NULL
; m
= m
->next
)
2666 simple_unlock(&vm_page_bucket_lock
);
2668 /* don't touch pageable memory while holding locks */
2669 info
[i
].hib_count
= bucket_count
;
2672 return vm_page_bucket_count
;
2674 #endif /* MACH_VM_DEBUG */
2676 #include <mach_kdb.h>
2679 #include <ddb/db_output.h>
2680 #include <vm/vm_print.h>
2681 #define printf kdbprintf
2684 * Routine: vm_page_print [exported]
2692 p
= (vm_page_t
) (long) db_addr
;
2694 iprintf("page 0x%x\n", p
);
2698 iprintf("object=0x%x", p
->object
);
2699 printf(", offset=0x%x", p
->offset
);
2700 printf(", wire_count=%d", p
->wire_count
);
2702 iprintf("%sinactive, %sactive, %sgobbled, %slaundry, %sfree, %sref, %sencrypted\n",
2703 (p
->inactive
? "" : "!"),
2704 (p
->active
? "" : "!"),
2705 (p
->gobbled
? "" : "!"),
2706 (p
->laundry
? "" : "!"),
2707 (p
->free
? "" : "!"),
2708 (p
->reference
? "" : "!"),
2709 (p
->encrypted
? "" : "!"));
2710 iprintf("%sbusy, %swanted, %stabled, %sfictitious, %sprivate, %sprecious\n",
2711 (p
->busy
? "" : "!"),
2712 (p
->wanted
? "" : "!"),
2713 (p
->tabled
? "" : "!"),
2714 (p
->fictitious
? "" : "!"),
2715 (p
->private ? "" : "!"),
2716 (p
->precious
? "" : "!"));
2717 iprintf("%sabsent, %serror, %sdirty, %scleaning, %spageout, %sclustered\n",
2718 (p
->absent
? "" : "!"),
2719 (p
->error
? "" : "!"),
2720 (p
->dirty
? "" : "!"),
2721 (p
->cleaning
? "" : "!"),
2722 (p
->pageout
? "" : "!"),
2723 (p
->clustered
? "" : "!"));
2724 iprintf("%slock_supplied, %soverwriting, %srestart, %sunusual\n",
2725 (p
->lock_supplied
? "" : "!"),
2726 (p
->overwriting
? "" : "!"),
2727 (p
->restart
? "" : "!"),
2728 (p
->unusual
? "" : "!"));
2730 iprintf("phys_page=0x%x", p
->phys_page
);
2731 printf(", page_error=0x%x", p
->page_error
);
2732 printf(", page_lock=0x%x", p
->page_lock
);
2733 printf(", unlock_request=%d\n", p
->unlock_request
);
2737 #endif /* MACH_KDB */