2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr., Michael Wayne Young
56 * Resident memory management module.
61 #include <mach/clock_types.h>
62 #include <mach/vm_prot.h>
63 #include <mach/vm_statistics.h>
64 #include <kern/counters.h>
65 #include <kern/sched_prim.h>
66 #include <kern/task.h>
67 #include <kern/thread.h>
68 #include <kern/zalloc.h>
71 #include <vm/vm_init.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/vm_kern.h> /* kernel_memory_allocate() */
76 #include <kern/misc_protos.h>
77 #include <zone_debug.h>
79 #include <ppc/mappings.h> /* (BRINGUP) */
80 #include <pexpert/pexpert.h> /* (BRINGUP) */
82 #include <vm/vm_protos.h>
84 /* Variables used to indicate the relative age of pages in the
88 unsigned int vm_page_ticket_roll
= 0;
89 unsigned int vm_page_ticket
= 0;
91 * Associated with page of user-allocatable memory is a
96 * These variables record the values returned by vm_page_bootstrap,
97 * for debugging purposes. The implementation of pmap_steal_memory
98 * and pmap_startup here also uses them internally.
101 vm_offset_t virtual_space_start
;
102 vm_offset_t virtual_space_end
;
106 * The vm_page_lookup() routine, which provides for fast
107 * (virtual memory object, offset) to page lookup, employs
108 * the following hash table. The vm_page_{insert,remove}
109 * routines install and remove associations in the table.
110 * [This table is often called the virtual-to-physical,
115 #if MACH_PAGE_HASH_STATS
116 int cur_count
; /* current count */
117 int hi_count
; /* high water mark */
118 #endif /* MACH_PAGE_HASH_STATS */
121 vm_page_bucket_t
*vm_page_buckets
; /* Array of buckets */
122 unsigned int vm_page_bucket_count
= 0; /* How big is array? */
123 unsigned int vm_page_hash_mask
; /* Mask for hash function */
124 unsigned int vm_page_hash_shift
; /* Shift for hash function */
125 uint32_t vm_page_bucket_hash
; /* Basic bucket hash */
126 decl_simple_lock_data(,vm_page_bucket_lock
)
129 vm_page_lookup_nohint(vm_object_t object
, vm_object_offset_t offset
);
132 #if MACH_PAGE_HASH_STATS
133 /* This routine is only for debug. It is intended to be called by
134 * hand by a developer using a kernel debugger. This routine prints
135 * out vm_page_hash table statistics to the kernel debug console.
145 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
146 if (vm_page_buckets
[i
].hi_count
) {
148 highsum
+= vm_page_buckets
[i
].hi_count
;
149 if (vm_page_buckets
[i
].hi_count
> maxdepth
)
150 maxdepth
= vm_page_buckets
[i
].hi_count
;
153 printf("Total number of buckets: %d\n", vm_page_bucket_count
);
154 printf("Number used buckets: %d = %d%%\n",
155 numbuckets
, 100*numbuckets
/vm_page_bucket_count
);
156 printf("Number unused buckets: %d = %d%%\n",
157 vm_page_bucket_count
- numbuckets
,
158 100*(vm_page_bucket_count
-numbuckets
)/vm_page_bucket_count
);
159 printf("Sum of bucket max depth: %d\n", highsum
);
160 printf("Average bucket depth: %d.%2d\n",
161 highsum
/vm_page_bucket_count
,
162 highsum%vm_page_bucket_count
);
163 printf("Maximum bucket depth: %d\n", maxdepth
);
165 #endif /* MACH_PAGE_HASH_STATS */
168 * The virtual page size is currently implemented as a runtime
169 * variable, but is constant once initialized using vm_set_page_size.
170 * This initialization must be done in the machine-dependent
171 * bootstrap sequence, before calling other machine-independent
174 * All references to the virtual page size outside this
175 * module must use the PAGE_SIZE, PAGE_MASK and PAGE_SHIFT
178 vm_size_t page_size
= PAGE_SIZE
;
179 vm_size_t page_mask
= PAGE_MASK
;
180 int page_shift
= PAGE_SHIFT
;
183 * Resident page structures are initialized from
184 * a template (see vm_page_alloc).
186 * When adding a new field to the virtual memory
187 * object structure, be sure to add initialization
188 * (see vm_page_bootstrap).
190 struct vm_page vm_page_template
;
193 * Resident pages that represent real memory
194 * are allocated from a free list.
196 vm_page_t vm_page_queue_free
;
197 vm_page_t vm_page_queue_fictitious
;
198 unsigned int vm_page_free_wanted
;
199 unsigned int vm_page_free_count
;
200 unsigned int vm_page_fictitious_count
;
202 unsigned int vm_page_free_count_minimum
; /* debugging */
205 * Occasionally, the virtual memory system uses
206 * resident page structures that do not refer to
207 * real pages, for example to leave a page with
208 * important state information in the VP table.
210 * These page structures are allocated the way
211 * most other kernel structures are.
214 decl_mutex_data(,vm_page_alloc_lock
)
215 unsigned int io_throttle_zero_fill
;
218 * Fictitious pages don't have a physical address,
219 * but we must initialize phys_page to something.
220 * For debugging, this should be a strange value
221 * that the pmap module can recognize in assertions.
223 vm_offset_t vm_page_fictitious_addr
= (vm_offset_t
) -1;
226 * Resident page structures are also chained on
227 * queues that are used by the page replacement
228 * system (pageout daemon). These queues are
229 * defined here, but are shared by the pageout
230 * module. The inactive queue is broken into
231 * inactive and zf for convenience as the
232 * pageout daemon often assignes a higher
233 * affinity to zf pages
235 queue_head_t vm_page_queue_active
;
236 queue_head_t vm_page_queue_inactive
;
237 unsigned int vm_page_active_count
;
238 unsigned int vm_page_inactive_count
;
239 unsigned int vm_page_wire_count
;
240 unsigned int vm_page_gobble_count
= 0;
241 unsigned int vm_page_wire_count_warning
= 0;
242 unsigned int vm_page_gobble_count_warning
= 0;
244 unsigned int vm_page_purgeable_count
= 0; /* # of pages purgeable now */
245 uint64_t vm_page_purged_count
= 0; /* total count of purged pages */
247 ppnum_t vm_lopage_poolstart
= 0;
248 ppnum_t vm_lopage_poolend
= 0;
249 int vm_lopage_poolsize
= 0;
250 uint64_t max_valid_dma_address
= 0xffffffffffffffffULL
;
254 * Several page replacement parameters are also
255 * shared with this module, so that page allocation
256 * (done here in vm_page_alloc) can trigger the
259 unsigned int vm_page_free_target
= 0;
260 unsigned int vm_page_free_min
= 0;
261 unsigned int vm_page_inactive_target
= 0;
262 unsigned int vm_page_free_reserved
= 0;
263 unsigned int vm_page_throttled_count
= 0;
266 * The VM system has a couple of heuristics for deciding
267 * that pages are "uninteresting" and should be placed
268 * on the inactive queue as likely candidates for replacement.
269 * These variables let the heuristics be controlled at run-time
270 * to make experimentation easier.
273 boolean_t vm_page_deactivate_hint
= TRUE
;
278 * Sets the page size, perhaps based upon the memory
279 * size. Must be called before any use of page-size
280 * dependent functions.
282 * Sets page_shift and page_mask from page_size.
285 vm_set_page_size(void)
287 page_mask
= page_size
- 1;
289 if ((page_mask
& page_size
) != 0)
290 panic("vm_set_page_size: page size not a power of two");
292 for (page_shift
= 0; ; page_shift
++)
293 if ((1U << page_shift
) == page_size
)
300 * Initializes the resident memory module.
302 * Allocates memory for the page cells, and
303 * for the object/offset-to-page hash table headers.
304 * Each page cell is initialized and placed on the free list.
305 * Returns the range of available kernel virtual memory.
313 register vm_page_t m
;
320 * Initialize the vm_page template.
323 m
= &vm_page_template
;
324 m
->object
= VM_OBJECT_NULL
; /* reset later */
325 m
->offset
= (vm_object_offset_t
) -1; /* reset later */
328 m
->pageq
.next
= NULL
;
329 m
->pageq
.prev
= NULL
;
330 m
->listq
.next
= NULL
;
331 m
->listq
.prev
= NULL
;
338 m
->reference
= FALSE
;
340 m
->dump_cleaning
= FALSE
;
341 m
->list_req_pending
= FALSE
;
346 m
->fictitious
= FALSE
;
353 m
->clustered
= FALSE
;
354 m
->lock_supplied
= FALSE
;
357 m
->zero_fill
= FALSE
;
358 m
->encrypted
= FALSE
;
360 m
->phys_page
= 0; /* reset later */
362 m
->page_lock
= VM_PROT_NONE
;
363 m
->unlock_request
= VM_PROT_NONE
;
364 m
->page_error
= KERN_SUCCESS
;
367 * Initialize the page queues.
370 mutex_init(&vm_page_queue_free_lock
, 0);
371 mutex_init(&vm_page_queue_lock
, 0);
373 vm_page_queue_free
= VM_PAGE_NULL
;
374 vm_page_queue_fictitious
= VM_PAGE_NULL
;
375 queue_init(&vm_page_queue_active
);
376 queue_init(&vm_page_queue_inactive
);
377 queue_init(&vm_page_queue_zf
);
379 vm_page_free_wanted
= 0;
382 * Steal memory for the map and zone subsystems.
385 vm_map_steal_memory();
389 * Allocate (and initialize) the virtual-to-physical
390 * table hash buckets.
392 * The number of buckets should be a power of two to
393 * get a good hash function. The following computation
394 * chooses the first power of two that is greater
395 * than the number of physical pages in the system.
398 simple_lock_init(&vm_page_bucket_lock
, 0);
400 if (vm_page_bucket_count
== 0) {
401 unsigned int npages
= pmap_free_pages();
403 vm_page_bucket_count
= 1;
404 while (vm_page_bucket_count
< npages
)
405 vm_page_bucket_count
<<= 1;
408 vm_page_hash_mask
= vm_page_bucket_count
- 1;
411 * Calculate object shift value for hashing algorithm:
412 * O = log2(sizeof(struct vm_object))
413 * B = log2(vm_page_bucket_count)
414 * hash shifts the object left by
417 size
= vm_page_bucket_count
;
418 for (log1
= 0; size
> 1; log1
++)
420 size
= sizeof(struct vm_object
);
421 for (log2
= 0; size
> 1; log2
++)
423 vm_page_hash_shift
= log1
/2 - log2
+ 1;
425 vm_page_bucket_hash
= 1 << ((log1
+ 1) >> 1); /* Get (ceiling of sqrt of table size) */
426 vm_page_bucket_hash
|= 1 << ((log1
+ 1) >> 2); /* Get (ceiling of quadroot of table size) */
427 vm_page_bucket_hash
|= 1; /* Set bit and add 1 - always must be 1 to insure unique series */
429 if (vm_page_hash_mask
& vm_page_bucket_count
)
430 printf("vm_page_bootstrap: WARNING -- strange page hash\n");
432 vm_page_buckets
= (vm_page_bucket_t
*)
433 pmap_steal_memory(vm_page_bucket_count
*
434 sizeof(vm_page_bucket_t
));
436 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
437 register vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
439 bucket
->pages
= VM_PAGE_NULL
;
440 #if MACH_PAGE_HASH_STATS
441 bucket
->cur_count
= 0;
442 bucket
->hi_count
= 0;
443 #endif /* MACH_PAGE_HASH_STATS */
447 * Machine-dependent code allocates the resident page table.
448 * It uses vm_page_init to initialize the page frames.
449 * The code also returns to us the virtual space available
450 * to the kernel. We don't trust the pmap module
451 * to get the alignment right.
454 pmap_startup(&virtual_space_start
, &virtual_space_end
);
455 virtual_space_start
= round_page(virtual_space_start
);
456 virtual_space_end
= trunc_page(virtual_space_end
);
458 *startp
= virtual_space_start
;
459 *endp
= virtual_space_end
;
462 * Compute the initial "wire" count.
463 * Up until now, the pages which have been set aside are not under
464 * the VM system's control, so although they aren't explicitly
465 * wired, they nonetheless can't be moved. At this moment,
466 * all VM managed pages are "free", courtesy of pmap_startup.
468 vm_page_wire_count
= atop_64(max_mem
) - vm_page_free_count
; /* initial value */
470 printf("vm_page_bootstrap: %d free pages\n", vm_page_free_count
);
471 vm_page_free_count_minimum
= vm_page_free_count
;
473 simple_lock_init(&vm_paging_lock
, 0);
476 #ifndef MACHINE_PAGES
478 * We implement pmap_steal_memory and pmap_startup with the help
479 * of two simpler functions, pmap_virtual_space and pmap_next_page.
486 vm_offset_t addr
, vaddr
;
490 * We round the size to a round multiple.
493 size
= (size
+ sizeof (void *) - 1) &~ (sizeof (void *) - 1);
496 * If this is the first call to pmap_steal_memory,
497 * we have to initialize ourself.
500 if (virtual_space_start
== virtual_space_end
) {
501 pmap_virtual_space(&virtual_space_start
, &virtual_space_end
);
504 * The initial values must be aligned properly, and
505 * we don't trust the pmap module to do it right.
508 virtual_space_start
= round_page(virtual_space_start
);
509 virtual_space_end
= trunc_page(virtual_space_end
);
513 * Allocate virtual memory for this request.
516 addr
= virtual_space_start
;
517 virtual_space_start
+= size
;
519 kprintf("pmap_steal_memory: %08X - %08X; size=%08X\n", addr
, virtual_space_start
, size
); /* (TEST/DEBUG) */
522 * Allocate and map physical pages to back new virtual pages.
525 for (vaddr
= round_page(addr
);
527 vaddr
+= PAGE_SIZE
) {
528 if (!pmap_next_page(&phys_page
))
529 panic("pmap_steal_memory");
532 * XXX Logically, these mappings should be wired,
533 * but some pmap modules barf if they are.
536 pmap_enter(kernel_pmap
, vaddr
, phys_page
,
537 VM_PROT_READ
|VM_PROT_WRITE
,
538 VM_WIMG_USE_DEFAULT
, FALSE
);
540 * Account for newly stolen memory
542 vm_page_wire_count
++;
546 return (void *) addr
;
554 unsigned int i
, npages
, pages_initialized
, fill
, fillval
;
558 unsigned int num_of_lopages
= 0;
559 unsigned int last_index
;
562 * We calculate how many page frames we will have
563 * and then allocate the page structures in one chunk.
566 tmpaddr
= (addr64_t
)pmap_free_pages() * (addr64_t
)PAGE_SIZE
; /* Get the amount of memory left */
567 tmpaddr
= tmpaddr
+ (addr64_t
)(round_page_32(virtual_space_start
) - virtual_space_start
); /* Account for any slop */
568 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 */
570 pages
= (vm_page_t
) pmap_steal_memory(npages
* sizeof *pages
);
573 * Initialize the page frames.
575 for (i
= 0, pages_initialized
= 0; i
< npages
; i
++) {
576 if (!pmap_next_page(&phys_page
))
579 vm_page_init(&pages
[i
], phys_page
);
585 * Check if we want to initialize pages to a known value
587 fill
= 0; /* Assume no fill */
588 if (PE_parse_boot_arg("fill", &fillval
)) fill
= 1; /* Set fill */
591 * if vm_lopage_poolsize is non-zero, than we need to reserve
592 * a pool of pages whose addresess are less than 4G... this pool
593 * is used by drivers whose hardware can't DMA beyond 32 bits...
595 * note that I'm assuming that the page list is ascending and
596 * ordered w/r to the physical address
598 for (i
= 0, num_of_lopages
= vm_lopage_poolsize
; num_of_lopages
&& i
< pages_initialized
; num_of_lopages
--, i
++) {
603 if (m
->phys_page
>= (1 << (32 - PAGE_SHIFT
)))
604 panic("couldn't reserve the lopage pool: not enough lo pages\n");
606 if (m
->phys_page
< vm_lopage_poolend
)
607 panic("couldn't reserve the lopage pool: page list out of order\n");
609 vm_lopage_poolend
= m
->phys_page
;
611 if (vm_lopage_poolstart
== 0)
612 vm_lopage_poolstart
= m
->phys_page
;
614 if (m
->phys_page
< vm_lopage_poolstart
)
615 panic("couldn't reserve the lopage pool: page list out of order\n");
619 fillPage(m
->phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
625 // -debug code remove
626 if (2 == vm_himemory_mode
) {
627 // free low -> high so high is preferred
628 for (i
= last_index
+ 1; i
<= pages_initialized
; i
++) {
629 if(fill
) fillPage(pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
630 vm_page_release(&pages
[i
- 1]);
634 // debug code remove-
637 * Release pages in reverse order so that physical pages
638 * initially get allocated in ascending addresses. This keeps
639 * the devices (which must address physical memory) happy if
640 * they require several consecutive pages.
642 for (i
= pages_initialized
; i
> last_index
; i
--) {
643 if(fill
) fillPage(pages
[i
- 1].phys_page
, fillval
); /* Fill the page with a know value if requested at boot */
644 vm_page_release(&pages
[i
- 1]);
649 vm_page_t xx
, xxo
, xxl
;
652 j
= 0; /* (BRINGUP) */
655 for(xx
= vm_page_queue_free
; xx
; xxl
= xx
, xx
= xx
->pageq
.next
) { /* (BRINGUP) */
657 if(j
> vm_page_free_count
) { /* (BRINGUP) */
658 panic("pmap_startup: too many pages, xx = %08X, xxl = %08X\n", xx
, xxl
);
661 l
= vm_page_free_count
- j
; /* (BRINGUP) */
662 k
= 0; /* (BRINGUP) */
664 if(((j
- 1) & 0xFFFF) == 0) kprintf("checking number %d of %d\n", j
, vm_page_free_count
);
666 for(xxo
= xx
->pageq
.next
; xxo
; xxo
= xxo
->pageq
.next
) { /* (BRINGUP) */
668 if(k
> l
) panic("pmap_startup: too many in secondary check %d %d\n", k
, l
);
669 if((xx
->phys_page
& 0xFFFFFFFF) == (xxo
->phys_page
& 0xFFFFFFFF)) { /* (BRINGUP) */
670 panic("pmap_startup: duplicate physaddr, xx = %08X, xxo = %08X\n", xx
, xxo
);
675 if(j
!= vm_page_free_count
) { /* (BRINGUP) */
676 panic("pmap_startup: vm_page_free_count does not match, calc = %d, vm_page_free_count = %08X\n", j
, vm_page_free_count
);
683 * We have to re-align virtual_space_start,
684 * because pmap_steal_memory has been using it.
687 virtual_space_start
= round_page_32(virtual_space_start
);
689 *startp
= virtual_space_start
;
690 *endp
= virtual_space_end
;
692 #endif /* MACHINE_PAGES */
695 * Routine: vm_page_module_init
697 * Second initialization pass, to be done after
698 * the basic VM system is ready.
701 vm_page_module_init(void)
703 vm_page_zone
= zinit((vm_size_t
) sizeof(struct vm_page
),
704 0, PAGE_SIZE
, "vm pages");
707 zone_debug_disable(vm_page_zone
);
708 #endif /* ZONE_DEBUG */
710 zone_change(vm_page_zone
, Z_EXPAND
, FALSE
);
711 zone_change(vm_page_zone
, Z_EXHAUST
, TRUE
);
712 zone_change(vm_page_zone
, Z_FOREIGN
, TRUE
);
715 * Adjust zone statistics to account for the real pages allocated
716 * in vm_page_create(). [Q: is this really what we want?]
718 vm_page_zone
->count
+= vm_page_pages
;
719 vm_page_zone
->cur_size
+= vm_page_pages
* vm_page_zone
->elem_size
;
721 mutex_init(&vm_page_alloc_lock
, 0);
725 * Routine: vm_page_create
727 * After the VM system is up, machine-dependent code
728 * may stumble across more physical memory. For example,
729 * memory that it was reserving for a frame buffer.
730 * vm_page_create turns this memory into available pages.
741 for (phys_page
= start
;
744 while ((m
= (vm_page_t
) vm_page_grab_fictitious())
746 vm_page_more_fictitious();
748 vm_page_init(m
, phys_page
);
757 * Distributes the object/offset key pair among hash buckets.
759 * NOTE: The bucket count must be a power of 2
761 #define vm_page_hash(object, offset) (\
762 ( (natural_t)((uint32_t)object * vm_page_bucket_hash) + ((uint32_t)atop_64(offset) ^ vm_page_bucket_hash))\
766 * vm_page_insert: [ internal use only ]
768 * Inserts the given mem entry into the object/object-page
769 * table and object list.
771 * The object must be locked.
776 register vm_page_t mem
,
777 register vm_object_t object
,
778 register vm_object_offset_t offset
)
780 register vm_page_bucket_t
*bucket
;
783 "vm_page_insert, object 0x%X offset 0x%X page 0x%X\n",
784 (integer_t
)object
, (integer_t
)offset
, (integer_t
)mem
, 0,0);
788 _mutex_assert(&object
->Lock
, MA_OWNED
);
790 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
791 panic("vm_page_insert: page %p for (obj=%p,off=0x%llx) "
792 "already in (obj=%p,off=0x%llx)",
793 mem
, object
, offset
, mem
->object
, mem
->offset
);
795 assert(!object
->internal
|| offset
< object
->size
);
797 /* only insert "pageout" pages into "pageout" objects,
798 * and normal pages into normal objects */
799 assert(object
->pageout
== mem
->pageout
);
801 assert(vm_page_lookup(object
, offset
) == VM_PAGE_NULL
);
804 * Record the object/offset pair in this page
807 mem
->object
= object
;
808 mem
->offset
= offset
;
811 * Insert it into the object_object/offset hash table
814 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
815 simple_lock(&vm_page_bucket_lock
);
816 mem
->next
= bucket
->pages
;
818 #if MACH_PAGE_HASH_STATS
819 if (++bucket
->cur_count
> bucket
->hi_count
)
820 bucket
->hi_count
= bucket
->cur_count
;
821 #endif /* MACH_PAGE_HASH_STATS */
822 simple_unlock(&vm_page_bucket_lock
);
825 * Now link into the object's list of backed pages.
828 VM_PAGE_INSERT(mem
, object
);
832 * Show that the object has one more resident page.
835 object
->resident_page_count
++;
837 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
838 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
839 vm_page_lock_queues();
840 vm_page_purgeable_count
++;
841 vm_page_unlock_queues();
848 * Exactly like vm_page_insert, except that we first
849 * remove any existing page at the given offset in object.
851 * The object and page queues must be locked.
856 register vm_page_t mem
,
857 register vm_object_t object
,
858 register vm_object_offset_t offset
)
860 vm_page_bucket_t
*bucket
;
861 vm_page_t found_m
= VM_PAGE_NULL
;
865 _mutex_assert(&object
->Lock
, MA_OWNED
);
866 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
868 if (mem
->tabled
|| mem
->object
!= VM_OBJECT_NULL
)
869 panic("vm_page_replace: page %p for (obj=%p,off=0x%llx) "
870 "already in (obj=%p,off=0x%llx)",
871 mem
, object
, offset
, mem
->object
, mem
->offset
);
874 * Record the object/offset pair in this page
877 mem
->object
= object
;
878 mem
->offset
= offset
;
881 * Insert it into the object_object/offset hash table,
882 * replacing any page that might have been there.
885 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
886 simple_lock(&vm_page_bucket_lock
);
889 vm_page_t
*mp
= &bucket
->pages
;
890 register vm_page_t m
= *mp
;
893 if (m
->object
== object
&& m
->offset
== offset
) {
895 * Remove old page from hash list
905 mem
->next
= bucket
->pages
;
907 mem
->next
= VM_PAGE_NULL
;
910 * insert new page at head of hash list
914 simple_unlock(&vm_page_bucket_lock
);
918 * there was already a page at the specified
919 * offset for this object... remove it from
920 * the object and free it back to the free list
922 VM_PAGE_REMOVE(found_m
);
923 found_m
->tabled
= FALSE
;
925 found_m
->object
= VM_OBJECT_NULL
;
926 found_m
->offset
= (vm_object_offset_t
) -1;
927 object
->resident_page_count
--;
929 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
930 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
931 assert(vm_page_purgeable_count
> 0);
932 vm_page_purgeable_count
--;
936 * Return page to the free list.
937 * Note the page is not tabled now
939 vm_page_free(found_m
);
942 * Now link into the object's list of backed pages.
945 VM_PAGE_INSERT(mem
, object
);
949 * And show that the object has one more resident
953 object
->resident_page_count
++;
955 if (object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
956 object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
957 vm_page_purgeable_count
++;
962 * vm_page_remove: [ internal use only ]
964 * Removes the given mem entry from the object/offset-page
965 * table and the object page list.
967 * The object and page queues must be locked.
972 register vm_page_t mem
)
974 register vm_page_bucket_t
*bucket
;
975 register vm_page_t
this;
978 "vm_page_remove, object 0x%X offset 0x%X page 0x%X\n",
979 (integer_t
)mem
->object
, (integer_t
)mem
->offset
,
980 (integer_t
)mem
, 0,0);
982 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
983 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
986 assert(!mem
->cleaning
);
991 * Remove from the object_object/offset hash table
994 bucket
= &vm_page_buckets
[vm_page_hash(mem
->object
, mem
->offset
)];
995 simple_lock(&vm_page_bucket_lock
);
996 if ((this = bucket
->pages
) == mem
) {
997 /* optimize for common case */
999 bucket
->pages
= mem
->next
;
1001 register vm_page_t
*prev
;
1003 for (prev
= &this->next
;
1004 (this = *prev
) != mem
;
1009 #if MACH_PAGE_HASH_STATS
1010 bucket
->cur_count
--;
1011 #endif /* MACH_PAGE_HASH_STATS */
1012 simple_unlock(&vm_page_bucket_lock
);
1015 * Now remove from the object's list of backed pages.
1018 VM_PAGE_REMOVE(mem
);
1021 * And show that the object has one fewer resident
1025 mem
->object
->resident_page_count
--;
1027 if (mem
->object
->purgable
== VM_OBJECT_PURGABLE_VOLATILE
||
1028 mem
->object
->purgable
== VM_OBJECT_PURGABLE_EMPTY
) {
1029 assert(vm_page_purgeable_count
> 0);
1030 vm_page_purgeable_count
--;
1033 mem
->tabled
= FALSE
;
1034 mem
->object
= VM_OBJECT_NULL
;
1035 mem
->offset
= (vm_object_offset_t
) -1;
1041 * Returns the page associated with the object/offset
1042 * pair specified; if none is found, VM_PAGE_NULL is returned.
1044 * The object must be locked. No side effects.
1047 unsigned long vm_page_lookup_hint
= 0;
1048 unsigned long vm_page_lookup_hint_next
= 0;
1049 unsigned long vm_page_lookup_hint_prev
= 0;
1050 unsigned long vm_page_lookup_hint_miss
= 0;
1054 register vm_object_t object
,
1055 register vm_object_offset_t offset
)
1057 register vm_page_t mem
;
1058 register vm_page_bucket_t
*bucket
;
1061 _mutex_assert(&object
->Lock
, MA_OWNED
);
1064 mem
= object
->memq_hint
;
1065 if (mem
!= VM_PAGE_NULL
) {
1066 assert(mem
->object
== object
);
1067 if (mem
->offset
== offset
) {
1068 vm_page_lookup_hint
++;
1071 qe
= queue_next(&mem
->listq
);
1072 if (! queue_end(&object
->memq
, qe
)) {
1073 vm_page_t next_page
;
1075 next_page
= (vm_page_t
) qe
;
1076 assert(next_page
->object
== object
);
1077 if (next_page
->offset
== offset
) {
1078 vm_page_lookup_hint_next
++;
1079 object
->memq_hint
= next_page
; /* new hint */
1083 qe
= queue_prev(&mem
->listq
);
1084 if (! queue_end(&object
->memq
, qe
)) {
1085 vm_page_t prev_page
;
1087 prev_page
= (vm_page_t
) qe
;
1088 assert(prev_page
->object
== object
);
1089 if (prev_page
->offset
== offset
) {
1090 vm_page_lookup_hint_prev
++;
1091 object
->memq_hint
= prev_page
; /* new hint */
1098 * Search the hash table for this object/offset pair
1101 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1104 * since we hold the object lock, we are guaranteed that no
1105 * new pages can be inserted into this object... this in turn
1106 * guarantess that the page we're looking for can't exist
1107 * if the bucket it hashes to is currently NULL even when looked
1108 * at outside the scope of the hash bucket lock... this is a
1109 * really cheap optimiztion to avoid taking the lock
1111 if (bucket
->pages
== VM_PAGE_NULL
) {
1112 return (VM_PAGE_NULL
);
1114 simple_lock(&vm_page_bucket_lock
);
1116 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
1118 if ((mem
->object
== object
) && (mem
->offset
== offset
))
1121 simple_unlock(&vm_page_bucket_lock
);
1123 if (mem
!= VM_PAGE_NULL
) {
1124 if (object
->memq_hint
!= VM_PAGE_NULL
) {
1125 vm_page_lookup_hint_miss
++;
1127 assert(mem
->object
== object
);
1128 object
->memq_hint
= mem
;
1136 vm_page_lookup_nohint(
1138 vm_object_offset_t offset
)
1140 register vm_page_t mem
;
1141 register vm_page_bucket_t
*bucket
;
1144 _mutex_assert(&object
->Lock
, MA_OWNED
);
1147 * Search the hash table for this object/offset pair
1150 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
1152 simple_lock(&vm_page_bucket_lock
);
1153 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
1155 if ((mem
->object
== object
) && (mem
->offset
== offset
))
1158 simple_unlock(&vm_page_bucket_lock
);
1166 * Move the given memory entry from its
1167 * current object to the specified target object/offset.
1169 * The object must be locked.
1173 register vm_page_t mem
,
1174 register vm_object_t new_object
,
1175 vm_object_offset_t new_offset
)
1177 assert(mem
->object
!= new_object
);
1180 * The encryption key is based on the page's memory object
1181 * (aka "pager") and paging offset. Moving the page to
1182 * another VM object changes its "pager" and "paging_offset"
1183 * so it has to be decrypted first.
1185 if (mem
->encrypted
) {
1186 panic("vm_page_rename: page %p is encrypted\n", mem
);
1189 * Changes to mem->object require the page lock because
1190 * the pageout daemon uses that lock to get the object.
1194 "vm_page_rename, new object 0x%X, offset 0x%X page 0x%X\n",
1195 (integer_t
)new_object
, (integer_t
)new_offset
,
1196 (integer_t
)mem
, 0,0);
1198 vm_page_lock_queues();
1199 vm_page_remove(mem
);
1200 vm_page_insert(mem
, new_object
, new_offset
);
1201 vm_page_unlock_queues();
1207 * Initialize the fields in a new page.
1208 * This takes a structure with random values and initializes it
1209 * so that it can be given to vm_page_release or vm_page_insert.
1217 *mem
= vm_page_template
;
1218 mem
->phys_page
= phys_page
;
1222 * vm_page_grab_fictitious:
1224 * Remove a fictitious page from the free list.
1225 * Returns VM_PAGE_NULL if there are no free pages.
1227 int c_vm_page_grab_fictitious
= 0;
1228 int c_vm_page_release_fictitious
= 0;
1229 int c_vm_page_more_fictitious
= 0;
1232 vm_page_grab_fictitious(void)
1234 register vm_page_t m
;
1236 m
= (vm_page_t
)zget(vm_page_zone
);
1238 vm_page_init(m
, vm_page_fictitious_addr
);
1239 m
->fictitious
= TRUE
;
1242 c_vm_page_grab_fictitious
++;
1247 * vm_page_release_fictitious:
1249 * Release a fictitious page to the free list.
1253 vm_page_release_fictitious(
1254 register vm_page_t m
)
1258 assert(m
->fictitious
);
1259 assert(m
->phys_page
== vm_page_fictitious_addr
);
1261 c_vm_page_release_fictitious
++;
1264 panic("vm_page_release_fictitious");
1267 zfree(vm_page_zone
, m
);
1271 * vm_page_more_fictitious:
1273 * Add more fictitious pages to the free list.
1274 * Allowed to block. This routine is way intimate
1275 * with the zones code, for several reasons:
1276 * 1. we need to carve some page structures out of physical
1277 * memory before zones work, so they _cannot_ come from
1279 * 2. the zone needs to be collectable in order to prevent
1280 * growth without bound. These structures are used by
1281 * the device pager (by the hundreds and thousands), as
1282 * private pages for pageout, and as blocking pages for
1283 * pagein. Temporary bursts in demand should not result in
1284 * permanent allocation of a resource.
1285 * 3. To smooth allocation humps, we allocate single pages
1286 * with kernel_memory_allocate(), and cram them into the
1287 * zone. This also allows us to initialize the vm_page_t's
1288 * on the way into the zone, so that zget() always returns
1289 * an initialized structure. The zone free element pointer
1290 * and the free page pointer are both the first item in the
1292 * 4. By having the pages in the zone pre-initialized, we need
1293 * not keep 2 levels of lists. The garbage collector simply
1294 * scans our list, and reduces physical memory usage as it
1298 void vm_page_more_fictitious(void)
1300 register vm_page_t m
;
1302 kern_return_t retval
;
1305 c_vm_page_more_fictitious
++;
1308 * Allocate a single page from the zone_map. Do not wait if no physical
1309 * pages are immediately available, and do not zero the space. We need
1310 * our own blocking lock here to prevent having multiple,
1311 * simultaneous requests from piling up on the zone_map lock. Exactly
1312 * one (of our) threads should be potentially waiting on the map lock.
1313 * If winner is not vm-privileged, then the page allocation will fail,
1314 * and it will temporarily block here in the vm_page_wait().
1316 mutex_lock(&vm_page_alloc_lock
);
1318 * If another thread allocated space, just bail out now.
1320 if (zone_free_count(vm_page_zone
) > 5) {
1322 * The number "5" is a small number that is larger than the
1323 * number of fictitious pages that any single caller will
1324 * attempt to allocate. Otherwise, a thread will attempt to
1325 * acquire a fictitious page (vm_page_grab_fictitious), fail,
1326 * release all of the resources and locks already acquired,
1327 * and then call this routine. This routine finds the pages
1328 * that the caller released, so fails to allocate new space.
1329 * The process repeats infinitely. The largest known number
1330 * of fictitious pages required in this manner is 2. 5 is
1331 * simply a somewhat larger number.
1333 mutex_unlock(&vm_page_alloc_lock
);
1337 retval
= kernel_memory_allocate(zone_map
,
1338 &addr
, PAGE_SIZE
, VM_PROT_ALL
,
1339 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
1340 if (retval
!= KERN_SUCCESS
) {
1342 * No page was available. Tell the pageout daemon, drop the
1343 * lock to give another thread a chance at it, and
1344 * wait for the pageout daemon to make progress.
1346 mutex_unlock(&vm_page_alloc_lock
);
1347 vm_page_wait(THREAD_UNINT
);
1351 * Initialize as many vm_page_t's as will fit on this page. This
1352 * depends on the zone code disturbing ONLY the first item of
1353 * each zone element.
1355 m
= (vm_page_t
)addr
;
1356 for (i
= PAGE_SIZE
/sizeof(struct vm_page
); i
> 0; i
--) {
1357 vm_page_init(m
, vm_page_fictitious_addr
);
1358 m
->fictitious
= TRUE
;
1361 zcram(vm_page_zone
, (void *) addr
, PAGE_SIZE
);
1362 mutex_unlock(&vm_page_alloc_lock
);
1368 * Attempt to convert a fictitious page into a real page.
1373 register vm_page_t m
)
1375 register vm_page_t real_m
;
1378 assert(m
->fictitious
);
1381 real_m
= vm_page_grab();
1382 if (real_m
== VM_PAGE_NULL
)
1385 m
->phys_page
= real_m
->phys_page
;
1386 m
->fictitious
= FALSE
;
1389 vm_page_lock_queues();
1391 vm_page_active_count
++;
1392 else if (m
->inactive
)
1393 vm_page_inactive_count
++;
1394 vm_page_unlock_queues();
1396 real_m
->phys_page
= vm_page_fictitious_addr
;
1397 real_m
->fictitious
= TRUE
;
1399 vm_page_release_fictitious(real_m
);
1406 * Return true if it is not likely that a non-vm_privileged thread
1407 * can get memory without blocking. Advisory only, since the
1408 * situation may change under us.
1413 /* No locking, at worst we will fib. */
1414 return( vm_page_free_count
< vm_page_free_reserved
);
1420 * this is an interface to support bring-up of drivers
1421 * on platforms with physical memory > 4G...
1423 int vm_himemory_mode
= 0;
1427 * this interface exists to support hardware controllers
1428 * incapable of generating DMAs with more than 32 bits
1429 * of address on platforms with physical memory > 4G...
1431 unsigned int vm_lopage_free_count
= 0;
1432 unsigned int vm_lopage_max_count
= 0;
1433 vm_page_t vm_lopage_queue_free
= VM_PAGE_NULL
;
1436 vm_page_grablo(void)
1438 register vm_page_t mem
;
1439 unsigned int vm_lopage_alloc_count
;
1441 if (vm_lopage_poolsize
== 0)
1442 return (vm_page_grab());
1444 mutex_lock(&vm_page_queue_free_lock
);
1446 if ((mem
= vm_lopage_queue_free
) != VM_PAGE_NULL
) {
1448 vm_lopage_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1449 mem
->pageq
.next
= NULL
;
1450 mem
->pageq
.prev
= NULL
;
1452 mem
->no_isync
= TRUE
;
1454 vm_lopage_free_count
--;
1455 vm_lopage_alloc_count
= (vm_lopage_poolend
- vm_lopage_poolstart
) - vm_lopage_free_count
;
1456 if (vm_lopage_alloc_count
> vm_lopage_max_count
)
1457 vm_lopage_max_count
= vm_lopage_alloc_count
;
1459 mutex_unlock(&vm_page_queue_free_lock
);
1469 * Remove a page from the free list.
1470 * Returns VM_PAGE_NULL if the free list is too small.
1473 unsigned long vm_page_grab_count
= 0; /* measure demand */
1478 register vm_page_t mem
;
1480 mutex_lock(&vm_page_queue_free_lock
);
1481 vm_page_grab_count
++;
1484 * Optionally produce warnings if the wire or gobble
1485 * counts exceed some threshold.
1487 if (vm_page_wire_count_warning
> 0
1488 && vm_page_wire_count
>= vm_page_wire_count_warning
) {
1489 printf("mk: vm_page_grab(): high wired page count of %d\n",
1490 vm_page_wire_count
);
1491 assert(vm_page_wire_count
< vm_page_wire_count_warning
);
1493 if (vm_page_gobble_count_warning
> 0
1494 && vm_page_gobble_count
>= vm_page_gobble_count_warning
) {
1495 printf("mk: vm_page_grab(): high gobbled page count of %d\n",
1496 vm_page_gobble_count
);
1497 assert(vm_page_gobble_count
< vm_page_gobble_count_warning
);
1501 * Only let privileged threads (involved in pageout)
1502 * dip into the reserved pool.
1505 if ((vm_page_free_count
< vm_page_free_reserved
) &&
1506 !(current_thread()->options
& TH_OPT_VMPRIV
)) {
1507 mutex_unlock(&vm_page_queue_free_lock
);
1509 goto wakeup_pageout
;
1512 while (vm_page_queue_free
== VM_PAGE_NULL
) {
1513 mutex_unlock(&vm_page_queue_free_lock
);
1515 mutex_lock(&vm_page_queue_free_lock
);
1518 if (--vm_page_free_count
< vm_page_free_count_minimum
)
1519 vm_page_free_count_minimum
= vm_page_free_count
;
1520 mem
= vm_page_queue_free
;
1521 vm_page_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1522 mem
->pageq
.next
= NULL
;
1523 mem
->pageq
.prev
= NULL
;
1524 assert(mem
->listq
.next
== NULL
&& mem
->listq
.prev
== NULL
);
1525 assert(mem
->tabled
== FALSE
);
1526 assert(mem
->object
== VM_OBJECT_NULL
);
1527 assert(!mem
->laundry
);
1529 mem
->no_isync
= TRUE
;
1530 mutex_unlock(&vm_page_queue_free_lock
);
1532 assert(pmap_verify_free(mem
->phys_page
));
1535 * Decide if we should poke the pageout daemon.
1536 * We do this if the free count is less than the low
1537 * water mark, or if the free count is less than the high
1538 * water mark (but above the low water mark) and the inactive
1539 * count is less than its target.
1541 * We don't have the counts locked ... if they change a little,
1542 * it doesn't really matter.
1546 if ((vm_page_free_count
< vm_page_free_min
) ||
1547 ((vm_page_free_count
< vm_page_free_target
) &&
1548 (vm_page_inactive_count
< vm_page_inactive_target
)))
1549 thread_wakeup((event_t
) &vm_page_free_wanted
);
1551 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */
1559 * Return a page to the free list.
1564 register vm_page_t mem
)
1568 unsigned int pindex
;
1569 phys_entry
*physent
;
1571 physent
= mapping_phys_lookup(mem
->phys_page
, &pindex
); /* (BRINGUP) */
1572 if(physent
->ppLink
& ppN
) { /* (BRINGUP) */
1573 panic("vm_page_release: already released - %08X %08X\n", mem
, mem
->phys_page
);
1575 physent
->ppLink
= physent
->ppLink
| ppN
; /* (BRINGUP) */
1577 assert(!mem
->private && !mem
->fictitious
);
1579 // dbgLog(mem->phys_page, vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */
1581 mutex_lock(&vm_page_queue_free_lock
);
1584 panic("vm_page_release");
1587 assert(!mem
->laundry
);
1588 assert(mem
->object
== VM_OBJECT_NULL
);
1589 assert(mem
->pageq
.next
== NULL
&&
1590 mem
->pageq
.prev
== NULL
);
1592 if (mem
->phys_page
<= vm_lopage_poolend
&& mem
->phys_page
>= vm_lopage_poolstart
) {
1594 * this exists to support hardware controllers
1595 * incapable of generating DMAs with more than 32 bits
1596 * of address on platforms with physical memory > 4G...
1598 mem
->pageq
.next
= (queue_entry_t
) vm_lopage_queue_free
;
1599 vm_lopage_queue_free
= mem
;
1600 vm_lopage_free_count
++;
1602 mem
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1603 vm_page_queue_free
= mem
;
1604 vm_page_free_count
++;
1606 * Check if we should wake up someone waiting for page.
1607 * But don't bother waking them unless they can allocate.
1609 * We wakeup only one thread, to prevent starvation.
1610 * Because the scheduling system handles wait queues FIFO,
1611 * if we wakeup all waiting threads, one greedy thread
1612 * can starve multiple niceguy threads. When the threads
1613 * all wakeup, the greedy threads runs first, grabs the page,
1614 * and waits for another page. It will be the first to run
1615 * when the next page is freed.
1617 * However, there is a slight danger here.
1618 * The thread we wake might not use the free page.
1619 * Then the other threads could wait indefinitely
1620 * while the page goes unused. To forestall this,
1621 * the pageout daemon will keep making free pages
1622 * as long as vm_page_free_wanted is non-zero.
1625 if ((vm_page_free_wanted
> 0) &&
1626 (vm_page_free_count
>= vm_page_free_reserved
)) {
1627 vm_page_free_wanted
--;
1628 thread_wakeup_one((event_t
) &vm_page_free_count
);
1631 mutex_unlock(&vm_page_queue_free_lock
);
1637 * Wait for a page to become available.
1638 * If there are plenty of free pages, then we don't sleep.
1641 * TRUE: There may be another page, try again
1642 * FALSE: We were interrupted out of our wait, don't try again
1650 * We can't use vm_page_free_reserved to make this
1651 * determination. Consider: some thread might
1652 * need to allocate two pages. The first allocation
1653 * succeeds, the second fails. After the first page is freed,
1654 * a call to vm_page_wait must really block.
1656 kern_return_t wait_result
;
1657 int need_wakeup
= 0;
1659 mutex_lock(&vm_page_queue_free_lock
);
1660 if (vm_page_free_count
< vm_page_free_target
) {
1661 if (vm_page_free_wanted
++ == 0)
1663 wait_result
= assert_wait((event_t
)&vm_page_free_count
, interruptible
);
1664 mutex_unlock(&vm_page_queue_free_lock
);
1665 counter(c_vm_page_wait_block
++);
1668 thread_wakeup((event_t
)&vm_page_free_wanted
);
1670 if (wait_result
== THREAD_WAITING
)
1671 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
1673 return(wait_result
== THREAD_AWAKENED
);
1675 mutex_unlock(&vm_page_queue_free_lock
);
1683 * Allocate and return a memory cell associated
1684 * with this VM object/offset pair.
1686 * Object must be locked.
1692 vm_object_offset_t offset
)
1694 register vm_page_t mem
;
1697 _mutex_assert(&object
->Lock
, MA_OWNED
);
1699 mem
= vm_page_grab();
1700 if (mem
== VM_PAGE_NULL
)
1701 return VM_PAGE_NULL
;
1703 vm_page_insert(mem
, object
, offset
);
1712 vm_object_offset_t offset
)
1714 register vm_page_t mem
;
1717 _mutex_assert(&object
->Lock
, MA_OWNED
);
1719 mem
= vm_page_grablo();
1720 if (mem
== VM_PAGE_NULL
)
1721 return VM_PAGE_NULL
;
1723 vm_page_insert(mem
, object
, offset
);
1729 counter(unsigned int c_laundry_pages_freed
= 0;)
1731 int vm_pagein_cluster_unused
= 0;
1732 boolean_t vm_page_free_verify
= TRUE
;
1736 * Returns the given page to the free list,
1737 * disassociating it with any VM object.
1739 * Object and page queues must be locked prior to entry.
1743 register vm_page_t mem
)
1745 vm_object_t object
= mem
->object
;
1748 assert(!mem
->cleaning
);
1749 assert(!mem
->pageout
);
1750 if (vm_page_free_verify
&& !mem
->fictitious
&& !mem
->private) {
1751 assert(pmap_verify_free(mem
->phys_page
));
1756 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
1757 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1760 panic("vm_page_free: freeing page on free list\n");
1763 vm_page_remove(mem
); /* clears tabled, object, offset */
1764 VM_PAGE_QUEUES_REMOVE(mem
); /* clears active or inactive */
1766 if (mem
->clustered
) {
1767 mem
->clustered
= FALSE
;
1768 vm_pagein_cluster_unused
++;
1771 if (mem
->wire_count
) {
1772 if (!mem
->private && !mem
->fictitious
)
1773 vm_page_wire_count
--;
1774 mem
->wire_count
= 0;
1775 assert(!mem
->gobbled
);
1776 } else if (mem
->gobbled
) {
1777 if (!mem
->private && !mem
->fictitious
)
1778 vm_page_wire_count
--;
1779 vm_page_gobble_count
--;
1781 mem
->gobbled
= FALSE
;
1784 vm_pageout_throttle_up(mem
);
1785 counter(++c_laundry_pages_freed
);
1788 PAGE_WAKEUP(mem
); /* clears wanted */
1791 vm_object_absent_release(object
);
1793 /* Some of these may be unnecessary */
1795 mem
->unlock_request
= 0;
1797 mem
->absent
= FALSE
;
1800 mem
->precious
= FALSE
;
1801 mem
->reference
= FALSE
;
1802 mem
->encrypted
= FALSE
;
1804 mem
->page_error
= KERN_SUCCESS
;
1807 mem
->private = FALSE
;
1808 mem
->fictitious
= TRUE
;
1809 mem
->phys_page
= vm_page_fictitious_addr
;
1811 if (mem
->fictitious
) {
1812 vm_page_release_fictitious(mem
);
1814 /* depends on the queues lock */
1815 if(mem
->zero_fill
) {
1817 mem
->zero_fill
= FALSE
;
1819 vm_page_init(mem
, mem
->phys_page
);
1820 vm_page_release(mem
);
1827 register vm_page_t mem
)
1829 register vm_page_t nxt
;
1830 register vm_page_t first
= NULL
;
1831 register vm_page_t last
= VM_PAGE_NULL
;
1832 register int pg_count
= 0;
1835 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1839 if (mem
->tabled
|| mem
->object
)
1840 panic("vm_page_free_list: freeing tabled page\n");
1841 if (mem
->inactive
|| mem
->active
|| mem
->free
)
1842 panic("vm_page_free_list: freeing page on list\n");
1844 assert(mem
->pageq
.prev
== NULL
);
1845 nxt
= (vm_page_t
)(mem
->pageq
.next
);
1848 vm_pagein_cluster_unused
++;
1851 vm_pageout_throttle_up(mem
);
1852 counter(++c_laundry_pages_freed
);
1856 PAGE_WAKEUP(mem
); /* clears wanted */
1859 mem
->fictitious
= TRUE
;
1861 if (!mem
->fictitious
) {
1862 /* depends on the queues lock */
1865 assert(!mem
->laundry
);
1866 vm_page_init(mem
, mem
->phys_page
);
1872 mem
->pageq
.next
= (queue_t
) first
;
1877 mem
->phys_page
= vm_page_fictitious_addr
;
1878 vm_page_release_fictitious(mem
);
1884 mutex_lock(&vm_page_queue_free_lock
);
1886 last
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1887 vm_page_queue_free
= first
;
1889 vm_page_free_count
+= pg_count
;
1891 if ((vm_page_free_wanted
> 0) &&
1892 (vm_page_free_count
>= vm_page_free_reserved
)) {
1893 unsigned int available_pages
;
1895 if (vm_page_free_count
>= vm_page_free_reserved
) {
1896 available_pages
= (vm_page_free_count
1897 - vm_page_free_reserved
);
1899 available_pages
= 0;
1902 if (available_pages
>= vm_page_free_wanted
) {
1903 vm_page_free_wanted
= 0;
1904 thread_wakeup((event_t
) &vm_page_free_count
);
1906 while (available_pages
--) {
1907 vm_page_free_wanted
--;
1908 thread_wakeup_one((event_t
) &vm_page_free_count
);
1912 mutex_unlock(&vm_page_queue_free_lock
);
1920 * Mark this page as wired down by yet
1921 * another map, removing it from paging queues
1924 * The page's object and the page queues must be locked.
1928 register vm_page_t mem
)
1931 // dbgLog(current_thread(), mem->offset, mem->object, 1); /* (TEST/DEBUG) */
1936 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
1937 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
1939 if (mem
->wire_count
== 0) {
1940 VM_PAGE_QUEUES_REMOVE(mem
);
1941 if (!mem
->private && !mem
->fictitious
&& !mem
->gobbled
)
1942 vm_page_wire_count
++;
1944 vm_page_gobble_count
--;
1945 mem
->gobbled
= FALSE
;
1946 if(mem
->zero_fill
) {
1947 /* depends on the queues lock */
1949 mem
->zero_fill
= FALSE
;
1953 * The page could be encrypted, but
1954 * We don't have to decrypt it here
1955 * because we don't guarantee that the
1956 * data is actually valid at this point.
1957 * The page will get decrypted in
1958 * vm_fault_wire() if needed.
1961 assert(!mem
->gobbled
);
1968 * Mark this page as consumed by the vm/ipc/xmm subsystems.
1970 * Called only for freshly vm_page_grab()ed pages - w/ nothing locked.
1974 register vm_page_t mem
)
1976 vm_page_lock_queues();
1979 assert(!mem
->gobbled
);
1980 assert(mem
->wire_count
== 0);
1982 if (!mem
->gobbled
&& mem
->wire_count
== 0) {
1983 if (!mem
->private && !mem
->fictitious
)
1984 vm_page_wire_count
++;
1986 vm_page_gobble_count
++;
1987 mem
->gobbled
= TRUE
;
1988 vm_page_unlock_queues();
1994 * Release one wiring of this page, potentially
1995 * enabling it to be paged again.
1997 * The page's object and the page queues must be locked.
2001 register vm_page_t mem
)
2004 // dbgLog(current_thread(), mem->offset, mem->object, 0); /* (TEST/DEBUG) */
2007 assert(mem
->wire_count
> 0);
2010 _mutex_assert(&mem
->object
->Lock
, MA_OWNED
);
2011 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2013 if (--mem
->wire_count
== 0) {
2014 assert(!mem
->private && !mem
->fictitious
);
2015 vm_page_wire_count
--;
2016 assert(!mem
->laundry
);
2017 assert(mem
->object
!= kernel_object
);
2018 assert(mem
->pageq
.next
== NULL
&& mem
->pageq
.prev
== NULL
);
2019 queue_enter(&vm_page_queue_active
, mem
, vm_page_t
, pageq
);
2020 vm_page_active_count
++;
2022 mem
->reference
= TRUE
;
2027 * vm_page_deactivate:
2029 * Returns the given page to the inactive list,
2030 * indicating that no physical maps have access
2031 * to this page. [Used by the physical mapping system.]
2033 * The page queues must be locked.
2037 register vm_page_t m
)
2040 assert(m
->object
!= kernel_object
);
2042 // dbgLog(m->phys_page, vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
2044 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2047 * This page is no longer very interesting. If it was
2048 * interesting (active or inactive/referenced), then we
2049 * clear the reference bit and (re)enter it in the
2050 * inactive queue. Note wired pages should not have
2051 * their reference bit cleared.
2053 if (m
->gobbled
) { /* can this happen? */
2054 assert(m
->wire_count
== 0);
2055 if (!m
->private && !m
->fictitious
)
2056 vm_page_wire_count
--;
2057 vm_page_gobble_count
--;
2060 if (m
->private || (m
->wire_count
!= 0))
2062 if (m
->active
|| (m
->inactive
&& m
->reference
)) {
2063 if (!m
->fictitious
&& !m
->absent
)
2064 pmap_clear_reference(m
->phys_page
);
2065 m
->reference
= FALSE
;
2066 VM_PAGE_QUEUES_REMOVE(m
);
2068 if (m
->wire_count
== 0 && !m
->inactive
) {
2069 m
->page_ticket
= vm_page_ticket
;
2070 vm_page_ticket_roll
++;
2072 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
2073 vm_page_ticket_roll
= 0;
2074 if(vm_page_ticket
== VM_PAGE_TICKET_ROLL_IDS
)
2080 assert(!m
->laundry
);
2081 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2083 queue_enter(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
2085 queue_enter(&vm_page_queue_inactive
,
2086 m
, vm_page_t
, pageq
);
2091 vm_page_inactive_count
++;
2098 * Put the specified page on the active list (if appropriate).
2100 * The page queues must be locked.
2105 register vm_page_t m
)
2108 assert(m
->object
!= kernel_object
);
2110 _mutex_assert(&vm_page_queue_lock
, MA_OWNED
);
2113 assert(m
->wire_count
== 0);
2114 if (!m
->private && !m
->fictitious
)
2115 vm_page_wire_count
--;
2116 vm_page_gobble_count
--;
2123 assert(!m
->laundry
);
2125 queue_remove(&vm_page_queue_zf
, m
, vm_page_t
, pageq
);
2127 queue_remove(&vm_page_queue_inactive
,
2128 m
, vm_page_t
, pageq
);
2130 m
->pageq
.next
= NULL
;
2131 m
->pageq
.prev
= NULL
;
2133 vm_page_inactive_count
--;
2134 m
->inactive
= FALSE
;
2136 if (m
->wire_count
== 0) {
2139 panic("vm_page_activate: already active");
2141 assert(!m
->laundry
);
2142 assert(m
->pageq
.next
== NULL
&& m
->pageq
.prev
== NULL
);
2143 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
2145 m
->reference
= TRUE
;
2147 vm_page_active_count
++;
2152 * vm_page_part_zero_fill:
2154 * Zero-fill a part of the page.
2157 vm_page_part_zero_fill(
2165 #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
2166 pmap_zero_part_page(m
->phys_page
, m_pa
, len
);
2169 tmp
= vm_page_grab();
2170 if (tmp
== VM_PAGE_NULL
) {
2171 vm_page_wait(THREAD_UNINT
);
2176 vm_page_zero_fill(tmp
);
2178 vm_page_part_copy(m
, 0, tmp
, 0, m_pa
);
2180 if((m_pa
+ len
) < PAGE_SIZE
) {
2181 vm_page_part_copy(m
, m_pa
+ len
, tmp
,
2182 m_pa
+ len
, PAGE_SIZE
- (m_pa
+ len
));
2184 vm_page_copy(tmp
,m
);
2185 vm_page_lock_queues();
2187 vm_page_unlock_queues();
2193 * vm_page_zero_fill:
2195 * Zero-fill the specified page.
2202 "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
2203 (integer_t
)m
->object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
2207 // dbgTrace(0xAEAEAEAE, m->phys_page, 0); /* (BRINGUP) */
2208 pmap_zero_page(m
->phys_page
);
2212 * vm_page_part_copy:
2214 * copy part of one page to another
2225 VM_PAGE_CHECK(src_m
);
2226 VM_PAGE_CHECK(dst_m
);
2228 pmap_copy_part_page(src_m
->phys_page
, src_pa
,
2229 dst_m
->phys_page
, dst_pa
, len
);
2235 * Copy one page to another
2238 * The source page should not be encrypted. The caller should
2239 * make sure the page is decrypted first, if necessary.
2248 "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
2249 (integer_t
)src_m
->object
, src_m
->offset
,
2250 (integer_t
)dest_m
->object
, dest_m
->offset
,
2253 VM_PAGE_CHECK(src_m
);
2254 VM_PAGE_CHECK(dest_m
);
2258 * The source page should not be encrypted at this point.
2259 * The destination page will therefore not contain encrypted
2260 * data after the copy.
2262 if (src_m
->encrypted
) {
2263 panic("vm_page_copy: source page %p is encrypted\n", src_m
);
2265 dest_m
->encrypted
= FALSE
;
2267 pmap_copy_page(src_m
->phys_page
, dest_m
->phys_page
);
2271 * Currently, this is a primitive allocator that grabs
2272 * free pages from the system, sorts them by physical
2273 * address, then searches for a region large enough to
2274 * satisfy the user's request.
2276 * Additional levels of effort:
2277 * + steal clean active/inactive pages
2278 * + force pageouts of dirty pages
2279 * + maintain a map of available physical
2285 * Check that the list of pages is ordered by
2286 * ascending physical address and has no holes.
2288 int vm_page_verify_contiguous(
2290 unsigned int npages
);
2293 vm_page_verify_contiguous(
2295 unsigned int npages
)
2297 register vm_page_t m
;
2298 unsigned int page_count
;
2299 vm_offset_t prev_addr
;
2301 prev_addr
= pages
->phys_page
;
2303 for (m
= NEXT_PAGE(pages
); m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2304 if (m
->phys_page
!= prev_addr
+ 1) {
2305 printf("m 0x%x prev_addr 0x%x, current addr 0x%x\n",
2306 m
, prev_addr
, m
->phys_page
);
2307 printf("pages 0x%x page_count %d\n", pages
, page_count
);
2308 panic("vm_page_verify_contiguous: not contiguous!");
2310 prev_addr
= m
->phys_page
;
2313 if (page_count
!= npages
) {
2314 printf("pages 0x%x actual count 0x%x but requested 0x%x\n",
2315 pages
, page_count
, npages
);
2316 panic("vm_page_verify_contiguous: count error");
2320 #endif /* MACH_ASSERT */
2323 cpm_counter(unsigned int vpfls_pages_handled
= 0;)
2324 cpm_counter(unsigned int vpfls_head_insertions
= 0;)
2325 cpm_counter(unsigned int vpfls_tail_insertions
= 0;)
2326 cpm_counter(unsigned int vpfls_general_insertions
= 0;)
2327 cpm_counter(unsigned int vpfc_failed
= 0;)
2328 cpm_counter(unsigned int vpfc_satisfied
= 0;)
2331 * Find a region large enough to contain at least npages
2332 * of contiguous physical memory.
2335 * - Called while holding vm_page_queue_free_lock.
2336 * - Doesn't respect vm_page_free_reserved; caller
2337 * must not ask for more pages than are legal to grab.
2339 * Returns a pointer to a list of gobbled pages or VM_PAGE_NULL.
2342 * Loop over the free list, extracting one page at a time and
2343 * inserting those into a sorted sub-list. We stop as soon as
2344 * there's a contiguous range within the sorted list that can
2345 * satisfy the contiguous memory request. This contiguous sub-
2346 * list is chopped out of the sorted sub-list and the remainder
2347 * of the sorted sub-list is put back onto the beginning of the
2351 vm_page_find_contiguous(
2352 unsigned int contig_pages
)
2354 vm_page_t sort_list
;
2355 vm_page_t
*contfirstprev
, contlast
;
2357 ppnum_t prevcontaddr
;
2358 ppnum_t nextcontaddr
;
2359 unsigned int npages
;
2363 _mutex_assert(&vm_page_queue_free_lock
, MA_OWNED
);
2367 * Verify pages in the free list..
2370 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
))
2372 if (npages
!= vm_page_free_count
)
2373 panic("vm_sort_free_list: prelim: npages %u free_count %d",
2374 npages
, vm_page_free_count
);
2375 #endif /* MACH_ASSERT */
2377 if (contig_pages
== 0 || vm_page_queue_free
== VM_PAGE_NULL
)
2378 return VM_PAGE_NULL
;
2380 #define PPNUM_PREV(x) (((x) > 0) ? ((x) - 1) : 0)
2381 #define PPNUM_NEXT(x) (((x) < PPNUM_MAX) ? ((x) + 1) : PPNUM_MAX)
2382 #define SET_NEXT_PAGE(m,n) ((m)->pageq.next = (struct queue_entry *) (n))
2385 contfirstprev
= &sort_list
;
2386 contlast
= sort_list
= vm_page_queue_free
;
2387 vm_page_queue_free
= NEXT_PAGE(sort_list
);
2388 SET_NEXT_PAGE(sort_list
, VM_PAGE_NULL
);
2389 prevcontaddr
= PPNUM_PREV(sort_list
->phys_page
);
2390 nextcontaddr
= PPNUM_NEXT(sort_list
->phys_page
);
2392 while (npages
< contig_pages
&&
2393 (m
= vm_page_queue_free
) != VM_PAGE_NULL
)
2395 cpm_counter(++vpfls_pages_handled
);
2397 /* prepend to existing run? */
2398 if (m
->phys_page
== prevcontaddr
)
2400 vm_page_queue_free
= NEXT_PAGE(m
);
2401 cpm_counter(++vpfls_head_insertions
);
2402 prevcontaddr
= PPNUM_PREV(prevcontaddr
);
2403 SET_NEXT_PAGE(m
, *contfirstprev
);
2406 continue; /* no tail expansion check needed */
2409 /* append to tail of existing run? */
2410 else if (m
->phys_page
== nextcontaddr
)
2412 vm_page_queue_free
= NEXT_PAGE(m
);
2413 cpm_counter(++vpfls_tail_insertions
);
2414 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2415 SET_NEXT_PAGE(m
, NEXT_PAGE(contlast
));
2416 SET_NEXT_PAGE(contlast
, m
);
2421 /* prepend to the very front of sorted list? */
2422 else if (m
->phys_page
< sort_list
->phys_page
)
2424 vm_page_queue_free
= NEXT_PAGE(m
);
2425 cpm_counter(++vpfls_general_insertions
);
2426 prevcontaddr
= PPNUM_PREV(m
->phys_page
);
2427 nextcontaddr
= PPNUM_NEXT(m
->phys_page
);
2428 SET_NEXT_PAGE(m
, sort_list
);
2429 contfirstprev
= &sort_list
;
2430 contlast
= sort_list
= m
;
2434 else /* get to proper place for insertion */
2436 if (m
->phys_page
< nextcontaddr
)
2438 prevcontaddr
= PPNUM_PREV(sort_list
->phys_page
);
2439 nextcontaddr
= PPNUM_NEXT(sort_list
->phys_page
);
2440 contfirstprev
= &sort_list
;
2441 contlast
= sort_list
;
2444 for (m1
= NEXT_PAGE(contlast
);
2445 npages
< contig_pages
&&
2446 m1
!= VM_PAGE_NULL
&& m1
->phys_page
< m
->phys_page
;
2449 if (m1
->phys_page
!= nextcontaddr
) {
2450 prevcontaddr
= PPNUM_PREV(m1
->phys_page
);
2451 contfirstprev
= NEXT_PAGE_PTR(contlast
);
2456 nextcontaddr
= PPNUM_NEXT(m1
->phys_page
);
2461 * We may actually already have enough.
2462 * This could happen if a previous prepend
2463 * joined up two runs to meet our needs.
2464 * If so, bail before we take the current
2465 * page off the free queue.
2467 if (npages
== contig_pages
)
2470 if (m
->phys_page
!= nextcontaddr
)
2472 contfirstprev
= NEXT_PAGE_PTR(contlast
);
2473 prevcontaddr
= PPNUM_PREV(m
->phys_page
);
2474 nextcontaddr
= PPNUM_NEXT(m
->phys_page
);
2477 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2480 vm_page_queue_free
= NEXT_PAGE(m
);
2481 cpm_counter(++vpfls_general_insertions
);
2482 SET_NEXT_PAGE(m
, NEXT_PAGE(contlast
));
2483 SET_NEXT_PAGE(contlast
, m
);
2487 /* See how many pages are now contiguous after the insertion */
2488 for (m1
= NEXT_PAGE(m
);
2489 npages
< contig_pages
&&
2490 m1
!= VM_PAGE_NULL
&& m1
->phys_page
== nextcontaddr
;
2493 nextcontaddr
= PPNUM_NEXT(nextcontaddr
);
2499 /* how did we do? */
2500 if (npages
== contig_pages
)
2502 cpm_counter(++vpfc_satisfied
);
2504 /* remove the contiguous range from the sorted list */
2506 *contfirstprev
= NEXT_PAGE(contlast
);
2507 SET_NEXT_PAGE(contlast
, VM_PAGE_NULL
);
2508 assert(vm_page_verify_contiguous(m
, npages
));
2510 /* inline vm_page_gobble() for each returned page */
2511 for (m1
= m
; m1
!= VM_PAGE_NULL
; m1
= NEXT_PAGE(m1
)) {
2513 assert(!m1
->wanted
);
2514 assert(!m1
->laundry
);
2516 m1
->no_isync
= TRUE
;
2519 vm_page_wire_count
+= npages
;
2520 vm_page_gobble_count
+= npages
;
2521 vm_page_free_count
-= npages
;
2523 /* stick free list at the tail of the sorted list */
2524 while ((m1
= *contfirstprev
) != VM_PAGE_NULL
)
2525 contfirstprev
= (vm_page_t
*)&m1
->pageq
.next
;
2526 *contfirstprev
= vm_page_queue_free
;
2529 vm_page_queue_free
= sort_list
;
2534 * Allocate a list of contiguous, wired pages.
2542 register vm_page_t m
;
2544 unsigned int npages
;
2545 unsigned int vm_pages_available
;
2548 if (size
% page_size
!= 0)
2549 return KERN_INVALID_ARGUMENT
;
2551 vm_page_lock_queues();
2552 mutex_lock(&vm_page_queue_free_lock
);
2555 * Should also take active and inactive pages
2556 * into account... One day...
2558 npages
= size
/ page_size
;
2559 vm_pages_available
= vm_page_free_count
- vm_page_free_reserved
;
2561 if (npages
> vm_pages_available
) {
2562 mutex_unlock(&vm_page_queue_free_lock
);
2563 vm_page_unlock_queues();
2564 return KERN_RESOURCE_SHORTAGE
;
2568 * Obtain a pointer to a subset of the free
2569 * list large enough to satisfy the request;
2570 * the region will be physically contiguous.
2572 pages
= vm_page_find_contiguous(npages
);
2574 /* adjust global freelist counts and determine need for wakeups */
2575 if (vm_page_free_count
< vm_page_free_count_minimum
)
2576 vm_page_free_count_minimum
= vm_page_free_count
;
2578 wakeup
= ((vm_page_free_count
< vm_page_free_min
) ||
2579 ((vm_page_free_count
< vm_page_free_target
) &&
2580 (vm_page_inactive_count
< vm_page_inactive_target
)));
2582 mutex_unlock(&vm_page_queue_free_lock
);
2584 if (pages
== VM_PAGE_NULL
) {
2585 vm_page_unlock_queues();
2586 return KERN_NO_SPACE
;
2590 * Walk the returned list, wiring the pages.
2593 for (m
= pages
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2595 * Essentially inlined vm_page_wire.
2598 assert(!m
->inactive
);
2599 assert(!m
->private);
2600 assert(!m
->fictitious
);
2601 assert(m
->wire_count
== 0);
2605 --vm_page_gobble_count
;
2607 vm_page_unlock_queues();
2610 thread_wakeup((event_t
) &vm_page_free_wanted
);
2613 * The CPM pages should now be available and
2614 * ordered by ascending physical address.
2616 assert(vm_page_verify_contiguous(pages
, npages
));
2619 return KERN_SUCCESS
;
2623 #include <mach_vm_debug.h>
2626 #include <mach_debug/hash_info.h>
2627 #include <vm/vm_debug.h>
2630 * Routine: vm_page_info
2632 * Return information about the global VP table.
2633 * Fills the buffer with as much information as possible
2634 * and returns the desired size of the buffer.
2636 * Nothing locked. The caller should provide
2637 * possibly-pageable memory.
2642 hash_info_bucket_t
*info
,
2647 if (vm_page_bucket_count
< count
)
2648 count
= vm_page_bucket_count
;
2650 for (i
= 0; i
< count
; i
++) {
2651 vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
2652 unsigned int bucket_count
= 0;
2655 simple_lock(&vm_page_bucket_lock
);
2656 for (m
= bucket
->pages
; m
!= VM_PAGE_NULL
; m
= m
->next
)
2658 simple_unlock(&vm_page_bucket_lock
);
2660 /* don't touch pageable memory while holding locks */
2661 info
[i
].hib_count
= bucket_count
;
2664 return vm_page_bucket_count
;
2666 #endif /* MACH_VM_DEBUG */
2668 #include <mach_kdb.h>
2671 #include <ddb/db_output.h>
2672 #include <vm/vm_print.h>
2673 #define printf kdbprintf
2676 * Routine: vm_page_print [exported]
2684 p
= (vm_page_t
) (long) db_addr
;
2686 iprintf("page 0x%x\n", p
);
2690 iprintf("object=0x%x", p
->object
);
2691 printf(", offset=0x%x", p
->offset
);
2692 printf(", wire_count=%d", p
->wire_count
);
2694 iprintf("%sinactive, %sactive, %sgobbled, %slaundry, %sfree, %sref, %sencrypted\n",
2695 (p
->inactive
? "" : "!"),
2696 (p
->active
? "" : "!"),
2697 (p
->gobbled
? "" : "!"),
2698 (p
->laundry
? "" : "!"),
2699 (p
->free
? "" : "!"),
2700 (p
->reference
? "" : "!"),
2701 (p
->encrypted
? "" : "!"));
2702 iprintf("%sbusy, %swanted, %stabled, %sfictitious, %sprivate, %sprecious\n",
2703 (p
->busy
? "" : "!"),
2704 (p
->wanted
? "" : "!"),
2705 (p
->tabled
? "" : "!"),
2706 (p
->fictitious
? "" : "!"),
2707 (p
->private ? "" : "!"),
2708 (p
->precious
? "" : "!"));
2709 iprintf("%sabsent, %serror, %sdirty, %scleaning, %spageout, %sclustered\n",
2710 (p
->absent
? "" : "!"),
2711 (p
->error
? "" : "!"),
2712 (p
->dirty
? "" : "!"),
2713 (p
->cleaning
? "" : "!"),
2714 (p
->pageout
? "" : "!"),
2715 (p
->clustered
? "" : "!"));
2716 iprintf("%slock_supplied, %soverwriting, %srestart, %sunusual\n",
2717 (p
->lock_supplied
? "" : "!"),
2718 (p
->overwriting
? "" : "!"),
2719 (p
->restart
? "" : "!"),
2720 (p
->unusual
? "" : "!"));
2722 iprintf("phys_page=0x%x", p
->phys_page
);
2723 printf(", page_error=0x%x", p
->page_error
);
2724 printf(", page_lock=0x%x", p
->page_lock
);
2725 printf(", unlock_request=%d\n", p
->unlock_request
);
2729 #endif /* MACH_KDB */