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