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