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