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