pmap.h revision 1.39 1 1.39 thorpej /* $NetBSD: pmap.h,v 1.39 1999/05/12 19:28:30 thorpej Exp $ */
2 1.38 mycroft
3 1.38 mycroft /*-
4 1.38 mycroft * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.38 mycroft * All rights reserved.
6 1.38 mycroft *
7 1.38 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.38 mycroft * by Charles M. Hannum.
9 1.38 mycroft *
10 1.38 mycroft * Redistribution and use in source and binary forms, with or without
11 1.38 mycroft * modification, are permitted provided that the following conditions
12 1.38 mycroft * are met:
13 1.38 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.38 mycroft * notice, this list of conditions and the following disclaimer.
15 1.38 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.38 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.38 mycroft * documentation and/or other materials provided with the distribution.
18 1.38 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.38 mycroft * must display the following acknowledgement:
20 1.38 mycroft * This product includes software developed by the NetBSD
21 1.38 mycroft * Foundation, Inc. and its contributors.
22 1.38 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.38 mycroft * contributors may be used to endorse or promote products derived
24 1.38 mycroft * from this software without specific prior written permission.
25 1.38 mycroft *
26 1.38 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.38 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.38 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.38 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.38 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.38 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.38 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.38 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.38 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.38 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.38 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.38 mycroft */
38 1.15 cgd
39 1.1 cgd /*
40 1.1 cgd * Copyright (c) 1991 Regents of the University of California.
41 1.1 cgd * All rights reserved.
42 1.1 cgd *
43 1.1 cgd * This code is derived from software contributed to Berkeley by
44 1.1 cgd * the Systems Programming Group of the University of Utah Computer
45 1.1 cgd * Science Department and William Jolitz of UUNET Technologies Inc.
46 1.1 cgd *
47 1.1 cgd * Redistribution and use in source and binary forms, with or without
48 1.1 cgd * modification, are permitted provided that the following conditions
49 1.1 cgd * are met:
50 1.1 cgd * 1. Redistributions of source code must retain the above copyright
51 1.1 cgd * notice, this list of conditions and the following disclaimer.
52 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 cgd * notice, this list of conditions and the following disclaimer in the
54 1.1 cgd * documentation and/or other materials provided with the distribution.
55 1.1 cgd * 3. All advertising materials mentioning features or use of this software
56 1.1 cgd * must display the following acknowledgement:
57 1.1 cgd * This product includes software developed by the University of
58 1.1 cgd * California, Berkeley and its contributors.
59 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
60 1.1 cgd * may be used to endorse or promote products derived from this software
61 1.1 cgd * without specific prior written permission.
62 1.1 cgd *
63 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 cgd * SUCH DAMAGE.
74 1.1 cgd *
75 1.15 cgd * @(#)pmap.h 7.4 (Berkeley) 5/12/91
76 1.1 cgd */
77 1.1 cgd
78 1.1 cgd /*
79 1.1 cgd * Derived from hp300 version by Mike Hibler, this version by William
80 1.1 cgd * Jolitz uses a recursive map [a pde points to the page directory] to
81 1.1 cgd * map the page tables using the pagetables themselves. This is done to
82 1.1 cgd * reduce the impact on kernel virtual memory for lots of sparse address
83 1.1 cgd * space, and to reduce the cost of memory to each process.
84 1.1 cgd *
85 1.1 cgd * from hp300: @(#)pmap.h 7.2 (Berkeley) 12/16/90
86 1.1 cgd */
87 1.34 mrg
88 1.34 mrg #if defined(_KERNEL) && !defined(_LKM)
89 1.34 mrg #include "opt_pmap_new.h"
90 1.39 thorpej #include "opt_user_ldt.h"
91 1.34 mrg #endif
92 1.1 cgd
93 1.33 mrg #ifdef PMAP_NEW /* redirect */
94 1.33 mrg #include <machine/pmap.new.h> /* defines _I386_PMAP_H_ */
95 1.33 mrg #endif
96 1.33 mrg
97 1.6 mycroft #ifndef _I386_PMAP_H_
98 1.6 mycroft #define _I386_PMAP_H_
99 1.6 mycroft
100 1.14 mycroft #include <machine/cpufunc.h>
101 1.6 mycroft #include <machine/pte.h>
102 1.39 thorpej #include <machine/segments.h>
103 1.1 cgd
104 1.1 cgd /*
105 1.1 cgd * 386 page table entry and page table directory
106 1.1 cgd * W.Jolitz, 8/89
107 1.1 cgd */
108 1.33 mrg
109 1.33 mrg /*
110 1.33 mrg * PG_AVAIL usage ...
111 1.33 mrg */
112 1.33 mrg
113 1.33 mrg #define PG_W PG_AVAIL1 /* "wired" mapping */
114 1.1 cgd
115 1.1 cgd /*
116 1.1 cgd * One page directory, shared between
117 1.1 cgd * kernel and user modes.
118 1.1 cgd */
119 1.27 fvdl #define PTDPTDI 0x3bf /* ptd entry that points to ptd! */
120 1.27 fvdl #define KPTDI 0x3c0 /* start of kernel virtual pde's */
121 1.24 fvdl #define NKPDE_BASE 4 /* min. # of kernel PDEs */
122 1.27 fvdl #define NKPDE_MAX 63 /* max. # of kernel PDEs */
123 1.24 fvdl #define NKPDE_SCALE 1 /* # of kernel PDEs to add per meg. */
124 1.6 mycroft #define APTDPTDI 0x3ff /* start of alternate page directory */
125 1.29 fvdl
126 1.29 fvdl #define UPT_MIN_ADDRESS (PTDPTDI<<PDSHIFT)
127 1.29 fvdl #define UPT_MAX_ADDRESS (UPT_MIN_ADDRESS + (PTDPTDI<<PGSHIFT))
128 1.1 cgd
129 1.1 cgd /*
130 1.1 cgd * Address of current and alternate address space page table maps
131 1.1 cgd * and directories.
132 1.1 cgd */
133 1.17 jtc #ifdef _KERNEL
134 1.10 mycroft extern pt_entry_t PTmap[], APTmap[], Upte;
135 1.10 mycroft extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
136 1.6 mycroft extern pt_entry_t *Sysmap;
137 1.1 cgd
138 1.36 thorpej extern u_long PTDpaddr; /* physical address of kernel PTD */
139 1.11 mycroft
140 1.37 thorpej void pmap_bootstrap __P((vaddr_t start));
141 1.37 thorpej boolean_t pmap_testbit __P((paddr_t, int));
142 1.37 thorpej void pmap_changebit __P((paddr_t, int, int));
143 1.1 cgd #endif
144 1.1 cgd
145 1.1 cgd /*
146 1.1 cgd * virtual address to page table entry and
147 1.1 cgd * to physical address. Likewise for alternate address space.
148 1.1 cgd * Note: these work recursively, thus vtopte of a pte will give
149 1.1 cgd * the corresponding pde that in turn maps it.
150 1.1 cgd */
151 1.1 cgd #define vtopte(va) (PTmap + i386_btop(va))
152 1.1 cgd #define kvtopte(va) vtopte(va)
153 1.1 cgd #define ptetov(pt) (i386_ptob(pt - PTmap))
154 1.6 mycroft #define vtophys(va) \
155 1.10 mycroft ((*vtopte(va) & PG_FRAME) | ((unsigned)(va) & ~PG_FRAME))
156 1.1 cgd
157 1.1 cgd #define avtopte(va) (APTmap + i386_btop(va))
158 1.1 cgd #define ptetoav(pt) (i386_ptob(pt - APTmap))
159 1.6 mycroft #define avtophys(va) \
160 1.10 mycroft ((*avtopte(va) & PG_FRAME) | ((unsigned)(va) & ~PG_FRAME))
161 1.1 cgd
162 1.1 cgd /*
163 1.1 cgd * macros to generate page directory/table indicies
164 1.1 cgd */
165 1.10 mycroft #define pdei(va) (((va) & PD_MASK) >> PDSHIFT)
166 1.10 mycroft #define ptei(va) (((va) & PT_MASK) >> PGSHIFT)
167 1.1 cgd
168 1.1 cgd /*
169 1.1 cgd * Pmap stuff
170 1.1 cgd */
171 1.11 mycroft typedef struct pmap {
172 1.1 cgd pd_entry_t *pm_pdir; /* KVA of page directory */
173 1.1 cgd short pm_dref; /* page directory ref count */
174 1.1 cgd short pm_count; /* pmap reference count */
175 1.1 cgd simple_lock_data_t pm_lock; /* lock on pmap */
176 1.1 cgd struct pmap_statistics pm_stats; /* pmap statistics */
177 1.1 cgd long pm_ptpages; /* more stats: PT pages */
178 1.39 thorpej
179 1.39 thorpej int pm_flags; /* see below */
180 1.39 thorpej
181 1.39 thorpej union descriptor *pm_ldt; /* user-set LDT entries */
182 1.39 thorpej int pm_ldt_len; /* number of LDT entries */
183 1.39 thorpej int pm_ldt_sel; /* LDT selector */
184 1.11 mycroft } *pmap_t;
185 1.1 cgd
186 1.39 thorpej /* pm_flags */
187 1.39 thorpej #define PMF_USER_LDT 0x01 /* pmap has user-set LDT */
188 1.39 thorpej
189 1.1 cgd /*
190 1.1 cgd * For each vm_page_t, there is a list of all currently valid virtual
191 1.11 mycroft * mappings of that page. An entry is a pv_entry, the list is pv_table.
192 1.1 cgd */
193 1.11 mycroft struct pv_entry {
194 1.1 cgd struct pv_entry *pv_next; /* next pv_entry */
195 1.1 cgd pmap_t pv_pmap; /* pmap where mapping lies */
196 1.37 thorpej vaddr_t pv_va; /* virtual address for mapping */
197 1.11 mycroft };
198 1.11 mycroft
199 1.11 mycroft struct pv_page;
200 1.11 mycroft
201 1.11 mycroft struct pv_page_info {
202 1.11 mycroft TAILQ_ENTRY(pv_page) pgi_list;
203 1.11 mycroft struct pv_entry *pgi_freelist;
204 1.11 mycroft int pgi_nfree;
205 1.11 mycroft };
206 1.1 cgd
207 1.11 mycroft /*
208 1.11 mycroft * This is basically:
209 1.11 mycroft * ((NBPG - sizeof(struct pv_page_info)) / sizeof(struct pv_entry))
210 1.11 mycroft */
211 1.11 mycroft #define NPVPPG 340
212 1.1 cgd
213 1.11 mycroft struct pv_page {
214 1.11 mycroft struct pv_page_info pvp_pgi;
215 1.11 mycroft struct pv_entry pvp_pv[NPVPPG];
216 1.11 mycroft };
217 1.1 cgd
218 1.19 jtc #ifdef _KERNEL
219 1.24 fvdl extern int nkpde; /* number of kernel page dir. ents */
220 1.12 mycroft extern struct pmap kernel_pmap_store;
221 1.1 cgd
222 1.18 mycroft #define pmap_kernel() (&kernel_pmap_store)
223 1.1 cgd #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count)
224 1.13 mycroft #define pmap_update() tlbflush()
225 1.11 mycroft
226 1.37 thorpej vaddr_t reserve_dumppages __P((vaddr_t));
227 1.23 christos
228 1.11 mycroft static __inline void
229 1.37 thorpej pmap_clear_modify(paddr_t pa)
230 1.11 mycroft {
231 1.11 mycroft pmap_changebit(pa, 0, ~PG_M);
232 1.11 mycroft }
233 1.11 mycroft
234 1.11 mycroft static __inline void
235 1.37 thorpej pmap_clear_reference(paddr_t pa)
236 1.11 mycroft {
237 1.11 mycroft pmap_changebit(pa, 0, ~PG_U);
238 1.11 mycroft }
239 1.11 mycroft
240 1.11 mycroft static __inline void
241 1.37 thorpej pmap_copy_on_write(paddr_t pa)
242 1.11 mycroft {
243 1.11 mycroft pmap_changebit(pa, PG_RO, ~PG_RW);
244 1.11 mycroft }
245 1.11 mycroft
246 1.11 mycroft static __inline boolean_t
247 1.37 thorpej pmap_is_modified(paddr_t pa)
248 1.11 mycroft {
249 1.11 mycroft return pmap_testbit(pa, PG_M);
250 1.11 mycroft }
251 1.11 mycroft
252 1.11 mycroft static __inline boolean_t
253 1.37 thorpej pmap_is_referenced(paddr_t pa)
254 1.11 mycroft {
255 1.11 mycroft return pmap_testbit(pa, PG_U);
256 1.11 mycroft }
257 1.11 mycroft
258 1.37 thorpej static __inline paddr_t
259 1.11 mycroft pmap_phys_address(int ppn)
260 1.11 mycroft {
261 1.11 mycroft return i386_ptob(ppn);
262 1.11 mycroft }
263 1.35 cgd
264 1.37 thorpej vaddr_t pmap_map __P((vaddr_t, paddr_t, paddr_t, int));
265 1.39 thorpej
266 1.39 thorpej #if defined(USER_LDT)
267 1.39 thorpej void pmap_ldt_cleanup __P((struct proc *));
268 1.39 thorpej #define PMAP_FORK
269 1.39 thorpej #endif /* USER_LDT */
270 1.1 cgd
271 1.17 jtc #endif /* _KERNEL */
272 1.1 cgd
273 1.6 mycroft #endif /* _I386_PMAP_H_ */
274