pmap.h revision 1.38 1 1.38 mycroft /* $NetBSD: pmap.h,v 1.38 1998/08/15 05:10:25 mycroft 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.34 mrg #endif
91 1.1 cgd
92 1.33 mrg #ifdef PMAP_NEW /* redirect */
93 1.33 mrg #include <machine/pmap.new.h> /* defines _I386_PMAP_H_ */
94 1.33 mrg #endif
95 1.33 mrg
96 1.6 mycroft #ifndef _I386_PMAP_H_
97 1.6 mycroft #define _I386_PMAP_H_
98 1.6 mycroft
99 1.14 mycroft #include <machine/cpufunc.h>
100 1.6 mycroft #include <machine/pte.h>
101 1.1 cgd
102 1.1 cgd /*
103 1.1 cgd * 386 page table entry and page table directory
104 1.1 cgd * W.Jolitz, 8/89
105 1.1 cgd */
106 1.33 mrg
107 1.33 mrg /*
108 1.33 mrg * PG_AVAIL usage ...
109 1.33 mrg */
110 1.33 mrg
111 1.33 mrg #define PG_W PG_AVAIL1 /* "wired" mapping */
112 1.1 cgd
113 1.1 cgd /*
114 1.1 cgd * One page directory, shared between
115 1.1 cgd * kernel and user modes.
116 1.1 cgd */
117 1.27 fvdl #define PTDPTDI 0x3bf /* ptd entry that points to ptd! */
118 1.27 fvdl #define KPTDI 0x3c0 /* start of kernel virtual pde's */
119 1.24 fvdl #define NKPDE_BASE 4 /* min. # of kernel PDEs */
120 1.27 fvdl #define NKPDE_MAX 63 /* max. # of kernel PDEs */
121 1.24 fvdl #define NKPDE_SCALE 1 /* # of kernel PDEs to add per meg. */
122 1.6 mycroft #define APTDPTDI 0x3ff /* start of alternate page directory */
123 1.29 fvdl
124 1.29 fvdl #define UPT_MIN_ADDRESS (PTDPTDI<<PDSHIFT)
125 1.29 fvdl #define UPT_MAX_ADDRESS (UPT_MIN_ADDRESS + (PTDPTDI<<PGSHIFT))
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * Address of current and alternate address space page table maps
129 1.1 cgd * and directories.
130 1.1 cgd */
131 1.17 jtc #ifdef _KERNEL
132 1.10 mycroft extern pt_entry_t PTmap[], APTmap[], Upte;
133 1.10 mycroft extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
134 1.6 mycroft extern pt_entry_t *Sysmap;
135 1.1 cgd
136 1.36 thorpej extern u_long PTDpaddr; /* physical address of kernel PTD */
137 1.11 mycroft
138 1.37 thorpej void pmap_bootstrap __P((vaddr_t start));
139 1.37 thorpej boolean_t pmap_testbit __P((paddr_t, int));
140 1.37 thorpej void pmap_changebit __P((paddr_t, int, int));
141 1.1 cgd #endif
142 1.1 cgd
143 1.1 cgd /*
144 1.1 cgd * virtual address to page table entry and
145 1.1 cgd * to physical address. Likewise for alternate address space.
146 1.1 cgd * Note: these work recursively, thus vtopte of a pte will give
147 1.1 cgd * the corresponding pde that in turn maps it.
148 1.1 cgd */
149 1.1 cgd #define vtopte(va) (PTmap + i386_btop(va))
150 1.1 cgd #define kvtopte(va) vtopte(va)
151 1.1 cgd #define ptetov(pt) (i386_ptob(pt - PTmap))
152 1.6 mycroft #define vtophys(va) \
153 1.10 mycroft ((*vtopte(va) & PG_FRAME) | ((unsigned)(va) & ~PG_FRAME))
154 1.1 cgd
155 1.1 cgd #define avtopte(va) (APTmap + i386_btop(va))
156 1.1 cgd #define ptetoav(pt) (i386_ptob(pt - APTmap))
157 1.6 mycroft #define avtophys(va) \
158 1.10 mycroft ((*avtopte(va) & PG_FRAME) | ((unsigned)(va) & ~PG_FRAME))
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * macros to generate page directory/table indicies
162 1.1 cgd */
163 1.10 mycroft #define pdei(va) (((va) & PD_MASK) >> PDSHIFT)
164 1.10 mycroft #define ptei(va) (((va) & PT_MASK) >> PGSHIFT)
165 1.1 cgd
166 1.1 cgd /*
167 1.1 cgd * Pmap stuff
168 1.1 cgd */
169 1.11 mycroft typedef struct pmap {
170 1.1 cgd pd_entry_t *pm_pdir; /* KVA of page directory */
171 1.1 cgd short pm_dref; /* page directory ref count */
172 1.1 cgd short pm_count; /* pmap reference count */
173 1.1 cgd simple_lock_data_t pm_lock; /* lock on pmap */
174 1.1 cgd struct pmap_statistics pm_stats; /* pmap statistics */
175 1.1 cgd long pm_ptpages; /* more stats: PT pages */
176 1.11 mycroft } *pmap_t;
177 1.1 cgd
178 1.1 cgd /*
179 1.1 cgd * For each vm_page_t, there is a list of all currently valid virtual
180 1.11 mycroft * mappings of that page. An entry is a pv_entry, the list is pv_table.
181 1.1 cgd */
182 1.11 mycroft struct pv_entry {
183 1.1 cgd struct pv_entry *pv_next; /* next pv_entry */
184 1.1 cgd pmap_t pv_pmap; /* pmap where mapping lies */
185 1.37 thorpej vaddr_t pv_va; /* virtual address for mapping */
186 1.11 mycroft };
187 1.11 mycroft
188 1.11 mycroft struct pv_page;
189 1.11 mycroft
190 1.11 mycroft struct pv_page_info {
191 1.11 mycroft TAILQ_ENTRY(pv_page) pgi_list;
192 1.11 mycroft struct pv_entry *pgi_freelist;
193 1.11 mycroft int pgi_nfree;
194 1.11 mycroft };
195 1.1 cgd
196 1.11 mycroft /*
197 1.11 mycroft * This is basically:
198 1.11 mycroft * ((NBPG - sizeof(struct pv_page_info)) / sizeof(struct pv_entry))
199 1.11 mycroft */
200 1.11 mycroft #define NPVPPG 340
201 1.1 cgd
202 1.11 mycroft struct pv_page {
203 1.11 mycroft struct pv_page_info pvp_pgi;
204 1.11 mycroft struct pv_entry pvp_pv[NPVPPG];
205 1.11 mycroft };
206 1.1 cgd
207 1.19 jtc #ifdef _KERNEL
208 1.24 fvdl extern int nkpde; /* number of kernel page dir. ents */
209 1.12 mycroft extern struct pmap kernel_pmap_store;
210 1.1 cgd
211 1.18 mycroft #define pmap_kernel() (&kernel_pmap_store)
212 1.1 cgd #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count)
213 1.13 mycroft #define pmap_update() tlbflush()
214 1.11 mycroft
215 1.37 thorpej vaddr_t reserve_dumppages __P((vaddr_t));
216 1.23 christos
217 1.11 mycroft static __inline void
218 1.37 thorpej pmap_clear_modify(paddr_t pa)
219 1.11 mycroft {
220 1.11 mycroft pmap_changebit(pa, 0, ~PG_M);
221 1.11 mycroft }
222 1.11 mycroft
223 1.11 mycroft static __inline void
224 1.37 thorpej pmap_clear_reference(paddr_t pa)
225 1.11 mycroft {
226 1.11 mycroft pmap_changebit(pa, 0, ~PG_U);
227 1.11 mycroft }
228 1.11 mycroft
229 1.11 mycroft static __inline void
230 1.37 thorpej pmap_copy_on_write(paddr_t pa)
231 1.11 mycroft {
232 1.11 mycroft pmap_changebit(pa, PG_RO, ~PG_RW);
233 1.11 mycroft }
234 1.11 mycroft
235 1.11 mycroft static __inline boolean_t
236 1.37 thorpej pmap_is_modified(paddr_t pa)
237 1.11 mycroft {
238 1.11 mycroft return pmap_testbit(pa, PG_M);
239 1.11 mycroft }
240 1.11 mycroft
241 1.11 mycroft static __inline boolean_t
242 1.37 thorpej pmap_is_referenced(paddr_t pa)
243 1.11 mycroft {
244 1.11 mycroft return pmap_testbit(pa, PG_U);
245 1.11 mycroft }
246 1.11 mycroft
247 1.37 thorpej static __inline paddr_t
248 1.11 mycroft pmap_phys_address(int ppn)
249 1.11 mycroft {
250 1.11 mycroft return i386_ptob(ppn);
251 1.11 mycroft }
252 1.35 cgd
253 1.37 thorpej vaddr_t pmap_map __P((vaddr_t, paddr_t, paddr_t, int));
254 1.1 cgd
255 1.17 jtc #endif /* _KERNEL */
256 1.1 cgd
257 1.6 mycroft #endif /* _I386_PMAP_H_ */
258