xref: /freebsd/sys/vm/vm_page.c (revision 0640d357f29fb1c0daaaffadd0416c5981413afd)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)vm_page.c	7.4 (Berkeley) 5/7/91
37  *	$Id: vm_page.c,v 1.111 1998/10/28 13:37:02 dg Exp $
38  */
39 
40 /*
41  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
42  * All rights reserved.
43  *
44  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
45  *
46  * Permission to use, copy, modify and distribute this software and
47  * its documentation is hereby granted, provided that both the copyright
48  * notice and this permission notice appear in all copies of the
49  * software, derivative works or modified versions, and any portions
50  * thereof, and that both notices appear in supporting documentation.
51  *
52  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55  *
56  * Carnegie Mellon requests users of this software to return to
57  *
58  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
59  *  School of Computer Science
60  *  Carnegie Mellon University
61  *  Pittsburgh PA 15213-3890
62  *
63  * any improvements or extensions that they make and grant Carnegie the
64  * rights to redistribute these changes.
65  */
66 
67 /*
68  *	Resident memory management module.
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <sys/proc.h>
75 #include <sys/vmmeter.h>
76 #include <sys/vnode.h>
77 
78 #include <vm/vm.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_prot.h>
81 #include <sys/lock.h>
82 #include <vm/vm_kern.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_extern.h>
87 
88 static void	vm_page_queue_init __P((void));
89 static vm_page_t vm_page_select_free __P((vm_object_t object,
90 			vm_pindex_t pindex, int prefqueue));
91 static vm_page_t vm_page_select_cache __P((vm_object_t, vm_pindex_t));
92 
93 /*
94  *	Associated with page of user-allocatable memory is a
95  *	page structure.
96  */
97 
98 static struct pglist *vm_page_buckets;	/* Array of buckets */
99 static int vm_page_bucket_count;	/* How big is array? */
100 static int vm_page_hash_mask;		/* Mask for hash function */
101 static volatile int vm_page_bucket_generation;
102 
103 struct pglist vm_page_queue_free[PQ_L2_SIZE] = {0};
104 struct pglist vm_page_queue_zero[PQ_L2_SIZE] = {0};
105 struct pglist vm_page_queue_active = {0};
106 struct pglist vm_page_queue_inactive = {0};
107 struct pglist vm_page_queue_cache[PQ_L2_SIZE] = {0};
108 
109 static int no_queue=0;
110 
111 struct vpgqueues vm_page_queues[PQ_COUNT] = {0};
112 static int pqcnt[PQ_COUNT] = {0};
113 
114 static void
115 vm_page_queue_init(void) {
116 	int i;
117 
118 	vm_page_queues[PQ_NONE].pl = NULL;
119 	vm_page_queues[PQ_NONE].cnt = &no_queue;
120 	for(i=0;i<PQ_L2_SIZE;i++) {
121 		vm_page_queues[PQ_FREE+i].pl = &vm_page_queue_free[i];
122 		vm_page_queues[PQ_FREE+i].cnt = &cnt.v_free_count;
123 	}
124 	for(i=0;i<PQ_L2_SIZE;i++) {
125 		vm_page_queues[PQ_ZERO+i].pl = &vm_page_queue_zero[i];
126 		vm_page_queues[PQ_ZERO+i].cnt = &cnt.v_free_count;
127 	}
128 	vm_page_queues[PQ_INACTIVE].pl = &vm_page_queue_inactive;
129 	vm_page_queues[PQ_INACTIVE].cnt = &cnt.v_inactive_count;
130 
131 	vm_page_queues[PQ_ACTIVE].pl = &vm_page_queue_active;
132 	vm_page_queues[PQ_ACTIVE].cnt = &cnt.v_active_count;
133 	for(i=0;i<PQ_L2_SIZE;i++) {
134 		vm_page_queues[PQ_CACHE+i].pl = &vm_page_queue_cache[i];
135 		vm_page_queues[PQ_CACHE+i].cnt = &cnt.v_cache_count;
136 	}
137 	for(i=0;i<PQ_COUNT;i++) {
138 		if (vm_page_queues[i].pl) {
139 			TAILQ_INIT(vm_page_queues[i].pl);
140 		} else if (i != 0) {
141 			panic("vm_page_queue_init: queue %d is null", i);
142 		}
143 		vm_page_queues[i].lcnt = &pqcnt[i];
144 	}
145 }
146 
147 vm_page_t vm_page_array = 0;
148 static int vm_page_array_size = 0;
149 long first_page = 0;
150 static long last_page;
151 static vm_size_t page_mask;
152 static int page_shift;
153 int vm_page_zero_count = 0;
154 
155 /*
156  * map of contiguous valid DEV_BSIZE chunks in a page
157  * (this list is valid for page sizes upto 16*DEV_BSIZE)
158  */
159 static u_short vm_page_dev_bsize_chunks[] = {
160 	0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff,
161 	0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff
162 };
163 
164 static __inline int vm_page_hash __P((vm_object_t object, vm_pindex_t pindex));
165 static int vm_page_freechk_and_unqueue __P((vm_page_t m));
166 static void vm_page_free_wakeup __P((void));
167 
168 /*
169  *	vm_set_page_size:
170  *
171  *	Sets the page size, perhaps based upon the memory
172  *	size.  Must be called before any use of page-size
173  *	dependent functions.
174  *
175  *	Sets page_shift and page_mask from cnt.v_page_size.
176  */
177 void
178 vm_set_page_size()
179 {
180 
181 	if (cnt.v_page_size == 0)
182 		cnt.v_page_size = DEFAULT_PAGE_SIZE;
183 	page_mask = cnt.v_page_size - 1;
184 	if ((page_mask & cnt.v_page_size) != 0)
185 		panic("vm_set_page_size: page size not a power of two");
186 	for (page_shift = 0;; page_shift++)
187 		if ((1 << page_shift) == cnt.v_page_size)
188 			break;
189 }
190 
191 /*
192  *	vm_page_startup:
193  *
194  *	Initializes the resident memory module.
195  *
196  *	Allocates memory for the page cells, and
197  *	for the object/offset-to-page hash table headers.
198  *	Each page cell is initialized and placed on the free list.
199  */
200 
201 vm_offset_t
202 vm_page_startup(starta, enda, vaddr)
203 	register vm_offset_t starta;
204 	vm_offset_t enda;
205 	register vm_offset_t vaddr;
206 {
207 	register vm_offset_t mapped;
208 	register vm_page_t m;
209 	register struct pglist *bucket;
210 	vm_size_t npages, page_range;
211 	register vm_offset_t new_start;
212 	int i;
213 	vm_offset_t pa;
214 	int nblocks;
215 	vm_offset_t first_managed_page;
216 
217 	/* the biggest memory array is the second group of pages */
218 	vm_offset_t start;
219 	vm_offset_t biggestone, biggestsize;
220 
221 	vm_offset_t total;
222 
223 	total = 0;
224 	biggestsize = 0;
225 	biggestone = 0;
226 	nblocks = 0;
227 	vaddr = round_page(vaddr);
228 
229 	for (i = 0; phys_avail[i + 1]; i += 2) {
230 		phys_avail[i] = round_page(phys_avail[i]);
231 		phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
232 	}
233 
234 	for (i = 0; phys_avail[i + 1]; i += 2) {
235 		int size = phys_avail[i + 1] - phys_avail[i];
236 
237 		if (size > biggestsize) {
238 			biggestone = i;
239 			biggestsize = size;
240 		}
241 		++nblocks;
242 		total += size;
243 	}
244 
245 	start = phys_avail[biggestone];
246 
247 	/*
248 	 * Initialize the queue headers for the free queue, the active queue
249 	 * and the inactive queue.
250 	 */
251 
252 	vm_page_queue_init();
253 
254 	/*
255 	 * Allocate (and initialize) the hash table buckets.
256 	 *
257 	 * The number of buckets MUST BE a power of 2, and the actual value is
258 	 * the next power of 2 greater than the number of physical pages in
259 	 * the system.
260 	 *
261 	 * Note: This computation can be tweaked if desired.
262 	 */
263 	vm_page_buckets = (struct pglist *) vaddr;
264 	bucket = vm_page_buckets;
265 	if (vm_page_bucket_count == 0) {
266 		vm_page_bucket_count = 1;
267 		while (vm_page_bucket_count < atop(total))
268 			vm_page_bucket_count <<= 1;
269 	}
270 	vm_page_hash_mask = vm_page_bucket_count - 1;
271 
272 	/*
273 	 * Validate these addresses.
274 	 */
275 
276 	new_start = start + vm_page_bucket_count * sizeof(struct pglist);
277 	new_start = round_page(new_start);
278 	mapped = round_page(vaddr);
279 	vaddr = pmap_map(mapped, start, new_start,
280 	    VM_PROT_READ | VM_PROT_WRITE);
281 	start = new_start;
282 	vaddr = round_page(vaddr);
283 	bzero((caddr_t) mapped, vaddr - mapped);
284 
285 	for (i = 0; i < vm_page_bucket_count; i++) {
286 		TAILQ_INIT(bucket);
287 		bucket++;
288 	}
289 
290 	/*
291 	 * Compute the number of pages of memory that will be available for
292 	 * use (taking into account the overhead of a page structure per
293 	 * page).
294 	 */
295 
296 	first_page = phys_avail[0] / PAGE_SIZE;
297 	last_page = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE;
298 
299 	page_range = last_page - (phys_avail[0] / PAGE_SIZE);
300 	npages = (total - (page_range * sizeof(struct vm_page)) -
301 	    (start - phys_avail[biggestone])) / PAGE_SIZE;
302 
303 	/*
304 	 * Initialize the mem entry structures now, and put them in the free
305 	 * queue.
306 	 */
307 	vm_page_array = (vm_page_t) vaddr;
308 	mapped = vaddr;
309 
310 	/*
311 	 * Validate these addresses.
312 	 */
313 	new_start = round_page(start + page_range * sizeof(struct vm_page));
314 	mapped = pmap_map(mapped, start, new_start,
315 	    VM_PROT_READ | VM_PROT_WRITE);
316 	start = new_start;
317 
318 	first_managed_page = start / PAGE_SIZE;
319 
320 	/*
321 	 * Clear all of the page structures
322 	 */
323 	bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page));
324 	vm_page_array_size = page_range;
325 
326 	cnt.v_page_count = 0;
327 	cnt.v_free_count = 0;
328 	for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) {
329 		if (i == biggestone)
330 			pa = ptoa(first_managed_page);
331 		else
332 			pa = phys_avail[i];
333 		while (pa < phys_avail[i + 1] && npages-- > 0) {
334 			++cnt.v_page_count;
335 			++cnt.v_free_count;
336 			m = PHYS_TO_VM_PAGE(pa);
337 			m->phys_addr = pa;
338 			m->flags = 0;
339 			m->pc = (pa >> PAGE_SHIFT) & PQ_L2_MASK;
340 			m->queue = m->pc + PQ_FREE;
341 			TAILQ_INSERT_TAIL(vm_page_queues[m->queue].pl, m, pageq);
342 			++(*vm_page_queues[m->queue].lcnt);
343 			pa += PAGE_SIZE;
344 		}
345 	}
346 	return (mapped);
347 }
348 
349 /*
350  *	vm_page_hash:
351  *
352  *	Distributes the object/offset key pair among hash buckets.
353  *
354  *	NOTE:  This macro depends on vm_page_bucket_count being a power of 2.
355  */
356 static __inline int
357 vm_page_hash(object, pindex)
358 	vm_object_t object;
359 	vm_pindex_t pindex;
360 {
361 	return ((((uintptr_t) object) >> 5) + (pindex >> 1)) & vm_page_hash_mask;
362 }
363 
364 /*
365  *	vm_page_insert:		[ internal use only ]
366  *
367  *	Inserts the given mem entry into the object/object-page
368  *	table and object list.
369  *
370  *	The object and page must be locked, and must be splhigh.
371  */
372 
373 void
374 vm_page_insert(m, object, pindex)
375 	register vm_page_t m;
376 	register vm_object_t object;
377 	register vm_pindex_t pindex;
378 {
379 	register struct pglist *bucket;
380 
381 	if (m->object != NULL)
382 		panic("vm_page_insert: already inserted");
383 
384 	/*
385 	 * Record the object/offset pair in this page
386 	 */
387 
388 	m->object = object;
389 	m->pindex = pindex;
390 
391 	/*
392 	 * Insert it into the object_object/offset hash table
393 	 */
394 
395 	bucket = &vm_page_buckets[vm_page_hash(object, pindex)];
396 	TAILQ_INSERT_TAIL(bucket, m, hashq);
397 	vm_page_bucket_generation++;
398 
399 	/*
400 	 * Now link into the object's list of backed pages.
401 	 */
402 
403 	TAILQ_INSERT_TAIL(&object->memq, m, listq);
404 	m->object->page_hint = m;
405 	m->object->generation++;
406 
407 	if (m->wire_count)
408 		object->wire_count++;
409 
410 	if ((m->queue - m->pc) == PQ_CACHE)
411 		object->cache_count++;
412 
413 	/*
414 	 * And show that the object has one more resident page.
415 	 */
416 
417 	object->resident_page_count++;
418 }
419 
420 /*
421  *	vm_page_remove:		[ internal use only ]
422  *				NOTE: used by device pager as well -wfj
423  *
424  *	Removes the given mem entry from the object/offset-page
425  *	table and the object page list.
426  *
427  *	The object and page must be locked, and at splhigh.
428  */
429 
430 void
431 vm_page_remove(m)
432 	register vm_page_t m;
433 {
434 	register struct pglist *bucket;
435 	vm_object_t object;
436 
437 	if (m->object == NULL)
438 		return;
439 
440 #if !defined(MAX_PERF)
441 	if ((m->flags & PG_BUSY) == 0) {
442 		panic("vm_page_remove: page not busy");
443 	}
444 #endif
445 
446 	vm_page_flag_clear(m, PG_BUSY);
447 	if (m->flags & PG_WANTED) {
448 		vm_page_flag_clear(m, PG_WANTED);
449 		wakeup(m);
450 	}
451 
452 	object = m->object;
453 	if (object->page_hint == m)
454 		object->page_hint = NULL;
455 
456 	if (m->wire_count)
457 		object->wire_count--;
458 
459 	if ((m->queue - m->pc) == PQ_CACHE)
460 		object->cache_count--;
461 
462 	/*
463 	 * Remove from the object_object/offset hash table
464 	 */
465 
466 	bucket = &vm_page_buckets[vm_page_hash(m->object, m->pindex)];
467 	TAILQ_REMOVE(bucket, m, hashq);
468 	vm_page_bucket_generation++;
469 
470 	/*
471 	 * Now remove from the object's list of backed pages.
472 	 */
473 
474 	TAILQ_REMOVE(&object->memq, m, listq);
475 
476 	/*
477 	 * And show that the object has one fewer resident page.
478 	 */
479 
480 	object->resident_page_count--;
481 	object->generation++;
482 
483 	m->object = NULL;
484 }
485 
486 /*
487  *	vm_page_lookup:
488  *
489  *	Returns the page associated with the object/offset
490  *	pair specified; if none is found, NULL is returned.
491  *
492  *	The object must be locked.  No side effects.
493  */
494 
495 vm_page_t
496 vm_page_lookup(object, pindex)
497 	register vm_object_t object;
498 	register vm_pindex_t pindex;
499 {
500 	register vm_page_t m;
501 	register struct pglist *bucket;
502 	int generation;
503 
504 	/*
505 	 * Search the hash table for this object/offset pair
506 	 */
507 
508 	if (object->page_hint && (object->page_hint->pindex == pindex) &&
509 		(object->page_hint->object == object))
510 		return object->page_hint;
511 
512 retry:
513 	generation = vm_page_bucket_generation;
514 	bucket = &vm_page_buckets[vm_page_hash(object, pindex)];
515 	for (m = TAILQ_FIRST(bucket); m != NULL; m = TAILQ_NEXT(m,hashq)) {
516 		if ((m->object == object) && (m->pindex == pindex)) {
517 			if (vm_page_bucket_generation != generation)
518 				goto retry;
519 			m->object->page_hint = m;
520 			return (m);
521 		}
522 	}
523 	if (vm_page_bucket_generation != generation)
524 		goto retry;
525 	return (NULL);
526 }
527 
528 /*
529  *	vm_page_rename:
530  *
531  *	Move the given memory entry from its
532  *	current object to the specified target object/offset.
533  *
534  *	The object must be locked.
535  */
536 void
537 vm_page_rename(m, new_object, new_pindex)
538 	register vm_page_t m;
539 	register vm_object_t new_object;
540 	vm_pindex_t new_pindex;
541 {
542 	int s;
543 
544 	s = splvm();
545 	vm_page_remove(m);
546 	vm_page_insert(m, new_object, new_pindex);
547 	splx(s);
548 }
549 
550 /*
551  * vm_page_unqueue without any wakeup
552  */
553 void
554 vm_page_unqueue_nowakeup(m)
555 	vm_page_t m;
556 {
557 	int queue = m->queue;
558 	struct vpgqueues *pq;
559 	if (queue != PQ_NONE) {
560 		pq = &vm_page_queues[queue];
561 		m->queue = PQ_NONE;
562 		TAILQ_REMOVE(pq->pl, m, pageq);
563 		(*pq->cnt)--;
564 		(*pq->lcnt)--;
565 		if ((queue - m->pc) == PQ_CACHE) {
566 			if (m->object)
567 				m->object->cache_count--;
568 		}
569 	}
570 }
571 
572 /*
573  * vm_page_unqueue must be called at splhigh();
574  */
575 void
576 vm_page_unqueue(m)
577 	vm_page_t m;
578 {
579 	int queue = m->queue;
580 	struct vpgqueues *pq;
581 	if (queue != PQ_NONE) {
582 		m->queue = PQ_NONE;
583 		pq = &vm_page_queues[queue];
584 		TAILQ_REMOVE(pq->pl, m, pageq);
585 		(*pq->cnt)--;
586 		(*pq->lcnt)--;
587 		if ((queue - m->pc) == PQ_CACHE) {
588 			if ((cnt.v_cache_count + cnt.v_free_count) <
589 				(cnt.v_free_reserved + cnt.v_cache_min))
590 				pagedaemon_wakeup();
591 			if (m->object)
592 				m->object->cache_count--;
593 		}
594 	}
595 }
596 
597 /*
598  * Find a page on the specified queue with color optimization.
599  */
600 vm_page_t
601 vm_page_list_find(basequeue, index)
602 	int basequeue, index;
603 {
604 #if PQ_L2_SIZE > 1
605 
606 	int i,j;
607 	vm_page_t m;
608 	int hindex;
609 	struct vpgqueues *pq;
610 
611 	pq = &vm_page_queues[basequeue];
612 
613 	m = TAILQ_FIRST(pq[index].pl);
614 	if (m)
615 		return m;
616 
617 	for(j = 0; j < PQ_L1_SIZE; j++) {
618 		int ij;
619 		for(i = (PQ_L2_SIZE / 2) - PQ_L1_SIZE;
620 			(ij = i + j) > 0;
621 			i -= PQ_L1_SIZE) {
622 
623 			hindex = index + ij;
624 			if (hindex >= PQ_L2_SIZE)
625 				hindex -= PQ_L2_SIZE;
626 			if (m = TAILQ_FIRST(pq[hindex].pl))
627 				return m;
628 
629 			hindex = index - ij;
630 			if (hindex < 0)
631 				hindex += PQ_L2_SIZE;
632 			if (m = TAILQ_FIRST(pq[hindex].pl))
633 				return m;
634 		}
635 	}
636 
637 	hindex = index + PQ_L2_SIZE / 2;
638 	if (hindex >= PQ_L2_SIZE)
639 		hindex -= PQ_L2_SIZE;
640 	m = TAILQ_FIRST(pq[hindex].pl);
641 	if (m)
642 		return m;
643 
644 	return NULL;
645 #else
646 	return TAILQ_FIRST(vm_page_queues[basequeue].pl);
647 #endif
648 
649 }
650 
651 /*
652  * Find a page on the specified queue with color optimization.
653  */
654 vm_page_t
655 vm_page_select(object, pindex, basequeue)
656 	vm_object_t object;
657 	vm_pindex_t pindex;
658 	int basequeue;
659 {
660 
661 #if PQ_L2_SIZE > 1
662 	int index;
663 	index = (pindex + object->pg_color) & PQ_L2_MASK;
664 	return vm_page_list_find(basequeue, index);
665 
666 #else
667 	return TAILQ_FIRST(vm_page_queues[basequeue].pl);
668 #endif
669 
670 }
671 
672 /*
673  * Find a page on the cache queue with color optimization.  As pages
674  * might be found, but not applicable, they are deactivated.  This
675  * keeps us from using potentially busy cached pages.
676  */
677 vm_page_t
678 vm_page_select_cache(object, pindex)
679 	vm_object_t object;
680 	vm_pindex_t pindex;
681 {
682 	vm_page_t m;
683 
684 	while (TRUE) {
685 #if PQ_L2_SIZE > 1
686 		int index;
687 		index = (pindex + object->pg_color) & PQ_L2_MASK;
688 		m = vm_page_list_find(PQ_CACHE, index);
689 
690 #else
691 		m = TAILQ_FIRST(vm_page_queues[PQ_CACHE].pl);
692 #endif
693 		if (m && ((m->flags & PG_BUSY) || m->busy ||
694 			       m->hold_count || m->wire_count)) {
695 			vm_page_deactivate(m);
696 			continue;
697 		}
698 		return m;
699 	}
700 }
701 
702 /*
703  * Find a free or zero page, with specified preference.
704  */
705 static vm_page_t
706 vm_page_select_free(object, pindex, prefqueue)
707 	vm_object_t object;
708 	vm_pindex_t pindex;
709 	int prefqueue;
710 {
711 #if PQ_L2_SIZE > 1
712 	int i,j;
713 	int index, hindex;
714 #endif
715 	vm_page_t m, mh;
716 	int oqueuediff;
717 	struct vpgqueues *pq;
718 
719 	if (prefqueue == PQ_ZERO)
720 		oqueuediff = PQ_FREE - PQ_ZERO;
721 	else
722 		oqueuediff = PQ_ZERO - PQ_FREE;
723 
724 	if (mh = object->page_hint) {
725 		 if (mh->pindex == (pindex - 1)) {
726 			if ((mh->flags & PG_FICTITIOUS) == 0) {
727 				if ((mh < &vm_page_array[cnt.v_page_count-1]) &&
728 					(mh >= &vm_page_array[0])) {
729 					int queue;
730 					m = mh + 1;
731 					if (VM_PAGE_TO_PHYS(m) == (VM_PAGE_TO_PHYS(mh) + PAGE_SIZE)) {
732 						queue = m->queue - m->pc;
733 						if (queue == PQ_FREE || queue == PQ_ZERO) {
734 							return m;
735 						}
736 					}
737 				}
738 			}
739 		}
740 	}
741 
742 	pq = &vm_page_queues[prefqueue];
743 
744 #if PQ_L2_SIZE > 1
745 
746 	index = (pindex + object->pg_color) & PQ_L2_MASK;
747 
748 	if (m = TAILQ_FIRST(pq[index].pl))
749 		return m;
750 	if (m = TAILQ_FIRST(pq[index + oqueuediff].pl))
751 		return m;
752 
753 	for(j = 0; j < PQ_L1_SIZE; j++) {
754 		int ij;
755 		for(i = (PQ_L2_SIZE / 2) - PQ_L1_SIZE;
756 			(ij = i + j) >= 0;
757 			i -= PQ_L1_SIZE) {
758 
759 			hindex = index + ij;
760 			if (hindex >= PQ_L2_SIZE)
761 				hindex -= PQ_L2_SIZE;
762 			if (m = TAILQ_FIRST(pq[hindex].pl))
763 				return m;
764 			if (m = TAILQ_FIRST(pq[hindex + oqueuediff].pl))
765 				return m;
766 
767 			hindex = index - ij;
768 			if (hindex < 0)
769 				hindex += PQ_L2_SIZE;
770 			if (m = TAILQ_FIRST(pq[hindex].pl))
771 				return m;
772 			if (m = TAILQ_FIRST(pq[hindex + oqueuediff].pl))
773 				return m;
774 		}
775 	}
776 
777 	hindex = index + PQ_L2_SIZE / 2;
778 	if (hindex >= PQ_L2_SIZE)
779 		hindex -= PQ_L2_SIZE;
780 	if (m = TAILQ_FIRST(pq[hindex].pl))
781 		return m;
782 	if (m = TAILQ_FIRST(pq[hindex+oqueuediff].pl))
783 		return m;
784 
785 #else
786 	if (m = TAILQ_FIRST(pq[0].pl))
787 		return m;
788 	else
789 		return TAILQ_FIRST(pq[oqueuediff].pl);
790 #endif
791 
792 	return NULL;
793 }
794 
795 /*
796  *	vm_page_alloc:
797  *
798  *	Allocate and return a memory cell associated
799  *	with this VM object/offset pair.
800  *
801  *	page_req classes:
802  *	VM_ALLOC_NORMAL		normal process request
803  *	VM_ALLOC_SYSTEM		system *really* needs a page
804  *	VM_ALLOC_INTERRUPT	interrupt time request
805  *	VM_ALLOC_ZERO		zero page
806  *
807  *	Object must be locked.
808  */
809 vm_page_t
810 vm_page_alloc(object, pindex, page_req)
811 	vm_object_t object;
812 	vm_pindex_t pindex;
813 	int page_req;
814 {
815 	register vm_page_t m;
816 	struct vpgqueues *pq;
817 	vm_object_t oldobject;
818 	int queue, qtype;
819 	int s;
820 
821 #ifdef DIAGNOSTIC
822 	m = vm_page_lookup(object, pindex);
823 	if (m)
824 		panic("vm_page_alloc: page already allocated");
825 #endif
826 
827 	if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) {
828 		page_req = VM_ALLOC_SYSTEM;
829 	};
830 
831 	s = splvm();
832 
833 	switch (page_req) {
834 
835 	case VM_ALLOC_NORMAL:
836 		if (cnt.v_free_count >= cnt.v_free_reserved) {
837 			m = vm_page_select_free(object, pindex, PQ_FREE);
838 #if defined(DIAGNOSTIC)
839 			if (m == NULL)
840 				panic("vm_page_alloc(NORMAL): missing page on free queue\n");
841 #endif
842 		} else {
843 			m = vm_page_select_cache(object, pindex);
844 			if (m == NULL) {
845 				splx(s);
846 #if defined(DIAGNOSTIC)
847 				if (cnt.v_cache_count > 0)
848 					printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count);
849 #endif
850 				vm_pageout_deficit++;
851 				pagedaemon_wakeup();
852 				return (NULL);
853 			}
854 		}
855 		break;
856 
857 	case VM_ALLOC_ZERO:
858 		if (cnt.v_free_count >= cnt.v_free_reserved) {
859 			m = vm_page_select_free(object, pindex, PQ_ZERO);
860 #if defined(DIAGNOSTIC)
861 			if (m == NULL)
862 				panic("vm_page_alloc(ZERO): missing page on free queue\n");
863 #endif
864 		} else {
865 			m = vm_page_select_cache(object, pindex);
866 			if (m == NULL) {
867 				splx(s);
868 #if defined(DIAGNOSTIC)
869 				if (cnt.v_cache_count > 0)
870 					printf("vm_page_alloc(ZERO): missing pages on cache queue: %d\n", cnt.v_cache_count);
871 #endif
872 				vm_pageout_deficit++;
873 				pagedaemon_wakeup();
874 				return (NULL);
875 			}
876 		}
877 		break;
878 
879 	case VM_ALLOC_SYSTEM:
880 		if ((cnt.v_free_count >= cnt.v_free_reserved) ||
881 		    ((cnt.v_cache_count == 0) &&
882 		    (cnt.v_free_count >= cnt.v_interrupt_free_min))) {
883 			m = vm_page_select_free(object, pindex, PQ_FREE);
884 #if defined(DIAGNOSTIC)
885 			if (m == NULL)
886 				panic("vm_page_alloc(SYSTEM): missing page on free queue\n");
887 #endif
888 		} else {
889 			m = vm_page_select_cache(object, pindex);
890 			if (m == NULL) {
891 				splx(s);
892 #if defined(DIAGNOSTIC)
893 				if (cnt.v_cache_count > 0)
894 					printf("vm_page_alloc(SYSTEM): missing pages on cache queue: %d\n", cnt.v_cache_count);
895 #endif
896 				vm_pageout_deficit++;
897 				pagedaemon_wakeup();
898 				return (NULL);
899 			}
900 		}
901 		break;
902 
903 	case VM_ALLOC_INTERRUPT:
904 		if (cnt.v_free_count > 0) {
905 			m = vm_page_select_free(object, pindex, PQ_FREE);
906 #if defined(DIAGNOSTIC)
907 			if (m == NULL)
908 				panic("vm_page_alloc(INTERRUPT): missing page on free queue\n");
909 #endif
910 		} else {
911 			splx(s);
912 			vm_pageout_deficit++;
913 			pagedaemon_wakeup();
914 			return (NULL);
915 		}
916 		break;
917 
918 	default:
919 		m = NULL;
920 #if !defined(MAX_PERF)
921 		panic("vm_page_alloc: invalid allocation class");
922 #endif
923 	}
924 
925 	queue = m->queue;
926 	qtype = queue - m->pc;
927 	if (qtype == PQ_ZERO)
928 		vm_page_zero_count--;
929 	pq = &vm_page_queues[queue];
930 	TAILQ_REMOVE(pq->pl, m, pageq);
931 	(*pq->cnt)--;
932 	(*pq->lcnt)--;
933 	oldobject = NULL;
934 	if (qtype == PQ_ZERO) {
935 		m->flags = PG_ZERO | PG_BUSY;
936 	} else if (qtype == PQ_CACHE) {
937 		oldobject = m->object;
938 		vm_page_busy(m);
939 		vm_page_remove(m);
940 		m->flags = PG_BUSY;
941 	} else {
942 		m->flags = PG_BUSY;
943 	}
944 	m->wire_count = 0;
945 	m->hold_count = 0;
946 	m->act_count = 0;
947 	m->busy = 0;
948 	m->valid = 0;
949 	m->dirty = 0;
950 	m->queue = PQ_NONE;
951 
952 	/* XXX before splx until vm_page_insert is safe */
953 	vm_page_insert(m, object, pindex);
954 
955 	/*
956 	 * Don't wakeup too often - wakeup the pageout daemon when
957 	 * we would be nearly out of memory.
958 	 */
959 	if (((cnt.v_free_count + cnt.v_cache_count) <
960 		(cnt.v_free_reserved + cnt.v_cache_min)) ||
961 			(cnt.v_free_count < cnt.v_pageout_free_min))
962 		pagedaemon_wakeup();
963 
964 	if ((qtype == PQ_CACHE) &&
965 		((page_req == VM_ALLOC_NORMAL) || (page_req == VM_ALLOC_ZERO)) &&
966 		oldobject && (oldobject->type == OBJT_VNODE) &&
967 		((oldobject->flags & OBJ_DEAD) == 0)) {
968 		struct vnode *vp;
969 		vp = (struct vnode *) oldobject->handle;
970 		if (vp && VSHOULDFREE(vp)) {
971 			if ((vp->v_flag & (VFREE|VTBFREE|VDOOMED)) == 0) {
972 				TAILQ_INSERT_TAIL(&vnode_tobefree_list, vp, v_freelist);
973 				vp->v_flag |= VTBFREE;
974 			}
975 		}
976 	}
977 	splx(s);
978 
979 	return (m);
980 }
981 
982 void
983 vm_wait()
984 {
985 	int s;
986 
987 	s = splvm();
988 	if (curproc == pageproc) {
989 		vm_pageout_pages_needed = 1;
990 		tsleep(&vm_pageout_pages_needed, PSWP, "vmwait", 0);
991 	} else {
992 		if (!vm_pages_needed) {
993 			vm_pages_needed++;
994 			wakeup(&vm_pages_needed);
995 		}
996 		tsleep(&cnt.v_free_count, PVM, "vmwait", 0);
997 	}
998 	splx(s);
999 }
1000 
1001 int
1002 vm_page_sleep(vm_page_t m, char *msg, char *busy) {
1003 	int slept = 0;
1004 	if ((busy && *busy) || (m->flags & PG_BUSY)) {
1005 		int s;
1006 		s = splvm();
1007 		if ((busy && *busy) || (m->flags & PG_BUSY)) {
1008 			vm_page_flag_set(m, PG_WANTED);
1009 			tsleep(m, PVM, msg, 0);
1010 			slept = 1;
1011 		}
1012 		splx(s);
1013 	}
1014 	return slept;
1015 }
1016 
1017 /*
1018  *	vm_page_activate:
1019  *
1020  *	Put the specified page on the active list (if appropriate).
1021  *
1022  *	The page queues must be locked.
1023  */
1024 void
1025 vm_page_activate(m)
1026 	register vm_page_t m;
1027 {
1028 	int s;
1029 
1030 	s = splvm();
1031 	if (m->queue != PQ_ACTIVE) {
1032 		if ((m->queue - m->pc) == PQ_CACHE)
1033 			cnt.v_reactivated++;
1034 
1035 		vm_page_unqueue(m);
1036 
1037 		if (m->wire_count == 0) {
1038 			m->queue = PQ_ACTIVE;
1039 			++(*vm_page_queues[PQ_ACTIVE].lcnt);
1040 			TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1041 			if (m->act_count < ACT_INIT)
1042 				m->act_count = ACT_INIT;
1043 			cnt.v_active_count++;
1044 		}
1045 	} else {
1046 		if (m->act_count < ACT_INIT)
1047 			m->act_count = ACT_INIT;
1048 	}
1049 
1050 	splx(s);
1051 }
1052 
1053 /*
1054  * helper routine for vm_page_free and vm_page_free_zero
1055  */
1056 static int
1057 vm_page_freechk_and_unqueue(m)
1058 	vm_page_t m;
1059 {
1060 	vm_object_t oldobject;
1061 
1062 	oldobject = m->object;
1063 
1064 #if !defined(MAX_PERF)
1065 	if (m->busy || ((m->queue - m->pc) == PQ_FREE) ||
1066 		(m->hold_count != 0)) {
1067 		printf(
1068 		"vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n",
1069 		    (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0,
1070 		    m->hold_count);
1071 		if ((m->queue - m->pc) == PQ_FREE)
1072 			panic("vm_page_free: freeing free page");
1073 		else
1074 			panic("vm_page_free: freeing busy page");
1075 	}
1076 #endif
1077 
1078 	vm_page_unqueue_nowakeup(m);
1079 	vm_page_remove(m);
1080 
1081 	if ((m->flags & PG_FICTITIOUS) != 0) {
1082 		return 0;
1083 	}
1084 
1085 	m->valid = 0;
1086 
1087 	if (m->wire_count != 0) {
1088 #if !defined(MAX_PERF)
1089 		if (m->wire_count > 1) {
1090 			panic("vm_page_free: invalid wire count (%d), pindex: 0x%x",
1091 				m->wire_count, m->pindex);
1092 		}
1093 #endif
1094 		printf("vm_page_free: freeing wired page\n");
1095 		m->wire_count = 0;
1096 		if (m->object)
1097 			m->object->wire_count--;
1098 		cnt.v_wire_count--;
1099 	}
1100 
1101 	if (oldobject && (oldobject->type == OBJT_VNODE) &&
1102 		((oldobject->flags & OBJ_DEAD) == 0)) {
1103 		struct vnode *vp;
1104 		vp = (struct vnode *) oldobject->handle;
1105 		if (vp && VSHOULDFREE(vp)) {
1106 			if ((vp->v_flag & (VTBFREE|VDOOMED|VFREE)) == 0) {
1107 				TAILQ_INSERT_TAIL(&vnode_tobefree_list, vp, v_freelist);
1108 				vp->v_flag |= VTBFREE;
1109 			}
1110 		}
1111 	}
1112 
1113 #ifdef __alpha__
1114 	pmap_page_is_free(m);
1115 #endif
1116 
1117 	return 1;
1118 }
1119 
1120 /*
1121  * helper routine for vm_page_free and vm_page_free_zero
1122  */
1123 static __inline void
1124 vm_page_free_wakeup()
1125 {
1126 
1127 /*
1128  * if pageout daemon needs pages, then tell it that there are
1129  * some free.
1130  */
1131 	if (vm_pageout_pages_needed) {
1132 		wakeup(&vm_pageout_pages_needed);
1133 		vm_pageout_pages_needed = 0;
1134 	}
1135 	/*
1136 	 * wakeup processes that are waiting on memory if we hit a
1137 	 * high water mark. And wakeup scheduler process if we have
1138 	 * lots of memory. this process will swapin processes.
1139 	 */
1140 	if (vm_pages_needed &&
1141 		((cnt.v_free_count + cnt.v_cache_count) >= cnt.v_free_min)) {
1142 		wakeup(&cnt.v_free_count);
1143 		vm_pages_needed = 0;
1144 	}
1145 }
1146 
1147 /*
1148  *	vm_page_free:
1149  *
1150  *	Returns the given page to the free list,
1151  *	disassociating it with any VM object.
1152  *
1153  *	Object and page must be locked prior to entry.
1154  */
1155 void
1156 vm_page_free(m)
1157 	register vm_page_t m;
1158 {
1159 	int s;
1160 	struct vpgqueues *pq;
1161 
1162 	s = splvm();
1163 
1164 	cnt.v_tfree++;
1165 
1166 	if (!vm_page_freechk_and_unqueue(m)) {
1167 		splx(s);
1168 		return;
1169 	}
1170 
1171 	m->queue = PQ_FREE + m->pc;
1172 	pq = &vm_page_queues[m->queue];
1173 	++(*pq->lcnt);
1174 	++(*pq->cnt);
1175 	/*
1176 	 * If the pageout process is grabbing the page, it is likely
1177 	 * that the page is NOT in the cache.  It is more likely that
1178 	 * the page will be partially in the cache if it is being
1179 	 * explicitly freed.
1180 	 */
1181 	if (curproc == pageproc) {
1182 		TAILQ_INSERT_TAIL(pq->pl, m, pageq);
1183 	} else {
1184 		TAILQ_INSERT_HEAD(pq->pl, m, pageq);
1185 	}
1186 
1187 	vm_page_free_wakeup();
1188 	splx(s);
1189 }
1190 
1191 void
1192 vm_page_free_zero(m)
1193 	register vm_page_t m;
1194 {
1195 	int s;
1196 	struct vpgqueues *pq;
1197 
1198 	s = splvm();
1199 
1200 	cnt.v_tfree++;
1201 
1202 	if (!vm_page_freechk_and_unqueue(m)) {
1203 		splx(s);
1204 		return;
1205 	}
1206 
1207 	m->queue = PQ_ZERO + m->pc;
1208 	pq = &vm_page_queues[m->queue];
1209 	++(*pq->lcnt);
1210 	++(*pq->cnt);
1211 
1212 	TAILQ_INSERT_HEAD(pq->pl, m, pageq);
1213 	++vm_page_zero_count;
1214 	vm_page_free_wakeup();
1215 	splx(s);
1216 }
1217 
1218 /*
1219  *	vm_page_wire:
1220  *
1221  *	Mark this page as wired down by yet
1222  *	another map, removing it from paging queues
1223  *	as necessary.
1224  *
1225  *	The page queues must be locked.
1226  */
1227 void
1228 vm_page_wire(m)
1229 	register vm_page_t m;
1230 {
1231 	int s;
1232 
1233 	if (m->wire_count == 0) {
1234 		s = splvm();
1235 		vm_page_unqueue(m);
1236 		splx(s);
1237 		cnt.v_wire_count++;
1238 		if (m->object)
1239 			m->object->wire_count++;
1240 	}
1241 	(*vm_page_queues[PQ_NONE].lcnt)++;
1242 	m->wire_count++;
1243 	vm_page_flag_set(m, PG_MAPPED);
1244 }
1245 
1246 /*
1247  *	vm_page_unwire:
1248  *
1249  *	Release one wiring of this page, potentially
1250  *	enabling it to be paged again.
1251  *
1252  *	The page queues must be locked.
1253  */
1254 void
1255 vm_page_unwire(m, activate)
1256 	register vm_page_t m;
1257 	int activate;
1258 {
1259 	int s;
1260 
1261 	s = splvm();
1262 
1263 	if (m->wire_count > 0) {
1264 		m->wire_count--;
1265 		if (m->wire_count == 0) {
1266 			if (m->object)
1267 				m->object->wire_count--;
1268 			cnt.v_wire_count--;
1269 			if (activate) {
1270 				TAILQ_INSERT_TAIL(&vm_page_queue_active, m, pageq);
1271 				m->queue = PQ_ACTIVE;
1272 				(*vm_page_queues[PQ_ACTIVE].lcnt)++;
1273 				cnt.v_active_count++;
1274 			} else {
1275 				TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
1276 				m->queue = PQ_INACTIVE;
1277 				(*vm_page_queues[PQ_INACTIVE].lcnt)++;
1278 				cnt.v_inactive_count++;
1279 			}
1280 		}
1281 	} else {
1282 #if !defined(MAX_PERF)
1283 		panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count);
1284 #endif
1285 	}
1286 	splx(s);
1287 }
1288 
1289 
1290 /*
1291  * Move the specified page to the inactive queue.
1292  */
1293 void
1294 vm_page_deactivate(m)
1295 	register vm_page_t m;
1296 {
1297 	int s;
1298 
1299 	/*
1300 	 * Ignore if already inactive.
1301 	 */
1302 	if (m->queue == PQ_INACTIVE)
1303 		return;
1304 
1305 	s = splvm();
1306 	if (m->wire_count == 0) {
1307 		if ((m->queue - m->pc) == PQ_CACHE)
1308 			cnt.v_reactivated++;
1309 		vm_page_unqueue(m);
1310 		TAILQ_INSERT_TAIL(&vm_page_queue_inactive, m, pageq);
1311 		m->queue = PQ_INACTIVE;
1312 		++(*vm_page_queues[PQ_INACTIVE].lcnt);
1313 		cnt.v_inactive_count++;
1314 	}
1315 	splx(s);
1316 }
1317 
1318 /*
1319  * vm_page_cache
1320  *
1321  * Put the specified page onto the page cache queue (if appropriate).
1322  */
1323 void
1324 vm_page_cache(m)
1325 	register vm_page_t m;
1326 {
1327 	int s;
1328 
1329 #if !defined(MAX_PERF)
1330 	if ((m->flags & PG_BUSY) || m->busy || m->wire_count) {
1331 		printf("vm_page_cache: attempting to cache busy page\n");
1332 		return;
1333 	}
1334 #endif
1335 	if ((m->queue - m->pc) == PQ_CACHE)
1336 		return;
1337 
1338 	vm_page_protect(m, VM_PROT_NONE);
1339 #if !defined(MAX_PERF)
1340 	if (m->dirty != 0) {
1341 		panic("vm_page_cache: caching a dirty page, pindex: %d", m->pindex);
1342 	}
1343 #endif
1344 	s = splvm();
1345 	vm_page_unqueue_nowakeup(m);
1346 	m->queue = PQ_CACHE + m->pc;
1347 	(*vm_page_queues[m->queue].lcnt)++;
1348 	TAILQ_INSERT_TAIL(vm_page_queues[m->queue].pl, m, pageq);
1349 	cnt.v_cache_count++;
1350 	m->object->cache_count++;
1351 	vm_page_free_wakeup();
1352 	splx(s);
1353 }
1354 
1355 /*
1356  * Grab a page, waiting until we are waken up due to the page
1357  * changing state.  We keep on waiting, if the page continues
1358  * to be in the object.  If the page doesn't exist, allocate it.
1359  */
1360 vm_page_t
1361 vm_page_grab(object, pindex, allocflags)
1362 	vm_object_t object;
1363 	vm_pindex_t pindex;
1364 	int allocflags;
1365 {
1366 
1367 	vm_page_t m;
1368 	int s, generation;
1369 
1370 retrylookup:
1371 	if ((m = vm_page_lookup(object, pindex)) != NULL) {
1372 		if (m->busy || (m->flags & PG_BUSY)) {
1373 			generation = object->generation;
1374 
1375 			s = splvm();
1376 			while ((object->generation == generation) &&
1377 					(m->busy || (m->flags & PG_BUSY))) {
1378 				vm_page_flag_set(m, PG_WANTED | PG_REFERENCED);
1379 				tsleep(m, PVM, "pgrbwt", 0);
1380 				if ((allocflags & VM_ALLOC_RETRY) == 0) {
1381 					splx(s);
1382 					return NULL;
1383 				}
1384 			}
1385 			splx(s);
1386 			goto retrylookup;
1387 		} else {
1388 			vm_page_busy(m);
1389 			return m;
1390 		}
1391 	}
1392 
1393 	m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY);
1394 	if (m == NULL) {
1395 		VM_WAIT;
1396 		if ((allocflags & VM_ALLOC_RETRY) == 0)
1397 			return NULL;
1398 		goto retrylookup;
1399 	}
1400 
1401 	return m;
1402 }
1403 
1404 /*
1405  * mapping function for valid bits or for dirty bits in
1406  * a page
1407  */
1408 __inline int
1409 vm_page_bits(int base, int size)
1410 {
1411 	u_short chunk;
1412 
1413 	if ((base == 0) && (size >= PAGE_SIZE))
1414 		return VM_PAGE_BITS_ALL;
1415 
1416 	size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
1417 	base &= PAGE_MASK;
1418 	if (size > PAGE_SIZE - base) {
1419 		size = PAGE_SIZE - base;
1420 	}
1421 
1422 	base = base / DEV_BSIZE;
1423 	chunk = vm_page_dev_bsize_chunks[size / DEV_BSIZE];
1424 	return (chunk << base) & VM_PAGE_BITS_ALL;
1425 }
1426 
1427 /*
1428  * set a page valid and clean
1429  */
1430 void
1431 vm_page_set_validclean(m, base, size)
1432 	vm_page_t m;
1433 	int base;
1434 	int size;
1435 {
1436 	int pagebits = vm_page_bits(base, size);
1437 	m->valid |= pagebits;
1438 	m->dirty &= ~pagebits;
1439 	if( base == 0 && size == PAGE_SIZE)
1440 		pmap_clear_modify(VM_PAGE_TO_PHYS(m));
1441 }
1442 
1443 /*
1444  * set a page (partially) invalid
1445  */
1446 void
1447 vm_page_set_invalid(m, base, size)
1448 	vm_page_t m;
1449 	int base;
1450 	int size;
1451 {
1452 	int bits;
1453 
1454 	m->valid &= ~(bits = vm_page_bits(base, size));
1455 	if (m->valid == 0)
1456 		m->dirty &= ~bits;
1457 	m->object->generation++;
1458 }
1459 
1460 /*
1461  * is (partial) page valid?
1462  */
1463 int
1464 vm_page_is_valid(m, base, size)
1465 	vm_page_t m;
1466 	int base;
1467 	int size;
1468 {
1469 	int bits = vm_page_bits(base, size);
1470 
1471 	if (m->valid && ((m->valid & bits) == bits))
1472 		return 1;
1473 	else
1474 		return 0;
1475 }
1476 
1477 void
1478 vm_page_test_dirty(m)
1479 	vm_page_t m;
1480 {
1481 	if ((m->dirty != VM_PAGE_BITS_ALL) &&
1482 	    pmap_is_modified(VM_PAGE_TO_PHYS(m))) {
1483 		m->dirty = VM_PAGE_BITS_ALL;
1484 	}
1485 }
1486 
1487 /*
1488  * This interface is for merging with malloc() someday.
1489  * Even if we never implement compaction so that contiguous allocation
1490  * works after initialization time, malloc()'s data structures are good
1491  * for statistics and for allocations of less than a page.
1492  */
1493 void *
1494 contigmalloc1(size, type, flags, low, high, alignment, boundary, map)
1495 	unsigned long size;	/* should be size_t here and for malloc() */
1496 	struct malloc_type *type;
1497 	int flags;
1498 	unsigned long low;
1499 	unsigned long high;
1500 	unsigned long alignment;
1501 	unsigned long boundary;
1502 	vm_map_t map;
1503 {
1504 	int i, s, start;
1505 	vm_offset_t addr, phys, tmp_addr;
1506 	int pass;
1507 	vm_page_t pga = vm_page_array;
1508 
1509 	size = round_page(size);
1510 #if !defined(MAX_PERF)
1511 	if (size == 0)
1512 		panic("contigmalloc1: size must not be 0");
1513 	if ((alignment & (alignment - 1)) != 0)
1514 		panic("contigmalloc1: alignment must be a power of 2");
1515 	if ((boundary & (boundary - 1)) != 0)
1516 		panic("contigmalloc1: boundary must be a power of 2");
1517 #endif
1518 
1519 	start = 0;
1520 	for (pass = 0; pass <= 1; pass++) {
1521 		s = splvm();
1522 again:
1523 		/*
1524 		 * Find first page in array that is free, within range, aligned, and
1525 		 * such that the boundary won't be crossed.
1526 		 */
1527 		for (i = start; i < cnt.v_page_count; i++) {
1528 			int pqtype;
1529 			phys = VM_PAGE_TO_PHYS(&pga[i]);
1530 			pqtype = pga[i].queue - pga[i].pc;
1531 			if (((pqtype == PQ_ZERO) || (pqtype == PQ_FREE) || (pqtype == PQ_CACHE)) &&
1532 			    (phys >= low) && (phys < high) &&
1533 			    ((phys & (alignment - 1)) == 0) &&
1534 			    (((phys ^ (phys + size - 1)) & ~(boundary - 1)) == 0))
1535 				break;
1536 		}
1537 
1538 		/*
1539 		 * If the above failed or we will exceed the upper bound, fail.
1540 		 */
1541 		if ((i == cnt.v_page_count) ||
1542 			((VM_PAGE_TO_PHYS(&pga[i]) + size) > high)) {
1543 			vm_page_t m, next;
1544 
1545 again1:
1546 			for (m = TAILQ_FIRST(&vm_page_queue_inactive);
1547 				m != NULL;
1548 				m = next) {
1549 
1550 				if (m->queue != PQ_INACTIVE) {
1551 					break;
1552 				}
1553 
1554 				next = TAILQ_NEXT(m, pageq);
1555 				if (vm_page_sleep(m, "vpctw0", &m->busy))
1556 					goto again1;
1557 				vm_page_test_dirty(m);
1558 				if (m->dirty) {
1559 					if (m->object->type == OBJT_VNODE) {
1560 						vn_lock(m->object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
1561 						vm_object_page_clean(m->object, 0, 0, OBJPC_SYNC);
1562 						VOP_UNLOCK(m->object->handle, 0, curproc);
1563 						goto again1;
1564 					} else if (m->object->type == OBJT_SWAP ||
1565 								m->object->type == OBJT_DEFAULT) {
1566 						vm_pageout_flush(&m, 1, 0);
1567 						goto again1;
1568 					}
1569 				}
1570 				if ((m->dirty == 0) && (m->busy == 0) && (m->hold_count == 0))
1571 					vm_page_cache(m);
1572 			}
1573 
1574 			for (m = TAILQ_FIRST(&vm_page_queue_active);
1575 				m != NULL;
1576 				m = next) {
1577 
1578 				if (m->queue != PQ_ACTIVE) {
1579 					break;
1580 				}
1581 
1582 				next = TAILQ_NEXT(m, pageq);
1583 				if (vm_page_sleep(m, "vpctw1", &m->busy))
1584 					goto again1;
1585 				vm_page_test_dirty(m);
1586 				if (m->dirty) {
1587 					if (m->object->type == OBJT_VNODE) {
1588 						vn_lock(m->object->handle, LK_EXCLUSIVE | LK_RETRY, curproc);
1589 						vm_object_page_clean(m->object, 0, 0, OBJPC_SYNC);
1590 						VOP_UNLOCK(m->object->handle, 0, curproc);
1591 						goto again1;
1592 					} else if (m->object->type == OBJT_SWAP ||
1593 								m->object->type == OBJT_DEFAULT) {
1594 						vm_pageout_flush(&m, 1, 0);
1595 						goto again1;
1596 					}
1597 				}
1598 				if ((m->dirty == 0) && (m->busy == 0) && (m->hold_count == 0))
1599 					vm_page_cache(m);
1600 			}
1601 
1602 			splx(s);
1603 			continue;
1604 		}
1605 		start = i;
1606 
1607 		/*
1608 		 * Check successive pages for contiguous and free.
1609 		 */
1610 		for (i = start + 1; i < (start + size / PAGE_SIZE); i++) {
1611 			int pqtype;
1612 			pqtype = pga[i].queue - pga[i].pc;
1613 			if ((VM_PAGE_TO_PHYS(&pga[i]) !=
1614 			    (VM_PAGE_TO_PHYS(&pga[i - 1]) + PAGE_SIZE)) ||
1615 			    ((pqtype != PQ_ZERO) && (pqtype != PQ_FREE) && (pqtype != PQ_CACHE))) {
1616 				start++;
1617 				goto again;
1618 			}
1619 		}
1620 
1621 		for (i = start; i < (start + size / PAGE_SIZE); i++) {
1622 			int pqtype;
1623 			vm_page_t m = &pga[i];
1624 
1625 			pqtype = m->queue - m->pc;
1626 			if (pqtype == PQ_CACHE) {
1627 				vm_page_busy(m);
1628 				vm_page_free(m);
1629 			}
1630 
1631 			TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
1632 			(*vm_page_queues[m->queue].lcnt)--;
1633 			cnt.v_free_count--;
1634 			m->valid = VM_PAGE_BITS_ALL;
1635 			m->flags = 0;
1636 			m->dirty = 0;
1637 			m->wire_count = 0;
1638 			m->busy = 0;
1639 			m->queue = PQ_NONE;
1640 			m->object = NULL;
1641 			vm_page_wire(m);
1642 		}
1643 
1644 		/*
1645 		 * We've found a contiguous chunk that meets are requirements.
1646 		 * Allocate kernel VM, unfree and assign the physical pages to it and
1647 		 * return kernel VM pointer.
1648 		 */
1649 		tmp_addr = addr = kmem_alloc_pageable(map, size);
1650 		if (addr == 0) {
1651 			/*
1652 			 * XXX We almost never run out of kernel virtual
1653 			 * space, so we don't make the allocated memory
1654 			 * above available.
1655 			 */
1656 			splx(s);
1657 			return (NULL);
1658 		}
1659 
1660 		for (i = start; i < (start + size / PAGE_SIZE); i++) {
1661 			vm_page_t m = &pga[i];
1662 			vm_page_insert(m, kernel_object,
1663 				OFF_TO_IDX(tmp_addr - VM_MIN_KERNEL_ADDRESS));
1664 			pmap_kenter(tmp_addr, VM_PAGE_TO_PHYS(m));
1665 			tmp_addr += PAGE_SIZE;
1666 		}
1667 
1668 		splx(s);
1669 		return ((void *)addr);
1670 	}
1671 	return NULL;
1672 }
1673 
1674 void *
1675 contigmalloc(size, type, flags, low, high, alignment, boundary)
1676 	unsigned long size;	/* should be size_t here and for malloc() */
1677 	struct malloc_type *type;
1678 	int flags;
1679 	unsigned long low;
1680 	unsigned long high;
1681 	unsigned long alignment;
1682 	unsigned long boundary;
1683 {
1684 	return contigmalloc1(size, type, flags, low, high, alignment, boundary,
1685 			     kernel_map);
1686 }
1687 
1688 vm_offset_t
1689 vm_page_alloc_contig(size, low, high, alignment)
1690 	vm_offset_t size;
1691 	vm_offset_t low;
1692 	vm_offset_t high;
1693 	vm_offset_t alignment;
1694 {
1695 	return ((vm_offset_t)contigmalloc1(size, M_DEVBUF, M_NOWAIT, low, high,
1696 					  alignment, 0ul, kernel_map));
1697 }
1698 
1699 #include "opt_ddb.h"
1700 #ifdef DDB
1701 #include <sys/kernel.h>
1702 
1703 #include <ddb/ddb.h>
1704 
1705 DB_SHOW_COMMAND(page, vm_page_print_page_info)
1706 {
1707 	db_printf("cnt.v_free_count: %d\n", cnt.v_free_count);
1708 	db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count);
1709 	db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count);
1710 	db_printf("cnt.v_active_count: %d\n", cnt.v_active_count);
1711 	db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count);
1712 	db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved);
1713 	db_printf("cnt.v_free_min: %d\n", cnt.v_free_min);
1714 	db_printf("cnt.v_free_target: %d\n", cnt.v_free_target);
1715 	db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min);
1716 	db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target);
1717 }
1718 
1719 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info)
1720 {
1721 	int i;
1722 	db_printf("PQ_FREE:");
1723 	for(i=0;i<PQ_L2_SIZE;i++) {
1724 		db_printf(" %d", *vm_page_queues[PQ_FREE + i].lcnt);
1725 	}
1726 	db_printf("\n");
1727 
1728 	db_printf("PQ_CACHE:");
1729 	for(i=0;i<PQ_L2_SIZE;i++) {
1730 		db_printf(" %d", *vm_page_queues[PQ_CACHE + i].lcnt);
1731 	}
1732 	db_printf("\n");
1733 
1734 	db_printf("PQ_ZERO:");
1735 	for(i=0;i<PQ_L2_SIZE;i++) {
1736 		db_printf(" %d", *vm_page_queues[PQ_ZERO + i].lcnt);
1737 	}
1738 	db_printf("\n");
1739 
1740 	db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n",
1741 		*vm_page_queues[PQ_ACTIVE].lcnt,
1742 		*vm_page_queues[PQ_INACTIVE].lcnt);
1743 }
1744 #endif /* DDB */
1745