pmap.h revision 1.90.2.3 1 1.90.2.1 yamt /* $NetBSD: pmap.h,v 1.90.2.3 2007/10/04 15:36:57 yamt Exp $ */
2 1.38 mycroft
3 1.40 thorpej /*
4 1.40 thorpej *
5 1.40 thorpej * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 1.38 mycroft * All rights reserved.
7 1.38 mycroft *
8 1.38 mycroft * Redistribution and use in source and binary forms, with or without
9 1.38 mycroft * modification, are permitted provided that the following conditions
10 1.38 mycroft * are met:
11 1.38 mycroft * 1. Redistributions of source code must retain the above copyright
12 1.38 mycroft * notice, this list of conditions and the following disclaimer.
13 1.38 mycroft * 2. Redistributions in binary form must reproduce the above copyright
14 1.38 mycroft * notice, this list of conditions and the following disclaimer in the
15 1.38 mycroft * documentation and/or other materials provided with the distribution.
16 1.38 mycroft * 3. All advertising materials mentioning features or use of this software
17 1.40 thorpej * must display the following acknowledgment:
18 1.40 thorpej * This product includes software developed by Charles D. Cranor and
19 1.40 thorpej * Washington University.
20 1.40 thorpej * 4. The name of the author may not be used to endorse or promote products
21 1.40 thorpej * derived from this software without specific prior written permission.
22 1.1 cgd *
23 1.40 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.40 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.40 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.40 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.40 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.40 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.40 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.40 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.40 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.40 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.1 cgd /*
36 1.90.2.1 yamt * Copyright (c) 2001 Wasabi Systems, Inc.
37 1.90.2.1 yamt * All rights reserved.
38 1.90.2.1 yamt *
39 1.90.2.1 yamt * Written by Frank van der Linden for Wasabi Systems, Inc.
40 1.90.2.1 yamt *
41 1.90.2.1 yamt * Redistribution and use in source and binary forms, with or without
42 1.90.2.1 yamt * modification, are permitted provided that the following conditions
43 1.90.2.1 yamt * are met:
44 1.90.2.1 yamt * 1. Redistributions of source code must retain the above copyright
45 1.90.2.1 yamt * notice, this list of conditions and the following disclaimer.
46 1.90.2.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
47 1.90.2.1 yamt * notice, this list of conditions and the following disclaimer in the
48 1.90.2.1 yamt * documentation and/or other materials provided with the distribution.
49 1.90.2.1 yamt * 3. All advertising materials mentioning features or use of this software
50 1.90.2.1 yamt * must display the following acknowledgement:
51 1.90.2.1 yamt * This product includes software developed for the NetBSD Project by
52 1.90.2.1 yamt * Wasabi Systems, Inc.
53 1.90.2.1 yamt * 4. The name of Wasabi Systems, Inc. may not be used to endorse
54 1.90.2.1 yamt * or promote products derived from this software without specific prior
55 1.90.2.1 yamt * written permission.
56 1.90.2.1 yamt *
57 1.90.2.1 yamt * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
58 1.90.2.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59 1.90.2.1 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60 1.90.2.1 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
61 1.90.2.1 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 1.90.2.1 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 1.90.2.1 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 1.90.2.1 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 1.90.2.1 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 1.90.2.1 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
67 1.90.2.1 yamt * POSSIBILITY OF SUCH DAMAGE.
68 1.90.2.1 yamt */
69 1.90.2.1 yamt
70 1.90.2.1 yamt /*
71 1.40 thorpej * pmap.h: see pmap.c for the history of this pmap module.
72 1.1 cgd */
73 1.34 mrg
74 1.40 thorpej #ifndef _I386_PMAP_H_
75 1.40 thorpej #define _I386_PMAP_H_
76 1.40 thorpej
77 1.58 mrg #if defined(_KERNEL_OPT)
78 1.39 thorpej #include "opt_user_ldt.h"
79 1.34 mrg #endif
80 1.1 cgd
81 1.14 mycroft #include <machine/cpufunc.h>
82 1.6 mycroft #include <machine/pte.h>
83 1.39 thorpej #include <machine/segments.h>
84 1.90 ad #include <machine/atomic.h>
85 1.90 ad
86 1.40 thorpej #include <uvm/uvm_object.h>
87 1.1 cgd
88 1.1 cgd /*
89 1.40 thorpej * see pte.h for a description of i386 MMU terminology and hardware
90 1.40 thorpej * interface.
91 1.40 thorpej *
92 1.40 thorpej * a pmap describes a processes' 4GB virtual address space. this
93 1.40 thorpej * virtual address space can be broken up into 1024 4MB regions which
94 1.41 chs * are described by PDEs in the PDP. the PDEs are defined as follows:
95 1.40 thorpej *
96 1.40 thorpej * (ranges are inclusive -> exclusive, just like vm_map_entry start/end)
97 1.43 thorpej * (the following assumes that KERNBASE is 0xc0000000)
98 1.40 thorpej *
99 1.40 thorpej * PDE#s VA range usage
100 1.68 drochner * 0->766 0x0 -> 0xbfc00000 user address space
101 1.61 yamt * 767 0xbfc00000-> recursive mapping of PDP (used for
102 1.43 thorpej * 0xc0000000 linear mapping of PTPs)
103 1.43 thorpej * 768->1023 0xc0000000-> kernel address space (constant
104 1.40 thorpej * 0xffc00000 across all pmap's/processes)
105 1.40 thorpej * 1023 0xffc00000-> "alternate" recursive PDP mapping
106 1.40 thorpej * <end> (for other pmaps)
107 1.40 thorpej *
108 1.40 thorpej *
109 1.40 thorpej * note: a recursive PDP mapping provides a way to map all the PTEs for
110 1.41 chs * a 4GB address space into a linear chunk of virtual memory. in other
111 1.41 chs * words, the PTE for page 0 is the first int mapped into the 4MB recursive
112 1.41 chs * area. the PTE for page 1 is the second int. the very last int in the
113 1.81 junyoung * 4MB range is the PTE that maps VA 0xfffff000 (the last page in a 4GB
114 1.40 thorpej * address).
115 1.40 thorpej *
116 1.43 thorpej * all pmap's PD's must have the same values in slots 768->1023 so that
117 1.41 chs * the kernel is always mapped in every process. these values are loaded
118 1.40 thorpej * into the PD at pmap creation time.
119 1.40 thorpej *
120 1.41 chs * at any one time only one pmap can be active on a processor. this is
121 1.41 chs * the pmap whose PDP is pointed to by processor register %cr3. this pmap
122 1.40 thorpej * will have all its PTEs mapped into memory at the recursive mapping
123 1.43 thorpej * point (slot #767 as show above). when the pmap code wants to find the
124 1.40 thorpej * PTE for a virtual address, all it has to do is the following:
125 1.40 thorpej *
126 1.71 thorpej * address of PTE = (767 * 4MB) + (VA / PAGE_SIZE) * sizeof(pt_entry_t)
127 1.43 thorpej * = 0xbfc00000 + (VA / 4096) * 4
128 1.40 thorpej *
129 1.40 thorpej * what happens if the pmap layer is asked to perform an operation
130 1.41 chs * on a pmap that is not the one which is currently active? in that
131 1.41 chs * case we take the PA of the PDP of non-active pmap and put it in
132 1.41 chs * slot 1023 of the active pmap. this causes the non-active pmap's
133 1.40 thorpej * PTEs to get mapped in the final 4MB of the 4GB address space
134 1.40 thorpej * (e.g. starting at 0xffc00000).
135 1.40 thorpej *
136 1.40 thorpej * the following figure shows the effects of the recursive PDP mapping:
137 1.40 thorpej *
138 1.40 thorpej * PDP (%cr3)
139 1.40 thorpej * +----+
140 1.40 thorpej * | 0| -> PTP#0 that maps VA 0x0 -> 0x400000
141 1.40 thorpej * | |
142 1.40 thorpej * | |
143 1.43 thorpej * | 767| -> points back to PDP (%cr3) mapping VA 0xbfc00000 -> 0xc0000000
144 1.83 junyoung * | 768| -> first kernel PTP (maps 0xc0000000 -> 0xc0400000)
145 1.40 thorpej * | |
146 1.40 thorpej * |1023| -> points to alternate pmap's PDP (maps 0xffc00000 -> end)
147 1.40 thorpej * +----+
148 1.40 thorpej *
149 1.43 thorpej * note that the PDE#767 VA (0xbfc00000) is defined as "PTE_BASE"
150 1.40 thorpej * note that the PDE#1023 VA (0xffc00000) is defined as "APTE_BASE"
151 1.40 thorpej *
152 1.43 thorpej * starting at VA 0xbfc00000 the current active PDP (%cr3) acts as a
153 1.40 thorpej * PTP:
154 1.40 thorpej *
155 1.43 thorpej * PTP#767 == PDP(%cr3) => maps VA 0xbfc00000 -> 0xc0000000
156 1.40 thorpej * +----+
157 1.43 thorpej * | 0| -> maps the contents of PTP#0 at VA 0xbfc00000->0xbfc01000
158 1.40 thorpej * | |
159 1.40 thorpej * | |
160 1.81 junyoung * | 767| -> maps contents of PTP#767 (the PDP) at VA 0xbfeff000
161 1.43 thorpej * | 768| -> maps contents of first kernel PTP
162 1.40 thorpej * | |
163 1.40 thorpej * |1023|
164 1.40 thorpej * +----+
165 1.40 thorpej *
166 1.81 junyoung * note that mapping of the PDP at PTP#767's VA (0xbfeff000) is
167 1.40 thorpej * defined as "PDP_BASE".... within that mapping there are two
168 1.41 chs * defines:
169 1.59 chs * "PDP_PDE" (0xbfeffbfc) is the VA of the PDE in the PDP
170 1.41 chs * which points back to itself.
171 1.59 chs * "APDP_PDE" (0xbfeffffc) is the VA of the PDE in the PDP which
172 1.40 thorpej * establishes the recursive mapping of the alternate pmap.
173 1.40 thorpej * to set the alternate PDP, one just has to put the correct
174 1.40 thorpej * PA info in *APDP_PDE.
175 1.40 thorpej *
176 1.41 chs * note that in the APTE_BASE space, the APDP appears at VA
177 1.40 thorpej * "APDP_BASE" (0xfffff000).
178 1.1 cgd */
179 1.65 fvdl /* XXX MP should we allocate one APDP_PDE per processor?? */
180 1.33 mrg
181 1.33 mrg /*
182 1.90.2.1 yamt * Mask to get rid of the sign-extended part of addresses.
183 1.90.2.1 yamt */
184 1.90.2.1 yamt #define VA_SIGN_MASK 0
185 1.90.2.1 yamt #define VA_SIGN_NEG(va) ((va) | VA_SIGN_MASK)
186 1.90.2.1 yamt /*
187 1.90.2.1 yamt * XXXfvdl this one's not right.
188 1.90.2.1 yamt */
189 1.90.2.1 yamt #define VA_SIGN_POS(va) ((va) & ~VA_SIGN_MASK)
190 1.90.2.1 yamt
191 1.90.2.1 yamt /*
192 1.40 thorpej * the following defines identify the slots used as described above.
193 1.33 mrg */
194 1.33 mrg
195 1.90.2.1 yamt #define L2_SLOT_PTE (KERNBASE/NBPD_L2-1) /* 767: for recursive PDP map */
196 1.90.2.1 yamt #define L2_SLOT_KERN (KERNBASE/NBPD_L2) /* 768: start of kernel space */
197 1.90.2.1 yamt #define L2_SLOT_KERNBASE L2_SLOT_KERN
198 1.90.2.1 yamt #define L2_SLOT_APTE 1023 /* 1023: alternative recursive slot */
199 1.90.2.1 yamt
200 1.90.2.1 yamt #define PDIR_SLOT_KERN L2_SLOT_KERN
201 1.90.2.1 yamt #define PDIR_SLOT_PTE L2_SLOT_PTE
202 1.90.2.1 yamt #define PDIR_SLOT_APTE L2_SLOT_APTE
203 1.1 cgd
204 1.1 cgd /*
205 1.41 chs * the following defines give the virtual addresses of various MMU
206 1.40 thorpej * data structures:
207 1.40 thorpej * PTE_BASE and APTE_BASE: the base VA of the linear PTE mappings
208 1.81 junyoung * PDP_BASE and APDP_BASE: the base VA of the recursive mapping of the PDP
209 1.40 thorpej * PDP_PDE and APDP_PDE: the VA of the PDE that points back to the PDP/APDP
210 1.1 cgd */
211 1.29 fvdl
212 1.90.2.1 yamt #define PTE_BASE ((pt_entry_t *) (L2_SLOT_PTE * NBPD_L2))
213 1.90.2.1 yamt #define APTE_BASE ((pt_entry_t *) (VA_SIGN_NEG((L2_SLOT_APTE * NBPD_L2))))
214 1.90.2.1 yamt
215 1.90.2.1 yamt #define L1_BASE PTE_BASE
216 1.90.2.1 yamt #define AL1_BASE APTE_BASE
217 1.90.2.1 yamt
218 1.90.2.1 yamt #define L2_BASE ((pd_entry_t *)((char *)L1_BASE + L2_SLOT_PTE * NBPD_L1))
219 1.90.2.1 yamt
220 1.90.2.1 yamt #define AL2_BASE ((pd_entry_t *)((char *)AL1_BASE + L2_SLOT_PTE * NBPD_L1))
221 1.90.2.1 yamt
222 1.90.2.1 yamt #define PDP_PDE (L2_BASE + PDIR_SLOT_PTE)
223 1.90.2.1 yamt #define APDP_PDE (L2_BASE + PDIR_SLOT_APTE)
224 1.90.2.1 yamt
225 1.90.2.1 yamt #define PDP_BASE L2_BASE
226 1.90.2.1 yamt #define APDP_BASE AL2_BASE
227 1.90.2.1 yamt
228 1.90.2.1 yamt /* largest value (-1 for APTP space) */
229 1.90.2.1 yamt #define NKL2_MAX_ENTRIES (NTOPLEVEL_PDES - (KERNBASE/NBPD_L2) - 1)
230 1.90.2.1 yamt #define NKL1_MAX_ENTRIES (unsigned long)(NKL2_MAX_ENTRIES * NPDPG)
231 1.90.2.1 yamt
232 1.90.2.2 yamt #define NKL2_KIMG_ENTRIES 0 /* XXX unused */
233 1.90.2.1 yamt
234 1.90.2.2 yamt #define NKL2_START_ENTRIES 0 /* XXX computed on runtime */
235 1.90.2.2 yamt #define NKL1_START_ENTRIES 0 /* XXX unused */
236 1.40 thorpej
237 1.90.2.1 yamt #define NTOPLEVEL_PDES (PAGE_SIZE / (sizeof (pd_entry_t)))
238 1.90.2.1 yamt
239 1.90.2.1 yamt #define NPDPG (PAGE_SIZE / sizeof (pd_entry_t))
240 1.90.2.1 yamt
241 1.90.2.1 yamt #define ptei(VA) (((VA_SIGN_POS(VA)) & L1_MASK) >> L1_SHIFT)
242 1.1 cgd
243 1.1 cgd /*
244 1.90.2.1 yamt * pl*_pi: index in the ptp page for a pde mapping a VA.
245 1.90.2.1 yamt * (pl*_i below is the index in the virtual array of all pdes per level)
246 1.1 cgd */
247 1.90.2.1 yamt #define pl1_pi(VA) (((VA_SIGN_POS(VA)) & L1_MASK) >> L1_SHIFT)
248 1.90.2.1 yamt #define pl2_pi(VA) (((VA_SIGN_POS(VA)) & L2_MASK) >> L2_SHIFT)
249 1.90.2.1 yamt #define pl3_pi(VA) (((VA_SIGN_POS(VA)) & L3_MASK) >> L3_SHIFT)
250 1.90.2.1 yamt #define pl4_pi(VA) (((VA_SIGN_POS(VA)) & L4_MASK) >> L4_SHIFT)
251 1.90.2.1 yamt
252 1.90.2.1 yamt /*
253 1.90.2.1 yamt * pl*_i: generate index into pde/pte arrays in virtual space
254 1.90.2.1 yamt */
255 1.90.2.1 yamt #define pl1_i(VA) (((VA_SIGN_POS(VA)) & L1_FRAME) >> L1_SHIFT)
256 1.90.2.1 yamt #define pl2_i(VA) (((VA_SIGN_POS(VA)) & L2_FRAME) >> L2_SHIFT)
257 1.90.2.1 yamt #define pl3_i(VA) (((VA_SIGN_POS(VA)) & L3_FRAME) >> L3_SHIFT)
258 1.90.2.1 yamt #define pl4_i(VA) (((VA_SIGN_POS(VA)) & L4_FRAME) >> L4_SHIFT)
259 1.90.2.1 yamt #define pl_i(va, lvl) \
260 1.90.2.1 yamt (((VA_SIGN_POS(va)) & ptp_masks[(lvl)-1]) >> ptp_shifts[(lvl)-1])
261 1.90.2.1 yamt
262 1.90.2.1 yamt #define PTP_MASK_INITIALIZER { L1_FRAME, L2_FRAME }
263 1.90.2.1 yamt #define PTP_SHIFT_INITIALIZER { L1_SHIFT, L2_SHIFT }
264 1.90.2.1 yamt #define NKPTP_INITIALIZER { NKL1_START_ENTRIES, NKL2_START_ENTRIES }
265 1.90.2.1 yamt #define NKPTPMAX_INITIALIZER { NKL1_MAX_ENTRIES, NKL2_MAX_ENTRIES }
266 1.90.2.1 yamt #define NBPD_INITIALIZER { NBPD_L1, NBPD_L2 }
267 1.90.2.1 yamt #define PDES_INITIALIZER { L2_BASE }
268 1.90.2.1 yamt #define APDES_INITIALIZER { AL2_BASE }
269 1.1 cgd
270 1.1 cgd /*
271 1.40 thorpej * PTP macros:
272 1.40 thorpej * a PTP's index is the PD index of the PDE that points to it
273 1.40 thorpej * a PTP's offset is the byte-offset in the PTE space that this PTP is at
274 1.40 thorpej * a PTP's VA is the first VA mapped by that PTP
275 1.1 cgd */
276 1.39 thorpej
277 1.90.2.1 yamt #define ptp_va2o(va, lvl) (pl_i(va, (lvl)+1) * PAGE_SIZE)
278 1.90.2.1 yamt
279 1.90.2.1 yamt #define PTP_LEVELS 2
280 1.39 thorpej
281 1.40 thorpej /*
282 1.40 thorpej * PG_AVAIL usage: we make use of the ignored bits of the PTE
283 1.40 thorpej */
284 1.40 thorpej
285 1.40 thorpej #define PG_W PG_AVAIL1 /* "wired" mapping */
286 1.40 thorpej #define PG_PVLIST PG_AVAIL2 /* mapping has entry on pvlist */
287 1.75 chs #define PG_X PG_AVAIL3 /* executable mapping */
288 1.40 thorpej
289 1.65 fvdl /*
290 1.65 fvdl * Number of PTE's per cache line. 4 byte pte, 32-byte cache line
291 1.65 fvdl * Used to avoid false sharing of cache lines.
292 1.65 fvdl */
293 1.90.2.1 yamt #define NPTECL 8
294 1.65 fvdl
295 1.40 thorpej #ifdef _KERNEL
296 1.40 thorpej /*
297 1.40 thorpej * pmap data structures: see pmap.c for details of locking.
298 1.40 thorpej */
299 1.40 thorpej
300 1.40 thorpej struct pmap;
301 1.40 thorpej typedef struct pmap *pmap_t;
302 1.40 thorpej
303 1.40 thorpej /*
304 1.40 thorpej * we maintain a list of all non-kernel pmaps
305 1.40 thorpej */
306 1.40 thorpej
307 1.40 thorpej LIST_HEAD(pmap_head, pmap); /* struct pmap_head: head of a pmap list */
308 1.40 thorpej
309 1.40 thorpej /*
310 1.40 thorpej * the pmap structure
311 1.40 thorpej *
312 1.40 thorpej * note that the pm_obj contains the simple_lock, the reference count,
313 1.40 thorpej * page list, and number of PTPs within the pmap.
314 1.65 fvdl *
315 1.65 fvdl * XXX If we ever support processor numbers higher than 31, we'll have
316 1.65 fvdl * XXX to rethink the CPU mask.
317 1.40 thorpej */
318 1.40 thorpej
319 1.40 thorpej struct pmap {
320 1.90.2.1 yamt struct uvm_object pm_obj[PTP_LEVELS-1]; /* objects for lvl >= 1) */
321 1.90.2.1 yamt #define pm_lock pm_obj[0].vmobjlock
322 1.90.2.1 yamt #define pm_obj_l1 pm_obj[0]
323 1.90.2.1 yamt #define pm_obj_l2 pm_obj[1]
324 1.90.2.1 yamt #define pm_obj_l3 pm_obj[2]
325 1.41 chs LIST_ENTRY(pmap) pm_list; /* list (lck by pm_list lock) */
326 1.41 chs pd_entry_t *pm_pdir; /* VA of PD (lck by object lock) */
327 1.90.2.1 yamt paddr_t pm_pdirpa; /* PA of PD (read-only after create) */
328 1.90.2.1 yamt struct vm_page *pm_ptphint[PTP_LEVELS-1];
329 1.90.2.1 yamt /* pointer to a PTP in our pmap */
330 1.41 chs struct pmap_statistics pm_stats; /* pmap stats (lck by object lock) */
331 1.41 chs
332 1.75 chs vaddr_t pm_hiexec; /* highest executable mapping */
333 1.41 chs int pm_flags; /* see below */
334 1.41 chs
335 1.41 chs union descriptor *pm_ldt; /* user-set LDT */
336 1.41 chs int pm_ldt_len; /* number of LDT entries */
337 1.41 chs int pm_ldt_sel; /* LDT selector */
338 1.85 perry uint32_t pm_cpus; /* mask of CPUs using pmap */
339 1.90 ad uint32_t pm_kernel_cpus; /* mask of CPUs using kernel part
340 1.90 ad of pmap */
341 1.40 thorpej };
342 1.1 cgd
343 1.39 thorpej /* pm_flags */
344 1.39 thorpej #define PMF_USER_LDT 0x01 /* pmap has user-set LDT */
345 1.39 thorpej
346 1.1 cgd /*
347 1.40 thorpej * for each managed physical page we maintain a list of <PMAP,VA>'s
348 1.41 chs * which it is mapped at. the list is headed by a pv_head structure.
349 1.40 thorpej * there is one pv_head per managed phys page (allocated at boot time).
350 1.40 thorpej * the pv_head structure points to a list of pv_entry structures (each
351 1.40 thorpej * describes one mapping).
352 1.1 cgd */
353 1.40 thorpej
354 1.41 chs struct pv_entry { /* locked by its list's pvh_lock */
355 1.77 chs SPLAY_ENTRY(pv_entry) pv_node; /* splay-tree node */
356 1.41 chs struct pmap *pv_pmap; /* the pmap */
357 1.41 chs vaddr_t pv_va; /* the virtual address */
358 1.41 chs struct vm_page *pv_ptp; /* the vm_page of the PTP */
359 1.90 ad struct pmap_cpu *pv_alloc_cpu; /* CPU allocated from */
360 1.11 mycroft };
361 1.11 mycroft
362 1.40 thorpej /*
363 1.40 thorpej * pv_entrys are dynamically allocated in chunks from a single page.
364 1.40 thorpej * we keep track of how many pv_entrys are in use for each page and
365 1.41 chs * we can free pv_entry pages if needed. there is one lock for the
366 1.40 thorpej * entire allocation system.
367 1.40 thorpej */
368 1.11 mycroft
369 1.11 mycroft struct pv_page_info {
370 1.41 chs TAILQ_ENTRY(pv_page) pvpi_list;
371 1.41 chs struct pv_entry *pvpi_pvfree;
372 1.41 chs int pvpi_nfree;
373 1.11 mycroft };
374 1.1 cgd
375 1.11 mycroft /*
376 1.40 thorpej * number of pv_entry's in a pv_page
377 1.40 thorpej * (note: won't work on systems where NPBG isn't a constant)
378 1.40 thorpej */
379 1.40 thorpej
380 1.71 thorpej #define PVE_PER_PVPAGE ((PAGE_SIZE - sizeof(struct pv_page_info)) / \
381 1.41 chs sizeof(struct pv_entry))
382 1.40 thorpej
383 1.40 thorpej /*
384 1.40 thorpej * a pv_page: where pv_entrys are allocated from
385 1.11 mycroft */
386 1.1 cgd
387 1.11 mycroft struct pv_page {
388 1.41 chs struct pv_page_info pvinfo;
389 1.41 chs struct pv_entry pvents[PVE_PER_PVPAGE];
390 1.40 thorpej };
391 1.40 thorpej
392 1.40 thorpej /*
393 1.40 thorpej * global kernel variables
394 1.40 thorpej */
395 1.40 thorpej
396 1.82 junyoung /* PDPpaddr: is the physical address of the kernel's PDP */
397 1.82 junyoung extern u_long PDPpaddr;
398 1.40 thorpej
399 1.40 thorpej extern struct pmap kernel_pmap_store; /* kernel pmap */
400 1.40 thorpej extern int nkpde; /* current # of PDEs for kernel */
401 1.40 thorpej extern int pmap_pg_g; /* do we support PG_G? */
402 1.90.2.2 yamt extern long nkptp[PTP_LEVELS];
403 1.40 thorpej
404 1.40 thorpej /*
405 1.40 thorpej * macros
406 1.40 thorpej */
407 1.1 cgd
408 1.18 mycroft #define pmap_kernel() (&kernel_pmap_store)
409 1.1 cgd #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count)
410 1.50 is #define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count)
411 1.11 mycroft
412 1.65 fvdl #define pmap_clear_modify(pg) pmap_clear_attrs(pg, PG_M)
413 1.65 fvdl #define pmap_clear_reference(pg) pmap_clear_attrs(pg, PG_U)
414 1.65 fvdl #define pmap_copy(DP,SP,D,L,S)
415 1.40 thorpej #define pmap_is_modified(pg) pmap_test_attrs(pg, PG_M)
416 1.40 thorpej #define pmap_is_referenced(pg) pmap_test_attrs(pg, PG_U)
417 1.65 fvdl #define pmap_move(DP,SP,D,L,S)
418 1.69 fvdl #define pmap_phys_address(ppn) x86_ptob(ppn)
419 1.40 thorpej #define pmap_valid_entry(E) ((E) & PG_V) /* is PDE or PTE valid? */
420 1.40 thorpej
421 1.40 thorpej
422 1.40 thorpej /*
423 1.40 thorpej * prototypes
424 1.40 thorpej */
425 1.40 thorpej
426 1.78 junyoung void pmap_activate(struct lwp *);
427 1.78 junyoung void pmap_bootstrap(vaddr_t);
428 1.90.2.1 yamt bool pmap_clear_attrs(struct vm_page *, unsigned);
429 1.78 junyoung void pmap_deactivate(struct lwp *);
430 1.78 junyoung void pmap_page_remove (struct vm_page *);
431 1.78 junyoung void pmap_remove(struct pmap *, vaddr_t, vaddr_t);
432 1.90.2.1 yamt bool pmap_test_attrs(struct vm_page *, unsigned);
433 1.78 junyoung void pmap_write_protect(struct pmap *, vaddr_t, vaddr_t, vm_prot_t);
434 1.75 chs int pmap_exec_fixup(struct vm_map *, struct trapframe *,
435 1.75 chs struct pcb *);
436 1.79 yamt void pmap_load(void);
437 1.40 thorpej
438 1.78 junyoung vaddr_t reserve_dumppages(vaddr_t); /* XXX: not a pmap fn */
439 1.40 thorpej
440 1.90 ad void pmap_tlb_shootdown(pmap_t, vaddr_t, vaddr_t, pt_entry_t);
441 1.90 ad void pmap_tlb_shootwait(void);
442 1.65 fvdl
443 1.40 thorpej #define PMAP_GROWKERNEL /* turn on pmap_growkernel interface */
444 1.44 thorpej
445 1.44 thorpej /*
446 1.44 thorpej * Do idle page zero'ing uncached to avoid polluting the cache.
447 1.44 thorpej */
448 1.89 thorpej bool pmap_pageidlezero(paddr_t);
449 1.56 thorpej #define PMAP_PAGEIDLEZERO(pa) pmap_pageidlezero((pa))
450 1.40 thorpej
451 1.40 thorpej /*
452 1.40 thorpej * inline functions
453 1.40 thorpej */
454 1.63 chs
455 1.66 perry /*ARGSUSED*/
456 1.86 perry static __inline void
457 1.88 christos pmap_remove_all(struct pmap *pmap)
458 1.63 chs {
459 1.63 chs /* Nothing. */
460 1.63 chs }
461 1.40 thorpej
462 1.40 thorpej /*
463 1.40 thorpej * pmap_update_pg: flush one page from the TLB (or flush the whole thing
464 1.40 thorpej * if hardware doesn't support one-page flushing)
465 1.40 thorpej */
466 1.40 thorpej
467 1.86 perry __inline static void __attribute__((__unused__))
468 1.62 thorpej pmap_update_pg(vaddr_t va)
469 1.11 mycroft {
470 1.40 thorpej #if defined(I386_CPU)
471 1.41 chs if (cpu_class == CPUCLASS_386)
472 1.52 thorpej tlbflush();
473 1.41 chs else
474 1.40 thorpej #endif
475 1.41 chs invlpg((u_int) va);
476 1.11 mycroft }
477 1.11 mycroft
478 1.40 thorpej /*
479 1.40 thorpej * pmap_update_2pg: flush two pages from the TLB
480 1.40 thorpej */
481 1.40 thorpej
482 1.86 perry __inline static void __attribute__((__unused__))
483 1.62 thorpej pmap_update_2pg(vaddr_t va, vaddr_t vb)
484 1.11 mycroft {
485 1.40 thorpej #if defined(I386_CPU)
486 1.41 chs if (cpu_class == CPUCLASS_386)
487 1.52 thorpej tlbflush();
488 1.41 chs else
489 1.40 thorpej #endif
490 1.41 chs {
491 1.41 chs invlpg((u_int) va);
492 1.41 chs invlpg((u_int) vb);
493 1.41 chs }
494 1.11 mycroft }
495 1.11 mycroft
496 1.40 thorpej /*
497 1.40 thorpej * pmap_page_protect: change the protection of all recorded mappings
498 1.40 thorpej * of a managed page
499 1.40 thorpej *
500 1.65 fvdl * => this function is a frontend for pmap_page_remove/pmap_clear_attrs
501 1.40 thorpej * => we only have to worry about making the page more protected.
502 1.40 thorpej * unprotecting a page is done on-demand at fault time.
503 1.40 thorpej */
504 1.40 thorpej
505 1.86 perry __inline static void __attribute__((__unused__))
506 1.62 thorpej pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
507 1.11 mycroft {
508 1.41 chs if ((prot & VM_PROT_WRITE) == 0) {
509 1.41 chs if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
510 1.65 fvdl (void) pmap_clear_attrs(pg, PG_RW);
511 1.41 chs } else {
512 1.41 chs pmap_page_remove(pg);
513 1.41 chs }
514 1.41 chs }
515 1.11 mycroft }
516 1.11 mycroft
517 1.40 thorpej /*
518 1.40 thorpej * pmap_protect: change the protection of pages in a pmap
519 1.40 thorpej *
520 1.40 thorpej * => this function is a frontend for pmap_remove/pmap_write_protect
521 1.40 thorpej * => we only have to worry about making the page more protected.
522 1.40 thorpej * unprotecting a page is done on-demand at fault time.
523 1.40 thorpej */
524 1.40 thorpej
525 1.86 perry __inline static void __attribute__((__unused__))
526 1.62 thorpej pmap_protect(struct pmap *pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
527 1.11 mycroft {
528 1.41 chs if ((prot & VM_PROT_WRITE) == 0) {
529 1.41 chs if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
530 1.41 chs pmap_write_protect(pmap, sva, eva, prot);
531 1.41 chs } else {
532 1.41 chs pmap_remove(pmap, sva, eva);
533 1.41 chs }
534 1.41 chs }
535 1.47 thorpej }
536 1.47 thorpej
537 1.47 thorpej /*
538 1.47 thorpej * various address inlines
539 1.47 thorpej *
540 1.47 thorpej * vtopte: return a pointer to the PTE mapping a VA, works only for
541 1.47 thorpej * user and PT addresses
542 1.47 thorpej *
543 1.47 thorpej * kvtopte: return a pointer to the PTE mapping a kernel VA
544 1.47 thorpej */
545 1.47 thorpej
546 1.47 thorpej #include <lib/libkern/libkern.h>
547 1.47 thorpej
548 1.86 perry static __inline pt_entry_t * __attribute__((__unused__))
549 1.47 thorpej vtopte(vaddr_t va)
550 1.47 thorpej {
551 1.47 thorpej
552 1.90.2.1 yamt KASSERT(va < (L2_SLOT_KERN * NBPD_L2));
553 1.47 thorpej
554 1.90.2.1 yamt return (PTE_BASE + pl1_i(va));
555 1.47 thorpej }
556 1.47 thorpej
557 1.86 perry static __inline pt_entry_t * __attribute__((__unused__))
558 1.47 thorpej kvtopte(vaddr_t va)
559 1.47 thorpej {
560 1.90.2.3 yamt pd_entry_t *pde;
561 1.47 thorpej
562 1.90.2.1 yamt KASSERT(va >= (L2_SLOT_KERN * NBPD_L2));
563 1.48 thorpej
564 1.48 thorpej
565 1.90.2.3 yamt pde = L2_BASE + pl2_i(va);
566 1.90.2.3 yamt if (*pde & PG_PS)
567 1.90.2.3 yamt return ((pt_entry_t *)pde);
568 1.47 thorpej
569 1.90.2.1 yamt return (PTE_BASE + pl1_i(va));
570 1.41 chs }
571 1.70 fvdl
572 1.90 ad #define pmap_pte_set(p, n) x86_atomic_testset_ul(p, n)
573 1.90 ad #define pmap_pte_setbits(p, b) x86_atomic_setbits_l(p, b)
574 1.90 ad #define pmap_pte_clearbits(p, b) x86_atomic_clearbits_l(p, b)
575 1.70 fvdl #define pmap_cpu_has_pg_n() (cpu_class != CPUCLASS_386)
576 1.70 fvdl #define pmap_cpu_has_invlpg() (cpu_class != CPUCLASS_386)
577 1.35 cgd
578 1.78 junyoung paddr_t vtophys(vaddr_t);
579 1.78 junyoung vaddr_t pmap_map(vaddr_t, paddr_t, paddr_t, vm_prot_t);
580 1.78 junyoung void pmap_ldt_cleanup(struct lwp *);
581 1.90 ad void pmap_cpu_init_early(struct cpu_info *);
582 1.90 ad void pmap_cpu_init_late(struct cpu_info *);
583 1.90 ad void sse2_zero_page(void *);
584 1.90 ad void sse2_copy_page(void *, void *);
585 1.73 thorpej
586 1.83 junyoung /*
587 1.73 thorpej * Hooks for the pool allocator.
588 1.73 thorpej */
589 1.73 thorpej #define POOL_VTOPHYS(va) vtophys((vaddr_t) (va))
590 1.1 cgd
591 1.90 ad /*
592 1.90 ad * TLB shootdown mailbox.
593 1.90 ad */
594 1.90 ad
595 1.90 ad struct pmap_mbox {
596 1.90 ad volatile void *mb_pointer;
597 1.90 ad volatile uintptr_t mb_addr1;
598 1.90 ad volatile uintptr_t mb_addr2;
599 1.90 ad volatile uintptr_t mb_head;
600 1.90 ad volatile uintptr_t mb_tail;
601 1.90 ad volatile uintptr_t mb_global;
602 1.90 ad };
603 1.90 ad
604 1.40 thorpej #endif /* _KERNEL */
605 1.40 thorpej #endif /* _I386_PMAP_H_ */
606