]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_page.c
MFC r326055:
[FreeBSD/FreeBSD.git] / sys / vm / vm_page.c
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * The Mach Operating System project at Carnegie-Mellon University.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      from: @(#)vm_page.c     7.4 (Berkeley) 5/7/91
34  */
35
36 /*-
37  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38  * All rights reserved.
39  *
40  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41  *
42  * Permission to use, copy, modify and distribute this software and
43  * its documentation is hereby granted, provided that both the copyright
44  * notice and this permission notice appear in all copies of the
45  * software, derivative works or modified versions, and any portions
46  * thereof, and that both notices appear in supporting documentation.
47  *
48  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51  *
52  * Carnegie Mellon requests users of this software to return to
53  *
54  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55  *  School of Computer Science
56  *  Carnegie Mellon University
57  *  Pittsburgh PA 15213-3890
58  *
59  * any improvements or extensions that they make and grant Carnegie the
60  * rights to redistribute these changes.
61  */
62
63 /*
64  *                      GENERAL RULES ON VM_PAGE MANIPULATION
65  *
66  *      - A page queue lock is required when adding or removing a page from a
67  *        page queue regardless of other locks or the busy state of a page.
68  *
69  *              * In general, no thread besides the page daemon can acquire or
70  *                hold more than one page queue lock at a time.
71  *
72  *              * The page daemon can acquire and hold any pair of page queue
73  *                locks in any order.
74  *
75  *      - The object lock is required when inserting or removing
76  *        pages from an object (vm_page_insert() or vm_page_remove()).
77  *
78  */
79
80 /*
81  *      Resident memory management module.
82  */
83
84 #include <sys/cdefs.h>
85 __FBSDID("$FreeBSD$");
86
87 #include "opt_vm.h"
88
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/lock.h>
92 #include <sys/kernel.h>
93 #include <sys/limits.h>
94 #include <sys/linker.h>
95 #include <sys/malloc.h>
96 #include <sys/mman.h>
97 #include <sys/msgbuf.h>
98 #include <sys/mutex.h>
99 #include <sys/proc.h>
100 #include <sys/rwlock.h>
101 #include <sys/sbuf.h>
102 #include <sys/smp.h>
103 #include <sys/sysctl.h>
104 #include <sys/vmmeter.h>
105 #include <sys/vnode.h>
106
107 #include <vm/vm.h>
108 #include <vm/pmap.h>
109 #include <vm/vm_param.h>
110 #include <vm/vm_kern.h>
111 #include <vm/vm_object.h>
112 #include <vm/vm_page.h>
113 #include <vm/vm_pageout.h>
114 #include <vm/vm_pager.h>
115 #include <vm/vm_phys.h>
116 #include <vm/vm_radix.h>
117 #include <vm/vm_reserv.h>
118 #include <vm/vm_extern.h>
119 #include <vm/uma.h>
120 #include <vm/uma_int.h>
121
122 #include <machine/md_var.h>
123
124 /*
125  *      Associated with page of user-allocatable memory is a
126  *      page structure.
127  */
128
129 struct vm_domain vm_dom[MAXMEMDOM];
130 struct mtx_padalign vm_page_queue_free_mtx;
131
132 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
133
134 vm_page_t vm_page_array;
135 long vm_page_array_size;
136 long first_page;
137 int vm_page_zero_count;
138
139 static int boot_pages = UMA_BOOT_PAGES;
140 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
141     &boot_pages, 0,
142     "number of pages allocated for bootstrapping the VM system");
143
144 static int pa_tryrelock_restart;
145 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
146     &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
147
148 static TAILQ_HEAD(, vm_page) blacklist_head;
149 static int sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS);
150 SYSCTL_PROC(_vm, OID_AUTO, page_blacklist, CTLTYPE_STRING | CTLFLAG_RD |
151     CTLFLAG_MPSAFE, NULL, 0, sysctl_vm_page_blacklist, "A", "Blacklist pages");
152
153 /* Is the page daemon waiting for free pages? */
154 static int vm_pageout_pages_needed;
155
156 static uma_zone_t fakepg_zone;
157
158 static void vm_page_alloc_check(vm_page_t m);
159 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
160 static void vm_page_enqueue(uint8_t queue, vm_page_t m);
161 static void vm_page_free_phys(vm_page_t m);
162 static void vm_page_free_wakeup(void);
163 static void vm_page_init_fakepg(void *dummy);
164 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
165     vm_pindex_t pindex, vm_page_t mpred);
166 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
167     vm_page_t mpred);
168 static int vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
169     vm_paddr_t high);
170
171 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
172
173 static void
174 vm_page_init_fakepg(void *dummy)
175 {
176
177         fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
178             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
179 }
180
181 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
182 #if PAGE_SIZE == 32768
183 #ifdef CTASSERT
184 CTASSERT(sizeof(u_long) >= 8);
185 #endif
186 #endif
187
188 /*
189  * Try to acquire a physical address lock while a pmap is locked.  If we
190  * fail to trylock we unlock and lock the pmap directly and cache the
191  * locked pa in *locked.  The caller should then restart their loop in case
192  * the virtual to physical mapping has changed.
193  */
194 int
195 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
196 {
197         vm_paddr_t lockpa;
198
199         lockpa = *locked;
200         *locked = pa;
201         if (lockpa) {
202                 PA_LOCK_ASSERT(lockpa, MA_OWNED);
203                 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
204                         return (0);
205                 PA_UNLOCK(lockpa);
206         }
207         if (PA_TRYLOCK(pa))
208                 return (0);
209         PMAP_UNLOCK(pmap);
210         atomic_add_int(&pa_tryrelock_restart, 1);
211         PA_LOCK(pa);
212         PMAP_LOCK(pmap);
213         return (EAGAIN);
214 }
215
216 /*
217  *      vm_set_page_size:
218  *
219  *      Sets the page size, perhaps based upon the memory
220  *      size.  Must be called before any use of page-size
221  *      dependent functions.
222  */
223 void
224 vm_set_page_size(void)
225 {
226         if (vm_cnt.v_page_size == 0)
227                 vm_cnt.v_page_size = PAGE_SIZE;
228         if (((vm_cnt.v_page_size - 1) & vm_cnt.v_page_size) != 0)
229                 panic("vm_set_page_size: page size not a power of two");
230 }
231
232 /*
233  *      vm_page_blacklist_next:
234  *
235  *      Find the next entry in the provided string of blacklist
236  *      addresses.  Entries are separated by space, comma, or newline.
237  *      If an invalid integer is encountered then the rest of the
238  *      string is skipped.  Updates the list pointer to the next
239  *      character, or NULL if the string is exhausted or invalid.
240  */
241 static vm_paddr_t
242 vm_page_blacklist_next(char **list, char *end)
243 {
244         vm_paddr_t bad;
245         char *cp, *pos;
246
247         if (list == NULL || *list == NULL)
248                 return (0);
249         if (**list =='\0') {
250                 *list = NULL;
251                 return (0);
252         }
253
254         /*
255          * If there's no end pointer then the buffer is coming from
256          * the kenv and we know it's null-terminated.
257          */
258         if (end == NULL)
259                 end = *list + strlen(*list);
260
261         /* Ensure that strtoq() won't walk off the end */
262         if (*end != '\0') {
263                 if (*end == '\n' || *end == ' ' || *end  == ',')
264                         *end = '\0';
265                 else {
266                         printf("Blacklist not terminated, skipping\n");
267                         *list = NULL;
268                         return (0);
269                 }
270         }
271
272         for (pos = *list; *pos != '\0'; pos = cp) {
273                 bad = strtoq(pos, &cp, 0);
274                 if (*cp == '\0' || *cp == ' ' || *cp == ',' || *cp == '\n') {
275                         if (bad == 0) {
276                                 if (++cp < end)
277                                         continue;
278                                 else
279                                         break;
280                         }
281                 } else
282                         break;
283                 if (*cp == '\0' || ++cp >= end)
284                         *list = NULL;
285                 else
286                         *list = cp;
287                 return (trunc_page(bad));
288         }
289         printf("Garbage in RAM blacklist, skipping\n");
290         *list = NULL;
291         return (0);
292 }
293
294 /*
295  *      vm_page_blacklist_check:
296  *
297  *      Iterate through the provided string of blacklist addresses, pulling
298  *      each entry out of the physical allocator free list and putting it
299  *      onto a list for reporting via the vm.page_blacklist sysctl.
300  */
301 static void
302 vm_page_blacklist_check(char *list, char *end)
303 {
304         vm_paddr_t pa;
305         vm_page_t m;
306         char *next;
307         int ret;
308
309         next = list;
310         while (next != NULL) {
311                 if ((pa = vm_page_blacklist_next(&next, end)) == 0)
312                         continue;
313                 m = vm_phys_paddr_to_vm_page(pa);
314                 if (m == NULL)
315                         continue;
316                 mtx_lock(&vm_page_queue_free_mtx);
317                 ret = vm_phys_unfree_page(m);
318                 mtx_unlock(&vm_page_queue_free_mtx);
319                 if (ret == TRUE) {
320                         TAILQ_INSERT_TAIL(&blacklist_head, m, listq);
321                         if (bootverbose)
322                                 printf("Skipping page with pa 0x%jx\n",
323                                     (uintmax_t)pa);
324                 }
325         }
326 }
327
328 /*
329  *      vm_page_blacklist_load:
330  *
331  *      Search for a special module named "ram_blacklist".  It'll be a
332  *      plain text file provided by the user via the loader directive
333  *      of the same name.
334  */
335 static void
336 vm_page_blacklist_load(char **list, char **end)
337 {
338         void *mod;
339         u_char *ptr;
340         u_int len;
341
342         mod = NULL;
343         ptr = NULL;
344
345         mod = preload_search_by_type("ram_blacklist");
346         if (mod != NULL) {
347                 ptr = preload_fetch_addr(mod);
348                 len = preload_fetch_size(mod);
349         }
350         *list = ptr;
351         if (ptr != NULL)
352                 *end = ptr + len;
353         else
354                 *end = NULL;
355         return;
356 }
357
358 static int
359 sysctl_vm_page_blacklist(SYSCTL_HANDLER_ARGS)
360 {
361         vm_page_t m;
362         struct sbuf sbuf;
363         int error, first;
364
365         first = 1;
366         error = sysctl_wire_old_buffer(req, 0);
367         if (error != 0)
368                 return (error);
369         sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
370         TAILQ_FOREACH(m, &blacklist_head, listq) {
371                 sbuf_printf(&sbuf, "%s%#jx", first ? "" : ",",
372                     (uintmax_t)m->phys_addr);
373                 first = 0;
374         }
375         error = sbuf_finish(&sbuf);
376         sbuf_delete(&sbuf);
377         return (error);
378 }
379
380 static void
381 vm_page_domain_init(struct vm_domain *vmd)
382 {
383         struct vm_pagequeue *pq;
384         int i;
385
386         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
387             "vm inactive pagequeue";
388         *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) =
389             &vm_cnt.v_inactive_count;
390         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
391             "vm active pagequeue";
392         *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) =
393             &vm_cnt.v_active_count;
394         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_name) =
395             "vm laundry pagequeue";
396         *__DECONST(int **, &vmd->vmd_pagequeues[PQ_LAUNDRY].pq_vcnt) =
397             &vm_cnt.v_laundry_count;
398         vmd->vmd_page_count = 0;
399         vmd->vmd_free_count = 0;
400         vmd->vmd_segs = 0;
401         vmd->vmd_oom = FALSE;
402         for (i = 0; i < PQ_COUNT; i++) {
403                 pq = &vmd->vmd_pagequeues[i];
404                 TAILQ_INIT(&pq->pq_pl);
405                 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
406                     MTX_DEF | MTX_DUPOK);
407         }
408 }
409
410 /*
411  *      vm_page_startup:
412  *
413  *      Initializes the resident memory module.  Allocates physical memory for
414  *      bootstrapping UMA and some data structures that are used to manage
415  *      physical pages.  Initializes these structures, and populates the free
416  *      page queues.
417  */
418 vm_offset_t
419 vm_page_startup(vm_offset_t vaddr)
420 {
421         struct vm_domain *vmd;
422         struct vm_phys_seg *seg;
423         vm_page_t m;
424         char *list, *listend;
425         vm_offset_t mapped;
426         vm_paddr_t end, high_avail, low_avail, new_end, page_range, size;
427         vm_paddr_t biggestsize, last_pa, pa;
428         u_long pagecount;
429         int biggestone, i, pages_per_zone, segind;
430
431         biggestsize = 0;
432         biggestone = 0;
433         vaddr = round_page(vaddr);
434
435         for (i = 0; phys_avail[i + 1]; i += 2) {
436                 phys_avail[i] = round_page(phys_avail[i]);
437                 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
438         }
439         for (i = 0; phys_avail[i + 1]; i += 2) {
440                 size = phys_avail[i + 1] - phys_avail[i];
441                 if (size > biggestsize) {
442                         biggestone = i;
443                         biggestsize = size;
444                 }
445         }
446
447         end = phys_avail[biggestone+1];
448
449         /*
450          * Initialize the page and queue locks.
451          */
452         mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
453         for (i = 0; i < PA_LOCK_COUNT; i++)
454                 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
455         for (i = 0; i < vm_ndomains; i++)
456                 vm_page_domain_init(&vm_dom[i]);
457
458         /*
459          * Almost all of the pages needed for bootstrapping UMA are used
460          * for zone structures, so if the number of CPUs results in those
461          * structures taking more than one page each, we set aside more pages
462          * in proportion to the zone structure size.
463          */
464         pages_per_zone = howmany(sizeof(struct uma_zone) +
465             sizeof(struct uma_cache) * (mp_maxid + 1) +
466             roundup2(sizeof(struct uma_slab), sizeof(void *)), UMA_SLAB_SIZE);
467         if (pages_per_zone > 1) {
468                 /* Reserve more pages so that we don't run out. */
469                 boot_pages = UMA_BOOT_PAGES_ZONES * pages_per_zone;
470         }
471
472         /*
473          * Allocate memory for use when boot strapping the kernel memory
474          * allocator.
475          *
476          * CTFLAG_RDTUN doesn't work during the early boot process, so we must
477          * manually fetch the value.
478          */
479         TUNABLE_INT_FETCH("vm.boot_pages", &boot_pages);
480         new_end = end - (boot_pages * UMA_SLAB_SIZE);
481         new_end = trunc_page(new_end);
482         mapped = pmap_map(&vaddr, new_end, end,
483             VM_PROT_READ | VM_PROT_WRITE);
484         bzero((void *)mapped, end - new_end);
485         uma_startup((void *)mapped, boot_pages);
486
487 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
488     defined(__i386__) || defined(__mips__)
489         /*
490          * Allocate a bitmap to indicate that a random physical page
491          * needs to be included in a minidump.
492          *
493          * The amd64 port needs this to indicate which direct map pages
494          * need to be dumped, via calls to dump_add_page()/dump_drop_page().
495          *
496          * However, i386 still needs this workspace internally within the
497          * minidump code.  In theory, they are not needed on i386, but are
498          * included should the sf_buf code decide to use them.
499          */
500         last_pa = 0;
501         for (i = 0; dump_avail[i + 1] != 0; i += 2)
502                 if (dump_avail[i + 1] > last_pa)
503                         last_pa = dump_avail[i + 1];
504         page_range = last_pa / PAGE_SIZE;
505         vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
506         new_end -= vm_page_dump_size;
507         vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
508             new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
509         bzero((void *)vm_page_dump, vm_page_dump_size);
510 #else
511         (void)last_pa;
512 #endif
513 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
514         /*
515          * Include the UMA bootstrap pages and vm_page_dump in a crash dump.
516          * When pmap_map() uses the direct map, they are not automatically 
517          * included.
518          */
519         for (pa = new_end; pa < end; pa += PAGE_SIZE)
520                 dump_add_page(pa);
521 #endif
522         phys_avail[biggestone + 1] = new_end;
523 #ifdef __amd64__
524         /*
525          * Request that the physical pages underlying the message buffer be
526          * included in a crash dump.  Since the message buffer is accessed
527          * through the direct map, they are not automatically included.
528          */
529         pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
530         last_pa = pa + round_page(msgbufsize);
531         while (pa < last_pa) {
532                 dump_add_page(pa);
533                 pa += PAGE_SIZE;
534         }
535 #endif
536         /*
537          * Compute the number of pages of memory that will be available for
538          * use, taking into account the overhead of a page structure per page.
539          * In other words, solve
540          *      "available physical memory" - round_page(page_range *
541          *          sizeof(struct vm_page)) = page_range * PAGE_SIZE 
542          * for page_range.  
543          */
544         low_avail = phys_avail[0];
545         high_avail = phys_avail[1];
546         for (i = 0; i < vm_phys_nsegs; i++) {
547                 if (vm_phys_segs[i].start < low_avail)
548                         low_avail = vm_phys_segs[i].start;
549                 if (vm_phys_segs[i].end > high_avail)
550                         high_avail = vm_phys_segs[i].end;
551         }
552         /* Skip the first chunk.  It is already accounted for. */
553         for (i = 2; phys_avail[i + 1] != 0; i += 2) {
554                 if (phys_avail[i] < low_avail)
555                         low_avail = phys_avail[i];
556                 if (phys_avail[i + 1] > high_avail)
557                         high_avail = phys_avail[i + 1];
558         }
559         first_page = low_avail / PAGE_SIZE;
560 #ifdef VM_PHYSSEG_SPARSE
561         size = 0;
562         for (i = 0; i < vm_phys_nsegs; i++)
563                 size += vm_phys_segs[i].end - vm_phys_segs[i].start;
564         for (i = 0; phys_avail[i + 1] != 0; i += 2)
565                 size += phys_avail[i + 1] - phys_avail[i];
566 #elif defined(VM_PHYSSEG_DENSE)
567         size = high_avail - low_avail;
568 #else
569 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
570 #endif
571
572 #ifdef VM_PHYSSEG_DENSE
573         /*
574          * In the VM_PHYSSEG_DENSE case, the number of pages can account for
575          * the overhead of a page structure per page only if vm_page_array is
576          * allocated from the last physical memory chunk.  Otherwise, we must
577          * allocate page structures representing the physical memory
578          * underlying vm_page_array, even though they will not be used.
579          */
580         if (new_end != high_avail)
581                 page_range = size / PAGE_SIZE;
582         else
583 #endif
584         {
585                 page_range = size / (PAGE_SIZE + sizeof(struct vm_page));
586
587                 /*
588                  * If the partial bytes remaining are large enough for
589                  * a page (PAGE_SIZE) without a corresponding
590                  * 'struct vm_page', then new_end will contain an
591                  * extra page after subtracting the length of the VM
592                  * page array.  Compensate by subtracting an extra
593                  * page from new_end.
594                  */
595                 if (size % (PAGE_SIZE + sizeof(struct vm_page)) >= PAGE_SIZE) {
596                         if (new_end == high_avail)
597                                 high_avail -= PAGE_SIZE;
598                         new_end -= PAGE_SIZE;
599                 }
600         }
601         end = new_end;
602
603         /*
604          * Reserve an unmapped guard page to trap access to vm_page_array[-1].
605          * However, because this page is allocated from KVM, out-of-bounds
606          * accesses using the direct map will not be trapped.
607          */
608         vaddr += PAGE_SIZE;
609
610         /*
611          * Allocate physical memory for the page structures, and map it.
612          */
613         new_end = trunc_page(end - page_range * sizeof(struct vm_page));
614         mapped = pmap_map(&vaddr, new_end, end,
615             VM_PROT_READ | VM_PROT_WRITE);
616         vm_page_array = (vm_page_t)mapped;
617         vm_page_array_size = page_range;
618
619 #if VM_NRESERVLEVEL > 0
620         /*
621          * Allocate physical memory for the reservation management system's
622          * data structures, and map it.
623          */
624         if (high_avail == end)
625                 high_avail = new_end;
626         new_end = vm_reserv_startup(&vaddr, new_end, high_avail);
627 #endif
628 #if defined(__aarch64__) || defined(__amd64__) || defined(__mips__)
629         /*
630          * Include vm_page_array and vm_reserv_array in a crash dump.
631          */
632         for (pa = new_end; pa < end; pa += PAGE_SIZE)
633                 dump_add_page(pa);
634 #endif
635         phys_avail[biggestone + 1] = new_end;
636
637         /*
638          * Add physical memory segments corresponding to the available
639          * physical pages.
640          */
641         for (i = 0; phys_avail[i + 1] != 0; i += 2)
642                 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
643
644         /*
645          * Initialize the physical memory allocator.
646          */
647         vm_phys_init();
648
649         /*
650          * Initialize the page structures and add every available page to the
651          * physical memory allocator's free lists.
652          */
653         vm_cnt.v_page_count = 0;
654         vm_cnt.v_free_count = 0;
655         for (segind = 0; segind < vm_phys_nsegs; segind++) {
656                 seg = &vm_phys_segs[segind];
657                 for (pa = seg->start; pa < seg->end; pa += PAGE_SIZE)
658                         vm_phys_init_page(pa);
659
660                 /*
661                  * Add the segment to the free lists only if it is covered by
662                  * one of the ranges in phys_avail.  Because we've added the
663                  * ranges to the vm_phys_segs array, we can assume that each
664                  * segment is either entirely contained in one of the ranges,
665                  * or doesn't overlap any of them.
666                  */
667                 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
668                         if (seg->start < phys_avail[i] ||
669                             seg->end > phys_avail[i + 1])
670                                 continue;
671
672                         m = seg->first_page;
673                         pagecount = (u_long)atop(seg->end - seg->start);
674
675                         mtx_lock(&vm_page_queue_free_mtx);
676                         vm_phys_free_contig(m, pagecount);
677                         vm_phys_freecnt_adj(m, (int)pagecount);
678                         mtx_unlock(&vm_page_queue_free_mtx);
679                         vm_cnt.v_page_count += (u_int)pagecount;
680
681                         vmd = &vm_dom[seg->domain];
682                         vmd->vmd_page_count += (u_int)pagecount;
683                         vmd->vmd_segs |= 1UL << m->segind;
684                         break;
685                 }
686         }
687
688         /*
689          * Remove blacklisted pages from the physical memory allocator.
690          */
691         TAILQ_INIT(&blacklist_head);
692         vm_page_blacklist_load(&list, &listend);
693         vm_page_blacklist_check(list, listend);
694
695         list = kern_getenv("vm.blacklist");
696         vm_page_blacklist_check(list, NULL);
697
698         freeenv(list);
699 #if VM_NRESERVLEVEL > 0
700         /*
701          * Initialize the reservation management system.
702          */
703         vm_reserv_init();
704 #endif
705         return (vaddr);
706 }
707
708 void
709 vm_page_reference(vm_page_t m)
710 {
711
712         vm_page_aflag_set(m, PGA_REFERENCED);
713 }
714
715 /*
716  *      vm_page_busy_downgrade:
717  *
718  *      Downgrade an exclusive busy page into a single shared busy page.
719  */
720 void
721 vm_page_busy_downgrade(vm_page_t m)
722 {
723         u_int x;
724         bool locked;
725
726         vm_page_assert_xbusied(m);
727         locked = mtx_owned(vm_page_lockptr(m));
728
729         for (;;) {
730                 x = m->busy_lock;
731                 x &= VPB_BIT_WAITERS;
732                 if (x != 0 && !locked)
733                         vm_page_lock(m);
734                 if (atomic_cmpset_rel_int(&m->busy_lock,
735                     VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1)))
736                         break;
737                 if (x != 0 && !locked)
738                         vm_page_unlock(m);
739         }
740         if (x != 0) {
741                 wakeup(m);
742                 if (!locked)
743                         vm_page_unlock(m);
744         }
745 }
746
747 /*
748  *      vm_page_sbusied:
749  *
750  *      Return a positive value if the page is shared busied, 0 otherwise.
751  */
752 int
753 vm_page_sbusied(vm_page_t m)
754 {
755         u_int x;
756
757         x = m->busy_lock;
758         return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
759 }
760
761 /*
762  *      vm_page_sunbusy:
763  *
764  *      Shared unbusy a page.
765  */
766 void
767 vm_page_sunbusy(vm_page_t m)
768 {
769         u_int x;
770
771         vm_page_lock_assert(m, MA_NOTOWNED);
772         vm_page_assert_sbusied(m);
773
774         for (;;) {
775                 x = m->busy_lock;
776                 if (VPB_SHARERS(x) > 1) {
777                         if (atomic_cmpset_int(&m->busy_lock, x,
778                             x - VPB_ONE_SHARER))
779                                 break;
780                         continue;
781                 }
782                 if ((x & VPB_BIT_WAITERS) == 0) {
783                         KASSERT(x == VPB_SHARERS_WORD(1),
784                             ("vm_page_sunbusy: invalid lock state"));
785                         if (atomic_cmpset_int(&m->busy_lock,
786                             VPB_SHARERS_WORD(1), VPB_UNBUSIED))
787                                 break;
788                         continue;
789                 }
790                 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS),
791                     ("vm_page_sunbusy: invalid lock state for waiters"));
792
793                 vm_page_lock(m);
794                 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) {
795                         vm_page_unlock(m);
796                         continue;
797                 }
798                 wakeup(m);
799                 vm_page_unlock(m);
800                 break;
801         }
802 }
803
804 /*
805  *      vm_page_busy_sleep:
806  *
807  *      Sleep and release the page lock, using the page pointer as wchan.
808  *      This is used to implement the hard-path of busying mechanism.
809  *
810  *      The given page must be locked.
811  *
812  *      If nonshared is true, sleep only if the page is xbusy.
813  */
814 void
815 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared)
816 {
817         u_int x;
818
819         vm_page_assert_locked(m);
820
821         x = m->busy_lock;
822         if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) ||
823             ((x & VPB_BIT_WAITERS) == 0 &&
824             !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) {
825                 vm_page_unlock(m);
826                 return;
827         }
828         msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0);
829 }
830
831 /*
832  *      vm_page_trysbusy:
833  *
834  *      Try to shared busy a page.
835  *      If the operation succeeds 1 is returned otherwise 0.
836  *      The operation never sleeps.
837  */
838 int
839 vm_page_trysbusy(vm_page_t m)
840 {
841         u_int x;
842
843         for (;;) {
844                 x = m->busy_lock;
845                 if ((x & VPB_BIT_SHARED) == 0)
846                         return (0);
847                 if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER))
848                         return (1);
849         }
850 }
851
852 static void
853 vm_page_xunbusy_locked(vm_page_t m)
854 {
855
856         vm_page_assert_xbusied(m);
857         vm_page_assert_locked(m);
858
859         atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
860         /* There is a waiter, do wakeup() instead of vm_page_flash(). */
861         wakeup(m);
862 }
863
864 void
865 vm_page_xunbusy_maybelocked(vm_page_t m)
866 {
867         bool lockacq;
868
869         vm_page_assert_xbusied(m);
870
871         /*
872          * Fast path for unbusy.  If it succeeds, we know that there
873          * are no waiters, so we do not need a wakeup.
874          */
875         if (atomic_cmpset_rel_int(&m->busy_lock, VPB_SINGLE_EXCLUSIVER,
876             VPB_UNBUSIED))
877                 return;
878
879         lockacq = !mtx_owned(vm_page_lockptr(m));
880         if (lockacq)
881                 vm_page_lock(m);
882         vm_page_xunbusy_locked(m);
883         if (lockacq)
884                 vm_page_unlock(m);
885 }
886
887 /*
888  *      vm_page_xunbusy_hard:
889  *
890  *      Called after the first try the exclusive unbusy of a page failed.
891  *      It is assumed that the waiters bit is on.
892  */
893 void
894 vm_page_xunbusy_hard(vm_page_t m)
895 {
896
897         vm_page_assert_xbusied(m);
898
899         vm_page_lock(m);
900         vm_page_xunbusy_locked(m);
901         vm_page_unlock(m);
902 }
903
904 /*
905  *      vm_page_flash:
906  *
907  *      Wakeup anyone waiting for the page.
908  *      The ownership bits do not change.
909  *
910  *      The given page must be locked.
911  */
912 void
913 vm_page_flash(vm_page_t m)
914 {
915         u_int x;
916
917         vm_page_lock_assert(m, MA_OWNED);
918
919         for (;;) {
920                 x = m->busy_lock;
921                 if ((x & VPB_BIT_WAITERS) == 0)
922                         return;
923                 if (atomic_cmpset_int(&m->busy_lock, x,
924                     x & (~VPB_BIT_WAITERS)))
925                         break;
926         }
927         wakeup(m);
928 }
929
930 /*
931  * Avoid releasing and reacquiring the same page lock.
932  */
933 void
934 vm_page_change_lock(vm_page_t m, struct mtx **mtx)
935 {
936         struct mtx *mtx1;
937
938         mtx1 = vm_page_lockptr(m);
939         if (*mtx == mtx1)
940                 return;
941         if (*mtx != NULL)
942                 mtx_unlock(*mtx);
943         *mtx = mtx1;
944         mtx_lock(mtx1);
945 }
946
947 /*
948  * Keep page from being freed by the page daemon
949  * much of the same effect as wiring, except much lower
950  * overhead and should be used only for *very* temporary
951  * holding ("wiring").
952  */
953 void
954 vm_page_hold(vm_page_t mem)
955 {
956
957         vm_page_lock_assert(mem, MA_OWNED);
958         mem->hold_count++;
959 }
960
961 void
962 vm_page_unhold(vm_page_t mem)
963 {
964
965         vm_page_lock_assert(mem, MA_OWNED);
966         KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!"));
967         --mem->hold_count;
968         if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
969                 vm_page_free_toq(mem);
970 }
971
972 /*
973  *      vm_page_unhold_pages:
974  *
975  *      Unhold each of the pages that is referenced by the given array.
976  */
977 void
978 vm_page_unhold_pages(vm_page_t *ma, int count)
979 {
980         struct mtx *mtx;
981
982         mtx = NULL;
983         for (; count != 0; count--) {
984                 vm_page_change_lock(*ma, &mtx);
985                 vm_page_unhold(*ma);
986                 ma++;
987         }
988         if (mtx != NULL)
989                 mtx_unlock(mtx);
990 }
991
992 vm_page_t
993 PHYS_TO_VM_PAGE(vm_paddr_t pa)
994 {
995         vm_page_t m;
996
997 #ifdef VM_PHYSSEG_SPARSE
998         m = vm_phys_paddr_to_vm_page(pa);
999         if (m == NULL)
1000                 m = vm_phys_fictitious_to_vm_page(pa);
1001         return (m);
1002 #elif defined(VM_PHYSSEG_DENSE)
1003         long pi;
1004
1005         pi = atop(pa);
1006         if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
1007                 m = &vm_page_array[pi - first_page];
1008                 return (m);
1009         }
1010         return (vm_phys_fictitious_to_vm_page(pa));
1011 #else
1012 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
1013 #endif
1014 }
1015
1016 /*
1017  *      vm_page_getfake:
1018  *
1019  *      Create a fictitious page with the specified physical address and
1020  *      memory attribute.  The memory attribute is the only the machine-
1021  *      dependent aspect of a fictitious page that must be initialized.
1022  */
1023 vm_page_t
1024 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
1025 {
1026         vm_page_t m;
1027
1028         m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
1029         vm_page_initfake(m, paddr, memattr);
1030         return (m);
1031 }
1032
1033 void
1034 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1035 {
1036
1037         if ((m->flags & PG_FICTITIOUS) != 0) {
1038                 /*
1039                  * The page's memattr might have changed since the
1040                  * previous initialization.  Update the pmap to the
1041                  * new memattr.
1042                  */
1043                 goto memattr;
1044         }
1045         m->phys_addr = paddr;
1046         m->queue = PQ_NONE;
1047         /* Fictitious pages don't use "segind". */
1048         m->flags = PG_FICTITIOUS;
1049         /* Fictitious pages don't use "order" or "pool". */
1050         m->oflags = VPO_UNMANAGED;
1051         m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1052         m->wire_count = 1;
1053         pmap_page_init(m);
1054 memattr:
1055         pmap_page_set_memattr(m, memattr);
1056 }
1057
1058 /*
1059  *      vm_page_putfake:
1060  *
1061  *      Release a fictitious page.
1062  */
1063 void
1064 vm_page_putfake(vm_page_t m)
1065 {
1066
1067         KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
1068         KASSERT((m->flags & PG_FICTITIOUS) != 0,
1069             ("vm_page_putfake: bad page %p", m));
1070         uma_zfree(fakepg_zone, m);
1071 }
1072
1073 /*
1074  *      vm_page_updatefake:
1075  *
1076  *      Update the given fictitious page to the specified physical address and
1077  *      memory attribute.
1078  */
1079 void
1080 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
1081 {
1082
1083         KASSERT((m->flags & PG_FICTITIOUS) != 0,
1084             ("vm_page_updatefake: bad page %p", m));
1085         m->phys_addr = paddr;
1086         pmap_page_set_memattr(m, memattr);
1087 }
1088
1089 /*
1090  *      vm_page_free:
1091  *
1092  *      Free a page.
1093  */
1094 void
1095 vm_page_free(vm_page_t m)
1096 {
1097
1098         m->flags &= ~PG_ZERO;
1099         vm_page_free_toq(m);
1100 }
1101
1102 /*
1103  *      vm_page_free_zero:
1104  *
1105  *      Free a page to the zerod-pages queue
1106  */
1107 void
1108 vm_page_free_zero(vm_page_t m)
1109 {
1110
1111         m->flags |= PG_ZERO;
1112         vm_page_free_toq(m);
1113 }
1114
1115 /*
1116  * Unbusy and handle the page queueing for a page from a getpages request that
1117  * was optionally read ahead or behind.
1118  */
1119 void
1120 vm_page_readahead_finish(vm_page_t m)
1121 {
1122
1123         /* We shouldn't put invalid pages on queues. */
1124         KASSERT(m->valid != 0, ("%s: %p is invalid", __func__, m));
1125
1126         /*
1127          * Since the page is not the actually needed one, whether it should
1128          * be activated or deactivated is not obvious.  Empirical results
1129          * have shown that deactivating the page is usually the best choice,
1130          * unless the page is wanted by another thread.
1131          */
1132         vm_page_lock(m);
1133         if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
1134                 vm_page_activate(m);
1135         else
1136                 vm_page_deactivate(m);
1137         vm_page_unlock(m);
1138         vm_page_xunbusy(m);
1139 }
1140
1141 /*
1142  *      vm_page_sleep_if_busy:
1143  *
1144  *      Sleep and release the page queues lock if the page is busied.
1145  *      Returns TRUE if the thread slept.
1146  *
1147  *      The given page must be unlocked and object containing it must
1148  *      be locked.
1149  */
1150 int
1151 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
1152 {
1153         vm_object_t obj;
1154
1155         vm_page_lock_assert(m, MA_NOTOWNED);
1156         VM_OBJECT_ASSERT_WLOCKED(m->object);
1157
1158         if (vm_page_busied(m)) {
1159                 /*
1160                  * The page-specific object must be cached because page
1161                  * identity can change during the sleep, causing the
1162                  * re-lock of a different object.
1163                  * It is assumed that a reference to the object is already
1164                  * held by the callers.
1165                  */
1166                 obj = m->object;
1167                 vm_page_lock(m);
1168                 VM_OBJECT_WUNLOCK(obj);
1169                 vm_page_busy_sleep(m, msg, false);
1170                 VM_OBJECT_WLOCK(obj);
1171                 return (TRUE);
1172         }
1173         return (FALSE);
1174 }
1175
1176 /*
1177  *      vm_page_dirty_KBI:              [ internal use only ]
1178  *
1179  *      Set all bits in the page's dirty field.
1180  *
1181  *      The object containing the specified page must be locked if the
1182  *      call is made from the machine-independent layer.
1183  *
1184  *      See vm_page_clear_dirty_mask().
1185  *
1186  *      This function should only be called by vm_page_dirty().
1187  */
1188 void
1189 vm_page_dirty_KBI(vm_page_t m)
1190 {
1191
1192         /* Refer to this operation by its public name. */
1193         KASSERT(m->valid == VM_PAGE_BITS_ALL,
1194             ("vm_page_dirty: page is invalid!"));
1195         m->dirty = VM_PAGE_BITS_ALL;
1196 }
1197
1198 /*
1199  *      vm_page_insert:         [ internal use only ]
1200  *
1201  *      Inserts the given mem entry into the object and object list.
1202  *
1203  *      The object must be locked.
1204  */
1205 int
1206 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
1207 {
1208         vm_page_t mpred;
1209
1210         VM_OBJECT_ASSERT_WLOCKED(object);
1211         mpred = vm_radix_lookup_le(&object->rtree, pindex);
1212         return (vm_page_insert_after(m, object, pindex, mpred));
1213 }
1214
1215 /*
1216  *      vm_page_insert_after:
1217  *
1218  *      Inserts the page "m" into the specified object at offset "pindex".
1219  *
1220  *      The page "mpred" must immediately precede the offset "pindex" within
1221  *      the specified object.
1222  *
1223  *      The object must be locked.
1224  */
1225 static int
1226 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
1227     vm_page_t mpred)
1228 {
1229         vm_page_t msucc;
1230
1231         VM_OBJECT_ASSERT_WLOCKED(object);
1232         KASSERT(m->object == NULL,
1233             ("vm_page_insert_after: page already inserted"));
1234         if (mpred != NULL) {
1235                 KASSERT(mpred->object == object,
1236                     ("vm_page_insert_after: object doesn't contain mpred"));
1237                 KASSERT(mpred->pindex < pindex,
1238                     ("vm_page_insert_after: mpred doesn't precede pindex"));
1239                 msucc = TAILQ_NEXT(mpred, listq);
1240         } else
1241                 msucc = TAILQ_FIRST(&object->memq);
1242         if (msucc != NULL)
1243                 KASSERT(msucc->pindex > pindex,
1244                     ("vm_page_insert_after: msucc doesn't succeed pindex"));
1245
1246         /*
1247          * Record the object/offset pair in this page
1248          */
1249         m->object = object;
1250         m->pindex = pindex;
1251
1252         /*
1253          * Now link into the object's ordered list of backed pages.
1254          */
1255         if (vm_radix_insert(&object->rtree, m)) {
1256                 m->object = NULL;
1257                 m->pindex = 0;
1258                 return (1);
1259         }
1260         vm_page_insert_radixdone(m, object, mpred);
1261         return (0);
1262 }
1263
1264 /*
1265  *      vm_page_insert_radixdone:
1266  *
1267  *      Complete page "m" insertion into the specified object after the
1268  *      radix trie hooking.
1269  *
1270  *      The page "mpred" must precede the offset "m->pindex" within the
1271  *      specified object.
1272  *
1273  *      The object must be locked.
1274  */
1275 static void
1276 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1277 {
1278
1279         VM_OBJECT_ASSERT_WLOCKED(object);
1280         KASSERT(object != NULL && m->object == object,
1281             ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1282         if (mpred != NULL) {
1283                 KASSERT(mpred->object == object,
1284                     ("vm_page_insert_after: object doesn't contain mpred"));
1285                 KASSERT(mpred->pindex < m->pindex,
1286                     ("vm_page_insert_after: mpred doesn't precede pindex"));
1287         }
1288
1289         if (mpred != NULL)
1290                 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1291         else
1292                 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1293
1294         /*
1295          * Show that the object has one more resident page.
1296          */
1297         object->resident_page_count++;
1298
1299         /*
1300          * Hold the vnode until the last page is released.
1301          */
1302         if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1303                 vhold(object->handle);
1304
1305         /*
1306          * Since we are inserting a new and possibly dirty page,
1307          * update the object's OBJ_MIGHTBEDIRTY flag.
1308          */
1309         if (pmap_page_is_write_mapped(m))
1310                 vm_object_set_writeable_dirty(object);
1311 }
1312
1313 /*
1314  *      vm_page_remove:
1315  *
1316  *      Removes the specified page from its containing object, but does not
1317  *      invalidate any backing storage.
1318  *
1319  *      The object must be locked.  The page must be locked if it is managed.
1320  */
1321 void
1322 vm_page_remove(vm_page_t m)
1323 {
1324         vm_object_t object;
1325         vm_page_t mrem;
1326
1327         if ((m->oflags & VPO_UNMANAGED) == 0)
1328                 vm_page_assert_locked(m);
1329         if ((object = m->object) == NULL)
1330                 return;
1331         VM_OBJECT_ASSERT_WLOCKED(object);
1332         if (vm_page_xbusied(m))
1333                 vm_page_xunbusy_maybelocked(m);
1334         mrem = vm_radix_remove(&object->rtree, m->pindex);
1335         KASSERT(mrem == m, ("removed page %p, expected page %p", mrem, m));
1336
1337         /*
1338          * Now remove from the object's list of backed pages.
1339          */
1340         TAILQ_REMOVE(&object->memq, m, listq);
1341
1342         /*
1343          * And show that the object has one fewer resident page.
1344          */
1345         object->resident_page_count--;
1346
1347         /*
1348          * The vnode may now be recycled.
1349          */
1350         if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1351                 vdrop(object->handle);
1352
1353         m->object = NULL;
1354 }
1355
1356 /*
1357  *      vm_page_lookup:
1358  *
1359  *      Returns the page associated with the object/offset
1360  *      pair specified; if none is found, NULL is returned.
1361  *
1362  *      The object must be locked.
1363  */
1364 vm_page_t
1365 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1366 {
1367
1368         VM_OBJECT_ASSERT_LOCKED(object);
1369         return (vm_radix_lookup(&object->rtree, pindex));
1370 }
1371
1372 /*
1373  *      vm_page_find_least:
1374  *
1375  *      Returns the page associated with the object with least pindex
1376  *      greater than or equal to the parameter pindex, or NULL.
1377  *
1378  *      The object must be locked.
1379  */
1380 vm_page_t
1381 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1382 {
1383         vm_page_t m;
1384
1385         VM_OBJECT_ASSERT_LOCKED(object);
1386         if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1387                 m = vm_radix_lookup_ge(&object->rtree, pindex);
1388         return (m);
1389 }
1390
1391 /*
1392  * Returns the given page's successor (by pindex) within the object if it is
1393  * resident; if none is found, NULL is returned.
1394  *
1395  * The object must be locked.
1396  */
1397 vm_page_t
1398 vm_page_next(vm_page_t m)
1399 {
1400         vm_page_t next;
1401
1402         VM_OBJECT_ASSERT_LOCKED(m->object);
1403         if ((next = TAILQ_NEXT(m, listq)) != NULL) {
1404                 MPASS(next->object == m->object);
1405                 if (next->pindex != m->pindex + 1)
1406                         next = NULL;
1407         }
1408         return (next);
1409 }
1410
1411 /*
1412  * Returns the given page's predecessor (by pindex) within the object if it is
1413  * resident; if none is found, NULL is returned.
1414  *
1415  * The object must be locked.
1416  */
1417 vm_page_t
1418 vm_page_prev(vm_page_t m)
1419 {
1420         vm_page_t prev;
1421
1422         VM_OBJECT_ASSERT_LOCKED(m->object);
1423         if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL) {
1424                 MPASS(prev->object == m->object);
1425                 if (prev->pindex != m->pindex - 1)
1426                         prev = NULL;
1427         }
1428         return (prev);
1429 }
1430
1431 /*
1432  * Uses the page mnew as a replacement for an existing page at index
1433  * pindex which must be already present in the object.
1434  *
1435  * The existing page must not be on a paging queue.
1436  */
1437 vm_page_t
1438 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
1439 {
1440         vm_page_t mold;
1441
1442         VM_OBJECT_ASSERT_WLOCKED(object);
1443         KASSERT(mnew->object == NULL,
1444             ("vm_page_replace: page already in object"));
1445
1446         /*
1447          * This function mostly follows vm_page_insert() and
1448          * vm_page_remove() without the radix, object count and vnode
1449          * dance.  Double check such functions for more comments.
1450          */
1451
1452         mnew->object = object;
1453         mnew->pindex = pindex;
1454         mold = vm_radix_replace(&object->rtree, mnew);
1455         KASSERT(mold->queue == PQ_NONE,
1456             ("vm_page_replace: mold is on a paging queue"));
1457
1458         /* Keep the resident page list in sorted order. */
1459         TAILQ_INSERT_AFTER(&object->memq, mold, mnew, listq);
1460         TAILQ_REMOVE(&object->memq, mold, listq);
1461
1462         mold->object = NULL;
1463         vm_page_xunbusy_maybelocked(mold);
1464
1465         /*
1466          * The object's resident_page_count does not change because we have
1467          * swapped one page for another, but OBJ_MIGHTBEDIRTY.
1468          */
1469         if (pmap_page_is_write_mapped(mnew))
1470                 vm_object_set_writeable_dirty(object);
1471         return (mold);
1472 }
1473
1474 /*
1475  *      vm_page_rename:
1476  *
1477  *      Move the given memory entry from its
1478  *      current object to the specified target object/offset.
1479  *
1480  *      Note: swap associated with the page must be invalidated by the move.  We
1481  *            have to do this for several reasons:  (1) we aren't freeing the
1482  *            page, (2) we are dirtying the page, (3) the VM system is probably
1483  *            moving the page from object A to B, and will then later move
1484  *            the backing store from A to B and we can't have a conflict.
1485  *
1486  *      Note: we *always* dirty the page.  It is necessary both for the
1487  *            fact that we moved it, and because we may be invalidating
1488  *            swap.
1489  *
1490  *      The objects must be locked.
1491  */
1492 int
1493 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1494 {
1495         vm_page_t mpred;
1496         vm_pindex_t opidx;
1497
1498         VM_OBJECT_ASSERT_WLOCKED(new_object);
1499
1500         mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1501         KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1502             ("vm_page_rename: pindex already renamed"));
1503
1504         /*
1505          * Create a custom version of vm_page_insert() which does not depend
1506          * by m_prev and can cheat on the implementation aspects of the
1507          * function.
1508          */
1509         opidx = m->pindex;
1510         m->pindex = new_pindex;
1511         if (vm_radix_insert(&new_object->rtree, m)) {
1512                 m->pindex = opidx;
1513                 return (1);
1514         }
1515
1516         /*
1517          * The operation cannot fail anymore.  The removal must happen before
1518          * the listq iterator is tainted.
1519          */
1520         m->pindex = opidx;
1521         vm_page_lock(m);
1522         vm_page_remove(m);
1523
1524         /* Return back to the new pindex to complete vm_page_insert(). */
1525         m->pindex = new_pindex;
1526         m->object = new_object;
1527         vm_page_unlock(m);
1528         vm_page_insert_radixdone(m, new_object, mpred);
1529         vm_page_dirty(m);
1530         return (0);
1531 }
1532
1533 /*
1534  *      vm_page_alloc:
1535  *
1536  *      Allocate and return a page that is associated with the specified
1537  *      object and offset pair.  By default, this page is exclusive busied.
1538  *
1539  *      The caller must always specify an allocation class.
1540  *
1541  *      allocation classes:
1542  *      VM_ALLOC_NORMAL         normal process request
1543  *      VM_ALLOC_SYSTEM         system *really* needs a page
1544  *      VM_ALLOC_INTERRUPT      interrupt time request
1545  *
1546  *      optional allocation flags:
1547  *      VM_ALLOC_COUNT(number)  the number of additional pages that the caller
1548  *                              intends to allocate
1549  *      VM_ALLOC_NOBUSY         do not exclusive busy the page
1550  *      VM_ALLOC_NODUMP         do not include the page in a kernel core dump
1551  *      VM_ALLOC_NOOBJ          page is not associated with an object and
1552  *                              should not be exclusive busy
1553  *      VM_ALLOC_SBUSY          shared busy the allocated page
1554  *      VM_ALLOC_WIRED          wire the allocated page
1555  *      VM_ALLOC_ZERO           prefer a zeroed page
1556  *
1557  *      This routine may not sleep.
1558  */
1559 vm_page_t
1560 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1561 {
1562         vm_page_t m, mpred;
1563         int flags, req_class;
1564
1565         mpred = NULL;   /* XXX: pacify gcc */
1566         KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1567             (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1568             ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1569             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1570             ("vm_page_alloc: inconsistent object(%p)/req(%x)", object, req));
1571         if (object != NULL)
1572                 VM_OBJECT_ASSERT_WLOCKED(object);
1573
1574         if (__predict_false((req & VM_ALLOC_IFCACHED) != 0))
1575                 return (NULL);
1576
1577         req_class = req & VM_ALLOC_CLASS_MASK;
1578
1579         /*
1580          * The page daemon is allowed to dig deeper into the free page list.
1581          */
1582         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1583                 req_class = VM_ALLOC_SYSTEM;
1584
1585         if (object != NULL) {
1586                 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1587                 KASSERT(mpred == NULL || mpred->pindex != pindex,
1588                    ("vm_page_alloc: pindex already allocated"));
1589         }
1590
1591         /*
1592          * Allocate a page if the number of free pages exceeds the minimum
1593          * for the request class.
1594          */
1595         mtx_lock(&vm_page_queue_free_mtx);
1596         if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
1597             (req_class == VM_ALLOC_SYSTEM &&
1598             vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
1599             (req_class == VM_ALLOC_INTERRUPT &&
1600             vm_cnt.v_free_count > 0)) {
1601                 /*
1602                  * Can we allocate the page from a reservation?
1603                  */
1604 #if VM_NRESERVLEVEL > 0
1605                 if (object == NULL || (object->flags & (OBJ_COLORED |
1606                     OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
1607                     vm_reserv_alloc_page(object, pindex, mpred)) == NULL)
1608 #endif
1609                 {
1610                         /*
1611                          * If not, allocate it from the free page queues.
1612                          */
1613                         m = vm_phys_alloc_pages(object != NULL ?
1614                             VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1615 #if VM_NRESERVLEVEL > 0
1616                         if (m == NULL && vm_reserv_reclaim_inactive()) {
1617                                 m = vm_phys_alloc_pages(object != NULL ?
1618                                     VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1619                                     0);
1620                         }
1621 #endif
1622                 }
1623         } else {
1624                 /*
1625                  * Not allocatable, give up.
1626                  */
1627                 mtx_unlock(&vm_page_queue_free_mtx);
1628                 atomic_add_int(&vm_pageout_deficit,
1629                     max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1630                 pagedaemon_wakeup();
1631                 return (NULL);
1632         }
1633
1634         /*
1635          *  At this point we had better have found a good page.
1636          */
1637         KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1638         vm_phys_freecnt_adj(m, -1);
1639         if ((m->flags & PG_ZERO) != 0)
1640                 vm_page_zero_count--;
1641         mtx_unlock(&vm_page_queue_free_mtx);
1642         vm_page_alloc_check(m);
1643
1644         /*
1645          * Initialize the page.  Only the PG_ZERO flag is inherited.
1646          */
1647         flags = 0;
1648         if ((req & VM_ALLOC_ZERO) != 0)
1649                 flags = PG_ZERO;
1650         flags &= m->flags;
1651         if ((req & VM_ALLOC_NODUMP) != 0)
1652                 flags |= PG_NODUMP;
1653         m->flags = flags;
1654         m->aflags = 0;
1655         m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1656             VPO_UNMANAGED : 0;
1657         m->busy_lock = VPB_UNBUSIED;
1658         if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1659                 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1660         if ((req & VM_ALLOC_SBUSY) != 0)
1661                 m->busy_lock = VPB_SHARERS_WORD(1);
1662         if (req & VM_ALLOC_WIRED) {
1663                 /*
1664                  * The page lock is not required for wiring a page until that
1665                  * page is inserted into the object.
1666                  */
1667                 atomic_add_int(&vm_cnt.v_wire_count, 1);
1668                 m->wire_count = 1;
1669         }
1670         m->act_count = 0;
1671
1672         if (object != NULL) {
1673                 if (vm_page_insert_after(m, object, pindex, mpred)) {
1674                         pagedaemon_wakeup();
1675                         if (req & VM_ALLOC_WIRED) {
1676                                 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1677                                 m->wire_count = 0;
1678                         }
1679                         KASSERT(m->object == NULL, ("page %p has object", m));
1680                         m->oflags = VPO_UNMANAGED;
1681                         m->busy_lock = VPB_UNBUSIED;
1682                         /* Don't change PG_ZERO. */
1683                         vm_page_free_toq(m);
1684                         return (NULL);
1685                 }
1686
1687                 /* Ignore device objects; the pager sets "memattr" for them. */
1688                 if (object->memattr != VM_MEMATTR_DEFAULT &&
1689                     (object->flags & OBJ_FICTITIOUS) == 0)
1690                         pmap_page_set_memattr(m, object->memattr);
1691         } else
1692                 m->pindex = pindex;
1693
1694         /*
1695          * Don't wakeup too often - wakeup the pageout daemon when
1696          * we would be nearly out of memory.
1697          */
1698         if (vm_paging_needed())
1699                 pagedaemon_wakeup();
1700
1701         return (m);
1702 }
1703
1704 /*
1705  *      vm_page_alloc_contig:
1706  *
1707  *      Allocate a contiguous set of physical pages of the given size "npages"
1708  *      from the free lists.  All of the physical pages must be at or above
1709  *      the given physical address "low" and below the given physical address
1710  *      "high".  The given value "alignment" determines the alignment of the
1711  *      first physical page in the set.  If the given value "boundary" is
1712  *      non-zero, then the set of physical pages cannot cross any physical
1713  *      address boundary that is a multiple of that value.  Both "alignment"
1714  *      and "boundary" must be a power of two.
1715  *
1716  *      If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1717  *      then the memory attribute setting for the physical pages is configured
1718  *      to the object's memory attribute setting.  Otherwise, the memory
1719  *      attribute setting for the physical pages is configured to "memattr",
1720  *      overriding the object's memory attribute setting.  However, if the
1721  *      object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1722  *      memory attribute setting for the physical pages cannot be configured
1723  *      to VM_MEMATTR_DEFAULT.
1724  *
1725  *      The specified object may not contain fictitious pages.
1726  *
1727  *      The caller must always specify an allocation class.
1728  *
1729  *      allocation classes:
1730  *      VM_ALLOC_NORMAL         normal process request
1731  *      VM_ALLOC_SYSTEM         system *really* needs a page
1732  *      VM_ALLOC_INTERRUPT      interrupt time request
1733  *
1734  *      optional allocation flags:
1735  *      VM_ALLOC_NOBUSY         do not exclusive busy the page
1736  *      VM_ALLOC_NODUMP         do not include the page in a kernel core dump
1737  *      VM_ALLOC_NOOBJ          page is not associated with an object and
1738  *                              should not be exclusive busy
1739  *      VM_ALLOC_SBUSY          shared busy the allocated page
1740  *      VM_ALLOC_WIRED          wire the allocated page
1741  *      VM_ALLOC_ZERO           prefer a zeroed page
1742  *
1743  *      This routine may not sleep.
1744  */
1745 vm_page_t
1746 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1747     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1748     vm_paddr_t boundary, vm_memattr_t memattr)
1749 {
1750         vm_page_t m, m_ret, mpred;
1751         u_int busy_lock, flags, oflags;
1752         int req_class;
1753
1754         mpred = NULL;   /* XXX: pacify gcc */
1755         KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1756             (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1757             ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1758             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1759             ("vm_page_alloc_contig: inconsistent object(%p)/req(%x)", object,
1760             req));
1761         if (object != NULL) {
1762                 VM_OBJECT_ASSERT_WLOCKED(object);
1763                 KASSERT((object->flags & OBJ_FICTITIOUS) == 0,
1764                     ("vm_page_alloc_contig: object %p has fictitious pages",
1765                     object));
1766         }
1767         KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1768         req_class = req & VM_ALLOC_CLASS_MASK;
1769
1770         /*
1771          * The page daemon is allowed to dig deeper into the free page list.
1772          */
1773         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1774                 req_class = VM_ALLOC_SYSTEM;
1775
1776         if (object != NULL) {
1777                 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1778                 KASSERT(mpred == NULL || mpred->pindex != pindex,
1779                     ("vm_page_alloc_contig: pindex already allocated"));
1780         }
1781
1782         /*
1783          * Can we allocate the pages without the number of free pages falling
1784          * below the lower bound for the allocation class?
1785          */
1786         mtx_lock(&vm_page_queue_free_mtx);
1787         if (vm_cnt.v_free_count >= npages + vm_cnt.v_free_reserved ||
1788             (req_class == VM_ALLOC_SYSTEM &&
1789             vm_cnt.v_free_count >= npages + vm_cnt.v_interrupt_free_min) ||
1790             (req_class == VM_ALLOC_INTERRUPT &&
1791             vm_cnt.v_free_count >= npages)) {
1792                 /*
1793                  * Can we allocate the pages from a reservation?
1794                  */
1795 #if VM_NRESERVLEVEL > 0
1796 retry:
1797                 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1798                     (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1799                     low, high, alignment, boundary, mpred)) == NULL)
1800 #endif
1801                         /*
1802                          * If not, allocate them from the free page queues.
1803                          */
1804                         m_ret = vm_phys_alloc_contig(npages, low, high,
1805                             alignment, boundary);
1806         } else {
1807                 mtx_unlock(&vm_page_queue_free_mtx);
1808                 atomic_add_int(&vm_pageout_deficit, npages);
1809                 pagedaemon_wakeup();
1810                 return (NULL);
1811         }
1812         if (m_ret != NULL) {
1813                 vm_phys_freecnt_adj(m_ret, -npages);
1814                 for (m = m_ret; m < &m_ret[npages]; m++)
1815                         if ((m->flags & PG_ZERO) != 0)
1816                                 vm_page_zero_count--;
1817         } else {
1818 #if VM_NRESERVLEVEL > 0
1819                 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1820                     boundary))
1821                         goto retry;
1822 #endif
1823         }
1824         mtx_unlock(&vm_page_queue_free_mtx);
1825         if (m_ret == NULL)
1826                 return (NULL);
1827         for (m = m_ret; m < &m_ret[npages]; m++)
1828                 vm_page_alloc_check(m);
1829
1830         /*
1831          * Initialize the pages.  Only the PG_ZERO flag is inherited.
1832          */
1833         flags = 0;
1834         if ((req & VM_ALLOC_ZERO) != 0)
1835                 flags = PG_ZERO;
1836         if ((req & VM_ALLOC_NODUMP) != 0)
1837                 flags |= PG_NODUMP;
1838         oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1839             VPO_UNMANAGED : 0;
1840         busy_lock = VPB_UNBUSIED;
1841         if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1842                 busy_lock = VPB_SINGLE_EXCLUSIVER;
1843         if ((req & VM_ALLOC_SBUSY) != 0)
1844                 busy_lock = VPB_SHARERS_WORD(1);
1845         if ((req & VM_ALLOC_WIRED) != 0)
1846                 atomic_add_int(&vm_cnt.v_wire_count, npages);
1847         if (object != NULL) {
1848                 if (object->memattr != VM_MEMATTR_DEFAULT &&
1849                     memattr == VM_MEMATTR_DEFAULT)
1850                         memattr = object->memattr;
1851         }
1852         for (m = m_ret; m < &m_ret[npages]; m++) {
1853                 m->aflags = 0;
1854                 m->flags = (m->flags | PG_NODUMP) & flags;
1855                 m->busy_lock = busy_lock;
1856                 if ((req & VM_ALLOC_WIRED) != 0)
1857                         m->wire_count = 1;
1858                 m->act_count = 0;
1859                 m->oflags = oflags;
1860                 if (object != NULL) {
1861                         if (vm_page_insert_after(m, object, pindex, mpred)) {
1862                                 pagedaemon_wakeup();
1863                                 if ((req & VM_ALLOC_WIRED) != 0)
1864                                         atomic_subtract_int(
1865                                             &vm_cnt.v_wire_count, npages);
1866                                 KASSERT(m->object == NULL,
1867                                     ("page %p has object", m));
1868                                 mpred = m;
1869                                 for (m = m_ret; m < &m_ret[npages]; m++) {
1870                                         if (m <= mpred &&
1871                                             (req & VM_ALLOC_WIRED) != 0)
1872                                                 m->wire_count = 0;
1873                                         m->oflags = VPO_UNMANAGED;
1874                                         m->busy_lock = VPB_UNBUSIED;
1875                                         /* Don't change PG_ZERO. */
1876                                         vm_page_free_toq(m);
1877                                 }
1878                                 return (NULL);
1879                         }
1880                         mpred = m;
1881                 } else
1882                         m->pindex = pindex;
1883                 if (memattr != VM_MEMATTR_DEFAULT)
1884                         pmap_page_set_memattr(m, memattr);
1885                 pindex++;
1886         }
1887         if (vm_paging_needed())
1888                 pagedaemon_wakeup();
1889         return (m_ret);
1890 }
1891
1892 /*
1893  * Check a page that has been freshly dequeued from a freelist.
1894  */
1895 static void
1896 vm_page_alloc_check(vm_page_t m)
1897 {
1898
1899         KASSERT(m->object == NULL, ("page %p has object", m));
1900         KASSERT(m->queue == PQ_NONE,
1901             ("page %p has unexpected queue %d", m, m->queue));
1902         KASSERT(m->wire_count == 0, ("page %p is wired", m));
1903         KASSERT(m->hold_count == 0, ("page %p is held", m));
1904         KASSERT(!vm_page_busied(m), ("page %p is busy", m));
1905         KASSERT(m->dirty == 0, ("page %p is dirty", m));
1906         KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1907             ("page %p has unexpected memattr %d",
1908             m, pmap_page_get_memattr(m)));
1909         KASSERT(m->valid == 0, ("free page %p is valid", m));
1910 }
1911
1912 /*
1913  *      vm_page_alloc_freelist:
1914  *
1915  *      Allocate a physical page from the specified free page list.
1916  *
1917  *      The caller must always specify an allocation class.
1918  *
1919  *      allocation classes:
1920  *      VM_ALLOC_NORMAL         normal process request
1921  *      VM_ALLOC_SYSTEM         system *really* needs a page
1922  *      VM_ALLOC_INTERRUPT      interrupt time request
1923  *
1924  *      optional allocation flags:
1925  *      VM_ALLOC_COUNT(number)  the number of additional pages that the caller
1926  *                              intends to allocate
1927  *      VM_ALLOC_WIRED          wire the allocated page
1928  *      VM_ALLOC_ZERO           prefer a zeroed page
1929  *
1930  *      This routine may not sleep.
1931  */
1932 vm_page_t
1933 vm_page_alloc_freelist(int flind, int req)
1934 {
1935         vm_page_t m;
1936         u_int flags;
1937         int req_class;
1938
1939         req_class = req & VM_ALLOC_CLASS_MASK;
1940
1941         /*
1942          * The page daemon is allowed to dig deeper into the free page list.
1943          */
1944         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1945                 req_class = VM_ALLOC_SYSTEM;
1946
1947         /*
1948          * Do not allocate reserved pages unless the req has asked for it.
1949          */
1950         mtx_lock(&vm_page_queue_free_mtx);
1951         if (vm_cnt.v_free_count > vm_cnt.v_free_reserved ||
1952             (req_class == VM_ALLOC_SYSTEM &&
1953             vm_cnt.v_free_count > vm_cnt.v_interrupt_free_min) ||
1954             (req_class == VM_ALLOC_INTERRUPT &&
1955             vm_cnt.v_free_count > 0))
1956                 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1957         else {
1958                 mtx_unlock(&vm_page_queue_free_mtx);
1959                 atomic_add_int(&vm_pageout_deficit,
1960                     max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1961                 pagedaemon_wakeup();
1962                 return (NULL);
1963         }
1964         if (m == NULL) {
1965                 mtx_unlock(&vm_page_queue_free_mtx);
1966                 return (NULL);
1967         }
1968         vm_phys_freecnt_adj(m, -1);
1969         if ((m->flags & PG_ZERO) != 0)
1970                 vm_page_zero_count--;
1971         mtx_unlock(&vm_page_queue_free_mtx);
1972         vm_page_alloc_check(m);
1973
1974         /*
1975          * Initialize the page.  Only the PG_ZERO flag is inherited.
1976          */
1977         m->aflags = 0;
1978         flags = 0;
1979         if ((req & VM_ALLOC_ZERO) != 0)
1980                 flags = PG_ZERO;
1981         m->flags &= flags;
1982         if ((req & VM_ALLOC_WIRED) != 0) {
1983                 /*
1984                  * The page lock is not required for wiring a page that does
1985                  * not belong to an object.
1986                  */
1987                 atomic_add_int(&vm_cnt.v_wire_count, 1);
1988                 m->wire_count = 1;
1989         }
1990         /* Unmanaged pages don't use "act_count". */
1991         m->oflags = VPO_UNMANAGED;
1992         if (vm_paging_needed())
1993                 pagedaemon_wakeup();
1994         return (m);
1995 }
1996
1997 #define VPSC_ANY        0       /* No restrictions. */
1998 #define VPSC_NORESERV   1       /* Skip reservations; implies VPSC_NOSUPER. */
1999 #define VPSC_NOSUPER    2       /* Skip superpages. */
2000
2001 /*
2002  *      vm_page_scan_contig:
2003  *
2004  *      Scan vm_page_array[] between the specified entries "m_start" and
2005  *      "m_end" for a run of contiguous physical pages that satisfy the
2006  *      specified conditions, and return the lowest page in the run.  The
2007  *      specified "alignment" determines the alignment of the lowest physical
2008  *      page in the run.  If the specified "boundary" is non-zero, then the
2009  *      run of physical pages cannot span a physical address that is a
2010  *      multiple of "boundary".
2011  *
2012  *      "m_end" is never dereferenced, so it need not point to a vm_page
2013  *      structure within vm_page_array[].
2014  *
2015  *      "npages" must be greater than zero.  "m_start" and "m_end" must not
2016  *      span a hole (or discontiguity) in the physical address space.  Both
2017  *      "alignment" and "boundary" must be a power of two.
2018  */
2019 vm_page_t
2020 vm_page_scan_contig(u_long npages, vm_page_t m_start, vm_page_t m_end,
2021     u_long alignment, vm_paddr_t boundary, int options)
2022 {
2023         struct mtx *m_mtx;
2024         vm_object_t object;
2025         vm_paddr_t pa;
2026         vm_page_t m, m_run;
2027 #if VM_NRESERVLEVEL > 0
2028         int level;
2029 #endif
2030         int m_inc, order, run_ext, run_len;
2031
2032         KASSERT(npages > 0, ("npages is 0"));
2033         KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2034         KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2035         m_run = NULL;
2036         run_len = 0;
2037         m_mtx = NULL;
2038         for (m = m_start; m < m_end && run_len < npages; m += m_inc) {
2039                 KASSERT((m->flags & PG_MARKER) == 0,
2040                     ("page %p is PG_MARKER", m));
2041                 KASSERT((m->flags & PG_FICTITIOUS) == 0 || m->wire_count == 1,
2042                     ("fictitious page %p has invalid wire count", m));
2043
2044                 /*
2045                  * If the current page would be the start of a run, check its
2046                  * physical address against the end, alignment, and boundary
2047                  * conditions.  If it doesn't satisfy these conditions, either
2048                  * terminate the scan or advance to the next page that
2049                  * satisfies the failed condition.
2050                  */
2051                 if (run_len == 0) {
2052                         KASSERT(m_run == NULL, ("m_run != NULL"));
2053                         if (m + npages > m_end)
2054                                 break;
2055                         pa = VM_PAGE_TO_PHYS(m);
2056                         if ((pa & (alignment - 1)) != 0) {
2057                                 m_inc = atop(roundup2(pa, alignment) - pa);
2058                                 continue;
2059                         }
2060                         if (rounddown2(pa ^ (pa + ptoa(npages) - 1),
2061                             boundary) != 0) {
2062                                 m_inc = atop(roundup2(pa, boundary) - pa);
2063                                 continue;
2064                         }
2065                 } else
2066                         KASSERT(m_run != NULL, ("m_run == NULL"));
2067
2068                 vm_page_change_lock(m, &m_mtx);
2069                 m_inc = 1;
2070 retry:
2071                 if (m->wire_count != 0 || m->hold_count != 0)
2072                         run_ext = 0;
2073 #if VM_NRESERVLEVEL > 0
2074                 else if ((level = vm_reserv_level(m)) >= 0 &&
2075                     (options & VPSC_NORESERV) != 0) {
2076                         run_ext = 0;
2077                         /* Advance to the end of the reservation. */
2078                         pa = VM_PAGE_TO_PHYS(m);
2079                         m_inc = atop(roundup2(pa + 1, vm_reserv_size(level)) -
2080                             pa);
2081                 }
2082 #endif
2083                 else if ((object = m->object) != NULL) {
2084                         /*
2085                          * The page is considered eligible for relocation if
2086                          * and only if it could be laundered or reclaimed by
2087                          * the page daemon.
2088                          */
2089                         if (!VM_OBJECT_TRYRLOCK(object)) {
2090                                 mtx_unlock(m_mtx);
2091                                 VM_OBJECT_RLOCK(object);
2092                                 mtx_lock(m_mtx);
2093                                 if (m->object != object) {
2094                                         /*
2095                                          * The page may have been freed.
2096                                          */
2097                                         VM_OBJECT_RUNLOCK(object);
2098                                         goto retry;
2099                                 } else if (m->wire_count != 0 ||
2100                                     m->hold_count != 0) {
2101                                         run_ext = 0;
2102                                         goto unlock;
2103                                 }
2104                         }
2105                         KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2106                             ("page %p is PG_UNHOLDFREE", m));
2107                         /* Don't care: PG_NODUMP, PG_ZERO. */
2108                         if (object->type != OBJT_DEFAULT &&
2109                             object->type != OBJT_SWAP &&
2110                             object->type != OBJT_VNODE) {
2111                                 run_ext = 0;
2112 #if VM_NRESERVLEVEL > 0
2113                         } else if ((options & VPSC_NOSUPER) != 0 &&
2114                             (level = vm_reserv_level_iffullpop(m)) >= 0) {
2115                                 run_ext = 0;
2116                                 /* Advance to the end of the superpage. */
2117                                 pa = VM_PAGE_TO_PHYS(m);
2118                                 m_inc = atop(roundup2(pa + 1,
2119                                     vm_reserv_size(level)) - pa);
2120 #endif
2121                         } else if (object->memattr == VM_MEMATTR_DEFAULT &&
2122                             m->queue != PQ_NONE && !vm_page_busied(m)) {
2123                                 /*
2124                                  * The page is allocated but eligible for
2125                                  * relocation.  Extend the current run by one
2126                                  * page.
2127                                  */
2128                                 KASSERT(pmap_page_get_memattr(m) ==
2129                                     VM_MEMATTR_DEFAULT,
2130                                     ("page %p has an unexpected memattr", m));
2131                                 KASSERT((m->oflags & (VPO_SWAPINPROG |
2132                                     VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2133                                     ("page %p has unexpected oflags", m));
2134                                 /* Don't care: VPO_NOSYNC. */
2135                                 run_ext = 1;
2136                         } else
2137                                 run_ext = 0;
2138 unlock:
2139                         VM_OBJECT_RUNLOCK(object);
2140 #if VM_NRESERVLEVEL > 0
2141                 } else if (level >= 0) {
2142                         /*
2143                          * The page is reserved but not yet allocated.  In
2144                          * other words, it is still free.  Extend the current
2145                          * run by one page.
2146                          */
2147                         run_ext = 1;
2148 #endif
2149                 } else if ((order = m->order) < VM_NFREEORDER) {
2150                         /*
2151                          * The page is enqueued in the physical memory
2152                          * allocator's free page queues.  Moreover, it is the
2153                          * first page in a power-of-two-sized run of
2154                          * contiguous free pages.  Add these pages to the end
2155                          * of the current run, and jump ahead.
2156                          */
2157                         run_ext = 1 << order;
2158                         m_inc = 1 << order;
2159                 } else {
2160                         /*
2161                          * Skip the page for one of the following reasons: (1)
2162                          * It is enqueued in the physical memory allocator's
2163                          * free page queues.  However, it is not the first
2164                          * page in a run of contiguous free pages.  (This case
2165                          * rarely occurs because the scan is performed in
2166                          * ascending order.) (2) It is not reserved, and it is
2167                          * transitioning from free to allocated.  (Conversely,
2168                          * the transition from allocated to free for managed
2169                          * pages is blocked by the page lock.) (3) It is
2170                          * allocated but not contained by an object and not
2171                          * wired, e.g., allocated by Xen's balloon driver.
2172                          */
2173                         run_ext = 0;
2174                 }
2175
2176                 /*
2177                  * Extend or reset the current run of pages.
2178                  */
2179                 if (run_ext > 0) {
2180                         if (run_len == 0)
2181                                 m_run = m;
2182                         run_len += run_ext;
2183                 } else {
2184                         if (run_len > 0) {
2185                                 m_run = NULL;
2186                                 run_len = 0;
2187                         }
2188                 }
2189         }
2190         if (m_mtx != NULL)
2191                 mtx_unlock(m_mtx);
2192         if (run_len >= npages)
2193                 return (m_run);
2194         return (NULL);
2195 }
2196
2197 /*
2198  *      vm_page_reclaim_run:
2199  *
2200  *      Try to relocate each of the allocated virtual pages within the
2201  *      specified run of physical pages to a new physical address.  Free the
2202  *      physical pages underlying the relocated virtual pages.  A virtual page
2203  *      is relocatable if and only if it could be laundered or reclaimed by
2204  *      the page daemon.  Whenever possible, a virtual page is relocated to a
2205  *      physical address above "high".
2206  *
2207  *      Returns 0 if every physical page within the run was already free or
2208  *      just freed by a successful relocation.  Otherwise, returns a non-zero
2209  *      value indicating why the last attempt to relocate a virtual page was
2210  *      unsuccessful.
2211  *
2212  *      "req_class" must be an allocation class.
2213  */
2214 static int
2215 vm_page_reclaim_run(int req_class, u_long npages, vm_page_t m_run,
2216     vm_paddr_t high)
2217 {
2218         struct mtx *m_mtx;
2219         struct spglist free;
2220         vm_object_t object;
2221         vm_paddr_t pa;
2222         vm_page_t m, m_end, m_new;
2223         int error, order, req;
2224
2225         KASSERT((req_class & VM_ALLOC_CLASS_MASK) == req_class,
2226             ("req_class is not an allocation class"));
2227         SLIST_INIT(&free);
2228         error = 0;
2229         m = m_run;
2230         m_end = m_run + npages;
2231         m_mtx = NULL;
2232         for (; error == 0 && m < m_end; m++) {
2233                 KASSERT((m->flags & (PG_FICTITIOUS | PG_MARKER)) == 0,
2234                     ("page %p is PG_FICTITIOUS or PG_MARKER", m));
2235
2236                 /*
2237                  * Avoid releasing and reacquiring the same page lock.
2238                  */
2239                 vm_page_change_lock(m, &m_mtx);
2240 retry:
2241                 if (m->wire_count != 0 || m->hold_count != 0)
2242                         error = EBUSY;
2243                 else if ((object = m->object) != NULL) {
2244                         /*
2245                          * The page is relocated if and only if it could be
2246                          * laundered or reclaimed by the page daemon.
2247                          */
2248                         if (!VM_OBJECT_TRYWLOCK(object)) {
2249                                 mtx_unlock(m_mtx);
2250                                 VM_OBJECT_WLOCK(object);
2251                                 mtx_lock(m_mtx);
2252                                 if (m->object != object) {
2253                                         /*
2254                                          * The page may have been freed.
2255                                          */
2256                                         VM_OBJECT_WUNLOCK(object);
2257                                         goto retry;
2258                                 } else if (m->wire_count != 0 ||
2259                                     m->hold_count != 0) {
2260                                         error = EBUSY;
2261                                         goto unlock;
2262                                 }
2263                         }
2264                         KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2265                             ("page %p is PG_UNHOLDFREE", m));
2266                         /* Don't care: PG_NODUMP, PG_ZERO. */
2267                         if (object->type != OBJT_DEFAULT &&
2268                             object->type != OBJT_SWAP &&
2269                             object->type != OBJT_VNODE)
2270                                 error = EINVAL;
2271                         else if (object->memattr != VM_MEMATTR_DEFAULT)
2272                                 error = EINVAL;
2273                         else if (m->queue != PQ_NONE && !vm_page_busied(m)) {
2274                                 KASSERT(pmap_page_get_memattr(m) ==
2275                                     VM_MEMATTR_DEFAULT,
2276                                     ("page %p has an unexpected memattr", m));
2277                                 KASSERT((m->oflags & (VPO_SWAPINPROG |
2278                                     VPO_SWAPSLEEP | VPO_UNMANAGED)) == 0,
2279                                     ("page %p has unexpected oflags", m));
2280                                 /* Don't care: VPO_NOSYNC. */
2281                                 if (m->valid != 0) {
2282                                         /*
2283                                          * First, try to allocate a new page
2284                                          * that is above "high".  Failing
2285                                          * that, try to allocate a new page
2286                                          * that is below "m_run".  Allocate
2287                                          * the new page between the end of
2288                                          * "m_run" and "high" only as a last
2289                                          * resort.
2290                                          */
2291                                         req = req_class | VM_ALLOC_NOOBJ;
2292                                         if ((m->flags & PG_NODUMP) != 0)
2293                                                 req |= VM_ALLOC_NODUMP;
2294                                         if (trunc_page(high) !=
2295                                             ~(vm_paddr_t)PAGE_MASK) {
2296                                                 m_new = vm_page_alloc_contig(
2297                                                     NULL, 0, req, 1,
2298                                                     round_page(high),
2299                                                     ~(vm_paddr_t)0,
2300                                                     PAGE_SIZE, 0,
2301                                                     VM_MEMATTR_DEFAULT);
2302                                         } else
2303                                                 m_new = NULL;
2304                                         if (m_new == NULL) {
2305                                                 pa = VM_PAGE_TO_PHYS(m_run);
2306                                                 m_new = vm_page_alloc_contig(
2307                                                     NULL, 0, req, 1,
2308                                                     0, pa - 1, PAGE_SIZE, 0,
2309                                                     VM_MEMATTR_DEFAULT);
2310                                         }
2311                                         if (m_new == NULL) {
2312                                                 pa += ptoa(npages);
2313                                                 m_new = vm_page_alloc_contig(
2314                                                     NULL, 0, req, 1,
2315                                                     pa, high, PAGE_SIZE, 0,
2316                                                     VM_MEMATTR_DEFAULT);
2317                                         }
2318                                         if (m_new == NULL) {
2319                                                 error = ENOMEM;
2320                                                 goto unlock;
2321                                         }
2322                                         KASSERT(m_new->wire_count == 0,
2323                                             ("page %p is wired", m));
2324
2325                                         /*
2326                                          * Replace "m" with the new page.  For
2327                                          * vm_page_replace(), "m" must be busy
2328                                          * and dequeued.  Finally, change "m"
2329                                          * as if vm_page_free() was called.
2330                                          */
2331                                         if (object->ref_count != 0)
2332                                                 pmap_remove_all(m);
2333                                         m_new->aflags = m->aflags;
2334                                         KASSERT(m_new->oflags == VPO_UNMANAGED,
2335                                             ("page %p is managed", m));
2336                                         m_new->oflags = m->oflags & VPO_NOSYNC;
2337                                         pmap_copy_page(m, m_new);
2338                                         m_new->valid = m->valid;
2339                                         m_new->dirty = m->dirty;
2340                                         m->flags &= ~PG_ZERO;
2341                                         vm_page_xbusy(m);
2342                                         vm_page_remque(m);
2343                                         vm_page_replace_checked(m_new, object,
2344                                             m->pindex, m);
2345                                         m->valid = 0;
2346                                         vm_page_undirty(m);
2347
2348                                         /*
2349                                          * The new page must be deactivated
2350                                          * before the object is unlocked.
2351                                          */
2352                                         vm_page_change_lock(m_new, &m_mtx);
2353                                         vm_page_deactivate(m_new);
2354                                 } else {
2355                                         m->flags &= ~PG_ZERO;
2356                                         vm_page_remque(m);
2357                                         vm_page_remove(m);
2358                                         KASSERT(m->dirty == 0,
2359                                             ("page %p is dirty", m));
2360                                 }
2361                                 SLIST_INSERT_HEAD(&free, m, plinks.s.ss);
2362                         } else
2363                                 error = EBUSY;
2364 unlock:
2365                         VM_OBJECT_WUNLOCK(object);
2366                 } else {
2367                         mtx_lock(&vm_page_queue_free_mtx);
2368                         order = m->order;
2369                         if (order < VM_NFREEORDER) {
2370                                 /*
2371                                  * The page is enqueued in the physical memory
2372                                  * allocator's free page queues.  Moreover, it
2373                                  * is the first page in a power-of-two-sized
2374                                  * run of contiguous free pages.  Jump ahead
2375                                  * to the last page within that run, and
2376                                  * continue from there.
2377                                  */
2378                                 m += (1 << order) - 1;
2379                         }
2380 #if VM_NRESERVLEVEL > 0
2381                         else if (vm_reserv_is_page_free(m))
2382                                 order = 0;
2383 #endif
2384                         mtx_unlock(&vm_page_queue_free_mtx);
2385                         if (order == VM_NFREEORDER)
2386                                 error = EINVAL;
2387                 }
2388         }
2389         if (m_mtx != NULL)
2390                 mtx_unlock(m_mtx);
2391         if ((m = SLIST_FIRST(&free)) != NULL) {
2392                 mtx_lock(&vm_page_queue_free_mtx);
2393                 do {
2394                         SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2395                         vm_page_free_phys(m);
2396                 } while ((m = SLIST_FIRST(&free)) != NULL);
2397                 vm_page_zero_idle_wakeup();
2398                 vm_page_free_wakeup();
2399                 mtx_unlock(&vm_page_queue_free_mtx);
2400         }
2401         return (error);
2402 }
2403
2404 #define NRUNS   16
2405
2406 CTASSERT(powerof2(NRUNS));
2407
2408 #define RUN_INDEX(count)        ((count) & (NRUNS - 1))
2409
2410 #define MIN_RECLAIM     8
2411
2412 /*
2413  *      vm_page_reclaim_contig:
2414  *
2415  *      Reclaim allocated, contiguous physical memory satisfying the specified
2416  *      conditions by relocating the virtual pages using that physical memory.
2417  *      Returns true if reclamation is successful and false otherwise.  Since
2418  *      relocation requires the allocation of physical pages, reclamation may
2419  *      fail due to a shortage of free pages.  When reclamation fails, callers
2420  *      are expected to perform VM_WAIT before retrying a failed allocation
2421  *      operation, e.g., vm_page_alloc_contig().
2422  *
2423  *      The caller must always specify an allocation class through "req".
2424  *
2425  *      allocation classes:
2426  *      VM_ALLOC_NORMAL         normal process request
2427  *      VM_ALLOC_SYSTEM         system *really* needs a page
2428  *      VM_ALLOC_INTERRUPT      interrupt time request
2429  *
2430  *      The optional allocation flags are ignored.
2431  *
2432  *      "npages" must be greater than zero.  Both "alignment" and "boundary"
2433  *      must be a power of two.
2434  */
2435 bool
2436 vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, vm_paddr_t high,
2437     u_long alignment, vm_paddr_t boundary)
2438 {
2439         vm_paddr_t curr_low;
2440         vm_page_t m_run, m_runs[NRUNS];
2441         u_long count, reclaimed;
2442         int error, i, options, req_class;
2443
2444         KASSERT(npages > 0, ("npages is 0"));
2445         KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
2446         KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
2447         req_class = req & VM_ALLOC_CLASS_MASK;
2448
2449         /*
2450          * The page daemon is allowed to dig deeper into the free page list.
2451          */
2452         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
2453                 req_class = VM_ALLOC_SYSTEM;
2454
2455         /*
2456          * Return if the number of free pages cannot satisfy the requested
2457          * allocation.
2458          */
2459         count = vm_cnt.v_free_count;
2460         if (count < npages + vm_cnt.v_free_reserved || (count < npages +
2461             vm_cnt.v_interrupt_free_min && req_class == VM_ALLOC_SYSTEM) ||
2462             (count < npages && req_class == VM_ALLOC_INTERRUPT))
2463                 return (false);
2464
2465         /*
2466          * Scan up to three times, relaxing the restrictions ("options") on
2467          * the reclamation of reservations and superpages each time.
2468          */
2469         for (options = VPSC_NORESERV;;) {
2470                 /*
2471                  * Find the highest runs that satisfy the given constraints
2472                  * and restrictions, and record them in "m_runs".
2473                  */
2474                 curr_low = low;
2475                 count = 0;
2476                 for (;;) {
2477                         m_run = vm_phys_scan_contig(npages, curr_low, high,
2478                             alignment, boundary, options);
2479                         if (m_run == NULL)
2480                                 break;
2481                         curr_low = VM_PAGE_TO_PHYS(m_run) + ptoa(npages);
2482                         m_runs[RUN_INDEX(count)] = m_run;
2483                         count++;
2484                 }
2485
2486                 /*
2487                  * Reclaim the highest runs in LIFO (descending) order until
2488                  * the number of reclaimed pages, "reclaimed", is at least
2489                  * MIN_RECLAIM.  Reset "reclaimed" each time because each
2490                  * reclamation is idempotent, and runs will (likely) recur
2491                  * from one scan to the next as restrictions are relaxed.
2492                  */
2493                 reclaimed = 0;
2494                 for (i = 0; count > 0 && i < NRUNS; i++) {
2495                         count--;
2496                         m_run = m_runs[RUN_INDEX(count)];
2497                         error = vm_page_reclaim_run(req_class, npages, m_run,
2498                             high);
2499                         if (error == 0) {
2500                                 reclaimed += npages;
2501                                 if (reclaimed >= MIN_RECLAIM)
2502                                         return (true);
2503                         }
2504                 }
2505
2506                 /*
2507                  * Either relax the restrictions on the next scan or return if
2508                  * the last scan had no restrictions.
2509                  */
2510                 if (options == VPSC_NORESERV)
2511                         options = VPSC_NOSUPER;
2512                 else if (options == VPSC_NOSUPER)
2513                         options = VPSC_ANY;
2514                 else if (options == VPSC_ANY)
2515                         return (reclaimed != 0);
2516         }
2517 }
2518
2519 /*
2520  *      vm_wait:        (also see VM_WAIT macro)
2521  *
2522  *      Sleep until free pages are available for allocation.
2523  *      - Called in various places before memory allocations.
2524  */
2525 void
2526 vm_wait(void)
2527 {
2528
2529         mtx_lock(&vm_page_queue_free_mtx);
2530         if (curproc == pageproc) {
2531                 vm_pageout_pages_needed = 1;
2532                 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
2533                     PDROP | PSWP, "VMWait", 0);
2534         } else {
2535                 if (__predict_false(pageproc == NULL))
2536                         panic("vm_wait in early boot");
2537                 if (!vm_pageout_wanted) {
2538                         vm_pageout_wanted = true;
2539                         wakeup(&vm_pageout_wanted);
2540                 }
2541                 vm_pages_needed = true;
2542                 msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
2543                     "vmwait", 0);
2544         }
2545 }
2546
2547 /*
2548  *      vm_waitpfault:  (also see VM_WAITPFAULT macro)
2549  *
2550  *      Sleep until free pages are available for allocation.
2551  *      - Called only in vm_fault so that processes page faulting
2552  *        can be easily tracked.
2553  *      - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
2554  *        processes will be able to grab memory first.  Do not change
2555  *        this balance without careful testing first.
2556  */
2557 void
2558 vm_waitpfault(void)
2559 {
2560
2561         mtx_lock(&vm_page_queue_free_mtx);
2562         if (!vm_pageout_wanted) {
2563                 vm_pageout_wanted = true;
2564                 wakeup(&vm_pageout_wanted);
2565         }
2566         vm_pages_needed = true;
2567         msleep(&vm_cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
2568             "pfault", 0);
2569 }
2570
2571 struct vm_pagequeue *
2572 vm_page_pagequeue(vm_page_t m)
2573 {
2574
2575         if (vm_page_in_laundry(m))
2576                 return (&vm_dom[0].vmd_pagequeues[m->queue]);
2577         else
2578                 return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]);
2579 }
2580
2581 /*
2582  *      vm_page_dequeue:
2583  *
2584  *      Remove the given page from its current page queue.
2585  *
2586  *      The page must be locked.
2587  */
2588 void
2589 vm_page_dequeue(vm_page_t m)
2590 {
2591         struct vm_pagequeue *pq;
2592
2593         vm_page_assert_locked(m);
2594         KASSERT(m->queue < PQ_COUNT, ("vm_page_dequeue: page %p is not queued",
2595             m));
2596         pq = vm_page_pagequeue(m);
2597         vm_pagequeue_lock(pq);
2598         m->queue = PQ_NONE;
2599         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2600         vm_pagequeue_cnt_dec(pq);
2601         vm_pagequeue_unlock(pq);
2602 }
2603
2604 /*
2605  *      vm_page_dequeue_locked:
2606  *
2607  *      Remove the given page from its current page queue.
2608  *
2609  *      The page and page queue must be locked.
2610  */
2611 void
2612 vm_page_dequeue_locked(vm_page_t m)
2613 {
2614         struct vm_pagequeue *pq;
2615
2616         vm_page_lock_assert(m, MA_OWNED);
2617         pq = vm_page_pagequeue(m);
2618         vm_pagequeue_assert_locked(pq);
2619         m->queue = PQ_NONE;
2620         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2621         vm_pagequeue_cnt_dec(pq);
2622 }
2623
2624 /*
2625  *      vm_page_enqueue:
2626  *
2627  *      Add the given page to the specified page queue.
2628  *
2629  *      The page must be locked.
2630  */
2631 static void
2632 vm_page_enqueue(uint8_t queue, vm_page_t m)
2633 {
2634         struct vm_pagequeue *pq;
2635
2636         vm_page_lock_assert(m, MA_OWNED);
2637         KASSERT(queue < PQ_COUNT,
2638             ("vm_page_enqueue: invalid queue %u request for page %p",
2639             queue, m));
2640         if (queue == PQ_LAUNDRY)
2641                 pq = &vm_dom[0].vmd_pagequeues[queue];
2642         else
2643                 pq = &vm_phys_domain(m)->vmd_pagequeues[queue];
2644         vm_pagequeue_lock(pq);
2645         m->queue = queue;
2646         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2647         vm_pagequeue_cnt_inc(pq);
2648         vm_pagequeue_unlock(pq);
2649 }
2650
2651 /*
2652  *      vm_page_requeue:
2653  *
2654  *      Move the given page to the tail of its current page queue.
2655  *
2656  *      The page must be locked.
2657  */
2658 void
2659 vm_page_requeue(vm_page_t m)
2660 {
2661         struct vm_pagequeue *pq;
2662
2663         vm_page_lock_assert(m, MA_OWNED);
2664         KASSERT(m->queue != PQ_NONE,
2665             ("vm_page_requeue: page %p is not queued", m));
2666         pq = vm_page_pagequeue(m);
2667         vm_pagequeue_lock(pq);
2668         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2669         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2670         vm_pagequeue_unlock(pq);
2671 }
2672
2673 /*
2674  *      vm_page_requeue_locked:
2675  *
2676  *      Move the given page to the tail of its current page queue.
2677  *
2678  *      The page queue must be locked.
2679  */
2680 void
2681 vm_page_requeue_locked(vm_page_t m)
2682 {
2683         struct vm_pagequeue *pq;
2684
2685         KASSERT(m->queue != PQ_NONE,
2686             ("vm_page_requeue_locked: page %p is not queued", m));
2687         pq = vm_page_pagequeue(m);
2688         vm_pagequeue_assert_locked(pq);
2689         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2690         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2691 }
2692
2693 /*
2694  *      vm_page_activate:
2695  *
2696  *      Put the specified page on the active list (if appropriate).
2697  *      Ensure that act_count is at least ACT_INIT but do not otherwise
2698  *      mess with it.
2699  *
2700  *      The page must be locked.
2701  */
2702 void
2703 vm_page_activate(vm_page_t m)
2704 {
2705         int queue;
2706
2707         vm_page_lock_assert(m, MA_OWNED);
2708         if ((queue = m->queue) != PQ_ACTIVE) {
2709                 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2710                         if (m->act_count < ACT_INIT)
2711                                 m->act_count = ACT_INIT;
2712                         if (queue != PQ_NONE)
2713                                 vm_page_dequeue(m);
2714                         vm_page_enqueue(PQ_ACTIVE, m);
2715                 } else
2716                         KASSERT(queue == PQ_NONE,
2717                             ("vm_page_activate: wired page %p is queued", m));
2718         } else {
2719                 if (m->act_count < ACT_INIT)
2720                         m->act_count = ACT_INIT;
2721         }
2722 }
2723
2724 /*
2725  *      vm_page_free_wakeup:
2726  *
2727  *      Helper routine for vm_page_free_toq().  This routine is called
2728  *      when a page is added to the free queues.
2729  *
2730  *      The page queues must be locked.
2731  */
2732 static void
2733 vm_page_free_wakeup(void)
2734 {
2735
2736         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2737         /*
2738          * if pageout daemon needs pages, then tell it that there are
2739          * some free.
2740          */
2741         if (vm_pageout_pages_needed &&
2742             vm_cnt.v_free_count >= vm_cnt.v_pageout_free_min) {
2743                 wakeup(&vm_pageout_pages_needed);
2744                 vm_pageout_pages_needed = 0;
2745         }
2746         /*
2747          * wakeup processes that are waiting on memory if we hit a
2748          * high water mark. And wakeup scheduler process if we have
2749          * lots of memory. this process will swapin processes.
2750          */
2751         if (vm_pages_needed && !vm_page_count_min()) {
2752                 vm_pages_needed = false;
2753                 wakeup(&vm_cnt.v_free_count);
2754         }
2755 }
2756
2757 /*
2758  *      vm_page_free_prep:
2759  *
2760  *      Prepares the given page to be put on the free list,
2761  *      disassociating it from any VM object. The caller may return
2762  *      the page to the free list only if this function returns true.
2763  *
2764  *      The object must be locked.  The page must be locked if it is
2765  *      managed.  For a queued managed page, the pagequeue_locked
2766  *      argument specifies whether the page queue is already locked.
2767  */
2768 bool
2769 vm_page_free_prep(vm_page_t m, bool pagequeue_locked)
2770 {
2771
2772 #if defined(DIAGNOSTIC) && defined(PHYS_TO_DMAP)
2773         if ((m->flags & PG_ZERO) != 0) {
2774                 uint64_t *p;
2775                 int i;
2776                 p = (uint64_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
2777                 for (i = 0; i < PAGE_SIZE / sizeof(uint64_t); i++, p++)
2778                         KASSERT(*p == 0, ("vm_page_free_prep %p PG_ZERO %d %jx",
2779                             m, i, (uintmax_t)*p));
2780         }
2781 #endif
2782         if ((m->oflags & VPO_UNMANAGED) == 0) {
2783                 vm_page_lock_assert(m, MA_OWNED);
2784                 KASSERT(!pmap_page_is_mapped(m),
2785                     ("vm_page_free_toq: freeing mapped page %p", m));
2786         } else
2787                 KASSERT(m->queue == PQ_NONE,
2788                     ("vm_page_free_toq: unmanaged page %p is queued", m));
2789         PCPU_INC(cnt.v_tfree);
2790
2791         if (vm_page_sbusied(m))
2792                 panic("vm_page_free: freeing busy page %p", m);
2793
2794         /*
2795          * Unqueue, then remove page.  Note that we cannot destroy
2796          * the page here because we do not want to call the pager's
2797          * callback routine until after we've put the page on the
2798          * appropriate free queue.
2799          */
2800         if (m->queue != PQ_NONE) {
2801                 if (pagequeue_locked)
2802                         vm_page_dequeue_locked(m);
2803                 else
2804                         vm_page_dequeue(m);
2805         }
2806         vm_page_remove(m);
2807
2808         /*
2809          * If fictitious remove object association and
2810          * return, otherwise delay object association removal.
2811          */
2812         if ((m->flags & PG_FICTITIOUS) != 0)
2813                 return (false);
2814
2815         m->valid = 0;
2816         vm_page_undirty(m);
2817
2818         if (m->wire_count != 0)
2819                 panic("vm_page_free: freeing wired page %p", m);
2820         if (m->hold_count != 0) {
2821                 m->flags &= ~PG_ZERO;
2822                 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2823                     ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
2824                 m->flags |= PG_UNHOLDFREE;
2825                 return (false);
2826         }
2827
2828         /*
2829          * Restore the default memory attribute to the page.
2830          */
2831         if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2832                 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2833
2834         return (true);
2835 }
2836
2837 /*
2838  * Insert the page into the physical memory allocator's free page
2839  * queues.  This is the last step to free a page.
2840  */
2841 static void
2842 vm_page_free_phys(vm_page_t m)
2843 {
2844
2845         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2846
2847         vm_phys_freecnt_adj(m, 1);
2848 #if VM_NRESERVLEVEL > 0
2849         if (!vm_reserv_free_page(m))
2850 #endif
2851                         vm_phys_free_pages(m, 0);
2852         if ((m->flags & PG_ZERO) != 0)
2853                 ++vm_page_zero_count;
2854         else
2855                 vm_page_zero_idle_wakeup();
2856 }
2857
2858 void
2859 vm_page_free_phys_pglist(struct pglist *tq)
2860 {
2861         vm_page_t m;
2862
2863         if (TAILQ_EMPTY(tq))
2864                 return;
2865         mtx_lock(&vm_page_queue_free_mtx);
2866         TAILQ_FOREACH(m, tq, listq)
2867                 vm_page_free_phys(m);
2868         vm_page_free_wakeup();
2869         mtx_unlock(&vm_page_queue_free_mtx);
2870 }
2871
2872 /*
2873  *      vm_page_free_toq:
2874  *
2875  *      Returns the given page to the free list, disassociating it
2876  *      from any VM object.
2877  *
2878  *      The object must be locked.  The page must be locked if it is
2879  *      managed.
2880  */
2881 void
2882 vm_page_free_toq(vm_page_t m)
2883 {
2884
2885         if (!vm_page_free_prep(m, false))
2886                 return;
2887         mtx_lock(&vm_page_queue_free_mtx);
2888         vm_page_free_phys(m);
2889         vm_page_free_wakeup();
2890         mtx_unlock(&vm_page_queue_free_mtx);
2891 }
2892
2893 /*
2894  *      vm_page_wire:
2895  *
2896  *      Mark this page as wired down by yet
2897  *      another map, removing it from paging queues
2898  *      as necessary.
2899  *
2900  *      If the page is fictitious, then its wire count must remain one.
2901  *
2902  *      The page must be locked.
2903  */
2904 void
2905 vm_page_wire(vm_page_t m)
2906 {
2907
2908         /*
2909          * Only bump the wire statistics if the page is not already wired,
2910          * and only unqueue the page if it is on some queue (if it is unmanaged
2911          * it is already off the queues).
2912          */
2913         vm_page_lock_assert(m, MA_OWNED);
2914         if ((m->flags & PG_FICTITIOUS) != 0) {
2915                 KASSERT(m->wire_count == 1,
2916                     ("vm_page_wire: fictitious page %p's wire count isn't one",
2917                     m));
2918                 return;
2919         }
2920         if (m->wire_count == 0) {
2921                 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
2922                     m->queue == PQ_NONE,
2923                     ("vm_page_wire: unmanaged page %p is queued", m));
2924                 vm_page_remque(m);
2925                 atomic_add_int(&vm_cnt.v_wire_count, 1);
2926         }
2927         m->wire_count++;
2928         KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
2929 }
2930
2931 /*
2932  * vm_page_unwire:
2933  *
2934  * Release one wiring of the specified page, potentially allowing it to be
2935  * paged out.  Returns TRUE if the number of wirings transitions to zero and
2936  * FALSE otherwise.
2937  *
2938  * Only managed pages belonging to an object can be paged out.  If the number
2939  * of wirings transitions to zero and the page is eligible for page out, then
2940  * the page is added to the specified paging queue (unless PQ_NONE is
2941  * specified).
2942  *
2943  * If a page is fictitious, then its wire count must always be one.
2944  *
2945  * A managed page must be locked.
2946  */
2947 boolean_t
2948 vm_page_unwire(vm_page_t m, uint8_t queue)
2949 {
2950
2951         KASSERT(queue < PQ_COUNT || queue == PQ_NONE,
2952             ("vm_page_unwire: invalid queue %u request for page %p",
2953             queue, m));
2954         if ((m->oflags & VPO_UNMANAGED) == 0)
2955                 vm_page_assert_locked(m);
2956         if ((m->flags & PG_FICTITIOUS) != 0) {
2957                 KASSERT(m->wire_count == 1,
2958             ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2959                 return (FALSE);
2960         }
2961         if (m->wire_count > 0) {
2962                 m->wire_count--;
2963                 if (m->wire_count == 0) {
2964                         atomic_subtract_int(&vm_cnt.v_wire_count, 1);
2965                         if ((m->oflags & VPO_UNMANAGED) == 0 &&
2966                             m->object != NULL && queue != PQ_NONE)
2967                                 vm_page_enqueue(queue, m);
2968                         return (TRUE);
2969                 } else
2970                         return (FALSE);
2971         } else
2972                 panic("vm_page_unwire: page %p's wire count is zero", m);
2973 }
2974
2975 /*
2976  * Move the specified page to the inactive queue.
2977  *
2978  * Normally, "noreuse" is FALSE, resulting in LRU ordering of the inactive
2979  * queue.  However, setting "noreuse" to TRUE will accelerate the specified
2980  * page's reclamation, but it will not unmap the page from any address space.
2981  * This is implemented by inserting the page near the head of the inactive
2982  * queue, using a marker page to guide FIFO insertion ordering.
2983  *
2984  * The page must be locked.
2985  */
2986 static inline void
2987 _vm_page_deactivate(vm_page_t m, boolean_t noreuse)
2988 {
2989         struct vm_pagequeue *pq;
2990         int queue;
2991
2992         vm_page_assert_locked(m);
2993
2994         /*
2995          * Ignore if the page is already inactive, unless it is unlikely to be
2996          * reactivated.
2997          */
2998         if ((queue = m->queue) == PQ_INACTIVE && !noreuse)
2999                 return;
3000         if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
3001                 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE];
3002                 /* Avoid multiple acquisitions of the inactive queue lock. */
3003                 if (queue == PQ_INACTIVE) {
3004                         vm_pagequeue_lock(pq);
3005                         vm_page_dequeue_locked(m);
3006                 } else {
3007                         if (queue != PQ_NONE)
3008                                 vm_page_dequeue(m);
3009                         vm_pagequeue_lock(pq);
3010                 }
3011                 m->queue = PQ_INACTIVE;
3012                 if (noreuse)
3013                         TAILQ_INSERT_BEFORE(&vm_phys_domain(m)->vmd_inacthead,
3014                             m, plinks.q);
3015                 else
3016                         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
3017                 vm_pagequeue_cnt_inc(pq);
3018                 vm_pagequeue_unlock(pq);
3019         }
3020 }
3021
3022 /*
3023  * Move the specified page to the inactive queue.
3024  *
3025  * The page must be locked.
3026  */
3027 void
3028 vm_page_deactivate(vm_page_t m)
3029 {
3030
3031         _vm_page_deactivate(m, FALSE);
3032 }
3033
3034 /*
3035  * Move the specified page to the inactive queue with the expectation
3036  * that it is unlikely to be reused.
3037  *
3038  * The page must be locked.
3039  */
3040 void
3041 vm_page_deactivate_noreuse(vm_page_t m)
3042 {
3043
3044         _vm_page_deactivate(m, TRUE);
3045 }
3046
3047 /*
3048  * vm_page_launder
3049  *
3050  *      Put a page in the laundry.
3051  */
3052 void
3053 vm_page_launder(vm_page_t m)
3054 {
3055         int queue;
3056
3057         vm_page_assert_locked(m);
3058         if ((queue = m->queue) != PQ_LAUNDRY) {
3059                 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
3060                         if (queue != PQ_NONE)
3061                                 vm_page_dequeue(m);
3062                         vm_page_enqueue(PQ_LAUNDRY, m);
3063                 } else
3064                         KASSERT(queue == PQ_NONE,
3065                             ("wired page %p is queued", m));
3066         }
3067 }
3068
3069 /*
3070  * vm_page_try_to_free()
3071  *
3072  *      Attempt to free the page.  If we cannot free it, we do nothing.
3073  *      true is returned on success, false on failure.
3074  */
3075 bool
3076 vm_page_try_to_free(vm_page_t m)
3077 {
3078
3079         vm_page_assert_locked(m);
3080         if (m->object != NULL)
3081                 VM_OBJECT_ASSERT_WLOCKED(m->object);
3082         if (m->dirty != 0 || m->hold_count != 0 || m->wire_count != 0 ||
3083             (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
3084                 return (false);
3085         if (m->object != NULL && m->object->ref_count != 0) {
3086                 pmap_remove_all(m);
3087                 if (m->dirty != 0)
3088                         return (false);
3089         }
3090         vm_page_free(m);
3091         return (true);
3092 }
3093
3094 /*
3095  * vm_page_advise
3096  *
3097  *      Apply the specified advice to the given page.
3098  *
3099  *      The object and page must be locked.
3100  */
3101 void
3102 vm_page_advise(vm_page_t m, int advice)
3103 {
3104
3105         vm_page_assert_locked(m);
3106         VM_OBJECT_ASSERT_WLOCKED(m->object);
3107         if (advice == MADV_FREE)
3108                 /*
3109                  * Mark the page clean.  This will allow the page to be freed
3110                  * without first paging it out.  MADV_FREE pages are often
3111                  * quickly reused by malloc(3), so we do not do anything that
3112                  * would result in a page fault on a later access.
3113                  */
3114                 vm_page_undirty(m);
3115         else if (advice != MADV_DONTNEED) {
3116                 if (advice == MADV_WILLNEED)
3117                         vm_page_activate(m);
3118                 return;
3119         }
3120
3121         /*
3122          * Clear any references to the page.  Otherwise, the page daemon will
3123          * immediately reactivate the page.
3124          */
3125         vm_page_aflag_clear(m, PGA_REFERENCED);
3126
3127         if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
3128                 vm_page_dirty(m);
3129
3130         /*
3131          * Place clean pages near the head of the inactive queue rather than
3132          * the tail, thus defeating the queue's LRU operation and ensuring that
3133          * the page will be reused quickly.  Dirty pages not already in the
3134          * laundry are moved there.
3135          */
3136         if (m->dirty == 0)
3137                 vm_page_deactivate_noreuse(m);
3138         else
3139                 vm_page_launder(m);
3140 }
3141
3142 /*
3143  * Grab a page, waiting until we are waken up due to the page
3144  * changing state.  We keep on waiting, if the page continues
3145  * to be in the object.  If the page doesn't exist, first allocate it
3146  * and then conditionally zero it.
3147  *
3148  * This routine may sleep.
3149  *
3150  * The object must be locked on entry.  The lock will, however, be released
3151  * and reacquired if the routine sleeps.
3152  */
3153 vm_page_t
3154 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
3155 {
3156         vm_page_t m;
3157         int sleep;
3158
3159         VM_OBJECT_ASSERT_WLOCKED(object);
3160         KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
3161             (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
3162             ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
3163 retrylookup:
3164         if ((m = vm_page_lookup(object, pindex)) != NULL) {
3165                 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ?
3166                     vm_page_xbusied(m) : vm_page_busied(m);
3167                 if (sleep) {
3168                         if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3169                                 return (NULL);
3170                         /*
3171                          * Reference the page before unlocking and
3172                          * sleeping so that the page daemon is less
3173                          * likely to reclaim it.
3174                          */
3175                         vm_page_aflag_set(m, PGA_REFERENCED);
3176                         vm_page_lock(m);
3177                         VM_OBJECT_WUNLOCK(object);
3178                         vm_page_busy_sleep(m, "pgrbwt", (allocflags &
3179                             VM_ALLOC_IGN_SBUSY) != 0);
3180                         VM_OBJECT_WLOCK(object);
3181                         goto retrylookup;
3182                 } else {
3183                         if ((allocflags & VM_ALLOC_WIRED) != 0) {
3184                                 vm_page_lock(m);
3185                                 vm_page_wire(m);
3186                                 vm_page_unlock(m);
3187                         }
3188                         if ((allocflags &
3189                             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
3190                                 vm_page_xbusy(m);
3191                         if ((allocflags & VM_ALLOC_SBUSY) != 0)
3192                                 vm_page_sbusy(m);
3193                         return (m);
3194                 }
3195         }
3196         m = vm_page_alloc(object, pindex, allocflags);
3197         if (m == NULL) {
3198                 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3199                         return (NULL);
3200                 VM_OBJECT_WUNLOCK(object);
3201                 VM_WAIT;
3202                 VM_OBJECT_WLOCK(object);
3203                 goto retrylookup;
3204         }
3205         if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
3206                 pmap_zero_page(m);
3207         return (m);
3208 }
3209
3210 /*
3211  * Return the specified range of pages from the given object.  For each
3212  * page offset within the range, if a page already exists within the object
3213  * at that offset and it is busy, then wait for it to change state.  If,
3214  * instead, the page doesn't exist, then allocate it.
3215  *
3216  * The caller must always specify an allocation class.
3217  *
3218  * allocation classes:
3219  *      VM_ALLOC_NORMAL         normal process request
3220  *      VM_ALLOC_SYSTEM         system *really* needs the pages
3221  *
3222  * The caller must always specify that the pages are to be busied and/or
3223  * wired.
3224  *
3225  * optional allocation flags:
3226  *      VM_ALLOC_IGN_SBUSY      do not sleep on soft busy pages
3227  *      VM_ALLOC_NOBUSY         do not exclusive busy the page
3228  *      VM_ALLOC_NOWAIT         do not sleep
3229  *      VM_ALLOC_SBUSY          set page to sbusy state
3230  *      VM_ALLOC_WIRED          wire the pages
3231  *      VM_ALLOC_ZERO           zero and validate any invalid pages
3232  *
3233  * If VM_ALLOC_NOWAIT is not specified, this routine may sleep.  Otherwise, it
3234  * may return a partial prefix of the requested range.
3235  */
3236 int
3237 vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags,
3238     vm_page_t *ma, int count)
3239 {
3240         vm_page_t m;
3241         int i;
3242         bool sleep;
3243
3244         VM_OBJECT_ASSERT_WLOCKED(object);
3245         KASSERT(((u_int)allocflags >> VM_ALLOC_COUNT_SHIFT) == 0,
3246             ("vm_page_grap_pages: VM_ALLOC_COUNT() is not allowed"));
3247         KASSERT((allocflags & VM_ALLOC_NOBUSY) == 0 ||
3248             (allocflags & VM_ALLOC_WIRED) != 0,
3249             ("vm_page_grab_pages: the pages must be busied or wired"));
3250         KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
3251             (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
3252             ("vm_page_grab_pages: VM_ALLOC_SBUSY/IGN_SBUSY mismatch"));
3253         if (count == 0)
3254                 return (0);
3255         i = 0;
3256 retrylookup:
3257         m = vm_page_lookup(object, pindex + i);
3258         for (; i < count; i++) {
3259                 if (m != NULL) {
3260                         sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ?
3261                             vm_page_xbusied(m) : vm_page_busied(m);
3262                         if (sleep) {
3263                                 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3264                                         break;
3265                                 /*
3266                                  * Reference the page before unlocking and
3267                                  * sleeping so that the page daemon is less
3268                                  * likely to reclaim it.
3269                                  */
3270                                 vm_page_aflag_set(m, PGA_REFERENCED);
3271                                 vm_page_lock(m);
3272                                 VM_OBJECT_WUNLOCK(object);
3273                                 vm_page_busy_sleep(m, "grbmaw", (allocflags &
3274                                     VM_ALLOC_IGN_SBUSY) != 0);
3275                                 VM_OBJECT_WLOCK(object);
3276                                 goto retrylookup;
3277                         }
3278                         if ((allocflags & VM_ALLOC_WIRED) != 0) {
3279                                 vm_page_lock(m);
3280                                 vm_page_wire(m);
3281                                 vm_page_unlock(m);
3282                         }
3283                         if ((allocflags & (VM_ALLOC_NOBUSY |
3284                             VM_ALLOC_SBUSY)) == 0)
3285                                 vm_page_xbusy(m);
3286                         if ((allocflags & VM_ALLOC_SBUSY) != 0)
3287                                 vm_page_sbusy(m);
3288                 } else {
3289                         m = vm_page_alloc(object, pindex + i, (allocflags &
3290                             ~VM_ALLOC_IGN_SBUSY) | VM_ALLOC_COUNT(count - i));
3291                         if (m == NULL) {
3292                                 if ((allocflags & VM_ALLOC_NOWAIT) != 0)
3293                                         break;
3294                                 VM_OBJECT_WUNLOCK(object);
3295                                 VM_WAIT;
3296                                 VM_OBJECT_WLOCK(object);
3297                                 goto retrylookup;
3298                         }
3299                 }
3300                 if (m->valid == 0 && (allocflags & VM_ALLOC_ZERO) != 0) {
3301                         if ((m->flags & PG_ZERO) == 0)
3302                                 pmap_zero_page(m);
3303                         m->valid = VM_PAGE_BITS_ALL;
3304                 }
3305                 ma[i] = m;
3306                 m = vm_page_next(m);
3307         }
3308         return (i);
3309 }
3310
3311 /*
3312  * Mapping function for valid or dirty bits in a page.
3313  *
3314  * Inputs are required to range within a page.
3315  */
3316 vm_page_bits_t
3317 vm_page_bits(int base, int size)
3318 {
3319         int first_bit;
3320         int last_bit;
3321
3322         KASSERT(
3323             base + size <= PAGE_SIZE,
3324             ("vm_page_bits: illegal base/size %d/%d", base, size)
3325         );
3326
3327         if (size == 0)          /* handle degenerate case */
3328                 return (0);
3329
3330         first_bit = base >> DEV_BSHIFT;
3331         last_bit = (base + size - 1) >> DEV_BSHIFT;
3332
3333         return (((vm_page_bits_t)2 << last_bit) -
3334             ((vm_page_bits_t)1 << first_bit));
3335 }
3336
3337 /*
3338  *      vm_page_set_valid_range:
3339  *
3340  *      Sets portions of a page valid.  The arguments are expected
3341  *      to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3342  *      of any partial chunks touched by the range.  The invalid portion of
3343  *      such chunks will be zeroed.
3344  *
3345  *      (base + size) must be less then or equal to PAGE_SIZE.
3346  */
3347 void
3348 vm_page_set_valid_range(vm_page_t m, int base, int size)
3349 {
3350         int endoff, frag;
3351
3352         VM_OBJECT_ASSERT_WLOCKED(m->object);
3353         if (size == 0)  /* handle degenerate case */
3354                 return;
3355
3356         /*
3357          * If the base is not DEV_BSIZE aligned and the valid
3358          * bit is clear, we have to zero out a portion of the
3359          * first block.
3360          */
3361         if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3362             (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
3363                 pmap_zero_page_area(m, frag, base - frag);
3364
3365         /*
3366          * If the ending offset is not DEV_BSIZE aligned and the
3367          * valid bit is clear, we have to zero out a portion of
3368          * the last block.
3369          */
3370         endoff = base + size;
3371         if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3372             (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
3373                 pmap_zero_page_area(m, endoff,
3374                     DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3375
3376         /*
3377          * Assert that no previously invalid block that is now being validated
3378          * is already dirty.
3379          */
3380         KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
3381             ("vm_page_set_valid_range: page %p is dirty", m));
3382
3383         /*
3384          * Set valid bits inclusive of any overlap.
3385          */
3386         m->valid |= vm_page_bits(base, size);
3387 }
3388
3389 /*
3390  * Clear the given bits from the specified page's dirty field.
3391  */
3392 static __inline void
3393 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
3394 {
3395         uintptr_t addr;
3396 #if PAGE_SIZE < 16384
3397         int shift;
3398 #endif
3399
3400         /*
3401          * If the object is locked and the page is neither exclusive busy nor
3402          * write mapped, then the page's dirty field cannot possibly be
3403          * set by a concurrent pmap operation.
3404          */
3405         VM_OBJECT_ASSERT_WLOCKED(m->object);
3406         if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
3407                 m->dirty &= ~pagebits;
3408         else {
3409                 /*
3410                  * The pmap layer can call vm_page_dirty() without
3411                  * holding a distinguished lock.  The combination of
3412                  * the object's lock and an atomic operation suffice
3413                  * to guarantee consistency of the page dirty field.
3414                  *
3415                  * For PAGE_SIZE == 32768 case, compiler already
3416                  * properly aligns the dirty field, so no forcible
3417                  * alignment is needed. Only require existence of
3418                  * atomic_clear_64 when page size is 32768.
3419                  */
3420                 addr = (uintptr_t)&m->dirty;
3421 #if PAGE_SIZE == 32768
3422                 atomic_clear_64((uint64_t *)addr, pagebits);
3423 #elif PAGE_SIZE == 16384
3424                 atomic_clear_32((uint32_t *)addr, pagebits);
3425 #else           /* PAGE_SIZE <= 8192 */
3426                 /*
3427                  * Use a trick to perform a 32-bit atomic on the
3428                  * containing aligned word, to not depend on the existence
3429                  * of atomic_clear_{8, 16}.
3430                  */
3431                 shift = addr & (sizeof(uint32_t) - 1);
3432 #if BYTE_ORDER == BIG_ENDIAN
3433                 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
3434 #else
3435                 shift *= NBBY;
3436 #endif
3437                 addr &= ~(sizeof(uint32_t) - 1);
3438                 atomic_clear_32((uint32_t *)addr, pagebits << shift);
3439 #endif          /* PAGE_SIZE */
3440         }
3441 }
3442
3443 /*
3444  *      vm_page_set_validclean:
3445  *
3446  *      Sets portions of a page valid and clean.  The arguments are expected
3447  *      to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
3448  *      of any partial chunks touched by the range.  The invalid portion of
3449  *      such chunks will be zero'd.
3450  *
3451  *      (base + size) must be less then or equal to PAGE_SIZE.
3452  */
3453 void
3454 vm_page_set_validclean(vm_page_t m, int base, int size)
3455 {
3456         vm_page_bits_t oldvalid, pagebits;
3457         int endoff, frag;
3458
3459         VM_OBJECT_ASSERT_WLOCKED(m->object);
3460         if (size == 0)  /* handle degenerate case */
3461                 return;
3462
3463         /*
3464          * If the base is not DEV_BSIZE aligned and the valid
3465          * bit is clear, we have to zero out a portion of the
3466          * first block.
3467          */
3468         if ((frag = rounddown2(base, DEV_BSIZE)) != base &&
3469             (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
3470                 pmap_zero_page_area(m, frag, base - frag);
3471
3472         /*
3473          * If the ending offset is not DEV_BSIZE aligned and the
3474          * valid bit is clear, we have to zero out a portion of
3475          * the last block.
3476          */
3477         endoff = base + size;
3478         if ((frag = rounddown2(endoff, DEV_BSIZE)) != endoff &&
3479             (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
3480                 pmap_zero_page_area(m, endoff,
3481                     DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
3482
3483         /*
3484          * Set valid, clear dirty bits.  If validating the entire
3485          * page we can safely clear the pmap modify bit.  We also
3486          * use this opportunity to clear the VPO_NOSYNC flag.  If a process
3487          * takes a write fault on a MAP_NOSYNC memory area the flag will
3488          * be set again.
3489          *
3490          * We set valid bits inclusive of any overlap, but we can only
3491          * clear dirty bits for DEV_BSIZE chunks that are fully within
3492          * the range.
3493          */
3494         oldvalid = m->valid;
3495         pagebits = vm_page_bits(base, size);
3496         m->valid |= pagebits;
3497 #if 0   /* NOT YET */
3498         if ((frag = base & (DEV_BSIZE - 1)) != 0) {
3499                 frag = DEV_BSIZE - frag;
3500                 base += frag;
3501                 size -= frag;
3502                 if (size < 0)
3503                         size = 0;
3504         }
3505         pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
3506 #endif
3507         if (base == 0 && size == PAGE_SIZE) {
3508                 /*
3509                  * The page can only be modified within the pmap if it is
3510                  * mapped, and it can only be mapped if it was previously
3511                  * fully valid.
3512                  */
3513                 if (oldvalid == VM_PAGE_BITS_ALL)
3514                         /*
3515                          * Perform the pmap_clear_modify() first.  Otherwise,
3516                          * a concurrent pmap operation, such as
3517                          * pmap_protect(), could clear a modification in the
3518                          * pmap and set the dirty field on the page before
3519                          * pmap_clear_modify() had begun and after the dirty
3520                          * field was cleared here.
3521                          */
3522                         pmap_clear_modify(m);
3523                 m->dirty = 0;
3524                 m->oflags &= ~VPO_NOSYNC;
3525         } else if (oldvalid != VM_PAGE_BITS_ALL)
3526                 m->dirty &= ~pagebits;
3527         else
3528                 vm_page_clear_dirty_mask(m, pagebits);
3529 }
3530
3531 void
3532 vm_page_clear_dirty(vm_page_t m, int base, int size)
3533 {
3534
3535         vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
3536 }
3537
3538 /*
3539  *      vm_page_set_invalid:
3540  *
3541  *      Invalidates DEV_BSIZE'd chunks within a page.  Both the
3542  *      valid and dirty bits for the effected areas are cleared.
3543  */
3544 void
3545 vm_page_set_invalid(vm_page_t m, int base, int size)
3546 {
3547         vm_page_bits_t bits;
3548         vm_object_t object;
3549
3550         object = m->object;
3551         VM_OBJECT_ASSERT_WLOCKED(object);
3552         if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
3553             size >= object->un_pager.vnp.vnp_size)
3554                 bits = VM_PAGE_BITS_ALL;
3555         else
3556                 bits = vm_page_bits(base, size);
3557         if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL &&
3558             bits != 0)
3559                 pmap_remove_all(m);
3560         KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) ||
3561             !pmap_page_is_mapped(m),
3562             ("vm_page_set_invalid: page %p is mapped", m));
3563         m->valid &= ~bits;
3564         m->dirty &= ~bits;
3565 }
3566
3567 /*
3568  * vm_page_zero_invalid()
3569  *
3570  *      The kernel assumes that the invalid portions of a page contain
3571  *      garbage, but such pages can be mapped into memory by user code.
3572  *      When this occurs, we must zero out the non-valid portions of the
3573  *      page so user code sees what it expects.
3574  *
3575  *      Pages are most often semi-valid when the end of a file is mapped
3576  *      into memory and the file's size is not page aligned.
3577  */
3578 void
3579 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
3580 {
3581         int b;
3582         int i;
3583
3584         VM_OBJECT_ASSERT_WLOCKED(m->object);
3585         /*
3586          * Scan the valid bits looking for invalid sections that
3587          * must be zeroed.  Invalid sub-DEV_BSIZE'd areas ( where the
3588          * valid bit may be set ) have already been zeroed by
3589          * vm_page_set_validclean().
3590          */
3591         for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
3592                 if (i == (PAGE_SIZE / DEV_BSIZE) ||
3593                     (m->valid & ((vm_page_bits_t)1 << i))) {
3594                         if (i > b) {
3595                                 pmap_zero_page_area(m,
3596                                     b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
3597                         }
3598                         b = i + 1;
3599                 }
3600         }
3601
3602         /*
3603          * setvalid is TRUE when we can safely set the zero'd areas
3604          * as being valid.  We can do this if there are no cache consistancy
3605          * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
3606          */
3607         if (setvalid)
3608                 m->valid = VM_PAGE_BITS_ALL;
3609 }
3610
3611 /*
3612  *      vm_page_is_valid:
3613  *
3614  *      Is (partial) page valid?  Note that the case where size == 0
3615  *      will return FALSE in the degenerate case where the page is
3616  *      entirely invalid, and TRUE otherwise.
3617  */
3618 int
3619 vm_page_is_valid(vm_page_t m, int base, int size)
3620 {
3621         vm_page_bits_t bits;
3622
3623         VM_OBJECT_ASSERT_LOCKED(m->object);
3624         bits = vm_page_bits(base, size);
3625         return (m->valid != 0 && (m->valid & bits) == bits);
3626 }
3627
3628 /*
3629  * Returns true if all of the specified predicates are true for the entire
3630  * (super)page and false otherwise.
3631  */
3632 bool
3633 vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m)
3634 {
3635         vm_object_t object;
3636         int i, npages;
3637
3638         object = m->object;
3639         VM_OBJECT_ASSERT_LOCKED(object);
3640         npages = atop(pagesizes[m->psind]);
3641
3642         /*
3643          * The physically contiguous pages that make up a superpage, i.e., a
3644          * page with a page size index ("psind") greater than zero, will
3645          * occupy adjacent entries in vm_page_array[].
3646          */
3647         for (i = 0; i < npages; i++) {
3648                 /* Always test object consistency, including "skip_m". */
3649                 if (m[i].object != object)
3650                         return (false);
3651                 if (&m[i] == skip_m)
3652                         continue;
3653                 if ((flags & PS_NONE_BUSY) != 0 && vm_page_busied(&m[i]))
3654                         return (false);
3655                 if ((flags & PS_ALL_DIRTY) != 0) {
3656                         /*
3657                          * Calling vm_page_test_dirty() or pmap_is_modified()
3658                          * might stop this case from spuriously returning
3659                          * "false".  However, that would require a write lock
3660                          * on the object containing "m[i]".
3661                          */
3662                         if (m[i].dirty != VM_PAGE_BITS_ALL)
3663                                 return (false);
3664                 }
3665                 if ((flags & PS_ALL_VALID) != 0 &&
3666                     m[i].valid != VM_PAGE_BITS_ALL)
3667                         return (false);
3668         }
3669         return (true);
3670 }
3671
3672 /*
3673  * Set the page's dirty bits if the page is modified.
3674  */
3675 void
3676 vm_page_test_dirty(vm_page_t m)
3677 {
3678
3679         VM_OBJECT_ASSERT_WLOCKED(m->object);
3680         if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
3681                 vm_page_dirty(m);
3682 }
3683
3684 void
3685 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
3686 {
3687
3688         mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
3689 }
3690
3691 void
3692 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
3693 {
3694
3695         mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
3696 }
3697
3698 int
3699 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
3700 {
3701
3702         return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
3703 }
3704
3705 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
3706 void
3707 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
3708 {
3709
3710         vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
3711 }
3712
3713 void
3714 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
3715 {
3716
3717         mtx_assert_(vm_page_lockptr(m), a, file, line);
3718 }
3719 #endif
3720
3721 #ifdef INVARIANTS
3722 void
3723 vm_page_object_lock_assert(vm_page_t m)
3724 {
3725
3726         /*
3727          * Certain of the page's fields may only be modified by the
3728          * holder of the containing object's lock or the exclusive busy.
3729          * holder.  Unfortunately, the holder of the write busy is
3730          * not recorded, and thus cannot be checked here.
3731          */
3732         if (m->object != NULL && !vm_page_xbusied(m))
3733                 VM_OBJECT_ASSERT_WLOCKED(m->object);
3734 }
3735
3736 void
3737 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
3738 {
3739
3740         if ((bits & PGA_WRITEABLE) == 0)
3741                 return;
3742
3743         /*
3744          * The PGA_WRITEABLE flag can only be set if the page is
3745          * managed, is exclusively busied or the object is locked.
3746          * Currently, this flag is only set by pmap_enter().
3747          */
3748         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3749             ("PGA_WRITEABLE on unmanaged page"));
3750         if (!vm_page_xbusied(m))
3751                 VM_OBJECT_ASSERT_LOCKED(m->object);
3752 }
3753 #endif
3754
3755 #include "opt_ddb.h"
3756 #ifdef DDB
3757 #include <sys/kernel.h>
3758
3759 #include <ddb/ddb.h>
3760
3761 DB_SHOW_COMMAND(page, vm_page_print_page_info)
3762 {
3763
3764         db_printf("vm_cnt.v_free_count: %d\n", vm_cnt.v_free_count);
3765         db_printf("vm_cnt.v_inactive_count: %d\n", vm_cnt.v_inactive_count);
3766         db_printf("vm_cnt.v_active_count: %d\n", vm_cnt.v_active_count);
3767         db_printf("vm_cnt.v_laundry_count: %d\n", vm_cnt.v_laundry_count);
3768         db_printf("vm_cnt.v_wire_count: %d\n", vm_cnt.v_wire_count);
3769         db_printf("vm_cnt.v_free_reserved: %d\n", vm_cnt.v_free_reserved);
3770         db_printf("vm_cnt.v_free_min: %d\n", vm_cnt.v_free_min);
3771         db_printf("vm_cnt.v_free_target: %d\n", vm_cnt.v_free_target);
3772         db_printf("vm_cnt.v_inactive_target: %d\n", vm_cnt.v_inactive_target);
3773 }
3774
3775 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
3776 {
3777         int dom;
3778
3779         db_printf("pq_free %d\n", vm_cnt.v_free_count);
3780         for (dom = 0; dom < vm_ndomains; dom++) {
3781                 db_printf(
3782             "dom %d page_cnt %d free %d pq_act %d pq_inact %d pq_laund %d\n",
3783                     dom,
3784                     vm_dom[dom].vmd_page_count,
3785                     vm_dom[dom].vmd_free_count,
3786                     vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
3787                     vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
3788                     vm_dom[dom].vmd_pagequeues[PQ_LAUNDRY].pq_cnt);
3789         }
3790 }
3791
3792 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
3793 {
3794         vm_page_t m;
3795         boolean_t phys;
3796
3797         if (!have_addr) {
3798                 db_printf("show pginfo addr\n");
3799                 return;
3800         }
3801
3802         phys = strchr(modif, 'p') != NULL;
3803         if (phys)
3804                 m = PHYS_TO_VM_PAGE(addr);
3805         else
3806                 m = (vm_page_t)addr;
3807         db_printf(
3808     "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
3809     "  af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
3810             m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
3811             m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
3812             m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);
3813 }
3814 #endif /* DDB */