xref: /netbsd-src/sys/arch/i386/include/pmap.h (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
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  *	from: @(#)pmap.h	7.4 (Berkeley) 5/12/91
38  *	$Id: pmap.h,v 1.3 1993/07/29 21:42:21 jtc Exp $
39  */
40 
41 /*
42  * Derived from hp300 version by Mike Hibler, this version by William
43  * Jolitz uses a recursive map [a pde points to the page directory] to
44  * map the page tables using the pagetables themselves. This is done to
45  * reduce the impact on kernel virtual memory for lots of sparse address
46  * space, and to reduce the cost of memory to each process.
47  *
48  * from hp300:	@(#)pmap.h	7.2 (Berkeley) 12/16/90
49  */
50 
51 #ifndef	_PMAP_MACHINE_
52 #define	_PMAP_MACHINE_	1
53 
54 /*
55  * 386 page table entry and page table directory
56  * W.Jolitz, 8/89
57  */
58 
59 struct pde
60 {
61 unsigned int
62 		pd_v:1,			/* valid bit */
63 		pd_prot:2,		/* access control */
64 		pd_mbz1:2,		/* reserved, must be zero */
65 		pd_u:1,			/* hardware maintained 'used' bit */
66 		:1,			/* not used */
67 		pd_mbz2:2,		/* reserved, must be zero */
68 		:3,			/* reserved for software */
69 		pd_pfnum:20;		/* physical page frame number of pte's*/
70 };
71 
72 #define	PD_MASK		0xffc00000	/* page directory address bits */
73 #define	PT_MASK		0x003ff000	/* page table address bits */
74 #define	PD_SHIFT	22		/* page directory address shift */
75 #define	PG_SHIFT	12		/* page table address shift */
76 
77 struct pte
78 {
79 unsigned int
80 		pg_v:1,			/* valid bit */
81 		pg_prot:2,		/* access control */
82 		pg_mbz1:2,		/* reserved, must be zero */
83 		pg_u:1,			/* hardware maintained 'used' bit */
84 		pg_m:1,			/* hardware maintained modified bit */
85 		pg_mbz2:2,		/* reserved, must be zero */
86 		pg_w:1,			/* software, wired down page */
87 		:1,			/* software (unused) */
88 		pg_nc:1,		/* 'uncacheable page' bit */
89 		pg_pfnum:20;		/* physical page frame number */
90 };
91 
92 #define	PG_V		0x00000001
93 #define	PG_RO		0x00000000
94 #define	PG_RW		0x00000002
95 #define	PG_u		0x00000004
96 #define	PG_PROT		0x00000006 /* all protection bits . */
97 #define	PG_W		0x00000200
98 #define PG_N		0x00000800 /* Non-cacheable */
99 #define	PG_M		0x00000040
100 #define PG_U		0x00000020
101 #define	PG_FRAME	0xfffff000
102 
103 #define	PG_NOACC	0
104 #define	PG_KR		0x00000000
105 #define	PG_KW		0x00000002
106 #define	PG_URKR		0x00000004
107 #define	PG_URKW		0x00000004
108 #define	PG_UW		0x00000006
109 
110 /* Garbage for current bastardized pager that assumes a hp300 */
111 #define	PG_NV	0
112 #define	PG_CI	0
113 /*
114  * Page Protection Exception bits
115  */
116 
117 #define PGEX_P		0x01	/* Protection violation vs. not present */
118 #define PGEX_W		0x02	/* during a Write cycle */
119 #define PGEX_U		0x04	/* access from User mode (UPL) */
120 
121 typedef struct pde	pd_entry_t;	/* page directory entry */
122 typedef struct pte	pt_entry_t;	/* Mach page table entry */
123 
124 /*
125  * One page directory, shared between
126  * kernel and user modes.
127  */
128 #define I386_PAGE_SIZE	NBPG
129 #define I386_PDR_SIZE	NBPDR
130 
131 #define I386_KPDES	8 /* KPT page directory size */
132 #define I386_UPDES	NBPDR/sizeof(struct pde)-8 /* UPT page directory size */
133 
134 #define	UPTDI		0x3f6		/* ptd entry for u./kernel&user stack */
135 #define	PTDPTDI		0x3f7		/* ptd entry that points to ptd! */
136 #define	KPTDI_FIRST	0x3f8		/* start of kernel virtual pde's */
137 #define	KPTDI_LAST	0x3fA		/* last of kernel virtual pde's */
138 
139 /*
140  * Address of current and alternate address space page table maps
141  * and directories.
142  */
143 #ifdef KERNEL
144 extern struct pte	PTmap[], APTmap[], Upte;
145 extern struct pde	PTD[], APTD[], PTDpde, APTDpde, Upde;
146 extern	pt_entry_t	*Sysmap;
147 
148 extern int	IdlePTD;	/* physical address of "Idle" state directory */
149 #endif
150 
151 /*
152  * virtual address to page table entry and
153  * to physical address. Likewise for alternate address space.
154  * Note: these work recursively, thus vtopte of a pte will give
155  * the corresponding pde that in turn maps it.
156  */
157 #define	vtopte(va)	(PTmap + i386_btop(va))
158 #define	kvtopte(va)	vtopte(va)
159 #define	ptetov(pt)	(i386_ptob(pt - PTmap))
160 #define	vtophys(va)  (i386_ptob(vtopte(va)->pg_pfnum) | ((int)(va) & PGOFSET))
161 #define ispt(va)	((va) >= UPT_MIN_ADDRESS && (va) <= KPT_MAX_ADDRESS)
162 
163 #define	avtopte(va)	(APTmap + i386_btop(va))
164 #define	ptetoav(pt)	(i386_ptob(pt - APTmap))
165 #define	avtophys(va)  (i386_ptob(avtopte(va)->pg_pfnum) | ((int)(va) & PGOFSET))
166 
167 /*
168  * macros to generate page directory/table indicies
169  */
170 
171 #define	pdei(va)	(((va)&PD_MASK)>>PD_SHIFT)
172 #define	ptei(va)	(((va)&PT_MASK)>>PG_SHIFT)
173 
174 /*
175  * Pmap stuff
176  */
177 
178 struct pmap {
179 	pd_entry_t		*pm_pdir;	/* KVA of page directory */
180 	boolean_t		pm_pdchanged;	/* pdir changed */
181 	short			pm_dref;	/* page directory ref count */
182 	short			pm_count;	/* pmap reference count */
183 	simple_lock_data_t	pm_lock;	/* lock on pmap */
184 	struct pmap_statistics	pm_stats;	/* pmap statistics */
185 	long			pm_ptpages;	/* more stats: PT pages */
186 };
187 
188 typedef struct pmap	*pmap_t;
189 
190 #ifdef KERNEL
191 extern pmap_t		kernel_pmap;
192 #endif
193 
194 /*
195  * Macros for speed
196  */
197 #define PMAP_ACTIVATE(pmapp, pcbp) \
198 	if ((pmapp) != NULL /*&& (pmapp)->pm_pdchanged */) {  \
199 		(pcbp)->pcb_cr3 = \
200 		    pmap_extract(kernel_pmap, (pmapp)->pm_pdir); \
201 		if ((pmapp) == &curproc->p_vmspace->vm_pmap) \
202 			load_cr3((pcbp)->pcb_cr3); \
203 		(pmapp)->pm_pdchanged = FALSE; \
204 	}
205 
206 #define PMAP_DEACTIVATE(pmapp, pcbp)
207 
208 /*
209  * For each vm_page_t, there is a list of all currently valid virtual
210  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
211  */
212 typedef struct pv_entry {
213 	struct pv_entry	*pv_next;	/* next pv_entry */
214 	pmap_t		pv_pmap;	/* pmap where mapping lies */
215 	vm_offset_t	pv_va;		/* virtual address for mapping */
216 	int		pv_flags;	/* flags */
217 } *pv_entry_t;
218 
219 #define	PV_ENTRY_NULL	((pv_entry_t) 0)
220 
221 #define	PV_CI		0x01	/* all entries must be cache inhibited */
222 #define PV_PTPAGE	0x02	/* entry maps a page table page */
223 
224 #ifdef	KERNEL
225 
226 pv_entry_t	pv_table;		/* array of entries, one per page */
227 
228 #define pa_index(pa)		atop(pa - vm_first_phys)
229 #define pa_to_pvh(pa)		(&pv_table[pa_index(pa)])
230 
231 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
232 
233 #endif	/* KERNEL */
234 
235 #endif	/* _PMAP_MACHINE_ */
236