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