Home | History | Annotate | Line # | Download | only in uvm
uvm_page.c revision 1.31
      1 /*	$NetBSD: uvm_page.c,v 1.31 2000/03/26 20:54:47 kleink Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * Copyright (c) 1991, 1993, The Regents of the University of California.
      6  *
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * The Mach Operating System project at Carnegie-Mellon University.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by Charles D. Cranor,
     23  *      Washington University, the University of California, Berkeley and
     24  *      its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)vm_page.c   8.3 (Berkeley) 3/21/94
     42  * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp
     43  *
     44  *
     45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
     46  * All rights reserved.
     47  *
     48  * Permission to use, copy, modify and distribute this software and
     49  * its documentation is hereby granted, provided that both the copyright
     50  * notice and this permission notice appear in all copies of the
     51  * software, derivative works or modified versions, and any portions
     52  * thereof, and that both notices appear in supporting documentation.
     53  *
     54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     57  *
     58  * Carnegie Mellon requests users of this software to return to
     59  *
     60  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     61  *  School of Computer Science
     62  *  Carnegie Mellon University
     63  *  Pittsburgh PA 15213-3890
     64  *
     65  * any improvements or extensions that they make and grant Carnegie the
     66  * rights to redistribute these changes.
     67  */
     68 
     69 /*
     70  * uvm_page.c: page ops.
     71  */
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/malloc.h>
     76 #include <sys/proc.h>
     77 
     78 #include <vm/vm.h>
     79 #include <vm/vm_page.h>
     80 #include <vm/vm_kern.h>
     81 
     82 #define UVM_PAGE                /* pull in uvm_page.h functions */
     83 #include <uvm/uvm.h>
     84 
     85 /*
     86  * global vars... XXXCDC: move to uvm. structure.
     87  */
     88 
     89 /*
     90  * physical memory config is stored in vm_physmem.
     91  */
     92 
     93 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];	/* XXXCDC: uvm.physmem */
     94 int vm_nphysseg = 0;				/* XXXCDC: uvm.nphysseg */
     95 
     96 /*
     97  * local variables
     98  */
     99 
    100 /*
    101  * these variables record the values returned by vm_page_bootstrap,
    102  * for debugging purposes.  The implementation of uvm_pageboot_alloc
    103  * and pmap_startup here also uses them internally.
    104  */
    105 
    106 static vaddr_t      virtual_space_start;
    107 static vaddr_t      virtual_space_end;
    108 
    109 /*
    110  * we use a hash table with only one bucket during bootup.  we will
    111  * later rehash (resize) the hash table once the allocator is ready.
    112  * we static allocate the one bootstrap bucket below...
    113  */
    114 
    115 static struct pglist uvm_bootbucket;
    116 
    117 /*
    118  * local prototypes
    119  */
    120 
    121 static void uvm_pageinsert __P((struct vm_page *));
    122 
    123 
    124 /*
    125  * inline functions
    126  */
    127 
    128 /*
    129  * uvm_pageinsert: insert a page in the object and the hash table
    130  *
    131  * => caller must lock object
    132  * => caller must lock page queues
    133  * => call should have already set pg's object and offset pointers
    134  *    and bumped the version counter
    135  */
    136 
    137 __inline static void
    138 uvm_pageinsert(pg)
    139 	struct vm_page *pg;
    140 {
    141 	struct pglist *buck;
    142 	int s;
    143 
    144 #ifdef DIAGNOSTIC
    145 	if (pg->flags & PG_TABLED)
    146 		panic("uvm_pageinsert: already inserted");
    147 #endif
    148 
    149 	buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
    150 	s = splimp();
    151 	simple_lock(&uvm.hashlock);
    152 	TAILQ_INSERT_TAIL(buck, pg, hashq);	/* put in hash */
    153 	simple_unlock(&uvm.hashlock);
    154 	splx(s);
    155 
    156 	TAILQ_INSERT_TAIL(&pg->uobject->memq, pg, listq); /* put in object */
    157 	pg->flags |= PG_TABLED;
    158 	pg->uobject->uo_npages++;
    159 
    160 }
    161 
    162 /*
    163  * uvm_page_remove: remove page from object and hash
    164  *
    165  * => caller must lock object
    166  * => caller must lock page queues
    167  */
    168 
    169 void __inline
    170 uvm_pageremove(pg)
    171 	struct vm_page *pg;
    172 {
    173 	struct pglist *buck;
    174 	int s;
    175 
    176 #ifdef DIAGNOSTIC
    177 	if ((pg->flags & (PG_FAULTING)) != 0)
    178 		panic("uvm_pageremove: page is faulting");
    179 #endif
    180 
    181 	if ((pg->flags & PG_TABLED) == 0)
    182 		return;				/* XXX: log */
    183 
    184 	buck = &uvm.page_hash[uvm_pagehash(pg->uobject,pg->offset)];
    185 	s = splimp();
    186 	simple_lock(&uvm.hashlock);
    187 	TAILQ_REMOVE(buck, pg, hashq);
    188 	simple_unlock(&uvm.hashlock);
    189 	splx(s);
    190 
    191 	/* object should be locked */
    192 	TAILQ_REMOVE(&pg->uobject->memq, pg, listq);
    193 
    194 	pg->flags &= ~PG_TABLED;
    195 	pg->uobject->uo_npages--;
    196 	pg->uobject = NULL;
    197 	pg->version++;
    198 
    199 }
    200 
    201 /*
    202  * uvm_page_init: init the page system.   called from uvm_init().
    203  *
    204  * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
    205  */
    206 
    207 void
    208 uvm_page_init(kvm_startp, kvm_endp)
    209 	vaddr_t *kvm_startp, *kvm_endp;
    210 {
    211 	vsize_t freepages, pagecount, n;
    212 	vm_page_t pagearray;
    213 	int lcv, i;
    214 	paddr_t paddr;
    215 
    216 
    217 	/*
    218 	 * step 1: init the page queues and page queue locks
    219 	 */
    220 	for (lcv = 0; lcv < VM_NFREELIST; lcv++)
    221 	  TAILQ_INIT(&uvm.page_free[lcv]);
    222 	TAILQ_INIT(&uvm.page_active);
    223 	TAILQ_INIT(&uvm.page_inactive_swp);
    224 	TAILQ_INIT(&uvm.page_inactive_obj);
    225 	simple_lock_init(&uvm.pageqlock);
    226 	simple_lock_init(&uvm.fpageqlock);
    227 
    228 	/*
    229 	 * step 2: init the <obj,offset> => <page> hash table. for now
    230 	 * we just have one bucket (the bootstrap bucket).   later on we
    231 	 * will allocate new buckets as we dynamically resize the hash table.
    232 	 */
    233 
    234 	uvm.page_nhash = 1;			/* 1 bucket */
    235 	uvm.page_hashmask = 0;		/* mask for hash function */
    236 	uvm.page_hash = &uvm_bootbucket;	/* install bootstrap bucket */
    237 	TAILQ_INIT(uvm.page_hash);		/* init hash table */
    238 	simple_lock_init(&uvm.hashlock);	/* init hash table lock */
    239 
    240 	/*
    241 	 * step 3: allocate vm_page structures.
    242 	 */
    243 
    244 	/*
    245 	 * sanity check:
    246 	 * before calling this function the MD code is expected to register
    247 	 * some free RAM with the uvm_page_physload() function.   our job
    248 	 * now is to allocate vm_page structures for this memory.
    249 	 */
    250 
    251 	if (vm_nphysseg == 0)
    252 		panic("vm_page_bootstrap: no memory pre-allocated");
    253 
    254 	/*
    255 	 * first calculate the number of free pages...
    256 	 *
    257 	 * note that we use start/end rather than avail_start/avail_end.
    258 	 * this allows us to allocate extra vm_page structures in case we
    259 	 * want to return some memory to the pool after booting.
    260 	 */
    261 
    262 	freepages = 0;
    263 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    264 		freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
    265 
    266 	/*
    267 	 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
    268 	 * use.   for each page of memory we use we need a vm_page structure.
    269 	 * thus, the total number of pages we can use is the total size of
    270 	 * the memory divided by the PAGE_SIZE plus the size of the vm_page
    271 	 * structure.   we add one to freepages as a fudge factor to avoid
    272 	 * truncation errors (since we can only allocate in terms of whole
    273 	 * pages).
    274 	 */
    275 
    276 	pagecount = ((freepages + 1) << PAGE_SHIFT) /
    277 	    (PAGE_SIZE + sizeof(struct vm_page));
    278 	pagearray = (vm_page_t)uvm_pageboot_alloc(pagecount *
    279 	    sizeof(struct vm_page));
    280 	memset(pagearray, 0, pagecount * sizeof(struct vm_page));
    281 
    282 	/*
    283 	 * step 4: init the vm_page structures and put them in the correct
    284 	 * place...
    285 	 */
    286 
    287 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    288 
    289 		n = vm_physmem[lcv].end - vm_physmem[lcv].start;
    290 		if (n > pagecount) {
    291 			printf("uvm_page_init: lost %ld page(s) in init\n",
    292 			    (long)(n - pagecount));
    293 			panic("uvm_page_init");  /* XXXCDC: shouldn't happen? */
    294 			/* n = pagecount; */
    295 		}
    296 		/* set up page array pointers */
    297 		vm_physmem[lcv].pgs = pagearray;
    298 		pagearray += n;
    299 		pagecount -= n;
    300 		vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
    301 
    302 		/* init and free vm_pages (we've already zeroed them) */
    303 		paddr = ptoa(vm_physmem[lcv].start);
    304 		for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
    305 			vm_physmem[lcv].pgs[i].phys_addr = paddr;
    306 			if (atop(paddr) >= vm_physmem[lcv].avail_start &&
    307 			    atop(paddr) <= vm_physmem[lcv].avail_end) {
    308 				uvmexp.npages++;
    309 				/* add page to free pool */
    310 				uvm_pagefree(&vm_physmem[lcv].pgs[i]);
    311 			}
    312 		}
    313 	}
    314 	/*
    315 	 * step 5: pass up the values of virtual_space_start and
    316 	 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
    317 	 * layers of the VM.
    318 	 */
    319 
    320 	*kvm_startp = round_page(virtual_space_start);
    321 	*kvm_endp = trunc_page(virtual_space_end);
    322 
    323 	/*
    324 	 * step 6: init pagedaemon lock
    325 	 */
    326 
    327 	simple_lock_init(&uvm.pagedaemon_lock);
    328 
    329 	/*
    330 	 * step 7: init reserve thresholds
    331 	 * XXXCDC - values may need adjusting
    332 	 */
    333 	uvmexp.reserve_pagedaemon = 1;
    334 	uvmexp.reserve_kernel = 5;
    335 
    336 	/*
    337 	 * done!
    338 	 */
    339 
    340 }
    341 
    342 /*
    343  * uvm_setpagesize: set the page size
    344  *
    345  * => sets page_shift and page_mask from uvmexp.pagesize.
    346  * => XXXCDC: move global vars.
    347  */
    348 
    349 void
    350 uvm_setpagesize()
    351 {
    352 	if (uvmexp.pagesize == 0)
    353 		uvmexp.pagesize = DEFAULT_PAGE_SIZE;
    354 	uvmexp.pagemask = uvmexp.pagesize - 1;
    355 	if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
    356 		panic("uvm_setpagesize: page size not a power of two");
    357 	for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
    358 		if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
    359 			break;
    360 }
    361 
    362 /*
    363  * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
    364  */
    365 
    366 vaddr_t
    367 uvm_pageboot_alloc(size)
    368 	vsize_t size;
    369 {
    370 #if defined(PMAP_STEAL_MEMORY)
    371 	vaddr_t addr;
    372 
    373 	/*
    374 	 * defer bootstrap allocation to MD code (it may want to allocate
    375 	 * from a direct-mapped segment).  pmap_steal_memory should round
    376 	 * off virtual_space_start/virtual_space_end.
    377 	 */
    378 
    379 	addr = pmap_steal_memory(size, &virtual_space_start,
    380 	    &virtual_space_end);
    381 
    382 	return(addr);
    383 
    384 #else /* !PMAP_STEAL_MEMORY */
    385 
    386 	static boolean_t initialized = FALSE;
    387 	vaddr_t addr, vaddr;
    388 	paddr_t paddr;
    389 
    390 	/* round to page size */
    391 	size = round_page(size);
    392 
    393 	/*
    394 	 * on first call to this function, initialize ourselves.
    395 	 */
    396 	if (initialized == FALSE) {
    397 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
    398 
    399 		/* round it the way we like it */
    400 		virtual_space_start = round_page(virtual_space_start);
    401 		virtual_space_end = trunc_page(virtual_space_end);
    402 
    403 		initialized = TRUE;
    404 	}
    405 
    406 	/*
    407 	 * allocate virtual memory for this request
    408 	 */
    409 	if (virtual_space_start == virtual_space_end ||
    410 	    (virtual_space_end - virtual_space_start) < size)
    411 		panic("uvm_pageboot_alloc: out of virtual space");
    412 
    413 	addr = virtual_space_start;
    414 
    415 #ifdef PMAP_GROWKERNEL
    416 	/*
    417 	 * If the kernel pmap can't map the requested space,
    418 	 * then allocate more resources for it.
    419 	 */
    420 	if (uvm_maxkaddr < (addr + size)) {
    421 		uvm_maxkaddr = pmap_growkernel(addr + size);
    422 		if (uvm_maxkaddr < (addr + size))
    423 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
    424 	}
    425 #endif
    426 
    427 	virtual_space_start += size;
    428 
    429 	/*
    430 	 * allocate and mapin physical pages to back new virtual pages
    431 	 */
    432 
    433 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
    434 	    vaddr += PAGE_SIZE) {
    435 
    436 		if (!uvm_page_physget(&paddr))
    437 			panic("uvm_pageboot_alloc: out of memory");
    438 
    439 		/*
    440 		 * Note this memory is no longer managed, so using
    441 		 * pmap_kenter is safe.
    442 		 */
    443 		pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
    444 	}
    445 	return(addr);
    446 #endif	/* PMAP_STEAL_MEMORY */
    447 }
    448 
    449 #if !defined(PMAP_STEAL_MEMORY)
    450 /*
    451  * uvm_page_physget: "steal" one page from the vm_physmem structure.
    452  *
    453  * => attempt to allocate it off the end of a segment in which the "avail"
    454  *    values match the start/end values.   if we can't do that, then we
    455  *    will advance both values (making them equal, and removing some
    456  *    vm_page structures from the non-avail area).
    457  * => return false if out of memory.
    458  */
    459 
    460 /* subroutine: try to allocate from memory chunks on the specified freelist */
    461 static boolean_t uvm_page_physget_freelist __P((paddr_t *, int));
    462 
    463 static boolean_t
    464 uvm_page_physget_freelist(paddrp, freelist)
    465 	paddr_t *paddrp;
    466 	int freelist;
    467 {
    468 	int lcv, x;
    469 
    470 	/* pass 1: try allocating from a matching end */
    471 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    472 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    473 #else
    474 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    475 #endif
    476 	{
    477 
    478 		if (vm_physmem[lcv].pgs)
    479 			panic("vm_page_physget: called _after_ bootstrap");
    480 
    481 		if (vm_physmem[lcv].free_list != freelist)
    482 			continue;
    483 
    484 		/* try from front */
    485 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
    486 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    487 			*paddrp = ptoa(vm_physmem[lcv].avail_start);
    488 			vm_physmem[lcv].avail_start++;
    489 			vm_physmem[lcv].start++;
    490 			/* nothing left?   nuke it */
    491 			if (vm_physmem[lcv].avail_start ==
    492 			    vm_physmem[lcv].end) {
    493 				if (vm_nphysseg == 1)
    494 				    panic("vm_page_physget: out of memory!");
    495 				vm_nphysseg--;
    496 				for (x = lcv ; x < vm_nphysseg ; x++)
    497 					/* structure copy */
    498 					vm_physmem[x] = vm_physmem[x+1];
    499 			}
    500 			return (TRUE);
    501 		}
    502 
    503 		/* try from rear */
    504 		if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
    505 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
    506 			*paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
    507 			vm_physmem[lcv].avail_end--;
    508 			vm_physmem[lcv].end--;
    509 			/* nothing left?   nuke it */
    510 			if (vm_physmem[lcv].avail_end ==
    511 			    vm_physmem[lcv].start) {
    512 				if (vm_nphysseg == 1)
    513 				    panic("vm_page_physget: out of memory!");
    514 				vm_nphysseg--;
    515 				for (x = lcv ; x < vm_nphysseg ; x++)
    516 					/* structure copy */
    517 					vm_physmem[x] = vm_physmem[x+1];
    518 			}
    519 			return (TRUE);
    520 		}
    521 	}
    522 
    523 	/* pass2: forget about matching ends, just allocate something */
    524 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    525 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
    526 #else
    527 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    528 #endif
    529 	{
    530 
    531 		/* any room in this bank? */
    532 		if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
    533 			continue;  /* nope */
    534 
    535 		*paddrp = ptoa(vm_physmem[lcv].avail_start);
    536 		vm_physmem[lcv].avail_start++;
    537 		/* truncate! */
    538 		vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
    539 
    540 		/* nothing left?   nuke it */
    541 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
    542 			if (vm_nphysseg == 1)
    543 				panic("vm_page_physget: out of memory!");
    544 			vm_nphysseg--;
    545 			for (x = lcv ; x < vm_nphysseg ; x++)
    546 				/* structure copy */
    547 				vm_physmem[x] = vm_physmem[x+1];
    548 		}
    549 		return (TRUE);
    550 	}
    551 
    552 	return (FALSE);        /* whoops! */
    553 }
    554 
    555 boolean_t
    556 uvm_page_physget(paddrp)
    557 	paddr_t *paddrp;
    558 {
    559 	int i;
    560 
    561 	/* try in the order of freelist preference */
    562 	for (i = 0; i < VM_NFREELIST; i++)
    563 		if (uvm_page_physget_freelist(paddrp, i) == TRUE)
    564 			return (TRUE);
    565 	return (FALSE);
    566 }
    567 #endif /* PMAP_STEAL_MEMORY */
    568 
    569 /*
    570  * uvm_page_physload: load physical memory into VM system
    571  *
    572  * => all args are PFs
    573  * => all pages in start/end get vm_page structures
    574  * => areas marked by avail_start/avail_end get added to the free page pool
    575  * => we are limited to VM_PHYSSEG_MAX physical memory segments
    576  */
    577 
    578 void
    579 uvm_page_physload(start, end, avail_start, avail_end, free_list)
    580 	paddr_t start, end, avail_start, avail_end;
    581 	int free_list;
    582 {
    583 	int preload, lcv;
    584 	psize_t npages;
    585 	struct vm_page *pgs;
    586 	struct vm_physseg *ps;
    587 
    588 	if (uvmexp.pagesize == 0)
    589 		panic("vm_page_physload: page size not set!");
    590 
    591 	if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
    592 		panic("uvm_page_physload: bad free list %d\n", free_list);
    593 
    594 	if (start >= end)
    595 		panic("uvm_page_physload: start >= end");
    596 
    597 	/*
    598 	 * do we have room?
    599 	 */
    600 	if (vm_nphysseg == VM_PHYSSEG_MAX) {
    601 		printf("vm_page_physload: unable to load physical memory "
    602 		    "segment\n");
    603 		printf("\t%d segments allocated, ignoring 0x%lx -> 0x%lx\n",
    604 		    VM_PHYSSEG_MAX, start, end);
    605 		return;
    606 	}
    607 
    608 	/*
    609 	 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
    610 	 * called yet, so malloc is not available).
    611 	 */
    612 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
    613 		if (vm_physmem[lcv].pgs)
    614 			break;
    615 	}
    616 	preload = (lcv == vm_nphysseg);
    617 
    618 	/*
    619 	 * if VM is already running, attempt to malloc() vm_page structures
    620 	 */
    621 	if (!preload) {
    622 #if defined(VM_PHYSSEG_NOADD)
    623 		panic("vm_page_physload: tried to add RAM after vm_mem_init");
    624 #else
    625 		/* XXXCDC: need some sort of lockout for this case */
    626 		paddr_t paddr;
    627 		npages = end - start;  /* # of pages */
    628 		MALLOC(pgs, struct vm_page *, sizeof(struct vm_page) * npages,
    629 					 M_VMPAGE, M_NOWAIT);
    630 		if (pgs == NULL) {
    631 			printf("vm_page_physload: can not malloc vm_page "
    632 			    "structs for segment\n");
    633 			printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
    634 			return;
    635 		}
    636 		/* zero data, init phys_addr and free_list, and free pages */
    637 		memset(pgs, 0, sizeof(struct vm_page) * npages);
    638 		for (lcv = 0, paddr = ptoa(start) ;
    639 				 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
    640 			pgs[lcv].phys_addr = paddr;
    641 			pgs[lcv].free_list = free_list;
    642 			if (atop(paddr) >= avail_start &&
    643 			    atop(paddr) <= avail_end)
    644 				uvm_pagefree(&pgs[lcv]);
    645 		}
    646 		/* XXXCDC: incomplete: need to update uvmexp.free, what else? */
    647 		/* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
    648 #endif
    649 	} else {
    650 
    651 		/* gcc complains if these don't get init'd */
    652 		pgs = NULL;
    653 		npages = 0;
    654 
    655 	}
    656 
    657 	/*
    658 	 * now insert us in the proper place in vm_physmem[]
    659 	 */
    660 
    661 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
    662 
    663 	/* random: put it at the end (easy!) */
    664 	ps = &vm_physmem[vm_nphysseg];
    665 
    666 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
    667 
    668 	{
    669 		int x;
    670 		/* sort by address for binary search */
    671 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    672 			if (start < vm_physmem[lcv].start)
    673 				break;
    674 		ps = &vm_physmem[lcv];
    675 		/* move back other entries, if necessary ... */
    676 		for (x = vm_nphysseg ; x > lcv ; x--)
    677 			/* structure copy */
    678 			vm_physmem[x] = vm_physmem[x - 1];
    679 	}
    680 
    681 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
    682 
    683 	{
    684 		int x;
    685 		/* sort by largest segment first */
    686 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    687 			if ((end - start) >
    688 			    (vm_physmem[lcv].end - vm_physmem[lcv].start))
    689 				break;
    690 		ps = &vm_physmem[lcv];
    691 		/* move back other entries, if necessary ... */
    692 		for (x = vm_nphysseg ; x > lcv ; x--)
    693 			/* structure copy */
    694 			vm_physmem[x] = vm_physmem[x - 1];
    695 	}
    696 
    697 #else
    698 
    699 	panic("vm_page_physload: unknown physseg strategy selected!");
    700 
    701 #endif
    702 
    703 	ps->start = start;
    704 	ps->end = end;
    705 	ps->avail_start = avail_start;
    706 	ps->avail_end = avail_end;
    707 	if (preload) {
    708 		ps->pgs = NULL;
    709 	} else {
    710 		ps->pgs = pgs;
    711 		ps->lastpg = pgs + npages - 1;
    712 	}
    713 	ps->free_list = free_list;
    714 	vm_nphysseg++;
    715 
    716 	/*
    717 	 * done!
    718 	 */
    719 
    720 	if (!preload)
    721 		uvm_page_rehash();
    722 
    723 	return;
    724 }
    725 
    726 /*
    727  * uvm_page_rehash: reallocate hash table based on number of free pages.
    728  */
    729 
    730 void
    731 uvm_page_rehash()
    732 {
    733 	int freepages, lcv, bucketcount, s, oldcount;
    734 	struct pglist *newbuckets, *oldbuckets;
    735 	struct vm_page *pg;
    736 	size_t newsize, oldsize;
    737 
    738 	/*
    739 	 * compute number of pages that can go in the free pool
    740 	 */
    741 
    742 	freepages = 0;
    743 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    744 		freepages +=
    745 		    (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start);
    746 
    747 	/*
    748 	 * compute number of buckets needed for this number of pages
    749 	 */
    750 
    751 	bucketcount = 1;
    752 	while (bucketcount < freepages)
    753 		bucketcount = bucketcount * 2;
    754 
    755 	/*
    756 	 * compute the size of the current table and new table.
    757 	 */
    758 
    759 	oldbuckets = uvm.page_hash;
    760 	oldcount = uvm.page_nhash;
    761 	oldsize = round_page(sizeof(struct pglist) * oldcount);
    762 	newsize = round_page(sizeof(struct pglist) * bucketcount);
    763 
    764 	/*
    765 	 * allocate the new buckets
    766 	 */
    767 
    768 	newbuckets = (struct pglist *) uvm_km_alloc(kernel_map, newsize);
    769 	if (newbuckets == NULL) {
    770 		printf("uvm_page_physrehash: WARNING: could not grow page "
    771 		    "hash table\n");
    772 		return;
    773 	}
    774 	for (lcv = 0 ; lcv < bucketcount ; lcv++)
    775 		TAILQ_INIT(&newbuckets[lcv]);
    776 
    777 	/*
    778 	 * now replace the old buckets with the new ones and rehash everything
    779 	 */
    780 
    781 	s = splimp();
    782 	simple_lock(&uvm.hashlock);
    783 	uvm.page_hash = newbuckets;
    784 	uvm.page_nhash = bucketcount;
    785 	uvm.page_hashmask = bucketcount - 1;  /* power of 2 */
    786 
    787 	/* ... and rehash */
    788 	for (lcv = 0 ; lcv < oldcount ; lcv++) {
    789 		while ((pg = oldbuckets[lcv].tqh_first) != NULL) {
    790 			TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq);
    791 			TAILQ_INSERT_TAIL(
    792 			  &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)],
    793 			  pg, hashq);
    794 		}
    795 	}
    796 	simple_unlock(&uvm.hashlock);
    797 	splx(s);
    798 
    799 	/*
    800 	 * free old bucket array if is not the boot-time table
    801 	 */
    802 
    803 	if (oldbuckets != &uvm_bootbucket)
    804 		uvm_km_free(kernel_map, (vaddr_t) oldbuckets, oldsize);
    805 
    806 	/*
    807 	 * done
    808 	 */
    809 	return;
    810 }
    811 
    812 
    813 #if 1 /* XXXCDC: TMP TMP TMP DEBUG DEBUG DEBUG */
    814 
    815 void uvm_page_physdump __P((void)); /* SHUT UP GCC */
    816 
    817 /* call from DDB */
    818 void
    819 uvm_page_physdump()
    820 {
    821 	int lcv;
    822 
    823 	printf("rehash: physical memory config [segs=%d of %d]:\n",
    824 				 vm_nphysseg, VM_PHYSSEG_MAX);
    825 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
    826 		printf("0x%lx->0x%lx [0x%lx->0x%lx]\n", vm_physmem[lcv].start,
    827 		    vm_physmem[lcv].end, vm_physmem[lcv].avail_start,
    828 		    vm_physmem[lcv].avail_end);
    829 	printf("STRATEGY = ");
    830 	switch (VM_PHYSSEG_STRAT) {
    831 	case VM_PSTRAT_RANDOM: printf("RANDOM\n"); break;
    832 	case VM_PSTRAT_BSEARCH: printf("BSEARCH\n"); break;
    833 	case VM_PSTRAT_BIGFIRST: printf("BIGFIRST\n"); break;
    834 	default: printf("<<UNKNOWN>>!!!!\n");
    835 	}
    836 	printf("number of buckets = %d\n", uvm.page_nhash);
    837 }
    838 #endif
    839 
    840 /*
    841  * uvm_pagealloc_strat: allocate vm_page from a particular free list.
    842  *
    843  * => return null if no pages free
    844  * => wake up pagedaemon if number of free pages drops below low water mark
    845  * => if obj != NULL, obj must be locked (to put in hash)
    846  * => if anon != NULL, anon must be locked (to put in anon)
    847  * => only one of obj or anon can be non-null
    848  * => caller must activate/deactivate page if it is not wired.
    849  * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
    850  */
    851 
    852 struct vm_page *
    853 uvm_pagealloc_strat(obj, off, anon, flags, strat, free_list)
    854 	struct uvm_object *obj;
    855 	voff_t off;
    856 	int flags;
    857 	struct vm_anon *anon;
    858 	int strat, free_list;
    859 {
    860 	int lcv, s;
    861 	struct vm_page *pg;
    862 	struct pglist *freeq;
    863 	boolean_t use_reserve;
    864 
    865 #ifdef DIAGNOSTIC
    866 	/* sanity check */
    867 	if (obj && anon)
    868 		panic("uvm_pagealloc: obj and anon != NULL");
    869 #endif
    870 
    871 	s = uvm_lock_fpageq();		/* lock free page queue */
    872 
    873 	/*
    874 	 * check to see if we need to generate some free pages waking
    875 	 * the pagedaemon.
    876 	 */
    877 
    878 	if (uvmexp.free < uvmexp.freemin || (uvmexp.free < uvmexp.freetarg &&
    879 	    uvmexp.inactive < uvmexp.inactarg))
    880 		wakeup(&uvm.pagedaemon);
    881 
    882 	/*
    883 	 * fail if any of these conditions is true:
    884 	 * [1]  there really are no free pages, or
    885 	 * [2]  only kernel "reserved" pages remain and
    886 	 *        the page isn't being allocated to a kernel object.
    887 	 * [3]  only pagedaemon "reserved" pages remain and
    888 	 *        the requestor isn't the pagedaemon.
    889 	 */
    890 
    891 	use_reserve = (flags & UVM_PGA_USERESERVE) ||
    892 		(obj && UVM_OBJ_IS_KERN_OBJECT(obj));
    893 	if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
    894 	    (uvmexp.free <= uvmexp.reserve_pagedaemon &&
    895 	     !(use_reserve && curproc == uvm.pagedaemon_proc)))
    896 		goto fail;
    897 
    898  again:
    899 	switch (strat) {
    900 	case UVM_PGA_STRAT_NORMAL:
    901 		/* Check all freelists in descending priority order. */
    902 		for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
    903 			freeq = &uvm.page_free[lcv];
    904 			if ((pg = freeq->tqh_first) != NULL)
    905 				goto gotit;
    906 		}
    907 
    908 		/* No pages free! */
    909 		goto fail;
    910 
    911 	case UVM_PGA_STRAT_ONLY:
    912 	case UVM_PGA_STRAT_FALLBACK:
    913 		/* Attempt to allocate from the specified free list. */
    914 #ifdef DIAGNOSTIC
    915 		if (free_list >= VM_NFREELIST || free_list < 0)
    916 			panic("uvm_pagealloc_strat: bad free list %d",
    917 			    free_list);
    918 #endif
    919 		freeq = &uvm.page_free[free_list];
    920 		if ((pg = freeq->tqh_first) != NULL)
    921 			goto gotit;
    922 
    923 		/* Fall back, if possible. */
    924 		if (strat == UVM_PGA_STRAT_FALLBACK) {
    925 			strat = UVM_PGA_STRAT_NORMAL;
    926 			goto again;
    927 		}
    928 
    929 		/* No pages free! */
    930 		goto fail;
    931 
    932 	default:
    933 		panic("uvm_pagealloc_strat: bad strat %d", strat);
    934 		/* NOTREACHED */
    935 	}
    936 
    937  gotit:
    938 	TAILQ_REMOVE(freeq, pg, pageq);
    939 	uvmexp.free--;
    940 
    941 	uvm_unlock_fpageq(s);		/* unlock free page queue */
    942 
    943 	pg->offset = off;
    944 	pg->uobject = obj;
    945 	pg->uanon = anon;
    946 	pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
    947 	pg->version++;
    948 	pg->wire_count = 0;
    949 	pg->loan_count = 0;
    950 	if (anon) {
    951 		anon->u.an_page = pg;
    952 		pg->pqflags = PQ_ANON;
    953 	} else {
    954 		if (obj)
    955 			uvm_pageinsert(pg);
    956 		pg->pqflags = 0;
    957 	}
    958 #if defined(UVM_PAGE_TRKOWN)
    959 	pg->owner_tag = NULL;
    960 #endif
    961 	UVM_PAGE_OWN(pg, "new alloc");
    962 
    963 	return(pg);
    964 
    965  fail:
    966 	uvm_unlock_fpageq(s);
    967 	return (NULL);
    968 }
    969 
    970 /*
    971  * uvm_pagerealloc: reallocate a page from one object to another
    972  *
    973  * => both objects must be locked
    974  */
    975 
    976 void
    977 uvm_pagerealloc(pg, newobj, newoff)
    978 	struct vm_page *pg;
    979 	struct uvm_object *newobj;
    980 	voff_t newoff;
    981 {
    982 	/*
    983 	 * remove it from the old object
    984 	 */
    985 
    986 	if (pg->uobject) {
    987 		uvm_pageremove(pg);
    988 	}
    989 
    990 	/*
    991 	 * put it in the new object
    992 	 */
    993 
    994 	if (newobj) {
    995 		pg->uobject = newobj;
    996 		pg->offset = newoff;
    997 		pg->version++;
    998 		uvm_pageinsert(pg);
    999 	}
   1000 
   1001 	return;
   1002 }
   1003 
   1004 
   1005 /*
   1006  * uvm_pagefree: free page
   1007  *
   1008  * => erase page's identity (i.e. remove from hash/object)
   1009  * => put page on free list
   1010  * => caller must lock owning object (either anon or uvm_object)
   1011  * => caller must lock page queues
   1012  * => assumes all valid mappings of pg are gone
   1013  */
   1014 
   1015 void uvm_pagefree(pg)
   1016 
   1017 struct vm_page *pg;
   1018 
   1019 {
   1020 	int s;
   1021 	int saved_loan_count = pg->loan_count;
   1022 
   1023 	/*
   1024 	 * if the page was an object page (and thus "TABLED"), remove it
   1025 	 * from the object.
   1026 	 */
   1027 
   1028 	if (pg->flags & PG_TABLED) {
   1029 
   1030 		/*
   1031 		 * if the object page is on loan we are going to drop ownership.
   1032 		 * it is possible that an anon will take over as owner for this
   1033 		 * page later on.   the anon will want a !PG_CLEAN page so that
   1034 		 * it knows it needs to allocate swap if it wants to page the
   1035 		 * page out.
   1036 		 */
   1037 
   1038 		if (saved_loan_count)
   1039 			pg->flags &= ~PG_CLEAN;	/* in case an anon takes over */
   1040 
   1041 		uvm_pageremove(pg);
   1042 
   1043 		/*
   1044 		 * if our page was on loan, then we just lost control over it
   1045 		 * (in fact, if it was loaned to an anon, the anon may have
   1046 		 * already taken over ownership of the page by now and thus
   1047 		 * changed the loan_count [e.g. in uvmfault_anonget()]) we just
   1048 		 * return (when the last loan is dropped, then the page can be
   1049 		 * freed by whatever was holding the last loan).
   1050 		 */
   1051 		if (saved_loan_count)
   1052 			return;
   1053 
   1054 	} else if (saved_loan_count && (pg->pqflags & PQ_ANON)) {
   1055 
   1056 		/*
   1057 		 * if our page is owned by an anon and is loaned out to the
   1058 		 * kernel then we just want to drop ownership and return.
   1059 		 * the kernel must free the page when all its loans clear ...
   1060 		 * note that the kernel can't change the loan status of our
   1061 		 * page as long as we are holding PQ lock.
   1062 		 */
   1063 		pg->pqflags &= ~PQ_ANON;
   1064 		pg->uanon = NULL;
   1065 		return;
   1066 	}
   1067 
   1068 #ifdef DIAGNOSTIC
   1069 	if (saved_loan_count) {
   1070 		printf("uvm_pagefree: warning: freeing page with a loan "
   1071 		    "count of %d\n", saved_loan_count);
   1072 		panic("uvm_pagefree: loan count");
   1073 	}
   1074 #endif
   1075 
   1076 
   1077 	/*
   1078 	 * now remove the page from the queues
   1079 	 */
   1080 
   1081 	if (pg->pqflags & PQ_ACTIVE) {
   1082 		TAILQ_REMOVE(&uvm.page_active, pg, pageq);
   1083 		pg->pqflags &= ~PQ_ACTIVE;
   1084 		uvmexp.active--;
   1085 	}
   1086 	if (pg->pqflags & PQ_INACTIVE) {
   1087 		if (pg->pqflags & PQ_SWAPBACKED)
   1088 			TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq);
   1089 		else
   1090 			TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq);
   1091 		pg->pqflags &= ~PQ_INACTIVE;
   1092 		uvmexp.inactive--;
   1093 	}
   1094 
   1095 	/*
   1096 	 * if the page was wired, unwire it now.
   1097 	 */
   1098 	if (pg->wire_count)
   1099 	{
   1100 		pg->wire_count = 0;
   1101 		uvmexp.wired--;
   1102 	}
   1103 
   1104 	/*
   1105 	 * and put on free queue
   1106 	 */
   1107 
   1108 	s = uvm_lock_fpageq();
   1109 	TAILQ_INSERT_TAIL(&uvm.page_free[uvm_page_lookup_freelist(pg)],
   1110 	    pg, pageq);
   1111 	pg->pqflags = PQ_FREE;
   1112 #ifdef DEBUG
   1113 	pg->uobject = (void *)0xdeadbeef;
   1114 	pg->offset = 0xdeadbeef;
   1115 	pg->uanon = (void *)0xdeadbeef;
   1116 #endif
   1117 	uvmexp.free++;
   1118 	uvm_unlock_fpageq(s);
   1119 }
   1120 
   1121 #if defined(UVM_PAGE_TRKOWN)
   1122 /*
   1123  * uvm_page_own: set or release page ownership
   1124  *
   1125  * => this is a debugging function that keeps track of who sets PG_BUSY
   1126  *	and where they do it.   it can be used to track down problems
   1127  *	such a process setting "PG_BUSY" and never releasing it.
   1128  * => page's object [if any] must be locked
   1129  * => if "tag" is NULL then we are releasing page ownership
   1130  */
   1131 void
   1132 uvm_page_own(pg, tag)
   1133 	struct vm_page *pg;
   1134 	char *tag;
   1135 {
   1136 	/* gain ownership? */
   1137 	if (tag) {
   1138 		if (pg->owner_tag) {
   1139 			printf("uvm_page_own: page %p already owned "
   1140 			    "by proc %d [%s]\n", pg,
   1141 			     pg->owner, pg->owner_tag);
   1142 			panic("uvm_page_own");
   1143 		}
   1144 		pg->owner = (curproc) ? curproc->p_pid :  (pid_t) -1;
   1145 		pg->owner_tag = tag;
   1146 		return;
   1147 	}
   1148 
   1149 	/* drop ownership */
   1150 	if (pg->owner_tag == NULL) {
   1151 		printf("uvm_page_own: dropping ownership of an non-owned "
   1152 		    "page (%p)\n", pg);
   1153 		panic("uvm_page_own");
   1154 	}
   1155 	pg->owner_tag = NULL;
   1156 	return;
   1157 }
   1158 #endif
   1159