Home | History | Annotate | Line # | Download | only in uvm
uvm_page.c revision 1.158
      1  1.157  uebayasi /*	$NetBSD: uvm_page.c,v 1.158 2010/11/11 14:50:54 uebayasi Exp $	*/
      2  1.155        ad 
      3  1.155        ad /*
      4  1.155        ad  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.155        ad  * All rights reserved.
      6  1.155        ad  *
      7  1.155        ad  * Redistribution and use in source and binary forms, with or without
      8  1.155        ad  * modification, are permitted provided that the following conditions
      9  1.155        ad  * are met:
     10  1.155        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.155        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.155        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.155        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.155        ad  *    documentation and/or other materials provided with the distribution.
     15  1.155        ad  *
     16  1.155        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.155        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.155        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.155        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.155        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.155        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.155        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.155        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.155        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.155        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.155        ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.155        ad  */
     28    1.1       mrg 
     29   1.62       chs /*
     30    1.1       mrg  * Copyright (c) 1997 Charles D. Cranor and Washington University.
     31   1.62       chs  * Copyright (c) 1991, 1993, The Regents of the University of California.
     32    1.1       mrg  *
     33    1.1       mrg  * All rights reserved.
     34    1.1       mrg  *
     35    1.1       mrg  * This code is derived from software contributed to Berkeley by
     36    1.1       mrg  * The Mach Operating System project at Carnegie-Mellon University.
     37    1.1       mrg  *
     38    1.1       mrg  * Redistribution and use in source and binary forms, with or without
     39    1.1       mrg  * modification, are permitted provided that the following conditions
     40    1.1       mrg  * are met:
     41    1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     42    1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     43    1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     44    1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     45    1.1       mrg  *    documentation and/or other materials provided with the distribution.
     46    1.1       mrg  * 3. All advertising materials mentioning features or use of this software
     47    1.1       mrg  *    must display the following acknowledgement:
     48    1.1       mrg  *	This product includes software developed by Charles D. Cranor,
     49   1.62       chs  *      Washington University, the University of California, Berkeley and
     50    1.1       mrg  *      its contributors.
     51    1.1       mrg  * 4. Neither the name of the University nor the names of its contributors
     52    1.1       mrg  *    may be used to endorse or promote products derived from this software
     53    1.1       mrg  *    without specific prior written permission.
     54    1.1       mrg  *
     55    1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56    1.1       mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57    1.1       mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58    1.1       mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59    1.1       mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60    1.1       mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61    1.1       mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62    1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63    1.1       mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64    1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65    1.1       mrg  * SUCH DAMAGE.
     66    1.1       mrg  *
     67    1.1       mrg  *	@(#)vm_page.c   8.3 (Berkeley) 3/21/94
     68    1.4       mrg  * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp
     69    1.1       mrg  *
     70    1.1       mrg  *
     71    1.1       mrg  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     72    1.1       mrg  * All rights reserved.
     73   1.62       chs  *
     74    1.1       mrg  * Permission to use, copy, modify and distribute this software and
     75    1.1       mrg  * its documentation is hereby granted, provided that both the copyright
     76    1.1       mrg  * notice and this permission notice appear in all copies of the
     77    1.1       mrg  * software, derivative works or modified versions, and any portions
     78    1.1       mrg  * thereof, and that both notices appear in supporting documentation.
     79   1.62       chs  *
     80   1.62       chs  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     81   1.62       chs  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     82    1.1       mrg  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     83   1.62       chs  *
     84    1.1       mrg  * Carnegie Mellon requests users of this software to return to
     85    1.1       mrg  *
     86    1.1       mrg  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     87    1.1       mrg  *  School of Computer Science
     88    1.1       mrg  *  Carnegie Mellon University
     89    1.1       mrg  *  Pittsburgh PA 15213-3890
     90    1.1       mrg  *
     91    1.1       mrg  * any improvements or extensions that they make and grant Carnegie the
     92    1.1       mrg  * rights to redistribute these changes.
     93    1.1       mrg  */
     94    1.1       mrg 
     95    1.1       mrg /*
     96    1.1       mrg  * uvm_page.c: page ops.
     97    1.1       mrg  */
     98   1.71     lukem 
     99   1.71     lukem #include <sys/cdefs.h>
    100  1.157  uebayasi __KERNEL_RCSID(0, "$NetBSD: uvm_page.c,v 1.158 2010/11/11 14:50:54 uebayasi Exp $");
    101    1.6       mrg 
    102  1.151   thorpej #include "opt_ddb.h"
    103   1.44       chs #include "opt_uvmhist.h"
    104  1.113      yamt #include "opt_readahead.h"
    105   1.44       chs 
    106    1.1       mrg #include <sys/param.h>
    107    1.1       mrg #include <sys/systm.h>
    108    1.1       mrg #include <sys/malloc.h>
    109   1.35   thorpej #include <sys/sched.h>
    110   1.44       chs #include <sys/kernel.h>
    111   1.51       chs #include <sys/vnode.h>
    112   1.68       chs #include <sys/proc.h>
    113  1.126        ad #include <sys/atomic.h>
    114  1.133        ad #include <sys/cpu.h>
    115    1.1       mrg 
    116    1.1       mrg #include <uvm/uvm.h>
    117  1.151   thorpej #include <uvm/uvm_ddb.h>
    118  1.113      yamt #include <uvm/uvm_pdpolicy.h>
    119    1.1       mrg 
    120    1.1       mrg /*
    121    1.1       mrg  * global vars... XXXCDC: move to uvm. structure.
    122    1.1       mrg  */
    123    1.1       mrg 
    124    1.1       mrg /*
    125    1.1       mrg  * physical memory config is stored in vm_physmem.
    126    1.1       mrg  */
    127    1.1       mrg 
    128    1.1       mrg struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];	/* XXXCDC: uvm.physmem */
    129    1.1       mrg int vm_nphysseg = 0;				/* XXXCDC: uvm.nphysseg */
    130  1.158  uebayasi #define	vm_nphysmem	vm_nphysseg
    131    1.1       mrg 
    132    1.1       mrg /*
    133   1.36   thorpej  * Some supported CPUs in a given architecture don't support all
    134   1.36   thorpej  * of the things necessary to do idle page zero'ing efficiently.
    135  1.155        ad  * We therefore provide a way to enable it from machdep code here.
    136   1.44       chs  */
    137  1.119   thorpej bool vm_page_zero_enable = false;
    138   1.34   thorpej 
    139   1.34   thorpej /*
    140  1.140        ad  * number of pages per-CPU to reserve for the kernel.
    141  1.140        ad  */
    142  1.140        ad int vm_page_reserve_kernel = 5;
    143  1.140        ad 
    144  1.140        ad /*
    145  1.148      matt  * physical memory size;
    146  1.148      matt  */
    147  1.149      matt int physmem;
    148  1.148      matt 
    149  1.148      matt /*
    150    1.1       mrg  * local variables
    151    1.1       mrg  */
    152    1.1       mrg 
    153    1.1       mrg /*
    154   1.88   thorpej  * these variables record the values returned by vm_page_bootstrap,
    155   1.88   thorpej  * for debugging purposes.  The implementation of uvm_pageboot_alloc
    156   1.88   thorpej  * and pmap_startup here also uses them internally.
    157   1.88   thorpej  */
    158   1.88   thorpej 
    159   1.88   thorpej static vaddr_t      virtual_space_start;
    160   1.88   thorpej static vaddr_t      virtual_space_end;
    161   1.88   thorpej 
    162   1.88   thorpej /*
    163   1.60   thorpej  * we allocate an initial number of page colors in uvm_page_init(),
    164   1.60   thorpej  * and remember them.  We may re-color pages as cache sizes are
    165   1.60   thorpej  * discovered during the autoconfiguration phase.  But we can never
    166   1.60   thorpej  * free the initial set of buckets, since they are allocated using
    167   1.60   thorpej  * uvm_pageboot_alloc().
    168   1.60   thorpej  */
    169   1.60   thorpej 
    170  1.119   thorpej static bool have_recolored_pages /* = false */;
    171   1.83   thorpej 
    172   1.83   thorpej MALLOC_DEFINE(M_VMPAGE, "VM page", "VM page");
    173   1.60   thorpej 
    174   1.91      yamt #ifdef DEBUG
    175   1.91      yamt vaddr_t uvm_zerocheckkva;
    176   1.91      yamt #endif /* DEBUG */
    177   1.91      yamt 
    178   1.60   thorpej /*
    179  1.134        ad  * local prototypes
    180  1.124        ad  */
    181  1.124        ad 
    182  1.153  uebayasi static void uvm_pageinsert(struct uvm_object *, struct vm_page *);
    183  1.153  uebayasi static void uvm_pageremove(struct uvm_object *, struct vm_page *);
    184  1.124        ad 
    185  1.124        ad /*
    186  1.134        ad  * per-object tree of pages
    187    1.1       mrg  */
    188    1.1       mrg 
    189  1.134        ad static signed int
    190  1.156     rmind uvm_page_compare_nodes(void *ctx, const void *n1, const void *n2)
    191  1.134        ad {
    192  1.156     rmind 	const struct vm_page *pg1 = n1;
    193  1.156     rmind 	const struct vm_page *pg2 = n2;
    194  1.134        ad 	const voff_t a = pg1->offset;
    195  1.134        ad 	const voff_t b = pg2->offset;
    196  1.134        ad 
    197  1.134        ad 	if (a < b)
    198  1.156     rmind 		return -1;
    199  1.156     rmind 	if (a > b)
    200  1.134        ad 		return 1;
    201  1.134        ad 	return 0;
    202  1.134        ad }
    203  1.134        ad 
    204  1.134        ad static signed int
    205  1.156     rmind uvm_page_compare_key(void *ctx, const void *n, const void *key)
    206  1.134        ad {
    207  1.156     rmind 	const struct vm_page *pg = n;
    208  1.134        ad 	const voff_t a = pg->offset;
    209  1.134        ad 	const voff_t b = *(const voff_t *)key;
    210  1.134        ad 
    211  1.134        ad 	if (a < b)
    212  1.156     rmind 		return -1;
    213  1.156     rmind 	if (a > b)
    214  1.134        ad 		return 1;
    215  1.134        ad 	return 0;
    216  1.134        ad }
    217  1.134        ad 
    218  1.156     rmind const rb_tree_ops_t uvm_page_tree_ops = {
    219  1.137      matt 	.rbto_compare_nodes = uvm_page_compare_nodes,
    220  1.137      matt 	.rbto_compare_key = uvm_page_compare_key,
    221  1.156     rmind 	.rbto_node_offset = offsetof(struct vm_page, rb_node),
    222  1.156     rmind 	.rbto_context = NULL
    223  1.134        ad };
    224    1.1       mrg 
    225    1.1       mrg /*
    226    1.1       mrg  * inline functions
    227    1.1       mrg  */
    228    1.1       mrg 
    229    1.1       mrg /*
    230  1.134        ad  * uvm_pageinsert: insert a page in the object.
    231    1.1       mrg  *
    232    1.1       mrg  * => caller must lock object
    233    1.1       mrg  * => caller must lock page queues
    234    1.1       mrg  * => call should have already set pg's object and offset pointers
    235    1.1       mrg  *    and bumped the version counter
    236    1.1       mrg  */
    237    1.1       mrg 
    238  1.136      yamt static inline void
    239  1.136      yamt uvm_pageinsert_list(struct uvm_object *uobj, struct vm_page *pg,
    240  1.136      yamt     struct vm_page *where)
    241    1.1       mrg {
    242    1.1       mrg 
    243  1.136      yamt 	KASSERT(uobj == pg->uobject);
    244  1.127        ad 	KASSERT(mutex_owned(&uobj->vmobjlock));
    245   1.51       chs 	KASSERT((pg->flags & PG_TABLED) == 0);
    246   1.96      yamt 	KASSERT(where == NULL || (where->flags & PG_TABLED));
    247   1.96      yamt 	KASSERT(where == NULL || (where->uobject == uobj));
    248  1.123        ad 
    249   1.94      yamt 	if (UVM_OBJ_IS_VNODE(uobj)) {
    250   1.94      yamt 		if (uobj->uo_npages == 0) {
    251   1.94      yamt 			struct vnode *vp = (struct vnode *)uobj;
    252   1.94      yamt 
    253   1.94      yamt 			vholdl(vp);
    254   1.94      yamt 		}
    255   1.94      yamt 		if (UVM_OBJ_IS_VTEXT(uobj)) {
    256  1.126        ad 			atomic_inc_uint(&uvmexp.execpages);
    257   1.94      yamt 		} else {
    258  1.126        ad 			atomic_inc_uint(&uvmexp.filepages);
    259   1.94      yamt 		}
    260   1.86      yamt 	} else if (UVM_OBJ_IS_AOBJ(uobj)) {
    261  1.126        ad 		atomic_inc_uint(&uvmexp.anonpages);
    262   1.78       chs 	}
    263   1.78       chs 
    264   1.96      yamt 	if (where)
    265  1.133        ad 		TAILQ_INSERT_AFTER(&uobj->memq, where, pg, listq.queue);
    266   1.96      yamt 	else
    267  1.133        ad 		TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
    268    1.7       mrg 	pg->flags |= PG_TABLED;
    269   1.67       chs 	uobj->uo_npages++;
    270    1.1       mrg }
    271    1.1       mrg 
    272  1.136      yamt 
    273  1.136      yamt static inline void
    274  1.136      yamt uvm_pageinsert_tree(struct uvm_object *uobj, struct vm_page *pg)
    275  1.136      yamt {
    276  1.156     rmind 	struct vm_page *ret;
    277  1.136      yamt 
    278  1.136      yamt 	KASSERT(uobj == pg->uobject);
    279  1.156     rmind 	ret = rb_tree_insert_node(&uobj->rb_tree, pg);
    280  1.156     rmind 	KASSERT(ret == pg);
    281  1.136      yamt }
    282  1.136      yamt 
    283  1.136      yamt static inline void
    284  1.153  uebayasi uvm_pageinsert(struct uvm_object *uobj, struct vm_page *pg)
    285   1.96      yamt {
    286   1.96      yamt 
    287  1.153  uebayasi 	KDASSERT(uobj != NULL);
    288  1.136      yamt 	uvm_pageinsert_tree(uobj, pg);
    289  1.136      yamt 	uvm_pageinsert_list(uobj, pg, NULL);
    290   1.96      yamt }
    291   1.96      yamt 
    292    1.1       mrg /*
    293  1.134        ad  * uvm_page_remove: remove page from object.
    294    1.1       mrg  *
    295    1.1       mrg  * => caller must lock object
    296    1.1       mrg  * => caller must lock page queues
    297    1.1       mrg  */
    298    1.1       mrg 
    299  1.109     perry static inline void
    300  1.136      yamt uvm_pageremove_list(struct uvm_object *uobj, struct vm_page *pg)
    301    1.1       mrg {
    302    1.1       mrg 
    303  1.136      yamt 	KASSERT(uobj == pg->uobject);
    304  1.127        ad 	KASSERT(mutex_owned(&uobj->vmobjlock));
    305   1.44       chs 	KASSERT(pg->flags & PG_TABLED);
    306  1.123        ad 
    307   1.94      yamt 	if (UVM_OBJ_IS_VNODE(uobj)) {
    308   1.94      yamt 		if (uobj->uo_npages == 1) {
    309   1.94      yamt 			struct vnode *vp = (struct vnode *)uobj;
    310   1.94      yamt 
    311   1.94      yamt 			holdrelel(vp);
    312   1.94      yamt 		}
    313   1.94      yamt 		if (UVM_OBJ_IS_VTEXT(uobj)) {
    314  1.126        ad 			atomic_dec_uint(&uvmexp.execpages);
    315   1.94      yamt 		} else {
    316  1.126        ad 			atomic_dec_uint(&uvmexp.filepages);
    317   1.94      yamt 		}
    318   1.78       chs 	} else if (UVM_OBJ_IS_AOBJ(uobj)) {
    319  1.126        ad 		atomic_dec_uint(&uvmexp.anonpages);
    320   1.51       chs 	}
    321   1.44       chs 
    322    1.7       mrg 	/* object should be locked */
    323   1.67       chs 	uobj->uo_npages--;
    324  1.133        ad 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    325    1.7       mrg 	pg->flags &= ~PG_TABLED;
    326    1.7       mrg 	pg->uobject = NULL;
    327    1.1       mrg }
    328    1.1       mrg 
    329  1.136      yamt static inline void
    330  1.136      yamt uvm_pageremove_tree(struct uvm_object *uobj, struct vm_page *pg)
    331  1.136      yamt {
    332  1.136      yamt 
    333  1.136      yamt 	KASSERT(uobj == pg->uobject);
    334  1.156     rmind 	rb_tree_remove_node(&uobj->rb_tree, pg);
    335  1.136      yamt }
    336  1.136      yamt 
    337  1.136      yamt static inline void
    338  1.153  uebayasi uvm_pageremove(struct uvm_object *uobj, struct vm_page *pg)
    339  1.136      yamt {
    340  1.136      yamt 
    341  1.153  uebayasi 	KDASSERT(uobj != NULL);
    342  1.136      yamt 	uvm_pageremove_tree(uobj, pg);
    343  1.136      yamt 	uvm_pageremove_list(uobj, pg);
    344  1.136      yamt }
    345  1.136      yamt 
    346   1.60   thorpej static void
    347   1.60   thorpej uvm_page_init_buckets(struct pgfreelist *pgfl)
    348   1.60   thorpej {
    349   1.60   thorpej 	int color, i;
    350   1.60   thorpej 
    351   1.60   thorpej 	for (color = 0; color < uvmexp.ncolors; color++) {
    352   1.60   thorpej 		for (i = 0; i < PGFL_NQUEUES; i++) {
    353  1.133        ad 			LIST_INIT(&pgfl->pgfl_buckets[color].pgfl_queues[i]);
    354   1.60   thorpej 		}
    355   1.60   thorpej 	}
    356   1.60   thorpej }
    357   1.60   thorpej 
    358    1.1       mrg /*
    359    1.1       mrg  * uvm_page_init: init the page system.   called from uvm_init().
    360   1.62       chs  *
    361    1.1       mrg  * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
    362    1.1       mrg  */
    363    1.1       mrg 
    364    1.7       mrg void
    365  1.105   thorpej uvm_page_init(vaddr_t *kvm_startp, vaddr_t *kvm_endp)
    366    1.1       mrg {
    367  1.155        ad 	static struct uvm_cpu boot_cpu;
    368  1.154       jym 	psize_t freepages, pagecount, bucketcount, n;
    369  1.133        ad 	struct pgflbucket *bucketarray, *cpuarray;
    370   1.63       chs 	struct vm_page *pagearray;
    371  1.158  uebayasi 	struct vm_physseg *seg;
    372   1.81   thorpej 	int lcv;
    373   1.81   thorpej 	u_int i;
    374   1.14       eeh 	paddr_t paddr;
    375    1.7       mrg 
    376  1.133        ad 	KASSERT(ncpu <= 1);
    377  1.138      matt 	CTASSERT(sizeof(pagearray->offset) >= sizeof(struct uvm_cpu *));
    378  1.133        ad 
    379    1.7       mrg 	/*
    380   1.60   thorpej 	 * init the page queues and page queue locks, except the free
    381   1.60   thorpej 	 * list; we allocate that later (with the initial vm_page
    382   1.60   thorpej 	 * structures).
    383    1.7       mrg 	 */
    384   1.51       chs 
    385  1.155        ad 	uvm.cpus[0] = &boot_cpu;
    386  1.155        ad 	curcpu()->ci_data.cpu_uvm = &boot_cpu;
    387  1.146      haad 	uvm_reclaim_init();
    388  1.113      yamt 	uvmpdpol_init();
    389  1.127        ad 	mutex_init(&uvm_pageqlock, MUTEX_DRIVER, IPL_NONE);
    390  1.123        ad 	mutex_init(&uvm_fpageqlock, MUTEX_DRIVER, IPL_VM);
    391    1.7       mrg 
    392    1.7       mrg 	/*
    393   1.51       chs 	 * allocate vm_page structures.
    394    1.7       mrg 	 */
    395    1.7       mrg 
    396    1.7       mrg 	/*
    397    1.7       mrg 	 * sanity check:
    398    1.7       mrg 	 * before calling this function the MD code is expected to register
    399    1.7       mrg 	 * some free RAM with the uvm_page_physload() function.   our job
    400    1.7       mrg 	 * now is to allocate vm_page structures for this memory.
    401    1.7       mrg 	 */
    402    1.7       mrg 
    403  1.158  uebayasi 	if (vm_nphysmem == 0)
    404   1.42       mrg 		panic("uvm_page_bootstrap: no memory pre-allocated");
    405   1.62       chs 
    406    1.7       mrg 	/*
    407   1.62       chs 	 * first calculate the number of free pages...
    408    1.7       mrg 	 *
    409    1.7       mrg 	 * note that we use start/end rather than avail_start/avail_end.
    410    1.7       mrg 	 * this allows us to allocate extra vm_page structures in case we
    411    1.7       mrg 	 * want to return some memory to the pool after booting.
    412    1.7       mrg 	 */
    413   1.62       chs 
    414    1.7       mrg 	freepages = 0;
    415  1.158  uebayasi 	for (lcv = 0 ; lcv < vm_nphysmem ; lcv++) {
    416  1.158  uebayasi 		seg = VM_PHYSMEM_PTR(lcv);
    417  1.158  uebayasi 		freepages += (seg->end - seg->start);
    418  1.158  uebayasi 	}
    419    1.7       mrg 
    420    1.7       mrg 	/*
    421   1.60   thorpej 	 * Let MD code initialize the number of colors, or default
    422   1.60   thorpej 	 * to 1 color if MD code doesn't care.
    423   1.60   thorpej 	 */
    424   1.60   thorpej 	if (uvmexp.ncolors == 0)
    425   1.60   thorpej 		uvmexp.ncolors = 1;
    426   1.60   thorpej 	uvmexp.colormask = uvmexp.ncolors - 1;
    427   1.60   thorpej 
    428   1.60   thorpej 	/*
    429    1.7       mrg 	 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
    430    1.7       mrg 	 * use.   for each page of memory we use we need a vm_page structure.
    431    1.7       mrg 	 * thus, the total number of pages we can use is the total size of
    432    1.7       mrg 	 * the memory divided by the PAGE_SIZE plus the size of the vm_page
    433    1.7       mrg 	 * structure.   we add one to freepages as a fudge factor to avoid
    434    1.7       mrg 	 * truncation errors (since we can only allocate in terms of whole
    435    1.7       mrg 	 * pages).
    436    1.7       mrg 	 */
    437   1.62       chs 
    438   1.60   thorpej 	bucketcount = uvmexp.ncolors * VM_NFREELIST;
    439   1.15       chs 	pagecount = ((freepages + 1) << PAGE_SHIFT) /
    440    1.7       mrg 	    (PAGE_SIZE + sizeof(struct vm_page));
    441   1.60   thorpej 
    442   1.67       chs 	bucketarray = (void *)uvm_pageboot_alloc((bucketcount *
    443  1.133        ad 	    sizeof(struct pgflbucket) * 2) + (pagecount *
    444   1.60   thorpej 	    sizeof(struct vm_page)));
    445  1.133        ad 	cpuarray = bucketarray + bucketcount;
    446  1.133        ad 	pagearray = (struct vm_page *)(bucketarray + bucketcount * 2);
    447   1.60   thorpej 
    448   1.60   thorpej 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    449   1.60   thorpej 		uvm.page_free[lcv].pgfl_buckets =
    450   1.60   thorpej 		    (bucketarray + (lcv * uvmexp.ncolors));
    451   1.60   thorpej 		uvm_page_init_buckets(&uvm.page_free[lcv]);
    452  1.155        ad 		uvm.cpus[0]->page_free[lcv].pgfl_buckets =
    453  1.133        ad 		    (cpuarray + (lcv * uvmexp.ncolors));
    454  1.155        ad 		uvm_page_init_buckets(&uvm.cpus[0]->page_free[lcv]);
    455   1.60   thorpej 	}
    456   1.13     perry 	memset(pagearray, 0, pagecount * sizeof(struct vm_page));
    457   1.62       chs 
    458    1.7       mrg 	/*
    459   1.51       chs 	 * init the vm_page structures and put them in the correct place.
    460    1.7       mrg 	 */
    461    1.7       mrg 
    462  1.158  uebayasi 	for (lcv = 0 ; lcv < vm_nphysmem ; lcv++) {
    463  1.158  uebayasi 		seg = VM_PHYSMEM_PTR(lcv);
    464  1.158  uebayasi 		n = seg->end - seg->start;
    465   1.51       chs 
    466    1.7       mrg 		/* set up page array pointers */
    467  1.158  uebayasi 		seg->pgs = pagearray;
    468    1.7       mrg 		pagearray += n;
    469    1.7       mrg 		pagecount -= n;
    470  1.158  uebayasi 		seg->lastpg = seg->pgs + (n - 1);
    471    1.7       mrg 
    472   1.13     perry 		/* init and free vm_pages (we've already zeroed them) */
    473  1.158  uebayasi 		paddr = ctob(seg->start);
    474    1.7       mrg 		for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
    475  1.158  uebayasi 			seg->pgs[i].phys_addr = paddr;
    476   1.56   thorpej #ifdef __HAVE_VM_PAGE_MD
    477  1.158  uebayasi 			VM_MDPAGE_INIT(&seg->pgs[i]);
    478   1.56   thorpej #endif
    479  1.158  uebayasi 			if (atop(paddr) >= seg->avail_start &&
    480  1.158  uebayasi 			    atop(paddr) <= seg->avail_end) {
    481    1.7       mrg 				uvmexp.npages++;
    482    1.7       mrg 				/* add page to free pool */
    483  1.158  uebayasi 				uvm_pagefree(&seg->pgs[i]);
    484    1.7       mrg 			}
    485    1.7       mrg 		}
    486    1.7       mrg 	}
    487   1.44       chs 
    488    1.7       mrg 	/*
    489   1.88   thorpej 	 * pass up the values of virtual_space_start and
    490   1.88   thorpej 	 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
    491   1.88   thorpej 	 * layers of the VM.
    492   1.88   thorpej 	 */
    493   1.88   thorpej 
    494   1.88   thorpej 	*kvm_startp = round_page(virtual_space_start);
    495   1.88   thorpej 	*kvm_endp = trunc_page(virtual_space_end);
    496   1.91      yamt #ifdef DEBUG
    497   1.91      yamt 	/*
    498   1.91      yamt 	 * steal kva for uvm_pagezerocheck().
    499   1.91      yamt 	 */
    500   1.91      yamt 	uvm_zerocheckkva = *kvm_startp;
    501   1.91      yamt 	*kvm_startp += PAGE_SIZE;
    502   1.91      yamt #endif /* DEBUG */
    503   1.88   thorpej 
    504   1.88   thorpej 	/*
    505   1.51       chs 	 * init various thresholds.
    506    1.7       mrg 	 */
    507   1.51       chs 
    508    1.7       mrg 	uvmexp.reserve_pagedaemon = 1;
    509  1.140        ad 	uvmexp.reserve_kernel = vm_page_reserve_kernel;
    510    1.7       mrg 
    511    1.7       mrg 	/*
    512   1.51       chs 	 * determine if we should zero pages in the idle loop.
    513   1.34   thorpej 	 */
    514   1.51       chs 
    515  1.155        ad 	uvm.cpus[0]->page_idle_zero = vm_page_zero_enable;
    516   1.34   thorpej 
    517   1.34   thorpej 	/*
    518    1.7       mrg 	 * done!
    519    1.7       mrg 	 */
    520    1.1       mrg 
    521  1.119   thorpej 	uvm.page_init_done = true;
    522    1.1       mrg }
    523    1.1       mrg 
    524    1.1       mrg /*
    525    1.1       mrg  * uvm_setpagesize: set the page size
    526   1.62       chs  *
    527    1.1       mrg  * => sets page_shift and page_mask from uvmexp.pagesize.
    528   1.62       chs  */
    529    1.1       mrg 
    530    1.7       mrg void
    531  1.105   thorpej uvm_setpagesize(void)
    532    1.1       mrg {
    533   1.85   thorpej 
    534   1.85   thorpej 	/*
    535   1.85   thorpej 	 * If uvmexp.pagesize is 0 at this point, we expect PAGE_SIZE
    536   1.85   thorpej 	 * to be a constant (indicated by being a non-zero value).
    537   1.85   thorpej 	 */
    538   1.85   thorpej 	if (uvmexp.pagesize == 0) {
    539   1.85   thorpej 		if (PAGE_SIZE == 0)
    540   1.85   thorpej 			panic("uvm_setpagesize: uvmexp.pagesize not set");
    541   1.85   thorpej 		uvmexp.pagesize = PAGE_SIZE;
    542   1.85   thorpej 	}
    543    1.7       mrg 	uvmexp.pagemask = uvmexp.pagesize - 1;
    544    1.7       mrg 	if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
    545    1.7       mrg 		panic("uvm_setpagesize: page size not a power of two");
    546    1.7       mrg 	for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
    547    1.7       mrg 		if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
    548    1.7       mrg 			break;
    549    1.1       mrg }
    550    1.1       mrg 
    551    1.1       mrg /*
    552    1.1       mrg  * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
    553    1.1       mrg  */
    554    1.1       mrg 
    555   1.14       eeh vaddr_t
    556  1.105   thorpej uvm_pageboot_alloc(vsize_t size)
    557    1.1       mrg {
    558  1.119   thorpej 	static bool initialized = false;
    559   1.14       eeh 	vaddr_t addr;
    560   1.52   thorpej #if !defined(PMAP_STEAL_MEMORY)
    561   1.52   thorpej 	vaddr_t vaddr;
    562   1.14       eeh 	paddr_t paddr;
    563   1.52   thorpej #endif
    564    1.1       mrg 
    565    1.7       mrg 	/*
    566   1.19   thorpej 	 * on first call to this function, initialize ourselves.
    567    1.7       mrg 	 */
    568  1.119   thorpej 	if (initialized == false) {
    569   1.88   thorpej 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
    570    1.1       mrg 
    571    1.7       mrg 		/* round it the way we like it */
    572   1.88   thorpej 		virtual_space_start = round_page(virtual_space_start);
    573   1.88   thorpej 		virtual_space_end = trunc_page(virtual_space_end);
    574   1.19   thorpej 
    575  1.119   thorpej 		initialized = true;
    576    1.7       mrg 	}
    577   1.52   thorpej 
    578   1.52   thorpej 	/* round to page size */
    579   1.52   thorpej 	size = round_page(size);
    580   1.52   thorpej 
    581   1.52   thorpej #if defined(PMAP_STEAL_MEMORY)
    582   1.52   thorpej 
    583   1.62       chs 	/*
    584   1.62       chs 	 * defer bootstrap allocation to MD code (it may want to allocate
    585   1.52   thorpej 	 * from a direct-mapped segment).  pmap_steal_memory should adjust
    586   1.88   thorpej 	 * virtual_space_start/virtual_space_end if necessary.
    587   1.52   thorpej 	 */
    588   1.52   thorpej 
    589   1.88   thorpej 	addr = pmap_steal_memory(size, &virtual_space_start,
    590   1.88   thorpej 	    &virtual_space_end);
    591   1.52   thorpej 
    592   1.52   thorpej 	return(addr);
    593   1.52   thorpej 
    594   1.52   thorpej #else /* !PMAP_STEAL_MEMORY */
    595    1.1       mrg 
    596    1.7       mrg 	/*
    597    1.7       mrg 	 * allocate virtual memory for this request
    598    1.7       mrg 	 */
    599   1.88   thorpej 	if (virtual_space_start == virtual_space_end ||
    600   1.88   thorpej 	    (virtual_space_end - virtual_space_start) < size)
    601   1.19   thorpej 		panic("uvm_pageboot_alloc: out of virtual space");
    602   1.20   thorpej 
    603   1.88   thorpej 	addr = virtual_space_start;
    604   1.20   thorpej 
    605   1.20   thorpej #ifdef PMAP_GROWKERNEL
    606   1.20   thorpej 	/*
    607   1.20   thorpej 	 * If the kernel pmap can't map the requested space,
    608   1.20   thorpej 	 * then allocate more resources for it.
    609   1.20   thorpej 	 */
    610   1.20   thorpej 	if (uvm_maxkaddr < (addr + size)) {
    611   1.20   thorpej 		uvm_maxkaddr = pmap_growkernel(addr + size);
    612   1.20   thorpej 		if (uvm_maxkaddr < (addr + size))
    613   1.20   thorpej 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
    614   1.19   thorpej 	}
    615   1.20   thorpej #endif
    616    1.1       mrg 
    617   1.88   thorpej 	virtual_space_start += size;
    618    1.1       mrg 
    619    1.9   thorpej 	/*
    620    1.7       mrg 	 * allocate and mapin physical pages to back new virtual pages
    621    1.7       mrg 	 */
    622    1.1       mrg 
    623    1.7       mrg 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
    624    1.7       mrg 	    vaddr += PAGE_SIZE) {
    625    1.1       mrg 
    626    1.7       mrg 		if (!uvm_page_physget(&paddr))
    627    1.7       mrg 			panic("uvm_pageboot_alloc: out of memory");
    628    1.1       mrg 
    629   1.23   thorpej 		/*
    630   1.23   thorpej 		 * Note this memory is no longer managed, so using
    631   1.23   thorpej 		 * pmap_kenter is safe.
    632   1.23   thorpej 		 */
    633  1.152    cegger 		pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE, 0);
    634    1.7       mrg 	}
    635   1.66     chris 	pmap_update(pmap_kernel());
    636    1.7       mrg 	return(addr);
    637    1.1       mrg #endif	/* PMAP_STEAL_MEMORY */
    638    1.1       mrg }
    639    1.1       mrg 
    640    1.1       mrg #if !defined(PMAP_STEAL_MEMORY)
    641    1.1       mrg /*
    642    1.1       mrg  * uvm_page_physget: "steal" one page from the vm_physmem structure.
    643    1.1       mrg  *
    644    1.1       mrg  * => attempt to allocate it off the end of a segment in which the "avail"
    645    1.1       mrg  *    values match the start/end values.   if we can't do that, then we
    646    1.1       mrg  *    will advance both values (making them equal, and removing some
    647    1.1       mrg  *    vm_page structures from the non-avail area).
    648    1.1       mrg  * => return false if out of memory.
    649    1.1       mrg  */
    650    1.1       mrg 
    651   1.28  drochner /* subroutine: try to allocate from memory chunks on the specified freelist */
    652  1.118   thorpej static bool uvm_page_physget_freelist(paddr_t *, int);
    653   1.28  drochner 
    654  1.118   thorpej static bool
    655  1.105   thorpej uvm_page_physget_freelist(paddr_t *paddrp, int freelist)
    656    1.1       mrg {
    657    1.7       mrg 	int lcv, x;
    658    1.1       mrg 
    659    1.7       mrg 	/* pass 1: try allocating from a matching end */
    660    1.1       mrg #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    661  1.158  uebayasi 	for (lcv = vm_nphysmem - 1 ; lcv >= 0 ; lcv--)
    662    1.1       mrg #else
    663  1.158  uebayasi 	for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
    664    1.1       mrg #endif
    665    1.7       mrg 	{
    666    1.1       mrg 
    667  1.119   thorpej 		if (uvm.page_init_done == true)
    668   1.42       mrg 			panic("uvm_page_physget: called _after_ bootstrap");
    669    1.1       mrg 
    670   1.28  drochner 		if (vm_physmem[lcv].free_list != freelist)
    671   1.28  drochner 			continue;
    672   1.28  drochner 
    673    1.7       mrg 		/* try from front */
    674    1.7       mrg 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
    675    1.7       mrg 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    676  1.154       jym 			*paddrp = ctob(vm_physmem[lcv].avail_start);
    677    1.7       mrg 			vm_physmem[lcv].avail_start++;
    678    1.7       mrg 			vm_physmem[lcv].start++;
    679    1.7       mrg 			/* nothing left?   nuke it */
    680    1.7       mrg 			if (vm_physmem[lcv].avail_start ==
    681    1.7       mrg 			    vm_physmem[lcv].end) {
    682  1.158  uebayasi 				if (vm_nphysmem == 1)
    683   1.89       wiz 				    panic("uvm_page_physget: out of memory!");
    684  1.158  uebayasi 				vm_nphysmem--;
    685  1.158  uebayasi 				for (x = lcv ; x < vm_nphysmem ; x++)
    686    1.7       mrg 					/* structure copy */
    687  1.158  uebayasi 					VM_PHYSMEM_PTR_SWAP(x, x + 1);
    688    1.7       mrg 			}
    689  1.119   thorpej 			return (true);
    690    1.7       mrg 		}
    691    1.7       mrg 
    692    1.7       mrg 		/* try from rear */
    693    1.7       mrg 		if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
    694    1.7       mrg 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    695  1.154       jym 			*paddrp = ctob(vm_physmem[lcv].avail_end - 1);
    696    1.7       mrg 			vm_physmem[lcv].avail_end--;
    697    1.7       mrg 			vm_physmem[lcv].end--;
    698    1.7       mrg 			/* nothing left?   nuke it */
    699    1.7       mrg 			if (vm_physmem[lcv].avail_end ==
    700    1.7       mrg 			    vm_physmem[lcv].start) {
    701  1.158  uebayasi 				if (vm_nphysmem == 1)
    702   1.42       mrg 				    panic("uvm_page_physget: out of memory!");
    703  1.158  uebayasi 				vm_nphysmem--;
    704  1.158  uebayasi 				for (x = lcv ; x < vm_nphysmem ; x++)
    705    1.7       mrg 					/* structure copy */
    706  1.158  uebayasi 					VM_PHYSMEM_PTR_SWAP(x, x + 1);
    707    1.7       mrg 			}
    708  1.119   thorpej 			return (true);
    709    1.7       mrg 		}
    710    1.7       mrg 	}
    711    1.1       mrg 
    712    1.7       mrg 	/* pass2: forget about matching ends, just allocate something */
    713    1.1       mrg #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    714  1.158  uebayasi 	for (lcv = vm_nphysmem - 1 ; lcv >= 0 ; lcv--)
    715    1.1       mrg #else
    716  1.158  uebayasi 	for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
    717    1.1       mrg #endif
    718    1.7       mrg 	{
    719    1.1       mrg 
    720    1.7       mrg 		/* any room in this bank? */
    721    1.7       mrg 		if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
    722    1.7       mrg 			continue;  /* nope */
    723    1.7       mrg 
    724  1.154       jym 		*paddrp = ctob(vm_physmem[lcv].avail_start);
    725    1.7       mrg 		vm_physmem[lcv].avail_start++;
    726    1.7       mrg 		/* truncate! */
    727    1.7       mrg 		vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
    728    1.7       mrg 
    729    1.7       mrg 		/* nothing left?   nuke it */
    730    1.7       mrg 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
    731  1.158  uebayasi 			if (vm_nphysmem == 1)
    732   1.42       mrg 				panic("uvm_page_physget: out of memory!");
    733  1.158  uebayasi 			vm_nphysmem--;
    734  1.158  uebayasi 			for (x = lcv ; x < vm_nphysmem ; x++)
    735    1.7       mrg 				/* structure copy */
    736  1.158  uebayasi 				VM_PHYSMEM_PTR_SWAP(x, x + 1);
    737    1.7       mrg 		}
    738  1.119   thorpej 		return (true);
    739    1.7       mrg 	}
    740    1.1       mrg 
    741  1.119   thorpej 	return (false);        /* whoops! */
    742   1.28  drochner }
    743   1.28  drochner 
    744  1.118   thorpej bool
    745  1.105   thorpej uvm_page_physget(paddr_t *paddrp)
    746   1.28  drochner {
    747   1.28  drochner 	int i;
    748   1.28  drochner 
    749   1.28  drochner 	/* try in the order of freelist preference */
    750   1.28  drochner 	for (i = 0; i < VM_NFREELIST; i++)
    751  1.119   thorpej 		if (uvm_page_physget_freelist(paddrp, i) == true)
    752  1.119   thorpej 			return (true);
    753  1.119   thorpej 	return (false);
    754    1.1       mrg }
    755    1.1       mrg #endif /* PMAP_STEAL_MEMORY */
    756    1.1       mrg 
    757    1.1       mrg /*
    758    1.1       mrg  * uvm_page_physload: load physical memory into VM system
    759    1.1       mrg  *
    760    1.1       mrg  * => all args are PFs
    761    1.1       mrg  * => all pages in start/end get vm_page structures
    762    1.1       mrg  * => areas marked by avail_start/avail_end get added to the free page pool
    763    1.1       mrg  * => we are limited to VM_PHYSSEG_MAX physical memory segments
    764    1.1       mrg  */
    765    1.1       mrg 
    766    1.7       mrg void
    767  1.105   thorpej uvm_page_physload(paddr_t start, paddr_t end, paddr_t avail_start,
    768  1.105   thorpej     paddr_t avail_end, int free_list)
    769    1.1       mrg {
    770   1.14       eeh 	int preload, lcv;
    771   1.14       eeh 	psize_t npages;
    772    1.7       mrg 	struct vm_page *pgs;
    773    1.7       mrg 	struct vm_physseg *ps;
    774    1.7       mrg 
    775    1.7       mrg 	if (uvmexp.pagesize == 0)
    776   1.42       mrg 		panic("uvm_page_physload: page size not set!");
    777   1.12   thorpej 	if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
    778   1.79    provos 		panic("uvm_page_physload: bad free list %d", free_list);
    779   1.26  drochner 	if (start >= end)
    780   1.26  drochner 		panic("uvm_page_physload: start >= end");
    781   1.12   thorpej 
    782    1.7       mrg 	/*
    783    1.7       mrg 	 * do we have room?
    784    1.7       mrg 	 */
    785   1.67       chs 
    786  1.158  uebayasi 	if (vm_nphysmem == VM_PHYSSEG_MAX) {
    787   1.42       mrg 		printf("uvm_page_physload: unable to load physical memory "
    788    1.7       mrg 		    "segment\n");
    789   1.37      soda 		printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
    790   1.37      soda 		    VM_PHYSSEG_MAX, (long long)start, (long long)end);
    791   1.43  christos 		printf("\tincrease VM_PHYSSEG_MAX\n");
    792    1.7       mrg 		return;
    793    1.7       mrg 	}
    794    1.7       mrg 
    795    1.7       mrg 	/*
    796    1.7       mrg 	 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
    797    1.7       mrg 	 * called yet, so malloc is not available).
    798    1.7       mrg 	 */
    799   1.67       chs 
    800  1.158  uebayasi 	for (lcv = 0 ; lcv < vm_nphysmem ; lcv++) {
    801    1.7       mrg 		if (vm_physmem[lcv].pgs)
    802    1.7       mrg 			break;
    803    1.7       mrg 	}
    804  1.158  uebayasi 	preload = (lcv == vm_nphysmem);
    805    1.7       mrg 
    806    1.7       mrg 	/*
    807    1.7       mrg 	 * if VM is already running, attempt to malloc() vm_page structures
    808    1.7       mrg 	 */
    809   1.67       chs 
    810    1.7       mrg 	if (!preload) {
    811   1.42       mrg 		panic("uvm_page_physload: tried to add RAM after vm_mem_init");
    812    1.7       mrg 	} else {
    813    1.7       mrg 		pgs = NULL;
    814    1.7       mrg 		npages = 0;
    815    1.7       mrg 	}
    816    1.1       mrg 
    817    1.7       mrg 	/*
    818    1.7       mrg 	 * now insert us in the proper place in vm_physmem[]
    819    1.7       mrg 	 */
    820    1.1       mrg 
    821    1.1       mrg #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
    822    1.7       mrg 	/* random: put it at the end (easy!) */
    823  1.158  uebayasi 	ps = &vm_physmem[vm_nphysmem];
    824    1.1       mrg #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
    825    1.7       mrg 	{
    826    1.7       mrg 		int x;
    827    1.7       mrg 		/* sort by address for binary search */
    828  1.158  uebayasi 		for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
    829    1.7       mrg 			if (start < vm_physmem[lcv].start)
    830    1.7       mrg 				break;
    831    1.7       mrg 		ps = &vm_physmem[lcv];
    832    1.7       mrg 		/* move back other entries, if necessary ... */
    833  1.158  uebayasi 		for (x = vm_nphysmem ; x > lcv ; x--)
    834    1.7       mrg 			/* structure copy */
    835  1.158  uebayasi 			VM_PHYSMEM_PTR_SWAP(x, x - 1);
    836    1.7       mrg 	}
    837    1.1       mrg #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    838    1.7       mrg 	{
    839    1.7       mrg 		int x;
    840    1.7       mrg 		/* sort by largest segment first */
    841  1.158  uebayasi 		for (lcv = 0 ; lcv < vm_nphysmem ; lcv++)
    842    1.7       mrg 			if ((end - start) >
    843    1.7       mrg 			    (vm_physmem[lcv].end - vm_physmem[lcv].start))
    844    1.7       mrg 				break;
    845    1.7       mrg 		ps = &vm_physmem[lcv];
    846    1.7       mrg 		/* move back other entries, if necessary ... */
    847  1.158  uebayasi 		for (x = vm_nphysmem ; x > lcv ; x--)
    848    1.7       mrg 			/* structure copy */
    849  1.158  uebayasi 			VM_PHYSMEM_PTR_SWAP(x, x - 1);
    850    1.7       mrg 	}
    851    1.1       mrg #else
    852   1.42       mrg 	panic("uvm_page_physload: unknown physseg strategy selected!");
    853    1.1       mrg #endif
    854    1.1       mrg 
    855    1.7       mrg 	ps->start = start;
    856    1.7       mrg 	ps->end = end;
    857    1.7       mrg 	ps->avail_start = avail_start;
    858    1.7       mrg 	ps->avail_end = avail_end;
    859    1.7       mrg 	if (preload) {
    860    1.7       mrg 		ps->pgs = NULL;
    861    1.7       mrg 	} else {
    862    1.7       mrg 		ps->pgs = pgs;
    863    1.7       mrg 		ps->lastpg = pgs + npages - 1;
    864    1.7       mrg 	}
    865   1.12   thorpej 	ps->free_list = free_list;
    866  1.158  uebayasi 	vm_nphysmem++;
    867    1.7       mrg 
    868  1.113      yamt 	if (!preload) {
    869  1.113      yamt 		uvmpdpol_reinit();
    870  1.113      yamt 	}
    871    1.1       mrg }
    872    1.1       mrg 
    873    1.1       mrg /*
    874   1.60   thorpej  * uvm_page_recolor: Recolor the pages if the new bucket count is
    875   1.60   thorpej  * larger than the old one.
    876   1.60   thorpej  */
    877   1.60   thorpej 
    878   1.60   thorpej void
    879   1.60   thorpej uvm_page_recolor(int newncolors)
    880   1.60   thorpej {
    881  1.133        ad 	struct pgflbucket *bucketarray, *cpuarray, *oldbucketarray;
    882  1.133        ad 	struct pgfreelist gpgfl, pgfl;
    883   1.63       chs 	struct vm_page *pg;
    884   1.60   thorpej 	vsize_t bucketcount;
    885  1.123        ad 	int lcv, color, i, ocolors;
    886  1.133        ad 	struct uvm_cpu *ucpu;
    887   1.60   thorpej 
    888   1.60   thorpej 	if (newncolors <= uvmexp.ncolors)
    889   1.60   thorpej 		return;
    890   1.77  wrstuden 
    891  1.119   thorpej 	if (uvm.page_init_done == false) {
    892   1.77  wrstuden 		uvmexp.ncolors = newncolors;
    893   1.77  wrstuden 		return;
    894   1.77  wrstuden 	}
    895   1.60   thorpej 
    896   1.60   thorpej 	bucketcount = newncolors * VM_NFREELIST;
    897  1.133        ad 	bucketarray = malloc(bucketcount * sizeof(struct pgflbucket) * 2,
    898   1.60   thorpej 	    M_VMPAGE, M_NOWAIT);
    899  1.133        ad 	cpuarray = bucketarray + bucketcount;
    900   1.60   thorpej 	if (bucketarray == NULL) {
    901   1.60   thorpej 		printf("WARNING: unable to allocate %ld page color buckets\n",
    902   1.60   thorpej 		    (long) bucketcount);
    903   1.60   thorpej 		return;
    904   1.60   thorpej 	}
    905   1.60   thorpej 
    906  1.123        ad 	mutex_spin_enter(&uvm_fpageqlock);
    907   1.60   thorpej 
    908   1.60   thorpej 	/* Make sure we should still do this. */
    909   1.60   thorpej 	if (newncolors <= uvmexp.ncolors) {
    910  1.123        ad 		mutex_spin_exit(&uvm_fpageqlock);
    911   1.60   thorpej 		free(bucketarray, M_VMPAGE);
    912   1.60   thorpej 		return;
    913   1.60   thorpej 	}
    914   1.60   thorpej 
    915   1.60   thorpej 	oldbucketarray = uvm.page_free[0].pgfl_buckets;
    916   1.60   thorpej 	ocolors = uvmexp.ncolors;
    917   1.60   thorpej 
    918   1.60   thorpej 	uvmexp.ncolors = newncolors;
    919   1.60   thorpej 	uvmexp.colormask = uvmexp.ncolors - 1;
    920   1.60   thorpej 
    921  1.133        ad 	ucpu = curcpu()->ci_data.cpu_uvm;
    922   1.60   thorpej 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    923  1.133        ad 		gpgfl.pgfl_buckets = (bucketarray + (lcv * newncolors));
    924  1.133        ad 		pgfl.pgfl_buckets = (cpuarray + (lcv * uvmexp.ncolors));
    925  1.133        ad 		uvm_page_init_buckets(&gpgfl);
    926   1.60   thorpej 		uvm_page_init_buckets(&pgfl);
    927   1.60   thorpej 		for (color = 0; color < ocolors; color++) {
    928   1.60   thorpej 			for (i = 0; i < PGFL_NQUEUES; i++) {
    929  1.133        ad 				while ((pg = LIST_FIRST(&uvm.page_free[
    930   1.60   thorpej 				    lcv].pgfl_buckets[color].pgfl_queues[i]))
    931   1.60   thorpej 				    != NULL) {
    932  1.133        ad 					LIST_REMOVE(pg, pageq.list); /* global */
    933  1.133        ad 					LIST_REMOVE(pg, listq.list); /* cpu */
    934  1.133        ad 					LIST_INSERT_HEAD(&gpgfl.pgfl_buckets[
    935  1.133        ad 					    VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
    936  1.133        ad 					    i], pg, pageq.list);
    937  1.133        ad 					LIST_INSERT_HEAD(&pgfl.pgfl_buckets[
    938   1.60   thorpej 					    VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
    939  1.133        ad 					    i], pg, listq.list);
    940   1.60   thorpej 				}
    941   1.60   thorpej 			}
    942   1.60   thorpej 		}
    943  1.133        ad 		uvm.page_free[lcv].pgfl_buckets = gpgfl.pgfl_buckets;
    944  1.133        ad 		ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
    945   1.60   thorpej 	}
    946   1.60   thorpej 
    947   1.60   thorpej 	if (have_recolored_pages) {
    948  1.123        ad 		mutex_spin_exit(&uvm_fpageqlock);
    949   1.60   thorpej 		free(oldbucketarray, M_VMPAGE);
    950   1.60   thorpej 		return;
    951   1.60   thorpej 	}
    952   1.60   thorpej 
    953  1.119   thorpej 	have_recolored_pages = true;
    954  1.123        ad 	mutex_spin_exit(&uvm_fpageqlock);
    955   1.60   thorpej }
    956    1.1       mrg 
    957    1.1       mrg /*
    958  1.133        ad  * uvm_cpu_attach: initialize per-CPU data structures.
    959  1.133        ad  */
    960  1.133        ad 
    961  1.133        ad void
    962  1.133        ad uvm_cpu_attach(struct cpu_info *ci)
    963  1.133        ad {
    964  1.133        ad 	struct pgflbucket *bucketarray;
    965  1.133        ad 	struct pgfreelist pgfl;
    966  1.133        ad 	struct uvm_cpu *ucpu;
    967  1.133        ad 	vsize_t bucketcount;
    968  1.133        ad 	int lcv;
    969  1.133        ad 
    970  1.133        ad 	if (CPU_IS_PRIMARY(ci)) {
    971  1.133        ad 		/* Already done in uvm_page_init(). */
    972  1.133        ad 		return;
    973  1.133        ad 	}
    974  1.133        ad 
    975  1.140        ad 	/* Add more reserve pages for this CPU. */
    976  1.140        ad 	uvmexp.reserve_kernel += vm_page_reserve_kernel;
    977  1.140        ad 
    978  1.140        ad 	/* Configure this CPU's free lists. */
    979  1.133        ad 	bucketcount = uvmexp.ncolors * VM_NFREELIST;
    980  1.133        ad 	bucketarray = malloc(bucketcount * sizeof(struct pgflbucket),
    981  1.133        ad 	    M_VMPAGE, M_WAITOK);
    982  1.155        ad 	ucpu = kmem_zalloc(sizeof(*ucpu), KM_SLEEP);
    983  1.155        ad 	uvm.cpus[cpu_index(ci)] = ucpu;
    984  1.133        ad 	ci->ci_data.cpu_uvm = ucpu;
    985  1.133        ad 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    986  1.133        ad 		pgfl.pgfl_buckets = (bucketarray + (lcv * uvmexp.ncolors));
    987  1.133        ad 		uvm_page_init_buckets(&pgfl);
    988  1.133        ad 		ucpu->page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
    989  1.133        ad 	}
    990  1.133        ad }
    991  1.133        ad 
    992  1.133        ad /*
    993   1.54   thorpej  * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat
    994   1.54   thorpej  */
    995   1.54   thorpej 
    996  1.114   thorpej static struct vm_page *
    997  1.133        ad uvm_pagealloc_pgfl(struct uvm_cpu *ucpu, int flist, int try1, int try2,
    998   1.69    simonb     int *trycolorp)
    999   1.54   thorpej {
   1000  1.133        ad 	struct pgflist *freeq;
   1001   1.54   thorpej 	struct vm_page *pg;
   1002   1.58     enami 	int color, trycolor = *trycolorp;
   1003  1.133        ad 	struct pgfreelist *gpgfl, *pgfl;
   1004   1.54   thorpej 
   1005  1.130        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
   1006  1.130        ad 
   1007   1.58     enami 	color = trycolor;
   1008  1.133        ad 	pgfl = &ucpu->page_free[flist];
   1009  1.133        ad 	gpgfl = &uvm.page_free[flist];
   1010   1.58     enami 	do {
   1011  1.133        ad 		/* cpu, try1 */
   1012  1.133        ad 		if ((pg = LIST_FIRST((freeq =
   1013  1.133        ad 		    &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) {
   1014  1.133        ad 			VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--;
   1015  1.133        ad 		    	uvmexp.cpuhit++;
   1016  1.133        ad 			goto gotit;
   1017  1.133        ad 		}
   1018  1.133        ad 		/* global, try1 */
   1019  1.133        ad 		if ((pg = LIST_FIRST((freeq =
   1020  1.133        ad 		    &gpgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL) {
   1021  1.133        ad 			VM_FREE_PAGE_TO_CPU(pg)->pages[try1]--;
   1022  1.133        ad 		    	uvmexp.cpumiss++;
   1023   1.54   thorpej 			goto gotit;
   1024  1.133        ad 		}
   1025  1.133        ad 		/* cpu, try2 */
   1026  1.133        ad 		if ((pg = LIST_FIRST((freeq =
   1027  1.133        ad 		    &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) {
   1028  1.133        ad 			VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--;
   1029  1.133        ad 		    	uvmexp.cpuhit++;
   1030   1.54   thorpej 			goto gotit;
   1031  1.133        ad 		}
   1032  1.133        ad 		/* global, try2 */
   1033  1.133        ad 		if ((pg = LIST_FIRST((freeq =
   1034  1.133        ad 		    &gpgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL) {
   1035  1.133        ad 			VM_FREE_PAGE_TO_CPU(pg)->pages[try2]--;
   1036  1.133        ad 		    	uvmexp.cpumiss++;
   1037  1.133        ad 			goto gotit;
   1038  1.133        ad 		}
   1039   1.60   thorpej 		color = (color + 1) & uvmexp.colormask;
   1040   1.58     enami 	} while (color != trycolor);
   1041   1.54   thorpej 
   1042   1.54   thorpej 	return (NULL);
   1043   1.54   thorpej 
   1044   1.54   thorpej  gotit:
   1045  1.133        ad 	LIST_REMOVE(pg, pageq.list);	/* global list */
   1046  1.133        ad 	LIST_REMOVE(pg, listq.list);	/* per-cpu list */
   1047   1.54   thorpej 	uvmexp.free--;
   1048   1.54   thorpej 
   1049   1.54   thorpej 	/* update zero'd page count */
   1050   1.54   thorpej 	if (pg->flags & PG_ZERO)
   1051   1.54   thorpej 		uvmexp.zeropages--;
   1052   1.54   thorpej 
   1053   1.54   thorpej 	if (color == trycolor)
   1054   1.54   thorpej 		uvmexp.colorhit++;
   1055   1.54   thorpej 	else {
   1056   1.54   thorpej 		uvmexp.colormiss++;
   1057   1.54   thorpej 		*trycolorp = color;
   1058   1.54   thorpej 	}
   1059   1.54   thorpej 
   1060   1.54   thorpej 	return (pg);
   1061   1.54   thorpej }
   1062   1.54   thorpej 
   1063   1.54   thorpej /*
   1064   1.12   thorpej  * uvm_pagealloc_strat: allocate vm_page from a particular free list.
   1065    1.1       mrg  *
   1066    1.1       mrg  * => return null if no pages free
   1067    1.1       mrg  * => wake up pagedaemon if number of free pages drops below low water mark
   1068  1.133        ad  * => if obj != NULL, obj must be locked (to put in obj's tree)
   1069    1.1       mrg  * => if anon != NULL, anon must be locked (to put in anon)
   1070    1.1       mrg  * => only one of obj or anon can be non-null
   1071    1.1       mrg  * => caller must activate/deactivate page if it is not wired.
   1072   1.12   thorpej  * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
   1073   1.34   thorpej  * => policy decision: it is more important to pull a page off of the
   1074   1.34   thorpej  *	appropriate priority free list than it is to get a zero'd or
   1075   1.34   thorpej  *	unknown contents page.  This is because we live with the
   1076   1.34   thorpej  *	consequences of a bad free list decision for the entire
   1077   1.34   thorpej  *	lifetime of the page, e.g. if the page comes from memory that
   1078   1.34   thorpej  *	is slower to access.
   1079    1.1       mrg  */
   1080    1.1       mrg 
   1081    1.7       mrg struct vm_page *
   1082  1.105   thorpej uvm_pagealloc_strat(struct uvm_object *obj, voff_t off, struct vm_anon *anon,
   1083  1.105   thorpej     int flags, int strat, int free_list)
   1084    1.1       mrg {
   1085  1.123        ad 	int lcv, try1, try2, zeroit = 0, color;
   1086  1.133        ad 	struct uvm_cpu *ucpu;
   1087    1.7       mrg 	struct vm_page *pg;
   1088  1.141        ad 	lwp_t *l;
   1089    1.1       mrg 
   1090   1.44       chs 	KASSERT(obj == NULL || anon == NULL);
   1091  1.113      yamt 	KASSERT(anon == NULL || off == 0);
   1092   1.44       chs 	KASSERT(off == trunc_page(off));
   1093  1.127        ad 	KASSERT(obj == NULL || mutex_owned(&obj->vmobjlock));
   1094  1.127        ad 	KASSERT(anon == NULL || mutex_owned(&anon->an_lock));
   1095   1.48   thorpej 
   1096  1.123        ad 	mutex_spin_enter(&uvm_fpageqlock);
   1097    1.1       mrg 
   1098    1.7       mrg 	/*
   1099   1.54   thorpej 	 * This implements a global round-robin page coloring
   1100   1.54   thorpej 	 * algorithm.
   1101   1.54   thorpej 	 *
   1102   1.54   thorpej 	 * XXXJRT: What about virtually-indexed caches?
   1103   1.54   thorpej 	 */
   1104   1.67       chs 
   1105  1.133        ad 	ucpu = curcpu()->ci_data.cpu_uvm;
   1106  1.133        ad 	color = ucpu->page_free_nextcolor;
   1107   1.54   thorpej 
   1108   1.54   thorpej 	/*
   1109    1.7       mrg 	 * check to see if we need to generate some free pages waking
   1110    1.7       mrg 	 * the pagedaemon.
   1111    1.7       mrg 	 */
   1112    1.7       mrg 
   1113  1.113      yamt 	uvm_kick_pdaemon();
   1114    1.7       mrg 
   1115    1.7       mrg 	/*
   1116    1.7       mrg 	 * fail if any of these conditions is true:
   1117    1.7       mrg 	 * [1]  there really are no free pages, or
   1118    1.7       mrg 	 * [2]  only kernel "reserved" pages remain and
   1119  1.141        ad 	 *        reserved pages have not been requested.
   1120    1.7       mrg 	 * [3]  only pagedaemon "reserved" pages remain and
   1121    1.7       mrg 	 *        the requestor isn't the pagedaemon.
   1122  1.141        ad 	 * we make kernel reserve pages available if called by a
   1123  1.141        ad 	 * kernel thread or a realtime thread.
   1124    1.7       mrg 	 */
   1125  1.141        ad 	l = curlwp;
   1126  1.141        ad 	if (__predict_true(l != NULL) && lwp_eprio(l) >= PRI_KTHREAD) {
   1127  1.141        ad 		flags |= UVM_PGA_USERESERVE;
   1128  1.141        ad 	}
   1129  1.141        ad 	if ((uvmexp.free <= uvmexp.reserve_kernel &&
   1130  1.141        ad 	    (flags & UVM_PGA_USERESERVE) == 0) ||
   1131    1.7       mrg 	    (uvmexp.free <= uvmexp.reserve_pagedaemon &&
   1132  1.141        ad 	     curlwp != uvm.pagedaemon_lwp))
   1133   1.12   thorpej 		goto fail;
   1134   1.12   thorpej 
   1135   1.34   thorpej #if PGFL_NQUEUES != 2
   1136   1.34   thorpej #error uvm_pagealloc_strat needs to be updated
   1137   1.34   thorpej #endif
   1138   1.34   thorpej 
   1139   1.34   thorpej 	/*
   1140   1.34   thorpej 	 * If we want a zero'd page, try the ZEROS queue first, otherwise
   1141   1.34   thorpej 	 * we try the UNKNOWN queue first.
   1142   1.34   thorpej 	 */
   1143   1.34   thorpej 	if (flags & UVM_PGA_ZERO) {
   1144   1.34   thorpej 		try1 = PGFL_ZEROS;
   1145   1.34   thorpej 		try2 = PGFL_UNKNOWN;
   1146   1.34   thorpej 	} else {
   1147   1.34   thorpej 		try1 = PGFL_UNKNOWN;
   1148   1.34   thorpej 		try2 = PGFL_ZEROS;
   1149   1.34   thorpej 	}
   1150   1.34   thorpej 
   1151   1.12   thorpej  again:
   1152   1.12   thorpej 	switch (strat) {
   1153   1.12   thorpej 	case UVM_PGA_STRAT_NORMAL:
   1154  1.145       abs 		/* Check freelists: descending priority (ascending id) order */
   1155   1.12   thorpej 		for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
   1156  1.133        ad 			pg = uvm_pagealloc_pgfl(ucpu, lcv,
   1157   1.54   thorpej 			    try1, try2, &color);
   1158   1.54   thorpej 			if (pg != NULL)
   1159   1.12   thorpej 				goto gotit;
   1160   1.12   thorpej 		}
   1161   1.12   thorpej 
   1162   1.12   thorpej 		/* No pages free! */
   1163   1.12   thorpej 		goto fail;
   1164   1.12   thorpej 
   1165   1.12   thorpej 	case UVM_PGA_STRAT_ONLY:
   1166   1.12   thorpej 	case UVM_PGA_STRAT_FALLBACK:
   1167   1.12   thorpej 		/* Attempt to allocate from the specified free list. */
   1168   1.44       chs 		KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
   1169  1.133        ad 		pg = uvm_pagealloc_pgfl(ucpu, free_list,
   1170   1.54   thorpej 		    try1, try2, &color);
   1171   1.54   thorpej 		if (pg != NULL)
   1172   1.12   thorpej 			goto gotit;
   1173   1.12   thorpej 
   1174   1.12   thorpej 		/* Fall back, if possible. */
   1175   1.12   thorpej 		if (strat == UVM_PGA_STRAT_FALLBACK) {
   1176   1.12   thorpej 			strat = UVM_PGA_STRAT_NORMAL;
   1177   1.12   thorpej 			goto again;
   1178   1.12   thorpej 		}
   1179   1.12   thorpej 
   1180   1.12   thorpej 		/* No pages free! */
   1181   1.12   thorpej 		goto fail;
   1182   1.12   thorpej 
   1183   1.12   thorpej 	default:
   1184   1.12   thorpej 		panic("uvm_pagealloc_strat: bad strat %d", strat);
   1185   1.12   thorpej 		/* NOTREACHED */
   1186    1.7       mrg 	}
   1187    1.7       mrg 
   1188   1.12   thorpej  gotit:
   1189   1.54   thorpej 	/*
   1190   1.54   thorpej 	 * We now know which color we actually allocated from; set
   1191   1.54   thorpej 	 * the next color accordingly.
   1192   1.54   thorpej 	 */
   1193   1.67       chs 
   1194  1.133        ad 	ucpu->page_free_nextcolor = (color + 1) & uvmexp.colormask;
   1195   1.34   thorpej 
   1196   1.34   thorpej 	/*
   1197   1.34   thorpej 	 * update allocation statistics and remember if we have to
   1198   1.34   thorpej 	 * zero the page
   1199   1.34   thorpej 	 */
   1200   1.67       chs 
   1201   1.34   thorpej 	if (flags & UVM_PGA_ZERO) {
   1202   1.34   thorpej 		if (pg->flags & PG_ZERO) {
   1203   1.34   thorpej 			uvmexp.pga_zerohit++;
   1204   1.34   thorpej 			zeroit = 0;
   1205   1.34   thorpej 		} else {
   1206   1.34   thorpej 			uvmexp.pga_zeromiss++;
   1207   1.34   thorpej 			zeroit = 1;
   1208   1.34   thorpej 		}
   1209  1.133        ad 		if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) {
   1210  1.133        ad 			ucpu->page_idle_zero = vm_page_zero_enable;
   1211  1.133        ad 		}
   1212   1.34   thorpej 	}
   1213  1.143  drochner 	KASSERT(pg->pqflags == PQ_FREE);
   1214    1.7       mrg 
   1215    1.7       mrg 	pg->offset = off;
   1216    1.7       mrg 	pg->uobject = obj;
   1217    1.7       mrg 	pg->uanon = anon;
   1218    1.7       mrg 	pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
   1219    1.7       mrg 	if (anon) {
   1220  1.103      yamt 		anon->an_page = pg;
   1221    1.7       mrg 		pg->pqflags = PQ_ANON;
   1222  1.126        ad 		atomic_inc_uint(&uvmexp.anonpages);
   1223    1.7       mrg 	} else {
   1224   1.67       chs 		if (obj) {
   1225  1.153  uebayasi 			uvm_pageinsert(obj, pg);
   1226   1.67       chs 		}
   1227    1.7       mrg 		pg->pqflags = 0;
   1228    1.7       mrg 	}
   1229  1.143  drochner 	mutex_spin_exit(&uvm_fpageqlock);
   1230  1.143  drochner 
   1231    1.1       mrg #if defined(UVM_PAGE_TRKOWN)
   1232    1.7       mrg 	pg->owner_tag = NULL;
   1233    1.1       mrg #endif
   1234    1.7       mrg 	UVM_PAGE_OWN(pg, "new alloc");
   1235   1.33   thorpej 
   1236   1.33   thorpej 	if (flags & UVM_PGA_ZERO) {
   1237   1.33   thorpej 		/*
   1238   1.34   thorpej 		 * A zero'd page is not clean.  If we got a page not already
   1239   1.34   thorpej 		 * zero'd, then we have to zero it ourselves.
   1240   1.33   thorpej 		 */
   1241   1.33   thorpej 		pg->flags &= ~PG_CLEAN;
   1242   1.34   thorpej 		if (zeroit)
   1243   1.34   thorpej 			pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1244   1.33   thorpej 	}
   1245    1.1       mrg 
   1246    1.7       mrg 	return(pg);
   1247   1.12   thorpej 
   1248   1.12   thorpej  fail:
   1249  1.123        ad 	mutex_spin_exit(&uvm_fpageqlock);
   1250   1.12   thorpej 	return (NULL);
   1251    1.1       mrg }
   1252    1.1       mrg 
   1253    1.1       mrg /*
   1254   1.96      yamt  * uvm_pagereplace: replace a page with another
   1255   1.96      yamt  *
   1256   1.96      yamt  * => object must be locked
   1257   1.96      yamt  */
   1258   1.96      yamt 
   1259   1.96      yamt void
   1260  1.105   thorpej uvm_pagereplace(struct vm_page *oldpg, struct vm_page *newpg)
   1261   1.96      yamt {
   1262  1.136      yamt 	struct uvm_object *uobj = oldpg->uobject;
   1263   1.97  junyoung 
   1264   1.96      yamt 	KASSERT((oldpg->flags & PG_TABLED) != 0);
   1265  1.136      yamt 	KASSERT(uobj != NULL);
   1266   1.96      yamt 	KASSERT((newpg->flags & PG_TABLED) == 0);
   1267   1.96      yamt 	KASSERT(newpg->uobject == NULL);
   1268  1.136      yamt 	KASSERT(mutex_owned(&uobj->vmobjlock));
   1269   1.96      yamt 
   1270  1.136      yamt 	newpg->uobject = uobj;
   1271   1.96      yamt 	newpg->offset = oldpg->offset;
   1272   1.96      yamt 
   1273  1.136      yamt 	uvm_pageremove_tree(uobj, oldpg);
   1274  1.136      yamt 	uvm_pageinsert_tree(uobj, newpg);
   1275  1.136      yamt 	uvm_pageinsert_list(uobj, newpg, oldpg);
   1276  1.136      yamt 	uvm_pageremove_list(uobj, oldpg);
   1277   1.96      yamt }
   1278   1.96      yamt 
   1279   1.96      yamt /*
   1280    1.1       mrg  * uvm_pagerealloc: reallocate a page from one object to another
   1281    1.1       mrg  *
   1282    1.1       mrg  * => both objects must be locked
   1283    1.1       mrg  */
   1284    1.1       mrg 
   1285    1.7       mrg void
   1286  1.105   thorpej uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff)
   1287    1.1       mrg {
   1288    1.7       mrg 	/*
   1289    1.7       mrg 	 * remove it from the old object
   1290    1.7       mrg 	 */
   1291    1.7       mrg 
   1292    1.7       mrg 	if (pg->uobject) {
   1293  1.153  uebayasi 		uvm_pageremove(pg->uobject, pg);
   1294    1.7       mrg 	}
   1295    1.7       mrg 
   1296    1.7       mrg 	/*
   1297    1.7       mrg 	 * put it in the new object
   1298    1.7       mrg 	 */
   1299    1.7       mrg 
   1300    1.7       mrg 	if (newobj) {
   1301    1.7       mrg 		pg->uobject = newobj;
   1302    1.7       mrg 		pg->offset = newoff;
   1303  1.153  uebayasi 		uvm_pageinsert(newobj, pg);
   1304    1.7       mrg 	}
   1305    1.1       mrg }
   1306    1.1       mrg 
   1307   1.91      yamt #ifdef DEBUG
   1308   1.91      yamt /*
   1309   1.91      yamt  * check if page is zero-filled
   1310   1.91      yamt  *
   1311   1.91      yamt  *  - called with free page queue lock held.
   1312   1.91      yamt  */
   1313   1.91      yamt void
   1314   1.91      yamt uvm_pagezerocheck(struct vm_page *pg)
   1315   1.91      yamt {
   1316   1.91      yamt 	int *p, *ep;
   1317   1.91      yamt 
   1318   1.91      yamt 	KASSERT(uvm_zerocheckkva != 0);
   1319  1.123        ad 	KASSERT(mutex_owned(&uvm_fpageqlock));
   1320   1.91      yamt 
   1321   1.91      yamt 	/*
   1322   1.91      yamt 	 * XXX assuming pmap_kenter_pa and pmap_kremove never call
   1323   1.91      yamt 	 * uvm page allocator.
   1324   1.91      yamt 	 *
   1325   1.95       wiz 	 * it might be better to have "CPU-local temporary map" pmap interface.
   1326   1.91      yamt 	 */
   1327  1.152    cegger 	pmap_kenter_pa(uvm_zerocheckkva, VM_PAGE_TO_PHYS(pg), VM_PROT_READ, 0);
   1328   1.91      yamt 	p = (int *)uvm_zerocheckkva;
   1329   1.91      yamt 	ep = (int *)((char *)p + PAGE_SIZE);
   1330   1.92      yamt 	pmap_update(pmap_kernel());
   1331   1.91      yamt 	while (p < ep) {
   1332   1.91      yamt 		if (*p != 0)
   1333   1.91      yamt 			panic("PG_ZERO page isn't zero-filled");
   1334   1.91      yamt 		p++;
   1335   1.91      yamt 	}
   1336   1.91      yamt 	pmap_kremove(uvm_zerocheckkva, PAGE_SIZE);
   1337  1.131      yamt 	/*
   1338  1.131      yamt 	 * pmap_update() is not necessary here because no one except us
   1339  1.131      yamt 	 * uses this VA.
   1340  1.131      yamt 	 */
   1341   1.91      yamt }
   1342   1.91      yamt #endif /* DEBUG */
   1343   1.91      yamt 
   1344    1.1       mrg /*
   1345    1.1       mrg  * uvm_pagefree: free page
   1346    1.1       mrg  *
   1347  1.133        ad  * => erase page's identity (i.e. remove from object)
   1348    1.1       mrg  * => put page on free list
   1349    1.1       mrg  * => caller must lock owning object (either anon or uvm_object)
   1350    1.1       mrg  * => caller must lock page queues
   1351    1.1       mrg  * => assumes all valid mappings of pg are gone
   1352    1.1       mrg  */
   1353    1.1       mrg 
   1354   1.44       chs void
   1355  1.105   thorpej uvm_pagefree(struct vm_page *pg)
   1356    1.1       mrg {
   1357  1.133        ad 	struct pgflist *pgfl;
   1358  1.133        ad 	struct uvm_cpu *ucpu;
   1359  1.133        ad 	int index, color, queue;
   1360  1.118   thorpej 	bool iszero;
   1361   1.67       chs 
   1362   1.44       chs #ifdef DEBUG
   1363   1.44       chs 	if (pg->uobject == (void *)0xdeadbeef &&
   1364   1.44       chs 	    pg->uanon == (void *)0xdeadbeef) {
   1365   1.79    provos 		panic("uvm_pagefree: freeing free page %p", pg);
   1366   1.44       chs 	}
   1367   1.91      yamt #endif /* DEBUG */
   1368   1.44       chs 
   1369  1.123        ad 	KASSERT((pg->flags & PG_PAGEOUT) == 0);
   1370  1.143  drochner 	KASSERT(!(pg->pqflags & PQ_FREE));
   1371  1.128      yamt 	KASSERT(mutex_owned(&uvm_pageqlock) || !uvmpdpol_pageisqueued_p(pg));
   1372  1.128      yamt 	KASSERT(pg->uobject == NULL || mutex_owned(&pg->uobject->vmobjlock));
   1373  1.127        ad 	KASSERT(pg->uobject != NULL || pg->uanon == NULL ||
   1374  1.127        ad 		mutex_owned(&pg->uanon->an_lock));
   1375  1.123        ad 
   1376    1.7       mrg 	/*
   1377   1.67       chs 	 * if the page is loaned, resolve the loan instead of freeing.
   1378    1.7       mrg 	 */
   1379    1.7       mrg 
   1380   1.67       chs 	if (pg->loan_count) {
   1381   1.70       chs 		KASSERT(pg->wire_count == 0);
   1382    1.7       mrg 
   1383    1.7       mrg 		/*
   1384   1.67       chs 		 * if the page is owned by an anon then we just want to
   1385   1.70       chs 		 * drop anon ownership.  the kernel will free the page when
   1386   1.70       chs 		 * it is done with it.  if the page is owned by an object,
   1387   1.70       chs 		 * remove it from the object and mark it dirty for the benefit
   1388   1.70       chs 		 * of possible anon owners.
   1389   1.70       chs 		 *
   1390   1.70       chs 		 * regardless of previous ownership, wakeup any waiters,
   1391   1.70       chs 		 * unbusy the page, and we're done.
   1392    1.7       mrg 		 */
   1393    1.7       mrg 
   1394   1.73       chs 		if (pg->uobject != NULL) {
   1395  1.153  uebayasi 			uvm_pageremove(pg->uobject, pg);
   1396   1.67       chs 			pg->flags &= ~PG_CLEAN;
   1397   1.73       chs 		} else if (pg->uanon != NULL) {
   1398   1.73       chs 			if ((pg->pqflags & PQ_ANON) == 0) {
   1399   1.73       chs 				pg->loan_count--;
   1400   1.73       chs 			} else {
   1401   1.73       chs 				pg->pqflags &= ~PQ_ANON;
   1402  1.126        ad 				atomic_dec_uint(&uvmexp.anonpages);
   1403   1.73       chs 			}
   1404  1.103      yamt 			pg->uanon->an_page = NULL;
   1405   1.73       chs 			pg->uanon = NULL;
   1406   1.67       chs 		}
   1407   1.70       chs 		if (pg->flags & PG_WANTED) {
   1408   1.70       chs 			wakeup(pg);
   1409   1.70       chs 		}
   1410   1.84  perseant 		pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED|PG_PAGER1);
   1411   1.70       chs #ifdef UVM_PAGE_TRKOWN
   1412   1.70       chs 		pg->owner_tag = NULL;
   1413   1.70       chs #endif
   1414   1.73       chs 		if (pg->loan_count) {
   1415  1.115      yamt 			KASSERT(pg->uobject == NULL);
   1416  1.115      yamt 			if (pg->uanon == NULL) {
   1417  1.115      yamt 				uvm_pagedequeue(pg);
   1418  1.115      yamt 			}
   1419   1.73       chs 			return;
   1420   1.73       chs 		}
   1421   1.67       chs 	}
   1422   1.62       chs 
   1423   1.67       chs 	/*
   1424   1.67       chs 	 * remove page from its object or anon.
   1425   1.67       chs 	 */
   1426   1.44       chs 
   1427   1.73       chs 	if (pg->uobject != NULL) {
   1428  1.153  uebayasi 		uvm_pageremove(pg->uobject, pg);
   1429   1.73       chs 	} else if (pg->uanon != NULL) {
   1430  1.103      yamt 		pg->uanon->an_page = NULL;
   1431  1.126        ad 		atomic_dec_uint(&uvmexp.anonpages);
   1432    1.7       mrg 	}
   1433    1.1       mrg 
   1434    1.7       mrg 	/*
   1435   1.70       chs 	 * now remove the page from the queues.
   1436    1.7       mrg 	 */
   1437    1.7       mrg 
   1438   1.67       chs 	uvm_pagedequeue(pg);
   1439    1.7       mrg 
   1440    1.7       mrg 	/*
   1441    1.7       mrg 	 * if the page was wired, unwire it now.
   1442    1.7       mrg 	 */
   1443   1.44       chs 
   1444   1.34   thorpej 	if (pg->wire_count) {
   1445    1.7       mrg 		pg->wire_count = 0;
   1446    1.7       mrg 		uvmexp.wired--;
   1447   1.44       chs 	}
   1448    1.7       mrg 
   1449    1.7       mrg 	/*
   1450   1.44       chs 	 * and put on free queue
   1451    1.7       mrg 	 */
   1452    1.7       mrg 
   1453   1.90      yamt 	iszero = (pg->flags & PG_ZERO);
   1454  1.133        ad 	index = uvm_page_lookup_freelist(pg);
   1455  1.133        ad 	color = VM_PGCOLOR_BUCKET(pg);
   1456  1.133        ad 	queue = (iszero ? PGFL_ZEROS : PGFL_UNKNOWN);
   1457   1.34   thorpej 
   1458    1.3       chs #ifdef DEBUG
   1459    1.7       mrg 	pg->uobject = (void *)0xdeadbeef;
   1460    1.7       mrg 	pg->uanon = (void *)0xdeadbeef;
   1461    1.3       chs #endif
   1462   1.90      yamt 
   1463  1.123        ad 	mutex_spin_enter(&uvm_fpageqlock);
   1464  1.143  drochner 	pg->pqflags = PQ_FREE;
   1465   1.91      yamt 
   1466   1.91      yamt #ifdef DEBUG
   1467   1.91      yamt 	if (iszero)
   1468   1.91      yamt 		uvm_pagezerocheck(pg);
   1469   1.91      yamt #endif /* DEBUG */
   1470   1.91      yamt 
   1471  1.133        ad 
   1472  1.133        ad 	/* global list */
   1473  1.133        ad 	pgfl = &uvm.page_free[index].pgfl_buckets[color].pgfl_queues[queue];
   1474  1.133        ad 	LIST_INSERT_HEAD(pgfl, pg, pageq.list);
   1475    1.7       mrg 	uvmexp.free++;
   1476  1.133        ad 	if (iszero) {
   1477   1.90      yamt 		uvmexp.zeropages++;
   1478  1.133        ad 	}
   1479   1.34   thorpej 
   1480  1.133        ad 	/* per-cpu list */
   1481  1.133        ad 	ucpu = curcpu()->ci_data.cpu_uvm;
   1482  1.133        ad 	pg->offset = (uintptr_t)ucpu;
   1483  1.133        ad 	pgfl = &ucpu->page_free[index].pgfl_buckets[color].pgfl_queues[queue];
   1484  1.133        ad 	LIST_INSERT_HEAD(pgfl, pg, listq.list);
   1485  1.133        ad 	ucpu->pages[queue]++;
   1486  1.133        ad 	if (ucpu->pages[PGFL_ZEROS] < ucpu->pages[PGFL_UNKNOWN]) {
   1487  1.133        ad 		ucpu->page_idle_zero = vm_page_zero_enable;
   1488  1.133        ad 	}
   1489   1.34   thorpej 
   1490  1.123        ad 	mutex_spin_exit(&uvm_fpageqlock);
   1491   1.44       chs }
   1492   1.44       chs 
   1493   1.44       chs /*
   1494   1.44       chs  * uvm_page_unbusy: unbusy an array of pages.
   1495   1.44       chs  *
   1496   1.44       chs  * => pages must either all belong to the same object, or all belong to anons.
   1497   1.44       chs  * => if pages are object-owned, object must be locked.
   1498   1.67       chs  * => if pages are anon-owned, anons must be locked.
   1499   1.76     enami  * => caller must lock page queues if pages may be released.
   1500   1.98      yamt  * => caller must make sure that anon-owned pages are not PG_RELEASED.
   1501   1.44       chs  */
   1502   1.44       chs 
   1503   1.44       chs void
   1504  1.105   thorpej uvm_page_unbusy(struct vm_page **pgs, int npgs)
   1505   1.44       chs {
   1506   1.44       chs 	struct vm_page *pg;
   1507   1.44       chs 	int i;
   1508   1.44       chs 	UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
   1509   1.44       chs 
   1510   1.44       chs 	for (i = 0; i < npgs; i++) {
   1511   1.44       chs 		pg = pgs[i];
   1512   1.82     enami 		if (pg == NULL || pg == PGO_DONTCARE) {
   1513   1.44       chs 			continue;
   1514   1.44       chs 		}
   1515   1.98      yamt 
   1516  1.127        ad 		KASSERT(pg->uobject == NULL ||
   1517  1.127        ad 		    mutex_owned(&pg->uobject->vmobjlock));
   1518  1.127        ad 		KASSERT(pg->uobject != NULL ||
   1519  1.128      yamt 		    (pg->uanon != NULL && mutex_owned(&pg->uanon->an_lock)));
   1520   1.98      yamt 
   1521   1.98      yamt 		KASSERT(pg->flags & PG_BUSY);
   1522   1.98      yamt 		KASSERT((pg->flags & PG_PAGEOUT) == 0);
   1523   1.44       chs 		if (pg->flags & PG_WANTED) {
   1524   1.44       chs 			wakeup(pg);
   1525   1.44       chs 		}
   1526   1.44       chs 		if (pg->flags & PG_RELEASED) {
   1527   1.44       chs 			UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
   1528   1.98      yamt 			KASSERT(pg->uobject != NULL ||
   1529   1.98      yamt 			    (pg->uanon != NULL && pg->uanon->an_ref > 0));
   1530   1.67       chs 			pg->flags &= ~PG_RELEASED;
   1531   1.67       chs 			uvm_pagefree(pg);
   1532   1.44       chs 		} else {
   1533   1.44       chs 			UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
   1534  1.142      yamt 			KASSERT((pg->flags & PG_FAKE) == 0);
   1535   1.44       chs 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1536   1.44       chs 			UVM_PAGE_OWN(pg, NULL);
   1537   1.44       chs 		}
   1538   1.44       chs 	}
   1539    1.1       mrg }
   1540    1.1       mrg 
   1541    1.1       mrg #if defined(UVM_PAGE_TRKOWN)
   1542    1.1       mrg /*
   1543    1.1       mrg  * uvm_page_own: set or release page ownership
   1544    1.1       mrg  *
   1545    1.1       mrg  * => this is a debugging function that keeps track of who sets PG_BUSY
   1546    1.1       mrg  *	and where they do it.   it can be used to track down problems
   1547    1.1       mrg  *	such a process setting "PG_BUSY" and never releasing it.
   1548    1.1       mrg  * => page's object [if any] must be locked
   1549    1.1       mrg  * => if "tag" is NULL then we are releasing page ownership
   1550    1.1       mrg  */
   1551    1.7       mrg void
   1552  1.105   thorpej uvm_page_own(struct vm_page *pg, const char *tag)
   1553    1.1       mrg {
   1554  1.112      yamt 	struct uvm_object *uobj;
   1555  1.112      yamt 	struct vm_anon *anon;
   1556  1.112      yamt 
   1557   1.67       chs 	KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0);
   1558   1.67       chs 
   1559  1.112      yamt 	uobj = pg->uobject;
   1560  1.112      yamt 	anon = pg->uanon;
   1561  1.112      yamt 	if (uobj != NULL) {
   1562  1.127        ad 		KASSERT(mutex_owned(&uobj->vmobjlock));
   1563  1.112      yamt 	} else if (anon != NULL) {
   1564  1.127        ad 		KASSERT(mutex_owned(&anon->an_lock));
   1565  1.112      yamt 	}
   1566  1.112      yamt 
   1567  1.112      yamt 	KASSERT((pg->flags & PG_WANTED) == 0);
   1568  1.112      yamt 
   1569    1.7       mrg 	/* gain ownership? */
   1570    1.7       mrg 	if (tag) {
   1571  1.112      yamt 		KASSERT((pg->flags & PG_BUSY) != 0);
   1572    1.7       mrg 		if (pg->owner_tag) {
   1573    1.7       mrg 			printf("uvm_page_own: page %p already owned "
   1574    1.7       mrg 			    "by proc %d [%s]\n", pg,
   1575   1.74     enami 			    pg->owner, pg->owner_tag);
   1576    1.7       mrg 			panic("uvm_page_own");
   1577    1.7       mrg 		}
   1578    1.7       mrg 		pg->owner = (curproc) ? curproc->p_pid :  (pid_t) -1;
   1579  1.120  perseant 		pg->lowner = (curlwp) ? curlwp->l_lid :  (lwpid_t) -1;
   1580    1.7       mrg 		pg->owner_tag = tag;
   1581    1.7       mrg 		return;
   1582    1.7       mrg 	}
   1583    1.7       mrg 
   1584    1.7       mrg 	/* drop ownership */
   1585  1.112      yamt 	KASSERT((pg->flags & PG_BUSY) == 0);
   1586    1.7       mrg 	if (pg->owner_tag == NULL) {
   1587    1.7       mrg 		printf("uvm_page_own: dropping ownership of an non-owned "
   1588    1.7       mrg 		    "page (%p)\n", pg);
   1589    1.7       mrg 		panic("uvm_page_own");
   1590    1.7       mrg 	}
   1591  1.115      yamt 	if (!uvmpdpol_pageisqueued_p(pg)) {
   1592  1.115      yamt 		KASSERT((pg->uanon == NULL && pg->uobject == NULL) ||
   1593  1.115      yamt 		    pg->wire_count > 0);
   1594  1.115      yamt 	} else {
   1595  1.115      yamt 		KASSERT(pg->wire_count == 0);
   1596  1.115      yamt 	}
   1597    1.7       mrg 	pg->owner_tag = NULL;
   1598    1.1       mrg }
   1599    1.1       mrg #endif
   1600   1.34   thorpej 
   1601   1.34   thorpej /*
   1602   1.34   thorpej  * uvm_pageidlezero: zero free pages while the system is idle.
   1603   1.34   thorpej  *
   1604   1.54   thorpej  * => try to complete one color bucket at a time, to reduce our impact
   1605   1.54   thorpej  *	on the CPU cache.
   1606  1.132        ad  * => we loop until we either reach the target or there is a lwp ready
   1607  1.132        ad  *      to run, or MD code detects a reason to break early.
   1608   1.34   thorpej  */
   1609   1.34   thorpej void
   1610  1.105   thorpej uvm_pageidlezero(void)
   1611   1.34   thorpej {
   1612   1.34   thorpej 	struct vm_page *pg;
   1613  1.133        ad 	struct pgfreelist *pgfl, *gpgfl;
   1614  1.133        ad 	struct uvm_cpu *ucpu;
   1615  1.133        ad 	int free_list, firstbucket, nextbucket;
   1616  1.133        ad 
   1617  1.133        ad 	ucpu = curcpu()->ci_data.cpu_uvm;
   1618  1.133        ad 	if (!ucpu->page_idle_zero ||
   1619  1.133        ad 	    ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) {
   1620  1.133        ad 	    	ucpu->page_idle_zero = false;
   1621  1.132        ad 		return;
   1622  1.132        ad 	}
   1623  1.133        ad 	mutex_enter(&uvm_fpageqlock);
   1624  1.133        ad 	firstbucket = ucpu->page_free_nextcolor;
   1625  1.133        ad 	nextbucket = firstbucket;
   1626   1.58     enami 	do {
   1627   1.54   thorpej 		for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
   1628  1.139        ad 			if (sched_curcpu_runnable_p()) {
   1629  1.139        ad 				goto quit;
   1630  1.139        ad 			}
   1631  1.133        ad 			pgfl = &ucpu->page_free[free_list];
   1632  1.133        ad 			gpgfl = &uvm.page_free[free_list];
   1633  1.133        ad 			while ((pg = LIST_FIRST(&pgfl->pgfl_buckets[
   1634   1.54   thorpej 			    nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) {
   1635  1.132        ad 				if (sched_curcpu_runnable_p()) {
   1636  1.101      yamt 					goto quit;
   1637  1.132        ad 				}
   1638  1.133        ad 				LIST_REMOVE(pg, pageq.list); /* global list */
   1639  1.133        ad 				LIST_REMOVE(pg, listq.list); /* per-cpu list */
   1640  1.133        ad 				ucpu->pages[PGFL_UNKNOWN]--;
   1641   1.54   thorpej 				uvmexp.free--;
   1642  1.143  drochner 				KASSERT(pg->pqflags == PQ_FREE);
   1643  1.143  drochner 				pg->pqflags = 0;
   1644  1.123        ad 				mutex_spin_exit(&uvm_fpageqlock);
   1645   1.34   thorpej #ifdef PMAP_PAGEIDLEZERO
   1646   1.67       chs 				if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) {
   1647   1.67       chs 
   1648   1.54   thorpej 					/*
   1649   1.54   thorpej 					 * The machine-dependent code detected
   1650   1.54   thorpej 					 * some reason for us to abort zeroing
   1651   1.54   thorpej 					 * pages, probably because there is a
   1652   1.54   thorpej 					 * process now ready to run.
   1653   1.54   thorpej 					 */
   1654   1.67       chs 
   1655  1.123        ad 					mutex_spin_enter(&uvm_fpageqlock);
   1656  1.144  drochner 					pg->pqflags = PQ_FREE;
   1657  1.133        ad 					LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[
   1658  1.133        ad 					    nextbucket].pgfl_queues[
   1659  1.133        ad 					    PGFL_UNKNOWN], pg, pageq.list);
   1660  1.133        ad 					LIST_INSERT_HEAD(&pgfl->pgfl_buckets[
   1661   1.54   thorpej 					    nextbucket].pgfl_queues[
   1662  1.133        ad 					    PGFL_UNKNOWN], pg, listq.list);
   1663  1.133        ad 					ucpu->pages[PGFL_UNKNOWN]++;
   1664   1.54   thorpej 					uvmexp.free++;
   1665   1.54   thorpej 					uvmexp.zeroaborts++;
   1666  1.101      yamt 					goto quit;
   1667   1.54   thorpej 				}
   1668   1.54   thorpej #else
   1669   1.54   thorpej 				pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1670   1.54   thorpej #endif /* PMAP_PAGEIDLEZERO */
   1671   1.54   thorpej 				pg->flags |= PG_ZERO;
   1672   1.54   thorpej 
   1673  1.123        ad 				mutex_spin_enter(&uvm_fpageqlock);
   1674  1.143  drochner 				pg->pqflags = PQ_FREE;
   1675  1.133        ad 				LIST_INSERT_HEAD(&gpgfl->pgfl_buckets[
   1676  1.133        ad 				    nextbucket].pgfl_queues[PGFL_ZEROS],
   1677  1.133        ad 				    pg, pageq.list);
   1678  1.133        ad 				LIST_INSERT_HEAD(&pgfl->pgfl_buckets[
   1679   1.54   thorpej 				    nextbucket].pgfl_queues[PGFL_ZEROS],
   1680  1.133        ad 				    pg, listq.list);
   1681  1.133        ad 				ucpu->pages[PGFL_ZEROS]++;
   1682   1.54   thorpej 				uvmexp.free++;
   1683   1.54   thorpej 				uvmexp.zeropages++;
   1684   1.54   thorpej 			}
   1685   1.41   thorpej 		}
   1686  1.133        ad 		if (ucpu->pages[PGFL_UNKNOWN] < uvmexp.ncolors) {
   1687  1.133        ad 			break;
   1688  1.133        ad 		}
   1689   1.60   thorpej 		nextbucket = (nextbucket + 1) & uvmexp.colormask;
   1690   1.58     enami 	} while (nextbucket != firstbucket);
   1691  1.133        ad 	ucpu->page_idle_zero = false;
   1692  1.133        ad  quit:
   1693  1.123        ad 	mutex_spin_exit(&uvm_fpageqlock);
   1694   1.34   thorpej }
   1695  1.110      yamt 
   1696  1.110      yamt /*
   1697  1.110      yamt  * uvm_pagelookup: look up a page
   1698  1.110      yamt  *
   1699  1.110      yamt  * => caller should lock object to keep someone from pulling the page
   1700  1.110      yamt  *	out from under it
   1701  1.110      yamt  */
   1702  1.110      yamt 
   1703  1.110      yamt struct vm_page *
   1704  1.110      yamt uvm_pagelookup(struct uvm_object *obj, voff_t off)
   1705  1.110      yamt {
   1706  1.110      yamt 	struct vm_page *pg;
   1707  1.110      yamt 
   1708  1.127        ad 	KASSERT(mutex_owned(&obj->vmobjlock));
   1709  1.123        ad 
   1710  1.156     rmind 	pg = rb_tree_find_node(&obj->rb_tree, &off);
   1711  1.134        ad 
   1712  1.110      yamt 	KASSERT(pg == NULL || obj->uo_npages != 0);
   1713  1.110      yamt 	KASSERT(pg == NULL || (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   1714  1.110      yamt 		(pg->flags & PG_BUSY) != 0);
   1715  1.156     rmind 	return pg;
   1716  1.110      yamt }
   1717  1.110      yamt 
   1718  1.110      yamt /*
   1719  1.110      yamt  * uvm_pagewire: wire the page, thus removing it from the daemon's grasp
   1720  1.110      yamt  *
   1721  1.110      yamt  * => caller must lock page queues
   1722  1.110      yamt  */
   1723  1.110      yamt 
   1724  1.110      yamt void
   1725  1.110      yamt uvm_pagewire(struct vm_page *pg)
   1726  1.110      yamt {
   1727  1.127        ad 	KASSERT(mutex_owned(&uvm_pageqlock));
   1728  1.113      yamt #if defined(READAHEAD_STATS)
   1729  1.113      yamt 	if ((pg->pqflags & PQ_READAHEAD) != 0) {
   1730  1.113      yamt 		uvm_ra_hit.ev_count++;
   1731  1.113      yamt 		pg->pqflags &= ~PQ_READAHEAD;
   1732  1.113      yamt 	}
   1733  1.113      yamt #endif /* defined(READAHEAD_STATS) */
   1734  1.110      yamt 	if (pg->wire_count == 0) {
   1735  1.110      yamt 		uvm_pagedequeue(pg);
   1736  1.110      yamt 		uvmexp.wired++;
   1737  1.110      yamt 	}
   1738  1.110      yamt 	pg->wire_count++;
   1739  1.110      yamt }
   1740  1.110      yamt 
   1741  1.110      yamt /*
   1742  1.110      yamt  * uvm_pageunwire: unwire the page.
   1743  1.110      yamt  *
   1744  1.110      yamt  * => activate if wire count goes to zero.
   1745  1.110      yamt  * => caller must lock page queues
   1746  1.110      yamt  */
   1747  1.110      yamt 
   1748  1.110      yamt void
   1749  1.110      yamt uvm_pageunwire(struct vm_page *pg)
   1750  1.110      yamt {
   1751  1.127        ad 	KASSERT(mutex_owned(&uvm_pageqlock));
   1752  1.110      yamt 	pg->wire_count--;
   1753  1.110      yamt 	if (pg->wire_count == 0) {
   1754  1.111      yamt 		uvm_pageactivate(pg);
   1755  1.110      yamt 		uvmexp.wired--;
   1756  1.110      yamt 	}
   1757  1.110      yamt }
   1758  1.110      yamt 
   1759  1.110      yamt /*
   1760  1.110      yamt  * uvm_pagedeactivate: deactivate page
   1761  1.110      yamt  *
   1762  1.110      yamt  * => caller must lock page queues
   1763  1.110      yamt  * => caller must check to make sure page is not wired
   1764  1.110      yamt  * => object that page belongs to must be locked (so we can adjust pg->flags)
   1765  1.110      yamt  * => caller must clear the reference on the page before calling
   1766  1.110      yamt  */
   1767  1.110      yamt 
   1768  1.110      yamt void
   1769  1.110      yamt uvm_pagedeactivate(struct vm_page *pg)
   1770  1.110      yamt {
   1771  1.113      yamt 
   1772  1.127        ad 	KASSERT(mutex_owned(&uvm_pageqlock));
   1773  1.113      yamt 	KASSERT(pg->wire_count != 0 || uvmpdpol_pageisqueued_p(pg));
   1774  1.113      yamt 	uvmpdpol_pagedeactivate(pg);
   1775  1.110      yamt }
   1776  1.110      yamt 
   1777  1.110      yamt /*
   1778  1.110      yamt  * uvm_pageactivate: activate page
   1779  1.110      yamt  *
   1780  1.110      yamt  * => caller must lock page queues
   1781  1.110      yamt  */
   1782  1.110      yamt 
   1783  1.110      yamt void
   1784  1.110      yamt uvm_pageactivate(struct vm_page *pg)
   1785  1.110      yamt {
   1786  1.113      yamt 
   1787  1.127        ad 	KASSERT(mutex_owned(&uvm_pageqlock));
   1788  1.113      yamt #if defined(READAHEAD_STATS)
   1789  1.113      yamt 	if ((pg->pqflags & PQ_READAHEAD) != 0) {
   1790  1.113      yamt 		uvm_ra_hit.ev_count++;
   1791  1.113      yamt 		pg->pqflags &= ~PQ_READAHEAD;
   1792  1.113      yamt 	}
   1793  1.113      yamt #endif /* defined(READAHEAD_STATS) */
   1794  1.113      yamt 	if (pg->wire_count != 0) {
   1795  1.113      yamt 		return;
   1796  1.110      yamt 	}
   1797  1.113      yamt 	uvmpdpol_pageactivate(pg);
   1798  1.110      yamt }
   1799  1.110      yamt 
   1800  1.110      yamt /*
   1801  1.110      yamt  * uvm_pagedequeue: remove a page from any paging queue
   1802  1.110      yamt  */
   1803  1.110      yamt 
   1804  1.110      yamt void
   1805  1.110      yamt uvm_pagedequeue(struct vm_page *pg)
   1806  1.110      yamt {
   1807  1.113      yamt 
   1808  1.113      yamt 	if (uvmpdpol_pageisqueued_p(pg)) {
   1809  1.127        ad 		KASSERT(mutex_owned(&uvm_pageqlock));
   1810  1.110      yamt 	}
   1811  1.123        ad 
   1812  1.113      yamt 	uvmpdpol_pagedequeue(pg);
   1813  1.113      yamt }
   1814  1.113      yamt 
   1815  1.113      yamt /*
   1816  1.113      yamt  * uvm_pageenqueue: add a page to a paging queue without activating.
   1817  1.113      yamt  * used where a page is not really demanded (yet).  eg. read-ahead
   1818  1.113      yamt  */
   1819  1.113      yamt 
   1820  1.113      yamt void
   1821  1.113      yamt uvm_pageenqueue(struct vm_page *pg)
   1822  1.113      yamt {
   1823  1.113      yamt 
   1824  1.127        ad 	KASSERT(mutex_owned(&uvm_pageqlock));
   1825  1.113      yamt 	if (pg->wire_count != 0) {
   1826  1.113      yamt 		return;
   1827  1.113      yamt 	}
   1828  1.113      yamt 	uvmpdpol_pageenqueue(pg);
   1829  1.110      yamt }
   1830  1.110      yamt 
   1831  1.110      yamt /*
   1832  1.110      yamt  * uvm_pagezero: zero fill a page
   1833  1.110      yamt  *
   1834  1.110      yamt  * => if page is part of an object then the object should be locked
   1835  1.110      yamt  *	to protect pg->flags.
   1836  1.110      yamt  */
   1837  1.110      yamt 
   1838  1.110      yamt void
   1839  1.110      yamt uvm_pagezero(struct vm_page *pg)
   1840  1.110      yamt {
   1841  1.110      yamt 	pg->flags &= ~PG_CLEAN;
   1842  1.110      yamt 	pmap_zero_page(VM_PAGE_TO_PHYS(pg));
   1843  1.110      yamt }
   1844  1.110      yamt 
   1845  1.110      yamt /*
   1846  1.110      yamt  * uvm_pagecopy: copy a page
   1847  1.110      yamt  *
   1848  1.110      yamt  * => if page is part of an object then the object should be locked
   1849  1.110      yamt  *	to protect pg->flags.
   1850  1.110      yamt  */
   1851  1.110      yamt 
   1852  1.110      yamt void
   1853  1.110      yamt uvm_pagecopy(struct vm_page *src, struct vm_page *dst)
   1854  1.110      yamt {
   1855  1.110      yamt 
   1856  1.110      yamt 	dst->flags &= ~PG_CLEAN;
   1857  1.110      yamt 	pmap_copy_page(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
   1858  1.110      yamt }
   1859  1.110      yamt 
   1860  1.110      yamt /*
   1861  1.150   thorpej  * uvm_pageismanaged: test it see that a page (specified by PA) is managed.
   1862  1.150   thorpej  */
   1863  1.150   thorpej 
   1864  1.150   thorpej bool
   1865  1.150   thorpej uvm_pageismanaged(paddr_t pa)
   1866  1.150   thorpej {
   1867  1.150   thorpej 
   1868  1.150   thorpej 	return (vm_physseg_find(atop(pa), NULL) != -1);
   1869  1.150   thorpej }
   1870  1.150   thorpej 
   1871  1.150   thorpej /*
   1872  1.110      yamt  * uvm_page_lookup_freelist: look up the free list for the specified page
   1873  1.110      yamt  */
   1874  1.110      yamt 
   1875  1.110      yamt int
   1876  1.110      yamt uvm_page_lookup_freelist(struct vm_page *pg)
   1877  1.110      yamt {
   1878  1.110      yamt 	int lcv;
   1879  1.110      yamt 
   1880  1.110      yamt 	lcv = vm_physseg_find(atop(VM_PAGE_TO_PHYS(pg)), NULL);
   1881  1.110      yamt 	KASSERT(lcv != -1);
   1882  1.110      yamt 	return (vm_physmem[lcv].free_list);
   1883  1.110      yamt }
   1884  1.151   thorpej 
   1885  1.151   thorpej #if defined(DDB) || defined(DEBUGPRINT)
   1886  1.151   thorpej 
   1887  1.151   thorpej /*
   1888  1.151   thorpej  * uvm_page_printit: actually print the page
   1889  1.151   thorpej  */
   1890  1.151   thorpej 
   1891  1.151   thorpej static const char page_flagbits[] = UVM_PGFLAGBITS;
   1892  1.151   thorpej static const char page_pqflagbits[] = UVM_PQFLAGBITS;
   1893  1.151   thorpej 
   1894  1.151   thorpej void
   1895  1.151   thorpej uvm_page_printit(struct vm_page *pg, bool full,
   1896  1.151   thorpej     void (*pr)(const char *, ...))
   1897  1.151   thorpej {
   1898  1.151   thorpej 	struct vm_page *tpg;
   1899  1.151   thorpej 	struct uvm_object *uobj;
   1900  1.151   thorpej 	struct pgflist *pgl;
   1901  1.151   thorpej 	char pgbuf[128];
   1902  1.151   thorpej 	char pqbuf[128];
   1903  1.151   thorpej 
   1904  1.151   thorpej 	(*pr)("PAGE %p:\n", pg);
   1905  1.151   thorpej 	snprintb(pgbuf, sizeof(pgbuf), page_flagbits, pg->flags);
   1906  1.151   thorpej 	snprintb(pqbuf, sizeof(pqbuf), page_pqflagbits, pg->pqflags);
   1907  1.151   thorpej 	(*pr)("  flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n",
   1908  1.151   thorpej 	    pgbuf, pqbuf, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg));
   1909  1.151   thorpej 	(*pr)("  uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n",
   1910  1.151   thorpej 	    pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count);
   1911  1.151   thorpej #if defined(UVM_PAGE_TRKOWN)
   1912  1.151   thorpej 	if (pg->flags & PG_BUSY)
   1913  1.151   thorpej 		(*pr)("  owning process = %d, tag=%s\n",
   1914  1.151   thorpej 		    pg->owner, pg->owner_tag);
   1915  1.151   thorpej 	else
   1916  1.151   thorpej 		(*pr)("  page not busy, no owner\n");
   1917  1.151   thorpej #else
   1918  1.151   thorpej 	(*pr)("  [page ownership tracking disabled]\n");
   1919  1.151   thorpej #endif
   1920  1.151   thorpej 
   1921  1.151   thorpej 	if (!full)
   1922  1.151   thorpej 		return;
   1923  1.151   thorpej 
   1924  1.151   thorpej 	/* cross-verify object/anon */
   1925  1.151   thorpej 	if ((pg->pqflags & PQ_FREE) == 0) {
   1926  1.151   thorpej 		if (pg->pqflags & PQ_ANON) {
   1927  1.151   thorpej 			if (pg->uanon == NULL || pg->uanon->an_page != pg)
   1928  1.151   thorpej 			    (*pr)("  >>> ANON DOES NOT POINT HERE <<< (%p)\n",
   1929  1.151   thorpej 				(pg->uanon) ? pg->uanon->an_page : NULL);
   1930  1.151   thorpej 			else
   1931  1.151   thorpej 				(*pr)("  anon backpointer is OK\n");
   1932  1.151   thorpej 		} else {
   1933  1.151   thorpej 			uobj = pg->uobject;
   1934  1.151   thorpej 			if (uobj) {
   1935  1.151   thorpej 				(*pr)("  checking object list\n");
   1936  1.151   thorpej 				TAILQ_FOREACH(tpg, &uobj->memq, listq.queue) {
   1937  1.151   thorpej 					if (tpg == pg) {
   1938  1.151   thorpej 						break;
   1939  1.151   thorpej 					}
   1940  1.151   thorpej 				}
   1941  1.151   thorpej 				if (tpg)
   1942  1.151   thorpej 					(*pr)("  page found on object list\n");
   1943  1.151   thorpej 				else
   1944  1.151   thorpej 			(*pr)("  >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n");
   1945  1.151   thorpej 			}
   1946  1.151   thorpej 		}
   1947  1.151   thorpej 	}
   1948  1.151   thorpej 
   1949  1.151   thorpej 	/* cross-verify page queue */
   1950  1.151   thorpej 	if (pg->pqflags & PQ_FREE) {
   1951  1.151   thorpej 		int fl = uvm_page_lookup_freelist(pg);
   1952  1.151   thorpej 		int color = VM_PGCOLOR_BUCKET(pg);
   1953  1.151   thorpej 		pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[
   1954  1.151   thorpej 		    ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN];
   1955  1.151   thorpej 	} else {
   1956  1.151   thorpej 		pgl = NULL;
   1957  1.151   thorpej 	}
   1958  1.151   thorpej 
   1959  1.151   thorpej 	if (pgl) {
   1960  1.151   thorpej 		(*pr)("  checking pageq list\n");
   1961  1.151   thorpej 		LIST_FOREACH(tpg, pgl, pageq.list) {
   1962  1.151   thorpej 			if (tpg == pg) {
   1963  1.151   thorpej 				break;
   1964  1.151   thorpej 			}
   1965  1.151   thorpej 		}
   1966  1.151   thorpej 		if (tpg)
   1967  1.151   thorpej 			(*pr)("  page found on pageq list\n");
   1968  1.151   thorpej 		else
   1969  1.151   thorpej 			(*pr)("  >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n");
   1970  1.151   thorpej 	}
   1971  1.151   thorpej }
   1972  1.151   thorpej 
   1973  1.151   thorpej /*
   1974  1.151   thorpej  * uvm_pages_printthem - print a summary of all managed pages
   1975  1.151   thorpej  */
   1976  1.151   thorpej 
   1977  1.151   thorpej void
   1978  1.151   thorpej uvm_page_printall(void (*pr)(const char *, ...))
   1979  1.151   thorpej {
   1980  1.151   thorpej 	unsigned i;
   1981  1.151   thorpej 	struct vm_page *pg;
   1982  1.151   thorpej 
   1983  1.151   thorpej 	(*pr)("%18s %4s %4s %18s %18s"
   1984  1.151   thorpej #ifdef UVM_PAGE_TRKOWN
   1985  1.151   thorpej 	    " OWNER"
   1986  1.151   thorpej #endif
   1987  1.151   thorpej 	    "\n", "PAGE", "FLAG", "PQ", "UOBJECT", "UANON");
   1988  1.158  uebayasi 	for (i = 0; i < vm_nphysmem; i++) {
   1989  1.151   thorpej 		for (pg = vm_physmem[i].pgs; pg <= vm_physmem[i].lastpg; pg++) {
   1990  1.151   thorpej 			(*pr)("%18p %04x %04x %18p %18p",
   1991  1.151   thorpej 			    pg, pg->flags, pg->pqflags, pg->uobject,
   1992  1.151   thorpej 			    pg->uanon);
   1993  1.151   thorpej #ifdef UVM_PAGE_TRKOWN
   1994  1.151   thorpej 			if (pg->flags & PG_BUSY)
   1995  1.151   thorpej 				(*pr)(" %d [%s]", pg->owner, pg->owner_tag);
   1996  1.151   thorpej #endif
   1997  1.151   thorpej 			(*pr)("\n");
   1998  1.151   thorpej 		}
   1999  1.151   thorpej 	}
   2000  1.151   thorpej }
   2001  1.151   thorpej 
   2002  1.151   thorpej #endif /* DDB || DEBUGPRINT */
   2003