xref: /freebsd/sys/i386/include/pmap.h (revision 0c43d89a0d8e976ca494d4837f4c1f3734d2c300)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * the Systems Programming Group of the University of Utah Computer
7  * Science Department and William Jolitz of UUNET Technologies Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * Derived from hp300 version by Mike Hibler, this version by William
38  * Jolitz uses a recursive map [a pde points to the page directory] to
39  * map the page tables using the pagetables themselves. This is done to
40  * reduce the impact on kernel virtual memory for lots of sparse address
41  * space, and to reduce the cost of memory to each process.
42  *
43  *	from: hp300: @(#)pmap.h	7.2 (Berkeley) 12/16/90
44  *	from: @(#)pmap.h	7.4 (Berkeley) 5/12/91
45  * 	$Id: pmap.h,v 1.15 1994/08/18 22:34:47 wollman Exp $
46  */
47 
48 #ifndef	_PMAP_MACHINE_
49 #define	_PMAP_MACHINE_	1
50 
51 #include <machine/pte.h>
52 
53 typedef unsigned int *pd_entry_t;
54 typedef unsigned int *pt_entry_t;
55 
56 /*
57  * NKPDE controls the virtual space of the kernel, what ever is left, minus
58  * the alternate page table area is given to the user (NUPDE)
59  */
60 /*
61  * NKPDE controls the virtual space of the kernel, what ever is left is
62  * given to the user (NUPDE)
63  */
64 #ifndef NKPT
65 #define	NKPT			24	/* actual number of kernel page tables */
66 #endif
67 #ifndef NKPDE
68 #define NKPDE			63	/* addressable number of page tables/pde's */
69 #endif
70 
71 #define	NUPDE		(NPTEPG-NKPDE)	/* number of user pde's */
72 
73 /*
74  * The *PTDI values control the layout of virtual memory
75  *
76  * XXX This works for now, but I am not real happy with it, I'll fix it
77  * right after I fix locore.s and the magic 28K hole
78  */
79 #define	APTDPTDI	(NPTEPG-1)	/* alt ptd entry that points to APTD */
80 #define	KPTDI		(APTDPTDI-NKPDE)/* start of kernel virtual pde's */
81 #define	PTDPTDI		(KPTDI-1)	/* ptd entry that points to ptd! */
82 #define	KSTKPTDI	(PTDPTDI-1)	/* ptd entry for u./kernel&user stack */
83 #define KSTKPTEOFF	(NBPG/sizeof(pd_entry_t)-UPAGES) /* pte entry for kernel stack */
84 
85 #define PDESIZE		sizeof(pd_entry_t) /* for assembly files */
86 #define PTESIZE		sizeof(pt_entry_t) /* for assembly files */
87 
88 /*
89  * Address of current and alternate address space page table maps
90  * and directories.
91  */
92 #ifdef KERNEL
93 extern pt_entry_t PTmap[], APTmap[], Upte;
94 extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
95 
96 extern int	IdlePTD;	/* physical address of "Idle" state directory */
97 #endif
98 
99 /*
100  * virtual address to page table entry and
101  * to physical address. Likewise for alternate address space.
102  * Note: these work recursively, thus vtopte of a pte will give
103  * the corresponding pde that in turn maps it.
104  */
105 #define	vtopte(va)	(PTmap + i386_btop(va))
106 #define	kvtopte(va)	vtopte(va)
107 #define	ptetov(pt)	(i386_ptob(pt - PTmap))
108 #define	vtophys(va)	(((int) (*vtopte(va))&PG_FRAME) | ((int)(va) & PGOFSET))
109 #define	ispt(va)	((va) >= UPT_MIN_ADDRESS && (va) <= KPT_MAX_ADDRESS)
110 
111 #define	avtopte(va)	(APTmap + i386_btop(va))
112 #define	ptetoav(pt)	(i386_ptob(pt - APTmap))
113 #define	avtophys(va)	(((int) (*avtopte(va))&PG_FRAME) | ((int)(va) & PGOFSET))
114 
115 #ifdef KERNEL
116 /*
117  *	Routine:	pmap_kextract
118  *	Function:
119  *		Extract the physical page address associated
120  *		kernel virtual address.
121  */
122 static inline vm_offset_t
123 pmap_kextract(va)
124 	vm_offset_t va;
125 {
126 	vm_offset_t pa = *(int *)vtopte(va);
127 	pa = (pa & PG_FRAME) | (va & ~PG_FRAME);
128 	return pa;
129 }
130 #endif
131 
132 /*
133  * macros to generate page directory/table indicies
134  */
135 
136 #define	pdei(va)	(((va)&PD_MASK)>>PD_SHIFT)
137 #define	ptei(va)	(((va)&PT_MASK)>>PG_SHIFT)
138 
139 /*
140  * Pmap stuff
141  */
142 
143 struct pmap {
144 	pd_entry_t		*pm_pdir;	/* KVA of page directory */
145 	boolean_t		pm_pdchanged;	/* pdir changed */
146 	short			pm_dref;	/* page directory ref count */
147 	short			pm_count;	/* pmap reference count */
148 	simple_lock_data_t	pm_lock;	/* lock on pmap */
149 	struct pmap_statistics	pm_stats;	/* pmap statistics */
150 	long			pm_ptpages;	/* more stats: PT pages */
151 };
152 
153 typedef struct pmap	*pmap_t;
154 
155 #ifdef KERNEL
156 extern pmap_t		kernel_pmap;
157 #endif
158 
159 /*
160  * Macros for speed
161  */
162 #define	PMAP_ACTIVATE(pmapp, pcbp) \
163 	if ((pmapp) != NULL /*&& (pmapp)->pm_pdchanged */) {  \
164 		(pcbp)->pcb_cr3 = \
165 		    pmap_extract(kernel_pmap, (vm_offset_t)(pmapp)->pm_pdir); \
166 		if ((pmapp) == &curproc->p_vmspace->vm_pmap) \
167 			load_cr3((pcbp)->pcb_cr3); \
168 		(pmapp)->pm_pdchanged = FALSE; \
169 	}
170 
171 #define	PMAP_DEACTIVATE(pmapp, pcbp)
172 
173 /*
174  * For each vm_page_t, there is a list of all currently valid virtual
175  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
176  */
177 typedef struct pv_entry {
178 	struct pv_entry	*pv_next;	/* next pv_entry */
179 	pmap_t		pv_pmap;	/* pmap where mapping lies */
180 	vm_offset_t	pv_va;		/* virtual address for mapping */
181 } *pv_entry_t;
182 
183 #define	PV_ENTRY_NULL	((pv_entry_t) 0)
184 
185 #define	PV_CI		0x01	/* all entries must be cache inhibited */
186 #define	PV_PTPAGE	0x02	/* entry maps a page table page */
187 
188 #ifdef	KERNEL
189 
190 pv_entry_t	pv_table;		/* array of entries, one per page */
191 
192 #define	pa_index(pa)		atop(pa - vm_first_phys)
193 #define	pa_to_pvh(pa)		(&pv_table[pa_index(pa)])
194 
195 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
196 
197 extern inline pt_entry_t *pmap_pte(pmap_t, vm_offset_t);
198 struct pcb; extern void pmap_activate(pmap_t, struct pcb *);
199 extern pmap_t pmap_kernel(void);
200 
201 #endif /* KERNEL */
202 
203 #endif /* _PMAP_MACHINE_ */
204