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