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