xref: /freebsd/sys/amd64/include/pmap.h (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
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.19 1994/12/18 03:11:46 davidg Exp $
46  */
47 
48 #ifndef _MACHINE_PMAP_H_
49 #define	_MACHINE_PMAP_H_
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 #if 0
66 #define	NKPT			26	/* actual number of kernel page tables */
67 #else
68 #define	NKPT			9	/* actual number of kernel page tables */
69 #endif
70 #endif
71 #ifndef NKPDE
72 #define NKPDE			63	/* addressable number of page tables/pde's */
73 #endif
74 
75 #define	NUPDE		(NPTEPG-NKPDE)	/* number of user pde's */
76 
77 /*
78  * The *PTDI values control the layout of virtual memory
79  *
80  * XXX This works for now, but I am not real happy with it, I'll fix it
81  * right after I fix locore.s and the magic 28K hole
82  */
83 #define	APTDPTDI	(NPTEPG-1)	/* alt ptd entry that points to APTD */
84 #define	KPTDI		(APTDPTDI-NKPDE)/* start of kernel virtual pde's */
85 #define	PTDPTDI		(KPTDI-1)	/* ptd entry that points to ptd! */
86 #define	KSTKPTDI	(PTDPTDI-1)	/* ptd entry for u./kernel&user stack */
87 #define KSTKPTEOFF	(NBPG/sizeof(pd_entry_t)-UPAGES) /* pte entry for kernel stack */
88 
89 #define PDESIZE		sizeof(pd_entry_t) /* for assembly files */
90 #define PTESIZE		sizeof(pt_entry_t) /* for assembly files */
91 
92 /*
93  * Address of current and alternate address space page table maps
94  * and directories.
95  */
96 #ifdef KERNEL
97 extern pt_entry_t PTmap[], APTmap[], Upte;
98 extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
99 
100 extern int	IdlePTD;	/* physical address of "Idle" state directory */
101 #endif
102 
103 /*
104  * virtual address to page table entry and
105  * to physical address. Likewise for alternate address space.
106  * Note: these work recursively, thus vtopte of a pte will give
107  * the corresponding pde that in turn maps it.
108  */
109 #define	vtopte(va)	(PTmap + i386_btop(va))
110 #define	kvtopte(va)	vtopte(va)
111 #define	ptetov(pt)	(i386_ptob(pt - PTmap))
112 #define	vtophys(va)	(((int) (*vtopte(va))&PG_FRAME) | ((int)(va) & PGOFSET))
113 #define	ispt(va)	((va) >= UPT_MIN_ADDRESS && (va) <= KPT_MAX_ADDRESS)
114 
115 #define	avtopte(va)	(APTmap + i386_btop(va))
116 #define	ptetoav(pt)	(i386_ptob(pt - APTmap))
117 #define	avtophys(va)	(((int) (*avtopte(va))&PG_FRAME) | ((int)(va) & PGOFSET))
118 
119 #ifdef KERNEL
120 /*
121  *	Routine:	pmap_kextract
122  *	Function:
123  *		Extract the physical page address associated
124  *		kernel virtual address.
125  */
126 static __inline vm_offset_t
127 pmap_kextract(vm_offset_t va)
128 {
129 	vm_offset_t pa = *(int *)vtopte(va);
130 	pa = (pa & PG_FRAME) | (va & ~PG_FRAME);
131 	return pa;
132 }
133 
134 /*
135  *	pmap_is_referenced:
136  *
137  *	Return whether or not the specified physical page was referenced
138  *	by any physical maps.
139  */
140 #define pmap_is_referenced(pa)		pmap_testbit((pa), PG_U)
141 
142 /*
143  *	pmap_is_modified:
144  *
145  *	Return whether or not the specified physical page was modified
146  *	in any physical maps.
147  */
148 #define pmap_is_modified(pa)		pmap_testbit((pa), PG_M)
149 
150 /*
151  *	Clear the modify bits on the specified physical page.
152  */
153 #define pmap_clear_modify(pa)		pmap_changebit((pa), PG_M, FALSE)
154 
155 /*
156  *	pmap_clear_reference:
157  *
158  *	Clear the reference bit on the specified physical page.
159  */
160 #define pmap_clear_reference(pa)	pmap_changebit((pa), PG_U, FALSE)
161 
162 /*
163  *	Routine:	pmap_copy_on_write
164  *	Function:
165  *		Remove write privileges from all
166  *		physical maps for this physical page.
167  */
168 #define pmap_copy_on_write(pa)		pmap_changebit((pa), PG_RW, FALSE)
169 
170 #endif
171 
172 /*
173  * macros to generate page directory/table indicies
174  */
175 
176 #define	pdei(va)	(((va)&PD_MASK)>>PD_SHIFT)
177 #define	ptei(va)	(((va)&PT_MASK)>>PG_SHIFT)
178 
179 /*
180  * Pmap stuff
181  */
182 
183 struct pmap {
184 	pd_entry_t		*pm_pdir;	/* KVA of page directory */
185 	boolean_t		pm_pdchanged;	/* pdir changed */
186 	short			pm_dref;	/* page directory ref count */
187 	short			pm_count;	/* pmap reference count */
188 	simple_lock_data_t	pm_lock;	/* lock on pmap */
189 	struct pmap_statistics	pm_stats;	/* pmap statistics */
190 	long			pm_ptpages;	/* more stats: PT pages */
191 };
192 
193 typedef struct pmap	*pmap_t;
194 
195 #ifdef KERNEL
196 extern pmap_t		kernel_pmap;
197 #endif
198 
199 /*
200  * Macros for speed
201  */
202 #define	PMAP_ACTIVATE(pmapp, pcbp) \
203 	if ((pmapp) != NULL /*&& (pmapp)->pm_pdchanged */) {  \
204 		(pcbp)->pcb_cr3 = \
205 		    pmap_extract(kernel_pmap, (vm_offset_t)(pmapp)->pm_pdir); \
206 		if ((pmapp) == &curproc->p_vmspace->vm_pmap) \
207 			load_cr3((pcbp)->pcb_cr3); \
208 		(pmapp)->pm_pdchanged = FALSE; \
209 	}
210 
211 #define	PMAP_DEACTIVATE(pmapp, pcbp)
212 
213 /*
214  * For each vm_page_t, there is a list of all currently valid virtual
215  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
216  */
217 typedef struct pv_entry {
218 	struct pv_entry	*pv_next;	/* next pv_entry */
219 	pmap_t		pv_pmap;	/* pmap where mapping lies */
220 	vm_offset_t	pv_va;		/* virtual address for mapping */
221 } *pv_entry_t;
222 
223 #define	PV_ENTRY_NULL	((pv_entry_t) 0)
224 
225 #define	PV_CI		0x01	/* all entries must be cache inhibited */
226 #define	PV_PTPAGE	0x02	/* entry maps a page table page */
227 
228 #ifdef	KERNEL
229 
230 pv_entry_t	pv_table;		/* array of entries, one per page */
231 
232 #define	pa_index(pa)		atop(pa - vm_first_phys)
233 #define	pa_to_pvh(pa)		(&pv_table[pa_index(pa)])
234 
235 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
236 
237 struct pcb;
238 
239 void	pmap_activate __P((pmap_t, struct pcb *));
240 void	pmap_changebit __P((vm_offset_t, int, boolean_t));
241 pmap_t	pmap_kernel __P((void));
242 boolean_t pmap_page_exists __P((pmap_t, vm_offset_t));
243 pt_entry_t *pmap_pte(pmap_t, vm_offset_t);
244 vm_page_t pmap_pte_vm_page __P((pmap_t, vm_offset_t));
245 
246 #endif /* KERNEL */
247 
248 #endif /* !_MACHINE_PMAP_H_ */
249