2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * Resident memory management module.
66 #include <libkern/OSAtomic.h>
68 #include <mach/clock_types.h>
69 #include <mach/vm_prot.h>
70 #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>
91 #include <vm/memory_object.h>
92 #include <vm/vm_purgeable_internal.h>
95 #include <sys/kern_memorystatus.h>
98 int speculative_age_index
= 0;
99 int speculative_steal_index
= 0;
101 struct vm_speculative_age_q vm_page_queue_speculative
[VM_PAGE_MAX_SPECULATIVE_AGE_Q
+ 1];
105 * Associated with page of user-allocatable memory is a
110 * These variables record the values returned by vm_page_bootstrap,
111 * for debugging purposes. The implementation of pmap_steal_memory
112 * and pmap_startup here also uses them internally.
115 vm_offset_t virtual_space_start
;
116 vm_offset_t virtual_space_end
;
120 * The vm_page_lookup() routine, which provides for fast
121 * (virtual memory object, offset) to page lookup, employs
122 * the following hash table. The vm_page_{insert,remove}
123 * routines install and remove associations in the table.
124 * [This table is often called the virtual-to-physical,
129 #if MACH_PAGE_HASH_STATS
130 int cur_count
; /* current count */
131 int hi_count
; /* high water mark */
132 #endif /* MACH_PAGE_HASH_STATS */
135 vm_page_bucket_t
*vm_page_buckets
; /* Array of buckets */
136 unsigned int vm_page_bucket_count
= 0; /* How big is array? */
137 unsigned int vm_page_hash_mask
; /* Mask for hash function */
138 unsigned int vm_page_hash_shift
; /* Shift for hash function */
139 uint32_t vm_page_bucket_hash
; /* Basic bucket hash */
140 decl_simple_lock_data(,vm_page_bucket_lock
)
143 #if MACH_PAGE_HASH_STATS
144 /* This routine is only for debug. It is intended to be called by
145 * hand by a developer using a kernel debugger. This routine prints
146 * out vm_page_hash table statistics to the kernel debug console.
156 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
157 if (vm_page_buckets
[i
].hi_count
) {
159 highsum
+= vm_page_buckets
[i
].hi_count
;
160 if (vm_page_buckets
[i
].hi_count
> maxdepth
)
161 maxdepth
= vm_page_buckets
[i
].hi_count
;
164 printf("Total number of buckets: %d\n", vm_page_bucket_count
);
165 printf("Number used buckets: %d = %d%%\n",
166 numbuckets
, 100*numbuckets
/vm_page_bucket_count
);
167 printf("Number unused buckets: %d = %d%%\n",
168 vm_page_bucket_count
- numbuckets
,
169 100*(vm_page_bucket_count
-numbuckets
)/vm_page_bucket_count
);
170 printf("Sum of bucket max depth: %d\n", highsum
);
171 printf("Average bucket depth: %d.%2d\n",
172 highsum
/vm_page_bucket_count
,
173 highsum%vm_page_bucket_count
);
174 printf("Maximum bucket depth: %d\n", maxdepth
);
176 #endif /* MACH_PAGE_HASH_STATS */
179 * The virtual page size is currently implemented as a runtime
180 * variable, but is constant once initialized using vm_set_page_size.
181 * This initialization must be done in the machine-dependent
182 * bootstrap sequence, before calling other machine-independent
185 * All references to the virtual page size outside this
186 * module must use the PAGE_SIZE, PAGE_MASK and PAGE_SHIFT
189 vm_size_t page_size
= PAGE_SIZE
;
190 vm_size_t page_mask
= PAGE_MASK
;
191 int page_shift
= PAGE_SHIFT
;
194 * Resident page structures are initialized from
195 * a template (see vm_page_alloc).
197 * When adding a new field to the virtual memory
198 * object structure, be sure to add initialization
199 * (see vm_page_bootstrap).
201 struct vm_page vm_page_template
;
203 vm_page_t vm_pages
= VM_PAGE_NULL
;
204 unsigned int vm_pages_count
= 0;
207 * Resident pages that represent real memory
208 * are allocated from a set of free lists,
211 unsigned int vm_colors
;
212 unsigned int vm_color_mask
; /* mask is == (vm_colors-1) */
213 unsigned int vm_cache_geometry_colors
= 0; /* set by hw dependent code during startup */
214 queue_head_t vm_page_queue_free
[MAX_COLORS
];
215 vm_page_t vm_page_queue_fictitious
;
216 unsigned int vm_page_free_wanted
;
217 unsigned int vm_page_free_wanted_privileged
;
218 unsigned int vm_page_free_count
;
219 unsigned int vm_page_fictitious_count
;
221 unsigned int vm_page_free_count_minimum
; /* debugging */
224 * Occasionally, the virtual memory system uses
225 * resident page structures that do not refer to
226 * real pages, for example to leave a page with
227 * important state information in the VP table.
229 * These page structures are allocated the way
230 * most other kernel structures are.
233 decl_mutex_data(,vm_page_alloc_lock
)
234 unsigned int io_throttle_zero_fill
;
237 * Fictitious pages don't have a physical address,
238 * but we must initialize phys_page to something.
239 * For debugging, this should be a strange value
240 * that the pmap module can recognize in assertions.
242 vm_offset_t vm_page_fictitious_addr
= (vm_offset_t
) -1;
245 * Guard pages are not accessible so they don't
246 * need a physical address, but we need to enter
248 * Let's make it recognizable and make sure that
249 * we don't use a real physical page with that
252 vm_offset_t vm_page_guard_addr
= (vm_offset_t
) -2;
255 * Resident page structures are also chained on
256 * queues that are used by the page replacement
257 * system (pageout daemon). These queues are
258 * defined here, but are shared by the pageout
259 * module. The inactive queue is broken into
260 * inactive and zf for convenience as the
261 * pageout daemon often assignes a higher
262 * affinity to zf pages
264 queue_head_t vm_page_queue_active
;
265 queue_head_t vm_page_queue_inactive
;
266 queue_head_t vm_page_queue_zf
; /* inactive memory queue for zero fill */
268 unsigned int vm_page_active_count
;
269 unsigned int vm_page_inactive_count
;
270 unsigned int vm_page_throttled_count
;
271 unsigned int vm_page_speculative_count
;
272 unsigned int vm_page_wire_count
;
273 unsigned int vm_page_gobble_count
= 0;
274 unsigned int vm_page_wire_count_warning
= 0;
275 unsigned int vm_page_gobble_count_warning
= 0;
277 unsigned int vm_page_purgeable_count
= 0; /* # of pages purgeable now */
278 uint64_t vm_page_purged_count
= 0; /* total count of purged pages */
280 unsigned int vm_page_speculative_recreated
= 0;
281 unsigned int vm_page_speculative_created
= 0;
282 unsigned int vm_page_speculative_used
= 0;
284 ppnum_t vm_lopage_poolstart
= 0;
285 ppnum_t vm_lopage_poolend
= 0;
286 int vm_lopage_poolsize
= 0;
287 uint64_t max_valid_dma_address
= 0xffffffffffffffffULL
;
291 * Several page replacement parameters are also
292 * shared with this module, so that page allocation
293 * (done here in vm_page_alloc) can trigger the
296 unsigned int vm_page_free_target
= 0;
297 unsigned int vm_page_free_min
= 0;
298 unsigned int vm_page_inactive_target
= 0;
299 unsigned int vm_page_inactive_min
= 0;
300 unsigned int vm_page_free_reserved
= 0;
301 unsigned int vm_page_zfill_throttle_count
= 0;
304 * The VM system has a couple of heuristics for deciding
305 * that pages are "uninteresting" and should be placed
306 * on the inactive queue as likely candidates for replacement.
307 * These variables let the heuristics be controlled at run-time
308 * to make experimentation easier.
311 boolean_t vm_page_deactivate_hint
= TRUE
;
316 * Sets the page size, perhaps based upon the memory
317 * size. Must be called before any use of page-size
318 * dependent functions.
320 * Sets page_shift and page_mask from page_size.
323 vm_set_page_size(void)
325 page_mask
= page_size
- 1;
327 if ((page_mask
& page_size
) != 0)
328 panic("vm_set_page_size: page size not a power of two");
330 for (page_shift
= 0; ; page_shift
++)
331 if ((1U << page_shift
) == page_size
)
336 /* Called once during statup, once the cache geometry is known.
339 vm_page_set_colors( void )
341 unsigned int n
, override
;
343 if ( PE_parse_boot_arg("colors", &override
) ) /* colors specified as a boot-arg? */
345 else if ( vm_cache_geometry_colors
) /* do we know what the cache geometry is? */
346 n
= vm_cache_geometry_colors
;
347 else n
= DEFAULT_COLORS
; /* use default if all else fails */
351 if ( n
> MAX_COLORS
)
354 /* the count must be a power of 2 */
355 if ( ( n
& (n
- 1)) !=0 )
356 panic("vm_page_set_colors");
359 vm_color_mask
= n
- 1;
366 * Initializes the resident memory module.
368 * Allocates memory for the page cells, and
369 * for the object/offset-to-page hash table headers.
370 * Each page cell is initialized and placed on the free list.
371 * Returns the range of available kernel virtual memory.
379 register vm_page_t m
;
386 * Initialize the vm_page template.
389 m
= &vm_page_template
;
390 m
->object
= VM_OBJECT_NULL
; /* reset later */
391 m
->offset
= (vm_object_offset_t
) -1; /* reset later */
394 m
->pageq
.next
= NULL
;
395 m
->pageq
.prev
= NULL
;
396 m
->listq
.next
= NULL
;
397 m
->listq
.prev
= NULL
;
399 m
->speculative
= FALSE
;
400 m
->throttled
= FALSE
;
408 m
->reference
= FALSE
;
410 m
->dump_cleaning
= FALSE
;
411 m
->list_req_pending
= FALSE
;
416 m
->fictitious
= FALSE
;
423 m
->clustered
= FALSE
;
426 m
->zero_fill
= FALSE
;
427 m
->encrypted
= FALSE
;
428 m
->encrypted_cleaning
= FALSE
;
429 m
->deactivated
= FALSE
;
431 m
->phys_page
= 0; /* reset later */
434 * Initialize the page queues.
437 mutex_init(&vm_page_queue_free_lock
, 0);
438 mutex_init(&vm_page_queue_lock
, 0);
440 mutex_init(&vm_purgeable_queue_lock
, 0);
442 for (i
= 0; i
< PURGEABLE_Q_TYPE_MAX
; i
++) {
445 purgeable_queues
[i
].token_q_head
= 0;
446 purgeable_queues
[i
].token_q_tail
= 0;
447 for (group
= 0; group
< NUM_VOLATILE_GROUPS
; group
++)
448 queue_init(&purgeable_queues
[i
].objq
[group
]);
450 purgeable_queues
[i
].type
= i
;
451 purgeable_queues
[i
].new_pages
= 0;
453 purgeable_queues
[i
].debug_count_tokens
= 0;
454 purgeable_queues
[i
].debug_count_objects
= 0;
458 for (i
= 0; i
< MAX_COLORS
; i
++ )
459 queue_init(&vm_page_queue_free
[i
]);
460 queue_init(&vm_lopage_queue_free
);
461 vm_page_queue_fictitious
= VM_PAGE_NULL
;
462 queue_init(&vm_page_queue_active
);
463 queue_init(&vm_page_queue_inactive
);
464 queue_init(&vm_page_queue_throttled
);
465 queue_init(&vm_page_queue_zf
);
467 for ( i
= 0; i
<= VM_PAGE_MAX_SPECULATIVE_AGE_Q
; i
++ ) {
468 queue_init(&vm_page_queue_speculative
[i
].age_q
);
470 vm_page_queue_speculative
[i
].age_ts
.tv_sec
= 0;
471 vm_page_queue_speculative
[i
].age_ts
.tv_nsec
= 0;
473 vm_page_free_wanted
= 0;
474 vm_page_free_wanted_privileged
= 0;
476 vm_page_set_colors();
480 * Steal memory for the map and zone subsystems.
483 vm_map_steal_memory();
487 * Allocate (and initialize) the virtual-to-physical
488 * table hash buckets.
490 * The number of buckets should be a power of two to
491 * get a good hash function. The following computation
492 * chooses the first power of two that is greater
493 * than the number of physical pages in the system.
496 simple_lock_init(&vm_page_bucket_lock
, 0);
498 if (vm_page_bucket_count
== 0) {
499 unsigned int npages
= pmap_free_pages();
501 vm_page_bucket_count
= 1;
502 while (vm_page_bucket_count
< npages
)
503 vm_page_bucket_count
<<= 1;
506 vm_page_hash_mask
= vm_page_bucket_count
- 1;
509 * Calculate object shift value for hashing algorithm:
510 * O = log2(sizeof(struct vm_object))
511 * B = log2(vm_page_bucket_count)
512 * hash shifts the object left by
515 size
= vm_page_bucket_count
;
516 for (log1
= 0; size
> 1; log1
++)
518 size
= sizeof(struct vm_object
);
519 for (log2
= 0; size
> 1; log2
++)
521 vm_page_hash_shift
= log1
/2 - log2
+ 1;
523 vm_page_bucket_hash
= 1 << ((log1
+ 1) >> 1); /* Get (ceiling of sqrt of table size) */
524 vm_page_bucket_hash
|= 1 << ((log1
+ 1) >> 2); /* Get (ceiling of quadroot of table size) */
525 vm_page_bucket_hash
|= 1; /* Set bit and add 1 - always must be 1 to insure unique series */
527 if (vm_page_hash_mask
& vm_page_bucket_count
)
528 printf("vm_page_bootstrap: WARNING -- strange page hash\n");
530 vm_page_buckets
= (vm_page_bucket_t
*)
531 pmap_steal_memory(vm_page_bucket_count
*
532 sizeof(vm_page_bucket_t
));
534 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
535 register vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
537 bucket
->pages
= VM_PAGE_NULL
;
538 #if MACH_PAGE_HASH_STATS
539 bucket
->cur_count
= 0;
540 bucket
->hi_count
= 0;
541 #endif /* MACH_PAGE_HASH_STATS */
545 * Machine-dependent code allocates the resident page table.
546 * It uses vm_page_init to initialize the page frames.
547 * The code also returns to us the virtual space available
548 * to the kernel. We don't trust the pmap module
549 * to get the alignment right.
552 pmap_startup(&virtual_space_start
, &virtual_space_end
);
553 virtual_space_start
= round_page(virtual_space_start
);
554 virtual_space_end
= trunc_page(virtual_space_end
);
556 *startp
= virtual_space_start
;
557 *endp
= virtual_space_end
;
560 * Compute the initial "wire" count.
561 * Up until now, the pages which have been set aside are not under
562 * the VM system's control, so although they aren't explicitly
563 * wired, they nonetheless can't be moved. At this moment,
564 * all VM managed pages are "free", courtesy of pmap_startup.
566 vm_page_wire_count
= atop_64(max_mem
) - vm_page_free_count
; /* initial value */
567 vm_page_free_count_minimum
= vm_page_free_count
;
569 printf("vm_page_bootstrap: %d free pages and %d wired pages\n",
570 vm_page_free_count
, vm_page_wire_count
);
572 simple_lock_init(&vm_paging_lock
, 0);
575 #ifndef MACHINE_PAGES
577 * We implement pmap_steal_memory and pmap_startup with the help
578 * of two simpler functions, pmap_virtual_space and pmap_next_page.
585 vm_offset_t addr
, vaddr
;
589 * We round the size to a round multiple.
592 size
= (size
+ sizeof (void *) - 1) &~ (sizeof (void *) - 1);
595 * If this is the first call to pmap_steal_memory,
596 * we have to initialize ourself.
599 if (virtual_space_start
== virtual_space_end
) {
600 pmap_virtual_space(&virtual_space_start
, &virtual_space_end
);
603 * The initial values must be aligned properly, and
604 * we don't trust the pmap module to do it right.
607 virtual_space_start
= round_page(virtual_space_start
);
608 virtual_space_end
= trunc_page(virtual_space_end
);
612 * Allocate virtual memory for this request.
615 addr
= virtual_space_start
;
616 virtual_space_start
+= size
;
618 kprintf("pmap_steal_memory: %08X - %08X; size=%08X\n", addr
, virtual_space_start
, size
); /* (TEST/DEBUG) */
621 * Allocate and map physical pages to back new virtual pages.
624 for (vaddr
= round_page(addr
);
626 vaddr
+= PAGE_SIZE
) {
627 if (!pmap_next_page(&phys_page
))
628 panic("pmap_steal_memory");
631 * XXX Logically, these mappings should be wired,
632 * but some pmap modules barf if they are.
635 pmap_enter(kernel_pmap
, vaddr
, phys_page
,
636 VM_PROT_READ
|VM_PROT_WRITE
,
637 VM_WIMG_USE_DEFAULT
, FALSE
);
639 * Account for newly stolen memory
641 vm_page_wire_count
++;
645 return (void *) addr
;
653 unsigned int i
, npages
, pages_initialized
, fill
, fillval
;
656 unsigned int num_of_lopages
= 0;
657 unsigned int last_index
;
660 * We calculate how many page frames we will have
661 * and then allocate the page structures in one chunk.
664 tmpaddr
= (addr64_t
)pmap_free_pages() * (addr64_t
)PAGE_SIZE
; /* Get the amount of memory left */
665 tmpaddr
= tmpaddr
+ (addr64_t
)(round_page_32(virtual_space_start
) - virtual_space_start
); /* Account for any slop */
666 npages
= (unsigned int)(tmpaddr
/ (addr64_t
)(PAGE_SIZE
+ sizeof(*vm_pages
))); /* Figure size of all vm_page_ts, including enough to hold the vm_page_ts */
668 vm_pages
= (vm_page_t
) pmap_steal_memory(npages
* sizeof *vm_pages
);
671 * Initialize the page frames.
673 for (i
= 0, pages_initialized
= 0; i
< npages
; i
++) {
674 if (!pmap_next_page(&phys_page
))
677 vm_page_init(&vm_pages
[i
], phys_page
);
681 vm_pages_count
= pages_initialized
;
684 * Check if we want to initialize pages to a known value
686 fill
= 0; /* Assume no fill */
687 if (PE_parse_boot_arg("fill", &fillval
)) fill
= 1; /* Set fill */
691 * if vm_lopage_poolsize is non-zero, than we need to reserve
692 * a pool of pages whose addresess are less than 4G... this pool
693 * is used by drivers whose hardware can't DMA beyond 32 bits...
695 * note that I'm assuming that the page list is ascending and
696 * ordered w/r to the physical address
698 for (i
= 0, num_of_lopages
= vm_lopage_poolsize
; num_of_lopages
&& i
< pages_initialized
; num_of_lopages
--, i
++) {
703 if (m
->phys_page
>= (1 << (32 - PAGE_SHIFT
)))
704 panic("couldn't reserve the lopage pool: not enough lo pages\n");
706 if (m
->phys_page
< vm_lopage_poolend
)
707 panic("couldn't reserve the lopage pool: page list out of order\n");
709 vm_lopage_poolend
= m
->phys_page
;
711 if (vm_lopage_poolstart
== 0)
712 vm_lopage_poolstart
= m
->phys_page
;
714 if (m
->phys_page
< vm_lopage_poolstart
)
715 panic("couldn't reserve the lopage pool: page list out of order\n");
719 fillPage(m
->phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
725 // -debug code remove
726 if (2 == vm_himemory_mode
) {
727 // free low -> high so high is preferred
728 for (i
= last_index
+ 1; i
<= pages_initialized
; i
++) {
729 if(fill
) fillPage(vm_pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
730 vm_page_release(&vm_pages
[i
- 1]);
734 // debug code remove-
737 * Release pages in reverse order so that physical pages
738 * initially get allocated in ascending addresses. This keeps
739 * the devices (which must address physical memory) happy if
740 * they require several consecutive pages.
742 for (i
= pages_initialized
; i
> last_index
; i
--) {
743 if(fill
) fillPage(vm_pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
744 vm_page_release(&vm_pages
[i
- 1]);
749 vm_page_t xx
, xxo
, xxl
;
752 j
= 0; /* (BRINGUP) */
755 for( i
= 0; i
< vm_colors
; i
++ ) {
756 queue_iterate(&vm_page_queue_free
[i
],
759 pageq
) { /* BRINGUP */
761 if(j
> vm_page_free_count
) { /* (BRINGUP) */
762 panic("pmap_startup: too many pages, xx = %08X, xxl = %08X\n", xx
, xxl
);
765 l
= vm_page_free_count
- j
; /* (BRINGUP) */
766 k
= 0; /* (BRINGUP) */
768 if(((j
- 1) & 0xFFFF) == 0) kprintf("checking number %d of %d\n", j
, vm_page_free_count
);
770 for(xxo
= xx
->pageq
.next
; xxo
!= &vm_page_queue_free
[i
]; xxo
= xxo
->pageq
.next
) { /* (BRINGUP) */
772 if(k
> l
) panic("pmap_startup: too many in secondary check %d %d\n", k
, l
);
773 if((xx
->phys_page
& 0xFFFFFFFF) == (xxo
->phys_page
& 0xFFFFFFFF)) { /* (BRINGUP) */
774 panic("pmap_startup: duplicate physaddr, xx = %08X, xxo = %08X\n", xx
, xxo
);
782 if(j
!= vm_page_free_count
) { /* (BRINGUP) */
783 panic("pmap_startup: vm_page_free_count does not match, calc = %d, vm_page_free_count = %08X\n", j
, vm_page_free_count
);
790 * We have to re-align virtual_space_start,
791 * because pmap_steal_memory has been using it.
794 virtual_space_start
= round_page_32(virtual_space_start
);
796 *startp
= virtual_space_start
;
797 *endp
= virtual_space_end
;
799 #endif /* MACHINE_PAGES */
802 * Routine: vm_page_module_init
804 * Second initialization pass, to be done after
805 * the basic VM system is ready.
808 vm_page_module_init(void)
810 vm_page_zone
= zinit((vm_size_t
) sizeof(struct vm_page
),
811 0, PAGE_SIZE
, "vm pages");
814 zone_debug_disable(vm_page_zone
);
815 #endif /* ZONE_DEBUG */
817 zone_change(vm_page_zone
, Z_EXPAND
, FALSE
);
818 zone_change(vm_page_zone
, Z_EXHAUST
, TRUE
);
819 zone_change(vm_page_zone
, Z_FOREIGN
, TRUE
);
822 * Adjust zone statistics to account for the real pages allocated
823 * in vm_page_create(). [Q: is this really what we want?]
825 vm_page_zone
->count
+= vm_page_pages
;
826 vm_page_zone
->cur_size
+= vm_page_pages
* vm_page_zone
->elem_size
;
828 mutex_init(&vm_page_alloc_lock
, 0);
832 * Routine: vm_page_create
834 * After the VM system is up, machine-dependent code
835 * may stumble across more physical memory. For example,
836 * memory that it was reserving for a frame buffer.
837 * vm_page_create turns this memory into available pages.
848 for (phys_page
= start
;
851 while ((m
= (vm_page_t
) vm_page_grab_fictitious())
853 vm_page_more_fictitious();
855 vm_page_init(m
, phys_page
);
864 * Distributes the object/offset key pair among hash buckets.
866 * NOTE: The bucket count must be a power of 2
868 #define vm_page_hash(object, offset) (\
869 ( (natural_t)((uint32_t)object * vm_page_bucket_hash) + ((uint32_t)atop_64(offset) ^ vm_page_bucket_hash))\
874 * vm_page_insert: [ internal use only ]
876 * Inserts the given mem entry into the object/object-page
877 * table and object list.
879 * The object must be locked.
885 vm_object_offset_t offset
)
887 vm_page_insert_internal(mem
, object
, offset
, FALSE
);
892 vm_page_insert_internal(
895 vm_object_offset_t offset
,
896 boolean_t queues_lock_held
)
898 register vm_page_bucket_t
*bucket
;
901 "vm_page_insert, object 0x%X offset 0x%X page 0x%X\n",
902 (integer_t
)object
, (integer_t
)offset
, (integer_t
)mem
, 0,0);
906 if (object
== vm_submap_object
) {
907 /* the vm_submap_object is only a placeholder for submaps */
908 panic("vm_page_insert(vm_submap_object,0x%llx)\n", offset
);
911 vm_object_lock_assert_exclusive(object
);
913 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
914 panic("vm_page_insert: page %p for (obj=%p,off=0x%llx) "
915 "already in (obj=%p,off=0x%llx)",
916 mem
, object
, offset
, mem
->object
, mem
->offset
);
918 assert(!object
->internal
|| offset
< object
->size
);
920 /* only insert "pageout" pages into "pageout" objects,
921 * and normal pages into normal objects */
922 assert(object
->pageout
== mem
->pageout
);
924 assert(vm_page_lookup(object
, offset
) == VM_PAGE_NULL
);
927 * Record the object/offset pair in this page
930 mem
->object
= object
;
931 mem
->offset
= offset
;
934 * Insert it into the object_object/offset hash table
937 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
938 simple_lock(&vm_page_bucket_lock
);
939 mem
->next
= bucket
->pages
;
941 #if MACH_PAGE_HASH_STATS
942 if (++bucket
->cur_count
> bucket
->hi_count
)
943 bucket
->hi_count
= bucket
->cur_count
;
944 #endif /* MACH_PAGE_HASH_STATS */
945 simple_unlock(&vm_page_bucket_lock
);
948 * Now link into the object's list of backed pages.
951 VM_PAGE_INSERT(mem
, object
);
955 * Show that the object has one more resident page.
958 object
->resident_page_count
++;
960 if (object
->purgable
== VM_PURGABLE_VOLATILE
||
961 object
->purgable
== VM_PURGABLE_EMPTY
) {
962 if (queues_lock_held
== FALSE
)
963 vm_page_lockspin_queues();
965 vm_page_purgeable_count
++;
967 if (queues_lock_held
== FALSE
)
968 vm_page_unlock_queues();
975 * Exactly like vm_page_insert, except that we first
976 * remove any existing page at the given offset in object.
978 * The object and page queues must be locked.
983 register vm_page_t mem
,
984 register vm_object_t object
,
985 register vm_object_offset_t offset
)
987 vm_page_bucket_t
*bucket
;
988 vm_page_t found_m
= VM_PAGE_NULL
;
991 vm_object_lock_assert_exclusive(object
);
993 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
995 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
996 panic("vm_page_replace: page %p for (obj=%p,off=0x%llx) "
997 "already in (obj=%p,off=0x%llx)",
998 mem
, object
, offset
, mem
->object
, mem
->offset
);
1001 * Record the object/offset pair in this page
1004 mem
->object
= object
;
1005 mem
->offset
= offset
;
1008 * Insert it into the object_object/offset hash table,
1009 * replacing any page that might have been there.
1012 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1013 simple_lock(&vm_page_bucket_lock
);
1015 if (bucket
->pages
) {
1016 vm_page_t
*mp
= &bucket
->pages
;
1017 register vm_page_t m
= *mp
;
1020 if (m
->object
== object
&& m
->offset
== offset
) {
1022 * Remove old page from hash list
1030 } while ((m
= *mp
));
1032 mem
->next
= bucket
->pages
;
1034 mem
->next
= VM_PAGE_NULL
;
1037 * insert new page at head of hash list
1039 bucket
->pages
= mem
;
1041 simple_unlock(&vm_page_bucket_lock
);
1045 * there was already a page at the specified
1046 * offset for this object... remove it from
1047 * the object and free it back to the free list
1049 VM_PAGE_REMOVE(found_m
);
1050 found_m
->tabled
= FALSE
;
1052 found_m
->object
= VM_OBJECT_NULL
;
1053 found_m
->offset
= (vm_object_offset_t
) -1;
1054 object
->resident_page_count
--;
1056 if (object
->purgable
== VM_PURGABLE_VOLATILE
||
1057 object
->purgable
== VM_PURGABLE_EMPTY
) {
1058 assert(vm_page_purgeable_count
> 0);
1059 vm_page_purgeable_count
--;
1063 * Return page to the free list.
1064 * Note the page is not tabled now
1066 vm_page_free(found_m
);
1069 * Now link into the object's list of backed pages.
1072 VM_PAGE_INSERT(mem
, object
);
1076 * And show that the object has one more resident
1080 object
->resident_page_count
++;
1082 if (object
->purgable
== VM_PURGABLE_VOLATILE
||
1083 object
->purgable
== VM_PURGABLE_EMPTY
) {
1084 vm_page_purgeable_count
++;
1089 * vm_page_remove: [ internal use only ]
1091 * Removes the given mem entry from the object/offset-page
1092 * table and the object page list.
1094 * The object and page queues must be locked.
1099 register vm_page_t mem
)
1101 register vm_page_bucket_t
*bucket
;
1102 register vm_page_t
this;
1105 "vm_page_remove, object 0x%X offset 0x%X page 0x%X\n",
1106 (integer_t
)mem
->object
, (integer_t
)mem
->offset
,
1107 (integer_t
)mem
, 0,0);
1109 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1111 vm_object_lock_assert_exclusive(mem
->object
);
1112 assert(mem
->tabled
);
1113 assert(!mem
->cleaning
);
1118 * Remove from the object_object/offset hash table
1121 bucket
= &vm_page_buckets
[vm_page_hash(mem
->object
, mem
->offset
)];
1122 simple_lock(&vm_page_bucket_lock
);
1123 if ((this = bucket
->pages
) == mem
) {
1124 /* optimize for common case */
1126 bucket
->pages
= mem
->next
;
1128 register vm_page_t
*prev
;
1130 for (prev
= &this->next
;
1131 (this = *prev
) != mem
;
1136 #if MACH_PAGE_HASH_STATS
1137 bucket
->cur_count
--;
1138 #endif /* MACH_PAGE_HASH_STATS */
1139 simple_unlock(&vm_page_bucket_lock
);
1142 * Now remove from the object's list of backed pages.
1145 VM_PAGE_REMOVE(mem
);
1148 * And show that the object has one fewer resident
1152 mem
->object
->resident_page_count
--;
1154 if (mem
->object
->purgable
== VM_PURGABLE_VOLATILE
||
1155 mem
->object
->purgable
== VM_PURGABLE_EMPTY
) {
1156 assert(vm_page_purgeable_count
> 0);
1157 vm_page_purgeable_count
--;
1159 mem
->tabled
= FALSE
;
1160 mem
->object
= VM_OBJECT_NULL
;
1161 mem
->offset
= (vm_object_offset_t
) -1;
1167 * Returns the page associated with the object/offset
1168 * pair specified; if none is found, VM_PAGE_NULL is returned.
1170 * The object must be locked. No side effects.
1173 unsigned long vm_page_lookup_hint
= 0;
1174 unsigned long vm_page_lookup_hint_next
= 0;
1175 unsigned long vm_page_lookup_hint_prev
= 0;
1176 unsigned long vm_page_lookup_hint_miss
= 0;
1177 unsigned long vm_page_lookup_bucket_NULL
= 0;
1178 unsigned long vm_page_lookup_miss
= 0;
1183 register vm_object_t object
,
1184 register vm_object_offset_t offset
)
1186 register vm_page_t mem
;
1187 register vm_page_bucket_t
*bucket
;
1190 vm_object_lock_assert_held(object
);
1191 mem
= object
->memq_hint
;
1193 if (mem
!= VM_PAGE_NULL
) {
1194 assert(mem
->object
== object
);
1196 if (mem
->offset
== offset
) {
1197 vm_page_lookup_hint
++;
1200 qe
= queue_next(&mem
->listq
);
1202 if (! queue_end(&object
->memq
, qe
)) {
1203 vm_page_t next_page
;
1205 next_page
= (vm_page_t
) qe
;
1206 assert(next_page
->object
== object
);
1208 if (next_page
->offset
== offset
) {
1209 vm_page_lookup_hint_next
++;
1210 object
->memq_hint
= next_page
; /* new hint */
1214 qe
= queue_prev(&mem
->listq
);
1216 if (! queue_end(&object
->memq
, qe
)) {
1217 vm_page_t prev_page
;
1219 prev_page
= (vm_page_t
) qe
;
1220 assert(prev_page
->object
== object
);
1222 if (prev_page
->offset
== offset
) {
1223 vm_page_lookup_hint_prev
++;
1224 object
->memq_hint
= prev_page
; /* new hint */
1230 * Search the hash table for this object/offset pair
1232 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1235 * since we hold the object lock, we are guaranteed that no
1236 * new pages can be inserted into this object... this in turn
1237 * guarantess that the page we're looking for can't exist
1238 * if the bucket it hashes to is currently NULL even when looked
1239 * at outside the scope of the hash bucket lock... this is a
1240 * really cheap optimiztion to avoid taking the lock
1242 if (bucket
->pages
== VM_PAGE_NULL
) {
1243 vm_page_lookup_bucket_NULL
++;
1245 return (VM_PAGE_NULL
);
1247 simple_lock(&vm_page_bucket_lock
);
1249 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
1251 if ((mem
->object
== object
) && (mem
->offset
== offset
))
1254 simple_unlock(&vm_page_bucket_lock
);
1256 if (mem
!= VM_PAGE_NULL
) {
1257 if (object
->memq_hint
!= VM_PAGE_NULL
) {
1258 vm_page_lookup_hint_miss
++;
1260 assert(mem
->object
== object
);
1261 object
->memq_hint
= mem
;
1263 vm_page_lookup_miss
++;
1272 * Move the given memory entry from its
1273 * current object to the specified target object/offset.
1275 * The object must be locked.
1279 register vm_page_t mem
,
1280 register vm_object_t new_object
,
1281 vm_object_offset_t new_offset
,
1282 boolean_t encrypted_ok
)
1284 assert(mem
->object
!= new_object
);
1288 * The encryption key is based on the page's memory object
1289 * (aka "pager") and paging offset. Moving the page to
1290 * another VM object changes its "pager" and "paging_offset"
1291 * so it has to be decrypted first, or we would lose the key.
1293 * One exception is VM object collapsing, where we transfer pages
1294 * from one backing object to its parent object. This operation also
1295 * transfers the paging information, so the <pager,paging_offset> info
1296 * should remain consistent. The caller (vm_object_do_collapse())
1297 * sets "encrypted_ok" in this case.
1299 if (!encrypted_ok
&& mem
->encrypted
) {
1300 panic("vm_page_rename: page %p is encrypted\n", mem
);
1304 * Changes to mem->object require the page lock because
1305 * the pageout daemon uses that lock to get the object.
1309 "vm_page_rename, new object 0x%X, offset 0x%X page 0x%X\n",
1310 (integer_t
)new_object
, (integer_t
)new_offset
,
1311 (integer_t
)mem
, 0,0);
1313 vm_page_lockspin_queues();
1314 vm_page_remove(mem
);
1315 vm_page_insert(mem
, new_object
, new_offset
);
1316 vm_page_unlock_queues();
1322 * Initialize the fields in a new page.
1323 * This takes a structure with random values and initializes it
1324 * so that it can be given to vm_page_release or vm_page_insert.
1332 *mem
= vm_page_template
;
1333 mem
->phys_page
= phys_page
;
1337 * vm_page_grab_fictitious:
1339 * Remove a fictitious page from the free list.
1340 * Returns VM_PAGE_NULL if there are no free pages.
1342 int c_vm_page_grab_fictitious
= 0;
1343 int c_vm_page_release_fictitious
= 0;
1344 int c_vm_page_more_fictitious
= 0;
1346 extern vm_page_t
vm_page_grab_fictitious_common(vm_offset_t phys_addr
);
1349 vm_page_grab_fictitious_common(
1350 vm_offset_t phys_addr
)
1352 register vm_page_t m
;
1354 m
= (vm_page_t
)zget(vm_page_zone
);
1356 vm_page_init(m
, phys_addr
);
1357 m
->fictitious
= TRUE
;
1360 c_vm_page_grab_fictitious
++;
1365 vm_page_grab_fictitious(void)
1367 return vm_page_grab_fictitious_common(vm_page_fictitious_addr
);
1371 vm_page_grab_guard(void)
1373 return vm_page_grab_fictitious_common(vm_page_guard_addr
);
1377 * vm_page_release_fictitious:
1379 * Release a fictitious page to the free list.
1383 vm_page_release_fictitious(
1384 register vm_page_t m
)
1388 assert(m
->fictitious
);
1389 assert(m
->phys_page
== vm_page_fictitious_addr
||
1390 m
->phys_page
== vm_page_guard_addr
);
1392 c_vm_page_release_fictitious
++;
1395 panic("vm_page_release_fictitious");
1398 zfree(vm_page_zone
, m
);
1402 * vm_page_more_fictitious:
1404 * Add more fictitious pages to the free list.
1405 * Allowed to block. This routine is way intimate
1406 * with the zones code, for several reasons:
1407 * 1. we need to carve some page structures out of physical
1408 * memory before zones work, so they _cannot_ come from
1410 * 2. the zone needs to be collectable in order to prevent
1411 * growth without bound. These structures are used by
1412 * the device pager (by the hundreds and thousands), as
1413 * private pages for pageout, and as blocking pages for
1414 * pagein. Temporary bursts in demand should not result in
1415 * permanent allocation of a resource.
1416 * 3. To smooth allocation humps, we allocate single pages
1417 * with kernel_memory_allocate(), and cram them into the
1418 * zone. This also allows us to initialize the vm_page_t's
1419 * on the way into the zone, so that zget() always returns
1420 * an initialized structure. The zone free element pointer
1421 * and the free page pointer are both the first item in the
1423 * 4. By having the pages in the zone pre-initialized, we need
1424 * not keep 2 levels of lists. The garbage collector simply
1425 * scans our list, and reduces physical memory usage as it
1429 void vm_page_more_fictitious(void)
1431 register vm_page_t m
;
1433 kern_return_t retval
;
1436 c_vm_page_more_fictitious
++;
1439 * Allocate a single page from the zone_map. Do not wait if no physical
1440 * pages are immediately available, and do not zero the space. We need
1441 * our own blocking lock here to prevent having multiple,
1442 * simultaneous requests from piling up on the zone_map lock. Exactly
1443 * one (of our) threads should be potentially waiting on the map lock.
1444 * If winner is not vm-privileged, then the page allocation will fail,
1445 * and it will temporarily block here in the vm_page_wait().
1447 mutex_lock(&vm_page_alloc_lock
);
1449 * If another thread allocated space, just bail out now.
1451 if (zone_free_count(vm_page_zone
) > 5) {
1453 * The number "5" is a small number that is larger than the
1454 * number of fictitious pages that any single caller will
1455 * attempt to allocate. Otherwise, a thread will attempt to
1456 * acquire a fictitious page (vm_page_grab_fictitious), fail,
1457 * release all of the resources and locks already acquired,
1458 * and then call this routine. This routine finds the pages
1459 * that the caller released, so fails to allocate new space.
1460 * The process repeats infinitely. The largest known number
1461 * of fictitious pages required in this manner is 2. 5 is
1462 * simply a somewhat larger number.
1464 mutex_unlock(&vm_page_alloc_lock
);
1468 retval
= kernel_memory_allocate(zone_map
,
1469 &addr
, PAGE_SIZE
, VM_PROT_ALL
,
1470 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
1471 if (retval
!= KERN_SUCCESS
) {
1473 * No page was available. Tell the pageout daemon, drop the
1474 * lock to give another thread a chance at it, and
1475 * wait for the pageout daemon to make progress.
1477 mutex_unlock(&vm_page_alloc_lock
);
1478 vm_page_wait(THREAD_UNINT
);
1482 * Initialize as many vm_page_t's as will fit on this page. This
1483 * depends on the zone code disturbing ONLY the first item of
1484 * each zone element.
1486 m
= (vm_page_t
)addr
;
1487 for (i
= PAGE_SIZE
/sizeof(struct vm_page
); i
> 0; i
--) {
1488 vm_page_init(m
, vm_page_fictitious_addr
);
1489 m
->fictitious
= TRUE
;
1492 zcram(vm_page_zone
, (void *) addr
, PAGE_SIZE
);
1493 mutex_unlock(&vm_page_alloc_lock
);
1500 * Return true if it is not likely that a non-vm_privileged thread
1501 * can get memory without blocking. Advisory only, since the
1502 * situation may change under us.
1507 /* No locking, at worst we will fib. */
1508 return( vm_page_free_count
< vm_page_free_reserved
);
1514 * this is an interface to support bring-up of drivers
1515 * on platforms with physical memory > 4G...
1517 int vm_himemory_mode
= 0;
1521 * this interface exists to support hardware controllers
1522 * incapable of generating DMAs with more than 32 bits
1523 * of address on platforms with physical memory > 4G...
1525 unsigned int vm_lopage_free_count
= 0;
1526 unsigned int vm_lopage_max_count
= 0;
1527 queue_head_t vm_lopage_queue_free
;
1530 vm_page_grablo(void)
1532 register vm_page_t mem
;
1533 unsigned int vm_lopage_alloc_count
;
1535 if (vm_lopage_poolsize
== 0)
1536 return (vm_page_grab());
1538 mutex_lock(&vm_page_queue_free_lock
);
1540 if (! queue_empty(&vm_lopage_queue_free
)) {
1541 queue_remove_first(&vm_lopage_queue_free
,
1547 assert(!mem
->pmapped
);
1548 assert(!mem
->wpmapped
);
1550 mem
->pageq
.next
= NULL
;
1551 mem
->pageq
.prev
= NULL
;
1554 vm_lopage_free_count
--;
1555 vm_lopage_alloc_count
= (vm_lopage_poolend
- vm_lopage_poolstart
) - vm_lopage_free_count
;
1556 if (vm_lopage_alloc_count
> vm_lopage_max_count
)
1557 vm_lopage_max_count
= vm_lopage_alloc_count
;
1561 mutex_unlock(&vm_page_queue_free_lock
);
1570 * first try to grab a page from the per-cpu free list...
1571 * this must be done while pre-emption is disabled... if
1572 * a page is available, we're done...
1573 * if no page is available, grab the vm_page_queue_free_lock
1574 * and see if current number of free pages would allow us
1575 * to grab at least 1... if not, return VM_PAGE_NULL as before...
1576 * if there are pages available, disable preemption and
1577 * recheck the state of the per-cpu free list... we could
1578 * have been preempted and moved to a different cpu, or
1579 * some other thread could have re-filled it... if still
1580 * empty, figure out how many pages we can steal from the
1581 * global free queue and move to the per-cpu queue...
1582 * return 1 of these pages when done... only wakeup the
1583 * pageout_scan thread if we moved pages from the global
1584 * list... no need for the wakeup if we've satisfied the
1585 * request from the per-cpu queue.
1588 #define COLOR_GROUPS_TO_STEAL 4
1592 vm_page_grab( void )
1597 disable_preemption();
1599 if ((mem
= PROCESSOR_DATA(current_processor(), free_pages
))) {
1600 return_page_from_cpu_list
:
1601 PROCESSOR_DATA(current_processor(), page_grab_count
) += 1;
1602 PROCESSOR_DATA(current_processor(), free_pages
) = mem
->pageq
.next
;
1603 mem
->pageq
.next
= NULL
;
1605 enable_preemption();
1607 assert(mem
->listq
.next
== NULL
&& mem
->listq
.prev
== NULL
);
1608 assert(mem
->tabled
== FALSE
);
1609 assert(mem
->object
== VM_OBJECT_NULL
);
1610 assert(!mem
->laundry
);
1612 assert(pmap_verify_free(mem
->phys_page
));
1614 assert(!mem
->encrypted
);
1615 assert(!mem
->pmapped
);
1616 assert(!mem
->wpmapped
);
1620 enable_preemption();
1623 mutex_lock(&vm_page_queue_free_lock
);
1626 * Optionally produce warnings if the wire or gobble
1627 * counts exceed some threshold.
1629 if (vm_page_wire_count_warning
> 0
1630 && vm_page_wire_count
>= vm_page_wire_count_warning
) {
1631 printf("mk: vm_page_grab(): high wired page count of %d\n",
1632 vm_page_wire_count
);
1633 assert(vm_page_wire_count
< vm_page_wire_count_warning
);
1635 if (vm_page_gobble_count_warning
> 0
1636 && vm_page_gobble_count
>= vm_page_gobble_count_warning
) {
1637 printf("mk: vm_page_grab(): high gobbled page count of %d\n",
1638 vm_page_gobble_count
);
1639 assert(vm_page_gobble_count
< vm_page_gobble_count_warning
);
1643 * Only let privileged threads (involved in pageout)
1644 * dip into the reserved pool.
1646 if ((vm_page_free_count
< vm_page_free_reserved
) &&
1647 !(current_thread()->options
& TH_OPT_VMPRIV
)) {
1648 mutex_unlock(&vm_page_queue_free_lock
);
1654 unsigned int pages_to_steal
;
1657 while ( vm_page_free_count
== 0 ) {
1659 mutex_unlock(&vm_page_queue_free_lock
);
1661 * must be a privileged thread to be
1662 * in this state since a non-privileged
1663 * thread would have bailed if we were
1664 * under the vm_page_free_reserved mark
1667 mutex_lock(&vm_page_queue_free_lock
);
1670 disable_preemption();
1672 if ((mem
= PROCESSOR_DATA(current_processor(), free_pages
))) {
1673 mutex_unlock(&vm_page_queue_free_lock
);
1676 * we got preempted and moved to another processor
1677 * or we got preempted and someone else ran and filled the cache
1679 goto return_page_from_cpu_list
;
1681 if (vm_page_free_count
<= vm_page_free_reserved
)
1684 pages_to_steal
= COLOR_GROUPS_TO_STEAL
* vm_colors
;
1686 if (pages_to_steal
> (vm_page_free_count
- vm_page_free_reserved
))
1687 pages_to_steal
= (vm_page_free_count
- vm_page_free_reserved
);
1689 color
= PROCESSOR_DATA(current_processor(), start_color
);
1692 while (pages_to_steal
--) {
1693 if (--vm_page_free_count
< vm_page_free_count_minimum
)
1694 vm_page_free_count_minimum
= vm_page_free_count
;
1696 while (queue_empty(&vm_page_queue_free
[color
]))
1697 color
= (color
+ 1) & vm_color_mask
;
1699 queue_remove_first(&vm_page_queue_free
[color
],
1703 mem
->pageq
.next
= NULL
;
1704 mem
->pageq
.prev
= NULL
;
1706 color
= (color
+ 1) & vm_color_mask
;
1711 tail
->pageq
.next
= (queue_t
)mem
;
1714 mem
->pageq
.prev
= NULL
;
1715 assert(mem
->listq
.next
== NULL
&& mem
->listq
.prev
== NULL
);
1716 assert(mem
->tabled
== FALSE
);
1717 assert(mem
->object
== VM_OBJECT_NULL
);
1718 assert(!mem
->laundry
);
1722 assert(pmap_verify_free(mem
->phys_page
));
1725 assert(!mem
->encrypted
);
1726 assert(!mem
->pmapped
);
1727 assert(!mem
->wpmapped
);
1729 PROCESSOR_DATA(current_processor(), free_pages
) = head
->pageq
.next
;
1730 PROCESSOR_DATA(current_processor(), start_color
) = color
;
1733 * satisfy this request
1735 PROCESSOR_DATA(current_processor(), page_grab_count
) += 1;
1737 mem
->pageq
.next
= NULL
;
1739 mutex_unlock(&vm_page_queue_free_lock
);
1741 enable_preemption();
1744 * Decide if we should poke the pageout daemon.
1745 * We do this if the free count is less than the low
1746 * water mark, or if the free count is less than the high
1747 * water mark (but above the low water mark) and the inactive
1748 * count is less than its target.
1750 * We don't have the counts locked ... if they change a little,
1751 * it doesn't really matter.
1753 if ((vm_page_free_count
< vm_page_free_min
) ||
1754 ((vm_page_free_count
< vm_page_free_target
) &&
1755 ((vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_min
)))
1756 thread_wakeup((event_t
) &vm_page_free_wanted
);
1763 * Decide if we need to poke the memorystatus notification thread.
1766 (vm_page_active_count
+ vm_page_inactive_count
+
1767 vm_page_speculative_count
+ vm_page_free_count
+
1768 vm_page_purgeable_count
) * 100 /
1770 if (percent_avail
<= (kern_memorystatus_level
- 5)) {
1771 kern_memorystatus_level
= percent_avail
;
1772 thread_wakeup((event_t
)&kern_memorystatus_wakeup
);
1777 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */
1785 * Return a page to the free list.
1790 register vm_page_t mem
)
1794 unsigned int pindex
;
1795 phys_entry
*physent
;
1797 physent
= mapping_phys_lookup(mem
->phys_page
, &pindex
); /* (BRINGUP) */
1798 if(physent
->ppLink
& ppN
) { /* (BRINGUP) */
1799 panic("vm_page_release: already released - %08X %08X\n", mem
, mem
->phys_page
);
1801 physent
->ppLink
= physent
->ppLink
| ppN
; /* (BRINGUP) */
1803 assert(!mem
->private && !mem
->fictitious
);
1805 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */
1807 mutex_lock(&vm_page_queue_free_lock
);
1810 panic("vm_page_release");
1815 assert(!mem
->laundry
);
1816 assert(mem
->object
== VM_OBJECT_NULL
);
1817 assert(mem
->pageq
.next
== NULL
&&
1818 mem
->pageq
.prev
== NULL
);
1819 assert(mem
->listq
.next
== NULL
&&
1820 mem
->listq
.prev
== NULL
);
1822 if (mem
->phys_page
<= vm_lopage_poolend
&& mem
->phys_page
>= vm_lopage_poolstart
) {
1824 * this exists to support hardware controllers
1825 * incapable of generating DMAs with more than 32 bits
1826 * of address on platforms with physical memory > 4G...
1828 queue_enter_first(&vm_lopage_queue_free
,
1832 vm_lopage_free_count
++;
1834 color
= mem
->phys_page
& vm_color_mask
;
1835 queue_enter_first(&vm_page_queue_free
[color
],
1839 vm_page_free_count
++;
1841 * Check if we should wake up someone waiting for page.
1842 * But don't bother waking them unless they can allocate.
1844 * We wakeup only one thread, to prevent starvation.
1845 * Because the scheduling system handles wait queues FIFO,
1846 * if we wakeup all waiting threads, one greedy thread
1847 * can starve multiple niceguy threads. When the threads
1848 * all wakeup, the greedy threads runs first, grabs the page,
1849 * and waits for another page. It will be the first to run
1850 * when the next page is freed.
1852 * However, there is a slight danger here.
1853 * The thread we wake might not use the free page.
1854 * Then the other threads could wait indefinitely
1855 * while the page goes unused. To forestall this,
1856 * the pageout daemon will keep making free pages
1857 * as long as vm_page_free_wanted is non-zero.
1860 if ((vm_page_free_wanted_privileged
> 0) && vm_page_free_count
) {
1861 vm_page_free_wanted_privileged
--;
1862 thread_wakeup_one((event_t
) &vm_page_free_wanted_privileged
);
1863 } else if ((vm_page_free_wanted
> 0) &&
1864 (vm_page_free_count
>= vm_page_free_reserved
)) {
1865 vm_page_free_wanted
--;
1866 thread_wakeup_one((event_t
) &vm_page_free_count
);
1869 mutex_unlock(&vm_page_queue_free_lock
);
1876 * Decide if we need to poke the memorystatus notification thread.
1877 * Locking is not a big issue, as only a single thread delivers these.
1880 (vm_page_active_count
+ vm_page_inactive_count
+
1881 vm_page_speculative_count
+ vm_page_free_count
+
1882 vm_page_purgeable_count
) * 100 /
1884 if (percent_avail
>= (kern_memorystatus_level
+ 5)) {
1885 kern_memorystatus_level
= percent_avail
;
1886 thread_wakeup((event_t
)&kern_memorystatus_wakeup
);
1895 * Wait for a page to become available.
1896 * If there are plenty of free pages, then we don't sleep.
1899 * TRUE: There may be another page, try again
1900 * FALSE: We were interrupted out of our wait, don't try again
1908 * We can't use vm_page_free_reserved to make this
1909 * determination. Consider: some thread might
1910 * need to allocate two pages. The first allocation
1911 * succeeds, the second fails. After the first page is freed,
1912 * a call to vm_page_wait must really block.
1914 kern_return_t wait_result
;
1915 int need_wakeup
= 0;
1916 int is_privileged
= current_thread()->options
& TH_OPT_VMPRIV
;
1918 mutex_lock(&vm_page_queue_free_lock
);
1920 if (is_privileged
&& vm_page_free_count
) {
1921 mutex_unlock(&vm_page_queue_free_lock
);
1924 if (vm_page_free_count
< vm_page_free_target
) {
1926 if (is_privileged
) {
1927 if (vm_page_free_wanted_privileged
++ == 0)
1929 wait_result
= assert_wait((event_t
)&vm_page_free_wanted_privileged
, interruptible
);
1931 if (vm_page_free_wanted
++ == 0)
1933 wait_result
= assert_wait((event_t
)&vm_page_free_count
, interruptible
);
1935 mutex_unlock(&vm_page_queue_free_lock
);
1936 counter(c_vm_page_wait_block
++);
1939 thread_wakeup((event_t
)&vm_page_free_wanted
);
1941 if (wait_result
== THREAD_WAITING
)
1942 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1944 return(wait_result
== THREAD_AWAKENED
);
1946 mutex_unlock(&vm_page_queue_free_lock
);
1954 * Allocate and return a memory cell associated
1955 * with this VM object/offset pair.
1957 * Object must be locked.
1963 vm_object_offset_t offset
)
1965 register vm_page_t mem
;
1967 vm_object_lock_assert_exclusive(object
);
1968 mem
= vm_page_grab();
1969 if (mem
== VM_PAGE_NULL
)
1970 return VM_PAGE_NULL
;
1972 vm_page_insert(mem
, object
, offset
);
1980 vm_object_offset_t offset
)
1982 register vm_page_t mem
;
1984 vm_object_lock_assert_exclusive(object
);
1985 mem
= vm_page_grablo();
1986 if (mem
== VM_PAGE_NULL
)
1987 return VM_PAGE_NULL
;
1989 vm_page_insert(mem
, object
, offset
);
1996 * vm_page_alloc_guard:
1998 * Allocate a ficticious page which will be used
1999 * as a guard page. The page will be inserted into
2000 * the object and returned to the caller.
2004 vm_page_alloc_guard(
2006 vm_object_offset_t offset
)
2008 register vm_page_t mem
;
2010 vm_object_lock_assert_exclusive(object
);
2011 mem
= vm_page_grab_guard();
2012 if (mem
== VM_PAGE_NULL
)
2013 return VM_PAGE_NULL
;
2015 vm_page_insert(mem
, object
, offset
);
2021 counter(unsigned int c_laundry_pages_freed
= 0;)
2023 boolean_t vm_page_free_verify
= TRUE
;
2027 * Returns the given page to the free list,
2028 * disassociating it with any VM object.
2030 * Object and page queues must be locked prior to entry.
2033 vm_page_free_prepare(
2034 register vm_page_t mem
)
2038 assert(!mem
->cleaning
);
2039 assert(!mem
->pageout
);
2042 if (vm_page_free_verify
&& !mem
->fictitious
&& !mem
->private) {
2043 assert(pmap_verify_free(mem
->phys_page
));
2046 vm_object_lock_assert_exclusive(mem
->object
);
2047 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2050 panic("vm_page_free: freeing page on free list\n");
2055 * We may have to free a page while it's being laundered
2056 * if we lost its pager (due to a forced unmount, for example).
2057 * We need to call vm_pageout_throttle_up() before removing
2058 * the page from its VM object, so that we can find out on
2059 * which pageout queue the page is.
2061 vm_pageout_throttle_up(mem
);
2062 counter(++c_laundry_pages_freed
);
2066 vm_page_remove(mem
); /* clears tabled, object, offset */
2068 VM_PAGE_QUEUES_REMOVE(mem
); /* clears active/inactive/throttled/speculative */
2070 if (mem
->wire_count
) {
2071 if (!mem
->private && !mem
->fictitious
)
2072 vm_page_wire_count
--;
2073 mem
->wire_count
= 0;
2074 assert(!mem
->gobbled
);
2075 } else if (mem
->gobbled
) {
2076 if (!mem
->private && !mem
->fictitious
)
2077 vm_page_wire_count
--;
2078 vm_page_gobble_count
--;
2080 mem
->gobbled
= FALSE
;
2082 PAGE_WAKEUP(mem
); /* clears wanted */
2084 /* Some of these may be unnecessary */
2086 mem
->absent
= FALSE
;
2089 mem
->precious
= FALSE
;
2090 mem
->reference
= FALSE
;
2091 mem
->encrypted
= FALSE
;
2092 mem
->encrypted_cleaning
= FALSE
;
2093 mem
->deactivated
= FALSE
;
2094 mem
->pmapped
= FALSE
;
2095 mem
->wpmapped
= FALSE
;
2098 mem
->private = FALSE
;
2099 mem
->fictitious
= TRUE
;
2100 mem
->phys_page
= vm_page_fictitious_addr
;
2102 if (!mem
->fictitious
) {
2103 if (mem
->zero_fill
== TRUE
) {
2104 mem
->zero_fill
= FALSE
;
2105 OSAddAtomic(-1, (SInt32
*)&vm_zf_count
);
2107 vm_page_init(mem
, mem
->phys_page
);
2115 vm_page_free_prepare(mem
);
2116 if (mem
->fictitious
) {
2117 vm_page_release_fictitious(mem
);
2119 vm_page_release(mem
);
2124 * Free a list of pages. The list can be up to several hundred pages,
2125 * as blocked up by vm_pageout_scan().
2126 * The big win is not having to take the page q and free list locks once
2127 * per page. We sort the incoming pages into n lists, one for
2130 * The page queues must be locked, and are kept locked.
2139 int inuse_list_head
= -1;
2141 queue_head_t free_list
[MAX_COLORS
];
2142 int inuse
[MAX_COLORS
];
2144 for (color
= 0; color
< (signed) vm_colors
; color
++) {
2145 queue_init(&free_list
[color
]);
2149 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2153 if (mem
->tabled
|| mem
->object
)
2154 panic("vm_page_free_list: freeing tabled page\n");
2155 if (mem
->inactive
|| mem
->active
|| mem
->throttled
|| mem
->free
)
2156 panic("vm_page_free_list: freeing page on list\n");
2157 if (vm_page_free_verify
&& !mem
->fictitious
&& !mem
->private) {
2158 assert(pmap_verify_free(mem
->phys_page
));
2161 assert(mem
->pageq
.prev
== NULL
);
2164 nxt
= (vm_page_t
)(mem
->pageq
.next
);
2166 if (!mem
->fictitious
) {
2169 color
= mem
->phys_page
& vm_color_mask
;
2170 if (queue_empty(&free_list
[color
])) {
2171 inuse
[color
] = inuse_list_head
;
2172 inuse_list_head
= color
;
2174 queue_enter_first(&free_list
[color
],
2180 assert(mem
->phys_page
== vm_page_fictitious_addr
||
2181 mem
->phys_page
== vm_page_guard_addr
);
2182 vm_page_release_fictitious(mem
);
2187 unsigned int avail_free_count
;
2189 mutex_lock(&vm_page_queue_free_lock
);
2191 color
= inuse_list_head
;
2193 while( color
!= -1 ) {
2194 vm_page_t first
, last
;
2195 vm_page_t first_free
;
2197 first
= (vm_page_t
) queue_first(&free_list
[color
]);
2198 last
= (vm_page_t
) queue_last(&free_list
[color
]);
2199 first_free
= (vm_page_t
) queue_first(&vm_page_queue_free
[color
]);
2201 if (queue_empty(&vm_page_queue_free
[color
])) {
2202 queue_last(&vm_page_queue_free
[color
]) =
2203 (queue_entry_t
) last
;
2205 queue_prev(&first_free
->pageq
) =
2206 (queue_entry_t
) last
;
2208 queue_first(&vm_page_queue_free
[color
]) =
2209 (queue_entry_t
) first
;
2210 queue_prev(&first
->pageq
) =
2211 (queue_entry_t
) &vm_page_queue_free
[color
];
2212 queue_next(&last
->pageq
) =
2213 (queue_entry_t
) first_free
;
2214 color
= inuse
[color
];
2217 vm_page_free_count
+= pg_count
;
2218 avail_free_count
= vm_page_free_count
;
2220 while ((vm_page_free_wanted_privileged
> 0) && avail_free_count
) {
2221 vm_page_free_wanted_privileged
--;
2224 thread_wakeup_one((event_t
) &vm_page_free_wanted_privileged
);
2227 if ((vm_page_free_wanted
> 0) &&
2228 (avail_free_count
>= vm_page_free_reserved
)) {
2229 unsigned int available_pages
;
2231 if (avail_free_count
>= vm_page_free_reserved
) {
2232 available_pages
= (avail_free_count
- vm_page_free_reserved
);
2234 available_pages
= 0;
2237 if (available_pages
>= vm_page_free_wanted
) {
2238 vm_page_free_wanted
= 0;
2239 thread_wakeup((event_t
) &vm_page_free_count
);
2241 while (available_pages
--) {
2242 vm_page_free_wanted
--;
2243 thread_wakeup_one((event_t
) &vm_page_free_count
);
2247 mutex_unlock(&vm_page_queue_free_lock
);
2254 * Decide if we need to poke the memorystatus notification thread.
2257 (vm_page_active_count
+ vm_page_inactive_count
+
2258 vm_page_speculative_count
+ vm_page_free_count
+
2259 vm_page_purgeable_count
) * 100 /
2261 if (percent_avail
>= (kern_memorystatus_level
+ 5)) {
2262 kern_memorystatus_level
= percent_avail
;
2263 thread_wakeup((event_t
)&kern_memorystatus_wakeup
);
2274 * Mark this page as wired down by yet
2275 * another map, removing it from paging queues
2278 * The page's object and the page queues must be locked.
2282 register vm_page_t mem
)
2285 // dbgLog(current_thread(), mem->offset, mem->object, 1); /* (TEST/DEBUG) */
2290 vm_object_lock_assert_exclusive(mem
->object
);
2291 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2293 if (mem
->wire_count
== 0) {
2294 VM_PAGE_QUEUES_REMOVE(mem
);
2295 if (!mem
->private && !mem
->fictitious
&& !mem
->gobbled
)
2296 vm_page_wire_count
++;
2298 vm_page_gobble_count
--;
2299 mem
->gobbled
= FALSE
;
2300 if (mem
->zero_fill
== TRUE
) {
2301 mem
->zero_fill
= FALSE
;
2302 OSAddAtomic(-1, (SInt32
*)&vm_zf_count
);
2306 * The page could be encrypted, but
2307 * We don't have to decrypt it here
2308 * because we don't guarantee that the
2309 * data is actually valid at this point.
2310 * The page will get decrypted in
2311 * vm_fault_wire() if needed.
2314 assert(!mem
->gobbled
);
2321 * Mark this page as consumed by the vm/ipc/xmm subsystems.
2323 * Called only for freshly vm_page_grab()ed pages - w/ nothing locked.
2327 register vm_page_t mem
)
2329 vm_page_lockspin_queues();
2332 assert(!mem
->gobbled
);
2333 assert(mem
->wire_count
== 0);
2335 if (!mem
->gobbled
&& mem
->wire_count
== 0) {
2336 if (!mem
->private && !mem
->fictitious
)
2337 vm_page_wire_count
++;
2339 vm_page_gobble_count
++;
2340 mem
->gobbled
= TRUE
;
2341 vm_page_unlock_queues();
2347 * Release one wiring of this page, potentially
2348 * enabling it to be paged again.
2350 * The page's object and the page queues must be locked.
2354 register vm_page_t mem
)
2357 // dbgLog(current_thread(), mem->offset, mem->object, 0); /* (TEST/DEBUG) */
2360 assert(mem
->wire_count
> 0);
2363 vm_object_lock_assert_exclusive(mem
->object
);
2364 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2366 if (--mem
->wire_count
== 0) {
2367 assert(!mem
->private && !mem
->fictitious
);
2368 vm_page_wire_count
--;
2369 assert(!mem
->laundry
);
2370 assert(mem
->object
!= kernel_object
);
2371 assert(mem
->pageq
.next
== NULL
&& mem
->pageq
.prev
== NULL
);
2372 if (!IP_VALID(memory_manager_default
) &&
2373 mem
->dirty
&& mem
->object
->internal
&&
2374 (mem
->object
->purgable
== VM_PURGABLE_DENY
||
2375 mem
->object
->purgable
== VM_PURGABLE_NONVOLATILE
)) {
2376 queue_enter(&vm_page_queue_throttled
, mem
, vm_page_t
, pageq
);
2377 vm_page_throttled_count
++;
2378 mem
->throttled
= TRUE
;
2380 queue_enter(&vm_page_queue_active
, mem
, vm_page_t
, pageq
);
2381 vm_page_active_count
++;
2384 mem
->reference
= TRUE
;
2390 * vm_page_deactivate:
2392 * Returns the given page to the inactive list,
2393 * indicating that no physical maps have access
2394 * to this page. [Used by the physical mapping system.]
2396 * The page queues must be locked.
2400 register vm_page_t m
)
2402 boolean_t rapid_age
= FALSE
;
2405 assert(m
->object
!= kernel_object
);
2406 assert(m
->phys_page
!= vm_page_guard_addr
);
2408 // dbgLog(m->phys_page, vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
2410 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2413 * This page is no longer very interesting. If it was
2414 * interesting (active or inactive/referenced), then we
2415 * clear the reference bit and (re)enter it in the
2416 * inactive queue. Note wired pages should not have
2417 * their reference bit cleared.
2419 if (m
->gobbled
) { /* can this happen? */
2420 assert(m
->wire_count
== 0);
2422 if (!m
->private && !m
->fictitious
)
2423 vm_page_wire_count
--;
2424 vm_page_gobble_count
--;
2427 if (m
->private || (m
->wire_count
!= 0))
2430 if (m
->active
&& m
->deactivated
== TRUE
) {
2431 if (!pmap_is_referenced(m
->phys_page
))
2434 if (rapid_age
== FALSE
&& !m
->fictitious
&& !m
->absent
)
2435 pmap_clear_reference(m
->phys_page
);
2437 m
->reference
= FALSE
;
2438 m
->deactivated
= FALSE
;
2439 m
->no_cache
= FALSE
;
2442 VM_PAGE_QUEUES_REMOVE(m
);
2444 assert(!m
->laundry
);
2445 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2447 if (!IP_VALID(memory_manager_default
) &&
2448 m
->dirty
&& m
->object
->internal
&&
2449 (m
->object
->purgable
== VM_PURGABLE_DENY
||
2450 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
)) {
2451 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
2452 m
->throttled
= TRUE
;
2453 vm_page_throttled_count
++;
2455 if (rapid_age
== TRUE
||
2456 (!m
->fictitious
&& m
->object
->named
&& m
->object
->ref_count
== 1)) {
2457 vm_page_speculate(m
, FALSE
);
2458 vm_page_speculative_recreated
++;
2462 queue_enter(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
2463 vm_zf_queue_count
++;
2465 queue_enter(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
2469 if (!m
->fictitious
) {
2470 vm_page_inactive_count
++;
2471 token_new_pagecount
++;
2480 * Put the specified page on the active list (if appropriate).
2482 * The page queues must be locked.
2487 register vm_page_t m
)
2490 #ifdef FIXME_4778297
2491 assert(m
->object
!= kernel_object
);
2493 assert(m
->phys_page
!= vm_page_guard_addr
);
2495 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2498 assert(m
->wire_count
== 0);
2499 if (!m
->private && !m
->fictitious
)
2500 vm_page_wire_count
--;
2501 vm_page_gobble_count
--;
2509 panic("vm_page_activate: already active");
2512 if (m
->speculative
) {
2513 DTRACE_VM2(pgrec
, int, 1, (uint64_t *), NULL
);
2514 DTRACE_VM2(pgfrec
, int, 1, (uint64_t *), NULL
);
2517 VM_PAGE_QUEUES_REMOVE(m
);
2519 if (m
->wire_count
== 0) {
2520 assert(!m
->laundry
);
2521 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2522 if (!IP_VALID(memory_manager_default
) &&
2523 !m
->fictitious
&& m
->dirty
&& m
->object
->internal
&&
2524 (m
->object
->purgable
== VM_PURGABLE_DENY
||
2525 m
->object
->purgable
== VM_PURGABLE_NONVOLATILE
)) {
2526 queue_enter(&vm_page_queue_throttled
, m
, vm_page_t
, pageq
);
2527 m
->throttled
= TRUE
;
2528 vm_page_throttled_count
++;
2530 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
2533 vm_page_active_count
++;
2535 m
->reference
= TRUE
;
2536 m
->no_cache
= FALSE
;
2542 * vm_page_speculate:
2544 * Put the specified page on the speculative list (if appropriate).
2546 * The page queues must be locked.
2553 struct vm_speculative_age_q
*aq
;
2556 assert(m
->object
!= kernel_object
);
2557 assert(!m
->speculative
&& !m
->active
&& !m
->inactive
&& !m
->throttled
);
2558 assert(m
->phys_page
!= vm_page_guard_addr
);
2559 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2561 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2563 if (m
->wire_count
== 0) {
2566 clock_get_system_nanotime(&ts
.tv_sec
, (unsigned *)&ts
.tv_nsec
);
2568 if (vm_page_speculative_count
== 0) {
2570 speculative_age_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
2571 speculative_steal_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
2573 aq
= &vm_page_queue_speculative
[speculative_age_index
];
2576 * set the timer to begin a new group
2578 aq
->age_ts
.tv_sec
= VM_PAGE_SPECULATIVE_Q_AGE_MS
/ 1000;
2579 aq
->age_ts
.tv_nsec
= (VM_PAGE_SPECULATIVE_Q_AGE_MS
% 1000) * 1000 * NSEC_PER_USEC
;
2581 ADD_MACH_TIMESPEC(&aq
->age_ts
, &ts
);
2583 aq
= &vm_page_queue_speculative
[speculative_age_index
];
2585 if (CMP_MACH_TIMESPEC(&ts
, &aq
->age_ts
) >= 0) {
2587 speculative_age_index
++;
2589 if (speculative_age_index
> VM_PAGE_MAX_SPECULATIVE_AGE_Q
)
2590 speculative_age_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
2591 if (speculative_age_index
== speculative_steal_index
) {
2592 speculative_steal_index
= speculative_age_index
+ 1;
2594 if (speculative_steal_index
> VM_PAGE_MAX_SPECULATIVE_AGE_Q
)
2595 speculative_steal_index
= VM_PAGE_MIN_SPECULATIVE_AGE_Q
;
2597 aq
= &vm_page_queue_speculative
[speculative_age_index
];
2599 if (!queue_empty(&aq
->age_q
))
2600 vm_page_speculate_ageit(aq
);
2602 aq
->age_ts
.tv_sec
= VM_PAGE_SPECULATIVE_Q_AGE_MS
/ 1000;
2603 aq
->age_ts
.tv_nsec
= (VM_PAGE_SPECULATIVE_Q_AGE_MS
% 1000) * 1000 * NSEC_PER_USEC
;
2605 ADD_MACH_TIMESPEC(&aq
->age_ts
, &ts
);
2608 enqueue_tail(&aq
->age_q
, &m
->pageq
);
2609 m
->speculative
= TRUE
;
2610 vm_page_speculative_count
++;
2613 m
->object
->pages_created
++;
2614 vm_page_speculative_created
++;
2621 * move pages from the specified aging bin to
2622 * the speculative bin that pageout_scan claims from
2624 * The page queues must be locked.
2627 vm_page_speculate_ageit(struct vm_speculative_age_q
*aq
)
2629 struct vm_speculative_age_q
*sq
;
2632 sq
= &vm_page_queue_speculative
[VM_PAGE_SPECULATIVE_AGED_Q
];
2634 if (queue_empty(&sq
->age_q
)) {
2635 sq
->age_q
.next
= aq
->age_q
.next
;
2636 sq
->age_q
.prev
= aq
->age_q
.prev
;
2638 t
= (vm_page_t
)sq
->age_q
.next
;
2639 t
->pageq
.prev
= &sq
->age_q
;
2641 t
= (vm_page_t
)sq
->age_q
.prev
;
2642 t
->pageq
.next
= &sq
->age_q
;
2644 t
= (vm_page_t
)sq
->age_q
.prev
;
2645 t
->pageq
.next
= aq
->age_q
.next
;
2647 t
= (vm_page_t
)aq
->age_q
.next
;
2648 t
->pageq
.prev
= sq
->age_q
.prev
;
2650 t
= (vm_page_t
)aq
->age_q
.prev
;
2651 t
->pageq
.next
= &sq
->age_q
;
2653 sq
->age_q
.prev
= aq
->age_q
.prev
;
2655 queue_init(&aq
->age_q
);
2664 assert(m
->object
!= kernel_object
);
2665 assert(m
->phys_page
!= vm_page_guard_addr
);
2668 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2670 if (m
->active
|| m
->reference
)
2673 if (m
->private || (m
->wire_count
!= 0))
2676 m
->no_cache
= FALSE
;
2678 VM_PAGE_QUEUES_REMOVE(m
);
2680 assert(!m
->laundry
);
2681 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2683 queue_enter(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
2686 vm_page_inactive_count
++;
2687 token_new_pagecount
++;
2692 * vm_page_part_zero_fill:
2694 * Zero-fill a part of the page.
2697 vm_page_part_zero_fill(
2705 #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
2706 pmap_zero_part_page(m
->phys_page
, m_pa
, len
);
2709 tmp
= vm_page_grab();
2710 if (tmp
== VM_PAGE_NULL
) {
2711 vm_page_wait(THREAD_UNINT
);
2716 vm_page_zero_fill(tmp
);
2718 vm_page_part_copy(m
, 0, tmp
, 0, m_pa
);
2720 if((m_pa
+ len
) < PAGE_SIZE
) {
2721 vm_page_part_copy(m
, m_pa
+ len
, tmp
,
2722 m_pa
+ len
, PAGE_SIZE
- (m_pa
+ len
));
2724 vm_page_copy(tmp
,m
);
2725 vm_page_lock_queues();
2727 vm_page_unlock_queues();
2733 * vm_page_zero_fill:
2735 * Zero-fill the specified page.
2742 "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
2743 (integer_t
)m
->object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
2747 // dbgTrace(0xAEAEAEAE, m->phys_page, 0); /* (BRINGUP) */
2748 pmap_zero_page(m
->phys_page
);
2752 * vm_page_part_copy:
2754 * copy part of one page to another
2765 VM_PAGE_CHECK(src_m
);
2766 VM_PAGE_CHECK(dst_m
);
2768 pmap_copy_part_page(src_m
->phys_page
, src_pa
,
2769 dst_m
->phys_page
, dst_pa
, len
);
2775 * Copy one page to another
2778 * The source page should not be encrypted. The caller should
2779 * make sure the page is decrypted first, if necessary.
2782 int vm_page_copy_cs_validations
= 0;
2783 int vm_page_copy_cs_tainted
= 0;
2791 "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
2792 (integer_t
)src_m
->object
, src_m
->offset
,
2793 (integer_t
)dest_m
->object
, dest_m
->offset
,
2796 VM_PAGE_CHECK(src_m
);
2797 VM_PAGE_CHECK(dest_m
);
2801 * The source page should not be encrypted at this point.
2802 * The destination page will therefore not contain encrypted
2803 * data after the copy.
2805 if (src_m
->encrypted
) {
2806 panic("vm_page_copy: source page %p is encrypted\n", src_m
);
2808 dest_m
->encrypted
= FALSE
;
2810 if (src_m
->object
!= VM_OBJECT_NULL
&&
2811 src_m
->object
->code_signed
) {
2813 * We're copying a page from a code-signed object.
2814 * Whoever ends up mapping the copy page might care about
2815 * the original page's integrity, so let's validate the
2818 vm_page_copy_cs_validations
++;
2819 vm_page_validate_cs(src_m
);
2822 * Propagate the code-signing bits to the copy page.
2824 dest_m
->cs_validated
= src_m
->cs_validated
;
2825 dest_m
->cs_tainted
= src_m
->cs_tainted
;
2826 if (dest_m
->cs_tainted
) {
2827 assert(dest_m
->cs_validated
);
2828 vm_page_copy_cs_tainted
++;
2831 pmap_copy_page(src_m
->phys_page
, dest_m
->phys_page
);
2836 * Check that the list of pages is ordered by
2837 * ascending physical address and has no holes.
2840 vm_page_verify_contiguous(
2842 unsigned int npages
)
2844 register vm_page_t m
;
2845 unsigned int page_count
;
2846 vm_offset_t prev_addr
;
2848 prev_addr
= pages
->phys_page
;
2850 for (m
= NEXT_PAGE(pages
); m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2851 if (m
->phys_page
!= prev_addr
+ 1) {
2852 printf("m %p prev_addr 0x%x, current addr 0x%x\n",
2853 m
, prev_addr
, m
->phys_page
);
2854 printf("pages %p page_count %d\n", pages
, page_count
);
2855 panic("vm_page_verify_contiguous: not contiguous!");
2857 prev_addr
= m
->phys_page
;
2860 if (page_count
!= npages
) {
2861 printf("pages %p actual count 0x%x but requested 0x%x\n",
2862 pages
, page_count
, npages
);
2863 panic("vm_page_verify_contiguous: count error");
2867 #endif /* MACH_ASSERT */
2872 * Check the free lists for proper length etc.
2875 vm_page_verify_free_lists( void )
2877 unsigned int color
, npages
;
2883 mutex_lock(&vm_page_queue_free_lock
);
2885 for( color
= 0; color
< vm_colors
; color
++ ) {
2886 prev_m
= (vm_page_t
) &vm_page_queue_free
[color
];
2887 queue_iterate(&vm_page_queue_free
[color
],
2891 if ((vm_page_t
) m
->pageq
.prev
!= prev_m
)
2892 panic("vm_page_verify_free_lists: corrupted prev ptr");
2894 panic("vm_page_verify_free_lists: not free");
2896 panic("vm_page_verify_free_lists: not busy");
2897 if ( (m
->phys_page
& vm_color_mask
) != color
)
2898 panic("vm_page_verify_free_lists: wrong color");
2903 if (npages
!= vm_page_free_count
)
2904 panic("vm_page_verify_free_lists: npages %u free_count %d",
2905 npages
, vm_page_free_count
);
2907 mutex_unlock(&vm_page_queue_free_lock
);
2909 #endif /* MACH_ASSERT */
2914 * CONTIGUOUS PAGE ALLOCATION
2915 * Additional levels of effort:
2916 * + consider pages that are currently 'pmapped'
2917 * this could be expensive since we'd have
2918 * to ask the pmap layer about there state
2919 * + consider dirty pages
2920 * either clean them or
2921 * copy them to other locations...
2923 * Find a region large enough to contain at least n pages
2924 * of contiguous physical memory.
2926 * This is done by traversing the vm_page_t array in a linear fashion
2927 * we assume that the vm_page_t array has the avaiable physical pages in an
2928 * ordered, ascending list... this is currently true of all our implementations
2929 * and must remain so... there can be 'holes' in the array... we also can
2930 * no longer tolerate the vm_page_t's in the list being 'freed' and reclaimed
2931 * which use to happen via 'vm_page_convert'... that function was no longer
2932 * being called and was removed...
2934 * The basic flow consists of stabilizing some of the interesting state of
2935 * a vm_page_t behind the vm_page_queue and vm_page_free locks... we start our
2936 * sweep at the beginning of the array looking for pages that meet our criterea
2937 * for a 'stealable' page... currently we are pretty conservative... if the page
2938 * meets this criterea and is physically contiguous to the previous page in the 'run'
2939 * we keep developing it. If we hit a page that doesn't fit, we reset our state
2940 * and start to develop a new run... if at this point we've already considered
2941 * at least MAX_CONSIDERED_BEFORE_YIELD pages, we'll drop the 2 locks we hold,
2942 * and mutex_pause (which will yield the processor), to keep the latency low w/r
2943 * to other threads trying to acquire free pages (or move pages from q to q),
2944 * and then continue from the spot we left off... we only make 1 pass through the
2945 * array. Once we have a 'run' that is long enough, we'll go into the loop which
2946 * which steals the pages from the queues they're currently on... pages on the free
2947 * queue can be stolen directly... pages that are on any of the other queues
2948 * must be removed from the object they are tabled on... this requires taking the
2949 * object lock... we do this as a 'try' to prevent deadlocks... if the 'try' fails
2950 * or if the state of the page behind the vm_object lock is no longer viable, we'll
2951 * dump the pages we've currently stolen back to the free list, and pick up our
2952 * scan from the point where we aborted the 'current' run.
2956 * - neither vm_page_queue nor vm_free_list lock can be held on entry
2958 * Returns a pointer to a list of gobbled/wired pages or VM_PAGE_NULL.
2963 #define MAX_CONSIDERED_BEFORE_YIELD 1000
2966 #define RESET_STATE_OF_RUN() \
2968 prevcontaddr = -2; \
2969 free_considered = 0; \
2970 substitute_needed = 0; \
2976 vm_page_find_contiguous(
2977 unsigned int contig_pages
,
2982 ppnum_t prevcontaddr
;
2983 unsigned int npages
, considered
;
2984 unsigned int page_idx
, start_idx
;
2985 int free_considered
, free_available
;
2986 int substitute_needed
;
2988 uint32_t tv_start_sec
, tv_start_usec
, tv_end_sec
, tv_end_usec
;
2991 int stolen_pages
= 0;
2994 if (contig_pages
== 0)
2995 return VM_PAGE_NULL
;
2998 vm_page_verify_free_lists();
3000 clock_get_system_microtime(&tv_start_sec
, &tv_start_usec
);
3002 vm_page_lock_queues();
3003 mutex_lock(&vm_page_queue_free_lock
);
3005 RESET_STATE_OF_RUN();
3008 free_available
= vm_page_free_count
- vm_page_free_reserved
;
3010 for (page_idx
= 0, start_idx
= 0;
3011 npages
< contig_pages
&& page_idx
< vm_pages_count
;
3014 m
= &vm_pages
[page_idx
];
3016 if (max_pnum
&& m
->phys_page
> max_pnum
) {
3017 /* no more low pages... */
3020 if (m
->phys_page
<= vm_lopage_poolend
&&
3021 m
->phys_page
>= vm_lopage_poolstart
) {
3023 * don't want to take pages from our
3024 * reserved pool of low memory
3025 * so don't consider it which
3026 * means starting a new run
3028 RESET_STATE_OF_RUN();
3030 } else if (m
->wire_count
|| m
->gobbled
||
3031 m
->encrypted
|| m
->encrypted_cleaning
|| m
->cs_validated
|| m
->cs_tainted
||
3032 m
->error
|| m
->absent
|| m
->pageout_queue
|| m
->laundry
|| m
->wanted
|| m
->precious
||
3033 m
->cleaning
|| m
->overwriting
|| m
->restart
|| m
->unusual
|| m
->list_req_pending
) {
3035 * page is in a transient state
3036 * or a state we don't want to deal
3037 * with, so don't consider it which
3038 * means starting a new run
3040 RESET_STATE_OF_RUN();
3042 } else if (!m
->free
&& !m
->active
&& !m
->inactive
&& !m
->speculative
&& !m
->throttled
) {
3044 * page needs to be on one of our queues
3045 * in order for it to be stable behind the
3046 * locks we hold at this point...
3047 * if not, don't consider it which
3048 * means starting a new run
3050 RESET_STATE_OF_RUN();
3052 } else if (!m
->free
&& (!m
->tabled
|| m
->busy
)) {
3054 * pages on the free list are always 'busy'
3055 * so we couldn't test for 'busy' in the check
3056 * for the transient states... pages that are
3057 * 'free' are never 'tabled', so we also couldn't
3058 * test for 'tabled'. So we check here to make
3059 * sure that a non-free page is not busy and is
3060 * tabled on an object...
3061 * if not, don't consider it which
3062 * means starting a new run
3064 RESET_STATE_OF_RUN();
3067 if (m
->phys_page
!= prevcontaddr
+ 1) {
3069 start_idx
= page_idx
;
3073 prevcontaddr
= m
->phys_page
;
3075 if (m
->pmapped
|| m
->dirty
)
3076 substitute_needed
++;
3081 if ((free_considered
+ substitute_needed
) > free_available
) {
3083 * if we let this run continue
3084 * we will end up dropping the vm_page_free_count
3085 * below the reserve limit... we need to abort
3086 * this run, but we can at least re-consider this
3087 * page... thus the jump back to 'retry'
3089 RESET_STATE_OF_RUN();
3091 if (free_available
&& considered
<= MAX_CONSIDERED_BEFORE_YIELD
) {
3096 * free_available == 0
3097 * so can't consider any free pages... if
3098 * we went to retry in this case, we'd
3099 * get stuck looking at the same page
3100 * w/o making any forward progress
3101 * we also want to take this path if we've already
3102 * reached our limit that controls the lock latency
3106 if (considered
> MAX_CONSIDERED_BEFORE_YIELD
&& npages
<= 1) {
3108 mutex_unlock(&vm_page_queue_free_lock
);
3109 vm_page_unlock_queues();
3113 vm_page_lock_queues();
3114 mutex_lock(&vm_page_queue_free_lock
);
3116 RESET_STATE_OF_RUN();
3118 * reset our free page limit since we
3119 * dropped the lock protecting the vm_page_free_queue
3121 free_available
= vm_page_free_count
- vm_page_free_reserved
;
3132 if (npages
!= contig_pages
)
3133 mutex_unlock(&vm_page_queue_free_lock
);
3137 unsigned int cur_idx
;
3138 unsigned int tmp_start_idx
;
3139 vm_object_t locked_object
= VM_OBJECT_NULL
;
3140 boolean_t abort_run
= FALSE
;
3142 tmp_start_idx
= start_idx
;
3145 * first pass through to pull the free pages
3146 * off of the free queue so that in case we
3147 * need substitute pages, we won't grab any
3148 * of the free pages in the run... we'll clear
3149 * the 'free' bit in the 2nd pass, and even in
3150 * an abort_run case, we'll collect all of the
3151 * free pages in this run and return them to the free list
3153 while (start_idx
< page_idx
) {
3155 m1
= &vm_pages
[start_idx
++];
3160 color
= m1
->phys_page
& vm_color_mask
;
3161 queue_remove(&vm_page_queue_free
[color
],
3166 vm_page_free_count
--;
3170 * adjust global freelist counts
3172 if (vm_page_free_count
< vm_page_free_count_minimum
)
3173 vm_page_free_count_minimum
= vm_page_free_count
;
3176 * we can drop the free queue lock at this point since
3177 * we've pulled any 'free' candidates off of the list
3178 * we need it dropped so that we can do a vm_page_grab
3179 * when substituing for pmapped/dirty pages
3181 mutex_unlock(&vm_page_queue_free_lock
);
3183 start_idx
= tmp_start_idx
;
3184 cur_idx
= page_idx
- 1;
3186 while (start_idx
++ < page_idx
) {
3188 * must go through the list from back to front
3189 * so that the page list is created in the
3190 * correct order - low -> high phys addresses
3192 m1
= &vm_pages
[cur_idx
--];
3196 * pages have already been removed from
3197 * the free list in the 1st pass
3201 assert(!m1
->wanted
);
3202 assert(!m1
->laundry
);
3208 if (abort_run
== TRUE
)
3211 object
= m1
->object
;
3213 if (object
!= locked_object
) {
3214 if (locked_object
) {
3215 vm_object_unlock(locked_object
);
3216 locked_object
= VM_OBJECT_NULL
;
3218 if (vm_object_lock_try(object
))
3219 locked_object
= object
;
3221 if (locked_object
== VM_OBJECT_NULL
||
3222 (m1
->wire_count
|| m1
->gobbled
||
3223 m1
->encrypted
|| m1
->encrypted_cleaning
|| m1
->cs_validated
|| m1
->cs_tainted
||
3224 m1
->error
|| m1
->absent
|| m1
->pageout_queue
|| m1
->laundry
|| m1
->wanted
|| m1
->precious
||
3225 m1
->cleaning
|| m1
->overwriting
|| m1
->restart
|| m1
->unusual
|| m1
->list_req_pending
|| m1
->busy
)) {
3227 if (locked_object
) {
3228 vm_object_unlock(locked_object
);
3229 locked_object
= VM_OBJECT_NULL
;
3231 tmp_start_idx
= cur_idx
;
3235 if (m1
->pmapped
|| m1
->dirty
) {
3237 vm_object_offset_t offset
;
3239 m2
= vm_page_grab();
3241 if (m2
== VM_PAGE_NULL
) {
3242 if (locked_object
) {
3243 vm_object_unlock(locked_object
);
3244 locked_object
= VM_OBJECT_NULL
;
3246 tmp_start_idx
= cur_idx
;
3251 refmod
= pmap_disconnect(m1
->phys_page
);
3254 vm_page_copy(m1
, m2
);
3256 m2
->reference
= m1
->reference
;
3257 m2
->dirty
= m1
->dirty
;
3259 if (refmod
& VM_MEM_REFERENCED
)
3260 m2
->reference
= TRUE
;
3261 if (refmod
& VM_MEM_MODIFIED
)
3263 offset
= m1
->offset
;
3266 * completely cleans up the state
3267 * of the page so that it is ready
3268 * to be put onto the free list, or
3269 * for this purpose it looks like it
3270 * just came off of the free list
3272 vm_page_free_prepare(m1
);
3275 * make sure we clear the ref/mod state
3276 * from the pmap layer... else we risk
3277 * inheriting state from the last time
3278 * this page was used...
3280 pmap_clear_refmod(m2
->phys_page
, VM_MEM_MODIFIED
| VM_MEM_REFERENCED
);
3282 * now put the substitute page on the object
3284 vm_page_insert_internal(m2
, locked_object
, offset
, TRUE
);
3287 vm_page_activate(m2
);
3289 vm_page_deactivate(m2
);
3291 PAGE_WAKEUP_DONE(m2
);
3295 * completely cleans up the state
3296 * of the page so that it is ready
3297 * to be put onto the free list, or
3298 * for this purpose it looks like it
3299 * just came off of the free list
3301 vm_page_free_prepare(m1
);
3307 m1
->pageq
.next
= (queue_entry_t
) m
;
3308 m1
->pageq
.prev
= NULL
;
3311 if (locked_object
) {
3312 vm_object_unlock(locked_object
);
3313 locked_object
= VM_OBJECT_NULL
;
3316 if (abort_run
== TRUE
) {
3317 if (m
!= VM_PAGE_NULL
) {
3318 vm_page_free_list(m
);
3324 * want the index of the last
3325 * page in this run that was
3326 * successfully 'stolen', so back
3327 * it up 1 for the auto-decrement on use
3328 * and 1 more to bump back over this page
3330 page_idx
= tmp_start_idx
+ 2;
3332 if (page_idx
>= vm_pages_count
)
3335 mutex_lock(&vm_page_queue_free_lock
);
3337 RESET_STATE_OF_RUN();
3340 * reset our free page limit since we
3341 * dropped the lock protecting the vm_page_free_queue
3343 free_available
= vm_page_free_count
- vm_page_free_reserved
;
3348 for (m1
= m
; m1
!= VM_PAGE_NULL
; m1
= NEXT_PAGE(m1
)) {
3356 vm_page_gobble_count
+= npages
;
3359 * gobbled pages are also counted as wired pages
3361 vm_page_wire_count
+= npages
;
3363 assert(vm_page_verify_contiguous(m
, npages
));
3366 vm_page_unlock_queues();
3369 clock_get_system_microtime(&tv_end_sec
, &tv_end_usec
);
3371 tv_end_sec
-= tv_start_sec
;
3372 if (tv_end_usec
< tv_start_usec
) {
3374 tv_end_usec
+= 1000000;
3376 tv_end_usec
-= tv_start_usec
;
3377 if (tv_end_usec
>= 1000000) {
3379 tv_end_sec
-= 1000000;
3381 printf("vm_find_page_contiguous(num=%d,low=%d): found %d pages in %d.%06ds... scanned %d pages... yielded %d times... dumped run %d times... stole %d pages\n",
3382 contig_pages
, max_pnum
, npages
, tv_end_sec
, tv_end_usec
, page_idx
, yielded
, dumped_run
, stolen_pages
);
3384 vm_page_verify_free_lists();
3390 * Allocate a list of contiguous, wired pages.
3400 unsigned int npages
;
3402 if (size
% page_size
!= 0)
3403 return KERN_INVALID_ARGUMENT
;
3405 npages
= size
/ page_size
;
3408 * Obtain a pointer to a subset of the free
3409 * list large enough to satisfy the request;
3410 * the region will be physically contiguous.
3412 pages
= vm_page_find_contiguous(npages
, max_pnum
, wire
);
3414 if (pages
== VM_PAGE_NULL
)
3415 return KERN_NO_SPACE
;
3417 * determine need for wakeups
3419 if ((vm_page_free_count
< vm_page_free_min
) ||
3420 ((vm_page_free_count
< vm_page_free_target
) &&
3421 ((vm_page_inactive_count
+ vm_page_speculative_count
) < vm_page_inactive_min
)))
3422 thread_wakeup((event_t
) &vm_page_free_wanted
);
3429 * Decide if we need to poke the memorystatus notification thread.
3432 (vm_page_active_count
+ vm_page_inactive_count
+
3433 vm_page_speculative_count
+ vm_page_free_count
+
3434 vm_page_purgeable_count
) * 100 /
3436 if (percent_avail
<= (kern_memorystatus_level
- 5)) {
3437 kern_memorystatus_level
= percent_avail
;
3438 thread_wakeup((event_t
)&kern_memorystatus_wakeup
);
3443 * The CPM pages should now be available and
3444 * ordered by ascending physical address.
3446 assert(vm_page_verify_contiguous(pages
, npages
));
3449 return KERN_SUCCESS
;
3453 #include <mach_vm_debug.h>
3456 #include <mach_debug/hash_info.h>
3457 #include <vm/vm_debug.h>
3460 * Routine: vm_page_info
3462 * Return information about the global VP table.
3463 * Fills the buffer with as much information as possible
3464 * and returns the desired size of the buffer.
3466 * Nothing locked. The caller should provide
3467 * possibly-pageable memory.
3472 hash_info_bucket_t
*info
,
3477 if (vm_page_bucket_count
< count
)
3478 count
= vm_page_bucket_count
;
3480 for (i
= 0; i
< count
; i
++) {
3481 vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
3482 unsigned int bucket_count
= 0;
3485 simple_lock(&vm_page_bucket_lock
);
3486 for (m
= bucket
->pages
; m
!= VM_PAGE_NULL
; m
= m
->next
)
3488 simple_unlock(&vm_page_bucket_lock
);
3490 /* don't touch pageable memory while holding locks */
3491 info
[i
].hib_count
= bucket_count
;
3494 return vm_page_bucket_count
;
3496 #endif /* MACH_VM_DEBUG */
3498 #include <mach_kdb.h>
3501 #include <ddb/db_output.h>
3502 #include <vm/vm_print.h>
3503 #define printf kdbprintf
3506 * Routine: vm_page_print [exported]
3514 p
= (vm_page_t
) (long) db_addr
;
3516 iprintf("page 0x%x\n", p
);
3520 iprintf("object=0x%x", p
->object
);
3521 printf(", offset=0x%x", p
->offset
);
3522 printf(", wire_count=%d", p
->wire_count
);
3524 iprintf("%sinactive, %sactive, %sthrottled, %sgobbled, %slaundry, %sfree, %sref, %sencrypted\n",
3525 (p
->inactive
? "" : "!"),
3526 (p
->active
? "" : "!"),
3527 (p
->throttled
? "" : "!"),
3528 (p
->gobbled
? "" : "!"),
3529 (p
->laundry
? "" : "!"),
3530 (p
->free
? "" : "!"),
3531 (p
->reference
? "" : "!"),
3532 (p
->encrypted
? "" : "!"));
3533 iprintf("%sbusy, %swanted, %stabled, %sfictitious, %sprivate, %sprecious\n",
3534 (p
->busy
? "" : "!"),
3535 (p
->wanted
? "" : "!"),
3536 (p
->tabled
? "" : "!"),
3537 (p
->fictitious
? "" : "!"),
3538 (p
->private ? "" : "!"),
3539 (p
->precious
? "" : "!"));
3540 iprintf("%sabsent, %serror, %sdirty, %scleaning, %spageout, %sclustered\n",
3541 (p
->absent
? "" : "!"),
3542 (p
->error
? "" : "!"),
3543 (p
->dirty
? "" : "!"),
3544 (p
->cleaning
? "" : "!"),
3545 (p
->pageout
? "" : "!"),
3546 (p
->clustered
? "" : "!"));
3547 iprintf("%soverwriting, %srestart, %sunusual\n",
3548 (p
->overwriting
? "" : "!"),
3549 (p
->restart
? "" : "!"),
3550 (p
->unusual
? "" : "!"));
3552 iprintf("phys_page=0x%x", p
->phys_page
);
3556 #endif /* MACH_KDB */