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