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