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