xref: /original-bsd/sys/i386/include/pmap.h (revision 3705696b)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  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  * %sccs.include.redist.c%
10  *
11  *	@(#)pmap.h	8.1 (Berkeley) 06/11/93
12  */
13 
14 /*
15  * Derived from hp300 version by Mike Hibler, this version by William
16  * Jolitz uses a recursive map [a pde points to the page directory] to
17  * map the page tables using the pagetables themselves. This is done to
18  * reduce the impact on kernel virtual memory for lots of sparse address
19  * space, and to reduce the cost of memory to each process.
20  *
21  * from hp300:	@(#)pmap.h	7.2 (Berkeley) 12/16/90
22  */
23 
24 #ifndef	_PMAP_MACHINE_
25 #define	_PMAP_MACHINE_	1
26 
27 /*
28  * 386 page table entry and page table directory
29  * W.Jolitz, 8/89
30  */
31 
32 struct pde
33 {
34 unsigned int
35 		pd_v:1,			/* valid bit */
36 		pd_prot:2,		/* access control */
37 		pd_mbz1:2,		/* reserved, must be zero */
38 		pd_u:1,			/* hardware maintained 'used' bit */
39 		:1,			/* not used */
40 		pd_mbz2:2,		/* reserved, must be zero */
41 		:3,			/* reserved for software */
42 		pd_pfnum:20;		/* physical page frame number of pte's*/
43 };
44 
45 #define	PD_MASK		0xffc00000	/* page directory address bits */
46 #define	PT_MASK		0x003ff000	/* page table address bits */
47 #define	PD_SHIFT	22		/* page directory address shift */
48 #define	PG_SHIFT	12		/* page table address shift */
49 
50 struct pte
51 {
52 unsigned int
53 		pg_v:1,			/* valid bit */
54 		pg_prot:2,		/* access control */
55 		pg_mbz1:2,		/* reserved, must be zero */
56 		pg_u:1,			/* hardware maintained 'used' bit */
57 		pg_m:1,			/* hardware maintained modified bit */
58 		pg_mbz2:2,		/* reserved, must be zero */
59 		pg_w:1,			/* software, wired down page */
60 		:1,			/* software (unused) */
61 		pg_nc:1,		/* 'uncacheable page' bit */
62 		pg_pfnum:20;		/* physical page frame number */
63 };
64 
65 #define	PG_V		0x00000001
66 #define	PG_RO		0x00000000
67 #define	PG_RW		0x00000002
68 #define	PG_u		0x00000004
69 #define	PG_PROT		0x00000006 /* all protection bits . */
70 #define	PG_W		0x00000200
71 #define PG_N		0x00000800 /* Non-cacheable */
72 #define	PG_M		0x00000040
73 #define PG_U		0x00000020
74 #define	PG_FRAME	0xfffff000
75 
76 #define	PG_NOACC	0
77 #define	PG_KR		0x00000000
78 #define	PG_KW		0x00000002
79 #define	PG_URKR		0x00000004
80 #define	PG_URKW		0x00000004
81 #define	PG_UW		0x00000006
82 
83 /* Garbage for current bastardized pager that assumes a hp300 */
84 #define	PG_NV	0
85 #define	PG_CI	0
86 /*
87  * Page Protection Exception bits
88  */
89 
90 #define PGEX_P		0x01	/* Protection violation vs. not present */
91 #define PGEX_W		0x02	/* during a Write cycle */
92 #define PGEX_U		0x04	/* access from User mode (UPL) */
93 
94 typedef struct pde	pd_entry_t;	/* page directory entry */
95 typedef struct pte	pt_entry_t;	/* Mach page table entry */
96 
97 /*
98  * One page directory, shared between
99  * kernel and user modes.
100  */
101 #define I386_PAGE_SIZE	NBPG
102 #define I386_PDR_SIZE	NBPDR
103 
104 #define I386_KPDES	8 /* KPT page directory size */
105 #define I386_UPDES	NBPDR/sizeof(struct pde)-8 /* UPT page directory size */
106 
107 #define	UPTDI		0x3f6		/* ptd entry for u./kernel&user stack */
108 #define	PTDPTDI		0x3f7		/* ptd entry that points to ptd! */
109 #define	KPTDI_FIRST	0x3f8		/* start of kernel virtual pde's */
110 #define	KPTDI_LAST	0x3fA		/* last of kernel virtual pde's */
111 
112 /*
113  * Address of current and alternate address space page table maps
114  * and directories.
115  */
116 #ifdef KERNEL
117 extern struct pte	PTmap[], APTmap[], Upte;
118 extern struct pde	PTD[], APTD[], PTDpde, APTDpde, Upde;
119 extern	pt_entry_t	*Sysmap;
120 
121 extern int	IdlePTD;	/* physical address of "Idle" state directory */
122 #endif
123 
124 /*
125  * virtual address to page table entry and
126  * to physical address. Likewise for alternate address space.
127  * Note: these work recursively, thus vtopte of a pte will give
128  * the corresponding pde that in turn maps it.
129  */
130 #define	vtopte(va)	(PTmap + i386_btop(va))
131 #define	kvtopte(va)	vtopte(va)
132 #define	ptetov(pt)	(i386_ptob(pt - PTmap))
133 #define	vtophys(va)  (i386_ptob(vtopte(va)->pg_pfnum) | ((int)(va) & PGOFSET))
134 #define ispt(va)	((va) >= UPT_MIN_ADDRESS && (va) <= KPT_MAX_ADDRESS)
135 
136 #define	avtopte(va)	(APTmap + i386_btop(va))
137 #define	ptetoav(pt)	(i386_ptob(pt - APTmap))
138 #define	avtophys(va)  (i386_ptob(avtopte(va)->pg_pfnum) | ((int)(va) & PGOFSET))
139 
140 /*
141  * macros to generate page directory/table indicies
142  */
143 
144 #define	pdei(va)	(((va)&PD_MASK)>>PD_SHIFT)
145 #define	ptei(va)	(((va)&PT_MASK)>>PT_SHIFT)
146 
147 /*
148  * Pmap stuff
149  */
150 
151 struct pmap {
152 	pd_entry_t		*pm_pdir;	/* KVA of page directory */
153 	boolean_t		pm_pdchanged;	/* pdir changed */
154 	short			pm_dref;	/* page directory ref count */
155 	short			pm_count;	/* pmap reference count */
156 	simple_lock_data_t	pm_lock;	/* lock on pmap */
157 	struct pmap_statistics	pm_stats;	/* pmap statistics */
158 	long			pm_ptpages;	/* more stats: PT pages */
159 };
160 
161 typedef struct pmap	*pmap_t;
162 
163 #ifdef KERNEL
164 extern struct pmap	kernel_pmap_store;
165 #define kernel_pmap (&kernel_pmap_store)
166 #endif
167 
168 /*
169  * Macros for speed
170  */
171 #define PMAP_ACTIVATE(pmapp, pcbp) \
172 	if ((pmapp) != 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 #define	pmap_wired_count(pmap)		((pmap)->pm_stats.wired_count)
207 
208 #endif	KERNEL
209 
210 #endif	_PMAP_MACHINE_
211