2 * Copyright (c) 2000 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.
59 #include <mach/vm_prot.h>
60 #include <mach/vm_statistics.h>
61 #include <kern/counters.h>
62 #include <kern/sched_prim.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/zalloc.h>
68 #include <vm/vm_init.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/vm_kern.h> /* kernel_memory_allocate() */
73 #include <kern/misc_protos.h>
74 #include <zone_debug.h>
77 /* Variables used to indicate the relative age of pages in the
81 int vm_page_ticket_roll
= 0;
82 int vm_page_ticket
= 0;
84 * Associated with page of user-allocatable memory is a
89 * These variables record the values returned by vm_page_bootstrap,
90 * for debugging purposes. The implementation of pmap_steal_memory
91 * and pmap_startup here also uses them internally.
94 vm_offset_t virtual_space_start
;
95 vm_offset_t virtual_space_end
;
99 * The vm_page_lookup() routine, which provides for fast
100 * (virtual memory object, offset) to page lookup, employs
101 * the following hash table. The vm_page_{insert,remove}
102 * routines install and remove associations in the table.
103 * [This table is often called the virtual-to-physical,
108 #if MACH_PAGE_HASH_STATS
109 int cur_count
; /* current count */
110 int hi_count
; /* high water mark */
111 #endif /* MACH_PAGE_HASH_STATS */
114 vm_page_bucket_t
*vm_page_buckets
; /* Array of buckets */
115 unsigned int vm_page_bucket_count
= 0; /* How big is array? */
116 unsigned int vm_page_hash_mask
; /* Mask for hash function */
117 unsigned int vm_page_hash_shift
; /* Shift for hash function */
118 decl_simple_lock_data(,vm_page_bucket_lock
)
120 #if MACH_PAGE_HASH_STATS
121 /* This routine is only for debug. It is intended to be called by
122 * hand by a developer using a kernel debugger. This routine prints
123 * out vm_page_hash table statistics to the kernel debug console.
133 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
134 if (vm_page_buckets
[i
].hi_count
) {
136 highsum
+= vm_page_buckets
[i
].hi_count
;
137 if (vm_page_buckets
[i
].hi_count
> maxdepth
)
138 maxdepth
= vm_page_buckets
[i
].hi_count
;
141 printf("Total number of buckets: %d\n", vm_page_bucket_count
);
142 printf("Number used buckets: %d = %d%%\n",
143 numbuckets
, 100*numbuckets
/vm_page_bucket_count
);
144 printf("Number unused buckets: %d = %d%%\n",
145 vm_page_bucket_count
- numbuckets
,
146 100*(vm_page_bucket_count
-numbuckets
)/vm_page_bucket_count
);
147 printf("Sum of bucket max depth: %d\n", highsum
);
148 printf("Average bucket depth: %d.%2d\n",
149 highsum
/vm_page_bucket_count
,
150 highsum%vm_page_bucket_count
);
151 printf("Maximum bucket depth: %d\n", maxdepth
);
153 #endif /* MACH_PAGE_HASH_STATS */
156 * The virtual page size is currently implemented as a runtime
157 * variable, but is constant once initialized using vm_set_page_size.
158 * This initialization must be done in the machine-dependent
159 * bootstrap sequence, before calling other machine-independent
162 * All references to the virtual page size outside this
163 * module must use the PAGE_SIZE, PAGE_MASK and PAGE_SHIFT
166 #ifndef PAGE_SIZE_FIXED
167 vm_size_t page_size
= 4096;
168 vm_size_t page_mask
= 4095;
170 #endif /* PAGE_SIZE_FIXED */
173 * Resident page structures are initialized from
174 * a template (see vm_page_alloc).
176 * When adding a new field to the virtual memory
177 * object structure, be sure to add initialization
178 * (see vm_page_bootstrap).
180 struct vm_page vm_page_template
;
183 * Resident pages that represent real memory
184 * are allocated from a free list.
186 vm_page_t vm_page_queue_free
;
187 vm_page_t vm_page_queue_fictitious
;
188 decl_mutex_data(,vm_page_queue_free_lock
)
189 unsigned int vm_page_free_wanted
;
190 int vm_page_free_count
;
191 int vm_page_fictitious_count
;
193 unsigned int vm_page_free_count_minimum
; /* debugging */
196 * Occasionally, the virtual memory system uses
197 * resident page structures that do not refer to
198 * real pages, for example to leave a page with
199 * important state information in the VP table.
201 * These page structures are allocated the way
202 * most other kernel structures are.
205 decl_mutex_data(,vm_page_alloc_lock
)
208 * Fictitious pages don't have a physical address,
209 * but we must initialize phys_addr to something.
210 * For debugging, this should be a strange value
211 * that the pmap module can recognize in assertions.
213 vm_offset_t vm_page_fictitious_addr
= (vm_offset_t
) -1;
216 * Resident page structures are also chained on
217 * queues that are used by the page replacement
218 * system (pageout daemon). These queues are
219 * defined here, but are shared by the pageout
222 queue_head_t vm_page_queue_active
;
223 queue_head_t vm_page_queue_inactive
;
224 decl_mutex_data(,vm_page_queue_lock
)
225 int vm_page_active_count
;
226 int vm_page_inactive_count
;
227 int vm_page_wire_count
;
228 int vm_page_gobble_count
= 0;
229 int vm_page_wire_count_warning
= 0;
230 int vm_page_gobble_count_warning
= 0;
232 /* the following fields are protected by the vm_page_queue_lock */
233 queue_head_t vm_page_queue_limbo
;
234 int vm_page_limbo_count
= 0; /* total pages in limbo */
235 int vm_page_limbo_real_count
= 0; /* real pages in limbo */
236 int vm_page_pin_count
= 0; /* number of pinned pages */
238 decl_simple_lock_data(,vm_page_preppin_lock
)
241 * Several page replacement parameters are also
242 * shared with this module, so that page allocation
243 * (done here in vm_page_alloc) can trigger the
246 int vm_page_free_target
= 0;
247 int vm_page_free_min
= 0;
248 int vm_page_inactive_target
= 0;
249 int vm_page_free_reserved
= 0;
250 int vm_page_laundry_count
= 0;
253 * The VM system has a couple of heuristics for deciding
254 * that pages are "uninteresting" and should be placed
255 * on the inactive queue as likely candidates for replacement.
256 * These variables let the heuristics be controlled at run-time
257 * to make experimentation easier.
260 boolean_t vm_page_deactivate_hint
= TRUE
;
265 * Sets the page size, perhaps based upon the memory
266 * size. Must be called before any use of page-size
267 * dependent functions.
269 * Sets page_shift and page_mask from page_size.
272 vm_set_page_size(void)
274 #ifndef PAGE_SIZE_FIXED
275 page_mask
= page_size
- 1;
277 if ((page_mask
& page_size
) != 0)
278 panic("vm_set_page_size: page size not a power of two");
280 for (page_shift
= 0; ; page_shift
++)
281 if ((1 << page_shift
) == page_size
)
283 #endif /* PAGE_SIZE_FIXED */
289 * Initializes the resident memory module.
291 * Allocates memory for the page cells, and
292 * for the object/offset-to-page hash table headers.
293 * Each page cell is initialized and placed on the free list.
294 * Returns the range of available kernel virtual memory.
302 register vm_page_t m
;
309 * Initialize the vm_page template.
312 m
= &vm_page_template
;
313 m
->object
= VM_OBJECT_NULL
; /* reset later */
314 m
->offset
= 0; /* reset later */
322 m
->reference
= FALSE
;
324 m
->dump_cleaning
= FALSE
;
325 m
->list_req_pending
= FALSE
;
330 m
->fictitious
= FALSE
;
337 m
->clustered
= FALSE
;
338 m
->lock_supplied
= FALSE
;
342 m
->phys_addr
= 0; /* reset later */
344 m
->page_lock
= VM_PROT_NONE
;
345 m
->unlock_request
= VM_PROT_NONE
;
346 m
->page_error
= KERN_SUCCESS
;
349 * Initialize the page queues.
352 mutex_init(&vm_page_queue_free_lock
, ETAP_VM_PAGEQ_FREE
);
353 mutex_init(&vm_page_queue_lock
, ETAP_VM_PAGEQ
);
354 simple_lock_init(&vm_page_preppin_lock
, ETAP_VM_PREPPIN
);
356 vm_page_queue_free
= VM_PAGE_NULL
;
357 vm_page_queue_fictitious
= VM_PAGE_NULL
;
358 queue_init(&vm_page_queue_active
);
359 queue_init(&vm_page_queue_inactive
);
360 queue_init(&vm_page_queue_limbo
);
362 vm_page_free_wanted
= 0;
365 * Steal memory for the map and zone subsystems.
368 vm_map_steal_memory();
372 * Allocate (and initialize) the virtual-to-physical
373 * table hash buckets.
375 * The number of buckets should be a power of two to
376 * get a good hash function. The following computation
377 * chooses the first power of two that is greater
378 * than the number of physical pages in the system.
381 simple_lock_init(&vm_page_bucket_lock
, ETAP_VM_BUCKET
);
383 if (vm_page_bucket_count
== 0) {
384 unsigned int npages
= pmap_free_pages();
386 vm_page_bucket_count
= 1;
387 while (vm_page_bucket_count
< npages
)
388 vm_page_bucket_count
<<= 1;
391 vm_page_hash_mask
= vm_page_bucket_count
- 1;
394 * Calculate object shift value for hashing algorithm:
395 * O = log2(sizeof(struct vm_object))
396 * B = log2(vm_page_bucket_count)
397 * hash shifts the object left by
400 size
= vm_page_bucket_count
;
401 for (log1
= 0; size
> 1; log1
++)
403 size
= sizeof(struct vm_object
);
404 for (log2
= 0; size
> 1; log2
++)
406 vm_page_hash_shift
= log1
/2 - log2
+ 1;
408 if (vm_page_hash_mask
& vm_page_bucket_count
)
409 printf("vm_page_bootstrap: WARNING -- strange page hash\n");
411 vm_page_buckets
= (vm_page_bucket_t
*)
412 pmap_steal_memory(vm_page_bucket_count
*
413 sizeof(vm_page_bucket_t
));
415 for (i
= 0; i
< vm_page_bucket_count
; i
++) {
416 register vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
418 bucket
->pages
= VM_PAGE_NULL
;
419 #if MACH_PAGE_HASH_STATS
420 bucket
->cur_count
= 0;
421 bucket
->hi_count
= 0;
422 #endif /* MACH_PAGE_HASH_STATS */
426 * Machine-dependent code allocates the resident page table.
427 * It uses vm_page_init to initialize the page frames.
428 * The code also returns to us the virtual space available
429 * to the kernel. We don't trust the pmap module
430 * to get the alignment right.
433 pmap_startup(&virtual_space_start
, &virtual_space_end
);
434 virtual_space_start
= round_page(virtual_space_start
);
435 virtual_space_end
= trunc_page(virtual_space_end
);
437 *startp
= virtual_space_start
;
438 *endp
= virtual_space_end
;
441 * Compute the initial "wire" count.
442 * Up until now, the pages which have been set aside are not under
443 * the VM system's control, so although they aren't explicitly
444 * wired, they nonetheless can't be moved. At this moment,
445 * all VM managed pages are "free", courtesy of pmap_startup.
447 vm_page_wire_count
= atop(mem_size
) - vm_page_free_count
; /* initial value */
449 printf("vm_page_bootstrap: %d free pages\n", vm_page_free_count
);
450 vm_page_free_count_minimum
= vm_page_free_count
;
453 #ifndef MACHINE_PAGES
455 * We implement pmap_steal_memory and pmap_startup with the help
456 * of two simpler functions, pmap_virtual_space and pmap_next_page.
463 vm_offset_t addr
, vaddr
, paddr
;
466 * We round the size to a round multiple.
469 size
= (size
+ sizeof (void *) - 1) &~ (sizeof (void *) - 1);
472 * If this is the first call to pmap_steal_memory,
473 * we have to initialize ourself.
476 if (virtual_space_start
== virtual_space_end
) {
477 pmap_virtual_space(&virtual_space_start
, &virtual_space_end
);
480 * The initial values must be aligned properly, and
481 * we don't trust the pmap module to do it right.
484 virtual_space_start
= round_page(virtual_space_start
);
485 virtual_space_end
= trunc_page(virtual_space_end
);
489 * Allocate virtual memory for this request.
492 addr
= virtual_space_start
;
493 virtual_space_start
+= size
;
495 kprintf("pmap_steal_memory: %08X - %08X; size=%08X\n", addr
, virtual_space_start
, size
); /* (TEST/DEBUG) */
498 * Allocate and map physical pages to back new virtual pages.
501 for (vaddr
= round_page(addr
);
503 vaddr
+= PAGE_SIZE
) {
504 if (!pmap_next_page(&paddr
))
505 panic("pmap_steal_memory");
508 * XXX Logically, these mappings should be wired,
509 * but some pmap modules barf if they are.
512 pmap_enter(kernel_pmap
, vaddr
, paddr
,
513 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
515 * Account for newly stolen memory
517 vm_page_wire_count
++;
529 unsigned int i
, npages
, pages_initialized
;
534 * We calculate how many page frames we will have
535 * and then allocate the page structures in one chunk.
538 npages
= ((PAGE_SIZE
* pmap_free_pages() +
539 (round_page(virtual_space_start
) - virtual_space_start
)) /
540 (PAGE_SIZE
+ sizeof *pages
));
542 pages
= (vm_page_t
) pmap_steal_memory(npages
* sizeof *pages
);
545 * Initialize the page frames.
548 for (i
= 0, pages_initialized
= 0; i
< npages
; i
++) {
549 if (!pmap_next_page(&paddr
))
552 vm_page_init(&pages
[i
], paddr
);
558 * Release pages in reverse order so that physical pages
559 * initially get allocated in ascending addresses. This keeps
560 * the devices (which must address physical memory) happy if
561 * they require several consecutive pages.
564 for (i
= pages_initialized
; i
> 0; i
--) {
565 vm_page_release(&pages
[i
- 1]);
569 * We have to re-align virtual_space_start,
570 * because pmap_steal_memory has been using it.
573 virtual_space_start
= round_page(virtual_space_start
);
575 *startp
= virtual_space_start
;
576 *endp
= virtual_space_end
;
578 #endif /* MACHINE_PAGES */
581 * Routine: vm_page_module_init
583 * Second initialization pass, to be done after
584 * the basic VM system is ready.
587 vm_page_module_init(void)
589 vm_page_zone
= zinit((vm_size_t
) sizeof(struct vm_page
),
590 0, PAGE_SIZE
, "vm pages");
593 zone_debug_disable(vm_page_zone
);
594 #endif /* ZONE_DEBUG */
596 zone_change(vm_page_zone
, Z_EXPAND
, FALSE
);
597 zone_change(vm_page_zone
, Z_EXHAUST
, TRUE
);
598 zone_change(vm_page_zone
, Z_FOREIGN
, TRUE
);
601 * Adjust zone statistics to account for the real pages allocated
602 * in vm_page_create(). [Q: is this really what we want?]
604 vm_page_zone
->count
+= vm_page_pages
;
605 vm_page_zone
->cur_size
+= vm_page_pages
* vm_page_zone
->elem_size
;
607 mutex_init(&vm_page_alloc_lock
, ETAP_VM_PAGE_ALLOC
);
611 * Routine: vm_page_create
613 * After the VM system is up, machine-dependent code
614 * may stumble across more physical memory. For example,
615 * memory that it was reserving for a frame buffer.
616 * vm_page_create turns this memory into available pages.
627 for (paddr
= round_page(start
);
628 paddr
< trunc_page(end
);
629 paddr
+= PAGE_SIZE
) {
630 while ((m
= (vm_page_t
) vm_page_grab_fictitious())
632 vm_page_more_fictitious();
634 vm_page_init(m
, paddr
);
643 * Distributes the object/offset key pair among hash buckets.
645 * NOTE: To get a good hash function, the bucket count should
648 #define vm_page_hash(object, offset) (\
649 ( ((natural_t)(vm_offset_t)object<<vm_page_hash_shift) + (natural_t)atop(offset))\
653 * vm_page_insert: [ internal use only ]
655 * Inserts the given mem entry into the object/object-page
656 * table and object list.
658 * The object must be locked.
663 register vm_page_t mem
,
664 register vm_object_t object
,
665 register vm_object_offset_t offset
)
667 register vm_page_bucket_t
*bucket
;
670 "vm_page_insert, object 0x%X offset 0x%X page 0x%X\n",
671 (integer_t
)object
, (integer_t
)offset
, (integer_t
)mem
, 0,0);
676 panic("vm_page_insert");
678 assert(!object
->internal
|| offset
< object
->size
);
680 /* only insert "pageout" pages into "pageout" objects,
681 * and normal pages into normal objects */
682 assert(object
->pageout
== mem
->pageout
);
685 * Record the object/offset pair in this page
688 mem
->object
= object
;
689 mem
->offset
= offset
;
692 * Insert it into the object_object/offset hash table
695 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
696 simple_lock(&vm_page_bucket_lock
);
697 mem
->next
= bucket
->pages
;
699 #if MACH_PAGE_HASH_STATS
700 if (++bucket
->cur_count
> bucket
->hi_count
)
701 bucket
->hi_count
= bucket
->cur_count
;
702 #endif /* MACH_PAGE_HASH_STATS */
703 simple_unlock(&vm_page_bucket_lock
);
706 * Now link into the object's list of backed pages.
709 queue_enter(&object
->memq
, mem
, vm_page_t
, listq
);
713 * Show that the object has one more resident page.
716 object
->resident_page_count
++;
722 * Exactly like vm_page_insert, except that we first
723 * remove any existing page at the given offset in object.
725 * The object and page queues must be locked.
730 register vm_page_t mem
,
731 register vm_object_t object
,
732 register vm_object_offset_t offset
)
734 register vm_page_bucket_t
*bucket
;
739 panic("vm_page_replace");
742 * Record the object/offset pair in this page
745 mem
->object
= object
;
746 mem
->offset
= offset
;
749 * Insert it into the object_object/offset hash table,
750 * replacing any page that might have been there.
753 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
754 simple_lock(&vm_page_bucket_lock
);
756 vm_page_t
*mp
= &bucket
->pages
;
757 register vm_page_t m
= *mp
;
759 if (m
->object
== object
&& m
->offset
== offset
) {
761 * Remove page from bucket and from object,
762 * and return it to the free list.
765 queue_remove(&object
->memq
, m
, vm_page_t
,
768 object
->resident_page_count
--;
771 * Return page to the free list.
772 * Note the page is not tabled now, so this
773 * won't self-deadlock on the bucket lock.
781 mem
->next
= bucket
->pages
;
783 mem
->next
= VM_PAGE_NULL
;
786 simple_unlock(&vm_page_bucket_lock
);
789 * Now link into the object's list of backed pages.
792 queue_enter(&object
->memq
, mem
, vm_page_t
, listq
);
796 * And show that the object has one more resident
800 object
->resident_page_count
++;
804 * vm_page_remove: [ internal use only ]
806 * Removes the given mem entry from the object/offset-page
807 * table and the object page list.
809 * The object and page must be locked.
814 register vm_page_t mem
)
816 register vm_page_bucket_t
*bucket
;
817 register vm_page_t
this;
820 "vm_page_remove, object 0x%X offset 0x%X page 0x%X\n",
821 (integer_t
)mem
->object
, (integer_t
)mem
->offset
,
822 (integer_t
)mem
, 0,0);
825 assert(!mem
->cleaning
);
829 * Remove from the object_object/offset hash table
832 bucket
= &vm_page_buckets
[vm_page_hash(mem
->object
, mem
->offset
)];
833 simple_lock(&vm_page_bucket_lock
);
834 if ((this = bucket
->pages
) == mem
) {
835 /* optimize for common case */
837 bucket
->pages
= mem
->next
;
839 register vm_page_t
*prev
;
841 for (prev
= &this->next
;
842 (this = *prev
) != mem
;
847 #if MACH_PAGE_HASH_STATS
849 #endif /* MACH_PAGE_HASH_STATS */
850 simple_unlock(&vm_page_bucket_lock
);
853 * Now remove from the object's list of backed pages.
856 queue_remove(&mem
->object
->memq
, mem
, vm_page_t
, listq
);
859 * And show that the object has one fewer resident
863 mem
->object
->resident_page_count
--;
866 mem
->object
= VM_OBJECT_NULL
;
873 * Returns the page associated with the object/offset
874 * pair specified; if none is found, VM_PAGE_NULL is returned.
876 * The object must be locked. No side effects.
881 register vm_object_t object
,
882 register vm_object_offset_t offset
)
884 register vm_page_t mem
;
885 register vm_page_bucket_t
*bucket
;
888 * Search the hash table for this object/offset pair
891 bucket
= &vm_page_buckets
[vm_page_hash(object
, offset
)];
893 simple_lock(&vm_page_bucket_lock
);
894 for (mem
= bucket
->pages
; mem
!= VM_PAGE_NULL
; mem
= mem
->next
) {
896 if ((mem
->object
== object
) && (mem
->offset
== offset
))
899 simple_unlock(&vm_page_bucket_lock
);
906 * Move the given memory entry from its
907 * current object to the specified target object/offset.
909 * The object must be locked.
913 register vm_page_t mem
,
914 register vm_object_t new_object
,
915 vm_object_offset_t new_offset
)
917 assert(mem
->object
!= new_object
);
919 * Changes to mem->object require the page lock because
920 * the pageout daemon uses that lock to get the object.
924 "vm_page_rename, new object 0x%X, offset 0x%X page 0x%X\n",
925 (integer_t
)new_object
, (integer_t
)new_offset
,
926 (integer_t
)mem
, 0,0);
928 vm_page_lock_queues();
930 vm_page_insert(mem
, new_object
, new_offset
);
931 vm_page_unlock_queues();
937 * Initialize the fields in a new page.
938 * This takes a structure with random values and initializes it
939 * so that it can be given to vm_page_release or vm_page_insert.
944 vm_offset_t phys_addr
)
946 *mem
= vm_page_template
;
947 mem
->phys_addr
= phys_addr
;
951 * vm_page_grab_fictitious:
953 * Remove a fictitious page from the free list.
954 * Returns VM_PAGE_NULL if there are no free pages.
956 int c_vm_page_grab_fictitious
= 0;
957 int c_vm_page_release_fictitious
= 0;
958 int c_vm_page_more_fictitious
= 0;
961 vm_page_grab_fictitious(void)
963 register vm_page_t m
;
965 m
= (vm_page_t
)zget(vm_page_zone
);
967 vm_page_init(m
, vm_page_fictitious_addr
);
968 m
->fictitious
= TRUE
;
971 c_vm_page_grab_fictitious
++;
976 * vm_page_release_fictitious:
978 * Release a fictitious page to the free list.
982 vm_page_release_fictitious(
983 register vm_page_t m
)
987 assert(m
->fictitious
);
988 assert(m
->phys_addr
== vm_page_fictitious_addr
);
990 c_vm_page_release_fictitious
++;
993 panic("vm_page_release_fictitious");
995 zfree(vm_page_zone
, (vm_offset_t
)m
);
999 * vm_page_more_fictitious:
1001 * Add more fictitious pages to the free list.
1002 * Allowed to block. This routine is way intimate
1003 * with the zones code, for several reasons:
1004 * 1. we need to carve some page structures out of physical
1005 * memory before zones work, so they _cannot_ come from
1007 * 2. the zone needs to be collectable in order to prevent
1008 * growth without bound. These structures are used by
1009 * the device pager (by the hundreds and thousands), as
1010 * private pages for pageout, and as blocking pages for
1011 * pagein. Temporary bursts in demand should not result in
1012 * permanent allocation of a resource.
1013 * 3. To smooth allocation humps, we allocate single pages
1014 * with kernel_memory_allocate(), and cram them into the
1015 * zone. This also allows us to initialize the vm_page_t's
1016 * on the way into the zone, so that zget() always returns
1017 * an initialized structure. The zone free element pointer
1018 * and the free page pointer are both the first item in the
1020 * 4. By having the pages in the zone pre-initialized, we need
1021 * not keep 2 levels of lists. The garbage collector simply
1022 * scans our list, and reduces physical memory usage as it
1026 void vm_page_more_fictitious(void)
1028 extern vm_map_t zone_map
;
1029 register vm_page_t m
;
1031 kern_return_t retval
;
1034 c_vm_page_more_fictitious
++;
1037 * Allocate a single page from the zone_map. Do not wait if no physical
1038 * pages are immediately available, and do not zero the space. We need
1039 * our own blocking lock here to prevent having multiple,
1040 * simultaneous requests from piling up on the zone_map lock. Exactly
1041 * one (of our) threads should be potentially waiting on the map lock.
1042 * If winner is not vm-privileged, then the page allocation will fail,
1043 * and it will temporarily block here in the vm_page_wait().
1045 mutex_lock(&vm_page_alloc_lock
);
1047 * If another thread allocated space, just bail out now.
1049 if (zone_free_count(vm_page_zone
) > 5) {
1051 * The number "5" is a small number that is larger than the
1052 * number of fictitious pages that any single caller will
1053 * attempt to allocate. Otherwise, a thread will attempt to
1054 * acquire a fictitious page (vm_page_grab_fictitious), fail,
1055 * release all of the resources and locks already acquired,
1056 * and then call this routine. This routine finds the pages
1057 * that the caller released, so fails to allocate new space.
1058 * The process repeats infinitely. The largest known number
1059 * of fictitious pages required in this manner is 2. 5 is
1060 * simply a somewhat larger number.
1062 mutex_unlock(&vm_page_alloc_lock
);
1066 if ((retval
= kernel_memory_allocate(zone_map
,
1067 &addr
, PAGE_SIZE
, VM_PROT_ALL
,
1068 KMA_KOBJECT
|KMA_NOPAGEWAIT
)) != KERN_SUCCESS
) {
1070 * No page was available. Tell the pageout daemon, drop the
1071 * lock to give another thread a chance at it, and
1072 * wait for the pageout daemon to make progress.
1074 mutex_unlock(&vm_page_alloc_lock
);
1075 vm_page_wait(THREAD_UNINT
);
1079 * Initialize as many vm_page_t's as will fit on this page. This
1080 * depends on the zone code disturbing ONLY the first item of
1081 * each zone element.
1083 m
= (vm_page_t
)addr
;
1084 for (i
= PAGE_SIZE
/sizeof(struct vm_page
); i
> 0; i
--) {
1085 vm_page_init(m
, vm_page_fictitious_addr
);
1086 m
->fictitious
= TRUE
;
1089 zcram(vm_page_zone
, addr
, PAGE_SIZE
);
1090 mutex_unlock(&vm_page_alloc_lock
);
1096 * Attempt to convert a fictitious page into a real page.
1101 register vm_page_t m
)
1103 register vm_page_t real_m
;
1106 assert(m
->fictitious
);
1109 real_m
= vm_page_grab();
1110 if (real_m
== VM_PAGE_NULL
)
1113 m
->phys_addr
= real_m
->phys_addr
;
1114 m
->fictitious
= FALSE
;
1117 vm_page_lock_queues();
1119 vm_page_active_count
++;
1120 else if (m
->inactive
)
1121 vm_page_inactive_count
++;
1122 vm_page_unlock_queues();
1124 real_m
->phys_addr
= vm_page_fictitious_addr
;
1125 real_m
->fictitious
= TRUE
;
1127 vm_page_release_fictitious(real_m
);
1134 * Return true if it is not likely that a non-vm_privileged thread
1135 * can get memory without blocking. Advisory only, since the
1136 * situation may change under us.
1141 /* No locking, at worst we will fib. */
1142 return( vm_page_free_count
< vm_page_free_reserved
);
1148 * Remove a page from the free list.
1149 * Returns VM_PAGE_NULL if the free list is too small.
1152 unsigned long vm_page_grab_count
= 0; /* measure demand */
1157 register vm_page_t mem
;
1159 mutex_lock(&vm_page_queue_free_lock
);
1160 vm_page_grab_count
++;
1163 * Optionally produce warnings if the wire or gobble
1164 * counts exceed some threshold.
1166 if (vm_page_wire_count_warning
> 0
1167 && vm_page_wire_count
>= vm_page_wire_count_warning
) {
1168 printf("mk: vm_page_grab(): high wired page count of %d\n",
1169 vm_page_wire_count
);
1170 assert(vm_page_wire_count
< vm_page_wire_count_warning
);
1172 if (vm_page_gobble_count_warning
> 0
1173 && vm_page_gobble_count
>= vm_page_gobble_count_warning
) {
1174 printf("mk: vm_page_grab(): high gobbled page count of %d\n",
1175 vm_page_gobble_count
);
1176 assert(vm_page_gobble_count
< vm_page_gobble_count_warning
);
1180 * Only let privileged threads (involved in pageout)
1181 * dip into the reserved pool.
1184 if ((vm_page_free_count
< vm_page_free_reserved
) &&
1185 !current_thread()->vm_privilege
) {
1186 mutex_unlock(&vm_page_queue_free_lock
);
1188 goto wakeup_pageout
;
1191 while (vm_page_queue_free
== VM_PAGE_NULL
) {
1192 printf("vm_page_grab: no free pages, trouble expected...\n");
1193 mutex_unlock(&vm_page_queue_free_lock
);
1195 mutex_lock(&vm_page_queue_free_lock
);
1198 if (--vm_page_free_count
< vm_page_free_count_minimum
)
1199 vm_page_free_count_minimum
= vm_page_free_count
;
1200 mem
= vm_page_queue_free
;
1201 vm_page_queue_free
= (vm_page_t
) mem
->pageq
.next
;
1203 mem
->no_isync
= TRUE
;
1204 mutex_unlock(&vm_page_queue_free_lock
);
1207 * Decide if we should poke the pageout daemon.
1208 * We do this if the free count is less than the low
1209 * water mark, or if the free count is less than the high
1210 * water mark (but above the low water mark) and the inactive
1211 * count is less than its target.
1213 * We don't have the counts locked ... if they change a little,
1214 * it doesn't really matter.
1218 if ((vm_page_free_count
< vm_page_free_min
) ||
1219 ((vm_page_free_count
< vm_page_free_target
) &&
1220 (vm_page_inactive_count
< vm_page_inactive_target
)))
1221 thread_wakeup((event_t
) &vm_page_free_wanted
);
1223 // dbgLog(mem->phys_addr, vm_page_free_count, vm_page_wire_count, 4); /* (TEST/DEBUG) */
1231 * Return a page to the free list.
1236 register vm_page_t mem
)
1238 assert(!mem
->private && !mem
->fictitious
);
1240 // dbgLog(mem->phys_addr, vm_page_free_count, vm_page_wire_count, 5); /* (TEST/DEBUG) */
1242 mutex_lock(&vm_page_queue_free_lock
);
1244 panic("vm_page_release");
1246 mem
->pageq
.next
= (queue_entry_t
) vm_page_queue_free
;
1247 vm_page_queue_free
= mem
;
1248 vm_page_free_count
++;
1251 * Check if we should wake up someone waiting for page.
1252 * But don't bother waking them unless they can allocate.
1254 * We wakeup only one thread, to prevent starvation.
1255 * Because the scheduling system handles wait queues FIFO,
1256 * if we wakeup all waiting threads, one greedy thread
1257 * can starve multiple niceguy threads. When the threads
1258 * all wakeup, the greedy threads runs first, grabs the page,
1259 * and waits for another page. It will be the first to run
1260 * when the next page is freed.
1262 * However, there is a slight danger here.
1263 * The thread we wake might not use the free page.
1264 * Then the other threads could wait indefinitely
1265 * while the page goes unused. To forestall this,
1266 * the pageout daemon will keep making free pages
1267 * as long as vm_page_free_wanted is non-zero.
1270 if ((vm_page_free_wanted
> 0) &&
1271 (vm_page_free_count
>= vm_page_free_reserved
)) {
1272 vm_page_free_wanted
--;
1273 thread_wakeup_one((event_t
) &vm_page_free_count
);
1276 mutex_unlock(&vm_page_queue_free_lock
);
1282 * Wait for a page to become available.
1283 * If there are plenty of free pages, then we don't sleep.
1286 * TRUE: There may be another page, try again
1287 * FALSE: We were interrupted out of our wait, don't try again
1295 * We can't use vm_page_free_reserved to make this
1296 * determination. Consider: some thread might
1297 * need to allocate two pages. The first allocation
1298 * succeeds, the second fails. After the first page is freed,
1299 * a call to vm_page_wait must really block.
1301 kern_return_t wait_result
;
1302 int need_wakeup
= 0;
1304 mutex_lock(&vm_page_queue_free_lock
);
1305 if (vm_page_free_count
< vm_page_free_target
) {
1306 if (vm_page_free_wanted
++ == 0)
1308 assert_wait((event_t
)&vm_page_free_count
, interruptible
);
1309 mutex_unlock(&vm_page_queue_free_lock
);
1310 counter(c_vm_page_wait_block
++);
1313 thread_wakeup((event_t
)&vm_page_free_wanted
);
1314 wait_result
= thread_block((void (*)(void))0);
1316 return(wait_result
== THREAD_AWAKENED
);
1318 mutex_unlock(&vm_page_queue_free_lock
);
1326 * Allocate and return a memory cell associated
1327 * with this VM object/offset pair.
1329 * Object must be locked.
1335 vm_object_offset_t offset
)
1337 register vm_page_t mem
;
1339 mem
= vm_page_grab();
1340 if (mem
== VM_PAGE_NULL
)
1341 return VM_PAGE_NULL
;
1343 vm_page_insert(mem
, object
, offset
);
1348 counter(unsigned int c_laundry_pages_freed
= 0;)
1350 int vm_pagein_cluster_unused
= 0;
1351 boolean_t vm_page_free_verify
= FALSE
;
1355 * Returns the given page to the free list,
1356 * disassociating it with any VM object.
1358 * Object and page queues must be locked prior to entry.
1362 register vm_page_t mem
)
1364 vm_object_t object
= mem
->object
;
1367 assert(!mem
->cleaning
);
1368 assert(!mem
->pageout
);
1369 assert(!vm_page_free_verify
|| pmap_verify_free(mem
->phys_addr
));
1372 vm_page_remove(mem
); /* clears tabled, object, offset */
1373 VM_PAGE_QUEUES_REMOVE(mem
); /* clears active or inactive */
1375 if (mem
->clustered
) {
1376 mem
->clustered
= FALSE
;
1377 vm_pagein_cluster_unused
++;
1380 if (mem
->wire_count
) {
1381 if (!mem
->private && !mem
->fictitious
)
1382 vm_page_wire_count
--;
1383 mem
->wire_count
= 0;
1384 assert(!mem
->gobbled
);
1385 } else if (mem
->gobbled
) {
1386 if (!mem
->private && !mem
->fictitious
)
1387 vm_page_wire_count
--;
1388 vm_page_gobble_count
--;
1390 mem
->gobbled
= FALSE
;
1393 extern int vm_page_laundry_min
;
1394 vm_page_laundry_count
--;
1395 mem
->laundry
= FALSE
; /* laundry is now clear */
1396 counter(++c_laundry_pages_freed
);
1397 if (vm_page_laundry_count
< vm_page_laundry_min
) {
1398 vm_page_laundry_min
= 0;
1399 thread_wakeup((event_t
) &vm_page_laundry_count
);
1403 mem
->discard_request
= FALSE
;
1405 PAGE_WAKEUP(mem
); /* clears wanted */
1408 vm_object_absent_release(object
);
1410 /* Some of these may be unnecessary */
1412 mem
->unlock_request
= 0;
1414 mem
->absent
= FALSE
;
1417 mem
->precious
= FALSE
;
1418 mem
->reference
= FALSE
;
1420 mem
->page_error
= KERN_SUCCESS
;
1423 mem
->private = FALSE
;
1424 mem
->fictitious
= TRUE
;
1425 mem
->phys_addr
= vm_page_fictitious_addr
;
1427 if (mem
->fictitious
) {
1428 vm_page_release_fictitious(mem
);
1430 vm_page_init(mem
, mem
->phys_addr
);
1431 vm_page_release(mem
);
1438 * Mark this page as wired down by yet
1439 * another map, removing it from paging queues
1442 * The page's object and the page queues must be locked.
1446 register vm_page_t mem
)
1449 // dbgLog(current_act(), mem->offset, mem->object, 1); /* (TEST/DEBUG) */
1453 if (mem
->wire_count
== 0) {
1454 VM_PAGE_QUEUES_REMOVE(mem
);
1455 if (!mem
->private && !mem
->fictitious
&& !mem
->gobbled
)
1456 vm_page_wire_count
++;
1458 vm_page_gobble_count
--;
1459 mem
->gobbled
= FALSE
;
1461 assert(!mem
->gobbled
);
1468 * Mark this page as consumed by the vm/ipc/xmm subsystems.
1470 * Called only for freshly vm_page_grab()ed pages - w/ nothing locked.
1474 register vm_page_t mem
)
1476 vm_page_lock_queues();
1479 assert(!mem
->gobbled
);
1480 assert(mem
->wire_count
== 0);
1482 if (!mem
->gobbled
&& mem
->wire_count
== 0) {
1483 if (!mem
->private && !mem
->fictitious
)
1484 vm_page_wire_count
++;
1486 vm_page_gobble_count
++;
1487 mem
->gobbled
= TRUE
;
1488 vm_page_unlock_queues();
1494 * Release one wiring of this page, potentially
1495 * enabling it to be paged again.
1497 * The page's object and the page queues must be locked.
1501 register vm_page_t mem
)
1504 // dbgLog(current_act(), mem->offset, mem->object, 0); /* (TEST/DEBUG) */
1507 assert(mem
->wire_count
> 0);
1509 if (--mem
->wire_count
== 0) {
1510 assert(!mem
->private && !mem
->fictitious
);
1511 vm_page_wire_count
--;
1512 queue_enter(&vm_page_queue_active
, mem
, vm_page_t
, pageq
);
1513 vm_page_active_count
++;
1515 mem
->reference
= TRUE
;
1520 * vm_page_deactivate:
1522 * Returns the given page to the inactive list,
1523 * indicating that no physical maps have access
1524 * to this page. [Used by the physical mapping system.]
1526 * The page queues must be locked.
1530 register vm_page_t m
)
1534 // dbgLog(m->phys_addr, vm_page_free_count, vm_page_wire_count, 6); /* (TEST/DEBUG) */
1537 * This page is no longer very interesting. If it was
1538 * interesting (active or inactive/referenced), then we
1539 * clear the reference bit and (re)enter it in the
1540 * inactive queue. Note wired pages should not have
1541 * their reference bit cleared.
1543 if (m
->gobbled
) { /* can this happen? */
1544 assert(m
->wire_count
== 0);
1545 if (!m
->private && !m
->fictitious
)
1546 vm_page_wire_count
--;
1547 vm_page_gobble_count
--;
1550 if (m
->private || (m
->wire_count
!= 0))
1552 if (m
->active
|| (m
->inactive
&& m
->reference
)) {
1553 if (!m
->fictitious
&& !m
->absent
)
1554 pmap_clear_reference(m
->phys_addr
);
1555 m
->reference
= FALSE
;
1556 VM_PAGE_QUEUES_REMOVE(m
);
1558 if (m
->wire_count
== 0 && !m
->inactive
) {
1559 m
->page_ticket
= vm_page_ticket
;
1560 vm_page_ticket_roll
++;
1562 if(vm_page_ticket_roll
== VM_PAGE_TICKETS_IN_ROLL
) {
1563 vm_page_ticket_roll
= 0;
1564 if(vm_page_ticket
== VM_PAGE_TICKET_ROLL_IDS
)
1570 queue_enter(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
1573 vm_page_inactive_count
++;
1580 * Put the specified page on the active list (if appropriate).
1582 * The page queues must be locked.
1587 register vm_page_t m
)
1592 assert(m
->wire_count
== 0);
1593 if (!m
->private && !m
->fictitious
)
1594 vm_page_wire_count
--;
1595 vm_page_gobble_count
--;
1602 queue_remove(&vm_page_queue_inactive
, m
, vm_page_t
, pageq
);
1604 vm_page_inactive_count
--;
1605 m
->inactive
= FALSE
;
1607 if (m
->wire_count
== 0) {
1609 panic("vm_page_activate: already active");
1611 queue_enter(&vm_page_queue_active
, m
, vm_page_t
, pageq
);
1613 m
->reference
= TRUE
;
1615 vm_page_active_count
++;
1620 * vm_page_part_zero_fill:
1622 * Zero-fill a part of the page.
1625 vm_page_part_zero_fill(
1633 #ifdef PMAP_ZERO_PART_PAGE_IMPLEMENTED
1634 pmap_zero_part_page(m
->phys_addr
, m_pa
, len
);
1637 tmp
= vm_page_grab();
1638 if (tmp
== VM_PAGE_NULL
) {
1639 vm_page_wait(THREAD_UNINT
);
1644 vm_page_zero_fill(tmp
);
1646 vm_page_part_copy(m
, 0, tmp
, 0, m_pa
);
1648 if((m_pa
+ len
) < PAGE_SIZE
) {
1649 vm_page_part_copy(m
, m_pa
+ len
, tmp
,
1650 m_pa
+ len
, PAGE_SIZE
- (m_pa
+ len
));
1652 vm_page_copy(tmp
,m
);
1653 vm_page_lock_queues();
1655 vm_page_unlock_queues();
1661 * vm_page_zero_fill:
1663 * Zero-fill the specified page.
1670 "vm_page_zero_fill, object 0x%X offset 0x%X page 0x%X\n",
1671 (integer_t
)m
->object
, (integer_t
)m
->offset
, (integer_t
)m
, 0,0);
1675 pmap_zero_page(m
->phys_addr
);
1679 * vm_page_part_copy:
1681 * copy part of one page to another
1692 VM_PAGE_CHECK(src_m
);
1693 VM_PAGE_CHECK(dst_m
);
1695 pmap_copy_part_page(src_m
->phys_addr
, src_pa
,
1696 dst_m
->phys_addr
, dst_pa
, len
);
1702 * Copy one page to another
1711 "vm_page_copy, object 0x%X offset 0x%X to object 0x%X offset 0x%X\n",
1712 (integer_t
)src_m
->object
, src_m
->offset
,
1713 (integer_t
)dest_m
->object
, dest_m
->offset
,
1716 VM_PAGE_CHECK(src_m
);
1717 VM_PAGE_CHECK(dest_m
);
1719 pmap_copy_page(src_m
->phys_addr
, dest_m
->phys_addr
);
1723 * Currently, this is a primitive allocator that grabs
1724 * free pages from the system, sorts them by physical
1725 * address, then searches for a region large enough to
1726 * satisfy the user's request.
1728 * Additional levels of effort:
1729 * + steal clean active/inactive pages
1730 * + force pageouts of dirty pages
1731 * + maintain a map of available physical
1735 #define SET_NEXT_PAGE(m,n) ((m)->pageq.next = (struct queue_entry *) (n))
1738 int vm_page_verify_contiguous(
1740 unsigned int npages
);
1741 #endif /* MACH_ASSERT */
1743 cpm_counter(unsigned int vpfls_pages_handled
= 0;)
1744 cpm_counter(unsigned int vpfls_head_insertions
= 0;)
1745 cpm_counter(unsigned int vpfls_tail_insertions
= 0;)
1746 cpm_counter(unsigned int vpfls_general_insertions
= 0;)
1747 cpm_counter(unsigned int vpfc_failed
= 0;)
1748 cpm_counter(unsigned int vpfc_satisfied
= 0;)
1751 * Sort free list by ascending physical address,
1752 * using a not-particularly-bright sort algorithm.
1753 * Caller holds vm_page_queue_free_lock.
1756 vm_page_free_list_sort(void)
1758 vm_page_t sort_list
;
1759 vm_page_t sort_list_end
;
1760 vm_page_t m
, m1
, *prev
, next_m
;
1763 unsigned int npages
;
1765 #endif /* MACH_ASSERT */
1769 * Verify pages in the free list..
1772 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
))
1774 if (npages
!= vm_page_free_count
)
1775 panic("vm_sort_free_list: prelim: npages %d free_count %d",
1776 npages
, vm_page_free_count
);
1777 old_free_count
= vm_page_free_count
;
1778 #endif /* MACH_ASSERT */
1780 sort_list
= sort_list_end
= vm_page_queue_free
;
1781 m
= NEXT_PAGE(vm_page_queue_free
);
1782 SET_NEXT_PAGE(vm_page_queue_free
, VM_PAGE_NULL
);
1783 cpm_counter(vpfls_pages_handled
= 0);
1784 while (m
!= VM_PAGE_NULL
) {
1785 cpm_counter(++vpfls_pages_handled
);
1786 next_m
= NEXT_PAGE(m
);
1787 if (m
->phys_addr
< sort_list
->phys_addr
) {
1788 cpm_counter(++vpfls_head_insertions
);
1789 SET_NEXT_PAGE(m
, sort_list
);
1791 } else if (m
->phys_addr
> sort_list_end
->phys_addr
) {
1792 cpm_counter(++vpfls_tail_insertions
);
1793 SET_NEXT_PAGE(sort_list_end
, m
);
1794 SET_NEXT_PAGE(m
, VM_PAGE_NULL
);
1797 cpm_counter(++vpfls_general_insertions
);
1798 /* general sorted list insertion */
1800 for (m1
=sort_list
; m1
!=VM_PAGE_NULL
; m1
=NEXT_PAGE(m1
)) {
1801 if (m1
->phys_addr
> m
->phys_addr
) {
1803 panic("vm_sort_free_list: ugh");
1804 SET_NEXT_PAGE(m
, *prev
);
1808 prev
= (vm_page_t
*) &m1
->pageq
.next
;
1816 * Verify that pages are sorted into ascending order.
1818 for (m
= sort_list
, npages
= 0; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
1819 if (m
!= sort_list
&&
1820 m
->phys_addr
<= addr
) {
1821 printf("m 0x%x addr 0x%x\n", m
, addr
);
1822 panic("vm_sort_free_list");
1824 addr
= m
->phys_addr
;
1827 if (old_free_count
!= vm_page_free_count
)
1828 panic("vm_sort_free_list: old_free %d free_count %d",
1829 old_free_count
, vm_page_free_count
);
1830 if (npages
!= vm_page_free_count
)
1831 panic("vm_sort_free_list: npages %d free_count %d",
1832 npages
, vm_page_free_count
);
1833 #endif /* MACH_ASSERT */
1835 vm_page_queue_free
= sort_list
;
1841 * Check that the list of pages is ordered by
1842 * ascending physical address and has no holes.
1845 vm_page_verify_contiguous(
1847 unsigned int npages
)
1849 register vm_page_t m
;
1850 unsigned int page_count
;
1851 vm_offset_t prev_addr
;
1853 prev_addr
= pages
->phys_addr
;
1855 for (m
= NEXT_PAGE(pages
); m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
1856 if (m
->phys_addr
!= prev_addr
+ page_size
) {
1857 printf("m 0x%x prev_addr 0x%x, current addr 0x%x\n",
1858 m
, prev_addr
, m
->phys_addr
);
1859 printf("pages 0x%x page_count %d\n", pages
, page_count
);
1860 panic("vm_page_verify_contiguous: not contiguous!");
1862 prev_addr
= m
->phys_addr
;
1865 if (page_count
!= npages
) {
1866 printf("pages 0x%x actual count 0x%x but requested 0x%x\n",
1867 pages
, page_count
, npages
);
1868 panic("vm_page_verify_contiguous: count error");
1872 #endif /* MACH_ASSERT */
1876 * Find a region large enough to contain at least npages
1877 * of contiguous physical memory.
1880 * - Called while holding vm_page_queue_free_lock.
1881 * - Doesn't respect vm_page_free_reserved; caller
1882 * must not ask for more pages than are legal to grab.
1884 * Returns a pointer to a list of gobbled pages or VM_PAGE_NULL.
1888 vm_page_find_contiguous(
1891 vm_page_t m
, *contig_prev
, *prev_ptr
;
1892 vm_offset_t prev_addr
;
1893 unsigned int contig_npages
;
1897 return VM_PAGE_NULL
;
1899 prev_addr
= vm_page_queue_free
->phys_addr
- (page_size
+ 1);
1900 prev_ptr
= &vm_page_queue_free
;
1901 for (m
= vm_page_queue_free
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
1903 if (m
->phys_addr
!= prev_addr
+ page_size
) {
1905 * Whoops! Pages aren't contiguous. Start over.
1908 contig_prev
= prev_ptr
;
1911 if (++contig_npages
== npages
) {
1913 * Chop these pages out of the free list.
1914 * Mark them all as gobbled.
1916 list
= *contig_prev
;
1917 *contig_prev
= NEXT_PAGE(m
);
1918 SET_NEXT_PAGE(m
, VM_PAGE_NULL
);
1919 for (m
= list
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
1926 vm_page_free_count
-= npages
;
1927 if (vm_page_free_count
< vm_page_free_count_minimum
)
1928 vm_page_free_count_minimum
= vm_page_free_count
;
1929 vm_page_wire_count
+= npages
;
1930 vm_page_gobble_count
+= npages
;
1931 cpm_counter(++vpfc_satisfied
);
1932 assert(vm_page_verify_contiguous(list
, contig_npages
));
1936 assert(contig_npages
< npages
);
1937 prev_ptr
= (vm_page_t
*) &m
->pageq
.next
;
1938 prev_addr
= m
->phys_addr
;
1940 cpm_counter(++vpfc_failed
);
1941 return VM_PAGE_NULL
;
1945 * Allocate a list of contiguous, wired pages.
1953 register vm_page_t m
;
1954 vm_page_t
*first_contig
;
1955 vm_page_t free_list
, pages
;
1956 unsigned int npages
, n1pages
;
1957 int vm_pages_available
;
1959 if (size
% page_size
!= 0)
1960 return KERN_INVALID_ARGUMENT
;
1962 vm_page_lock_queues();
1963 mutex_lock(&vm_page_queue_free_lock
);
1966 * Should also take active and inactive pages
1967 * into account... One day...
1969 vm_pages_available
= vm_page_free_count
- vm_page_free_reserved
;
1971 if (size
> vm_pages_available
* page_size
) {
1972 mutex_unlock(&vm_page_queue_free_lock
);
1973 return KERN_RESOURCE_SHORTAGE
;
1976 vm_page_free_list_sort();
1978 npages
= size
/ page_size
;
1981 * Obtain a pointer to a subset of the free
1982 * list large enough to satisfy the request;
1983 * the region will be physically contiguous.
1985 pages
= vm_page_find_contiguous(npages
);
1986 if (pages
== VM_PAGE_NULL
) {
1987 mutex_unlock(&vm_page_queue_free_lock
);
1988 vm_page_unlock_queues();
1989 return KERN_NO_SPACE
;
1992 mutex_unlock(&vm_page_queue_free_lock
);
1995 * Walk the returned list, wiring the pages.
1998 for (m
= pages
; m
!= VM_PAGE_NULL
; m
= NEXT_PAGE(m
)) {
2000 * Essentially inlined vm_page_wire.
2003 assert(!m
->inactive
);
2004 assert(!m
->private);
2005 assert(!m
->fictitious
);
2006 assert(m
->wire_count
== 0);
2010 --vm_page_gobble_count
;
2012 vm_page_unlock_queues();
2015 * The CPM pages should now be available and
2016 * ordered by ascending physical address.
2018 assert(vm_page_verify_contiguous(pages
, npages
));
2021 return KERN_SUCCESS
;
2025 #include <mach_vm_debug.h>
2028 #include <mach_debug/hash_info.h>
2029 #include <vm/vm_debug.h>
2032 * Routine: vm_page_info
2034 * Return information about the global VP table.
2035 * Fills the buffer with as much information as possible
2036 * and returns the desired size of the buffer.
2038 * Nothing locked. The caller should provide
2039 * possibly-pageable memory.
2044 hash_info_bucket_t
*info
,
2049 if (vm_page_bucket_count
< count
)
2050 count
= vm_page_bucket_count
;
2052 for (i
= 0; i
< count
; i
++) {
2053 vm_page_bucket_t
*bucket
= &vm_page_buckets
[i
];
2054 unsigned int bucket_count
= 0;
2057 simple_lock(&vm_page_bucket_lock
);
2058 for (m
= bucket
->pages
; m
!= VM_PAGE_NULL
; m
= m
->next
)
2060 simple_unlock(&vm_page_bucket_lock
);
2062 /* don't touch pageable memory while holding locks */
2063 info
[i
].hib_count
= bucket_count
;
2066 return vm_page_bucket_count
;
2068 #endif /* MACH_VM_DEBUG */
2070 #include <mach_kdb.h>
2073 #include <ddb/db_output.h>
2074 #include <vm/vm_print.h>
2075 #define printf kdbprintf
2078 * Routine: vm_page_print [exported]
2086 iprintf("page 0x%x\n", p
);
2090 iprintf("object=0x%x", p
->object
);
2091 printf(", offset=0x%x", p
->offset
);
2092 printf(", wire_count=%d", p
->wire_count
);
2094 iprintf("%sinactive, %sactive, %sgobbled, %slaundry, %sfree, %sref, %sdiscard\n",
2095 (p
->inactive
? "" : "!"),
2096 (p
->active
? "" : "!"),
2097 (p
->gobbled
? "" : "!"),
2098 (p
->laundry
? "" : "!"),
2099 (p
->free
? "" : "!"),
2100 (p
->reference
? "" : "!"),
2101 (p
->discard_request
? "" : "!"));
2102 iprintf("%sbusy, %swanted, %stabled, %sfictitious, %sprivate, %sprecious\n",
2103 (p
->busy
? "" : "!"),
2104 (p
->wanted
? "" : "!"),
2105 (p
->tabled
? "" : "!"),
2106 (p
->fictitious
? "" : "!"),
2107 (p
->private ? "" : "!"),
2108 (p
->precious
? "" : "!"));
2109 iprintf("%sabsent, %serror, %sdirty, %scleaning, %spageout, %sclustered\n",
2110 (p
->absent
? "" : "!"),
2111 (p
->error
? "" : "!"),
2112 (p
->dirty
? "" : "!"),
2113 (p
->cleaning
? "" : "!"),
2114 (p
->pageout
? "" : "!"),
2115 (p
->clustered
? "" : "!"));
2116 iprintf("%slock_supplied, %soverwriting, %srestart, %sunusual\n",
2117 (p
->lock_supplied
? "" : "!"),
2118 (p
->overwriting
? "" : "!"),
2119 (p
->restart
? "" : "!"),
2120 (p
->unusual
? "" : "!"));
2122 iprintf("phys_addr=0x%x", p
->phys_addr
);
2123 printf(", page_error=0x%x", p
->page_error
);
2124 printf(", page_lock=0x%x", p
->page_lock
);
2125 printf(", unlock_request=%d\n", p
->unlock_request
);
2129 #endif /* MACH_KDB */