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