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