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