xref: /netbsd-src/sys/uvm/uvm_page.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: uvm_page.c,v 1.89 2003/06/01 09:26:10 wiz 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@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/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: uvm_page.c,v 1.89 2003/06/01 09:26:10 wiz Exp $");
75 
76 #include "opt_uvmhist.h"
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/malloc.h>
81 #include <sys/sched.h>
82 #include <sys/kernel.h>
83 #include <sys/vnode.h>
84 #include <sys/proc.h>
85 
86 #define UVM_PAGE                /* pull in uvm_page.h functions */
87 #include <uvm/uvm.h>
88 
89 /*
90  * global vars... XXXCDC: move to uvm. structure.
91  */
92 
93 /*
94  * physical memory config is stored in vm_physmem.
95  */
96 
97 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX];	/* XXXCDC: uvm.physmem */
98 int vm_nphysseg = 0;				/* XXXCDC: uvm.nphysseg */
99 
100 /*
101  * Some supported CPUs in a given architecture don't support all
102  * of the things necessary to do idle page zero'ing efficiently.
103  * We therefore provide a way to disable it from machdep code here.
104  */
105 /*
106  * XXX disabled until we can find a way to do this without causing
107  * problems for either cpu caches or DMA latency.
108  */
109 boolean_t vm_page_zero_enable = FALSE;
110 
111 /*
112  * local variables
113  */
114 
115 /*
116  * these variables record the values returned by vm_page_bootstrap,
117  * for debugging purposes.  The implementation of uvm_pageboot_alloc
118  * and pmap_startup here also uses them internally.
119  */
120 
121 static vaddr_t      virtual_space_start;
122 static vaddr_t      virtual_space_end;
123 
124 /*
125  * we use a hash table with only one bucket during bootup.  we will
126  * later rehash (resize) the hash table once the allocator is ready.
127  * we static allocate the one bootstrap bucket below...
128  */
129 
130 static struct pglist uvm_bootbucket;
131 
132 /*
133  * we allocate an initial number of page colors in uvm_page_init(),
134  * and remember them.  We may re-color pages as cache sizes are
135  * discovered during the autoconfiguration phase.  But we can never
136  * free the initial set of buckets, since they are allocated using
137  * uvm_pageboot_alloc().
138  */
139 
140 static boolean_t have_recolored_pages /* = FALSE */;
141 
142 MALLOC_DEFINE(M_VMPAGE, "VM page", "VM page");
143 
144 /*
145  * local prototypes
146  */
147 
148 static void uvm_pageinsert __P((struct vm_page *));
149 static void uvm_pageremove __P((struct vm_page *));
150 
151 /*
152  * inline functions
153  */
154 
155 /*
156  * uvm_pageinsert: insert a page in the object and the hash table
157  *
158  * => caller must lock object
159  * => caller must lock page queues
160  * => call should have already set pg's object and offset pointers
161  *    and bumped the version counter
162  */
163 
164 __inline static void
165 uvm_pageinsert(pg)
166 	struct vm_page *pg;
167 {
168 	struct pglist *buck;
169 	struct uvm_object *uobj = pg->uobject;
170 
171 	KASSERT((pg->flags & PG_TABLED) == 0);
172 	buck = &uvm.page_hash[uvm_pagehash(uobj, pg->offset)];
173 	simple_lock(&uvm.hashlock);
174 	TAILQ_INSERT_TAIL(buck, pg, hashq);
175 	simple_unlock(&uvm.hashlock);
176 
177 	if (UVM_OBJ_IS_VTEXT(uobj)) {
178 		uvmexp.execpages++;
179 	} else if (UVM_OBJ_IS_VNODE(uobj)) {
180 		uvmexp.filepages++;
181 	} else if (UVM_OBJ_IS_AOBJ(uobj)) {
182 		uvmexp.anonpages++;
183 	}
184 
185 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq);
186 	pg->flags |= PG_TABLED;
187 	uobj->uo_npages++;
188 }
189 
190 /*
191  * uvm_page_remove: remove page from object and hash
192  *
193  * => caller must lock object
194  * => caller must lock page queues
195  */
196 
197 static __inline void
198 uvm_pageremove(pg)
199 	struct vm_page *pg;
200 {
201 	struct pglist *buck;
202 	struct uvm_object *uobj = pg->uobject;
203 
204 	KASSERT(pg->flags & PG_TABLED);
205 	buck = &uvm.page_hash[uvm_pagehash(uobj, pg->offset)];
206 	simple_lock(&uvm.hashlock);
207 	TAILQ_REMOVE(buck, pg, hashq);
208 	simple_unlock(&uvm.hashlock);
209 
210 	if (UVM_OBJ_IS_VTEXT(uobj)) {
211 		uvmexp.execpages--;
212 	} else if (UVM_OBJ_IS_VNODE(uobj)) {
213 		uvmexp.filepages--;
214 	} else if (UVM_OBJ_IS_AOBJ(uobj)) {
215 		uvmexp.anonpages--;
216 	}
217 
218 	/* object should be locked */
219 	uobj->uo_npages--;
220 	TAILQ_REMOVE(&uobj->memq, pg, listq);
221 	pg->flags &= ~PG_TABLED;
222 	pg->uobject = NULL;
223 }
224 
225 static void
226 uvm_page_init_buckets(struct pgfreelist *pgfl)
227 {
228 	int color, i;
229 
230 	for (color = 0; color < uvmexp.ncolors; color++) {
231 		for (i = 0; i < PGFL_NQUEUES; i++) {
232 			TAILQ_INIT(&pgfl->pgfl_buckets[
233 			    color].pgfl_queues[i]);
234 		}
235 	}
236 }
237 
238 /*
239  * uvm_page_init: init the page system.   called from uvm_init().
240  *
241  * => we return the range of kernel virtual memory in kvm_startp/kvm_endp
242  */
243 
244 void
245 uvm_page_init(kvm_startp, kvm_endp)
246 	vaddr_t *kvm_startp, *kvm_endp;
247 {
248 	vsize_t freepages, pagecount, bucketcount, n;
249 	struct pgflbucket *bucketarray;
250 	struct vm_page *pagearray;
251 	int lcv;
252 	u_int i;
253 	paddr_t paddr;
254 
255 	/*
256 	 * init the page queues and page queue locks, except the free
257 	 * list; we allocate that later (with the initial vm_page
258 	 * structures).
259 	 */
260 
261 	TAILQ_INIT(&uvm.page_active);
262 	TAILQ_INIT(&uvm.page_inactive);
263 	simple_lock_init(&uvm.pageqlock);
264 	simple_lock_init(&uvm.fpageqlock);
265 
266 	/*
267 	 * init the <obj,offset> => <page> hash table.  for now
268 	 * we just have one bucket (the bootstrap bucket).  later on we
269 	 * will allocate new buckets as we dynamically resize the hash table.
270 	 */
271 
272 	uvm.page_nhash = 1;			/* 1 bucket */
273 	uvm.page_hashmask = 0;			/* mask for hash function */
274 	uvm.page_hash = &uvm_bootbucket;	/* install bootstrap bucket */
275 	TAILQ_INIT(uvm.page_hash);		/* init hash table */
276 	simple_lock_init(&uvm.hashlock);	/* init hash table lock */
277 
278 	/*
279 	 * allocate vm_page structures.
280 	 */
281 
282 	/*
283 	 * sanity check:
284 	 * before calling this function the MD code is expected to register
285 	 * some free RAM with the uvm_page_physload() function.   our job
286 	 * now is to allocate vm_page structures for this memory.
287 	 */
288 
289 	if (vm_nphysseg == 0)
290 		panic("uvm_page_bootstrap: no memory pre-allocated");
291 
292 	/*
293 	 * first calculate the number of free pages...
294 	 *
295 	 * note that we use start/end rather than avail_start/avail_end.
296 	 * this allows us to allocate extra vm_page structures in case we
297 	 * want to return some memory to the pool after booting.
298 	 */
299 
300 	freepages = 0;
301 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
302 		freepages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
303 
304 	/*
305 	 * Let MD code initialize the number of colors, or default
306 	 * to 1 color if MD code doesn't care.
307 	 */
308 	if (uvmexp.ncolors == 0)
309 		uvmexp.ncolors = 1;
310 	uvmexp.colormask = uvmexp.ncolors - 1;
311 
312 	/*
313 	 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can
314 	 * use.   for each page of memory we use we need a vm_page structure.
315 	 * thus, the total number of pages we can use is the total size of
316 	 * the memory divided by the PAGE_SIZE plus the size of the vm_page
317 	 * structure.   we add one to freepages as a fudge factor to avoid
318 	 * truncation errors (since we can only allocate in terms of whole
319 	 * pages).
320 	 */
321 
322 	bucketcount = uvmexp.ncolors * VM_NFREELIST;
323 	pagecount = ((freepages + 1) << PAGE_SHIFT) /
324 	    (PAGE_SIZE + sizeof(struct vm_page));
325 
326 	bucketarray = (void *)uvm_pageboot_alloc((bucketcount *
327 	    sizeof(struct pgflbucket)) + (pagecount *
328 	    sizeof(struct vm_page)));
329 	pagearray = (struct vm_page *)(bucketarray + bucketcount);
330 
331 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
332 		uvm.page_free[lcv].pgfl_buckets =
333 		    (bucketarray + (lcv * uvmexp.ncolors));
334 		uvm_page_init_buckets(&uvm.page_free[lcv]);
335 	}
336 	memset(pagearray, 0, pagecount * sizeof(struct vm_page));
337 
338 	/*
339 	 * init the vm_page structures and put them in the correct place.
340 	 */
341 
342 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
343 		n = vm_physmem[lcv].end - vm_physmem[lcv].start;
344 
345 		/* set up page array pointers */
346 		vm_physmem[lcv].pgs = pagearray;
347 		pagearray += n;
348 		pagecount -= n;
349 		vm_physmem[lcv].lastpg = vm_physmem[lcv].pgs + (n - 1);
350 
351 		/* init and free vm_pages (we've already zeroed them) */
352 		paddr = ptoa(vm_physmem[lcv].start);
353 		for (i = 0 ; i < n ; i++, paddr += PAGE_SIZE) {
354 			vm_physmem[lcv].pgs[i].phys_addr = paddr;
355 #ifdef __HAVE_VM_PAGE_MD
356 			VM_MDPAGE_INIT(&vm_physmem[lcv].pgs[i]);
357 #endif
358 			if (atop(paddr) >= vm_physmem[lcv].avail_start &&
359 			    atop(paddr) <= vm_physmem[lcv].avail_end) {
360 				uvmexp.npages++;
361 				/* add page to free pool */
362 				uvm_pagefree(&vm_physmem[lcv].pgs[i]);
363 			}
364 		}
365 	}
366 
367 	/*
368 	 * pass up the values of virtual_space_start and
369 	 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper
370 	 * layers of the VM.
371 	 */
372 
373 	*kvm_startp = round_page(virtual_space_start);
374 	*kvm_endp = trunc_page(virtual_space_end);
375 
376 	/*
377 	 * init locks for kernel threads
378 	 */
379 
380 	simple_lock_init(&uvm.pagedaemon_lock);
381 	simple_lock_init(&uvm.aiodoned_lock);
382 
383 	/*
384 	 * init various thresholds.
385 	 */
386 
387 	uvmexp.reserve_pagedaemon = 1;
388 	uvmexp.reserve_kernel = 5;
389 	uvmexp.anonminpct = 10;
390 	uvmexp.fileminpct = 10;
391 	uvmexp.execminpct = 5;
392 	uvmexp.anonmaxpct = 80;
393 	uvmexp.filemaxpct = 50;
394 	uvmexp.execmaxpct = 30;
395 	uvmexp.anonmin = uvmexp.anonminpct * 256 / 100;
396 	uvmexp.filemin = uvmexp.fileminpct * 256 / 100;
397 	uvmexp.execmin = uvmexp.execminpct * 256 / 100;
398 	uvmexp.anonmax = uvmexp.anonmaxpct * 256 / 100;
399 	uvmexp.filemax = uvmexp.filemaxpct * 256 / 100;
400 	uvmexp.execmax = uvmexp.execmaxpct * 256 / 100;
401 
402 	/*
403 	 * determine if we should zero pages in the idle loop.
404 	 */
405 
406 	uvm.page_idle_zero = vm_page_zero_enable;
407 
408 	/*
409 	 * done!
410 	 */
411 
412 	uvm.page_init_done = TRUE;
413 }
414 
415 /*
416  * uvm_setpagesize: set the page size
417  *
418  * => sets page_shift and page_mask from uvmexp.pagesize.
419  */
420 
421 void
422 uvm_setpagesize()
423 {
424 
425 	/*
426 	 * If uvmexp.pagesize is 0 at this point, we expect PAGE_SIZE
427 	 * to be a constant (indicated by being a non-zero value).
428 	 */
429 	if (uvmexp.pagesize == 0) {
430 		if (PAGE_SIZE == 0)
431 			panic("uvm_setpagesize: uvmexp.pagesize not set");
432 		uvmexp.pagesize = PAGE_SIZE;
433 	}
434 	uvmexp.pagemask = uvmexp.pagesize - 1;
435 	if ((uvmexp.pagemask & uvmexp.pagesize) != 0)
436 		panic("uvm_setpagesize: page size not a power of two");
437 	for (uvmexp.pageshift = 0; ; uvmexp.pageshift++)
438 		if ((1 << uvmexp.pageshift) == uvmexp.pagesize)
439 			break;
440 }
441 
442 /*
443  * uvm_pageboot_alloc: steal memory from physmem for bootstrapping
444  */
445 
446 vaddr_t
447 uvm_pageboot_alloc(size)
448 	vsize_t size;
449 {
450 	static boolean_t initialized = FALSE;
451 	vaddr_t addr;
452 #if !defined(PMAP_STEAL_MEMORY)
453 	vaddr_t vaddr;
454 	paddr_t paddr;
455 #endif
456 
457 	/*
458 	 * on first call to this function, initialize ourselves.
459 	 */
460 	if (initialized == FALSE) {
461 		pmap_virtual_space(&virtual_space_start, &virtual_space_end);
462 
463 		/* round it the way we like it */
464 		virtual_space_start = round_page(virtual_space_start);
465 		virtual_space_end = trunc_page(virtual_space_end);
466 
467 		initialized = TRUE;
468 	}
469 
470 	/* round to page size */
471 	size = round_page(size);
472 
473 #if defined(PMAP_STEAL_MEMORY)
474 
475 	/*
476 	 * defer bootstrap allocation to MD code (it may want to allocate
477 	 * from a direct-mapped segment).  pmap_steal_memory should adjust
478 	 * virtual_space_start/virtual_space_end if necessary.
479 	 */
480 
481 	addr = pmap_steal_memory(size, &virtual_space_start,
482 	    &virtual_space_end);
483 
484 	return(addr);
485 
486 #else /* !PMAP_STEAL_MEMORY */
487 
488 	/*
489 	 * allocate virtual memory for this request
490 	 */
491 	if (virtual_space_start == virtual_space_end ||
492 	    (virtual_space_end - virtual_space_start) < size)
493 		panic("uvm_pageboot_alloc: out of virtual space");
494 
495 	addr = virtual_space_start;
496 
497 #ifdef PMAP_GROWKERNEL
498 	/*
499 	 * If the kernel pmap can't map the requested space,
500 	 * then allocate more resources for it.
501 	 */
502 	if (uvm_maxkaddr < (addr + size)) {
503 		uvm_maxkaddr = pmap_growkernel(addr + size);
504 		if (uvm_maxkaddr < (addr + size))
505 			panic("uvm_pageboot_alloc: pmap_growkernel() failed");
506 	}
507 #endif
508 
509 	virtual_space_start += size;
510 
511 	/*
512 	 * allocate and mapin physical pages to back new virtual pages
513 	 */
514 
515 	for (vaddr = round_page(addr) ; vaddr < addr + size ;
516 	    vaddr += PAGE_SIZE) {
517 
518 		if (!uvm_page_physget(&paddr))
519 			panic("uvm_pageboot_alloc: out of memory");
520 
521 		/*
522 		 * Note this memory is no longer managed, so using
523 		 * pmap_kenter is safe.
524 		 */
525 		pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE);
526 	}
527 	pmap_update(pmap_kernel());
528 	return(addr);
529 #endif	/* PMAP_STEAL_MEMORY */
530 }
531 
532 #if !defined(PMAP_STEAL_MEMORY)
533 /*
534  * uvm_page_physget: "steal" one page from the vm_physmem structure.
535  *
536  * => attempt to allocate it off the end of a segment in which the "avail"
537  *    values match the start/end values.   if we can't do that, then we
538  *    will advance both values (making them equal, and removing some
539  *    vm_page structures from the non-avail area).
540  * => return false if out of memory.
541  */
542 
543 /* subroutine: try to allocate from memory chunks on the specified freelist */
544 static boolean_t uvm_page_physget_freelist __P((paddr_t *, int));
545 
546 static boolean_t
547 uvm_page_physget_freelist(paddrp, freelist)
548 	paddr_t *paddrp;
549 	int freelist;
550 {
551 	int lcv, x;
552 
553 	/* pass 1: try allocating from a matching end */
554 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
555 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
556 #else
557 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
558 #endif
559 	{
560 
561 		if (uvm.page_init_done == TRUE)
562 			panic("uvm_page_physget: called _after_ bootstrap");
563 
564 		if (vm_physmem[lcv].free_list != freelist)
565 			continue;
566 
567 		/* try from front */
568 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].start &&
569 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
570 			*paddrp = ptoa(vm_physmem[lcv].avail_start);
571 			vm_physmem[lcv].avail_start++;
572 			vm_physmem[lcv].start++;
573 			/* nothing left?   nuke it */
574 			if (vm_physmem[lcv].avail_start ==
575 			    vm_physmem[lcv].end) {
576 				if (vm_nphysseg == 1)
577 				    panic("uvm_page_physget: out of memory!");
578 				vm_nphysseg--;
579 				for (x = lcv ; x < vm_nphysseg ; x++)
580 					/* structure copy */
581 					vm_physmem[x] = vm_physmem[x+1];
582 			}
583 			return (TRUE);
584 		}
585 
586 		/* try from rear */
587 		if (vm_physmem[lcv].avail_end == vm_physmem[lcv].end &&
588 		    vm_physmem[lcv].avail_start < vm_physmem[lcv].avail_end) {
589 			*paddrp = ptoa(vm_physmem[lcv].avail_end - 1);
590 			vm_physmem[lcv].avail_end--;
591 			vm_physmem[lcv].end--;
592 			/* nothing left?   nuke it */
593 			if (vm_physmem[lcv].avail_end ==
594 			    vm_physmem[lcv].start) {
595 				if (vm_nphysseg == 1)
596 				    panic("uvm_page_physget: out of memory!");
597 				vm_nphysseg--;
598 				for (x = lcv ; x < vm_nphysseg ; x++)
599 					/* structure copy */
600 					vm_physmem[x] = vm_physmem[x+1];
601 			}
602 			return (TRUE);
603 		}
604 	}
605 
606 	/* pass2: forget about matching ends, just allocate something */
607 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
608 	for (lcv = vm_nphysseg - 1 ; lcv >= 0 ; lcv--)
609 #else
610 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
611 #endif
612 	{
613 
614 		/* any room in this bank? */
615 		if (vm_physmem[lcv].avail_start >= vm_physmem[lcv].avail_end)
616 			continue;  /* nope */
617 
618 		*paddrp = ptoa(vm_physmem[lcv].avail_start);
619 		vm_physmem[lcv].avail_start++;
620 		/* truncate! */
621 		vm_physmem[lcv].start = vm_physmem[lcv].avail_start;
622 
623 		/* nothing left?   nuke it */
624 		if (vm_physmem[lcv].avail_start == vm_physmem[lcv].end) {
625 			if (vm_nphysseg == 1)
626 				panic("uvm_page_physget: out of memory!");
627 			vm_nphysseg--;
628 			for (x = lcv ; x < vm_nphysseg ; x++)
629 				/* structure copy */
630 				vm_physmem[x] = vm_physmem[x+1];
631 		}
632 		return (TRUE);
633 	}
634 
635 	return (FALSE);        /* whoops! */
636 }
637 
638 boolean_t
639 uvm_page_physget(paddrp)
640 	paddr_t *paddrp;
641 {
642 	int i;
643 
644 	/* try in the order of freelist preference */
645 	for (i = 0; i < VM_NFREELIST; i++)
646 		if (uvm_page_physget_freelist(paddrp, i) == TRUE)
647 			return (TRUE);
648 	return (FALSE);
649 }
650 #endif /* PMAP_STEAL_MEMORY */
651 
652 /*
653  * uvm_page_physload: load physical memory into VM system
654  *
655  * => all args are PFs
656  * => all pages in start/end get vm_page structures
657  * => areas marked by avail_start/avail_end get added to the free page pool
658  * => we are limited to VM_PHYSSEG_MAX physical memory segments
659  */
660 
661 void
662 uvm_page_physload(start, end, avail_start, avail_end, free_list)
663 	paddr_t start, end, avail_start, avail_end;
664 	int free_list;
665 {
666 	int preload, lcv;
667 	psize_t npages;
668 	struct vm_page *pgs;
669 	struct vm_physseg *ps;
670 
671 	if (uvmexp.pagesize == 0)
672 		panic("uvm_page_physload: page size not set!");
673 	if (free_list >= VM_NFREELIST || free_list < VM_FREELIST_DEFAULT)
674 		panic("uvm_page_physload: bad free list %d", free_list);
675 	if (start >= end)
676 		panic("uvm_page_physload: start >= end");
677 
678 	/*
679 	 * do we have room?
680 	 */
681 
682 	if (vm_nphysseg == VM_PHYSSEG_MAX) {
683 		printf("uvm_page_physload: unable to load physical memory "
684 		    "segment\n");
685 		printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n",
686 		    VM_PHYSSEG_MAX, (long long)start, (long long)end);
687 		printf("\tincrease VM_PHYSSEG_MAX\n");
688 		return;
689 	}
690 
691 	/*
692 	 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been
693 	 * called yet, so malloc is not available).
694 	 */
695 
696 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
697 		if (vm_physmem[lcv].pgs)
698 			break;
699 	}
700 	preload = (lcv == vm_nphysseg);
701 
702 	/*
703 	 * if VM is already running, attempt to malloc() vm_page structures
704 	 */
705 
706 	if (!preload) {
707 #if defined(VM_PHYSSEG_NOADD)
708 		panic("uvm_page_physload: tried to add RAM after vm_mem_init");
709 #else
710 		/* XXXCDC: need some sort of lockout for this case */
711 		paddr_t paddr;
712 		npages = end - start;  /* # of pages */
713 		pgs = malloc(sizeof(struct vm_page) * npages,
714 		    M_VMPAGE, M_NOWAIT);
715 		if (pgs == NULL) {
716 			printf("uvm_page_physload: can not malloc vm_page "
717 			    "structs for segment\n");
718 			printf("\tignoring 0x%lx -> 0x%lx\n", start, end);
719 			return;
720 		}
721 		/* zero data, init phys_addr and free_list, and free pages */
722 		memset(pgs, 0, sizeof(struct vm_page) * npages);
723 		for (lcv = 0, paddr = ptoa(start) ;
724 				 lcv < npages ; lcv++, paddr += PAGE_SIZE) {
725 			pgs[lcv].phys_addr = paddr;
726 			pgs[lcv].free_list = free_list;
727 			if (atop(paddr) >= avail_start &&
728 			    atop(paddr) <= avail_end)
729 				uvm_pagefree(&pgs[lcv]);
730 		}
731 		/* XXXCDC: incomplete: need to update uvmexp.free, what else? */
732 		/* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */
733 #endif
734 	} else {
735 		pgs = NULL;
736 		npages = 0;
737 	}
738 
739 	/*
740 	 * now insert us in the proper place in vm_physmem[]
741 	 */
742 
743 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM)
744 	/* random: put it at the end (easy!) */
745 	ps = &vm_physmem[vm_nphysseg];
746 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH)
747 	{
748 		int x;
749 		/* sort by address for binary search */
750 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
751 			if (start < vm_physmem[lcv].start)
752 				break;
753 		ps = &vm_physmem[lcv];
754 		/* move back other entries, if necessary ... */
755 		for (x = vm_nphysseg ; x > lcv ; x--)
756 			/* structure copy */
757 			vm_physmem[x] = vm_physmem[x - 1];
758 	}
759 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST)
760 	{
761 		int x;
762 		/* sort by largest segment first */
763 		for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
764 			if ((end - start) >
765 			    (vm_physmem[lcv].end - vm_physmem[lcv].start))
766 				break;
767 		ps = &vm_physmem[lcv];
768 		/* move back other entries, if necessary ... */
769 		for (x = vm_nphysseg ; x > lcv ; x--)
770 			/* structure copy */
771 			vm_physmem[x] = vm_physmem[x - 1];
772 	}
773 #else
774 	panic("uvm_page_physload: unknown physseg strategy selected!");
775 #endif
776 
777 	ps->start = start;
778 	ps->end = end;
779 	ps->avail_start = avail_start;
780 	ps->avail_end = avail_end;
781 	if (preload) {
782 		ps->pgs = NULL;
783 	} else {
784 		ps->pgs = pgs;
785 		ps->lastpg = pgs + npages - 1;
786 	}
787 	ps->free_list = free_list;
788 	vm_nphysseg++;
789 
790 	if (!preload)
791 		uvm_page_rehash();
792 }
793 
794 /*
795  * uvm_page_rehash: reallocate hash table based on number of free pages.
796  */
797 
798 void
799 uvm_page_rehash()
800 {
801 	int freepages, lcv, bucketcount, oldcount;
802 	struct pglist *newbuckets, *oldbuckets;
803 	struct vm_page *pg;
804 	size_t newsize, oldsize;
805 
806 	/*
807 	 * compute number of pages that can go in the free pool
808 	 */
809 
810 	freepages = 0;
811 	for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
812 		freepages +=
813 		    (vm_physmem[lcv].avail_end - vm_physmem[lcv].avail_start);
814 
815 	/*
816 	 * compute number of buckets needed for this number of pages
817 	 */
818 
819 	bucketcount = 1;
820 	while (bucketcount < freepages)
821 		bucketcount = bucketcount * 2;
822 
823 	/*
824 	 * compute the size of the current table and new table.
825 	 */
826 
827 	oldbuckets = uvm.page_hash;
828 	oldcount = uvm.page_nhash;
829 	oldsize = round_page(sizeof(struct pglist) * oldcount);
830 	newsize = round_page(sizeof(struct pglist) * bucketcount);
831 
832 	/*
833 	 * allocate the new buckets
834 	 */
835 
836 	newbuckets = (struct pglist *) uvm_km_alloc(kernel_map, newsize);
837 	if (newbuckets == NULL) {
838 		printf("uvm_page_physrehash: WARNING: could not grow page "
839 		    "hash table\n");
840 		return;
841 	}
842 	for (lcv = 0 ; lcv < bucketcount ; lcv++)
843 		TAILQ_INIT(&newbuckets[lcv]);
844 
845 	/*
846 	 * now replace the old buckets with the new ones and rehash everything
847 	 */
848 
849 	simple_lock(&uvm.hashlock);
850 	uvm.page_hash = newbuckets;
851 	uvm.page_nhash = bucketcount;
852 	uvm.page_hashmask = bucketcount - 1;  /* power of 2 */
853 
854 	/* ... and rehash */
855 	for (lcv = 0 ; lcv < oldcount ; lcv++) {
856 		while ((pg = oldbuckets[lcv].tqh_first) != NULL) {
857 			TAILQ_REMOVE(&oldbuckets[lcv], pg, hashq);
858 			TAILQ_INSERT_TAIL(
859 			  &uvm.page_hash[uvm_pagehash(pg->uobject, pg->offset)],
860 			  pg, hashq);
861 		}
862 	}
863 	simple_unlock(&uvm.hashlock);
864 
865 	/*
866 	 * free old bucket array if is not the boot-time table
867 	 */
868 
869 	if (oldbuckets != &uvm_bootbucket)
870 		uvm_km_free(kernel_map, (vaddr_t) oldbuckets, oldsize);
871 }
872 
873 /*
874  * uvm_page_recolor: Recolor the pages if the new bucket count is
875  * larger than the old one.
876  */
877 
878 void
879 uvm_page_recolor(int newncolors)
880 {
881 	struct pgflbucket *bucketarray, *oldbucketarray;
882 	struct pgfreelist pgfl;
883 	struct vm_page *pg;
884 	vsize_t bucketcount;
885 	int s, lcv, color, i, ocolors;
886 
887 	if (newncolors <= uvmexp.ncolors)
888 		return;
889 
890 	if (uvm.page_init_done == FALSE) {
891 		uvmexp.ncolors = newncolors;
892 		return;
893 	}
894 
895 	bucketcount = newncolors * VM_NFREELIST;
896 	bucketarray = malloc(bucketcount * sizeof(struct pgflbucket),
897 	    M_VMPAGE, M_NOWAIT);
898 	if (bucketarray == NULL) {
899 		printf("WARNING: unable to allocate %ld page color buckets\n",
900 		    (long) bucketcount);
901 		return;
902 	}
903 
904 	s = uvm_lock_fpageq();
905 
906 	/* Make sure we should still do this. */
907 	if (newncolors <= uvmexp.ncolors) {
908 		uvm_unlock_fpageq(s);
909 		free(bucketarray, M_VMPAGE);
910 		return;
911 	}
912 
913 	oldbucketarray = uvm.page_free[0].pgfl_buckets;
914 	ocolors = uvmexp.ncolors;
915 
916 	uvmexp.ncolors = newncolors;
917 	uvmexp.colormask = uvmexp.ncolors - 1;
918 
919 	for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
920 		pgfl.pgfl_buckets = (bucketarray + (lcv * newncolors));
921 		uvm_page_init_buckets(&pgfl);
922 		for (color = 0; color < ocolors; color++) {
923 			for (i = 0; i < PGFL_NQUEUES; i++) {
924 				while ((pg = TAILQ_FIRST(&uvm.page_free[
925 				    lcv].pgfl_buckets[color].pgfl_queues[i]))
926 				    != NULL) {
927 					TAILQ_REMOVE(&uvm.page_free[
928 					    lcv].pgfl_buckets[
929 					    color].pgfl_queues[i], pg, pageq);
930 					TAILQ_INSERT_TAIL(&pgfl.pgfl_buckets[
931 					    VM_PGCOLOR_BUCKET(pg)].pgfl_queues[
932 					    i], pg, pageq);
933 				}
934 			}
935 		}
936 		uvm.page_free[lcv].pgfl_buckets = pgfl.pgfl_buckets;
937 	}
938 
939 	if (have_recolored_pages) {
940 		uvm_unlock_fpageq(s);
941 		free(oldbucketarray, M_VMPAGE);
942 		return;
943 	}
944 
945 	have_recolored_pages = TRUE;
946 	uvm_unlock_fpageq(s);
947 }
948 
949 /*
950  * uvm_pagealloc_pgfl: helper routine for uvm_pagealloc_strat
951  */
952 
953 static __inline struct vm_page *
954 uvm_pagealloc_pgfl(struct pgfreelist *pgfl, int try1, int try2,
955     int *trycolorp)
956 {
957 	struct pglist *freeq;
958 	struct vm_page *pg;
959 	int color, trycolor = *trycolorp;
960 
961 	color = trycolor;
962 	do {
963 		if ((pg = TAILQ_FIRST((freeq =
964 		    &pgfl->pgfl_buckets[color].pgfl_queues[try1]))) != NULL)
965 			goto gotit;
966 		if ((pg = TAILQ_FIRST((freeq =
967 		    &pgfl->pgfl_buckets[color].pgfl_queues[try2]))) != NULL)
968 			goto gotit;
969 		color = (color + 1) & uvmexp.colormask;
970 	} while (color != trycolor);
971 
972 	return (NULL);
973 
974  gotit:
975 	TAILQ_REMOVE(freeq, pg, pageq);
976 	uvmexp.free--;
977 
978 	/* update zero'd page count */
979 	if (pg->flags & PG_ZERO)
980 		uvmexp.zeropages--;
981 
982 	if (color == trycolor)
983 		uvmexp.colorhit++;
984 	else {
985 		uvmexp.colormiss++;
986 		*trycolorp = color;
987 	}
988 
989 	return (pg);
990 }
991 
992 /*
993  * uvm_pagealloc_strat: allocate vm_page from a particular free list.
994  *
995  * => return null if no pages free
996  * => wake up pagedaemon if number of free pages drops below low water mark
997  * => if obj != NULL, obj must be locked (to put in hash)
998  * => if anon != NULL, anon must be locked (to put in anon)
999  * => only one of obj or anon can be non-null
1000  * => caller must activate/deactivate page if it is not wired.
1001  * => free_list is ignored if strat == UVM_PGA_STRAT_NORMAL.
1002  * => policy decision: it is more important to pull a page off of the
1003  *	appropriate priority free list than it is to get a zero'd or
1004  *	unknown contents page.  This is because we live with the
1005  *	consequences of a bad free list decision for the entire
1006  *	lifetime of the page, e.g. if the page comes from memory that
1007  *	is slower to access.
1008  */
1009 
1010 struct vm_page *
1011 uvm_pagealloc_strat(obj, off, anon, flags, strat, free_list)
1012 	struct uvm_object *obj;
1013 	voff_t off;
1014 	int flags;
1015 	struct vm_anon *anon;
1016 	int strat, free_list;
1017 {
1018 	int lcv, try1, try2, s, zeroit = 0, color;
1019 	struct vm_page *pg;
1020 	boolean_t use_reserve;
1021 
1022 	KASSERT(obj == NULL || anon == NULL);
1023 	KASSERT(off == trunc_page(off));
1024 	LOCK_ASSERT(obj == NULL || simple_lock_held(&obj->vmobjlock));
1025 	LOCK_ASSERT(anon == NULL || simple_lock_held(&anon->an_lock));
1026 
1027 	s = uvm_lock_fpageq();
1028 
1029 	/*
1030 	 * This implements a global round-robin page coloring
1031 	 * algorithm.
1032 	 *
1033 	 * XXXJRT: Should we make the `nextcolor' per-cpu?
1034 	 * XXXJRT: What about virtually-indexed caches?
1035 	 */
1036 
1037 	color = uvm.page_free_nextcolor;
1038 
1039 	/*
1040 	 * check to see if we need to generate some free pages waking
1041 	 * the pagedaemon.
1042 	 */
1043 
1044 	UVM_KICK_PDAEMON();
1045 
1046 	/*
1047 	 * fail if any of these conditions is true:
1048 	 * [1]  there really are no free pages, or
1049 	 * [2]  only kernel "reserved" pages remain and
1050 	 *        the page isn't being allocated to a kernel object.
1051 	 * [3]  only pagedaemon "reserved" pages remain and
1052 	 *        the requestor isn't the pagedaemon.
1053 	 */
1054 
1055 	use_reserve = (flags & UVM_PGA_USERESERVE) ||
1056 		(obj && UVM_OBJ_IS_KERN_OBJECT(obj));
1057 	if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) ||
1058 	    (uvmexp.free <= uvmexp.reserve_pagedaemon &&
1059 	     !(use_reserve && curproc == uvm.pagedaemon_proc)))
1060 		goto fail;
1061 
1062 #if PGFL_NQUEUES != 2
1063 #error uvm_pagealloc_strat needs to be updated
1064 #endif
1065 
1066 	/*
1067 	 * If we want a zero'd page, try the ZEROS queue first, otherwise
1068 	 * we try the UNKNOWN queue first.
1069 	 */
1070 	if (flags & UVM_PGA_ZERO) {
1071 		try1 = PGFL_ZEROS;
1072 		try2 = PGFL_UNKNOWN;
1073 	} else {
1074 		try1 = PGFL_UNKNOWN;
1075 		try2 = PGFL_ZEROS;
1076 	}
1077 
1078  again:
1079 	switch (strat) {
1080 	case UVM_PGA_STRAT_NORMAL:
1081 		/* Check all freelists in descending priority order. */
1082 		for (lcv = 0; lcv < VM_NFREELIST; lcv++) {
1083 			pg = uvm_pagealloc_pgfl(&uvm.page_free[lcv],
1084 			    try1, try2, &color);
1085 			if (pg != NULL)
1086 				goto gotit;
1087 		}
1088 
1089 		/* No pages free! */
1090 		goto fail;
1091 
1092 	case UVM_PGA_STRAT_ONLY:
1093 	case UVM_PGA_STRAT_FALLBACK:
1094 		/* Attempt to allocate from the specified free list. */
1095 		KASSERT(free_list >= 0 && free_list < VM_NFREELIST);
1096 		pg = uvm_pagealloc_pgfl(&uvm.page_free[free_list],
1097 		    try1, try2, &color);
1098 		if (pg != NULL)
1099 			goto gotit;
1100 
1101 		/* Fall back, if possible. */
1102 		if (strat == UVM_PGA_STRAT_FALLBACK) {
1103 			strat = UVM_PGA_STRAT_NORMAL;
1104 			goto again;
1105 		}
1106 
1107 		/* No pages free! */
1108 		goto fail;
1109 
1110 	default:
1111 		panic("uvm_pagealloc_strat: bad strat %d", strat);
1112 		/* NOTREACHED */
1113 	}
1114 
1115  gotit:
1116 	/*
1117 	 * We now know which color we actually allocated from; set
1118 	 * the next color accordingly.
1119 	 */
1120 
1121 	uvm.page_free_nextcolor = (color + 1) & uvmexp.colormask;
1122 
1123 	/*
1124 	 * update allocation statistics and remember if we have to
1125 	 * zero the page
1126 	 */
1127 
1128 	if (flags & UVM_PGA_ZERO) {
1129 		if (pg->flags & PG_ZERO) {
1130 			uvmexp.pga_zerohit++;
1131 			zeroit = 0;
1132 		} else {
1133 			uvmexp.pga_zeromiss++;
1134 			zeroit = 1;
1135 		}
1136 	}
1137 	uvm_unlock_fpageq(s);
1138 
1139 	pg->offset = off;
1140 	pg->uobject = obj;
1141 	pg->uanon = anon;
1142 	pg->flags = PG_BUSY|PG_CLEAN|PG_FAKE;
1143 	if (anon) {
1144 		anon->u.an_page = pg;
1145 		pg->pqflags = PQ_ANON;
1146 		uvmexp.anonpages++;
1147 	} else {
1148 		if (obj) {
1149 			uvm_pageinsert(pg);
1150 		}
1151 		pg->pqflags = 0;
1152 	}
1153 #if defined(UVM_PAGE_TRKOWN)
1154 	pg->owner_tag = NULL;
1155 #endif
1156 	UVM_PAGE_OWN(pg, "new alloc");
1157 
1158 	if (flags & UVM_PGA_ZERO) {
1159 		/*
1160 		 * A zero'd page is not clean.  If we got a page not already
1161 		 * zero'd, then we have to zero it ourselves.
1162 		 */
1163 		pg->flags &= ~PG_CLEAN;
1164 		if (zeroit)
1165 			pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1166 	}
1167 
1168 	return(pg);
1169 
1170  fail:
1171 	uvm_unlock_fpageq(s);
1172 	return (NULL);
1173 }
1174 
1175 /*
1176  * uvm_pagerealloc: reallocate a page from one object to another
1177  *
1178  * => both objects must be locked
1179  */
1180 
1181 void
1182 uvm_pagerealloc(pg, newobj, newoff)
1183 	struct vm_page *pg;
1184 	struct uvm_object *newobj;
1185 	voff_t newoff;
1186 {
1187 	/*
1188 	 * remove it from the old object
1189 	 */
1190 
1191 	if (pg->uobject) {
1192 		uvm_pageremove(pg);
1193 	}
1194 
1195 	/*
1196 	 * put it in the new object
1197 	 */
1198 
1199 	if (newobj) {
1200 		pg->uobject = newobj;
1201 		pg->offset = newoff;
1202 		uvm_pageinsert(pg);
1203 	}
1204 }
1205 
1206 /*
1207  * uvm_pagefree: free page
1208  *
1209  * => erase page's identity (i.e. remove from hash/object)
1210  * => put page on free list
1211  * => caller must lock owning object (either anon or uvm_object)
1212  * => caller must lock page queues
1213  * => assumes all valid mappings of pg are gone
1214  */
1215 
1216 void
1217 uvm_pagefree(pg)
1218 	struct vm_page *pg;
1219 {
1220 	int s;
1221 
1222 	KASSERT((pg->flags & PG_PAGEOUT) == 0);
1223 	LOCK_ASSERT(simple_lock_held(&uvm.pageqlock) ||
1224 		    (pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) == 0);
1225 	LOCK_ASSERT(pg->uobject == NULL ||
1226 		    simple_lock_held(&pg->uobject->vmobjlock));
1227 	LOCK_ASSERT(pg->uobject != NULL || pg->uanon == NULL ||
1228 		    simple_lock_held(&pg->uanon->an_lock));
1229 
1230 #ifdef DEBUG
1231 	if (pg->uobject == (void *)0xdeadbeef &&
1232 	    pg->uanon == (void *)0xdeadbeef) {
1233 		panic("uvm_pagefree: freeing free page %p", pg);
1234 	}
1235 #endif
1236 
1237 	/*
1238 	 * if the page is loaned, resolve the loan instead of freeing.
1239 	 */
1240 
1241 	if (pg->loan_count) {
1242 		KASSERT(pg->wire_count == 0);
1243 
1244 		/*
1245 		 * if the page is owned by an anon then we just want to
1246 		 * drop anon ownership.  the kernel will free the page when
1247 		 * it is done with it.  if the page is owned by an object,
1248 		 * remove it from the object and mark it dirty for the benefit
1249 		 * of possible anon owners.
1250 		 *
1251 		 * regardless of previous ownership, wakeup any waiters,
1252 		 * unbusy the page, and we're done.
1253 		 */
1254 
1255 		if (pg->uobject != NULL) {
1256 			uvm_pageremove(pg);
1257 			pg->flags &= ~PG_CLEAN;
1258 		} else if (pg->uanon != NULL) {
1259 			if ((pg->pqflags & PQ_ANON) == 0) {
1260 				pg->loan_count--;
1261 			} else {
1262 				pg->pqflags &= ~PQ_ANON;
1263 			}
1264 			pg->uanon = NULL;
1265 		}
1266 		if (pg->flags & PG_WANTED) {
1267 			wakeup(pg);
1268 		}
1269 		pg->flags &= ~(PG_WANTED|PG_BUSY|PG_RELEASED|PG_PAGER1);
1270 #ifdef UVM_PAGE_TRKOWN
1271 		pg->owner_tag = NULL;
1272 #endif
1273 		if (pg->loan_count) {
1274 			uvm_pagedequeue(pg);
1275 			return;
1276 		}
1277 	}
1278 
1279 	/*
1280 	 * remove page from its object or anon.
1281 	 */
1282 
1283 	if (pg->uobject != NULL) {
1284 		uvm_pageremove(pg);
1285 	} else if (pg->uanon != NULL) {
1286 		pg->uanon->u.an_page = NULL;
1287 		uvmexp.anonpages--;
1288 	}
1289 
1290 	/*
1291 	 * now remove the page from the queues.
1292 	 */
1293 
1294 	uvm_pagedequeue(pg);
1295 
1296 	/*
1297 	 * if the page was wired, unwire it now.
1298 	 */
1299 
1300 	if (pg->wire_count) {
1301 		pg->wire_count = 0;
1302 		uvmexp.wired--;
1303 	}
1304 
1305 	/*
1306 	 * and put on free queue
1307 	 */
1308 
1309 	pg->flags &= ~PG_ZERO;
1310 
1311 	s = uvm_lock_fpageq();
1312 	TAILQ_INSERT_TAIL(&uvm.page_free[
1313 	    uvm_page_lookup_freelist(pg)].pgfl_buckets[
1314 	    VM_PGCOLOR_BUCKET(pg)].pgfl_queues[PGFL_UNKNOWN], pg, pageq);
1315 	pg->pqflags = PQ_FREE;
1316 #ifdef DEBUG
1317 	pg->uobject = (void *)0xdeadbeef;
1318 	pg->offset = 0xdeadbeef;
1319 	pg->uanon = (void *)0xdeadbeef;
1320 #endif
1321 	uvmexp.free++;
1322 
1323 	if (uvmexp.zeropages < UVM_PAGEZERO_TARGET)
1324 		uvm.page_idle_zero = vm_page_zero_enable;
1325 
1326 	uvm_unlock_fpageq(s);
1327 }
1328 
1329 /*
1330  * uvm_page_unbusy: unbusy an array of pages.
1331  *
1332  * => pages must either all belong to the same object, or all belong to anons.
1333  * => if pages are object-owned, object must be locked.
1334  * => if pages are anon-owned, anons must be locked.
1335  * => caller must lock page queues if pages may be released.
1336  */
1337 
1338 void
1339 uvm_page_unbusy(pgs, npgs)
1340 	struct vm_page **pgs;
1341 	int npgs;
1342 {
1343 	struct vm_page *pg;
1344 	int i;
1345 	UVMHIST_FUNC("uvm_page_unbusy"); UVMHIST_CALLED(ubchist);
1346 
1347 	for (i = 0; i < npgs; i++) {
1348 		pg = pgs[i];
1349 		if (pg == NULL || pg == PGO_DONTCARE) {
1350 			continue;
1351 		}
1352 		if (pg->flags & PG_WANTED) {
1353 			wakeup(pg);
1354 		}
1355 		if (pg->flags & PG_RELEASED) {
1356 			UVMHIST_LOG(ubchist, "releasing pg %p", pg,0,0,0);
1357 			pg->flags &= ~PG_RELEASED;
1358 			uvm_pagefree(pg);
1359 		} else {
1360 			UVMHIST_LOG(ubchist, "unbusying pg %p", pg,0,0,0);
1361 			pg->flags &= ~(PG_WANTED|PG_BUSY);
1362 			UVM_PAGE_OWN(pg, NULL);
1363 		}
1364 	}
1365 }
1366 
1367 #if defined(UVM_PAGE_TRKOWN)
1368 /*
1369  * uvm_page_own: set or release page ownership
1370  *
1371  * => this is a debugging function that keeps track of who sets PG_BUSY
1372  *	and where they do it.   it can be used to track down problems
1373  *	such a process setting "PG_BUSY" and never releasing it.
1374  * => page's object [if any] must be locked
1375  * => if "tag" is NULL then we are releasing page ownership
1376  */
1377 void
1378 uvm_page_own(pg, tag)
1379 	struct vm_page *pg;
1380 	char *tag;
1381 {
1382 	KASSERT((pg->flags & (PG_PAGEOUT|PG_RELEASED)) == 0);
1383 
1384 	/* gain ownership? */
1385 	if (tag) {
1386 		if (pg->owner_tag) {
1387 			printf("uvm_page_own: page %p already owned "
1388 			    "by proc %d [%s]\n", pg,
1389 			    pg->owner, pg->owner_tag);
1390 			panic("uvm_page_own");
1391 		}
1392 		pg->owner = (curproc) ? curproc->p_pid :  (pid_t) -1;
1393 		pg->owner_tag = tag;
1394 		return;
1395 	}
1396 
1397 	/* drop ownership */
1398 	if (pg->owner_tag == NULL) {
1399 		printf("uvm_page_own: dropping ownership of an non-owned "
1400 		    "page (%p)\n", pg);
1401 		panic("uvm_page_own");
1402 	}
1403 	KASSERT((pg->pqflags & (PQ_ACTIVE|PQ_INACTIVE)) ||
1404 	    (pg->uanon == NULL && pg->uobject == NULL) ||
1405 	    pg->uobject == uvm.kernel_object ||
1406 	    pg->wire_count > 0 ||
1407 	    (pg->loan_count == 1 && pg->uanon == NULL) ||
1408 	    pg->loan_count > 1);
1409 	pg->owner_tag = NULL;
1410 }
1411 #endif
1412 
1413 /*
1414  * uvm_pageidlezero: zero free pages while the system is idle.
1415  *
1416  * => try to complete one color bucket at a time, to reduce our impact
1417  *	on the CPU cache.
1418  * => we loop until we either reach the target or whichqs indicates that
1419  *	there is a process ready to run.
1420  */
1421 void
1422 uvm_pageidlezero()
1423 {
1424 	struct vm_page *pg;
1425 	struct pgfreelist *pgfl;
1426 	int free_list, s, firstbucket;
1427 	static int nextbucket;
1428 
1429 	s = uvm_lock_fpageq();
1430 	firstbucket = nextbucket;
1431 	do {
1432 		if (sched_whichqs != 0) {
1433 			uvm_unlock_fpageq(s);
1434 			return;
1435 		}
1436 		if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) {
1437 			uvm.page_idle_zero = FALSE;
1438 			uvm_unlock_fpageq(s);
1439 			return;
1440 		}
1441 		for (free_list = 0; free_list < VM_NFREELIST; free_list++) {
1442 			pgfl = &uvm.page_free[free_list];
1443 			while ((pg = TAILQ_FIRST(&pgfl->pgfl_buckets[
1444 			    nextbucket].pgfl_queues[PGFL_UNKNOWN])) != NULL) {
1445 				if (sched_whichqs != 0) {
1446 					uvm_unlock_fpageq(s);
1447 					return;
1448 				}
1449 
1450 				TAILQ_REMOVE(&pgfl->pgfl_buckets[
1451 				    nextbucket].pgfl_queues[PGFL_UNKNOWN],
1452 				    pg, pageq);
1453 				uvmexp.free--;
1454 				uvm_unlock_fpageq(s);
1455 #ifdef PMAP_PAGEIDLEZERO
1456 				if (!PMAP_PAGEIDLEZERO(VM_PAGE_TO_PHYS(pg))) {
1457 
1458 					/*
1459 					 * The machine-dependent code detected
1460 					 * some reason for us to abort zeroing
1461 					 * pages, probably because there is a
1462 					 * process now ready to run.
1463 					 */
1464 
1465 					s = uvm_lock_fpageq();
1466 					TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[
1467 					    nextbucket].pgfl_queues[
1468 					    PGFL_UNKNOWN], pg, pageq);
1469 					uvmexp.free++;
1470 					uvmexp.zeroaborts++;
1471 					uvm_unlock_fpageq(s);
1472 					return;
1473 				}
1474 #else
1475 				pmap_zero_page(VM_PAGE_TO_PHYS(pg));
1476 #endif /* PMAP_PAGEIDLEZERO */
1477 				pg->flags |= PG_ZERO;
1478 
1479 				s = uvm_lock_fpageq();
1480 				TAILQ_INSERT_HEAD(&pgfl->pgfl_buckets[
1481 				    nextbucket].pgfl_queues[PGFL_ZEROS],
1482 				    pg, pageq);
1483 				uvmexp.free++;
1484 				uvmexp.zeropages++;
1485 			}
1486 		}
1487 		nextbucket = (nextbucket + 1) & uvmexp.colormask;
1488 	} while (nextbucket != firstbucket);
1489 	uvm_unlock_fpageq(s);
1490 }
1491