pmap.c revision 18275
1/*
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
5 * All rights reserved.
6 * Copyright (c) 1994 David Greenman
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department and William Jolitz of UUNET Technologies Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgement:
23 *	This product includes software developed by the University of
24 *	California, Berkeley and 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 *	from:	@(#)pmap.c	7.7 (Berkeley)	5/12/91
42 *	$Id: pmap.c,v 1.118 1996/09/12 04:54:55 dyson Exp $
43 */
44
45/*
46 *	Manages physical address maps.
47 *
48 *	In addition to hardware address maps, this
49 *	module is called upon to provide software-use-only
50 *	maps which may or may not be stored in the same
51 *	form as hardware maps.  These pseudo-maps are
52 *	used to store intermediate results from copy
53 *	operations to and from address spaces.
54 *
55 *	Since the information managed by this module is
56 *	also stored by the logical address mapping module,
57 *	this module may throw away valid virtual-to-physical
58 *	mappings at almost any time.  However, invalidations
59 *	of virtual-to-physical mappings must be done as
60 *	requested.
61 *
62 *	In order to cope with hardware architectures which
63 *	make virtual-to-physical map invalidates expensive,
64 *	this module may delay invalidate or reduced protection
65 *	operations until such time as they are actually
66 *	necessary.  This module is given full information as
67 *	to which processors are currently using which maps,
68 *	and to when physical maps must be made correct.
69 */
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/proc.h>
74#include <sys/malloc.h>
75#include <sys/msgbuf.h>
76#include <sys/queue.h>
77#include <sys/vmmeter.h>
78#include <sys/mman.h>
79
80#include <vm/vm.h>
81#include <vm/vm_param.h>
82#include <vm/vm_prot.h>
83#include <vm/lock.h>
84#include <vm/vm_kern.h>
85#include <vm/vm_page.h>
86#include <vm/vm_map.h>
87#include <vm/vm_object.h>
88#include <vm/vm_extern.h>
89#include <vm/vm_pageout.h>
90#include <vm/vm_pager.h>
91
92#include <machine/pcb.h>
93#include <machine/cputypes.h>
94#include <machine/md_var.h>
95
96#define PMAP_KEEP_PDIRS
97
98#if defined(DIAGNOSTIC)
99#define PMAP_DIAGNOSTIC
100#endif
101
102#if !defined(PMAP_DIAGNOSTIC)
103#define PMAP_INLINE __inline
104#else
105#define PMAP_INLINE
106#endif
107
108#define PTPHINT
109
110static void	init_pv_entries __P((int));
111
112/*
113 * Get PDEs and PTEs for user/kernel address space
114 */
115#define	pmap_pde(m, v)	(&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
116#define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
117
118#define pmap_pde_v(pte)		((*(int *)pte & PG_V) != 0)
119#define pmap_pte_w(pte)		((*(int *)pte & PG_W) != 0)
120#define pmap_pte_m(pte)		((*(int *)pte & PG_M) != 0)
121#define pmap_pte_u(pte)		((*(int *)pte & PG_A) != 0)
122#define pmap_pte_v(pte)		((*(int *)pte & PG_V) != 0)
123
124#define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W))
125#define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
126
127/*
128 * Given a map and a machine independent protection code,
129 * convert to a vax protection code.
130 */
131#define pte_prot(m, p)	(protection_codes[p])
132static int protection_codes[8];
133
134static struct pmap kernel_pmap_store;
135pmap_t kernel_pmap;
136
137vm_offset_t avail_start;	/* PA of first available physical page */
138vm_offset_t avail_end;		/* PA of last available physical page */
139vm_offset_t virtual_avail;	/* VA of first avail page (after kernel bss) */
140vm_offset_t virtual_end;	/* VA of last avail page (end of kernel AS) */
141static boolean_t pmap_initialized = FALSE;	/* Has pmap_init completed? */
142static vm_offset_t vm_first_phys;
143
144static int nkpt;
145static vm_page_t nkpg;
146vm_offset_t kernel_vm_end;
147
148extern vm_offset_t clean_sva, clean_eva;
149extern int cpu_class;
150
151#define PV_FREELIST_MIN ((PAGE_SIZE / sizeof (struct pv_entry)) / 2)
152
153/*
154 * Data for the pv entry allocation mechanism
155 */
156static int pv_freelistcnt;
157TAILQ_HEAD (,pv_entry) pv_freelist;
158static vm_offset_t pvva;
159static int npvvapg;
160
161/*
162 * All those kernel PT submaps that BSD is so fond of
163 */
164pt_entry_t *CMAP1;
165static pt_entry_t *CMAP2, *ptmmap;
166caddr_t CADDR1, ptvmmap;
167static caddr_t CADDR2;
168static pt_entry_t *msgbufmap;
169struct msgbuf *msgbufp;
170
171pt_entry_t *PMAP1;
172unsigned *PADDR1;
173
174static void	free_pv_entry __P((pv_entry_t pv));
175static unsigned * get_ptbase __P((pmap_t pmap));
176static pv_entry_t get_pv_entry __P((void));
177static void	i386_protection_init __P((void));
178static void	pmap_alloc_pv_entry __P((void));
179static void	pmap_changebit __P((vm_offset_t pa, int bit, boolean_t setem));
180
181static int	pmap_is_managed __P((vm_offset_t pa));
182static void	pmap_remove_all __P((vm_offset_t pa));
183static vm_page_t pmap_enter_quick __P((pmap_t pmap, vm_offset_t va,
184				      vm_offset_t pa, vm_page_t mpte));
185static int pmap_remove_pte __P((struct pmap *pmap, unsigned *ptq,
186					vm_offset_t sva));
187static void pmap_remove_page __P((struct pmap *pmap, vm_offset_t va));
188static int pmap_remove_entry __P((struct pmap *pmap, pv_table_t *pv,
189					vm_offset_t va));
190static boolean_t pmap_testbit __P((vm_offset_t pa, int bit));
191static void pmap_insert_entry __P((pmap_t pmap, vm_offset_t va,
192		vm_page_t mpte, vm_offset_t pa));
193
194static vm_page_t pmap_allocpte __P((pmap_t pmap, vm_offset_t va));
195
196static int pmap_release_free_page __P((pmap_t pmap, vm_page_t p));
197static vm_page_t _pmap_allocpte __P((pmap_t pmap, unsigned ptepindex));
198static unsigned * pmap_pte_quick __P((pmap_t pmap, vm_offset_t va));
199static vm_page_t pmap_page_alloc __P((vm_object_t object, vm_pindex_t pindex));
200static PMAP_INLINE void pmap_lock __P((pmap_t pmap));
201static PMAP_INLINE void pmap_unlock __P((pmap_t pmap));
202static void pmap_lock2 __P((pmap_t pmap1, pmap_t pmap2));
203
204#define PDSTACKMAX 6
205static vm_offset_t pdstack[PDSTACKMAX];
206static int pdstackptr;
207
208/*
209 *	Bootstrap the system enough to run with virtual memory.
210 *
211 *	On the i386 this is called after mapping has already been enabled
212 *	and just syncs the pmap module with what has already been done.
213 *	[We can't call it easily with mapping off since the kernel is not
214 *	mapped with PA == VA, hence we would have to relocate every address
215 *	from the linked base (virtual) address "KERNBASE" to the actual
216 *	(physical) address starting relative to 0]
217 */
218void
219pmap_bootstrap(firstaddr, loadaddr)
220	vm_offset_t firstaddr;
221	vm_offset_t loadaddr;
222{
223	vm_offset_t va;
224	pt_entry_t *pte;
225
226	avail_start = firstaddr;
227
228	/*
229	 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
230	 * large. It should instead be correctly calculated in locore.s and
231	 * not based on 'first' (which is a physical address, not a virtual
232	 * address, for the start of unused physical memory). The kernel
233	 * page tables are NOT double mapped and thus should not be included
234	 * in this calculation.
235	 */
236	virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
237	virtual_end = VM_MAX_KERNEL_ADDRESS;
238
239	/*
240	 * Initialize protection array.
241	 */
242	i386_protection_init();
243
244	/*
245	 * The kernel's pmap is statically allocated so we don't have to use
246	 * pmap_create, which is unlikely to work correctly at this part of
247	 * the boot sequence (XXX and which no longer exists).
248	 */
249	kernel_pmap = &kernel_pmap_store;
250
251	kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + IdlePTD);
252
253	kernel_pmap->pm_count = 1;
254	TAILQ_INIT(&kernel_pmap->pm_pvlist);
255	nkpt = NKPT;
256
257	/*
258	 * Reserve some special page table entries/VA space for temporary
259	 * mapping of pages.
260	 */
261#define	SYSMAP(c, p, v, n)	\
262	v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
263
264	va = virtual_avail;
265	pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
266
267	/*
268	 * CMAP1/CMAP2 are used for zeroing and copying pages.
269	 */
270	SYSMAP(caddr_t, CMAP1, CADDR1, 1)
271	SYSMAP(caddr_t, CMAP2, CADDR2, 1)
272
273	/*
274	 * ptmmap is used for reading arbitrary physical pages via /dev/mem.
275	 */
276	SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
277
278	/*
279	 * msgbufmap is used to map the system message buffer.
280	 */
281	SYSMAP(struct msgbuf *, msgbufmap, msgbufp, 1)
282
283	/*
284	 * ptemap is used for pmap_pte_quick
285	 */
286	SYSMAP(unsigned *, PMAP1, PADDR1, 1);
287
288	virtual_avail = va;
289
290	*(int *) CMAP1 = *(int *) CMAP2 = *(int *) PTD = 0;
291	pmap_update();
292
293}
294
295/*
296 *	Initialize the pmap module.
297 *	Called by vm_init, to initialize any structures that the pmap
298 *	system needs to map virtual memory.
299 *	pmap_init has been enhanced to support in a fairly consistant
300 *	way, discontiguous physical memory.
301 */
302void
303pmap_init(phys_start, phys_end)
304	vm_offset_t phys_start, phys_end;
305{
306	vm_offset_t addr;
307	vm_size_t npg, s;
308	int i;
309
310	/*
311	 * calculate the number of pv_entries needed
312	 */
313	vm_first_phys = phys_avail[0];
314	for (i = 0; phys_avail[i + 1]; i += 2);
315	npg = (phys_avail[(i - 2) + 1] - vm_first_phys) / PAGE_SIZE;
316
317	/*
318	 * Allocate memory for random pmap data structures.  Includes the
319	 * pv_head_table.
320	 */
321	s = (vm_size_t) (sizeof(pv_table_t) * npg);
322	s = round_page(s);
323
324	addr = (vm_offset_t) kmem_alloc(kernel_map, s);
325	pv_table = (pv_table_t *) addr;
326	for(i=0;i<npg;i++) {
327		vm_offset_t pa;
328		TAILQ_INIT(&pv_table[i].pv_list);
329		pv_table[i].pv_list_count = 0;
330		pa = vm_first_phys + i * PAGE_SIZE;
331		pv_table[i].pv_vm_page = PHYS_TO_VM_PAGE(pa);
332	}
333	TAILQ_INIT(&pv_freelist);
334
335	/*
336	 * init the pv free list
337	 */
338	init_pv_entries(npg);
339	/*
340	 * Now it is safe to enable pv_table recording.
341	 */
342	pmap_initialized = TRUE;
343}
344
345/*
346 *	Used to map a range of physical addresses into kernel
347 *	virtual address space.
348 *
349 *	For now, VM is already on, we only need to map the
350 *	specified memory.
351 */
352vm_offset_t
353pmap_map(virt, start, end, prot)
354	vm_offset_t virt;
355	vm_offset_t start;
356	vm_offset_t end;
357	int prot;
358{
359	while (start < end) {
360		pmap_enter(kernel_pmap, virt, start, prot, FALSE);
361		virt += PAGE_SIZE;
362		start += PAGE_SIZE;
363	}
364	return (virt);
365}
366
367
368/***************************************************
369 * Low level helper routines.....
370 ***************************************************/
371
372#if defined(PMAP_DIAGNOSTIC)
373
374/*
375 * This code checks for non-writeable/modified pages.
376 * This should be an invalid condition.
377 */
378static int
379pmap_nw_modified(pt_entry_t ptea) {
380	int pte;
381
382	pte = (int) ptea;
383
384	if ((pte & (PG_M|PG_RW)) == PG_M)
385		return 1;
386	else
387		return 0;
388}
389#endif
390
391
392/*
393 * this routine defines the region(s) of memory that should
394 * not be tested for the modified bit.
395 */
396static PMAP_INLINE int
397pmap_track_modified( vm_offset_t va) {
398	if ((va < clean_sva) || (va >= clean_eva))
399		return 1;
400	else
401		return 0;
402}
403
404/*
405 * The below are finer grained pmap_update routines.  These eliminate
406 * the gratuitious tlb flushes on non-i386 architectures.
407 */
408static PMAP_INLINE void
409pmap_update_1pg( vm_offset_t va) {
410#if defined(I386_CPU)
411	if (cpu_class == CPUCLASS_386)
412		pmap_update();
413	else
414#endif
415		__asm __volatile(".byte 0xf,0x1,0x38": :"a" (va));
416}
417
418static PMAP_INLINE void
419pmap_update_2pg( vm_offset_t va1, vm_offset_t va2) {
420#if defined(I386_CPU)
421	if (cpu_class == CPUCLASS_386) {
422		pmap_update();
423	} else
424#endif
425	{
426		__asm __volatile(".byte 0xf,0x1,0x38": :"a" (va1));
427		__asm __volatile(".byte 0xf,0x1,0x38": :"a" (va2));
428	}
429}
430
431
432static PMAP_INLINE void
433pmap_lock(pmap)
434pmap_t pmap;
435{
436	int s;
437	if (pmap == kernel_pmap)
438		return;
439	s = splhigh();
440	while (pmap->pm_flags & PM_FLAG_LOCKED) {
441		pmap->pm_flags |= PM_FLAG_WANTED;
442		tsleep(pmap, PVM - 1, "pmaplk", 0);
443	}
444	splx(s);
445}
446
447static PMAP_INLINE void
448pmap_unlock(pmap)
449pmap_t pmap;
450{
451	int s;
452	if (pmap == kernel_pmap)
453		return;
454	s = splhigh();
455	pmap->pm_flags &= ~PM_FLAG_LOCKED;
456	if (pmap->pm_flags & PM_FLAG_WANTED) {
457		pmap->pm_flags &= ~PM_FLAG_WANTED;
458		wakeup(pmap);
459	}
460}
461
462static void
463pmap_lock2(pmap1, pmap2)
464pmap_t pmap1, pmap2;
465{
466	int s;
467	if (pmap1 == kernel_pmap || pmap2 == kernel_pmap)
468		return;
469	s = splhigh();
470	while ((pmap1->pm_flags | pmap2->pm_flags) & PM_FLAG_LOCKED) {
471		while (pmap1->pm_flags & PM_FLAG_LOCKED) {
472			pmap1->pm_flags |= PM_FLAG_WANTED;
473			tsleep(pmap1, PVM - 1, "pmapl1", 0);
474		}
475		while (pmap2->pm_flags & PM_FLAG_LOCKED) {
476			pmap2->pm_flags |= PM_FLAG_WANTED;
477			tsleep(pmap2, PVM - 1, "pmapl2", 0);
478		}
479	}
480	splx(s);
481}
482
483static unsigned *
484get_ptbase(pmap)
485	pmap_t pmap;
486{
487	unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
488
489	/* are we current address space or kernel? */
490	if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
491		return (unsigned *) PTmap;
492	}
493	/* otherwise, we are alternate address space */
494	if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
495		APTDpde = (pd_entry_t) (frame | PG_RW | PG_V);
496		pmap_update();
497	}
498	return (unsigned *) APTmap;
499}
500
501/*
502 *	Routine:	pmap_pte
503 *	Function:
504 *		Extract the page table entry associated
505 *		with the given map/virtual_address pair.
506 */
507
508PMAP_INLINE unsigned *
509pmap_pte(pmap, va)
510	register pmap_t pmap;
511	vm_offset_t va;
512{
513	if (pmap && *pmap_pde(pmap, va)) {
514		return get_ptbase(pmap) + i386_btop(va);
515	}
516	return (0);
517}
518
519/*
520 * Super fast pmap_pte routine best used when scanning
521 * the pv lists.  This eliminates many coarse-grained
522 * pmap_update calls.  Note that many of the pv list
523 * scans are across different pmaps.  It is very wasteful
524 * to do an entire pmap_update for checking a single mapping.
525 */
526
527unsigned *
528pmap_pte_quick(pmap, va)
529	register pmap_t pmap;
530	vm_offset_t va;
531{
532	unsigned pde, newpf;
533	if (pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) {
534		unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
535		unsigned index = i386_btop(va);
536		/* are we current address space or kernel? */
537		if ((pmap == kernel_pmap) ||
538			(frame == (((unsigned) PTDpde) & PG_FRAME))) {
539			return (unsigned *) PTmap + index;
540		}
541		newpf = pde & PG_FRAME;
542		if ( ((* (unsigned *) PMAP1) & PG_FRAME) != newpf) {
543			* (unsigned *) PMAP1 = newpf | PG_RW | PG_V;
544			pmap_update_1pg((vm_offset_t) PADDR1);
545		}
546		return PADDR1 + ((unsigned) index & (NPTEPG - 1));
547	}
548	return (0);
549}
550
551/*
552 *	Routine:	pmap_extract
553 *	Function:
554 *		Extract the physical page address associated
555 *		with the given map/virtual_address pair.
556 */
557vm_offset_t
558pmap_extract(pmap, va)
559	register pmap_t pmap;
560	vm_offset_t va;
561{
562	vm_offset_t rtval;
563	pmap_lock(pmap);
564	if (pmap && *pmap_pde(pmap, va)) {
565		unsigned *pte;
566		pte = get_ptbase(pmap) + i386_btop(va);
567		rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
568		pmap_unlock(pmap);
569		return rtval;
570	}
571	pmap_unlock(pmap);
572	return 0;
573
574}
575
576/*
577 * determine if a page is managed (memory vs. device)
578 */
579static PMAP_INLINE int
580pmap_is_managed(pa)
581	vm_offset_t pa;
582{
583	int i;
584
585	if (!pmap_initialized)
586		return 0;
587
588	for (i = 0; phys_avail[i + 1]; i += 2) {
589		if (pa < phys_avail[i + 1] && pa >= phys_avail[i])
590			return 1;
591	}
592	return 0;
593}
594
595
596/***************************************************
597 * Low level mapping routines.....
598 ***************************************************/
599
600/*
601 * Add a list of wired pages to the kva
602 * this routine is only used for temporary
603 * kernel mappings that do not need to have
604 * page modification or references recorded.
605 * Note that old mappings are simply written
606 * over.  The page *must* be wired.
607 */
608void
609pmap_qenter(va, m, count)
610	vm_offset_t va;
611	vm_page_t *m;
612	int count;
613{
614	int i;
615	register unsigned *pte;
616
617	for (i = 0; i < count; i++) {
618		vm_offset_t tva = va + i * PAGE_SIZE;
619		unsigned npte = VM_PAGE_TO_PHYS(m[i]) | PG_RW | PG_V;
620		unsigned opte;
621		pte = (unsigned *)vtopte(tva);
622		opte = *pte;
623		*pte = npte;
624		if (opte)
625			pmap_update_1pg(tva);
626	}
627}
628
629/*
630 * this routine jerks page mappings from the
631 * kernel -- it is meant only for temporary mappings.
632 */
633void
634pmap_qremove(va, count)
635	vm_offset_t va;
636	int count;
637{
638	int i;
639	register unsigned *pte;
640
641	for (i = 0; i < count; i++) {
642		pte = (unsigned *)vtopte(va);
643		*pte = 0;
644		pmap_update_1pg(va);
645		va += PAGE_SIZE;
646	}
647}
648
649/*
650 * add a wired page to the kva
651 * note that in order for the mapping to take effect -- you
652 * should do a pmap_update after doing the pmap_kenter...
653 */
654PMAP_INLINE void
655pmap_kenter(va, pa)
656	vm_offset_t va;
657	register vm_offset_t pa;
658{
659	register unsigned *pte;
660	unsigned npte, opte;
661
662	npte = pa | PG_RW | PG_V;
663	pte = (unsigned *)vtopte(va);
664	opte = *pte;
665	*pte = npte;
666	if (opte)
667		pmap_update_1pg(va);
668}
669
670/*
671 * remove a page from the kernel pagetables
672 */
673PMAP_INLINE void
674pmap_kremove(va)
675	vm_offset_t va;
676{
677	register unsigned *pte;
678
679	pte = (unsigned *)vtopte(va);
680	*pte = 0;
681	pmap_update_1pg(va);
682}
683
684static vm_page_t
685pmap_page_alloc(object, pindex)
686	vm_object_t object;
687	vm_pindex_t pindex;
688{
689	vm_page_t m;
690	m = vm_page_alloc(object, pindex, VM_ALLOC_ZERO);
691	if (m == NULL) {
692		VM_WAIT;
693	}
694	return m;
695}
696
697vm_page_t
698pmap_page_lookup(object, pindex)
699	vm_object_t object;
700	vm_pindex_t pindex;
701{
702	vm_page_t m;
703retry:
704	m = vm_page_lookup(object, pindex);
705	if (m) {
706		if (m->flags & PG_BUSY) {
707			m->flags |= PG_WANTED;
708			tsleep(m, PVM, "pplookp", 0);
709			goto retry;
710		}
711	}
712
713	return m;
714}
715
716
717
718
719/***************************************************
720 * Page table page management routines.....
721 ***************************************************/
722
723/*
724 * This routine unholds page table pages, and if the hold count
725 * drops to zero, then it decrements the wire count.
726 */
727static int
728pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m) {
729	int s;
730
731	vm_page_unhold(m);
732
733	s = splvm();
734	while (m->flags & PG_BUSY) {
735		m->flags |= PG_WANTED;
736		tsleep(m, PVM, "pmuwpt", 0);
737	}
738	splx(s);
739
740	if (m->hold_count == 0) {
741		vm_offset_t pteva;
742		/*
743		 * unmap the page table page
744		 */
745		pmap->pm_pdir[m->pindex] = 0;
746		--pmap->pm_stats.resident_count;
747		if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
748			(((unsigned) PTDpde) & PG_FRAME)) {
749			/*
750			 * Do a pmap_update to make the invalidated mapping
751			 * take effect immediately.
752			 */
753			pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
754			pmap_update_1pg(pteva);
755		}
756
757#if defined(PTPHINT)
758		if (pmap->pm_ptphint == m)
759			pmap->pm_ptphint = NULL;
760#endif
761
762		/*
763		 * If the page is finally unwired, simply free it.
764		 */
765		--m->wire_count;
766		if (m->wire_count == 0) {
767
768			if (m->flags & PG_WANTED) {
769				m->flags &= ~PG_WANTED;
770				wakeup(m);
771			}
772
773			vm_page_free_zero(m);
774			--cnt.v_wire_count;
775		}
776		return 1;
777	}
778	return 0;
779}
780
781/*
782 * After removing a page table entry, this routine is used to
783 * conditionally free the page, and manage the hold/wire counts.
784 */
785int
786pmap_unuse_pt(pmap, va, mpte)
787	pmap_t pmap;
788	vm_offset_t va;
789	vm_page_t mpte;
790{
791	unsigned ptepindex;
792	if (va >= UPT_MIN_ADDRESS)
793		return 0;
794
795	if (mpte == NULL) {
796		ptepindex = (va >> PDRSHIFT);
797#if defined(PTPHINT)
798		if (pmap->pm_ptphint &&
799			(pmap->pm_ptphint->pindex == ptepindex)) {
800			mpte = pmap->pm_ptphint;
801		} else {
802			mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
803			pmap->pm_ptphint = mpte;
804		}
805#else
806		mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
807#endif
808	}
809
810	return pmap_unwire_pte_hold(pmap, mpte);
811}
812
813/*
814 * Initialize a preallocated and zeroed pmap structure,
815 * such as one in a vmspace structure.
816 */
817void
818pmap_pinit(pmap)
819	register struct pmap *pmap;
820{
821	vm_page_t ptdpg;
822	/*
823	 * No need to allocate page table space yet but we do need a valid
824	 * page directory table.
825	 */
826
827	if (pdstackptr > 0) {
828		--pdstackptr;
829		pmap->pm_pdir = (pd_entry_t *)pdstack[pdstackptr];
830	} else {
831		pmap->pm_pdir =
832			(pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
833	}
834
835	/*
836	 * allocate object for the ptes
837	 */
838	pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
839
840	/*
841	 * allocate the page directory page
842	 */
843retry:
844	ptdpg = pmap_page_alloc( pmap->pm_pteobj, PTDPTDI);
845	if (ptdpg == NULL)
846		goto retry;
847
848	ptdpg->wire_count = 1;
849	++cnt.v_wire_count;
850
851	ptdpg->flags &= ~(PG_MAPPED|PG_BUSY);	/* not mapped normally */
852	ptdpg->valid = VM_PAGE_BITS_ALL;
853
854	pmap_kenter((vm_offset_t) pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
855	if ((ptdpg->flags & PG_ZERO) == 0)
856		bzero(pmap->pm_pdir, PAGE_SIZE);
857
858	/* wire in kernel global address entries */
859	bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
860
861	/* install self-referential address mapping entry */
862	*(unsigned *) (pmap->pm_pdir + PTDPTDI) =
863		VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW;
864
865	pmap->pm_flags = 0;
866	pmap->pm_count = 1;
867	pmap->pm_ptphint = NULL;
868	TAILQ_INIT(&pmap->pm_pvlist);
869}
870
871static int
872pmap_release_free_page(pmap, p)
873	struct pmap *pmap;
874	vm_page_t p;
875{
876	int s;
877	unsigned *pde = (unsigned *) pmap->pm_pdir;
878	/*
879	 * This code optimizes the case of freeing non-busy
880	 * page-table pages.  Those pages are zero now, and
881	 * might as well be placed directly into the zero queue.
882	 */
883	s = splvm();
884	if (p->flags & PG_BUSY) {
885		p->flags |= PG_WANTED;
886		tsleep(p, PVM, "pmaprl", 0);
887		splx(s);
888		return 0;
889	}
890
891	if (p->flags & PG_WANTED) {
892		p->flags &= ~PG_WANTED;
893		wakeup(p);
894	}
895
896	/*
897	 * Remove the page table page from the processes address space.
898	 */
899	pde[p->pindex] = 0;
900	--pmap->pm_stats.resident_count;
901
902	if (p->hold_count)  {
903		panic("pmap_release: freeing held page table page");
904	}
905	/*
906	 * Page directory pages need to have the kernel
907	 * stuff cleared, so they can go into the zero queue also.
908	 */
909	if (p->pindex == PTDPTDI) {
910		bzero(pde + KPTDI, nkpt * PTESIZE);
911		pde[APTDPTDI] = 0;
912		pmap_kremove((vm_offset_t) pmap->pm_pdir);
913	}
914
915#if defined(PTPHINT)
916	if (pmap->pm_ptphint &&
917		(pmap->pm_ptphint->pindex == p->pindex))
918		pmap->pm_ptphint = NULL;
919#endif
920
921	vm_page_free_zero(p);
922	splx(s);
923	return 1;
924}
925
926/*
927 * this routine is called if the page table page is not
928 * mapped correctly.
929 */
930static vm_page_t
931_pmap_allocpte(pmap, ptepindex)
932	pmap_t	pmap;
933	unsigned ptepindex;
934{
935	vm_offset_t pteva, ptepa;
936	vm_page_t m;
937	int needszero = 0;
938
939	/*
940	 * Find or fabricate a new pagetable page
941	 */
942retry:
943	m = vm_page_lookup(pmap->pm_pteobj, ptepindex);
944	if (m == NULL) {
945		m = pmap_page_alloc(pmap->pm_pteobj, ptepindex);
946		if (m == NULL)
947			goto retry;
948		if ((m->flags & PG_ZERO) == 0)
949			needszero = 1;
950		m->flags &= ~(PG_ZERO|PG_BUSY);
951		m->valid = VM_PAGE_BITS_ALL;
952	} else {
953		if ((m->flags & PG_BUSY) || m->busy) {
954			m->flags |= PG_WANTED;
955			tsleep(m, PVM, "ptewai", 0);
956			goto retry;
957		}
958	}
959
960	if (m->queue != PQ_NONE) {
961		int s = splvm();
962		vm_page_unqueue(m);
963		splx(s);
964	}
965
966	if (m->wire_count == 0)
967		++cnt.v_wire_count;
968	++m->wire_count;
969
970	/*
971	 * Increment the hold count for the page table page
972	 * (denoting a new mapping.)
973	 */
974	++m->hold_count;
975
976	/*
977	 * Map the pagetable page into the process address space, if
978	 * it isn't already there.
979	 */
980
981	pmap->pm_stats.resident_count++;
982
983	ptepa = VM_PAGE_TO_PHYS(m);
984	pmap->pm_pdir[ptepindex] = (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V);
985
986#if defined(PTPHINT)
987	/*
988	 * Set the page table hint
989	 */
990	pmap->pm_ptphint = m;
991#endif
992
993	/*
994	 * Try to use the new mapping, but if we cannot, then
995	 * do it with the routine that maps the page explicitly.
996	 */
997	if (needszero) {
998		if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
999			(((unsigned) PTDpde) & PG_FRAME)) {
1000			pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1001			bzero((caddr_t) pteva, PAGE_SIZE);
1002		} else {
1003			pmap_zero_page(ptepa);
1004		}
1005	}
1006
1007	m->valid = VM_PAGE_BITS_ALL;
1008	m->flags |= PG_MAPPED;
1009
1010	return m;
1011}
1012
1013static vm_page_t
1014pmap_allocpte(pmap, va)
1015	pmap_t	pmap;
1016	vm_offset_t va;
1017{
1018	unsigned ptepindex;
1019	vm_offset_t ptepa;
1020	vm_page_t m;
1021
1022	/*
1023	 * Calculate pagetable page index
1024	 */
1025	ptepindex = va >> PDRSHIFT;
1026
1027	/*
1028	 * Get the page directory entry
1029	 */
1030	ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1031
1032	/*
1033	 * If the page table page is mapped, we just increment the
1034	 * hold count, and activate it.
1035	 */
1036	if (ptepa) {
1037#if defined(PTPHINT)
1038		/*
1039		 * In order to get the page table page, try the
1040		 * hint first.
1041		 */
1042		if (pmap->pm_ptphint &&
1043			(pmap->pm_ptphint->pindex == ptepindex)) {
1044			m = pmap->pm_ptphint;
1045		} else {
1046			m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1047			pmap->pm_ptphint = m;
1048		}
1049#else
1050		m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1051#endif
1052		++m->hold_count;
1053		return m;
1054	}
1055	/*
1056	 * Here if the pte page isn't mapped, or if it has been deallocated.
1057	 */
1058	return _pmap_allocpte(pmap, ptepindex);
1059}
1060
1061
1062/***************************************************
1063* Pmap allocation/deallocation routines.
1064 ***************************************************/
1065
1066/*
1067 * Release any resources held by the given physical map.
1068 * Called when a pmap initialized by pmap_pinit is being released.
1069 * Should only be called if the map contains no valid mappings.
1070 */
1071void
1072pmap_release(pmap)
1073	register struct pmap *pmap;
1074{
1075	vm_page_t p,n,ptdpg;
1076	vm_object_t object = pmap->pm_pteobj;
1077
1078	if (object->ref_count != 1)
1079		panic("pmap_release: pteobj reference count != 1");
1080
1081	pmap_lock(pmap);
1082	ptdpg = NULL;
1083retry:
1084	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1085		n = TAILQ_NEXT(p, listq);
1086		if (p->pindex == PTDPTDI) {
1087			ptdpg = p;
1088			continue;
1089		}
1090		if (!pmap_release_free_page(pmap, p))
1091			goto retry;
1092	}
1093
1094	if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1095		goto retry;
1096
1097	vm_object_deallocate(object);
1098	if (pdstackptr < PDSTACKMAX) {
1099		pdstack[pdstackptr] = (vm_offset_t) pmap->pm_pdir;
1100		++pdstackptr;
1101	} else {
1102		kmem_free(kernel_map, (vm_offset_t) pmap->pm_pdir, PAGE_SIZE);
1103	}
1104	pmap->pm_pdir = 0;
1105	pmap_update();
1106}
1107
1108/*
1109 * grow the number of kernel page table entries, if needed
1110 */
1111void
1112pmap_growkernel(vm_offset_t addr)
1113{
1114	struct proc *p;
1115	struct pmap *pmap;
1116	int s;
1117
1118	s = splhigh();
1119	if (kernel_vm_end == 0) {
1120		kernel_vm_end = KERNBASE;
1121		nkpt = 0;
1122		while (pdir_pde(PTD, kernel_vm_end)) {
1123			kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1124			++nkpt;
1125		}
1126	}
1127	addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1128	while (kernel_vm_end < addr) {
1129		if (pdir_pde(PTD, kernel_vm_end)) {
1130			kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1131			continue;
1132		}
1133		++nkpt;
1134		if (!nkpg) {
1135			vm_offset_t ptpkva = (vm_offset_t) vtopte(addr);
1136			/*
1137			 * This index is bogus, but out of the way
1138			 */
1139			vm_pindex_t ptpidx = (ptpkva >> PAGE_SHIFT);
1140			nkpg = vm_page_alloc(kernel_object,
1141				ptpidx, VM_ALLOC_SYSTEM);
1142			if (!nkpg)
1143				panic("pmap_growkernel: no memory to grow kernel");
1144			vm_page_wire(nkpg);
1145			vm_page_remove(nkpg);
1146			pmap_zero_page(VM_PAGE_TO_PHYS(nkpg));
1147		}
1148		pdir_pde(PTD, kernel_vm_end) = (pd_entry_t) (VM_PAGE_TO_PHYS(nkpg) | PG_V | PG_RW);
1149		nkpg = NULL;
1150
1151		for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1152			if (p->p_vmspace) {
1153				pmap = &p->p_vmspace->vm_pmap;
1154				*pmap_pde(pmap, kernel_vm_end) = pdir_pde(PTD, kernel_vm_end);
1155			}
1156		}
1157		*pmap_pde(kernel_pmap, kernel_vm_end) = pdir_pde(PTD, kernel_vm_end);
1158		kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1159	}
1160	splx(s);
1161}
1162
1163/*
1164 *	Retire the given physical map from service.
1165 *	Should only be called if the map contains
1166 *	no valid mappings.
1167 */
1168void
1169pmap_destroy(pmap)
1170	register pmap_t pmap;
1171{
1172	int count;
1173
1174	if (pmap == NULL)
1175		return;
1176
1177	count = --pmap->pm_count;
1178	if (count == 0) {
1179		pmap_release(pmap);
1180		free((caddr_t) pmap, M_VMPMAP);
1181	}
1182}
1183
1184/*
1185 *	Add a reference to the specified pmap.
1186 */
1187void
1188pmap_reference(pmap)
1189	pmap_t pmap;
1190{
1191	if (pmap != NULL) {
1192		pmap->pm_count++;
1193	}
1194}
1195
1196/***************************************************
1197* page management routines.
1198 ***************************************************/
1199
1200/*
1201 * free the pv_entry back to the free list
1202 */
1203static PMAP_INLINE void
1204free_pv_entry(pv)
1205	pv_entry_t pv;
1206{
1207	++pv_freelistcnt;
1208	TAILQ_INSERT_HEAD(&pv_freelist, pv, pv_list);
1209}
1210
1211/*
1212 * get a new pv_entry, allocating a block from the system
1213 * when needed.
1214 * the memory allocation is performed bypassing the malloc code
1215 * because of the possibility of allocations at interrupt time.
1216 */
1217static pv_entry_t
1218get_pv_entry()
1219{
1220	pv_entry_t tmp;
1221
1222	/*
1223	 * get more pv_entry pages if needed
1224	 */
1225	if (pv_freelistcnt < PV_FREELIST_MIN || !TAILQ_FIRST(&pv_freelist)) {
1226		pmap_alloc_pv_entry();
1227	}
1228	/*
1229	 * get a pv_entry off of the free list
1230	 */
1231	--pv_freelistcnt;
1232	tmp = TAILQ_FIRST(&pv_freelist);
1233	TAILQ_REMOVE(&pv_freelist, tmp, pv_list);
1234	return tmp;
1235}
1236
1237/*
1238 * This *strange* allocation routine eliminates the possibility of a malloc
1239 * failure (*FATAL*) for a pv_entry_t data structure.
1240 * also -- this code is MUCH MUCH faster than the malloc equiv...
1241 * We really need to do the slab allocator thingie here.
1242 */
1243static void
1244pmap_alloc_pv_entry()
1245{
1246	/*
1247	 * do we have any pre-allocated map-pages left?
1248	 */
1249	if (npvvapg) {
1250		vm_page_t m;
1251
1252		/*
1253		 * allocate a physical page out of the vm system
1254		 */
1255		m = vm_page_alloc(kernel_object,
1256		    OFF_TO_IDX(pvva - vm_map_min(kernel_map)),
1257		    VM_ALLOC_INTERRUPT);
1258		if (m) {
1259			int newentries;
1260			int i;
1261			pv_entry_t entry;
1262
1263			newentries = (PAGE_SIZE / sizeof(struct pv_entry));
1264			/*
1265			 * wire the page
1266			 */
1267			vm_page_wire(m);
1268			m->flags &= ~PG_BUSY;
1269			/*
1270			 * let the kernel see it
1271			 */
1272			pmap_kenter(pvva, VM_PAGE_TO_PHYS(m));
1273
1274			entry = (pv_entry_t) pvva;
1275			/*
1276			 * update the allocation pointers
1277			 */
1278			pvva += PAGE_SIZE;
1279			--npvvapg;
1280
1281			/*
1282			 * free the entries into the free list
1283			 */
1284			for (i = 0; i < newentries; i++) {
1285				free_pv_entry(entry);
1286				entry++;
1287			}
1288		}
1289	}
1290	if (!TAILQ_FIRST(&pv_freelist))
1291		panic("get_pv_entry: cannot get a pv_entry_t");
1292}
1293
1294/*
1295 * init the pv_entry allocation system
1296 */
1297#define PVSPERPAGE 64
1298void
1299init_pv_entries(npg)
1300	int npg;
1301{
1302	/*
1303	 * allocate enough kvm space for PVSPERPAGE entries per page (lots)
1304	 * kvm space is fairly cheap, be generous!!!  (the system can panic if
1305	 * this is too small.)
1306	 */
1307	npvvapg = ((npg * PVSPERPAGE) * sizeof(struct pv_entry)
1308		+ PAGE_SIZE - 1) / PAGE_SIZE;
1309	pvva = kmem_alloc_pageable(kernel_map, npvvapg * PAGE_SIZE);
1310	/*
1311	 * get the first batch of entries
1312	 */
1313	pmap_alloc_pv_entry();
1314}
1315
1316/*
1317 * If it is the first entry on the list, it is actually
1318 * in the header and we must copy the following entry up
1319 * to the header.  Otherwise we must search the list for
1320 * the entry.  In either case we free the now unused entry.
1321 */
1322
1323static int
1324pmap_remove_entry(pmap, ppv, va)
1325	struct pmap *pmap;
1326	pv_table_t *ppv;
1327	vm_offset_t va;
1328{
1329	pv_entry_t pv;
1330	int rtval;
1331	int s;
1332
1333	s = splvm();
1334	if (ppv->pv_list_count < pmap->pm_stats.resident_count) {
1335		for (pv = TAILQ_FIRST(&ppv->pv_list);
1336			pv;
1337			pv = TAILQ_NEXT(pv, pv_list)) {
1338			if (pmap == pv->pv_pmap && va == pv->pv_va)
1339				break;
1340		}
1341	} else {
1342		for (pv = TAILQ_FIRST(&pmap->pm_pvlist);
1343			pv;
1344			pv = TAILQ_NEXT(pv, pv_plist)) {
1345			if (va == pv->pv_va)
1346				break;
1347		}
1348	}
1349
1350	rtval = 0;
1351	if (pv) {
1352		rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1353		TAILQ_REMOVE(&ppv->pv_list, pv, pv_list);
1354		--ppv->pv_list_count;
1355		TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1356		free_pv_entry(pv);
1357	}
1358
1359	splx(s);
1360	return rtval;
1361}
1362
1363/*
1364 * Create a pv entry for page at pa for
1365 * (pmap, va).
1366 */
1367static void
1368pmap_insert_entry(pmap, va, mpte, pa)
1369	pmap_t pmap;
1370	vm_offset_t va;
1371	vm_page_t mpte;
1372	vm_offset_t pa;
1373{
1374
1375	int s;
1376	pv_entry_t pv;
1377	pv_table_t *ppv;
1378
1379	s = splvm();
1380	pv = get_pv_entry();
1381	pv->pv_va = va;
1382	pv->pv_pmap = pmap;
1383	pv->pv_ptem = mpte;
1384
1385	TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1386
1387	ppv = pa_to_pvh(pa);
1388	TAILQ_INSERT_TAIL(&ppv->pv_list, pv, pv_list);
1389	++ppv->pv_list_count;
1390
1391	splx(s);
1392}
1393
1394/*
1395 * pmap_remove_pte: do the things to unmap a page in a process
1396 */
1397static int
1398pmap_remove_pte(pmap, ptq, va)
1399	struct pmap *pmap;
1400	unsigned *ptq;
1401	vm_offset_t va;
1402{
1403	unsigned oldpte;
1404	pv_table_t *ppv;
1405
1406	oldpte = *ptq;
1407	*ptq = 0;
1408	if (oldpte & PG_W)
1409		pmap->pm_stats.wired_count -= 1;
1410	pmap->pm_stats.resident_count -= 1;
1411	if (oldpte & PG_MANAGED) {
1412		ppv = pa_to_pvh(oldpte);
1413		if (oldpte & PG_M) {
1414#if defined(PMAP_DIAGNOSTIC)
1415			if (pmap_nw_modified((pt_entry_t) oldpte)) {
1416				printf("pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, (int) oldpte);
1417			}
1418#endif
1419			if (pmap_track_modified(va))
1420				ppv->pv_vm_page->dirty = VM_PAGE_BITS_ALL;
1421		}
1422		return pmap_remove_entry(pmap, ppv, va);
1423	} else {
1424		return pmap_unuse_pt(pmap, va, NULL);
1425	}
1426
1427	return 0;
1428}
1429
1430/*
1431 * Remove a single page from a process address space
1432 */
1433static void
1434pmap_remove_page(pmap, va)
1435	struct pmap *pmap;
1436	register vm_offset_t va;
1437{
1438	register unsigned *ptq;
1439
1440	/*
1441	 * if there is no pte for this address, just skip it!!!
1442	 */
1443	if (*pmap_pde(pmap, va) == 0) {
1444		return;
1445	}
1446
1447	/*
1448	 * get a local va for mappings for this pmap.
1449	 */
1450	ptq = get_ptbase(pmap) + i386_btop(va);
1451	if (*ptq) {
1452		(void) pmap_remove_pte(pmap, ptq, va);
1453		pmap_update_1pg(va);
1454	}
1455	return;
1456}
1457
1458/*
1459 *	Remove the given range of addresses from the specified map.
1460 *
1461 *	It is assumed that the start and end are properly
1462 *	rounded to the page size.
1463 */
1464void
1465pmap_remove(pmap, sva, eva)
1466	struct pmap *pmap;
1467	register vm_offset_t sva;
1468	register vm_offset_t eva;
1469{
1470	register unsigned *ptbase;
1471	vm_offset_t pdnxt;
1472	vm_offset_t ptpaddr;
1473	vm_offset_t sindex, eindex;
1474	int anyvalid;
1475
1476	if (pmap == NULL)
1477		return;
1478
1479	pmap_lock(pmap);
1480	/*
1481	 * special handling of removing one page.  a very
1482	 * common operation and easy to short circuit some
1483	 * code.
1484	 */
1485	if ((sva + PAGE_SIZE) == eva) {
1486		pmap_remove_page(pmap, sva);
1487		pmap_unlock(pmap);
1488		return;
1489	}
1490
1491	anyvalid = 0;
1492
1493	/*
1494	 * Get a local virtual address for the mappings that are being
1495	 * worked with.
1496	 */
1497	ptbase = get_ptbase(pmap);
1498
1499	sindex = i386_btop(sva);
1500	eindex = i386_btop(eva);
1501
1502	for (; sindex < eindex; sindex = pdnxt) {
1503
1504		/*
1505		 * Calculate index for next page table.
1506		 */
1507		pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1508		ptpaddr = (vm_offset_t) *pmap_pde(pmap, i386_ptob(sindex));
1509
1510		/*
1511		 * Weed out invalid mappings. Note: we assume that the page
1512		 * directory table is always allocated, and in kernel virtual.
1513		 */
1514		if (ptpaddr == 0)
1515			continue;
1516
1517		/*
1518		 * Limit our scan to either the end of the va represented
1519		 * by the current page table page, or to the end of the
1520		 * range being removed.
1521		 */
1522		if (pdnxt > eindex) {
1523			pdnxt = eindex;
1524		}
1525
1526		for ( ;sindex != pdnxt; sindex++) {
1527			vm_offset_t va;
1528			if (ptbase[sindex] == 0) {
1529				continue;
1530			}
1531			va = i386_ptob(sindex);
1532
1533			anyvalid++;
1534			if (pmap_remove_pte(pmap,
1535				ptbase + sindex, va))
1536				break;
1537		}
1538	}
1539
1540	if (anyvalid) {
1541		pmap_update();
1542	}
1543	pmap_unlock(pmap);
1544}
1545
1546/*
1547 *	Routine:	pmap_remove_all
1548 *	Function:
1549 *		Removes this physical page from
1550 *		all physical maps in which it resides.
1551 *		Reflects back modify bits to the pager.
1552 *
1553 *	Notes:
1554 *		Original versions of this routine were very
1555 *		inefficient because they iteratively called
1556 *		pmap_remove (slow...)
1557 */
1558
1559static void
1560pmap_remove_all(pa)
1561	vm_offset_t pa;
1562{
1563	register pv_entry_t pv;
1564	pv_table_t *ppv;
1565	register unsigned *pte, tpte;
1566	int nmodify;
1567	int update_needed;
1568	int s;
1569
1570	nmodify = 0;
1571	update_needed = 0;
1572#if defined(PMAP_DIAGNOSTIC)
1573	/*
1574	 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1575	 * pages!
1576	 */
1577	if (!pmap_is_managed(pa)) {
1578		panic("pmap_page_protect: illegal for unmanaged page, va: 0x%lx", pa);
1579	}
1580#endif
1581
1582	s = splvm();
1583	ppv = pa_to_pvh(pa);
1584	while ((pv = TAILQ_FIRST(&ppv->pv_list)) != NULL) {
1585		pmap_lock(pv->pv_pmap);
1586		pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1587		if (tpte = *pte) {
1588			pv->pv_pmap->pm_stats.resident_count--;
1589			*pte = 0;
1590			if (tpte & PG_W)
1591				pv->pv_pmap->pm_stats.wired_count--;
1592			/*
1593			 * Update the vm_page_t clean and reference bits.
1594			 */
1595			if (tpte & PG_M) {
1596#if defined(PMAP_DIAGNOSTIC)
1597				if (pmap_nw_modified((pt_entry_t) tpte)) {
1598					printf("pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n", pv->pv_va, tpte);
1599				}
1600#endif
1601				if (pmap_track_modified(pv->pv_va))
1602					ppv->pv_vm_page->dirty = VM_PAGE_BITS_ALL;
1603			}
1604			if (!update_needed &&
1605				((!curproc || (&curproc->p_vmspace->vm_pmap == pv->pv_pmap)) ||
1606				(pv->pv_pmap == kernel_pmap))) {
1607				update_needed = 1;
1608			}
1609		}
1610		TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1611		TAILQ_REMOVE(&ppv->pv_list, pv, pv_list);
1612		--ppv->pv_list_count;
1613		pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1614		pmap_unlock(pv->pv_pmap);
1615		free_pv_entry(pv);
1616	}
1617
1618	if (update_needed)
1619		pmap_update();
1620	splx(s);
1621	return;
1622}
1623
1624/*
1625 *	Set the physical protection on the
1626 *	specified range of this map as requested.
1627 */
1628void
1629pmap_protect(pmap, sva, eva, prot)
1630	register pmap_t pmap;
1631	vm_offset_t sva, eva;
1632	vm_prot_t prot;
1633{
1634	register unsigned *ptbase;
1635	vm_offset_t pdnxt;
1636	vm_offset_t ptpaddr;
1637	vm_offset_t sindex, eindex;
1638	int anychanged;
1639
1640
1641	if (pmap == NULL)
1642		return;
1643
1644	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1645		pmap_remove(pmap, sva, eva);
1646		return;
1647	}
1648	if (prot & VM_PROT_WRITE) {
1649		return;
1650	}
1651
1652	pmap_lock(pmap);
1653	anychanged = 0;
1654
1655	ptbase = get_ptbase(pmap);
1656
1657	sindex = i386_btop(sva);
1658	eindex = i386_btop(eva);
1659
1660	for (; sindex < eindex; sindex = pdnxt) {
1661
1662		pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1663		ptpaddr = (vm_offset_t) *pmap_pde(pmap, i386_ptob(sindex));
1664
1665		/*
1666		 * Weed out invalid mappings. Note: we assume that the page
1667		 * directory table is always allocated, and in kernel virtual.
1668		 */
1669		if (ptpaddr == 0)
1670			continue;
1671
1672		if (pdnxt > eindex) {
1673			pdnxt = eindex;
1674		}
1675
1676		for (; sindex != pdnxt; sindex++) {
1677
1678			unsigned pbits = ptbase[sindex];
1679
1680			if (pbits & PG_RW) {
1681				if (pbits & PG_M) {
1682					vm_offset_t sva = i386_ptob(sindex);
1683					if (pmap_track_modified(sva)) {
1684						vm_page_t m = PHYS_TO_VM_PAGE(pbits);
1685						m->dirty = VM_PAGE_BITS_ALL;
1686					}
1687				}
1688				ptbase[sindex] = pbits & ~(PG_M|PG_RW);
1689				anychanged = 1;
1690			}
1691		}
1692	}
1693	pmap_unlock(pmap);
1694	if (anychanged)
1695		pmap_update();
1696}
1697
1698/*
1699 *	Insert the given physical page (p) at
1700 *	the specified virtual address (v) in the
1701 *	target physical map with the protection requested.
1702 *
1703 *	If specified, the page will be wired down, meaning
1704 *	that the related pte can not be reclaimed.
1705 *
1706 *	NB:  This is the only routine which MAY NOT lazy-evaluate
1707 *	or lose information.  That is, this routine must actually
1708 *	insert this page into the given map NOW.
1709 */
1710void
1711pmap_enter(pmap, va, pa, prot, wired)
1712	register pmap_t pmap;
1713	vm_offset_t va;
1714	register vm_offset_t pa;
1715	vm_prot_t prot;
1716	boolean_t wired;
1717{
1718	register unsigned *pte;
1719	vm_offset_t opa;
1720	vm_offset_t origpte, newpte;
1721	vm_page_t mpte;
1722
1723	if (pmap == NULL)
1724		return;
1725
1726	pmap_lock(pmap);
1727	va &= PG_FRAME;
1728#ifdef PMAP_DIAGNOSTIC
1729	if (va > VM_MAX_KERNEL_ADDRESS)
1730		panic("pmap_enter: toobig");
1731	if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1732		panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1733#endif
1734
1735	mpte = NULL;
1736	/*
1737	 * In the case that a page table page is not
1738	 * resident, we are creating it here.
1739	 */
1740	if (va < UPT_MIN_ADDRESS)
1741		mpte = pmap_allocpte(pmap, va);
1742
1743	pte = pmap_pte(pmap, va);
1744	/*
1745	 * Page Directory table entry not valid, we need a new PT page
1746	 */
1747	if (pte == NULL) {
1748		panic("pmap_enter: invalid page directory, pdir=%p, va=0x%lx\n",
1749			pmap->pm_pdir[PTDPTDI], va);
1750	}
1751
1752	origpte = *(vm_offset_t *)pte;
1753	pa &= PG_FRAME;
1754	opa = origpte & PG_FRAME;
1755
1756	/*
1757	 * Mapping has not changed, must be protection or wiring change.
1758	 */
1759	if (origpte && (opa == pa)) {
1760		/*
1761		 * Wiring change, just update stats. We don't worry about
1762		 * wiring PT pages as they remain resident as long as there
1763		 * are valid mappings in them. Hence, if a user page is wired,
1764		 * the PT page will be also.
1765		 */
1766		if (wired && ((origpte & PG_W) == 0))
1767			pmap->pm_stats.wired_count++;
1768		else if (!wired && (origpte & PG_W))
1769			pmap->pm_stats.wired_count--;
1770
1771#if defined(PMAP_DIAGNOSTIC)
1772		if (pmap_nw_modified((pt_entry_t) origpte)) {
1773			printf("pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, origpte);
1774		}
1775#endif
1776
1777		/*
1778		 * We might be turning off write access to the page,
1779		 * so we go ahead and sense modify status.
1780		 */
1781		if (origpte & PG_MANAGED) {
1782			vm_page_t m;
1783			if (origpte & PG_M) {
1784				if (pmap_track_modified(va)) {
1785					m = PHYS_TO_VM_PAGE(pa);
1786					m->dirty = VM_PAGE_BITS_ALL;
1787				}
1788			}
1789			pa |= PG_MANAGED;
1790		}
1791
1792		if (mpte)
1793			--mpte->hold_count;
1794
1795		goto validate;
1796	}
1797	/*
1798	 * Mapping has changed, invalidate old range and fall through to
1799	 * handle validating new mapping.
1800	 */
1801	if (opa) {
1802		int err;
1803		err = pmap_remove_pte(pmap, pte, va);
1804		if (err)
1805			panic("pmap_enter: pte vanished, va: 0x%x", va);
1806	}
1807
1808	/*
1809	 * Enter on the PV list if part of our managed memory Note that we
1810	 * raise IPL while manipulating pv_table since pmap_enter can be
1811	 * called at interrupt time.
1812	 */
1813	if (pmap_is_managed(pa)) {
1814		pmap_insert_entry(pmap, va, mpte, pa);
1815		pa |= PG_MANAGED;
1816	}
1817
1818	/*
1819	 * Increment counters
1820	 */
1821	pmap->pm_stats.resident_count++;
1822	if (wired)
1823		pmap->pm_stats.wired_count++;
1824
1825validate:
1826	/*
1827	 * Now validate mapping with desired protection/wiring.
1828	 */
1829	newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
1830
1831	if (wired)
1832		newpte |= PG_W;
1833	if (va < UPT_MIN_ADDRESS)
1834		newpte |= PG_U;
1835
1836	/*
1837	 * if the mapping or permission bits are different, we need
1838	 * to update the pte.
1839	 */
1840	if ((origpte & ~(PG_M|PG_A)) != newpte) {
1841		*pte = newpte;
1842		if (origpte)
1843			pmap_update_1pg(va);
1844	}
1845	pmap_unlock(pmap);
1846}
1847
1848/*
1849 * this code makes some *MAJOR* assumptions:
1850 * 1. Current pmap & pmap exists.
1851 * 2. Not wired.
1852 * 3. Read access.
1853 * 4. No page table pages.
1854 * 5. Tlbflush is deferred to calling procedure.
1855 * 6. Page IS managed.
1856 * but is *MUCH* faster than pmap_enter...
1857 */
1858
1859static vm_page_t
1860pmap_enter_quick(pmap, va, pa, mpte)
1861	register pmap_t pmap;
1862	vm_offset_t va;
1863	register vm_offset_t pa;
1864	vm_page_t mpte;
1865{
1866	register unsigned *pte;
1867
1868	/*
1869	 * In the case that a page table page is not
1870	 * resident, we are creating it here.
1871	 */
1872	if (va < UPT_MIN_ADDRESS) {
1873		unsigned ptepindex;
1874		vm_offset_t ptepa;
1875
1876		/*
1877		 * Calculate pagetable page index
1878		 */
1879		ptepindex = va >> PDRSHIFT;
1880		if (mpte && (mpte->pindex == ptepindex)) {
1881			++mpte->hold_count;
1882		} else {
1883retry:
1884			/*
1885			 * Get the page directory entry
1886			 */
1887			ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1888
1889			/*
1890			 * If the page table page is mapped, we just increment
1891			 * the hold count, and activate it.
1892			 */
1893			if (ptepa) {
1894#if defined(PTPHINT)
1895				if (pmap->pm_ptphint &&
1896					(pmap->pm_ptphint->pindex == ptepindex)) {
1897					mpte = pmap->pm_ptphint;
1898				} else {
1899					mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1900					pmap->pm_ptphint = mpte;
1901				}
1902#else
1903				mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1904#endif
1905				if (mpte == NULL)
1906					goto retry;
1907				++mpte->hold_count;
1908			} else {
1909				mpte = _pmap_allocpte(pmap, ptepindex);
1910			}
1911		}
1912	} else {
1913		mpte = NULL;
1914	}
1915
1916	/*
1917	 * This call to vtopte makes the assumption that we are
1918	 * entering the page into the current pmap.  In order to support
1919	 * quick entry into any pmap, one would likely use pmap_pte_quick.
1920	 * But that isn't as quick as vtopte.
1921	 */
1922	pte = (unsigned *)vtopte(va);
1923	if (*pte) {
1924		if (mpte)
1925			pmap_unwire_pte_hold(pmap, mpte);
1926		return 0;
1927	}
1928
1929	/*
1930	 * Enter on the PV list if part of our managed memory Note that we
1931	 * raise IPL while manipulating pv_table since pmap_enter can be
1932	 * called at interrupt time.
1933	 */
1934	pmap_insert_entry(pmap, va, mpte, pa);
1935
1936	/*
1937	 * Increment counters
1938	 */
1939	pmap->pm_stats.resident_count++;
1940
1941	/*
1942	 * Now validate mapping with RO protection
1943	 */
1944	*pte = pa | PG_V | PG_U | PG_MANAGED;
1945
1946	return mpte;
1947}
1948
1949#define MAX_INIT_PT (96)
1950/*
1951 * pmap_object_init_pt preloads the ptes for a given object
1952 * into the specified pmap.  This eliminates the blast of soft
1953 * faults on process startup and immediately after an mmap.
1954 */
1955void
1956pmap_object_init_pt(pmap, addr, object, pindex, size, limit)
1957	pmap_t pmap;
1958	vm_offset_t addr;
1959	vm_object_t object;
1960	vm_pindex_t pindex;
1961	vm_size_t size;
1962	int limit;
1963{
1964	vm_offset_t tmpidx;
1965	int psize;
1966	vm_page_t p, mpte;
1967	int objpgs;
1968
1969	psize = i386_btop(size);
1970
1971	if (!pmap || (object->type != OBJT_VNODE) ||
1972		(limit && (psize > MAX_INIT_PT) &&
1973			(object->resident_page_count > MAX_INIT_PT))) {
1974		return;
1975	}
1976
1977	pmap_lock(pmap);
1978	if (psize + pindex > object->size)
1979		psize = object->size - pindex;
1980
1981	mpte = NULL;
1982	/*
1983	 * if we are processing a major portion of the object, then scan the
1984	 * entire thing.
1985	 */
1986	if (psize > (object->size >> 2)) {
1987		objpgs = psize;
1988
1989		for (p = TAILQ_FIRST(&object->memq);
1990		    ((objpgs > 0) && (p != NULL));
1991		    p = TAILQ_NEXT(p, listq)) {
1992
1993			tmpidx = p->pindex;
1994			if (tmpidx < pindex) {
1995				continue;
1996			}
1997			tmpidx -= pindex;
1998			if (tmpidx >= psize) {
1999				continue;
2000			}
2001			if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2002			    (p->busy == 0) &&
2003			    (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2004				if ((p->queue - p->pc) == PQ_CACHE)
2005					vm_page_deactivate(p);
2006				p->flags |= PG_BUSY;
2007				mpte = pmap_enter_quick(pmap,
2008					addr + i386_ptob(tmpidx),
2009					VM_PAGE_TO_PHYS(p), mpte);
2010				p->flags |= PG_MAPPED;
2011				PAGE_WAKEUP(p);
2012			}
2013			objpgs -= 1;
2014		}
2015	} else {
2016		/*
2017		 * else lookup the pages one-by-one.
2018		 */
2019		for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2020			p = vm_page_lookup(object, tmpidx + pindex);
2021			if (p &&
2022			    ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2023			    (p->busy == 0) &&
2024			    (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2025				if ((p->queue - p->pc) == PQ_CACHE)
2026					vm_page_deactivate(p);
2027				p->flags |= PG_BUSY;
2028				mpte = pmap_enter_quick(pmap,
2029					addr + i386_ptob(tmpidx),
2030					VM_PAGE_TO_PHYS(p), mpte);
2031				p->flags |= PG_MAPPED;
2032				PAGE_WAKEUP(p);
2033			}
2034		}
2035	}
2036	pmap_unlock(pmap);
2037	return;
2038}
2039
2040/*
2041 * pmap_prefault provides a quick way of clustering
2042 * pagefaults into a processes address space.  It is a "cousin"
2043 * of pmap_object_init_pt, except it runs at page fault time instead
2044 * of mmap time.
2045 */
2046#define PFBAK 2
2047#define PFFOR 2
2048#define PAGEORDER_SIZE (PFBAK+PFFOR)
2049
2050static int pmap_prefault_pageorder[] = {
2051	-PAGE_SIZE, PAGE_SIZE, -2 * PAGE_SIZE, 2 * PAGE_SIZE
2052};
2053
2054void
2055pmap_prefault(pmap, addra, entry, object)
2056	pmap_t pmap;
2057	vm_offset_t addra;
2058	vm_map_entry_t entry;
2059	vm_object_t object;
2060{
2061	int i;
2062	vm_offset_t starta;
2063	vm_offset_t addr;
2064	vm_pindex_t pindex;
2065	vm_page_t m, mpte;
2066
2067	if (entry->object.vm_object != object)
2068		return;
2069
2070	if (!curproc || (pmap != &curproc->p_vmspace->vm_pmap))
2071		return;
2072
2073	pmap_lock(pmap);
2074	starta = addra - PFBAK * PAGE_SIZE;
2075	if (starta < entry->start) {
2076		starta = entry->start;
2077	} else if (starta > addra) {
2078		starta = 0;
2079	}
2080
2081	mpte = NULL;
2082	for (i = 0; i < PAGEORDER_SIZE; i++) {
2083		vm_object_t lobject;
2084		unsigned *pte;
2085
2086		addr = addra + pmap_prefault_pageorder[i];
2087		if (addr < starta || addr >= entry->end)
2088			continue;
2089
2090		if ((*pmap_pde(pmap, addr)) == NULL)
2091			continue;
2092
2093		pte = (unsigned *) vtopte(addr);
2094		if (*pte)
2095			continue;
2096
2097		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2098		lobject = object;
2099		for (m = vm_page_lookup(lobject, pindex);
2100		    (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object));
2101		    lobject = lobject->backing_object) {
2102			if (lobject->backing_object_offset & PAGE_MASK)
2103				break;
2104			pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2105			m = vm_page_lookup(lobject->backing_object, pindex);
2106		}
2107
2108		/*
2109		 * give-up when a page is not in memory
2110		 */
2111		if (m == NULL)
2112			break;
2113
2114		if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2115		    (m->busy == 0) &&
2116		    (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2117
2118			if ((m->queue - m->pc) == PQ_CACHE) {
2119				vm_page_deactivate(m);
2120			}
2121			m->flags |= PG_BUSY;
2122			mpte = pmap_enter_quick(pmap, addr,
2123				VM_PAGE_TO_PHYS(m), mpte);
2124			m->flags |= PG_MAPPED;
2125			PAGE_WAKEUP(m);
2126		}
2127	}
2128	pmap_unlock(pmap);
2129}
2130
2131/*
2132 *	Routine:	pmap_change_wiring
2133 *	Function:	Change the wiring attribute for a map/virtual-address
2134 *			pair.
2135 *	In/out conditions:
2136 *			The mapping must already exist in the pmap.
2137 */
2138void
2139pmap_change_wiring(pmap, va, wired)
2140	register pmap_t pmap;
2141	vm_offset_t va;
2142	boolean_t wired;
2143{
2144	register unsigned *pte;
2145
2146	if (pmap == NULL)
2147		return;
2148
2149	pmap_lock(pmap);
2150	pte = pmap_pte(pmap, va);
2151
2152	if (wired && !pmap_pte_w(pte))
2153		pmap->pm_stats.wired_count++;
2154	else if (!wired && pmap_pte_w(pte))
2155		pmap->pm_stats.wired_count--;
2156
2157	/*
2158	 * Wiring is not a hardware characteristic so there is no need to
2159	 * invalidate TLB.
2160	 */
2161	pmap_pte_set_w(pte, wired);
2162	pmap_unlock(pmap);
2163}
2164
2165
2166
2167/*
2168 *	Copy the range specified by src_addr/len
2169 *	from the source map to the range dst_addr/len
2170 *	in the destination map.
2171 *
2172 *	This routine is only advisory and need not do anything.
2173 */
2174
2175void
2176pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
2177	pmap_t dst_pmap, src_pmap;
2178	vm_offset_t dst_addr;
2179	vm_size_t len;
2180	vm_offset_t src_addr;
2181{
2182	vm_offset_t addr;
2183	vm_offset_t end_addr = src_addr + len;
2184	vm_offset_t pdnxt;
2185	unsigned src_frame, dst_frame;
2186
2187	if (dst_addr != src_addr)
2188		return;
2189
2190	pmap_lock2(src_pmap, dst_pmap);
2191	src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2192	if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2193		pmap_unlock(src_pmap);
2194		pmap_unlock(dst_pmap);
2195		return;
2196	}
2197
2198	dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2199	if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2200		APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2201		pmap_update();
2202	}
2203
2204	for(addr = src_addr; addr < end_addr; addr = pdnxt) {
2205		unsigned *src_pte, *dst_pte;
2206		vm_page_t dstmpte, srcmpte;
2207		vm_offset_t srcptepaddr;
2208		unsigned ptepindex;
2209
2210		if (addr >= UPT_MIN_ADDRESS)
2211			panic("pmap_copy: invalid to pmap_copy page tables\n");
2212
2213		pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2214		ptepindex = addr >> PDRSHIFT;
2215
2216		srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2217		if (srcptepaddr == 0)
2218			continue;
2219
2220		srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2221		if ((srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2222			continue;
2223
2224		if (pdnxt > end_addr)
2225			pdnxt = end_addr;
2226
2227		src_pte = (unsigned *) vtopte(addr);
2228		dst_pte = (unsigned *) avtopte(addr);
2229		while (addr < pdnxt) {
2230			unsigned ptetemp;
2231			ptetemp = *src_pte;
2232			/*
2233			 * we only virtual copy managed pages
2234			 */
2235			if ((ptetemp & PG_MANAGED) != 0) {
2236				/*
2237				 * We have to check after allocpte for the
2238				 * pte still being around...  allocpte can
2239				 * block.
2240				 */
2241				dstmpte = pmap_allocpte(dst_pmap, addr);
2242				if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2243					/*
2244					 * Clear the modified and
2245					 * accessed (referenced) bits
2246					 * during the copy.
2247					 */
2248					*dst_pte = ptetemp & ~(PG_M|PG_A);
2249					dst_pmap->pm_stats.resident_count++;
2250					pmap_insert_entry(dst_pmap, addr,
2251						dstmpte,
2252						(ptetemp & PG_FRAME));
2253	 			} else {
2254					pmap_unwire_pte_hold(dst_pmap, dstmpte);
2255				}
2256				if (dstmpte->hold_count >= srcmpte->hold_count)
2257					break;
2258			}
2259			addr += PAGE_SIZE;
2260			++src_pte;
2261			++dst_pte;
2262		}
2263	}
2264	pmap_unlock(src_pmap);
2265	pmap_unlock(dst_pmap);
2266}
2267
2268/*
2269 *	Routine:	pmap_kernel
2270 *	Function:
2271 *		Returns the physical map handle for the kernel.
2272 */
2273pmap_t
2274pmap_kernel()
2275{
2276	return (kernel_pmap);
2277}
2278
2279/*
2280 *	pmap_zero_page zeros the specified (machine independent)
2281 *	page by mapping the page into virtual memory and using
2282 *	bzero to clear its contents, one machine dependent page
2283 *	at a time.
2284 */
2285void
2286pmap_zero_page(phys)
2287	vm_offset_t phys;
2288{
2289	if (*(int *) CMAP2)
2290		panic("pmap_zero_page: CMAP busy");
2291
2292	*(int *) CMAP2 = PG_V | PG_RW | (phys & PG_FRAME);
2293	bzero(CADDR2, PAGE_SIZE);
2294	*(int *) CMAP2 = 0;
2295	pmap_update_1pg((vm_offset_t) CADDR2);
2296}
2297
2298/*
2299 *	pmap_copy_page copies the specified (machine independent)
2300 *	page by mapping the page into virtual memory and using
2301 *	bcopy to copy the page, one machine dependent page at a
2302 *	time.
2303 */
2304void
2305pmap_copy_page(src, dst)
2306	vm_offset_t src;
2307	vm_offset_t dst;
2308{
2309	if (*(int *) CMAP1 || *(int *) CMAP2)
2310		panic("pmap_copy_page: CMAP busy");
2311
2312	*(int *) CMAP1 = PG_V | PG_RW | (src & PG_FRAME);
2313	*(int *) CMAP2 = PG_V | PG_RW | (dst & PG_FRAME);
2314
2315	bcopy(CADDR1, CADDR2, PAGE_SIZE);
2316
2317	*(int *) CMAP1 = 0;
2318	*(int *) CMAP2 = 0;
2319	pmap_update_2pg( (vm_offset_t) CADDR1, (vm_offset_t) CADDR2);
2320}
2321
2322
2323/*
2324 *	Routine:	pmap_pageable
2325 *	Function:
2326 *		Make the specified pages (by pmap, offset)
2327 *		pageable (or not) as requested.
2328 *
2329 *		A page which is not pageable may not take
2330 *		a fault; therefore, its page table entry
2331 *		must remain valid for the duration.
2332 *
2333 *		This routine is merely advisory; pmap_enter
2334 *		will specify that these pages are to be wired
2335 *		down (or not) as appropriate.
2336 */
2337void
2338pmap_pageable(pmap, sva, eva, pageable)
2339	pmap_t pmap;
2340	vm_offset_t sva, eva;
2341	boolean_t pageable;
2342{
2343}
2344
2345/*
2346 * this routine returns true if a physical page resides
2347 * in the given pmap.
2348 */
2349boolean_t
2350pmap_page_exists(pmap, pa)
2351	pmap_t pmap;
2352	vm_offset_t pa;
2353{
2354	register pv_entry_t pv;
2355	pv_table_t *ppv;
2356	int s;
2357
2358	if (!pmap_is_managed(pa))
2359		return FALSE;
2360
2361	s = splvm();
2362
2363	ppv = pa_to_pvh(pa);
2364	/*
2365	 * Not found, check current mappings returning immediately if found.
2366	 */
2367	for (pv = TAILQ_FIRST(&ppv->pv_list);
2368		pv;
2369		pv = TAILQ_NEXT(pv, pv_list)) {
2370		if (pv->pv_pmap == pmap) {
2371			splx(s);
2372			return TRUE;
2373		}
2374	}
2375	splx(s);
2376	return (FALSE);
2377}
2378
2379#ifdef NOT_USED_YET
2380#define PMAP_REMOVE_PAGES_CURPROC_ONLY
2381/*
2382 * Remove all pages from specified address space
2383 * this aids process exit speeds.  Also, this code
2384 * is special cased for current process only.
2385 */
2386void
2387pmap_remove_pages(pmap, sva, eva)
2388	pmap_t pmap;
2389	vm_offset_t sva, eva;
2390{
2391	unsigned *pte, tpte;
2392	pv_table_t *ppv;
2393	pv_entry_t pv, npv;
2394	int s;
2395
2396#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2397	if (!curproc || (pmap != &curproc->p_vmspace->vm_pmap)) {
2398		printf("warning: pmap_remove_pages called with non-current pmap\n");
2399		return;
2400	}
2401#endif
2402
2403	pmap_lock(pmap);
2404	s = splhigh();
2405
2406	for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2407		pv;
2408		pv = npv) {
2409
2410		if (pv->pv_va >= eva || pv->pv_va < sva) {
2411			npv = TAILQ_NEXT(pv, pv_plist);
2412			continue;
2413		}
2414
2415#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2416		pte = (unsigned *)vtopte(pv->pv_va);
2417#else
2418		pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2419#endif
2420		tpte = *pte;
2421		*pte = 0;
2422
2423		ppv = pa_to_pvh(tpte);
2424
2425		if (tpte) {
2426			pv->pv_pmap->pm_stats.resident_count--;
2427			if (tpte & PG_W)
2428				pv->pv_pmap->pm_stats.wired_count--;
2429			/*
2430			 * Update the vm_page_t clean and reference bits.
2431			 */
2432			if (tpte & PG_M) {
2433				ppv->pv_vm_page->dirty = VM_PAGE_BITS_ALL;
2434			}
2435		}
2436
2437		npv = TAILQ_NEXT(pv, pv_plist);
2438		TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2439
2440		--ppv->pv_list_count;
2441		TAILQ_REMOVE(&ppv->pv_list, pv, pv_list);
2442
2443		pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2444		free_pv_entry(pv);
2445	}
2446	splx(s);
2447	pmap_update();
2448	pmap_unlock(pmap);
2449}
2450#endif
2451
2452/*
2453 * pmap_testbit tests bits in pte's
2454 * note that the testbit/changebit routines are inline,
2455 * and a lot of things compile-time evaluate.
2456 */
2457static boolean_t
2458pmap_testbit(pa, bit)
2459	register vm_offset_t pa;
2460	int bit;
2461{
2462	register pv_entry_t pv;
2463	pv_table_t *ppv;
2464	unsigned *pte;
2465	int s;
2466
2467	if (!pmap_is_managed(pa))
2468		return FALSE;
2469
2470	ppv = pa_to_pvh(pa);
2471	if (TAILQ_FIRST(&ppv->pv_list) == NULL)
2472		return FALSE;
2473
2474	s = splvm();
2475
2476	for (pv = TAILQ_FIRST(&ppv->pv_list);
2477		pv;
2478		pv = TAILQ_NEXT(pv, pv_list)) {
2479
2480		/*
2481		 * if the bit being tested is the modified bit, then
2482		 * mark clean_map and ptes as never
2483		 * modified.
2484		 */
2485		if (bit & (PG_A|PG_M)) {
2486			if (!pmap_track_modified(pv->pv_va))
2487				continue;
2488		}
2489
2490#if defined(PMAP_DIAGNOSTIC)
2491		if (!pv->pv_pmap) {
2492			printf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2493			continue;
2494		}
2495#endif
2496		pmap_lock(pv->pv_pmap);
2497		pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2498		if (pte == NULL) {
2499			pmap_unlock(pv->pv_pmap);
2500			continue;
2501		}
2502		if (*pte & bit) {
2503			pmap_unlock(pv->pv_pmap);
2504			splx(s);
2505			return TRUE;
2506		}
2507		pmap_unlock(pv->pv_pmap);
2508	}
2509	splx(s);
2510	return (FALSE);
2511}
2512
2513/*
2514 * this routine is used to modify bits in ptes
2515 */
2516static void
2517pmap_changebit(pa, bit, setem)
2518	vm_offset_t pa;
2519	int bit;
2520	boolean_t setem;
2521{
2522	register pv_entry_t pv;
2523	pv_table_t *ppv;
2524	register unsigned *pte;
2525	vm_offset_t va;
2526	int changed;
2527	int s;
2528
2529	if (!pmap_is_managed(pa))
2530		return;
2531
2532	s = splvm();
2533	changed = 0;
2534	ppv = pa_to_pvh(pa);
2535
2536	/*
2537	 * Loop over all current mappings setting/clearing as appropos If
2538	 * setting RO do we need to clear the VAC?
2539	 */
2540	for (pv = TAILQ_FIRST(&ppv->pv_list);
2541		pv;
2542		pv = TAILQ_NEXT(pv, pv_list)) {
2543
2544		va = pv->pv_va;
2545
2546		/*
2547		 * don't write protect pager mappings
2548		 */
2549		if (!setem && (bit == PG_RW)) {
2550			if (!pmap_track_modified(pv->pv_va))
2551				continue;
2552		}
2553
2554#if defined(PMAP_DIAGNOSTIC)
2555		if (!pv->pv_pmap) {
2556			printf("Null pmap (cb) at va: 0x%lx\n", va);
2557			continue;
2558		}
2559#endif
2560
2561		pmap_lock(pv->pv_pmap);
2562		pte = pmap_pte_quick(pv->pv_pmap, va);
2563		if (pte == NULL) {
2564			pmap_unlock(pv->pv_pmap);
2565			continue;
2566		}
2567		if (setem) {
2568			*(int *)pte |= bit;
2569			changed = 1;
2570		} else {
2571			vm_offset_t pbits = *(vm_offset_t *)pte;
2572			if (pbits & bit) {
2573				changed = 1;
2574				if (bit == PG_RW) {
2575					if (pbits & PG_M) {
2576						ppv->pv_vm_page->dirty = VM_PAGE_BITS_ALL;
2577					}
2578					*(int *)pte = pbits & ~(PG_M|PG_RW);
2579				} else {
2580					*(int *)pte = pbits & ~bit;
2581				}
2582			}
2583		}
2584		pmap_unlock(pv->pv_pmap);
2585	}
2586	splx(s);
2587	if (changed)
2588		pmap_update();
2589}
2590
2591/*
2592 *      pmap_page_protect:
2593 *
2594 *      Lower the permission for all mappings to a given page.
2595 */
2596void
2597pmap_page_protect(phys, prot)
2598	vm_offset_t phys;
2599	vm_prot_t prot;
2600{
2601	if ((prot & VM_PROT_WRITE) == 0) {
2602		if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2603			pmap_changebit(phys, PG_RW, FALSE);
2604		} else {
2605			pmap_remove_all(phys);
2606		}
2607	}
2608}
2609
2610vm_offset_t
2611pmap_phys_address(ppn)
2612	int ppn;
2613{
2614	return (i386_ptob(ppn));
2615}
2616
2617/*
2618 *	pmap_is_referenced:
2619 *
2620 *	Return whether or not the specified physical page was referenced
2621 *	by any physical maps.
2622 */
2623boolean_t
2624pmap_is_referenced(vm_offset_t pa)
2625{
2626	register pv_entry_t pv;
2627	pv_table_t *ppv;
2628	unsigned *pte;
2629	int s;
2630
2631	if (!pmap_is_managed(pa))
2632		return FALSE;
2633
2634	ppv = pa_to_pvh(pa);
2635
2636	s = splvm();
2637	/*
2638	 * Not found, check current mappings returning immediately if found.
2639	 */
2640	for (pv = TAILQ_FIRST(&ppv->pv_list);
2641		pv;
2642		pv = TAILQ_NEXT(pv, pv_list)) {
2643
2644		/*
2645		 * if the bit being tested is the modified bit, then
2646		 * mark clean_map and ptes as never
2647		 * modified.
2648		 */
2649		if (!pmap_track_modified(pv->pv_va))
2650			continue;
2651
2652		pmap_lock(pv->pv_pmap);
2653		pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2654		if (pte == NULL) {
2655			pmap_unlock(pv->pv_pmap);
2656			continue;
2657		}
2658		if ((int) *pte & PG_A) {
2659			pmap_unlock(pv->pv_pmap);
2660			splx(s);
2661			return TRUE;
2662		}
2663		pmap_unlock(pv->pv_pmap);
2664	}
2665	splx(s);
2666	return (FALSE);
2667}
2668
2669/*
2670 *	pmap_ts_referenced:
2671 *
2672 *	Return the count of reference bits for a page, clearing all of them.
2673 *
2674 */
2675int
2676pmap_ts_referenced(vm_offset_t pa)
2677{
2678	register pv_entry_t pv;
2679	pv_table_t *ppv;
2680	unsigned *pte;
2681	int s;
2682	int rtval = 0;
2683
2684	if (!pmap_is_managed(pa))
2685		return FALSE;
2686
2687	s = splvm();
2688
2689	ppv = pa_to_pvh(pa);
2690
2691	if (TAILQ_FIRST(&ppv->pv_list) == NULL) {
2692		splx(s);
2693		return 0;
2694	}
2695
2696	/*
2697	 * Not found, check current mappings returning immediately if found.
2698	 */
2699	for (pv = TAILQ_FIRST(&ppv->pv_list);
2700		pv;
2701		pv = TAILQ_NEXT(pv, pv_list)) {
2702		/*
2703		 * if the bit being tested is the modified bit, then
2704		 * mark clean_map and ptes as never
2705		 * modified.
2706		 */
2707		if (!pmap_track_modified(pv->pv_va))
2708			continue;
2709
2710		pmap_lock(pv->pv_pmap);
2711		pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2712		if (pte == NULL) {
2713			pmap_unlock(pv->pv_pmap);
2714			continue;
2715		}
2716		if (*pte & PG_A) {
2717			rtval++;
2718			*pte &= ~PG_A;
2719		}
2720		pmap_unlock(pv->pv_pmap);
2721	}
2722	splx(s);
2723	if (rtval) {
2724		pmap_update();
2725	}
2726	return (rtval);
2727}
2728
2729/*
2730 *	pmap_is_modified:
2731 *
2732 *	Return whether or not the specified physical page was modified
2733 *	in any physical maps.
2734 */
2735boolean_t
2736pmap_is_modified(vm_offset_t pa)
2737{
2738	return pmap_testbit((pa), PG_M);
2739}
2740
2741/*
2742 *	Clear the modify bits on the specified physical page.
2743 */
2744void
2745pmap_clear_modify(vm_offset_t pa)
2746{
2747	pmap_changebit((pa), PG_M, FALSE);
2748}
2749
2750/*
2751 *	pmap_clear_reference:
2752 *
2753 *	Clear the reference bit on the specified physical page.
2754 */
2755void
2756pmap_clear_reference(vm_offset_t pa)
2757{
2758	pmap_changebit((pa), PG_A, FALSE);
2759}
2760
2761#if 0
2762void
2763pmap_update_map(pmap_t pmap) {
2764	unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2765	if ((pmap == kernel_pmap) ||
2766		(frame == (((unsigned) PTDpde) & PG_FRAME))) {
2767		pmap_update();
2768	}
2769}
2770#endif
2771
2772/*
2773 * Miscellaneous support routines follow
2774 */
2775
2776static void
2777i386_protection_init()
2778{
2779	register int *kp, prot;
2780
2781	kp = protection_codes;
2782	for (prot = 0; prot < 8; prot++) {
2783		switch (prot) {
2784		case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
2785			/*
2786			 * Read access is also 0. There isn't any execute bit,
2787			 * so just make it readable.
2788			 */
2789		case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
2790		case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
2791		case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
2792			*kp++ = 0;
2793			break;
2794		case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
2795		case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
2796		case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
2797		case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
2798			*kp++ = PG_RW;
2799			break;
2800		}
2801	}
2802}
2803
2804/*
2805 * Map a set of physical memory pages into the kernel virtual
2806 * address space. Return a pointer to where it is mapped. This
2807 * routine is intended to be used for mapping device memory,
2808 * NOT real memory. The non-cacheable bits are set on each
2809 * mapped page.
2810 */
2811void *
2812pmap_mapdev(pa, size)
2813	vm_offset_t pa;
2814	vm_size_t size;
2815{
2816	vm_offset_t va, tmpva;
2817	unsigned *pte;
2818
2819	size = roundup(size, PAGE_SIZE);
2820
2821	va = kmem_alloc_pageable(kernel_map, size);
2822	if (!va)
2823		panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2824
2825	pa = pa & PG_FRAME;
2826	for (tmpva = va; size > 0;) {
2827		pte = (unsigned *)vtopte(tmpva);
2828		*pte = pa | PG_RW | PG_V | PG_N;
2829		size -= PAGE_SIZE;
2830		tmpva += PAGE_SIZE;
2831		pa += PAGE_SIZE;
2832	}
2833	pmap_update();
2834
2835	return ((void *) va);
2836}
2837
2838/*
2839 * perform the pmap work for mincore
2840 */
2841int
2842pmap_mincore(pmap, addr)
2843	pmap_t pmap;
2844	vm_offset_t addr;
2845{
2846
2847	unsigned *ptep, pte;
2848	int val = 0;
2849
2850	pmap_lock(pmap);
2851	ptep = pmap_pte(pmap, addr);
2852	if (ptep == 0) {
2853		pmap_unlock(pmap);
2854		return 0;
2855	}
2856
2857	if (pte = *ptep) {
2858		vm_offset_t pa;
2859		val = MINCORE_INCORE;
2860		pa = pte & PG_FRAME;
2861
2862		/*
2863		 * Modified by us
2864		 */
2865		if (pte & PG_M)
2866			val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2867		/*
2868		 * Modified by someone
2869		 */
2870		else if (PHYS_TO_VM_PAGE(pa)->dirty ||
2871			pmap_is_modified(pa))
2872			val |= MINCORE_MODIFIED_OTHER;
2873		/*
2874		 * Referenced by us
2875		 */
2876		if (pte & PG_U)
2877			val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2878
2879		/*
2880		 * Referenced by someone
2881		 */
2882		else if ((PHYS_TO_VM_PAGE(pa)->flags & PG_REFERENCED) ||
2883			pmap_is_referenced(pa))
2884			val |= MINCORE_REFERENCED_OTHER;
2885	}
2886	pmap_unlock(pmap);
2887	return val;
2888}
2889
2890#if defined(PMAP_DEBUG)
2891pmap_pid_dump(int pid) {
2892	pmap_t pmap;
2893	struct proc *p;
2894	int npte = 0;
2895	int index;
2896	for (p = allproc.lh_first; p != NULL; p = p->p_list.le_next) {
2897		if (p->p_pid != pid)
2898			continue;
2899
2900		if (p->p_vmspace) {
2901			int i,j;
2902			index = 0;
2903			pmap = &p->p_vmspace->vm_pmap;
2904			for(i=0;i<1024;i++) {
2905				pd_entry_t *pde;
2906				unsigned *pte;
2907				unsigned base = i << PDRSHIFT;
2908
2909				pde = &pmap->pm_pdir[i];
2910				if (pde && pmap_pde_v(pde)) {
2911					for(j=0;j<1024;j++) {
2912						unsigned va = base + (j << PAGE_SHIFT);
2913						if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
2914							if (index) {
2915								index = 0;
2916								printf("\n");
2917							}
2918							return npte;
2919						}
2920						pte = pmap_pte_quick( pmap, va);
2921						if (pte && pmap_pte_v(pte)) {
2922							vm_offset_t pa;
2923							vm_page_t m;
2924							pa = *(int *)pte;
2925							m = PHYS_TO_VM_PAGE((pa & PG_FRAME));
2926							printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
2927								va, pa, m->hold_count, m->wire_count, m->flags);
2928							npte++;
2929							index++;
2930							if (index >= 2) {
2931								index = 0;
2932								printf("\n");
2933							} else {
2934								printf(" ");
2935							}
2936						}
2937					}
2938				}
2939			}
2940		}
2941	}
2942	return npte;
2943}
2944#endif
2945
2946#if defined(DEBUG)
2947
2948static void	pads __P((pmap_t pm));
2949static void	pmap_pvdump __P((vm_offset_t pa));
2950
2951/* print address space of pmap*/
2952static void
2953pads(pm)
2954	pmap_t pm;
2955{
2956	unsigned va, i, j;
2957	unsigned *ptep;
2958
2959	if (pm == kernel_pmap)
2960		return;
2961	for (i = 0; i < 1024; i++)
2962		if (pm->pm_pdir[i])
2963			for (j = 0; j < 1024; j++) {
2964				va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
2965				if (pm == kernel_pmap && va < KERNBASE)
2966					continue;
2967				if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
2968					continue;
2969				ptep = pmap_pte_quick(pm, va);
2970				if (pmap_pte_v(ptep))
2971					printf("%x:%x ", va, *(int *) ptep);
2972			};
2973
2974}
2975
2976static void
2977pmap_pvdump(pa)
2978	vm_offset_t pa;
2979{
2980	pv_table_t *ppv;
2981	register pv_entry_t pv;
2982
2983	printf("pa %x", pa);
2984	ppv = pa_to_pvh(pa);
2985	for (pv = TAILQ_FIRST(&ppv->pv_list);
2986		pv;
2987		pv = TAILQ_NEXT(pv, pv_list)) {
2988#ifdef used_to_be
2989		printf(" -> pmap %x, va %x, flags %x",
2990		    pv->pv_pmap, pv->pv_va, pv->pv_flags);
2991#endif
2992		printf(" -> pmap %x, va %x",
2993		    pv->pv_pmap, pv->pv_va);
2994		pads(pv->pv_pmap);
2995	}
2996	printf(" ");
2997}
2998#endif
2999