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