]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/vm/vm_page.c
MFC r308733:
[FreeBSD/stable/10.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/malloc.h>
95 #include <sys/mman.h>
96 #include <sys/msgbuf.h>
97 #include <sys/mutex.h>
98 #include <sys/proc.h>
99 #include <sys/rwlock.h>
100 #include <sys/sysctl.h>
101 #include <sys/vmmeter.h>
102 #include <sys/vnode.h>
103
104 #include <vm/vm.h>
105 #include <vm/pmap.h>
106 #include <vm/vm_param.h>
107 #include <vm/vm_kern.h>
108 #include <vm/vm_object.h>
109 #include <vm/vm_page.h>
110 #include <vm/vm_pageout.h>
111 #include <vm/vm_pager.h>
112 #include <vm/vm_phys.h>
113 #include <vm/vm_radix.h>
114 #include <vm/vm_reserv.h>
115 #include <vm/vm_extern.h>
116 #include <vm/uma.h>
117 #include <vm/uma_int.h>
118
119 #include <machine/md_var.h>
120
121 /*
122  *      Associated with page of user-allocatable memory is a
123  *      page structure.
124  */
125
126 struct vm_domain vm_dom[MAXMEMDOM];
127 struct mtx_padalign vm_page_queue_free_mtx;
128
129 struct mtx_padalign pa_lock[PA_LOCK_COUNT];
130
131 vm_page_t vm_page_array;
132 long vm_page_array_size;
133 long first_page;
134 int vm_page_zero_count;
135
136 static int boot_pages = UMA_BOOT_PAGES;
137 TUNABLE_INT("vm.boot_pages", &boot_pages);
138 SYSCTL_INT(_vm, OID_AUTO, boot_pages, CTLFLAG_RD, &boot_pages, 0,
139         "number of pages allocated for bootstrapping the VM system");
140
141 static int pa_tryrelock_restart;
142 SYSCTL_INT(_vm, OID_AUTO, tryrelock_restart, CTLFLAG_RD,
143     &pa_tryrelock_restart, 0, "Number of tryrelock restarts");
144
145 static uma_zone_t fakepg_zone;
146
147 static struct vnode *vm_page_alloc_init(vm_page_t m);
148 static void vm_page_cache_turn_free(vm_page_t m);
149 static void vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits);
150 static void vm_page_enqueue(int queue, vm_page_t m);
151 static void vm_page_init_fakepg(void *dummy);
152 static int vm_page_insert_after(vm_page_t m, vm_object_t object,
153     vm_pindex_t pindex, vm_page_t mpred);
154 static void vm_page_insert_radixdone(vm_page_t m, vm_object_t object,
155     vm_page_t mpred);
156
157 SYSINIT(vm_page, SI_SUB_VM, SI_ORDER_SECOND, vm_page_init_fakepg, NULL);
158
159 static void
160 vm_page_init_fakepg(void *dummy)
161 {
162
163         fakepg_zone = uma_zcreate("fakepg", sizeof(struct vm_page), NULL, NULL,
164             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM); 
165 }
166
167 /* Make sure that u_long is at least 64 bits when PAGE_SIZE is 32K. */
168 #if PAGE_SIZE == 32768
169 #ifdef CTASSERT
170 CTASSERT(sizeof(u_long) >= 8);
171 #endif
172 #endif
173
174 /*
175  * Try to acquire a physical address lock while a pmap is locked.  If we
176  * fail to trylock we unlock and lock the pmap directly and cache the
177  * locked pa in *locked.  The caller should then restart their loop in case
178  * the virtual to physical mapping has changed.
179  */
180 int
181 vm_page_pa_tryrelock(pmap_t pmap, vm_paddr_t pa, vm_paddr_t *locked)
182 {
183         vm_paddr_t lockpa;
184
185         lockpa = *locked;
186         *locked = pa;
187         if (lockpa) {
188                 PA_LOCK_ASSERT(lockpa, MA_OWNED);
189                 if (PA_LOCKPTR(pa) == PA_LOCKPTR(lockpa))
190                         return (0);
191                 PA_UNLOCK(lockpa);
192         }
193         if (PA_TRYLOCK(pa))
194                 return (0);
195         PMAP_UNLOCK(pmap);
196         atomic_add_int(&pa_tryrelock_restart, 1);
197         PA_LOCK(pa);
198         PMAP_LOCK(pmap);
199         return (EAGAIN);
200 }
201
202 /*
203  *      vm_set_page_size:
204  *
205  *      Sets the page size, perhaps based upon the memory
206  *      size.  Must be called before any use of page-size
207  *      dependent functions.
208  */
209 void
210 vm_set_page_size(void)
211 {
212         if (cnt.v_page_size == 0)
213                 cnt.v_page_size = PAGE_SIZE;
214         if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0)
215                 panic("vm_set_page_size: page size not a power of two");
216 }
217
218 /*
219  *      vm_page_blacklist_lookup:
220  *
221  *      See if a physical address in this page has been listed
222  *      in the blacklist tunable.  Entries in the tunable are
223  *      separated by spaces or commas.  If an invalid integer is
224  *      encountered then the rest of the string is skipped.
225  */
226 static int
227 vm_page_blacklist_lookup(char *list, vm_paddr_t pa)
228 {
229         vm_paddr_t bad;
230         char *cp, *pos;
231
232         for (pos = list; *pos != '\0'; pos = cp) {
233                 bad = strtoq(pos, &cp, 0);
234                 if (*cp != '\0') {
235                         if (*cp == ' ' || *cp == ',') {
236                                 cp++;
237                                 if (cp == pos)
238                                         continue;
239                         } else
240                                 break;
241                 }
242                 if (pa == trunc_page(bad))
243                         return (1);
244         }
245         return (0);
246 }
247
248 static void
249 vm_page_domain_init(struct vm_domain *vmd)
250 {
251         struct vm_pagequeue *pq;
252         int i;
253
254         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_name) =
255             "vm inactive pagequeue";
256         *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_INACTIVE].pq_vcnt) =
257             &cnt.v_inactive_count;
258         *__DECONST(char **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_name) =
259             "vm active pagequeue";
260         *__DECONST(u_int **, &vmd->vmd_pagequeues[PQ_ACTIVE].pq_vcnt) =
261             &cnt.v_active_count;
262         vmd->vmd_page_count = 0;
263         vmd->vmd_free_count = 0;
264         vmd->vmd_segs = 0;
265         vmd->vmd_oom = FALSE;
266         vmd->vmd_pass = 0;
267         for (i = 0; i < PQ_COUNT; i++) {
268                 pq = &vmd->vmd_pagequeues[i];
269                 TAILQ_INIT(&pq->pq_pl);
270                 mtx_init(&pq->pq_mutex, pq->pq_name, "vm pagequeue",
271                     MTX_DEF | MTX_DUPOK);
272         }
273 }
274
275 /*
276  *      vm_page_startup:
277  *
278  *      Initializes the resident memory module.
279  *
280  *      Allocates memory for the page cells, and
281  *      for the object/offset-to-page hash table headers.
282  *      Each page cell is initialized and placed on the free list.
283  */
284 vm_offset_t
285 vm_page_startup(vm_offset_t vaddr)
286 {
287         vm_offset_t mapped;
288         vm_paddr_t page_range;
289         vm_paddr_t new_end;
290         int i;
291         vm_paddr_t pa;
292         vm_paddr_t last_pa;
293         char *list;
294
295         /* the biggest memory array is the second group of pages */
296         vm_paddr_t end;
297         vm_paddr_t biggestsize;
298         vm_paddr_t low_water, high_water;
299         int biggestone;
300
301         biggestsize = 0;
302         biggestone = 0;
303         vaddr = round_page(vaddr);
304
305         for (i = 0; phys_avail[i + 1]; i += 2) {
306                 phys_avail[i] = round_page(phys_avail[i]);
307                 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
308         }
309
310 #ifdef XEN
311         /*
312          * There is no obvious reason why i386 PV Xen needs vm_page structs
313          * created for these pseudo-physical addresses.  XXX
314          */
315         vm_phys_add_seg(0, phys_avail[0]);
316 #endif
317
318         low_water = phys_avail[0];
319         high_water = phys_avail[1];
320
321         for (i = 0; i < vm_phys_nsegs; i++) {
322                 if (vm_phys_segs[i].start < low_water)
323                         low_water = vm_phys_segs[i].start;
324                 if (vm_phys_segs[i].end > high_water)
325                         high_water = vm_phys_segs[i].end;
326         }
327         for (i = 0; phys_avail[i + 1]; i += 2) {
328                 vm_paddr_t size = phys_avail[i + 1] - phys_avail[i];
329
330                 if (size > biggestsize) {
331                         biggestone = i;
332                         biggestsize = size;
333                 }
334                 if (phys_avail[i] < low_water)
335                         low_water = phys_avail[i];
336                 if (phys_avail[i + 1] > high_water)
337                         high_water = phys_avail[i + 1];
338         }
339
340         end = phys_avail[biggestone+1];
341
342         /*
343          * Initialize the page and queue locks.
344          */
345         mtx_init(&vm_page_queue_free_mtx, "vm page free queue", NULL, MTX_DEF);
346         for (i = 0; i < PA_LOCK_COUNT; i++)
347                 mtx_init(&pa_lock[i], "vm page", NULL, MTX_DEF);
348         for (i = 0; i < vm_ndomains; i++)
349                 vm_page_domain_init(&vm_dom[i]);
350
351         /*
352          * Allocate memory for use when boot strapping the kernel memory
353          * allocator.
354          */
355         new_end = end - (boot_pages * UMA_SLAB_SIZE);
356         new_end = trunc_page(new_end);
357         mapped = pmap_map(&vaddr, new_end, end,
358             VM_PROT_READ | VM_PROT_WRITE);
359         bzero((void *)mapped, end - new_end);
360         uma_startup((void *)mapped, boot_pages);
361
362 #if defined(__amd64__) || defined(__i386__) || defined(__arm__) || \
363     defined(__mips__)
364         /*
365          * Allocate a bitmap to indicate that a random physical page
366          * needs to be included in a minidump.
367          *
368          * The amd64 port needs this to indicate which direct map pages
369          * need to be dumped, via calls to dump_add_page()/dump_drop_page().
370          *
371          * However, i386 still needs this workspace internally within the
372          * minidump code.  In theory, they are not needed on i386, but are
373          * included should the sf_buf code decide to use them.
374          */
375         last_pa = 0;
376         for (i = 0; dump_avail[i + 1] != 0; i += 2)
377                 if (dump_avail[i + 1] > last_pa)
378                         last_pa = dump_avail[i + 1];
379         page_range = last_pa / PAGE_SIZE;
380         vm_page_dump_size = round_page(roundup2(page_range, NBBY) / NBBY);
381         new_end -= vm_page_dump_size;
382         vm_page_dump = (void *)(uintptr_t)pmap_map(&vaddr, new_end,
383             new_end + vm_page_dump_size, VM_PROT_READ | VM_PROT_WRITE);
384         bzero((void *)vm_page_dump, vm_page_dump_size);
385 #endif
386 #ifdef __amd64__
387         /*
388          * Request that the physical pages underlying the message buffer be
389          * included in a crash dump.  Since the message buffer is accessed
390          * through the direct map, they are not automatically included.
391          */
392         pa = DMAP_TO_PHYS((vm_offset_t)msgbufp->msg_ptr);
393         last_pa = pa + round_page(msgbufsize);
394         while (pa < last_pa) {
395                 dump_add_page(pa);
396                 pa += PAGE_SIZE;
397         }
398 #endif
399         /*
400          * Compute the number of pages of memory that will be available for
401          * use (taking into account the overhead of a page structure per
402          * page).
403          */
404         first_page = low_water / PAGE_SIZE;
405 #ifdef VM_PHYSSEG_SPARSE
406         page_range = 0;
407         for (i = 0; i < vm_phys_nsegs; i++) {
408                 page_range += atop(vm_phys_segs[i].end -
409                     vm_phys_segs[i].start);
410         }
411         for (i = 0; phys_avail[i + 1] != 0; i += 2)
412                 page_range += atop(phys_avail[i + 1] - phys_avail[i]);
413 #elif defined(VM_PHYSSEG_DENSE)
414         page_range = high_water / PAGE_SIZE - first_page;
415 #else
416 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
417 #endif
418         end = new_end;
419
420         /*
421          * Reserve an unmapped guard page to trap access to vm_page_array[-1].
422          */
423         vaddr += PAGE_SIZE;
424
425         /*
426          * Initialize the mem entry structures now, and put them in the free
427          * queue.
428          */
429         new_end = trunc_page(end - page_range * sizeof(struct vm_page));
430         mapped = pmap_map(&vaddr, new_end, end,
431             VM_PROT_READ | VM_PROT_WRITE);
432         vm_page_array = (vm_page_t) mapped;
433 #if VM_NRESERVLEVEL > 0
434         /*
435          * Allocate memory for the reservation management system's data
436          * structures.
437          */
438         new_end = vm_reserv_startup(&vaddr, new_end, high_water);
439 #endif
440 #if defined(__amd64__) || defined(__mips__)
441         /*
442          * pmap_map on amd64 and mips can come out of the direct-map, not kvm
443          * like i386, so the pages must be tracked for a crashdump to include
444          * this data.  This includes the vm_page_array and the early UMA
445          * bootstrap pages.
446          */
447         for (pa = new_end; pa < phys_avail[biggestone + 1]; pa += PAGE_SIZE)
448                 dump_add_page(pa);
449 #endif  
450         phys_avail[biggestone + 1] = new_end;
451
452         /*
453          * Add physical memory segments corresponding to the available
454          * physical pages.
455          */
456         for (i = 0; phys_avail[i + 1] != 0; i += 2)
457                 vm_phys_add_seg(phys_avail[i], phys_avail[i + 1]);
458
459         /*
460          * Clear all of the page structures
461          */
462         bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
463         for (i = 0; i < page_range; i++)
464                 vm_page_array[i].order = VM_NFREEORDER;
465         vm_page_array_size = page_range;
466
467         /*
468          * Initialize the physical memory allocator.
469          */
470         vm_phys_init();
471
472         /*
473          * Add every available physical page that is not blacklisted to
474          * the free lists.
475          */
476         cnt.v_page_count = 0;
477         cnt.v_free_count = 0;
478         list = getenv("vm.blacklist");
479         for (i = 0; phys_avail[i + 1] != 0; i += 2) {
480                 pa = phys_avail[i];
481                 last_pa = phys_avail[i + 1];
482                 while (pa < last_pa) {
483                         if (list != NULL &&
484                             vm_page_blacklist_lookup(list, pa))
485                                 printf("Skipping page with pa 0x%jx\n",
486                                     (uintmax_t)pa);
487                         else
488                                 vm_phys_add_page(pa);
489                         pa += PAGE_SIZE;
490                 }
491         }
492         freeenv(list);
493 #if VM_NRESERVLEVEL > 0
494         /*
495          * Initialize the reservation management system.
496          */
497         vm_reserv_init();
498 #endif
499         return (vaddr);
500 }
501
502 void
503 vm_page_reference(vm_page_t m)
504 {
505
506         vm_page_aflag_set(m, PGA_REFERENCED);
507 }
508
509 /*
510  *      vm_page_busy_downgrade:
511  *
512  *      Downgrade an exclusive busy page into a single shared busy page.
513  */
514 void
515 vm_page_busy_downgrade(vm_page_t m)
516 {
517         u_int x;
518         bool locked;
519
520         vm_page_assert_xbusied(m);
521         locked = mtx_owned(vm_page_lockptr(m));
522
523         for (;;) {
524                 x = m->busy_lock;
525                 x &= VPB_BIT_WAITERS;
526                 if (x != 0 && !locked)
527                         vm_page_lock(m);
528                 if (atomic_cmpset_rel_int(&m->busy_lock,
529                     VPB_SINGLE_EXCLUSIVER | x, VPB_SHARERS_WORD(1)))
530                         break;
531                 if (x != 0 && !locked)
532                         vm_page_unlock(m);
533         }
534         if (x != 0) {
535                 wakeup(m);
536                 if (!locked)
537                         vm_page_unlock(m);
538         }
539 }
540
541 /*
542  *      vm_page_sbusied:
543  *
544  *      Return a positive value if the page is shared busied, 0 otherwise.
545  */
546 int
547 vm_page_sbusied(vm_page_t m)
548 {
549         u_int x;
550
551         x = m->busy_lock;
552         return ((x & VPB_BIT_SHARED) != 0 && x != VPB_UNBUSIED);
553 }
554
555 /*
556  *      vm_page_sunbusy:
557  *
558  *      Shared unbusy a page.
559  */
560 void
561 vm_page_sunbusy(vm_page_t m)
562 {
563         u_int x;
564
565         vm_page_assert_sbusied(m);
566
567         for (;;) {
568                 x = m->busy_lock;
569                 if (VPB_SHARERS(x) > 1) {
570                         if (atomic_cmpset_int(&m->busy_lock, x,
571                             x - VPB_ONE_SHARER))
572                                 break;
573                         continue;
574                 }
575                 if ((x & VPB_BIT_WAITERS) == 0) {
576                         KASSERT(x == VPB_SHARERS_WORD(1),
577                             ("vm_page_sunbusy: invalid lock state"));
578                         if (atomic_cmpset_int(&m->busy_lock,
579                             VPB_SHARERS_WORD(1), VPB_UNBUSIED))
580                                 break;
581                         continue;
582                 }
583                 KASSERT(x == (VPB_SHARERS_WORD(1) | VPB_BIT_WAITERS),
584                     ("vm_page_sunbusy: invalid lock state for waiters"));
585
586                 vm_page_lock(m);
587                 if (!atomic_cmpset_int(&m->busy_lock, x, VPB_UNBUSIED)) {
588                         vm_page_unlock(m);
589                         continue;
590                 }
591                 wakeup(m);
592                 vm_page_unlock(m);
593                 break;
594         }
595 }
596
597 /*
598  *      vm_page_busy_sleep:
599  *
600  *      Sleep and release the page lock, using the page pointer as wchan.
601  *      This is used to implement the hard-path of busying mechanism.
602  *
603  *      The given page must be locked.
604  *
605  *      If nonshared is true, sleep only if the page is xbusy.
606  */
607 void
608 vm_page_busy_sleep(vm_page_t m, const char *wmesg, bool nonshared)
609 {
610         u_int x;
611
612         vm_page_assert_locked(m);
613
614         x = m->busy_lock;
615         if (x == VPB_UNBUSIED || (nonshared && (x & VPB_BIT_SHARED) != 0) ||
616             ((x & VPB_BIT_WAITERS) == 0 &&
617             !atomic_cmpset_int(&m->busy_lock, x, x | VPB_BIT_WAITERS))) {
618                 vm_page_unlock(m);
619                 return;
620         }
621         msleep(m, vm_page_lockptr(m), PVM | PDROP, wmesg, 0);
622 }
623
624 /*
625  *      vm_page_trysbusy:
626  *
627  *      Try to shared busy a page.
628  *      If the operation succeeds 1 is returned otherwise 0.
629  *      The operation never sleeps.
630  */
631 int
632 vm_page_trysbusy(vm_page_t m)
633 {
634         u_int x;
635
636         for (;;) {
637                 x = m->busy_lock;
638                 if ((x & VPB_BIT_SHARED) == 0)
639                         return (0);
640                 if (atomic_cmpset_acq_int(&m->busy_lock, x, x + VPB_ONE_SHARER))
641                         return (1);
642         }
643 }
644
645 /*
646  *      vm_page_xunbusy_hard:
647  *
648  *      Called after the first try the exclusive unbusy of a page failed.
649  *      It is assumed that the waiters bit is on.
650  */
651 void
652 vm_page_xunbusy_hard(vm_page_t m)
653 {
654
655         vm_page_assert_xbusied(m);
656
657         vm_page_lock(m);
658         atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
659         wakeup(m);
660         vm_page_unlock(m);
661 }
662
663 /*
664  *      vm_page_flash:
665  *
666  *      Wakeup anyone waiting for the page.
667  *      The ownership bits do not change.
668  *
669  *      The given page must be locked.
670  */
671 void
672 vm_page_flash(vm_page_t m)
673 {
674         u_int x;
675
676         vm_page_lock_assert(m, MA_OWNED);
677
678         for (;;) {
679                 x = m->busy_lock;
680                 if ((x & VPB_BIT_WAITERS) == 0)
681                         return;
682                 if (atomic_cmpset_int(&m->busy_lock, x,
683                     x & (~VPB_BIT_WAITERS)))
684                         break;
685         }
686         wakeup(m);
687 }
688
689 /*
690  * Keep page from being freed by the page daemon
691  * much of the same effect as wiring, except much lower
692  * overhead and should be used only for *very* temporary
693  * holding ("wiring").
694  */
695 void
696 vm_page_hold(vm_page_t mem)
697 {
698
699         vm_page_lock_assert(mem, MA_OWNED);
700         mem->hold_count++;
701 }
702
703 void
704 vm_page_unhold(vm_page_t mem)
705 {
706
707         vm_page_lock_assert(mem, MA_OWNED);
708         KASSERT(mem->hold_count >= 1, ("vm_page_unhold: hold count < 0!!!"));
709         --mem->hold_count;
710         if (mem->hold_count == 0 && (mem->flags & PG_UNHOLDFREE) != 0)
711                 vm_page_free_toq(mem);
712 }
713
714 /*
715  *      vm_page_unhold_pages:
716  *
717  *      Unhold each of the pages that is referenced by the given array.
718  */ 
719 void
720 vm_page_unhold_pages(vm_page_t *ma, int count)
721 {
722         struct mtx *mtx, *new_mtx;
723
724         mtx = NULL;
725         for (; count != 0; count--) {
726                 /*
727                  * Avoid releasing and reacquiring the same page lock.
728                  */
729                 new_mtx = vm_page_lockptr(*ma);
730                 if (mtx != new_mtx) {
731                         if (mtx != NULL)
732                                 mtx_unlock(mtx);
733                         mtx = new_mtx;
734                         mtx_lock(mtx);
735                 }
736                 vm_page_unhold(*ma);
737                 ma++;
738         }
739         if (mtx != NULL)
740                 mtx_unlock(mtx);
741 }
742
743 vm_page_t
744 PHYS_TO_VM_PAGE(vm_paddr_t pa)
745 {
746         vm_page_t m;
747
748 #ifdef VM_PHYSSEG_SPARSE
749         m = vm_phys_paddr_to_vm_page(pa);
750         if (m == NULL)
751                 m = vm_phys_fictitious_to_vm_page(pa);
752         return (m);
753 #elif defined(VM_PHYSSEG_DENSE)
754         long pi;
755
756         pi = atop(pa);
757         if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
758                 m = &vm_page_array[pi - first_page];
759                 return (m);
760         }
761         return (vm_phys_fictitious_to_vm_page(pa));
762 #else
763 #error "Either VM_PHYSSEG_DENSE or VM_PHYSSEG_SPARSE must be defined."
764 #endif
765 }
766
767 /*
768  *      vm_page_getfake:
769  *
770  *      Create a fictitious page with the specified physical address and
771  *      memory attribute.  The memory attribute is the only the machine-
772  *      dependent aspect of a fictitious page that must be initialized.
773  */
774 vm_page_t
775 vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
776 {
777         vm_page_t m;
778
779         m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
780         vm_page_initfake(m, paddr, memattr);
781         return (m);
782 }
783
784 void
785 vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
786 {
787
788         if ((m->flags & PG_FICTITIOUS) != 0) {
789                 /*
790                  * The page's memattr might have changed since the
791                  * previous initialization.  Update the pmap to the
792                  * new memattr.
793                  */
794                 goto memattr;
795         }
796         m->phys_addr = paddr;
797         m->queue = PQ_NONE;
798         /* Fictitious pages don't use "segind". */
799         m->flags = PG_FICTITIOUS;
800         /* Fictitious pages don't use "order" or "pool". */
801         m->oflags = VPO_UNMANAGED;
802         m->busy_lock = VPB_SINGLE_EXCLUSIVER;
803         m->wire_count = 1;
804         pmap_page_init(m);
805 memattr:
806         pmap_page_set_memattr(m, memattr);
807 }
808
809 /*
810  *      vm_page_putfake:
811  *
812  *      Release a fictitious page.
813  */
814 void
815 vm_page_putfake(vm_page_t m)
816 {
817
818         KASSERT((m->oflags & VPO_UNMANAGED) != 0, ("managed %p", m));
819         KASSERT((m->flags & PG_FICTITIOUS) != 0,
820             ("vm_page_putfake: bad page %p", m));
821         uma_zfree(fakepg_zone, m);
822 }
823
824 /*
825  *      vm_page_updatefake:
826  *
827  *      Update the given fictitious page to the specified physical address and
828  *      memory attribute.
829  */
830 void
831 vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr)
832 {
833
834         KASSERT((m->flags & PG_FICTITIOUS) != 0,
835             ("vm_page_updatefake: bad page %p", m));
836         m->phys_addr = paddr;
837         pmap_page_set_memattr(m, memattr);
838 }
839
840 /*
841  *      vm_page_free:
842  *
843  *      Free a page.
844  */
845 void
846 vm_page_free(vm_page_t m)
847 {
848
849         m->flags &= ~PG_ZERO;
850         vm_page_free_toq(m);
851 }
852
853 /*
854  *      vm_page_free_zero:
855  *
856  *      Free a page to the zerod-pages queue
857  */
858 void
859 vm_page_free_zero(vm_page_t m)
860 {
861
862         m->flags |= PG_ZERO;
863         vm_page_free_toq(m);
864 }
865
866 /*
867  * Unbusy and handle the page queueing for a page from the VOP_GETPAGES()
868  * array which is not the request page.
869  */
870 void
871 vm_page_readahead_finish(vm_page_t m)
872 {
873
874         if (m->valid != 0) {
875                 /*
876                  * Since the page is not the requested page, whether
877                  * it should be activated or deactivated is not
878                  * obvious.  Empirical results have shown that
879                  * deactivating the page is usually the best choice,
880                  * unless the page is wanted by another thread.
881                  */
882                 vm_page_lock(m);
883                 if ((m->busy_lock & VPB_BIT_WAITERS) != 0)
884                         vm_page_activate(m);
885                 else
886                         vm_page_deactivate(m);
887                 vm_page_unlock(m);
888                 vm_page_xunbusy(m);
889         } else {
890                 /*
891                  * Free the completely invalid page.  Such page state
892                  * occurs due to the short read operation which did
893                  * not covered our page at all, or in case when a read
894                  * error happens.
895                  */
896                 vm_page_lock(m);
897                 vm_page_free(m);
898                 vm_page_unlock(m);
899         }
900 }
901
902 /*
903  *      vm_page_sleep_if_busy:
904  *
905  *      Sleep and release the page queues lock if the page is busied.
906  *      Returns TRUE if the thread slept.
907  *
908  *      The given page must be unlocked and object containing it must
909  *      be locked.
910  */
911 int
912 vm_page_sleep_if_busy(vm_page_t m, const char *msg)
913 {
914         vm_object_t obj;
915
916         vm_page_lock_assert(m, MA_NOTOWNED);
917         VM_OBJECT_ASSERT_WLOCKED(m->object);
918
919         if (vm_page_busied(m)) {
920                 /*
921                  * The page-specific object must be cached because page
922                  * identity can change during the sleep, causing the
923                  * re-lock of a different object.
924                  * It is assumed that a reference to the object is already
925                  * held by the callers.
926                  */
927                 obj = m->object;
928                 vm_page_lock(m);
929                 VM_OBJECT_WUNLOCK(obj);
930                 vm_page_busy_sleep(m, msg, false);
931                 VM_OBJECT_WLOCK(obj);
932                 return (TRUE);
933         }
934         return (FALSE);
935 }
936
937 /*
938  *      vm_page_dirty_KBI:              [ internal use only ]
939  *
940  *      Set all bits in the page's dirty field.
941  *
942  *      The object containing the specified page must be locked if the
943  *      call is made from the machine-independent layer.
944  *
945  *      See vm_page_clear_dirty_mask().
946  *
947  *      This function should only be called by vm_page_dirty().
948  */
949 void
950 vm_page_dirty_KBI(vm_page_t m)
951 {
952
953         /* These assertions refer to this operation by its public name. */
954         KASSERT((m->flags & PG_CACHED) == 0,
955             ("vm_page_dirty: page in cache!"));
956         KASSERT(!VM_PAGE_IS_FREE(m),
957             ("vm_page_dirty: page is free!"));
958         KASSERT(m->valid == VM_PAGE_BITS_ALL,
959             ("vm_page_dirty: page is invalid!"));
960         m->dirty = VM_PAGE_BITS_ALL;
961 }
962
963 /*
964  *      vm_page_insert:         [ internal use only ]
965  *
966  *      Inserts the given mem entry into the object and object list.
967  *
968  *      The object must be locked.
969  */
970 int
971 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex)
972 {
973         vm_page_t mpred;
974
975         VM_OBJECT_ASSERT_WLOCKED(object);
976         mpred = vm_radix_lookup_le(&object->rtree, pindex);
977         return (vm_page_insert_after(m, object, pindex, mpred));
978 }
979
980 /*
981  *      vm_page_insert_after:
982  *
983  *      Inserts the page "m" into the specified object at offset "pindex".
984  *
985  *      The page "mpred" must immediately precede the offset "pindex" within
986  *      the specified object.
987  *
988  *      The object must be locked.
989  */
990 static int
991 vm_page_insert_after(vm_page_t m, vm_object_t object, vm_pindex_t pindex,
992     vm_page_t mpred)
993 {
994         vm_page_t msucc;
995
996         VM_OBJECT_ASSERT_WLOCKED(object);
997         KASSERT(m->object == NULL,
998             ("vm_page_insert_after: page already inserted"));
999         if (mpred != NULL) {
1000                 KASSERT(mpred->object == object,
1001                     ("vm_page_insert_after: object doesn't contain mpred"));
1002                 KASSERT(mpred->pindex < pindex,
1003                     ("vm_page_insert_after: mpred doesn't precede pindex"));
1004                 msucc = TAILQ_NEXT(mpred, listq);
1005         } else
1006                 msucc = TAILQ_FIRST(&object->memq);
1007         if (msucc != NULL)
1008                 KASSERT(msucc->pindex > pindex,
1009                     ("vm_page_insert_after: msucc doesn't succeed pindex"));
1010
1011         /*
1012          * Record the object/offset pair in this page
1013          */
1014         m->object = object;
1015         m->pindex = pindex;
1016
1017         /*
1018          * Now link into the object's ordered list of backed pages.
1019          */
1020         if (vm_radix_insert(&object->rtree, m)) {
1021                 m->object = NULL;
1022                 m->pindex = 0;
1023                 return (1);
1024         }
1025         vm_page_insert_radixdone(m, object, mpred);
1026         return (0);
1027 }
1028
1029 /*
1030  *      vm_page_insert_radixdone:
1031  *
1032  *      Complete page "m" insertion into the specified object after the
1033  *      radix trie hooking.
1034  *
1035  *      The page "mpred" must precede the offset "m->pindex" within the
1036  *      specified object.
1037  *
1038  *      The object must be locked.
1039  */
1040 static void
1041 vm_page_insert_radixdone(vm_page_t m, vm_object_t object, vm_page_t mpred)
1042 {
1043
1044         VM_OBJECT_ASSERT_WLOCKED(object);
1045         KASSERT(object != NULL && m->object == object,
1046             ("vm_page_insert_radixdone: page %p has inconsistent object", m));
1047         if (mpred != NULL) {
1048                 KASSERT(mpred->object == object,
1049                     ("vm_page_insert_after: object doesn't contain mpred"));
1050                 KASSERT(mpred->pindex < m->pindex,
1051                     ("vm_page_insert_after: mpred doesn't precede pindex"));
1052         }
1053
1054         if (mpred != NULL)
1055                 TAILQ_INSERT_AFTER(&object->memq, mpred, m, listq);
1056         else
1057                 TAILQ_INSERT_HEAD(&object->memq, m, listq);
1058
1059         /*
1060          * Show that the object has one more resident page.
1061          */
1062         object->resident_page_count++;
1063
1064         /*
1065          * Hold the vnode until the last page is released.
1066          */
1067         if (object->resident_page_count == 1 && object->type == OBJT_VNODE)
1068                 vhold(object->handle);
1069
1070         /*
1071          * Since we are inserting a new and possibly dirty page,
1072          * update the object's OBJ_MIGHTBEDIRTY flag.
1073          */
1074         if (pmap_page_is_write_mapped(m))
1075                 vm_object_set_writeable_dirty(object);
1076 }
1077
1078 /*
1079  *      vm_page_remove:
1080  *
1081  *      Removes the given mem entry from the object/offset-page
1082  *      table and the object page list, but do not invalidate/terminate
1083  *      the backing store.
1084  *
1085  *      The object must be locked.  The page must be locked if it is managed.
1086  */
1087 void
1088 vm_page_remove(vm_page_t m)
1089 {
1090         vm_object_t object;
1091         boolean_t lockacq;
1092
1093         if ((m->oflags & VPO_UNMANAGED) == 0)
1094                 vm_page_lock_assert(m, MA_OWNED);
1095         if ((object = m->object) == NULL)
1096                 return;
1097         VM_OBJECT_ASSERT_WLOCKED(object);
1098         if (vm_page_xbusied(m)) {
1099                 lockacq = FALSE;
1100                 if ((m->oflags & VPO_UNMANAGED) != 0 &&
1101                     !mtx_owned(vm_page_lockptr(m))) {
1102                         lockacq = TRUE;
1103                         vm_page_lock(m);
1104                 }
1105                 vm_page_flash(m);
1106                 atomic_store_rel_int(&m->busy_lock, VPB_UNBUSIED);
1107                 if (lockacq)
1108                         vm_page_unlock(m);
1109         }
1110
1111         /*
1112          * Now remove from the object's list of backed pages.
1113          */
1114         vm_radix_remove(&object->rtree, m->pindex);
1115         TAILQ_REMOVE(&object->memq, m, listq);
1116
1117         /*
1118          * And show that the object has one fewer resident page.
1119          */
1120         object->resident_page_count--;
1121
1122         /*
1123          * The vnode may now be recycled.
1124          */
1125         if (object->resident_page_count == 0 && object->type == OBJT_VNODE)
1126                 vdrop(object->handle);
1127
1128         m->object = NULL;
1129 }
1130
1131 /*
1132  *      vm_page_lookup:
1133  *
1134  *      Returns the page associated with the object/offset
1135  *      pair specified; if none is found, NULL is returned.
1136  *
1137  *      The object must be locked.
1138  */
1139 vm_page_t
1140 vm_page_lookup(vm_object_t object, vm_pindex_t pindex)
1141 {
1142
1143         VM_OBJECT_ASSERT_LOCKED(object);
1144         return (vm_radix_lookup(&object->rtree, pindex));
1145 }
1146
1147 /*
1148  *      vm_page_find_least:
1149  *
1150  *      Returns the page associated with the object with least pindex
1151  *      greater than or equal to the parameter pindex, or NULL.
1152  *
1153  *      The object must be locked.
1154  */
1155 vm_page_t
1156 vm_page_find_least(vm_object_t object, vm_pindex_t pindex)
1157 {
1158         vm_page_t m;
1159
1160         VM_OBJECT_ASSERT_LOCKED(object);
1161         if ((m = TAILQ_FIRST(&object->memq)) != NULL && m->pindex < pindex)
1162                 m = vm_radix_lookup_ge(&object->rtree, pindex);
1163         return (m);
1164 }
1165
1166 /*
1167  * Returns the given page's successor (by pindex) within the object if it is
1168  * resident; if none is found, NULL is returned.
1169  *
1170  * The object must be locked.
1171  */
1172 vm_page_t
1173 vm_page_next(vm_page_t m)
1174 {
1175         vm_page_t next;
1176
1177         VM_OBJECT_ASSERT_WLOCKED(m->object);
1178         if ((next = TAILQ_NEXT(m, listq)) != NULL &&
1179             next->pindex != m->pindex + 1)
1180                 next = NULL;
1181         return (next);
1182 }
1183
1184 /*
1185  * Returns the given page's predecessor (by pindex) within the object if it is
1186  * resident; if none is found, NULL is returned.
1187  *
1188  * The object must be locked.
1189  */
1190 vm_page_t
1191 vm_page_prev(vm_page_t m)
1192 {
1193         vm_page_t prev;
1194
1195         VM_OBJECT_ASSERT_WLOCKED(m->object);
1196         if ((prev = TAILQ_PREV(m, pglist, listq)) != NULL &&
1197             prev->pindex != m->pindex - 1)
1198                 prev = NULL;
1199         return (prev);
1200 }
1201
1202 /*
1203  * Uses the page mnew as a replacement for an existing page at index
1204  * pindex which must be already present in the object.
1205  *
1206  * The existing page must not be on a paging queue.
1207  */
1208 vm_page_t
1209 vm_page_replace(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex)
1210 {
1211         vm_page_t mold, mpred;
1212
1213         VM_OBJECT_ASSERT_WLOCKED(object);
1214
1215         /*
1216          * This function mostly follows vm_page_insert() and
1217          * vm_page_remove() without the radix, object count and vnode
1218          * dance.  Double check such functions for more comments.
1219          */
1220         mpred = vm_radix_lookup(&object->rtree, pindex);
1221         KASSERT(mpred != NULL,
1222             ("vm_page_replace: replacing page not present with pindex"));
1223         mpred = TAILQ_PREV(mpred, respgs, listq);
1224         if (mpred != NULL)
1225                 KASSERT(mpred->pindex < pindex,
1226                     ("vm_page_insert_after: mpred doesn't precede pindex"));
1227
1228         mnew->object = object;
1229         mnew->pindex = pindex;
1230         mold = vm_radix_replace(&object->rtree, mnew);
1231         KASSERT(mold->queue == PQ_NONE,
1232             ("vm_page_replace: mold is on a paging queue"));
1233
1234         /* Detach the old page from the resident tailq. */
1235         TAILQ_REMOVE(&object->memq, mold, listq);
1236
1237         mold->object = NULL;
1238         vm_page_xunbusy(mold);
1239
1240         /* Insert the new page in the resident tailq. */
1241         if (mpred != NULL)
1242                 TAILQ_INSERT_AFTER(&object->memq, mpred, mnew, listq);
1243         else
1244                 TAILQ_INSERT_HEAD(&object->memq, mnew, listq);
1245         if (pmap_page_is_write_mapped(mnew))
1246                 vm_object_set_writeable_dirty(object);
1247         return (mold);
1248 }
1249
1250 /*
1251  *      vm_page_rename:
1252  *
1253  *      Move the given memory entry from its
1254  *      current object to the specified target object/offset.
1255  *
1256  *      Note: swap associated with the page must be invalidated by the move.  We
1257  *            have to do this for several reasons:  (1) we aren't freeing the
1258  *            page, (2) we are dirtying the page, (3) the VM system is probably
1259  *            moving the page from object A to B, and will then later move
1260  *            the backing store from A to B and we can't have a conflict.
1261  *
1262  *      Note: we *always* dirty the page.  It is necessary both for the
1263  *            fact that we moved it, and because we may be invalidating
1264  *            swap.  If the page is on the cache, we have to deactivate it
1265  *            or vm_page_dirty() will panic.  Dirty pages are not allowed
1266  *            on the cache.
1267  *
1268  *      The objects must be locked.
1269  */
1270 int
1271 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex)
1272 {
1273         vm_page_t mpred;
1274         vm_pindex_t opidx;
1275
1276         VM_OBJECT_ASSERT_WLOCKED(new_object);
1277
1278         mpred = vm_radix_lookup_le(&new_object->rtree, new_pindex);
1279         KASSERT(mpred == NULL || mpred->pindex != new_pindex,
1280             ("vm_page_rename: pindex already renamed"));
1281
1282         /*
1283          * Create a custom version of vm_page_insert() which does not depend
1284          * by m_prev and can cheat on the implementation aspects of the
1285          * function.
1286          */
1287         opidx = m->pindex;
1288         m->pindex = new_pindex;
1289         if (vm_radix_insert(&new_object->rtree, m)) {
1290                 m->pindex = opidx;
1291                 return (1);
1292         }
1293
1294         /*
1295          * The operation cannot fail anymore.  The removal must happen before
1296          * the listq iterator is tainted.
1297          */
1298         m->pindex = opidx;
1299         vm_page_lock(m);
1300         vm_page_remove(m);
1301
1302         /* Return back to the new pindex to complete vm_page_insert(). */
1303         m->pindex = new_pindex;
1304         m->object = new_object;
1305         vm_page_unlock(m);
1306         vm_page_insert_radixdone(m, new_object, mpred);
1307         vm_page_dirty(m);
1308         return (0);
1309 }
1310
1311 /*
1312  *      Convert all of the given object's cached pages that have a
1313  *      pindex within the given range into free pages.  If the value
1314  *      zero is given for "end", then the range's upper bound is
1315  *      infinity.  If the given object is backed by a vnode and it
1316  *      transitions from having one or more cached pages to none, the
1317  *      vnode's hold count is reduced. 
1318  */
1319 void
1320 vm_page_cache_free(vm_object_t object, vm_pindex_t start, vm_pindex_t end)
1321 {
1322         vm_page_t m;
1323         boolean_t empty;
1324
1325         mtx_lock(&vm_page_queue_free_mtx);
1326         if (__predict_false(vm_radix_is_empty(&object->cache))) {
1327                 mtx_unlock(&vm_page_queue_free_mtx);
1328                 return;
1329         }
1330         while ((m = vm_radix_lookup_ge(&object->cache, start)) != NULL) {
1331                 if (end != 0 && m->pindex >= end)
1332                         break;
1333                 vm_radix_remove(&object->cache, m->pindex);
1334                 vm_page_cache_turn_free(m);
1335         }
1336         empty = vm_radix_is_empty(&object->cache);
1337         mtx_unlock(&vm_page_queue_free_mtx);
1338         if (object->type == OBJT_VNODE && empty)
1339                 vdrop(object->handle);
1340 }
1341
1342 /*
1343  *      Returns the cached page that is associated with the given
1344  *      object and offset.  If, however, none exists, returns NULL.
1345  *
1346  *      The free page queue must be locked.
1347  */
1348 static inline vm_page_t
1349 vm_page_cache_lookup(vm_object_t object, vm_pindex_t pindex)
1350 {
1351
1352         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1353         return (vm_radix_lookup(&object->cache, pindex));
1354 }
1355
1356 /*
1357  *      Remove the given cached page from its containing object's
1358  *      collection of cached pages.
1359  *
1360  *      The free page queue must be locked.
1361  */
1362 static void
1363 vm_page_cache_remove(vm_page_t m)
1364 {
1365
1366         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1367         KASSERT((m->flags & PG_CACHED) != 0,
1368             ("vm_page_cache_remove: page %p is not cached", m));
1369         vm_radix_remove(&m->object->cache, m->pindex);
1370         m->object = NULL;
1371         cnt.v_cache_count--;
1372 }
1373
1374 /*
1375  *      Transfer all of the cached pages with offset greater than or
1376  *      equal to 'offidxstart' from the original object's cache to the
1377  *      new object's cache.  However, any cached pages with offset
1378  *      greater than or equal to the new object's size are kept in the
1379  *      original object.  Initially, the new object's cache must be
1380  *      empty.  Offset 'offidxstart' in the original object must
1381  *      correspond to offset zero in the new object.
1382  *
1383  *      The new object must be locked.
1384  */
1385 void
1386 vm_page_cache_transfer(vm_object_t orig_object, vm_pindex_t offidxstart,
1387     vm_object_t new_object)
1388 {
1389         vm_page_t m;
1390
1391         /*
1392          * Insertion into an object's collection of cached pages
1393          * requires the object to be locked.  In contrast, removal does
1394          * not.
1395          */
1396         VM_OBJECT_ASSERT_WLOCKED(new_object);
1397         KASSERT(vm_radix_is_empty(&new_object->cache),
1398             ("vm_page_cache_transfer: object %p has cached pages",
1399             new_object));
1400         mtx_lock(&vm_page_queue_free_mtx);
1401         while ((m = vm_radix_lookup_ge(&orig_object->cache,
1402             offidxstart)) != NULL) {
1403                 /*
1404                  * Transfer all of the pages with offset greater than or
1405                  * equal to 'offidxstart' from the original object's
1406                  * cache to the new object's cache.
1407                  */
1408                 if ((m->pindex - offidxstart) >= new_object->size)
1409                         break;
1410                 vm_radix_remove(&orig_object->cache, m->pindex);
1411                 /* Update the page's object and offset. */
1412                 m->object = new_object;
1413                 m->pindex -= offidxstart;
1414                 if (vm_radix_insert(&new_object->cache, m))
1415                         vm_page_cache_turn_free(m);
1416         }
1417         mtx_unlock(&vm_page_queue_free_mtx);
1418 }
1419
1420 /*
1421  *      Returns TRUE if a cached page is associated with the given object and
1422  *      offset, and FALSE otherwise.
1423  *
1424  *      The object must be locked.
1425  */
1426 boolean_t
1427 vm_page_is_cached(vm_object_t object, vm_pindex_t pindex)
1428 {
1429         vm_page_t m;
1430
1431         /*
1432          * Insertion into an object's collection of cached pages requires the
1433          * object to be locked.  Therefore, if the object is locked and the
1434          * object's collection is empty, there is no need to acquire the free
1435          * page queues lock in order to prove that the specified page doesn't
1436          * exist.
1437          */
1438         VM_OBJECT_ASSERT_WLOCKED(object);
1439         if (__predict_true(vm_object_cache_is_empty(object)))
1440                 return (FALSE);
1441         mtx_lock(&vm_page_queue_free_mtx);
1442         m = vm_page_cache_lookup(object, pindex);
1443         mtx_unlock(&vm_page_queue_free_mtx);
1444         return (m != NULL);
1445 }
1446
1447 /*
1448  *      vm_page_alloc:
1449  *
1450  *      Allocate and return a page that is associated with the specified
1451  *      object and offset pair.  By default, this page is exclusive busied.
1452  *
1453  *      The caller must always specify an allocation class.
1454  *
1455  *      allocation classes:
1456  *      VM_ALLOC_NORMAL         normal process request
1457  *      VM_ALLOC_SYSTEM         system *really* needs a page
1458  *      VM_ALLOC_INTERRUPT      interrupt time request
1459  *
1460  *      optional allocation flags:
1461  *      VM_ALLOC_COUNT(number)  the number of additional pages that the caller
1462  *                              intends to allocate
1463  *      VM_ALLOC_IFCACHED       return page only if it is cached
1464  *      VM_ALLOC_IFNOTCACHED    return NULL, do not reactivate if the page
1465  *                              is cached
1466  *      VM_ALLOC_NOBUSY         do not exclusive busy the page
1467  *      VM_ALLOC_NODUMP         do not include the page in a kernel core dump
1468  *      VM_ALLOC_NOOBJ          page is not associated with an object and
1469  *                              should not be exclusive busy 
1470  *      VM_ALLOC_SBUSY          shared busy the allocated page
1471  *      VM_ALLOC_WIRED          wire the allocated page
1472  *      VM_ALLOC_ZERO           prefer a zeroed page
1473  *
1474  *      This routine may not sleep.
1475  */
1476 vm_page_t
1477 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int req)
1478 {
1479         struct vnode *vp = NULL;
1480         vm_object_t m_object;
1481         vm_page_t m, mpred;
1482         int flags, req_class;
1483
1484         mpred = 0;      /* XXX: pacify gcc */
1485         KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1486             (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1487             ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1488             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1489             ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object,
1490             req));
1491         if (object != NULL)
1492                 VM_OBJECT_ASSERT_WLOCKED(object);
1493
1494         req_class = req & VM_ALLOC_CLASS_MASK;
1495
1496         /*
1497          * The page daemon is allowed to dig deeper into the free page list.
1498          */
1499         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1500                 req_class = VM_ALLOC_SYSTEM;
1501
1502         if (object != NULL) {
1503                 mpred = vm_radix_lookup_le(&object->rtree, pindex);
1504                 KASSERT(mpred == NULL || mpred->pindex != pindex,
1505                    ("vm_page_alloc: pindex already allocated"));
1506         }
1507
1508         /*
1509          * The page allocation request can came from consumers which already
1510          * hold the free page queue mutex, like vm_page_insert() in
1511          * vm_page_cache().
1512          */
1513         mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE);
1514         if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1515             (req_class == VM_ALLOC_SYSTEM &&
1516             cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1517             (req_class == VM_ALLOC_INTERRUPT &&
1518             cnt.v_free_count + cnt.v_cache_count > 0)) {
1519                 /*
1520                  * Allocate from the free queue if the number of free pages
1521                  * exceeds the minimum for the request class.
1522                  */
1523                 if (object != NULL &&
1524                     (m = vm_page_cache_lookup(object, pindex)) != NULL) {
1525                         if ((req & VM_ALLOC_IFNOTCACHED) != 0) {
1526                                 mtx_unlock(&vm_page_queue_free_mtx);
1527                                 return (NULL);
1528                         }
1529                         if (vm_phys_unfree_page(m))
1530                                 vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, 0);
1531 #if VM_NRESERVLEVEL > 0
1532                         else if (!vm_reserv_reactivate_page(m))
1533 #else
1534                         else
1535 #endif
1536                                 panic("vm_page_alloc: cache page %p is missing"
1537                                     " from the free queue", m);
1538                 } else if ((req & VM_ALLOC_IFCACHED) != 0) {
1539                         mtx_unlock(&vm_page_queue_free_mtx);
1540                         return (NULL);
1541 #if VM_NRESERVLEVEL > 0
1542                 } else if (object == NULL || (object->flags & (OBJ_COLORED |
1543                     OBJ_FICTITIOUS)) != OBJ_COLORED || (m =
1544                     vm_reserv_alloc_page(object, pindex, mpred)) == NULL) {
1545 #else
1546                 } else {
1547 #endif
1548                         m = vm_phys_alloc_pages(object != NULL ?
1549                             VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT, 0);
1550 #if VM_NRESERVLEVEL > 0
1551                         if (m == NULL && vm_reserv_reclaim_inactive()) {
1552                                 m = vm_phys_alloc_pages(object != NULL ?
1553                                     VM_FREEPOOL_DEFAULT : VM_FREEPOOL_DIRECT,
1554                                     0);
1555                         }
1556 #endif
1557                 }
1558         } else {
1559                 /*
1560                  * Not allocatable, give up.
1561                  */
1562                 mtx_unlock(&vm_page_queue_free_mtx);
1563                 atomic_add_int(&vm_pageout_deficit,
1564                     max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1565                 pagedaemon_wakeup();
1566                 return (NULL);
1567         }
1568
1569         /*
1570          *  At this point we had better have found a good page.
1571          */
1572         KASSERT(m != NULL, ("vm_page_alloc: missing page"));
1573         KASSERT(m->queue == PQ_NONE,
1574             ("vm_page_alloc: page %p has unexpected queue %d", m, m->queue));
1575         KASSERT(m->wire_count == 0, ("vm_page_alloc: page %p is wired", m));
1576         KASSERT(m->hold_count == 0, ("vm_page_alloc: page %p is held", m));
1577         KASSERT(!vm_page_busied(m), ("vm_page_alloc: page %p is busy", m));
1578         KASSERT(m->dirty == 0, ("vm_page_alloc: page %p is dirty", m));
1579         KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1580             ("vm_page_alloc: page %p has unexpected memattr %d", m,
1581             pmap_page_get_memattr(m)));
1582         if ((m->flags & PG_CACHED) != 0) {
1583                 KASSERT((m->flags & PG_ZERO) == 0,
1584                     ("vm_page_alloc: cached page %p is PG_ZERO", m));
1585                 KASSERT(m->valid != 0,
1586                     ("vm_page_alloc: cached page %p is invalid", m));
1587                 if (m->object == object && m->pindex == pindex)
1588                         cnt.v_reactivated++;
1589                 else
1590                         m->valid = 0;
1591                 m_object = m->object;
1592                 vm_page_cache_remove(m);
1593                 if (m_object->type == OBJT_VNODE &&
1594                     vm_object_cache_is_empty(m_object))
1595                         vp = m_object->handle;
1596         } else {
1597                 KASSERT(VM_PAGE_IS_FREE(m),
1598                     ("vm_page_alloc: page %p is not free", m));
1599                 KASSERT(m->valid == 0,
1600                     ("vm_page_alloc: free page %p is valid", m));
1601                 vm_phys_freecnt_adj(m, -1);
1602         }
1603
1604         /*
1605          * Only the PG_ZERO flag is inherited.  The PG_CACHED or PG_FREE flag
1606          * must be cleared before the free page queues lock is released.
1607          */
1608         flags = 0;
1609         if (m->flags & PG_ZERO) {
1610                 vm_page_zero_count--;
1611                 if (req & VM_ALLOC_ZERO)
1612                         flags = PG_ZERO;
1613         }
1614         if (req & VM_ALLOC_NODUMP)
1615                 flags |= PG_NODUMP;
1616         m->flags = flags;
1617         mtx_unlock(&vm_page_queue_free_mtx);
1618         m->aflags = 0;
1619         m->oflags = object == NULL || (object->flags & OBJ_UNMANAGED) != 0 ?
1620             VPO_UNMANAGED : 0;
1621         m->busy_lock = VPB_UNBUSIED;
1622         if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_NOOBJ | VM_ALLOC_SBUSY)) == 0)
1623                 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1624         if ((req & VM_ALLOC_SBUSY) != 0)
1625                 m->busy_lock = VPB_SHARERS_WORD(1);
1626         if (req & VM_ALLOC_WIRED) {
1627                 /*
1628                  * The page lock is not required for wiring a page until that
1629                  * page is inserted into the object.
1630                  */
1631                 atomic_add_int(&cnt.v_wire_count, 1);
1632                 m->wire_count = 1;
1633         }
1634         m->act_count = 0;
1635
1636         if (object != NULL) {
1637                 if (vm_page_insert_after(m, object, pindex, mpred)) {
1638                         /* See the comment below about hold count. */
1639                         if (vp != NULL)
1640                                 vdrop(vp);
1641                         pagedaemon_wakeup();
1642                         if (req & VM_ALLOC_WIRED) {
1643                                 atomic_subtract_int(&cnt.v_wire_count, 1);
1644                                 m->wire_count = 0;
1645                         }
1646                         m->object = NULL;
1647                         m->oflags = VPO_UNMANAGED;
1648                         m->busy_lock = VPB_UNBUSIED;
1649                         vm_page_free(m);
1650                         return (NULL);
1651                 }
1652
1653                 /* Ignore device objects; the pager sets "memattr" for them. */
1654                 if (object->memattr != VM_MEMATTR_DEFAULT &&
1655                     (object->flags & OBJ_FICTITIOUS) == 0)
1656                         pmap_page_set_memattr(m, object->memattr);
1657         } else
1658                 m->pindex = pindex;
1659
1660         /*
1661          * The following call to vdrop() must come after the above call
1662          * to vm_page_insert() in case both affect the same object and
1663          * vnode.  Otherwise, the affected vnode's hold count could
1664          * temporarily become zero.
1665          */
1666         if (vp != NULL)
1667                 vdrop(vp);
1668
1669         /*
1670          * Don't wakeup too often - wakeup the pageout daemon when
1671          * we would be nearly out of memory.
1672          */
1673         if (vm_paging_needed())
1674                 pagedaemon_wakeup();
1675
1676         return (m);
1677 }
1678
1679 static void
1680 vm_page_alloc_contig_vdrop(struct spglist *lst)
1681 {
1682
1683         while (!SLIST_EMPTY(lst)) {
1684                 vdrop((struct vnode *)SLIST_FIRST(lst)-> plinks.s.pv);
1685                 SLIST_REMOVE_HEAD(lst, plinks.s.ss);
1686         }
1687 }
1688
1689 /*
1690  *      vm_page_alloc_contig:
1691  *
1692  *      Allocate a contiguous set of physical pages of the given size "npages"
1693  *      from the free lists.  All of the physical pages must be at or above
1694  *      the given physical address "low" and below the given physical address
1695  *      "high".  The given value "alignment" determines the alignment of the
1696  *      first physical page in the set.  If the given value "boundary" is
1697  *      non-zero, then the set of physical pages cannot cross any physical
1698  *      address boundary that is a multiple of that value.  Both "alignment"
1699  *      and "boundary" must be a power of two.
1700  *
1701  *      If the specified memory attribute, "memattr", is VM_MEMATTR_DEFAULT,
1702  *      then the memory attribute setting for the physical pages is configured
1703  *      to the object's memory attribute setting.  Otherwise, the memory
1704  *      attribute setting for the physical pages is configured to "memattr",
1705  *      overriding the object's memory attribute setting.  However, if the
1706  *      object's memory attribute setting is not VM_MEMATTR_DEFAULT, then the
1707  *      memory attribute setting for the physical pages cannot be configured
1708  *      to VM_MEMATTR_DEFAULT.
1709  *
1710  *      The caller must always specify an allocation class.
1711  *
1712  *      allocation classes:
1713  *      VM_ALLOC_NORMAL         normal process request
1714  *      VM_ALLOC_SYSTEM         system *really* needs a page
1715  *      VM_ALLOC_INTERRUPT      interrupt time request
1716  *
1717  *      optional allocation flags:
1718  *      VM_ALLOC_NOBUSY         do not exclusive busy the page
1719  *      VM_ALLOC_NODUMP         do not include the page in a kernel core dump
1720  *      VM_ALLOC_NOOBJ          page is not associated with an object and
1721  *                              should not be exclusive busy 
1722  *      VM_ALLOC_SBUSY          shared busy the allocated page
1723  *      VM_ALLOC_WIRED          wire the allocated page
1724  *      VM_ALLOC_ZERO           prefer a zeroed page
1725  *
1726  *      This routine may not sleep.
1727  */
1728 vm_page_t
1729 vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
1730     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
1731     vm_paddr_t boundary, vm_memattr_t memattr)
1732 {
1733         struct vnode *drop;
1734         struct spglist deferred_vdrop_list;
1735         vm_page_t m, m_tmp, m_ret;
1736         u_int flags, oflags;
1737         int req_class;
1738
1739         KASSERT((object != NULL) == ((req & VM_ALLOC_NOOBJ) == 0) &&
1740             (object != NULL || (req & VM_ALLOC_SBUSY) == 0) &&
1741             ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) !=
1742             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)),
1743             ("vm_page_alloc: inconsistent object(%p)/req(%x)", (void *)object,
1744             req));
1745         if (object != NULL) {
1746                 VM_OBJECT_ASSERT_WLOCKED(object);
1747                 KASSERT(object->type == OBJT_PHYS,
1748                     ("vm_page_alloc_contig: object %p isn't OBJT_PHYS",
1749                     object));
1750         }
1751         KASSERT(npages > 0, ("vm_page_alloc_contig: npages is zero"));
1752         req_class = req & VM_ALLOC_CLASS_MASK;
1753
1754         /*
1755          * The page daemon is allowed to dig deeper into the free page list.
1756          */
1757         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1758                 req_class = VM_ALLOC_SYSTEM;
1759
1760         SLIST_INIT(&deferred_vdrop_list);
1761         mtx_lock(&vm_page_queue_free_mtx);
1762         if (cnt.v_free_count + cnt.v_cache_count >= npages +
1763             cnt.v_free_reserved || (req_class == VM_ALLOC_SYSTEM &&
1764             cnt.v_free_count + cnt.v_cache_count >= npages +
1765             cnt.v_interrupt_free_min) || (req_class == VM_ALLOC_INTERRUPT &&
1766             cnt.v_free_count + cnt.v_cache_count >= npages)) {
1767 #if VM_NRESERVLEVEL > 0
1768 retry:
1769                 if (object == NULL || (object->flags & OBJ_COLORED) == 0 ||
1770                     (m_ret = vm_reserv_alloc_contig(object, pindex, npages,
1771                     low, high, alignment, boundary)) == NULL)
1772 #endif
1773                         m_ret = vm_phys_alloc_contig(npages, low, high,
1774                             alignment, boundary);
1775         } else {
1776                 mtx_unlock(&vm_page_queue_free_mtx);
1777                 atomic_add_int(&vm_pageout_deficit, npages);
1778                 pagedaemon_wakeup();
1779                 return (NULL);
1780         }
1781         if (m_ret != NULL)
1782                 for (m = m_ret; m < &m_ret[npages]; m++) {
1783                         drop = vm_page_alloc_init(m);
1784                         if (drop != NULL) {
1785                                 /*
1786                                  * Enqueue the vnode for deferred vdrop().
1787                                  */
1788                                 m->plinks.s.pv = drop;
1789                                 SLIST_INSERT_HEAD(&deferred_vdrop_list, m,
1790                                     plinks.s.ss);
1791                         }
1792                 }
1793         else {
1794 #if VM_NRESERVLEVEL > 0
1795                 if (vm_reserv_reclaim_contig(npages, low, high, alignment,
1796                     boundary))
1797                         goto retry;
1798 #endif
1799         }
1800         mtx_unlock(&vm_page_queue_free_mtx);
1801         if (m_ret == NULL)
1802                 return (NULL);
1803
1804         /*
1805          * Initialize the pages.  Only the PG_ZERO flag is inherited.
1806          */
1807         flags = 0;
1808         if ((req & VM_ALLOC_ZERO) != 0)
1809                 flags = PG_ZERO;
1810         if ((req & VM_ALLOC_NODUMP) != 0)
1811                 flags |= PG_NODUMP;
1812         if ((req & VM_ALLOC_WIRED) != 0)
1813                 atomic_add_int(&cnt.v_wire_count, npages);
1814         oflags = VPO_UNMANAGED;
1815         if (object != NULL) {
1816                 if (object->memattr != VM_MEMATTR_DEFAULT &&
1817                     memattr == VM_MEMATTR_DEFAULT)
1818                         memattr = object->memattr;
1819         }
1820         for (m = m_ret; m < &m_ret[npages]; m++) {
1821                 m->aflags = 0;
1822                 m->flags = (m->flags | PG_NODUMP) & flags;
1823                 m->busy_lock = VPB_UNBUSIED;
1824                 if (object != NULL) {
1825                         if ((req & (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
1826                                 m->busy_lock = VPB_SINGLE_EXCLUSIVER;
1827                         if ((req & VM_ALLOC_SBUSY) != 0)
1828                                 m->busy_lock = VPB_SHARERS_WORD(1);
1829                 }
1830                 if ((req & VM_ALLOC_WIRED) != 0)
1831                         m->wire_count = 1;
1832                 /* Unmanaged pages don't use "act_count". */
1833                 m->oflags = oflags;
1834                 if (object != NULL) {
1835                         if (vm_page_insert(m, object, pindex)) {
1836                                 vm_page_alloc_contig_vdrop(
1837                                     &deferred_vdrop_list);
1838                                 if (vm_paging_needed())
1839                                         pagedaemon_wakeup();
1840                                 if ((req & VM_ALLOC_WIRED) != 0)
1841                                         atomic_subtract_int(&cnt.v_wire_count,
1842                                             npages);
1843                                 for (m_tmp = m, m = m_ret;
1844                                     m < &m_ret[npages]; m++) {
1845                                         if ((req & VM_ALLOC_WIRED) != 0)
1846                                                 m->wire_count = 0;
1847                                         if (m >= m_tmp) {
1848                                                 m->object = NULL;
1849                                                 m->oflags |= VPO_UNMANAGED;
1850                                         }
1851                                         m->busy_lock = VPB_UNBUSIED;
1852                                         vm_page_free(m);
1853                                 }
1854                                 return (NULL);
1855                         }
1856                 } else
1857                         m->pindex = pindex;
1858                 if (memattr != VM_MEMATTR_DEFAULT)
1859                         pmap_page_set_memattr(m, memattr);
1860                 pindex++;
1861         }
1862         vm_page_alloc_contig_vdrop(&deferred_vdrop_list);
1863         if (vm_paging_needed())
1864                 pagedaemon_wakeup();
1865         return (m_ret);
1866 }
1867
1868 /*
1869  * Initialize a page that has been freshly dequeued from a freelist.
1870  * The caller has to drop the vnode returned, if it is not NULL.
1871  *
1872  * This function may only be used to initialize unmanaged pages.
1873  *
1874  * To be called with vm_page_queue_free_mtx held.
1875  */
1876 static struct vnode *
1877 vm_page_alloc_init(vm_page_t m)
1878 {
1879         struct vnode *drop;
1880         vm_object_t m_object;
1881
1882         KASSERT(m->queue == PQ_NONE,
1883             ("vm_page_alloc_init: page %p has unexpected queue %d",
1884             m, m->queue));
1885         KASSERT(m->wire_count == 0,
1886             ("vm_page_alloc_init: page %p is wired", m));
1887         KASSERT(m->hold_count == 0,
1888             ("vm_page_alloc_init: page %p is held", m));
1889         KASSERT(!vm_page_busied(m),
1890             ("vm_page_alloc_init: page %p is busy", m));
1891         KASSERT(m->dirty == 0,
1892             ("vm_page_alloc_init: page %p is dirty", m));
1893         KASSERT(pmap_page_get_memattr(m) == VM_MEMATTR_DEFAULT,
1894             ("vm_page_alloc_init: page %p has unexpected memattr %d",
1895             m, pmap_page_get_memattr(m)));
1896         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
1897         drop = NULL;
1898         if ((m->flags & PG_CACHED) != 0) {
1899                 KASSERT((m->flags & PG_ZERO) == 0,
1900                     ("vm_page_alloc_init: cached page %p is PG_ZERO", m));
1901                 m->valid = 0;
1902                 m_object = m->object;
1903                 vm_page_cache_remove(m);
1904                 if (m_object->type == OBJT_VNODE &&
1905                     vm_object_cache_is_empty(m_object))
1906                         drop = m_object->handle;
1907         } else {
1908                 KASSERT(VM_PAGE_IS_FREE(m),
1909                     ("vm_page_alloc_init: page %p is not free", m));
1910                 KASSERT(m->valid == 0,
1911                     ("vm_page_alloc_init: free page %p is valid", m));
1912                 vm_phys_freecnt_adj(m, -1);
1913                 if ((m->flags & PG_ZERO) != 0)
1914                         vm_page_zero_count--;
1915         }
1916         /* Don't clear the PG_ZERO flag; we'll need it later. */
1917         m->flags &= PG_ZERO;
1918         return (drop);
1919 }
1920
1921 /*
1922  *      vm_page_alloc_freelist:
1923  *
1924  *      Allocate a physical page from the specified free page list.
1925  *
1926  *      The caller must always specify an allocation class.
1927  *
1928  *      allocation classes:
1929  *      VM_ALLOC_NORMAL         normal process request
1930  *      VM_ALLOC_SYSTEM         system *really* needs a page
1931  *      VM_ALLOC_INTERRUPT      interrupt time request
1932  *
1933  *      optional allocation flags:
1934  *      VM_ALLOC_COUNT(number)  the number of additional pages that the caller
1935  *                              intends to allocate
1936  *      VM_ALLOC_WIRED          wire the allocated page
1937  *      VM_ALLOC_ZERO           prefer a zeroed page
1938  *
1939  *      This routine may not sleep.
1940  */
1941 vm_page_t
1942 vm_page_alloc_freelist(int flind, int req)
1943 {
1944         struct vnode *drop;
1945         vm_page_t m;
1946         u_int flags;
1947         int req_class;
1948
1949         req_class = req & VM_ALLOC_CLASS_MASK;
1950
1951         /*
1952          * The page daemon is allowed to dig deeper into the free page list.
1953          */
1954         if (curproc == pageproc && req_class != VM_ALLOC_INTERRUPT)
1955                 req_class = VM_ALLOC_SYSTEM;
1956
1957         /*
1958          * Do not allocate reserved pages unless the req has asked for it.
1959          */
1960         mtx_lock_flags(&vm_page_queue_free_mtx, MTX_RECURSE);
1961         if (cnt.v_free_count + cnt.v_cache_count > cnt.v_free_reserved ||
1962             (req_class == VM_ALLOC_SYSTEM &&
1963             cnt.v_free_count + cnt.v_cache_count > cnt.v_interrupt_free_min) ||
1964             (req_class == VM_ALLOC_INTERRUPT &&
1965             cnt.v_free_count + cnt.v_cache_count > 0))
1966                 m = vm_phys_alloc_freelist_pages(flind, VM_FREEPOOL_DIRECT, 0);
1967         else {
1968                 mtx_unlock(&vm_page_queue_free_mtx);
1969                 atomic_add_int(&vm_pageout_deficit,
1970                     max((u_int)req >> VM_ALLOC_COUNT_SHIFT, 1));
1971                 pagedaemon_wakeup();
1972                 return (NULL);
1973         }
1974         if (m == NULL) {
1975                 mtx_unlock(&vm_page_queue_free_mtx);
1976                 return (NULL);
1977         }
1978         drop = vm_page_alloc_init(m);
1979         mtx_unlock(&vm_page_queue_free_mtx);
1980
1981         /*
1982          * Initialize the page.  Only the PG_ZERO flag is inherited.
1983          */
1984         m->aflags = 0;
1985         flags = 0;
1986         if ((req & VM_ALLOC_ZERO) != 0)
1987                 flags = PG_ZERO;
1988         m->flags &= flags;
1989         if ((req & VM_ALLOC_WIRED) != 0) {
1990                 /*
1991                  * The page lock is not required for wiring a page that does
1992                  * not belong to an object.
1993                  */
1994                 atomic_add_int(&cnt.v_wire_count, 1);
1995                 m->wire_count = 1;
1996         }
1997         /* Unmanaged pages don't use "act_count". */
1998         m->oflags = VPO_UNMANAGED;
1999         if (drop != NULL)
2000                 vdrop(drop);
2001         if (vm_paging_needed())
2002                 pagedaemon_wakeup();
2003         return (m);
2004 }
2005
2006 /*
2007  *      vm_wait:        (also see VM_WAIT macro)
2008  *
2009  *      Sleep until free pages are available for allocation.
2010  *      - Called in various places before memory allocations.
2011  */
2012 void
2013 vm_wait(void)
2014 {
2015
2016         mtx_lock(&vm_page_queue_free_mtx);
2017         if (curproc == pageproc) {
2018                 vm_pageout_pages_needed = 1;
2019                 msleep(&vm_pageout_pages_needed, &vm_page_queue_free_mtx,
2020                     PDROP | PSWP, "VMWait", 0);
2021         } else {
2022                 if (!vm_pages_needed) {
2023                         vm_pages_needed = 1;
2024                         wakeup(&vm_pages_needed);
2025                 }
2026                 msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PVM,
2027                     "vmwait", 0);
2028         }
2029 }
2030
2031 /*
2032  *      vm_waitpfault:  (also see VM_WAITPFAULT macro)
2033  *
2034  *      Sleep until free pages are available for allocation.
2035  *      - Called only in vm_fault so that processes page faulting
2036  *        can be easily tracked.
2037  *      - Sleeps at a lower priority than vm_wait() so that vm_wait()ing
2038  *        processes will be able to grab memory first.  Do not change
2039  *        this balance without careful testing first.
2040  */
2041 void
2042 vm_waitpfault(void)
2043 {
2044
2045         mtx_lock(&vm_page_queue_free_mtx);
2046         if (!vm_pages_needed) {
2047                 vm_pages_needed = 1;
2048                 wakeup(&vm_pages_needed);
2049         }
2050         msleep(&cnt.v_free_count, &vm_page_queue_free_mtx, PDROP | PUSER,
2051             "pfault", 0);
2052 }
2053
2054 struct vm_pagequeue *
2055 vm_page_pagequeue(vm_page_t m)
2056 {
2057
2058         return (&vm_phys_domain(m)->vmd_pagequeues[m->queue]);
2059 }
2060
2061 /*
2062  *      vm_page_dequeue:
2063  *
2064  *      Remove the given page from its current page queue.
2065  *
2066  *      The page must be locked.
2067  */
2068 void
2069 vm_page_dequeue(vm_page_t m)
2070 {
2071         struct vm_pagequeue *pq;
2072
2073         vm_page_lock_assert(m, MA_OWNED);
2074         KASSERT(m->queue != PQ_NONE,
2075             ("vm_page_dequeue: page %p is not queued", m));
2076         pq = vm_page_pagequeue(m);
2077         vm_pagequeue_lock(pq);
2078         m->queue = PQ_NONE;
2079         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2080         vm_pagequeue_cnt_dec(pq);
2081         vm_pagequeue_unlock(pq);
2082 }
2083
2084 /*
2085  *      vm_page_dequeue_locked:
2086  *
2087  *      Remove the given page from its current page queue.
2088  *
2089  *      The page and page queue must be locked.
2090  */
2091 void
2092 vm_page_dequeue_locked(vm_page_t m)
2093 {
2094         struct vm_pagequeue *pq;
2095
2096         vm_page_lock_assert(m, MA_OWNED);
2097         pq = vm_page_pagequeue(m);
2098         vm_pagequeue_assert_locked(pq);
2099         m->queue = PQ_NONE;
2100         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2101         vm_pagequeue_cnt_dec(pq);
2102 }
2103
2104 /*
2105  *      vm_page_enqueue:
2106  *
2107  *      Add the given page to the specified page queue.
2108  *
2109  *      The page must be locked.
2110  */
2111 static void
2112 vm_page_enqueue(int queue, vm_page_t m)
2113 {
2114         struct vm_pagequeue *pq;
2115
2116         vm_page_lock_assert(m, MA_OWNED);
2117         pq = &vm_phys_domain(m)->vmd_pagequeues[queue];
2118         vm_pagequeue_lock(pq);
2119         m->queue = queue;
2120         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2121         vm_pagequeue_cnt_inc(pq);
2122         vm_pagequeue_unlock(pq);
2123 }
2124
2125 /*
2126  *      vm_page_requeue:
2127  *
2128  *      Move the given page to the tail of its current page queue.
2129  *
2130  *      The page must be locked.
2131  */
2132 void
2133 vm_page_requeue(vm_page_t m)
2134 {
2135         struct vm_pagequeue *pq;
2136
2137         vm_page_lock_assert(m, MA_OWNED);
2138         KASSERT(m->queue != PQ_NONE,
2139             ("vm_page_requeue: page %p is not queued", m));
2140         pq = vm_page_pagequeue(m);
2141         vm_pagequeue_lock(pq);
2142         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2143         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2144         vm_pagequeue_unlock(pq);
2145 }
2146
2147 /*
2148  *      vm_page_requeue_locked:
2149  *
2150  *      Move the given page to the tail of its current page queue.
2151  *
2152  *      The page queue must be locked.
2153  */
2154 void
2155 vm_page_requeue_locked(vm_page_t m)
2156 {
2157         struct vm_pagequeue *pq;
2158
2159         KASSERT(m->queue != PQ_NONE,
2160             ("vm_page_requeue_locked: page %p is not queued", m));
2161         pq = vm_page_pagequeue(m);
2162         vm_pagequeue_assert_locked(pq);
2163         TAILQ_REMOVE(&pq->pq_pl, m, plinks.q);
2164         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2165 }
2166
2167 /*
2168  *      vm_page_activate:
2169  *
2170  *      Put the specified page on the active list (if appropriate).
2171  *      Ensure that act_count is at least ACT_INIT but do not otherwise
2172  *      mess with it.
2173  *
2174  *      The page must be locked.
2175  */
2176 void
2177 vm_page_activate(vm_page_t m)
2178 {
2179         int queue;
2180
2181         vm_page_lock_assert(m, MA_OWNED);
2182         if ((queue = m->queue) != PQ_ACTIVE) {
2183                 if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2184                         if (m->act_count < ACT_INIT)
2185                                 m->act_count = ACT_INIT;
2186                         if (queue != PQ_NONE)
2187                                 vm_page_dequeue(m);
2188                         vm_page_enqueue(PQ_ACTIVE, m);
2189                 } else
2190                         KASSERT(queue == PQ_NONE,
2191                             ("vm_page_activate: wired page %p is queued", m));
2192         } else {
2193                 if (m->act_count < ACT_INIT)
2194                         m->act_count = ACT_INIT;
2195         }
2196 }
2197
2198 /*
2199  *      vm_page_free_wakeup:
2200  *
2201  *      Helper routine for vm_page_free_toq() and vm_page_cache().  This
2202  *      routine is called when a page has been added to the cache or free
2203  *      queues.
2204  *
2205  *      The page queues must be locked.
2206  */
2207 static inline void
2208 vm_page_free_wakeup(void)
2209 {
2210
2211         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2212         /*
2213          * if pageout daemon needs pages, then tell it that there are
2214          * some free.
2215          */
2216         if (vm_pageout_pages_needed &&
2217             cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) {
2218                 wakeup(&vm_pageout_pages_needed);
2219                 vm_pageout_pages_needed = 0;
2220         }
2221         /*
2222          * wakeup processes that are waiting on memory if we hit a
2223          * high water mark. And wakeup scheduler process if we have
2224          * lots of memory. this process will swapin processes.
2225          */
2226         if (vm_pages_needed && !vm_page_count_min()) {
2227                 vm_pages_needed = 0;
2228                 wakeup(&cnt.v_free_count);
2229         }
2230 }
2231
2232 /*
2233  *      Turn a cached page into a free page, by changing its attributes.
2234  *      Keep the statistics up-to-date.
2235  *
2236  *      The free page queue must be locked.
2237  */
2238 static void
2239 vm_page_cache_turn_free(vm_page_t m)
2240 {
2241
2242         mtx_assert(&vm_page_queue_free_mtx, MA_OWNED);
2243
2244         m->object = NULL;
2245         m->valid = 0;
2246         /* Clear PG_CACHED and set PG_FREE. */
2247         m->flags ^= PG_CACHED | PG_FREE;
2248         KASSERT((m->flags & (PG_CACHED | PG_FREE)) == PG_FREE,
2249             ("vm_page_cache_free: page %p has inconsistent flags", m));
2250         cnt.v_cache_count--;
2251         vm_phys_freecnt_adj(m, 1);
2252 }
2253
2254 /*
2255  *      vm_page_free_toq:
2256  *
2257  *      Returns the given page to the free list,
2258  *      disassociating it with any VM object.
2259  *
2260  *      The object must be locked.  The page must be locked if it is managed.
2261  */
2262 void
2263 vm_page_free_toq(vm_page_t m)
2264 {
2265
2266         if ((m->oflags & VPO_UNMANAGED) == 0) {
2267                 vm_page_lock_assert(m, MA_OWNED);
2268                 KASSERT(!pmap_page_is_mapped(m),
2269                     ("vm_page_free_toq: freeing mapped page %p", m));
2270         } else
2271                 KASSERT(m->queue == PQ_NONE,
2272                     ("vm_page_free_toq: unmanaged page %p is queued", m));
2273         PCPU_INC(cnt.v_tfree);
2274
2275         if (VM_PAGE_IS_FREE(m))
2276                 panic("vm_page_free: freeing free page %p", m);
2277         else if (vm_page_sbusied(m))
2278                 panic("vm_page_free: freeing busy page %p", m);
2279
2280         /*
2281          * Unqueue, then remove page.  Note that we cannot destroy
2282          * the page here because we do not want to call the pager's
2283          * callback routine until after we've put the page on the
2284          * appropriate free queue.
2285          */
2286         vm_page_remque(m);
2287         vm_page_remove(m);
2288
2289         /*
2290          * If fictitious remove object association and
2291          * return, otherwise delay object association removal.
2292          */
2293         if ((m->flags & PG_FICTITIOUS) != 0) {
2294                 return;
2295         }
2296
2297         m->valid = 0;
2298         vm_page_undirty(m);
2299
2300         if (m->wire_count != 0)
2301                 panic("vm_page_free: freeing wired page %p", m);
2302         if (m->hold_count != 0) {
2303                 m->flags &= ~PG_ZERO;
2304                 KASSERT((m->flags & PG_UNHOLDFREE) == 0,
2305                     ("vm_page_free: freeing PG_UNHOLDFREE page %p", m));
2306                 m->flags |= PG_UNHOLDFREE;
2307         } else {
2308                 /*
2309                  * Restore the default memory attribute to the page.
2310                  */
2311                 if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2312                         pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2313
2314                 /*
2315                  * Insert the page into the physical memory allocator's
2316                  * cache/free page queues.
2317                  */
2318                 mtx_lock(&vm_page_queue_free_mtx);
2319                 m->flags |= PG_FREE;
2320                 vm_phys_freecnt_adj(m, 1);
2321 #if VM_NRESERVLEVEL > 0
2322                 if (!vm_reserv_free_page(m))
2323 #else
2324                 if (TRUE)
2325 #endif
2326                         vm_phys_free_pages(m, 0);
2327                 if ((m->flags & PG_ZERO) != 0)
2328                         ++vm_page_zero_count;
2329                 else
2330                         vm_page_zero_idle_wakeup();
2331                 vm_page_free_wakeup();
2332                 mtx_unlock(&vm_page_queue_free_mtx);
2333         }
2334 }
2335
2336 /*
2337  *      vm_page_wire:
2338  *
2339  *      Mark this page as wired down by yet
2340  *      another map, removing it from paging queues
2341  *      as necessary.
2342  *
2343  *      If the page is fictitious, then its wire count must remain one.
2344  *
2345  *      The page must be locked.
2346  */
2347 void
2348 vm_page_wire(vm_page_t m)
2349 {
2350
2351         /*
2352          * Only bump the wire statistics if the page is not already wired,
2353          * and only unqueue the page if it is on some queue (if it is unmanaged
2354          * it is already off the queues).
2355          */
2356         vm_page_lock_assert(m, MA_OWNED);
2357         if ((m->flags & PG_FICTITIOUS) != 0) {
2358                 KASSERT(m->wire_count == 1,
2359                     ("vm_page_wire: fictitious page %p's wire count isn't one",
2360                     m));
2361                 return;
2362         }
2363         if (m->wire_count == 0) {
2364                 KASSERT((m->oflags & VPO_UNMANAGED) == 0 ||
2365                     m->queue == PQ_NONE,
2366                     ("vm_page_wire: unmanaged page %p is queued", m));
2367                 vm_page_remque(m);
2368                 atomic_add_int(&cnt.v_wire_count, 1);
2369         }
2370         m->wire_count++;
2371         KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m));
2372 }
2373
2374 /*
2375  * vm_page_unwire:
2376  *
2377  * Release one wiring of the specified page, potentially enabling it to be
2378  * paged again.  If paging is enabled, then the value of the parameter
2379  * "activate" determines to which queue the page is added.  If "activate" is
2380  * non-zero, then the page is added to the active queue.  Otherwise, it is
2381  * added to the inactive queue.
2382  *
2383  * However, unless the page belongs to an object, it is not enqueued because
2384  * it cannot be paged out.
2385  *
2386  * If a page is fictitious, then its wire count must always be one.
2387  *
2388  * A managed page must be locked.
2389  */
2390 void
2391 vm_page_unwire(vm_page_t m, int activate)
2392 {
2393
2394         if ((m->oflags & VPO_UNMANAGED) == 0)
2395                 vm_page_lock_assert(m, MA_OWNED);
2396         if ((m->flags & PG_FICTITIOUS) != 0) {
2397                 KASSERT(m->wire_count == 1,
2398             ("vm_page_unwire: fictitious page %p's wire count isn't one", m));
2399                 return;
2400         }
2401         if (m->wire_count > 0) {
2402                 m->wire_count--;
2403                 if (m->wire_count == 0) {
2404                         atomic_subtract_int(&cnt.v_wire_count, 1);
2405                         if ((m->oflags & VPO_UNMANAGED) != 0 ||
2406                             m->object == NULL)
2407                                 return;
2408                         if (!activate)
2409                                 m->flags &= ~PG_WINATCFLS;
2410                         vm_page_enqueue(activate ? PQ_ACTIVE : PQ_INACTIVE, m);
2411                 }
2412         } else
2413                 panic("vm_page_unwire: page %p's wire count is zero", m);
2414 }
2415
2416 /*
2417  * Move the specified page to the inactive queue.
2418  *
2419  * Many pages placed on the inactive queue should actually go
2420  * into the cache, but it is difficult to figure out which.  What
2421  * we do instead, if the inactive target is well met, is to put
2422  * clean pages at the head of the inactive queue instead of the tail.
2423  * This will cause them to be moved to the cache more quickly and
2424  * if not actively re-referenced, reclaimed more quickly.  If we just
2425  * stick these pages at the end of the inactive queue, heavy filesystem
2426  * meta-data accesses can cause an unnecessary paging load on memory bound 
2427  * processes.  This optimization causes one-time-use metadata to be
2428  * reused more quickly.
2429  *
2430  * Normally athead is 0 resulting in LRU operation.  athead is set
2431  * to 1 if we want this page to be 'as if it were placed in the cache',
2432  * except without unmapping it from the process address space.
2433  *
2434  * The page must be locked.
2435  */
2436 static inline void
2437 _vm_page_deactivate(vm_page_t m, int athead)
2438 {
2439         struct vm_pagequeue *pq;
2440         int queue;
2441
2442         vm_page_assert_locked(m);
2443
2444         /*
2445          * Ignore if the page is already inactive, unless it is unlikely to be
2446          * reactivated.
2447          */
2448         if ((queue = m->queue) == PQ_INACTIVE && !athead)
2449                 return;
2450         if (m->wire_count == 0 && (m->oflags & VPO_UNMANAGED) == 0) {
2451                 pq = &vm_phys_domain(m)->vmd_pagequeues[PQ_INACTIVE];
2452                 /* Avoid multiple acquisitions of the inactive queue lock. */
2453                 if (queue == PQ_INACTIVE) {
2454                         vm_pagequeue_lock(pq);
2455                         vm_page_dequeue_locked(m);
2456                 } else {
2457                         if (queue != PQ_NONE)
2458                                 vm_page_dequeue(m);
2459                         m->flags &= ~PG_WINATCFLS;
2460                         vm_pagequeue_lock(pq);
2461                 }
2462                 m->queue = PQ_INACTIVE;
2463                 if (athead)
2464                         TAILQ_INSERT_HEAD(&pq->pq_pl, m, plinks.q);
2465                 else
2466                         TAILQ_INSERT_TAIL(&pq->pq_pl, m, plinks.q);
2467                 vm_pagequeue_cnt_inc(pq);
2468                 vm_pagequeue_unlock(pq);
2469         }
2470 }
2471
2472 /*
2473  * Move the specified page to the inactive queue.
2474  *
2475  * The page must be locked.
2476  */
2477 void
2478 vm_page_deactivate(vm_page_t m)
2479 {
2480
2481         _vm_page_deactivate(m, 0);
2482 }
2483
2484 /*
2485  * vm_page_try_to_cache:
2486  *
2487  * Returns 0 on failure, 1 on success
2488  */
2489 int
2490 vm_page_try_to_cache(vm_page_t m)
2491 {
2492
2493         vm_page_lock_assert(m, MA_OWNED);
2494         VM_OBJECT_ASSERT_WLOCKED(m->object);
2495         if (m->dirty || m->hold_count || m->wire_count ||
2496             (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
2497                 return (0);
2498         pmap_remove_all(m);
2499         if (m->dirty)
2500                 return (0);
2501         vm_page_cache(m);
2502         return (1);
2503 }
2504
2505 /*
2506  * vm_page_try_to_free()
2507  *
2508  *      Attempt to free the page.  If we cannot free it, we do nothing.
2509  *      1 is returned on success, 0 on failure.
2510  */
2511 int
2512 vm_page_try_to_free(vm_page_t m)
2513 {
2514
2515         vm_page_lock_assert(m, MA_OWNED);
2516         if (m->object != NULL)
2517                 VM_OBJECT_ASSERT_WLOCKED(m->object);
2518         if (m->dirty || m->hold_count || m->wire_count ||
2519             (m->oflags & VPO_UNMANAGED) != 0 || vm_page_busied(m))
2520                 return (0);
2521         pmap_remove_all(m);
2522         if (m->dirty)
2523                 return (0);
2524         vm_page_free(m);
2525         return (1);
2526 }
2527
2528 /*
2529  * vm_page_cache
2530  *
2531  * Put the specified page onto the page cache queue (if appropriate).
2532  *
2533  * The object and page must be locked.
2534  */
2535 void
2536 vm_page_cache(vm_page_t m)
2537 {
2538         vm_object_t object;
2539         boolean_t cache_was_empty;
2540
2541         vm_page_lock_assert(m, MA_OWNED);
2542         object = m->object;
2543         VM_OBJECT_ASSERT_WLOCKED(object);
2544         if (vm_page_busied(m) || (m->oflags & VPO_UNMANAGED) ||
2545             m->hold_count || m->wire_count)
2546                 panic("vm_page_cache: attempting to cache busy page");
2547         KASSERT(!pmap_page_is_mapped(m),
2548             ("vm_page_cache: page %p is mapped", m));
2549         KASSERT(m->dirty == 0, ("vm_page_cache: page %p is dirty", m));
2550         if (m->valid == 0 || object->type == OBJT_DEFAULT ||
2551             (object->type == OBJT_SWAP &&
2552             !vm_pager_has_page(object, m->pindex, NULL, NULL))) {
2553                 /*
2554                  * Hypothesis: A cache-elgible page belonging to a
2555                  * default object or swap object but without a backing
2556                  * store must be zero filled.
2557                  */
2558                 vm_page_free(m);
2559                 return;
2560         }
2561         KASSERT((m->flags & PG_CACHED) == 0,
2562             ("vm_page_cache: page %p is already cached", m));
2563
2564         /*
2565          * Remove the page from the paging queues.
2566          */
2567         vm_page_remque(m);
2568
2569         /*
2570          * Remove the page from the object's collection of resident
2571          * pages. 
2572          */
2573         vm_radix_remove(&object->rtree, m->pindex);
2574         TAILQ_REMOVE(&object->memq, m, listq);
2575         object->resident_page_count--;
2576
2577         /*
2578          * Restore the default memory attribute to the page.
2579          */
2580         if (pmap_page_get_memattr(m) != VM_MEMATTR_DEFAULT)
2581                 pmap_page_set_memattr(m, VM_MEMATTR_DEFAULT);
2582
2583         /*
2584          * Insert the page into the object's collection of cached pages
2585          * and the physical memory allocator's cache/free page queues.
2586          */
2587         m->flags &= ~PG_ZERO;
2588         mtx_lock(&vm_page_queue_free_mtx);
2589         cache_was_empty = vm_radix_is_empty(&object->cache);
2590         if (vm_radix_insert(&object->cache, m)) {
2591                 mtx_unlock(&vm_page_queue_free_mtx);
2592                 if (object->type == OBJT_VNODE &&
2593                     object->resident_page_count == 0)
2594                         vdrop(object->handle);
2595                 m->object = NULL;
2596                 vm_page_free(m);
2597                 return;
2598         }
2599
2600         /*
2601          * The above call to vm_radix_insert() could reclaim the one pre-
2602          * existing cached page from this object, resulting in a call to
2603          * vdrop().
2604          */
2605         if (!cache_was_empty)
2606                 cache_was_empty = vm_radix_is_singleton(&object->cache);
2607
2608         m->flags |= PG_CACHED;
2609         cnt.v_cache_count++;
2610         PCPU_INC(cnt.v_tcached);
2611 #if VM_NRESERVLEVEL > 0
2612         if (!vm_reserv_free_page(m)) {
2613 #else
2614         if (TRUE) {
2615 #endif
2616                 vm_phys_set_pool(VM_FREEPOOL_CACHE, m, 0);
2617                 vm_phys_free_pages(m, 0);
2618         }
2619         vm_page_free_wakeup();
2620         mtx_unlock(&vm_page_queue_free_mtx);
2621
2622         /*
2623          * Increment the vnode's hold count if this is the object's only
2624          * cached page.  Decrement the vnode's hold count if this was
2625          * the object's only resident page.
2626          */
2627         if (object->type == OBJT_VNODE) {
2628                 if (cache_was_empty && object->resident_page_count != 0)
2629                         vhold(object->handle);
2630                 else if (!cache_was_empty && object->resident_page_count == 0)
2631                         vdrop(object->handle);
2632         }
2633 }
2634
2635 /*
2636  * vm_page_advise
2637  *
2638  *      Deactivate or do nothing, as appropriate.  This routine is used
2639  *      by madvise() and vop_stdadvise().
2640  *
2641  *      The object and page must be locked.
2642  */
2643 void
2644 vm_page_advise(vm_page_t m, int advice)
2645 {
2646
2647         vm_page_assert_locked(m);
2648         VM_OBJECT_ASSERT_WLOCKED(m->object);
2649         if (advice == MADV_FREE)
2650                 /*
2651                  * Mark the page clean.  This will allow the page to be freed
2652                  * up by the system.  However, such pages are often reused
2653                  * quickly by malloc() so we do not do anything that would
2654                  * cause a page fault if we can help it.
2655                  *
2656                  * Specifically, we do not try to actually free the page now
2657                  * nor do we try to put it in the cache (which would cause a
2658                  * page fault on reuse).
2659                  *
2660                  * But we do make the page as freeable as we can without
2661                  * actually taking the step of unmapping it.
2662                  */
2663                 vm_page_undirty(m);
2664         else if (advice != MADV_DONTNEED)
2665                 return;
2666
2667         /*
2668          * Clear any references to the page.  Otherwise, the page daemon will
2669          * immediately reactivate the page.
2670          */
2671         vm_page_aflag_clear(m, PGA_REFERENCED);
2672
2673         if (advice != MADV_FREE && m->dirty == 0 && pmap_is_modified(m))
2674                 vm_page_dirty(m);
2675
2676         /*
2677          * Place clean pages at the head of the inactive queue rather than the
2678          * tail, thus defeating the queue's LRU operation and ensuring that the
2679          * page will be reused quickly.
2680          */
2681         _vm_page_deactivate(m, m->dirty == 0);
2682 }
2683
2684 /*
2685  * Grab a page, waiting until we are waken up due to the page
2686  * changing state.  We keep on waiting, if the page continues
2687  * to be in the object.  If the page doesn't exist, first allocate it
2688  * and then conditionally zero it.
2689  *
2690  * This routine may sleep.
2691  *
2692  * The object must be locked on entry.  The lock will, however, be released
2693  * and reacquired if the routine sleeps.
2694  */
2695 vm_page_t
2696 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags)
2697 {
2698         vm_page_t m;
2699         int sleep;
2700
2701         VM_OBJECT_ASSERT_WLOCKED(object);
2702         KASSERT((allocflags & VM_ALLOC_SBUSY) == 0 ||
2703             (allocflags & VM_ALLOC_IGN_SBUSY) != 0,
2704             ("vm_page_grab: VM_ALLOC_SBUSY/VM_ALLOC_IGN_SBUSY mismatch"));
2705 retrylookup:
2706         if ((m = vm_page_lookup(object, pindex)) != NULL) {
2707                 sleep = (allocflags & VM_ALLOC_IGN_SBUSY) != 0 ?
2708                     vm_page_xbusied(m) : vm_page_busied(m);
2709                 if (sleep) {
2710                         /*
2711                          * Reference the page before unlocking and
2712                          * sleeping so that the page daemon is less
2713                          * likely to reclaim it.
2714                          */
2715                         vm_page_aflag_set(m, PGA_REFERENCED);
2716                         vm_page_lock(m);
2717                         VM_OBJECT_WUNLOCK(object);
2718                         vm_page_busy_sleep(m, "pgrbwt", (allocflags &
2719                             VM_ALLOC_IGN_SBUSY) != 0);
2720                         VM_OBJECT_WLOCK(object);
2721                         goto retrylookup;
2722                 } else {
2723                         if ((allocflags & VM_ALLOC_WIRED) != 0) {
2724                                 vm_page_lock(m);
2725                                 vm_page_wire(m);
2726                                 vm_page_unlock(m);
2727                         }
2728                         if ((allocflags &
2729                             (VM_ALLOC_NOBUSY | VM_ALLOC_SBUSY)) == 0)
2730                                 vm_page_xbusy(m);
2731                         if ((allocflags & VM_ALLOC_SBUSY) != 0)
2732                                 vm_page_sbusy(m);
2733                         return (m);
2734                 }
2735         }
2736         m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_IGN_SBUSY);
2737         if (m == NULL) {
2738                 VM_OBJECT_WUNLOCK(object);
2739                 VM_WAIT;
2740                 VM_OBJECT_WLOCK(object);
2741                 goto retrylookup;
2742         } else if (m->valid != 0)
2743                 return (m);
2744         if (allocflags & VM_ALLOC_ZERO && (m->flags & PG_ZERO) == 0)
2745                 pmap_zero_page(m);
2746         return (m);
2747 }
2748
2749 /*
2750  * Mapping function for valid or dirty bits in a page.
2751  *
2752  * Inputs are required to range within a page.
2753  */
2754 vm_page_bits_t
2755 vm_page_bits(int base, int size)
2756 {
2757         int first_bit;
2758         int last_bit;
2759
2760         KASSERT(
2761             base + size <= PAGE_SIZE,
2762             ("vm_page_bits: illegal base/size %d/%d", base, size)
2763         );
2764
2765         if (size == 0)          /* handle degenerate case */
2766                 return (0);
2767
2768         first_bit = base >> DEV_BSHIFT;
2769         last_bit = (base + size - 1) >> DEV_BSHIFT;
2770
2771         return (((vm_page_bits_t)2 << last_bit) -
2772             ((vm_page_bits_t)1 << first_bit));
2773 }
2774
2775 /*
2776  *      vm_page_set_valid_range:
2777  *
2778  *      Sets portions of a page valid.  The arguments are expected
2779  *      to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2780  *      of any partial chunks touched by the range.  The invalid portion of
2781  *      such chunks will be zeroed.
2782  *
2783  *      (base + size) must be less then or equal to PAGE_SIZE.
2784  */
2785 void
2786 vm_page_set_valid_range(vm_page_t m, int base, int size)
2787 {
2788         int endoff, frag;
2789
2790         VM_OBJECT_ASSERT_WLOCKED(m->object);
2791         if (size == 0)  /* handle degenerate case */
2792                 return;
2793
2794         /*
2795          * If the base is not DEV_BSIZE aligned and the valid
2796          * bit is clear, we have to zero out a portion of the
2797          * first block.
2798          */
2799         if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2800             (m->valid & (1 << (base >> DEV_BSHIFT))) == 0)
2801                 pmap_zero_page_area(m, frag, base - frag);
2802
2803         /*
2804          * If the ending offset is not DEV_BSIZE aligned and the 
2805          * valid bit is clear, we have to zero out a portion of
2806          * the last block.
2807          */
2808         endoff = base + size;
2809         if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2810             (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0)
2811                 pmap_zero_page_area(m, endoff,
2812                     DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2813
2814         /*
2815          * Assert that no previously invalid block that is now being validated
2816          * is already dirty. 
2817          */
2818         KASSERT((~m->valid & vm_page_bits(base, size) & m->dirty) == 0,
2819             ("vm_page_set_valid_range: page %p is dirty", m));
2820
2821         /*
2822          * Set valid bits inclusive of any overlap.
2823          */
2824         m->valid |= vm_page_bits(base, size);
2825 }
2826
2827 /*
2828  * Clear the given bits from the specified page's dirty field.
2829  */
2830 static __inline void
2831 vm_page_clear_dirty_mask(vm_page_t m, vm_page_bits_t pagebits)
2832 {
2833         uintptr_t addr;
2834 #if PAGE_SIZE < 16384
2835         int shift;
2836 #endif
2837
2838         /*
2839          * If the object is locked and the page is neither exclusive busy nor
2840          * write mapped, then the page's dirty field cannot possibly be
2841          * set by a concurrent pmap operation.
2842          */
2843         VM_OBJECT_ASSERT_WLOCKED(m->object);
2844         if (!vm_page_xbusied(m) && !pmap_page_is_write_mapped(m))
2845                 m->dirty &= ~pagebits;
2846         else {
2847                 /*
2848                  * The pmap layer can call vm_page_dirty() without
2849                  * holding a distinguished lock.  The combination of
2850                  * the object's lock and an atomic operation suffice
2851                  * to guarantee consistency of the page dirty field.
2852                  *
2853                  * For PAGE_SIZE == 32768 case, compiler already
2854                  * properly aligns the dirty field, so no forcible
2855                  * alignment is needed. Only require existence of
2856                  * atomic_clear_64 when page size is 32768.
2857                  */
2858                 addr = (uintptr_t)&m->dirty;
2859 #if PAGE_SIZE == 32768
2860                 atomic_clear_64((uint64_t *)addr, pagebits);
2861 #elif PAGE_SIZE == 16384
2862                 atomic_clear_32((uint32_t *)addr, pagebits);
2863 #else           /* PAGE_SIZE <= 8192 */
2864                 /*
2865                  * Use a trick to perform a 32-bit atomic on the
2866                  * containing aligned word, to not depend on the existence
2867                  * of atomic_clear_{8, 16}.
2868                  */
2869                 shift = addr & (sizeof(uint32_t) - 1);
2870 #if BYTE_ORDER == BIG_ENDIAN
2871                 shift = (sizeof(uint32_t) - sizeof(m->dirty) - shift) * NBBY;
2872 #else
2873                 shift *= NBBY;
2874 #endif
2875                 addr &= ~(sizeof(uint32_t) - 1);
2876                 atomic_clear_32((uint32_t *)addr, pagebits << shift);
2877 #endif          /* PAGE_SIZE */
2878         }
2879 }
2880
2881 /*
2882  *      vm_page_set_validclean:
2883  *
2884  *      Sets portions of a page valid and clean.  The arguments are expected
2885  *      to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive
2886  *      of any partial chunks touched by the range.  The invalid portion of
2887  *      such chunks will be zero'd.
2888  *
2889  *      (base + size) must be less then or equal to PAGE_SIZE.
2890  */
2891 void
2892 vm_page_set_validclean(vm_page_t m, int base, int size)
2893 {
2894         vm_page_bits_t oldvalid, pagebits;
2895         int endoff, frag;
2896
2897         VM_OBJECT_ASSERT_WLOCKED(m->object);
2898         if (size == 0)  /* handle degenerate case */
2899                 return;
2900
2901         /*
2902          * If the base is not DEV_BSIZE aligned and the valid
2903          * bit is clear, we have to zero out a portion of the
2904          * first block.
2905          */
2906         if ((frag = base & ~(DEV_BSIZE - 1)) != base &&
2907             (m->valid & ((vm_page_bits_t)1 << (base >> DEV_BSHIFT))) == 0)
2908                 pmap_zero_page_area(m, frag, base - frag);
2909
2910         /*
2911          * If the ending offset is not DEV_BSIZE aligned and the 
2912          * valid bit is clear, we have to zero out a portion of
2913          * the last block.
2914          */
2915         endoff = base + size;
2916         if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff &&
2917             (m->valid & ((vm_page_bits_t)1 << (endoff >> DEV_BSHIFT))) == 0)
2918                 pmap_zero_page_area(m, endoff,
2919                     DEV_BSIZE - (endoff & (DEV_BSIZE - 1)));
2920
2921         /*
2922          * Set valid, clear dirty bits.  If validating the entire
2923          * page we can safely clear the pmap modify bit.  We also
2924          * use this opportunity to clear the VPO_NOSYNC flag.  If a process
2925          * takes a write fault on a MAP_NOSYNC memory area the flag will
2926          * be set again.
2927          *
2928          * We set valid bits inclusive of any overlap, but we can only
2929          * clear dirty bits for DEV_BSIZE chunks that are fully within
2930          * the range.
2931          */
2932         oldvalid = m->valid;
2933         pagebits = vm_page_bits(base, size);
2934         m->valid |= pagebits;
2935 #if 0   /* NOT YET */
2936         if ((frag = base & (DEV_BSIZE - 1)) != 0) {
2937                 frag = DEV_BSIZE - frag;
2938                 base += frag;
2939                 size -= frag;
2940                 if (size < 0)
2941                         size = 0;
2942         }
2943         pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1));
2944 #endif
2945         if (base == 0 && size == PAGE_SIZE) {
2946                 /*
2947                  * The page can only be modified within the pmap if it is
2948                  * mapped, and it can only be mapped if it was previously
2949                  * fully valid.
2950                  */
2951                 if (oldvalid == VM_PAGE_BITS_ALL)
2952                         /*
2953                          * Perform the pmap_clear_modify() first.  Otherwise,
2954                          * a concurrent pmap operation, such as
2955                          * pmap_protect(), could clear a modification in the
2956                          * pmap and set the dirty field on the page before
2957                          * pmap_clear_modify() had begun and after the dirty
2958                          * field was cleared here.
2959                          */
2960                         pmap_clear_modify(m);
2961                 m->dirty = 0;
2962                 m->oflags &= ~VPO_NOSYNC;
2963         } else if (oldvalid != VM_PAGE_BITS_ALL)
2964                 m->dirty &= ~pagebits;
2965         else
2966                 vm_page_clear_dirty_mask(m, pagebits);
2967 }
2968
2969 void
2970 vm_page_clear_dirty(vm_page_t m, int base, int size)
2971 {
2972
2973         vm_page_clear_dirty_mask(m, vm_page_bits(base, size));
2974 }
2975
2976 /*
2977  *      vm_page_set_invalid:
2978  *
2979  *      Invalidates DEV_BSIZE'd chunks within a page.  Both the
2980  *      valid and dirty bits for the effected areas are cleared.
2981  */
2982 void
2983 vm_page_set_invalid(vm_page_t m, int base, int size)
2984 {
2985         vm_page_bits_t bits;
2986         vm_object_t object;
2987
2988         object = m->object;
2989         VM_OBJECT_ASSERT_WLOCKED(object);
2990         if (object->type == OBJT_VNODE && base == 0 && IDX_TO_OFF(m->pindex) +
2991             size >= object->un_pager.vnp.vnp_size)
2992                 bits = VM_PAGE_BITS_ALL;
2993         else
2994                 bits = vm_page_bits(base, size);
2995         if (object->ref_count != 0 && m->valid == VM_PAGE_BITS_ALL &&
2996             bits != 0)
2997                 pmap_remove_all(m);
2998         KASSERT((bits == 0 && m->valid == VM_PAGE_BITS_ALL) ||
2999             !pmap_page_is_mapped(m),
3000             ("vm_page_set_invalid: page %p is mapped", m));
3001         m->valid &= ~bits;
3002         m->dirty &= ~bits;
3003 }
3004
3005 /*
3006  * vm_page_zero_invalid()
3007  *
3008  *      The kernel assumes that the invalid portions of a page contain 
3009  *      garbage, but such pages can be mapped into memory by user code.
3010  *      When this occurs, we must zero out the non-valid portions of the
3011  *      page so user code sees what it expects.
3012  *
3013  *      Pages are most often semi-valid when the end of a file is mapped 
3014  *      into memory and the file's size is not page aligned.
3015  */
3016 void
3017 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid)
3018 {
3019         int b;
3020         int i;
3021
3022         VM_OBJECT_ASSERT_WLOCKED(m->object);
3023         /*
3024          * Scan the valid bits looking for invalid sections that
3025          * must be zeroed.  Invalid sub-DEV_BSIZE'd areas ( where the
3026          * valid bit may be set ) have already been zeroed by
3027          * vm_page_set_validclean().
3028          */
3029         for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) {
3030                 if (i == (PAGE_SIZE / DEV_BSIZE) || 
3031                     (m->valid & ((vm_page_bits_t)1 << i))) {
3032                         if (i > b) {
3033                                 pmap_zero_page_area(m, 
3034                                     b << DEV_BSHIFT, (i - b) << DEV_BSHIFT);
3035                         }
3036                         b = i + 1;
3037                 }
3038         }
3039
3040         /*
3041          * setvalid is TRUE when we can safely set the zero'd areas
3042          * as being valid.  We can do this if there are no cache consistancy
3043          * issues.  e.g. it is ok to do with UFS, but not ok to do with NFS.
3044          */
3045         if (setvalid)
3046                 m->valid = VM_PAGE_BITS_ALL;
3047 }
3048
3049 /*
3050  *      vm_page_is_valid:
3051  *
3052  *      Is (partial) page valid?  Note that the case where size == 0
3053  *      will return FALSE in the degenerate case where the page is
3054  *      entirely invalid, and TRUE otherwise.
3055  */
3056 int
3057 vm_page_is_valid(vm_page_t m, int base, int size)
3058 {
3059         vm_page_bits_t bits;
3060
3061         VM_OBJECT_ASSERT_LOCKED(m->object);
3062         bits = vm_page_bits(base, size);
3063         return (m->valid != 0 && (m->valid & bits) == bits);
3064 }
3065
3066 /*
3067  *      vm_page_ps_is_valid:
3068  *
3069  *      Returns TRUE if the entire (super)page is valid and FALSE otherwise.
3070  */
3071 boolean_t
3072 vm_page_ps_is_valid(vm_page_t m)
3073 {
3074         int i, npages;
3075
3076         VM_OBJECT_ASSERT_LOCKED(m->object);
3077         npages = atop(pagesizes[m->psind]);
3078
3079         /*
3080          * The physically contiguous pages that make up a superpage, i.e., a
3081          * page with a page size index ("psind") greater than zero, will
3082          * occupy adjacent entries in vm_page_array[].
3083          */
3084         for (i = 0; i < npages; i++) {
3085                 if (m[i].valid != VM_PAGE_BITS_ALL)
3086                         return (FALSE);
3087         }
3088         return (TRUE);
3089 }
3090
3091 /*
3092  * Set the page's dirty bits if the page is modified.
3093  */
3094 void
3095 vm_page_test_dirty(vm_page_t m)
3096 {
3097
3098         VM_OBJECT_ASSERT_WLOCKED(m->object);
3099         if (m->dirty != VM_PAGE_BITS_ALL && pmap_is_modified(m))
3100                 vm_page_dirty(m);
3101 }
3102
3103 void
3104 vm_page_lock_KBI(vm_page_t m, const char *file, int line)
3105 {
3106
3107         mtx_lock_flags_(vm_page_lockptr(m), 0, file, line);
3108 }
3109
3110 void
3111 vm_page_unlock_KBI(vm_page_t m, const char *file, int line)
3112 {
3113
3114         mtx_unlock_flags_(vm_page_lockptr(m), 0, file, line);
3115 }
3116
3117 int
3118 vm_page_trylock_KBI(vm_page_t m, const char *file, int line)
3119 {
3120
3121         return (mtx_trylock_flags_(vm_page_lockptr(m), 0, file, line));
3122 }
3123
3124 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
3125 void
3126 vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line)
3127 {
3128
3129         vm_page_lock_assert_KBI(m, MA_OWNED, file, line);
3130 }
3131
3132 void
3133 vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line)
3134 {
3135
3136         mtx_assert_(vm_page_lockptr(m), a, file, line);
3137 }
3138 #endif
3139
3140 #ifdef INVARIANTS
3141 void
3142 vm_page_object_lock_assert(vm_page_t m)
3143 {
3144
3145         /*
3146          * Certain of the page's fields may only be modified by the
3147          * holder of the containing object's lock or the exclusive busy.
3148          * holder.  Unfortunately, the holder of the write busy is
3149          * not recorded, and thus cannot be checked here.
3150          */
3151         if (m->object != NULL && !vm_page_xbusied(m))
3152                 VM_OBJECT_ASSERT_WLOCKED(m->object);
3153 }
3154
3155 void
3156 vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits)
3157 {
3158
3159         if ((bits & PGA_WRITEABLE) == 0)
3160                 return;
3161
3162         /*
3163          * The PGA_WRITEABLE flag can only be set if the page is
3164          * managed, is exclusively busied or the object is locked.
3165          * Currently, this flag is only set by pmap_enter().
3166          */
3167         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3168             ("PGA_WRITEABLE on unmanaged page"));
3169         if (!vm_page_xbusied(m))
3170                 VM_OBJECT_ASSERT_LOCKED(m->object);
3171 }
3172 #endif
3173
3174 #include "opt_ddb.h"
3175 #ifdef DDB
3176 #include <sys/kernel.h>
3177
3178 #include <ddb/ddb.h>
3179
3180 DB_SHOW_COMMAND(page, vm_page_print_page_info)
3181 {
3182         db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
3183         db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
3184         db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
3185         db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
3186         db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
3187         db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
3188         db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
3189         db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
3190         db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
3191         db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
3192 }
3193
3194 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
3195 {
3196         int dom;
3197
3198         db_printf("pq_free %d pq_cache %d\n",
3199             cnt.v_free_count, cnt.v_cache_count);
3200         for (dom = 0; dom < vm_ndomains; dom++) {
3201                 db_printf(
3202         "dom %d page_cnt %d free %d pq_act %d pq_inact %d pass %d\n",
3203                     dom,
3204                     vm_dom[dom].vmd_page_count,
3205                     vm_dom[dom].vmd_free_count,
3206                     vm_dom[dom].vmd_pagequeues[PQ_ACTIVE].pq_cnt,
3207                     vm_dom[dom].vmd_pagequeues[PQ_INACTIVE].pq_cnt,
3208                     vm_dom[dom].vmd_pass);
3209         }
3210 }
3211
3212 DB_SHOW_COMMAND(pginfo, vm_page_print_pginfo)
3213 {
3214         vm_page_t m;
3215         boolean_t phys;
3216
3217         if (!have_addr) {
3218                 db_printf("show pginfo addr\n");
3219                 return;
3220         }
3221
3222         phys = strchr(modif, 'p') != NULL;
3223         if (phys)
3224                 m = PHYS_TO_VM_PAGE(addr);
3225         else
3226                 m = (vm_page_t)addr;
3227         db_printf(
3228     "page %p obj %p pidx 0x%jx phys 0x%jx q %d hold %d wire %d\n"
3229     "  af 0x%x of 0x%x f 0x%x act %d busy %x valid 0x%x dirty 0x%x\n",
3230             m, m->object, (uintmax_t)m->pindex, (uintmax_t)m->phys_addr,
3231             m->queue, m->hold_count, m->wire_count, m->aflags, m->oflags,
3232             m->flags, m->act_count, m->busy_lock, m->valid, m->dirty);
3233 }
3234 #endif /* DDB */