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