| Commit | Line | Data |
|---|---|---|
| d7f50089 | 1 | /* |
| d7f50089 | 2 | * Copyright (c) 1991 Regents of the University of California. |
| d7f50089 | 3 | * Copyright (c) 1994 John S. Dyson |
| d7f50089 | 4 | * Copyright (c) 1994 David Greenman |
| 48ffc236 JG |
5 | * Copyright (c) 2003 Peter Wemm |
| 6 | * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu> | |
| 7 | * Copyright (c) 2008, 2009 The DragonFly Project. | |
| 8 | * Copyright (c) 2008, 2009 Jordan Gordeev. | |
| d7f50089 | 9 | * All rights reserved. |
| c8fe38ae MD |
10 | * |
| 11 | * This code is derived from software contributed to Berkeley by | |
| 12 | * the Systems Programming Group of the University of Utah Computer | |
| 13 | * Science Department and William Jolitz of UUNET Technologies Inc. | |
| 14 | * | |
| d7f50089 YY |
15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions | |
| 17 | * are met: | |
| d7f50089 YY |
18 | * 1. Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. | |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright | |
| c8fe38ae MD |
21 | * notice, this list of conditions and the following disclaimer in the |
| 22 | * documentation and/or other materials provided with the distribution. | |
| 23 | * 3. All advertising materials mentioning features or use of this software | |
| 24 | * must display the following acknowledgement: | |
| 25 | * This product includes software developed by the University of | |
| 26 | * California, Berkeley and its contributors. | |
| 27 | * 4. Neither the name of the University nor the names of its contributors | |
| 28 | * may be used to endorse or promote products derived from this software | |
| 29 | * without specific prior written permission. | |
| 30 | * | |
| 31 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 32 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 33 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 34 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 35 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 37 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 39 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 40 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| d7f50089 | 41 | * SUCH DAMAGE. |
| c8fe38ae MD |
42 | * |
| 43 | * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91 | |
| d7f50089 | 44 | * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $ |
| d7f50089 | 45 | */ |
| c8fe38ae | 46 | |
| d7f50089 | 47 | /* |
| c8fe38ae MD |
48 | * Manages physical address maps. |
| 49 | * | |
| 50 | * In addition to hardware address maps, this | |
| 51 | * module is called upon to provide software-use-only | |
| 52 | * maps which may or may not be stored in the same | |
| 53 | * form as hardware maps. These pseudo-maps are | |
| 54 | * used to store intermediate results from copy | |
| 55 | * operations to and from address spaces. | |
| 56 | * | |
| 57 | * Since the information managed by this module is | |
| 58 | * also stored by the logical address mapping module, | |
| 59 | * this module may throw away valid virtual-to-physical | |
| 60 | * mappings at almost any time. However, invalidations | |
| 61 | * of virtual-to-physical mappings must be done as | |
| 62 | * requested. | |
| 63 | * | |
| 64 | * In order to cope with hardware architectures which | |
| 65 | * make virtual-to-physical map invalidates expensive, | |
| 66 | * this module may delay invalidate or reduced protection | |
| 67 | * operations until such time as they are actually | |
| 68 | * necessary. This module is given full information as | |
| 69 | * to which processors are currently using which maps, | |
| 70 | * and to when physical maps must be made correct. | |
| 71 | */ | |
| 72 | ||
| 73 | #if JG | |
| 74 | #include "opt_disable_pse.h" | |
| 75 | #include "opt_pmap.h" | |
| 76 | #endif | |
| 77 | #include "opt_msgbuf.h" | |
| d7f50089 | 78 | |
| c8fe38ae | 79 | #include <sys/param.h> |
| d7f50089 YY |
80 | #include <sys/systm.h> |
| 81 | #include <sys/kernel.h> | |
| d7f50089 | 82 | #include <sys/proc.h> |
| c8fe38ae MD |
83 | #include <sys/msgbuf.h> |
| 84 | #include <sys/vmmeter.h> | |
| 85 | #include <sys/mman.h> | |
| d7f50089 | 86 | |
| c8fe38ae MD |
87 | #include <vm/vm.h> |
| 88 | #include <vm/vm_param.h> | |
| 89 | #include <sys/sysctl.h> | |
| 90 | #include <sys/lock.h> | |
| d7f50089 | 91 | #include <vm/vm_kern.h> |
| c8fe38ae MD |
92 | #include <vm/vm_page.h> |
| 93 | #include <vm/vm_map.h> | |
| d7f50089 | 94 | #include <vm/vm_object.h> |
| c8fe38ae | 95 | #include <vm/vm_extern.h> |
| d7f50089 | 96 | #include <vm/vm_pageout.h> |
| c8fe38ae MD |
97 | #include <vm/vm_pager.h> |
| 98 | #include <vm/vm_zone.h> | |
| 99 | ||
| 100 | #include <sys/user.h> | |
| 101 | #include <sys/thread2.h> | |
| 102 | #include <sys/sysref2.h> | |
| d7f50089 | 103 | |
| c8fe38ae | 104 | #include <machine/cputypes.h> |
| d7f50089 | 105 | #include <machine/md_var.h> |
| c8fe38ae MD |
106 | #include <machine/specialreg.h> |
| 107 | #include <machine/smp.h> | |
| 108 | #include <machine_base/apic/apicreg.h> | |
| d7f50089 | 109 | #include <machine/globaldata.h> |
| c8fe38ae MD |
110 | #include <machine/pmap.h> |
| 111 | #include <machine/pmap_inval.h> | |
| 112 | ||
| 48ffc236 JG |
113 | #include <ddb/ddb.h> |
| 114 | ||
| c8fe38ae MD |
115 | #define PMAP_KEEP_PDIRS |
| 116 | #ifndef PMAP_SHPGPERPROC | |
| 117 | #define PMAP_SHPGPERPROC 200 | |
| 118 | #endif | |
| 119 | ||
| 120 | #if defined(DIAGNOSTIC) | |
| 121 | #define PMAP_DIAGNOSTIC | |
| 122 | #endif | |
| 123 | ||
| 124 | #define MINPV 2048 | |
| 125 | ||
| c8fe38ae MD |
126 | /* |
| 127 | * Get PDEs and PTEs for user/kernel address space | |
| 128 | */ | |
| 48ffc236 | 129 | static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va); |
| c8fe38ae MD |
130 | #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT]) |
| 131 | ||
| 132 | #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0) | |
| 133 | #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0) | |
| 134 | #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0) | |
| 135 | #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0) | |
| 136 | #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0) | |
| 137 | ||
| 138 | ||
| 139 | /* | |
| 140 | * Given a map and a machine independent protection code, | |
| 141 | * convert to a vax protection code. | |
| 142 | */ | |
| 143 | #define pte_prot(m, p) \ | |
| 144 | (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)]) | |
| 145 | static int protection_codes[8]; | |
| d7f50089 YY |
146 | |
| 147 | struct pmap kernel_pmap; | |
| c8fe38ae | 148 | static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list); |
| d7f50089 | 149 | |
| c8fe38ae MD |
150 | vm_paddr_t avail_start; /* PA of first available physical page */ |
| 151 | vm_paddr_t avail_end; /* PA of last available physical page */ | |
| 152 | vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */ | |
| 153 | vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */ | |
| 154 | vm_offset_t KvaStart; /* VA start of KVA space */ | |
| 155 | vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */ | |
| 156 | vm_offset_t KvaSize; /* max size of kernel virtual address space */ | |
| 157 | static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */ | |
| 158 | static int pgeflag; /* PG_G or-in */ | |
| 159 | static int pseflag; /* PG_PS or-in */ | |
| d7f50089 | 160 | |
| c8fe38ae MD |
161 | static vm_object_t kptobj; |
| 162 | ||
| 48ffc236 JG |
163 | static int ndmpdp; |
| 164 | static vm_paddr_t dmaplimit; | |
| c8fe38ae MD |
165 | static int nkpt; |
| 166 | vm_offset_t kernel_vm_end; | |
| d7f50089 | 167 | |
| 48ffc236 JG |
168 | static uint64_t KPDphys; /* phys addr of kernel level 2 */ |
| 169 | uint64_t KPDPphys; /* phys addr of kernel level 3 */ | |
| 170 | uint64_t KPML4phys; /* phys addr of kernel level 4 */ | |
| 171 | ||
| 172 | static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */ | |
| 173 | static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */ | |
| 174 | ||
| d7f50089 | 175 | /* |
| c8fe38ae | 176 | * Data for the pv entry allocation mechanism |
| d7f50089 | 177 | */ |
| c8fe38ae MD |
178 | static vm_zone_t pvzone; |
| 179 | static struct vm_zone pvzone_store; | |
| 180 | static struct vm_object pvzone_obj; | |
| 181 | static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0; | |
| 182 | static int pmap_pagedaemon_waken = 0; | |
| 183 | static struct pv_entry *pvinit; | |
| d7f50089 YY |
184 | |
| 185 | /* | |
| c8fe38ae | 186 | * All those kernel PT submaps that BSD is so fond of |
| d7f50089 | 187 | */ |
| c8fe38ae MD |
188 | pt_entry_t *CMAP1 = 0, *ptmmap; |
| 189 | caddr_t CADDR1 = 0, ptvmmap = 0; | |
| 190 | static pt_entry_t *msgbufmap; | |
| 191 | struct msgbuf *msgbufp=0; | |
| d7f50089 | 192 | |
| c8fe38ae MD |
193 | /* |
| 194 | * Crashdump maps. | |
| d7f50089 | 195 | */ |
| c8fe38ae MD |
196 | static pt_entry_t *pt_crashdumpmap; |
| 197 | static caddr_t crashdumpmap; | |
| 198 | ||
| 199 | extern uint64_t KPTphys; | |
| 200 | extern pt_entry_t *SMPpt; | |
| 201 | extern uint64_t SMPptpa; | |
| 202 | ||
| 203 | #define DISABLE_PSE | |
| 204 | ||
| c8fe38ae | 205 | static pv_entry_t get_pv_entry (void); |
| bfc09ba0 MD |
206 | static void i386_protection_init (void); |
| 207 | static void create_pagetables(vm_paddr_t *firstaddr); | |
| 208 | static void pmap_remove_all (vm_page_t m); | |
| 209 | static void pmap_enter_quick (pmap_t pmap, vm_offset_t va, vm_page_t m); | |
| 210 | static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq, | |
| c8fe38ae MD |
211 | vm_offset_t sva, pmap_inval_info_t info); |
| 212 | static void pmap_remove_page (struct pmap *pmap, | |
| 213 | vm_offset_t va, pmap_inval_info_t info); | |
| bfc09ba0 | 214 | static int pmap_remove_entry (struct pmap *pmap, vm_page_t m, |
| c8fe38ae MD |
215 | vm_offset_t va, pmap_inval_info_t info); |
| 216 | static boolean_t pmap_testbit (vm_page_t m, int bit); | |
| 217 | static void pmap_insert_entry (pmap_t pmap, vm_offset_t va, | |
| bfc09ba0 | 218 | vm_page_t mpte, vm_page_t m); |
| c8fe38ae MD |
219 | |
| 220 | static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va); | |
| 221 | ||
| 222 | static int pmap_release_free_page (pmap_t pmap, vm_page_t p); | |
| 223 | static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex); | |
| 224 | static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va); | |
| 225 | static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex); | |
| bfc09ba0 MD |
226 | static int _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, |
| 227 | pmap_inval_info_t info); | |
| c8fe38ae MD |
228 | static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t); |
| 229 | static vm_offset_t pmap_kmem_choose(vm_offset_t addr); | |
| 230 | ||
| 231 | static unsigned pdir4mb; | |
| d7f50089 YY |
232 | |
| 233 | /* | |
| c8fe38ae | 234 | * Move the kernel virtual free pointer to the next |
| f9cc0f15 JG |
235 | * 2MB. This is used to help improve performance |
| 236 | * by using a large (2MB) page for much of the kernel | |
| c8fe38ae | 237 | * (.text, .data, .bss) |
| d7f50089 | 238 | */ |
| bfc09ba0 MD |
239 | static |
| 240 | vm_offset_t | |
| c8fe38ae | 241 | pmap_kmem_choose(vm_offset_t addr) |
| d7f50089 | 242 | { |
| c8fe38ae | 243 | vm_offset_t newaddr = addr; |
| f9cc0f15 JG |
244 | |
| 245 | newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1); | |
| c8fe38ae | 246 | return newaddr; |
| d7f50089 YY |
247 | } |
| 248 | ||
| d7f50089 | 249 | /* |
| c8fe38ae | 250 | * pmap_pte_quick: |
| d7f50089 | 251 | * |
| c8fe38ae MD |
252 | * Super fast pmap_pte routine best used when scanning the pv lists. |
| 253 | * This eliminates many course-grained invltlb calls. Note that many of | |
| 254 | * the pv list scans are across different pmaps and it is very wasteful | |
| 255 | * to do an entire invltlb when checking a single mapping. | |
| 256 | * | |
| 257 | * Should only be called while in a critical section. | |
| 258 | */ | |
| 48ffc236 JG |
259 | static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va); |
| 260 | ||
| bfc09ba0 MD |
261 | static |
| 262 | pt_entry_t * | |
| c8fe38ae MD |
263 | pmap_pte_quick(pmap_t pmap, vm_offset_t va) |
| 264 | { | |
| 48ffc236 JG |
265 | return pmap_pte(pmap, va); |
| 266 | } | |
| 267 | ||
| 268 | /* Return a non-clipped PD index for a given VA */ | |
| bfc09ba0 MD |
269 | static __inline |
| 270 | vm_pindex_t | |
| 48ffc236 | 271 | pmap_pde_pindex(vm_offset_t va) |
| 48ffc236 JG |
272 | { |
| 273 | return va >> PDRSHIFT; | |
| 274 | } | |
| 275 | ||
| 276 | /* Return various clipped indexes for a given VA */ | |
| bfc09ba0 MD |
277 | static __inline |
| 278 | vm_pindex_t | |
| 48ffc236 | 279 | pmap_pte_index(vm_offset_t va) |
| 48ffc236 JG |
280 | { |
| 281 | ||
| 282 | return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1)); | |
| 283 | } | |
| 284 | ||
| bfc09ba0 MD |
285 | static __inline |
| 286 | vm_pindex_t | |
| 48ffc236 | 287 | pmap_pde_index(vm_offset_t va) |
| 48ffc236 JG |
288 | { |
| 289 | ||
| 290 | return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1)); | |
| 291 | } | |
| 292 | ||
| bfc09ba0 MD |
293 | static __inline |
| 294 | vm_pindex_t | |
| 48ffc236 | 295 | pmap_pdpe_index(vm_offset_t va) |
| 48ffc236 JG |
296 | { |
| 297 | ||
| 298 | return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1)); | |
| 299 | } | |
| 300 | ||
| bfc09ba0 MD |
301 | static __inline |
| 302 | vm_pindex_t | |
| 48ffc236 | 303 | pmap_pml4e_index(vm_offset_t va) |
| 48ffc236 JG |
304 | { |
| 305 | ||
| 306 | return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1)); | |
| 307 | } | |
| 308 | ||
| 309 | /* Return a pointer to the PML4 slot that corresponds to a VA */ | |
| bfc09ba0 MD |
310 | static __inline |
| 311 | pml4_entry_t * | |
| 48ffc236 | 312 | pmap_pml4e(pmap_t pmap, vm_offset_t va) |
| 48ffc236 JG |
313 | { |
| 314 | ||
| 315 | return (&pmap->pm_pml4[pmap_pml4e_index(va)]); | |
| 316 | } | |
| 317 | ||
| 318 | /* Return a pointer to the PDP slot that corresponds to a VA */ | |
| bfc09ba0 MD |
319 | static __inline |
| 320 | pdp_entry_t * | |
| 48ffc236 | 321 | pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va) |
| 48ffc236 JG |
322 | { |
| 323 | pdp_entry_t *pdpe; | |
| 324 | ||
| 325 | pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME); | |
| 326 | return (&pdpe[pmap_pdpe_index(va)]); | |
| 327 | } | |
| 328 | ||
| 329 | /* Return a pointer to the PDP slot that corresponds to a VA */ | |
| bfc09ba0 MD |
330 | static __inline |
| 331 | pdp_entry_t * | |
| 48ffc236 | 332 | pmap_pdpe(pmap_t pmap, vm_offset_t va) |
| 48ffc236 JG |
333 | { |
| 334 | pml4_entry_t *pml4e; | |
| 335 | ||
| 336 | pml4e = pmap_pml4e(pmap, va); | |
| 337 | if ((*pml4e & PG_V) == 0) | |
| 338 | return NULL; | |
| 339 | return (pmap_pml4e_to_pdpe(pml4e, va)); | |
| 340 | } | |
| 341 | ||
| 342 | /* Return a pointer to the PD slot that corresponds to a VA */ | |
| bfc09ba0 MD |
343 | static __inline |
| 344 | pd_entry_t * | |
| 48ffc236 | 345 | pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va) |
| 48ffc236 JG |
346 | { |
| 347 | pd_entry_t *pde; | |
| 348 | ||
| 349 | pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME); | |
| 350 | return (&pde[pmap_pde_index(va)]); | |
| 351 | } | |
| 352 | ||
| 353 | /* Return a pointer to the PD slot that corresponds to a VA */ | |
| bfc09ba0 MD |
354 | static __inline |
| 355 | pd_entry_t * | |
| 48ffc236 | 356 | pmap_pde(pmap_t pmap, vm_offset_t va) |
| 48ffc236 JG |
357 | { |
| 358 | pdp_entry_t *pdpe; | |
| 359 | ||
| 360 | pdpe = pmap_pdpe(pmap, va); | |
| 361 | if (pdpe == NULL || (*pdpe & PG_V) == 0) | |
| 362 | return NULL; | |
| 363 | return (pmap_pdpe_to_pde(pdpe, va)); | |
| 364 | } | |
| 365 | ||
| 366 | /* Return a pointer to the PT slot that corresponds to a VA */ | |
| bfc09ba0 MD |
367 | static __inline |
| 368 | pt_entry_t * | |
| 48ffc236 | 369 | pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va) |
| 48ffc236 JG |
370 | { |
| 371 | pt_entry_t *pte; | |
| 372 | ||
| 373 | pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME); | |
| 374 | return (&pte[pmap_pte_index(va)]); | |
| 375 | } | |
| 376 | ||
| 377 | /* Return a pointer to the PT slot that corresponds to a VA */ | |
| bfc09ba0 MD |
378 | static __inline |
| 379 | pt_entry_t * | |
| 48ffc236 | 380 | pmap_pte(pmap_t pmap, vm_offset_t va) |
| 48ffc236 JG |
381 | { |
| 382 | pd_entry_t *pde; | |
| 383 | ||
| 384 | pde = pmap_pde(pmap, va); | |
| 385 | if (pde == NULL || (*pde & PG_V) == 0) | |
| 386 | return NULL; | |
| 387 | if ((*pde & PG_PS) != 0) /* compat with i386 pmap_pte() */ | |
| 388 | return ((pt_entry_t *)pde); | |
| 389 | return (pmap_pde_to_pte(pde, va)); | |
| 390 | } | |
| 391 | ||
| bfc09ba0 MD |
392 | static __inline |
| 393 | pt_entry_t * | |
| 48ffc236 | 394 | vtopte(vm_offset_t va) |
| 48ffc236 JG |
395 | { |
| 396 | uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1); | |
| 397 | ||
| 398 | return (PTmap + ((va >> PAGE_SHIFT) & mask)); | |
| c8fe38ae | 399 | } |
| d7f50089 | 400 | |
| bfc09ba0 MD |
401 | static __inline |
| 402 | pd_entry_t * | |
| 48ffc236 | 403 | vtopde(vm_offset_t va) |
| 48ffc236 JG |
404 | { |
| 405 | uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1); | |
| 406 | ||
| 407 | return (PDmap + ((va >> PDRSHIFT) & mask)); | |
| 408 | } | |
| c8fe38ae | 409 | |
| 48ffc236 | 410 | static uint64_t |
| c8fe38ae | 411 | allocpages(vm_paddr_t *firstaddr, int n) |
| d7f50089 | 412 | { |
| 48ffc236 | 413 | uint64_t ret; |
| c8fe38ae MD |
414 | |
| 415 | ret = *firstaddr; | |
| 416 | bzero((void *)ret, n * PAGE_SIZE); | |
| 417 | *firstaddr += n * PAGE_SIZE; | |
| 418 | return (ret); | |
| d7f50089 YY |
419 | } |
| 420 | ||
| bfc09ba0 | 421 | static |
| c8fe38ae MD |
422 | void |
| 423 | create_pagetables(vm_paddr_t *firstaddr) | |
| 424 | { | |
| 425 | int i; | |
| c8fe38ae MD |
426 | |
| 427 | /* we are running (mostly) V=P at this point */ | |
| 428 | ||
| 48ffc236 JG |
429 | /* Allocate pages */ |
| 430 | KPTphys = allocpages(firstaddr, NKPT); | |
| 431 | KPML4phys = allocpages(firstaddr, 1); | |
| 432 | KPDPphys = allocpages(firstaddr, NKPML4E); | |
| 433 | KPDphys = allocpages(firstaddr, NKPDPE); | |
| 434 | ||
| 435 | ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT; | |
| 436 | if (ndmpdp < 4) /* Minimum 4GB of dirmap */ | |
| 437 | ndmpdp = 4; | |
| 438 | DMPDPphys = allocpages(firstaddr, NDMPML4E); | |
| 439 | if ((amd_feature & AMDID_PAGE1GB) == 0) | |
| 440 | DMPDphys = allocpages(firstaddr, ndmpdp); | |
| 441 | dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT; | |
| 442 | ||
| 443 | /* Fill in the underlying page table pages */ | |
| 444 | /* Read-only from zero to physfree */ | |
| 445 | /* XXX not fully used, underneath 2M pages */ | |
| 446 | for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) { | |
| 447 | ((pt_entry_t *)KPTphys)[i] = i << PAGE_SHIFT; | |
| 448 | ((pt_entry_t *)KPTphys)[i] |= PG_RW | PG_V | PG_G; | |
| 449 | } | |
| 450 | ||
| 451 | /* Now map the page tables at their location within PTmap */ | |
| 452 | for (i = 0; i < NKPT; i++) { | |
| 453 | ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT); | |
| 454 | ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V; | |
| 455 | } | |
| 456 | ||
| 457 | /* Map from zero to end of allocations under 2M pages */ | |
| 458 | /* This replaces some of the KPTphys entries above */ | |
| 459 | for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) { | |
| 460 | ((pd_entry_t *)KPDphys)[i] = i << PDRSHIFT; | |
| 461 | ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V | PG_PS | PG_G; | |
| 462 | } | |
| 463 | ||
| 464 | /* And connect up the PD to the PDP */ | |
| 465 | for (i = 0; i < NKPDPE; i++) { | |
| 466 | ((pdp_entry_t *)KPDPphys)[i + KPDPI] = KPDphys + | |
| 467 | (i << PAGE_SHIFT); | |
| 468 | ((pdp_entry_t *)KPDPphys)[i + KPDPI] |= PG_RW | PG_V | PG_U; | |
| 469 | } | |
| 470 | ||
| 471 | /* Now set up the direct map space using either 2MB or 1GB pages */ | |
| 472 | /* Preset PG_M and PG_A because demotion expects it */ | |
| 473 | if ((amd_feature & AMDID_PAGE1GB) == 0) { | |
| 474 | for (i = 0; i < NPDEPG * ndmpdp; i++) { | |
| 475 | ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT; | |
| 476 | ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS | | |
| 477 | PG_G | PG_M | PG_A; | |
| 478 | } | |
| 479 | /* And the direct map space's PDP */ | |
| 480 | for (i = 0; i < ndmpdp; i++) { | |
| 481 | ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys + | |
| 482 | (i << PAGE_SHIFT); | |
| 483 | ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U; | |
| 484 | } | |
| 485 | } else { | |
| 486 | for (i = 0; i < ndmpdp; i++) { | |
| 487 | ((pdp_entry_t *)DMPDPphys)[i] = | |
| 488 | (vm_paddr_t)i << PDPSHIFT; | |
| 489 | ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS | | |
| 490 | PG_G | PG_M | PG_A; | |
| 491 | } | |
| 492 | } | |
| 493 | ||
| 494 | /* And recursively map PML4 to itself in order to get PTmap */ | |
| 495 | ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys; | |
| 496 | ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U; | |
| 497 | ||
| 498 | /* Connect the Direct Map slot up to the PML4 */ | |
| 499 | ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys; | |
| 500 | ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U; | |
| 501 | ||
| 502 | /* Connect the KVA slot up to the PML4 */ | |
| 503 | ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys; | |
| 504 | ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U; | |
| c8fe38ae MD |
505 | } |
| 506 | ||
| 507 | void | |
| f81851b8 MD |
508 | init_paging(vm_paddr_t *firstaddr) |
| 509 | { | |
| c8fe38ae | 510 | create_pagetables(firstaddr); |
| d7f50089 YY |
511 | } |
| 512 | ||
| 513 | /* | |
| c8fe38ae MD |
514 | * Bootstrap the system enough to run with virtual memory. |
| 515 | * | |
| 516 | * On the i386 this is called after mapping has already been enabled | |
| 517 | * and just syncs the pmap module with what has already been done. | |
| 518 | * [We can't call it easily with mapping off since the kernel is not | |
| 519 | * mapped with PA == VA, hence we would have to relocate every address | |
| 520 | * from the linked base (virtual) address "KERNBASE" to the actual | |
| 521 | * (physical) address starting relative to 0] | |
| d7f50089 YY |
522 | */ |
| 523 | void | |
| 48ffc236 | 524 | pmap_bootstrap(vm_paddr_t *firstaddr) |
| c8fe38ae MD |
525 | { |
| 526 | vm_offset_t va; | |
| 527 | pt_entry_t *pte; | |
| 528 | struct mdglobaldata *gd; | |
| c8fe38ae MD |
529 | int pg; |
| 530 | ||
| 48ffc236 JG |
531 | KvaStart = VM_MIN_KERNEL_ADDRESS; |
| 532 | KvaEnd = VM_MAX_KERNEL_ADDRESS; | |
| 533 | KvaSize = KvaEnd - KvaStart; | |
| 534 | ||
| c8fe38ae MD |
535 | avail_start = *firstaddr; |
| 536 | ||
| 537 | /* | |
| 48ffc236 | 538 | * Create an initial set of page tables to run the kernel in. |
| c8fe38ae | 539 | */ |
| 48ffc236 JG |
540 | create_pagetables(firstaddr); |
| 541 | ||
| c8fe38ae MD |
542 | virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr; |
| 543 | virtual_start = pmap_kmem_choose(virtual_start); | |
| 48ffc236 JG |
544 | |
| 545 | virtual_end = VM_MAX_KERNEL_ADDRESS; | |
| 546 | ||
| 547 | /* XXX do %cr0 as well */ | |
| 548 | load_cr4(rcr4() | CR4_PGE | CR4_PSE); | |
| 549 | load_cr3(KPML4phys); | |
| c8fe38ae MD |
550 | |
| 551 | /* | |
| 552 | * Initialize protection array. | |
| 553 | */ | |
| 554 | i386_protection_init(); | |
| 555 | ||
| 556 | /* | |
| 557 | * The kernel's pmap is statically allocated so we don't have to use | |
| 558 | * pmap_create, which is unlikely to work correctly at this part of | |
| 559 | * the boot sequence (XXX and which no longer exists). | |
| 560 | */ | |
| 48ffc236 | 561 | kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys); |
| c8fe38ae MD |
562 | kernel_pmap.pm_count = 1; |
| 563 | kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */ | |
| 564 | TAILQ_INIT(&kernel_pmap.pm_pvlist); | |
| 565 | nkpt = NKPT; | |
| 566 | ||
| 567 | /* | |
| 568 | * Reserve some special page table entries/VA space for temporary | |
| 569 | * mapping of pages. | |
| 570 | */ | |
| 571 | #define SYSMAP(c, p, v, n) \ | |
| 572 | v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n); | |
| 573 | ||
| 574 | va = virtual_start; | |
| 48ffc236 | 575 | #ifdef JG |
| c8fe38ae | 576 | pte = (pt_entry_t *) pmap_pte(&kernel_pmap, va); |
| 48ffc236 JG |
577 | #else |
| 578 | pte = vtopte(va); | |
| 579 | #endif | |
| c8fe38ae MD |
580 | |
| 581 | /* | |
| 582 | * CMAP1/CMAP2 are used for zeroing and copying pages. | |
| 583 | */ | |
| 584 | SYSMAP(caddr_t, CMAP1, CADDR1, 1) | |
| 585 | ||
| 586 | /* | |
| 587 | * Crashdump maps. | |
| 588 | */ | |
| 589 | SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS); | |
| 590 | ||
| 591 | /* | |
| 592 | * ptvmmap is used for reading arbitrary physical pages via | |
| 593 | * /dev/mem. | |
| 594 | */ | |
| 595 | SYSMAP(caddr_t, ptmmap, ptvmmap, 1) | |
| 596 | ||
| 597 | /* | |
| 598 | * msgbufp is used to map the system message buffer. | |
| 599 | * XXX msgbufmap is not used. | |
| 600 | */ | |
| 601 | SYSMAP(struct msgbuf *, msgbufmap, msgbufp, | |
| 602 | atop(round_page(MSGBUF_SIZE))) | |
| 603 | ||
| 604 | virtual_start = va; | |
| 605 | ||
| 606 | *CMAP1 = 0; | |
| c8fe38ae MD |
607 | |
| 608 | /* | |
| 609 | * PG_G is terribly broken on SMP because we IPI invltlb's in some | |
| 610 | * cases rather then invl1pg. Actually, I don't even know why it | |
| 611 | * works under UP because self-referential page table mappings | |
| 612 | */ | |
| 613 | #ifdef SMP | |
| 614 | pgeflag = 0; | |
| 615 | #else | |
| 616 | if (cpu_feature & CPUID_PGE) | |
| 617 | pgeflag = PG_G; | |
| 618 | #endif | |
| 619 | ||
| 620 | /* | |
| 621 | * Initialize the 4MB page size flag | |
| 622 | */ | |
| 623 | pseflag = 0; | |
| 624 | /* | |
| 625 | * The 4MB page version of the initial | |
| 626 | * kernel page mapping. | |
| 627 | */ | |
| 628 | pdir4mb = 0; | |
| 629 | ||
| 630 | #if !defined(DISABLE_PSE) | |
| 631 | if (cpu_feature & CPUID_PSE) { | |
| 632 | pt_entry_t ptditmp; | |
| 633 | /* | |
| 634 | * Note that we have enabled PSE mode | |
| 635 | */ | |
| 636 | pseflag = PG_PS; | |
| c1543a89 | 637 | ptditmp = *(PTmap + x86_64_btop(KERNBASE)); |
| c8fe38ae MD |
638 | ptditmp &= ~(NBPDR - 1); |
| 639 | ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag; | |
| 640 | pdir4mb = ptditmp; | |
| 641 | ||
| 642 | #ifndef SMP | |
| 643 | /* | |
| 644 | * Enable the PSE mode. If we are SMP we can't do this | |
| 645 | * now because the APs will not be able to use it when | |
| 646 | * they boot up. | |
| 647 | */ | |
| 648 | load_cr4(rcr4() | CR4_PSE); | |
| 649 | ||
| 650 | /* | |
| 651 | * We can do the mapping here for the single processor | |
| 652 | * case. We simply ignore the old page table page from | |
| 653 | * now on. | |
| 654 | */ | |
| 655 | /* | |
| 656 | * For SMP, we still need 4K pages to bootstrap APs, | |
| 657 | * PSE will be enabled as soon as all APs are up. | |
| 658 | */ | |
| 659 | PTD[KPTDI] = (pd_entry_t)ptditmp; | |
| c8fe38ae MD |
660 | cpu_invltlb(); |
| 661 | #endif | |
| 662 | } | |
| 663 | #endif | |
| 664 | #ifdef SMP | |
| 665 | if (cpu_apic_address == 0) | |
| 666 | panic("pmap_bootstrap: no local apic!"); | |
| 057877ac | 667 | #endif |
| c8fe38ae MD |
668 | |
| 669 | /* | |
| 670 | * We need to finish setting up the globaldata page for the BSP. | |
| 671 | * locore has already populated the page table for the mdglobaldata | |
| 672 | * portion. | |
| 673 | */ | |
| 674 | pg = MDGLOBALDATA_BASEALLOC_PAGES; | |
| 675 | gd = &CPU_prvspace[0].mdglobaldata; | |
| 676 | gd->gd_CMAP1 = &SMPpt[pg + 0]; | |
| 677 | gd->gd_CMAP2 = &SMPpt[pg + 1]; | |
| 678 | gd->gd_CMAP3 = &SMPpt[pg + 2]; | |
| 679 | gd->gd_PMAP1 = &SMPpt[pg + 3]; | |
| 680 | gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1; | |
| 681 | gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2; | |
| 682 | gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3; | |
| 683 | gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1; | |
| 684 | ||
| 685 | cpu_invltlb(); | |
| d7f50089 YY |
686 | } |
| 687 | ||
| c8fe38ae | 688 | #ifdef SMP |
| d7f50089 | 689 | /* |
| c8fe38ae | 690 | * Set 4mb pdir for mp startup |
| d7f50089 YY |
691 | */ |
| 692 | void | |
| c8fe38ae MD |
693 | pmap_set_opt(void) |
| 694 | { | |
| 695 | if (pseflag && (cpu_feature & CPUID_PSE)) { | |
| 696 | load_cr4(rcr4() | CR4_PSE); | |
| 697 | if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */ | |
| c8fe38ae MD |
698 | cpu_invltlb(); |
| 699 | } | |
| 700 | } | |
| d7f50089 | 701 | } |
| c8fe38ae | 702 | #endif |
| d7f50089 | 703 | |
| c8fe38ae MD |
704 | /* |
| 705 | * Initialize the pmap module. | |
| 706 | * Called by vm_init, to initialize any structures that the pmap | |
| 707 | * system needs to map virtual memory. | |
| 708 | * pmap_init has been enhanced to support in a fairly consistant | |
| 709 | * way, discontiguous physical memory. | |
| d7f50089 YY |
710 | */ |
| 711 | void | |
| c8fe38ae | 712 | pmap_init(void) |
| d7f50089 | 713 | { |
| c8fe38ae MD |
714 | int i; |
| 715 | int initial_pvs; | |
| 716 | ||
| 717 | /* | |
| 718 | * object for kernel page table pages | |
| 719 | */ | |
| 48ffc236 JG |
720 | /* JG I think the number can be arbitrary */ |
| 721 | kptobj = vm_object_allocate(OBJT_DEFAULT, 5); | |
| c8fe38ae MD |
722 | |
| 723 | /* | |
| 724 | * Allocate memory for random pmap data structures. Includes the | |
| 725 | * pv_head_table. | |
| 726 | */ | |
| 727 | ||
| 728 | for(i = 0; i < vm_page_array_size; i++) { | |
| 729 | vm_page_t m; | |
| 730 | ||
| 731 | m = &vm_page_array[i]; | |
| 732 | TAILQ_INIT(&m->md.pv_list); | |
| 733 | m->md.pv_list_count = 0; | |
| 734 | } | |
| 735 | ||
| 736 | /* | |
| 737 | * init the pv free list | |
| 738 | */ | |
| 739 | initial_pvs = vm_page_array_size; | |
| 740 | if (initial_pvs < MINPV) | |
| 741 | initial_pvs = MINPV; | |
| 742 | pvzone = &pvzone_store; | |
| 743 | pvinit = (struct pv_entry *) kmem_alloc(&kernel_map, | |
| 744 | initial_pvs * sizeof (struct pv_entry)); | |
| 745 | zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit, | |
| 746 | initial_pvs); | |
| 747 | ||
| 748 | /* | |
| 749 | * Now it is safe to enable pv_table recording. | |
| 750 | */ | |
| 751 | pmap_initialized = TRUE; | |
| d887674b | 752 | #ifdef SMP |
| 057877ac | 753 | lapic = pmap_mapdev_uncacheable(cpu_apic_address, sizeof(struct LAPIC)); |
| d887674b | 754 | #endif |
| d7f50089 YY |
755 | } |
| 756 | ||
| c8fe38ae MD |
757 | /* |
| 758 | * Initialize the address space (zone) for the pv_entries. Set a | |
| 759 | * high water mark so that the system can recover from excessive | |
| 760 | * numbers of pv entries. | |
| 761 | */ | |
| d7f50089 | 762 | void |
| c8fe38ae | 763 | pmap_init2(void) |
| d7f50089 | 764 | { |
| c8fe38ae MD |
765 | int shpgperproc = PMAP_SHPGPERPROC; |
| 766 | ||
| 767 | TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc); | |
| 768 | pv_entry_max = shpgperproc * maxproc + vm_page_array_size; | |
| 769 | TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max); | |
| 770 | pv_entry_high_water = 9 * (pv_entry_max / 10); | |
| 771 | zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1); | |
| d7f50089 YY |
772 | } |
| 773 | ||
| c8fe38ae MD |
774 | |
| 775 | /*************************************************** | |
| 776 | * Low level helper routines..... | |
| 777 | ***************************************************/ | |
| 778 | ||
| 779 | #if defined(PMAP_DIAGNOSTIC) | |
| d7f50089 YY |
780 | |
| 781 | /* | |
| c8fe38ae MD |
782 | * This code checks for non-writeable/modified pages. |
| 783 | * This should be an invalid condition. | |
| d7f50089 | 784 | */ |
| bfc09ba0 MD |
785 | static |
| 786 | int | |
| 48ffc236 | 787 | pmap_nw_modified(pt_entry_t pte) |
| d7f50089 | 788 | { |
| c8fe38ae MD |
789 | if ((pte & (PG_M|PG_RW)) == PG_M) |
| 790 | return 1; | |
| 791 | else | |
| 792 | return 0; | |
| d7f50089 | 793 | } |
| c8fe38ae MD |
794 | #endif |
| 795 | ||
| d7f50089 | 796 | |
| c8fe38ae MD |
797 | /* |
| 798 | * this routine defines the region(s) of memory that should | |
| 799 | * not be tested for the modified bit. | |
| 800 | */ | |
| bfc09ba0 MD |
801 | static __inline |
| 802 | int | |
| c8fe38ae | 803 | pmap_track_modified(vm_offset_t va) |
| d7f50089 | 804 | { |
| c8fe38ae MD |
805 | if ((va < clean_sva) || (va >= clean_eva)) |
| 806 | return 1; | |
| 807 | else | |
| 808 | return 0; | |
| d7f50089 YY |
809 | } |
| 810 | ||
| d7f50089 | 811 | /* |
| c8fe38ae MD |
812 | * pmap_extract: |
| 813 | * | |
| 814 | * Extract the physical page address associated with the map/VA pair. | |
| 815 | * | |
| 816 | * This function may not be called from an interrupt if the pmap is | |
| 817 | * not kernel_pmap. | |
| d7f50089 | 818 | */ |
| c8fe38ae MD |
819 | vm_paddr_t |
| 820 | pmap_extract(pmap_t pmap, vm_offset_t va) | |
| d7f50089 | 821 | { |
| 48ffc236 JG |
822 | vm_paddr_t rtval; |
| 823 | pt_entry_t *pte; | |
| 824 | pd_entry_t pde, *pdep; | |
| c8fe38ae | 825 | |
| 48ffc236 JG |
826 | rtval = 0; |
| 827 | pdep = pmap_pde(pmap, va); | |
| 828 | if (pdep != NULL) { | |
| 829 | pde = *pdep; | |
| 830 | if (pde) { | |
| 831 | if ((pde & PG_PS) != 0) { | |
| 832 | rtval = (pde & PG_PS_FRAME) | (va & PDRMASK); | |
| 833 | } else { | |
| 834 | pte = pmap_pde_to_pte(pdep, va); | |
| 835 | rtval = (*pte & PG_FRAME) | (va & PAGE_MASK); | |
| 836 | } | |
| c8fe38ae | 837 | } |
| c8fe38ae | 838 | } |
| 48ffc236 JG |
839 | return rtval; |
| 840 | } | |
| 841 | ||
| 842 | /* | |
| 843 | * Routine: pmap_kextract | |
| 844 | * Function: | |
| 845 | * Extract the physical page address associated | |
| 846 | * kernel virtual address. | |
| 847 | */ | |
| 848 | vm_paddr_t | |
| 849 | pmap_kextract(vm_offset_t va) | |
| 48ffc236 JG |
850 | { |
| 851 | pd_entry_t pde; | |
| 852 | vm_paddr_t pa; | |
| 853 | ||
| 854 | if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) { | |
| 855 | pa = DMAP_TO_PHYS(va); | |
| 856 | } else { | |
| 857 | pde = *vtopde(va); | |
| 858 | if (pde & PG_PS) { | |
| 859 | pa = (pde & PG_PS_FRAME) | (va & PDRMASK); | |
| 860 | } else { | |
| 861 | /* | |
| 862 | * Beware of a concurrent promotion that changes the | |
| 863 | * PDE at this point! For example, vtopte() must not | |
| 864 | * be used to access the PTE because it would use the | |
| 865 | * new PDE. It is, however, safe to use the old PDE | |
| 866 | * because the page table page is preserved by the | |
| 867 | * promotion. | |
| 868 | */ | |
| 869 | pa = *pmap_pde_to_pte(&pde, va); | |
| 870 | pa = (pa & PG_FRAME) | (va & PAGE_MASK); | |
| 871 | } | |
| 872 | } | |
| 873 | return pa; | |
| d7f50089 YY |
874 | } |
| 875 | ||
| c8fe38ae MD |
876 | /*************************************************** |
| 877 | * Low level mapping routines..... | |
| 878 | ***************************************************/ | |
| 879 | ||
| d7f50089 | 880 | /* |
| c8fe38ae MD |
881 | * Routine: pmap_kenter |
| 882 | * Function: | |
| 883 | * Add a wired page to the KVA | |
| 884 | * NOTE! note that in order for the mapping to take effect -- you | |
| 885 | * should do an invltlb after doing the pmap_kenter(). | |
| d7f50089 | 886 | */ |
| c8fe38ae | 887 | void |
| d7f50089 YY |
888 | pmap_kenter(vm_offset_t va, vm_paddr_t pa) |
| 889 | { | |
| c8fe38ae MD |
890 | pt_entry_t *pte; |
| 891 | pt_entry_t npte; | |
| 892 | pmap_inval_info info; | |
| 893 | ||
| 894 | pmap_inval_init(&info); | |
| 895 | npte = pa | PG_RW | PG_V | pgeflag; | |
| 896 | pte = vtopte(va); | |
| 897 | pmap_inval_add(&info, &kernel_pmap, va); | |
| 898 | *pte = npte; | |
| 899 | pmap_inval_flush(&info); | |
| d7f50089 YY |
900 | } |
| 901 | ||
| 902 | /* | |
| c8fe38ae MD |
903 | * Routine: pmap_kenter_quick |
| 904 | * Function: | |
| 905 | * Similar to pmap_kenter(), except we only invalidate the | |
| 906 | * mapping on the current CPU. | |
| d7f50089 YY |
907 | */ |
| 908 | void | |
| c8fe38ae MD |
909 | pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa) |
| 910 | { | |
| 911 | pt_entry_t *pte; | |
| 912 | pt_entry_t npte; | |
| 913 | ||
| 914 | npte = pa | PG_RW | PG_V | pgeflag; | |
| 915 | pte = vtopte(va); | |
| 916 | *pte = npte; | |
| 917 | cpu_invlpg((void *)va); | |
| 918 | } | |
| 919 | ||
| 920 | void | |
| d7f50089 YY |
921 | pmap_kenter_sync(vm_offset_t va) |
| 922 | { | |
| c8fe38ae MD |
923 | pmap_inval_info info; |
| 924 | ||
| 925 | pmap_inval_init(&info); | |
| 926 | pmap_inval_add(&info, &kernel_pmap, va); | |
| 927 | pmap_inval_flush(&info); | |
| d7f50089 YY |
928 | } |
| 929 | ||
| d7f50089 YY |
930 | void |
| 931 | pmap_kenter_sync_quick(vm_offset_t va) | |
| 932 | { | |
| c8fe38ae | 933 | cpu_invlpg((void *)va); |
| d7f50089 YY |
934 | } |
| 935 | ||
| d7f50089 | 936 | /* |
| c8fe38ae | 937 | * remove a page from the kernel pagetables |
| d7f50089 YY |
938 | */ |
| 939 | void | |
| c8fe38ae | 940 | pmap_kremove(vm_offset_t va) |
| d7f50089 | 941 | { |
| c8fe38ae MD |
942 | pt_entry_t *pte; |
| 943 | pmap_inval_info info; | |
| 944 | ||
| 945 | pmap_inval_init(&info); | |
| 946 | pte = vtopte(va); | |
| 947 | pmap_inval_add(&info, &kernel_pmap, va); | |
| 948 | *pte = 0; | |
| 949 | pmap_inval_flush(&info); | |
| 950 | } | |
| 951 | ||
| 952 | void | |
| 953 | pmap_kremove_quick(vm_offset_t va) | |
| 954 | { | |
| 955 | pt_entry_t *pte; | |
| 956 | pte = vtopte(va); | |
| 957 | *pte = 0; | |
| 958 | cpu_invlpg((void *)va); | |
| d7f50089 YY |
959 | } |
| 960 | ||
| 961 | /* | |
| c8fe38ae | 962 | * XXX these need to be recoded. They are not used in any critical path. |
| d7f50089 YY |
963 | */ |
| 964 | void | |
| c8fe38ae | 965 | pmap_kmodify_rw(vm_offset_t va) |
| d7f50089 | 966 | { |
| c8fe38ae MD |
967 | *vtopte(va) |= PG_RW; |
| 968 | cpu_invlpg((void *)va); | |
| d7f50089 YY |
969 | } |
| 970 | ||
| c8fe38ae MD |
971 | void |
| 972 | pmap_kmodify_nc(vm_offset_t va) | |
| 973 | { | |
| 974 | *vtopte(va) |= PG_N; | |
| 975 | cpu_invlpg((void *)va); | |
| 976 | } | |
| d7f50089 YY |
977 | |
| 978 | /* | |
| c8fe38ae MD |
979 | * Used to map a range of physical addresses into kernel |
| 980 | * virtual address space. | |
| 981 | * | |
| 982 | * For now, VM is already on, we only need to map the | |
| 983 | * specified memory. | |
| d7f50089 YY |
984 | */ |
| 985 | vm_offset_t | |
| 986 | pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot) | |
| 987 | { | |
| 8fdd3267 | 988 | return PHYS_TO_DMAP(start); |
| d7f50089 YY |
989 | } |
| 990 | ||
| c8fe38ae | 991 | |
| d7f50089 | 992 | /* |
| c8fe38ae MD |
993 | * Add a list of wired pages to the kva |
| 994 | * this routine is only used for temporary | |
| 995 | * kernel mappings that do not need to have | |
| 996 | * page modification or references recorded. | |
| 997 | * Note that old mappings are simply written | |
| 998 | * over. The page *must* be wired. | |
| d7f50089 YY |
999 | */ |
| 1000 | void | |
| c8fe38ae | 1001 | pmap_qenter(vm_offset_t va, vm_page_t *m, int count) |
| d7f50089 | 1002 | { |
| c8fe38ae MD |
1003 | vm_offset_t end_va; |
| 1004 | ||
| 1005 | end_va = va + count * PAGE_SIZE; | |
| 1006 | ||
| 1007 | while (va < end_va) { | |
| 1008 | pt_entry_t *pte; | |
| 1009 | ||
| 1010 | pte = vtopte(va); | |
| 1011 | *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag; | |
| 1012 | cpu_invlpg((void *)va); | |
| 1013 | va += PAGE_SIZE; | |
| 1014 | m++; | |
| 1015 | } | |
| 1016 | #ifdef SMP | |
| 1017 | smp_invltlb(); /* XXX */ | |
| 1018 | #endif | |
| 1019 | } | |
| 1020 | ||
| 1021 | void | |
| 1022 | pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask) | |
| 1023 | { | |
| 1024 | vm_offset_t end_va; | |
| 1025 | cpumask_t cmask = mycpu->gd_cpumask; | |
| 1026 | ||
| 1027 | end_va = va + count * PAGE_SIZE; | |
| 1028 | ||
| 1029 | while (va < end_va) { | |
| 1030 | pt_entry_t *pte; | |
| 1031 | pt_entry_t pteval; | |
| 1032 | ||
| 1033 | /* | |
| 1034 | * Install the new PTE. If the pte changed from the prior | |
| 1035 | * mapping we must reset the cpu mask and invalidate the page. | |
| 1036 | * If the pte is the same but we have not seen it on the | |
| 1037 | * current cpu, invlpg the existing mapping. Otherwise the | |
| 1038 | * entry is optimal and no invalidation is required. | |
| 1039 | */ | |
| 1040 | pte = vtopte(va); | |
| 1041 | pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag; | |
| 1042 | if (*pte != pteval) { | |
| 1043 | *mask = 0; | |
| 1044 | *pte = pteval; | |
| 1045 | cpu_invlpg((void *)va); | |
| 1046 | } else if ((*mask & cmask) == 0) { | |
| 1047 | cpu_invlpg((void *)va); | |
| 1048 | } | |
| 1049 | va += PAGE_SIZE; | |
| 1050 | m++; | |
| 1051 | } | |
| 1052 | *mask |= cmask; | |
| d7f50089 YY |
1053 | } |
| 1054 | ||
| 1055 | /* | |
| 7155fc7d | 1056 | * This routine jerks page mappings from the |
| c8fe38ae | 1057 | * kernel -- it is meant only for temporary mappings. |
| 7155fc7d MD |
1058 | * |
| 1059 | * MPSAFE, INTERRUPT SAFE (cluster callback) | |
| d7f50089 | 1060 | */ |
| c8fe38ae MD |
1061 | void |
| 1062 | pmap_qremove(vm_offset_t va, int count) | |
| d7f50089 | 1063 | { |
| c8fe38ae MD |
1064 | vm_offset_t end_va; |
| 1065 | ||
| 48ffc236 | 1066 | end_va = va + count * PAGE_SIZE; |
| c8fe38ae MD |
1067 | |
| 1068 | while (va < end_va) { | |
| 1069 | pt_entry_t *pte; | |
| 1070 | ||
| 1071 | pte = vtopte(va); | |
| 1072 | *pte = 0; | |
| 1073 | cpu_invlpg((void *)va); | |
| 1074 | va += PAGE_SIZE; | |
| 1075 | } | |
| 1076 | #ifdef SMP | |
| 1077 | smp_invltlb(); | |
| 1078 | #endif | |
| d7f50089 YY |
1079 | } |
| 1080 | ||
| 1081 | /* | |
| c8fe38ae MD |
1082 | * This routine works like vm_page_lookup() but also blocks as long as the |
| 1083 | * page is busy. This routine does not busy the page it returns. | |
| 1084 | * | |
| 1085 | * Unless the caller is managing objects whos pages are in a known state, | |
| 1086 | * the call should be made with a critical section held so the page's object | |
| 1087 | * association remains valid on return. | |
| d7f50089 | 1088 | */ |
| bfc09ba0 MD |
1089 | static |
| 1090 | vm_page_t | |
| c8fe38ae | 1091 | pmap_page_lookup(vm_object_t object, vm_pindex_t pindex) |
| d7f50089 | 1092 | { |
| c8fe38ae MD |
1093 | vm_page_t m; |
| 1094 | ||
| 1095 | do { | |
| 1096 | m = vm_page_lookup(object, pindex); | |
| 1097 | } while (m && vm_page_sleep_busy(m, FALSE, "pplookp")); | |
| 1098 | ||
| 1099 | return(m); | |
| d7f50089 YY |
1100 | } |
| 1101 | ||
| 1102 | /* | |
| c8fe38ae MD |
1103 | * Create a new thread and optionally associate it with a (new) process. |
| 1104 | * NOTE! the new thread's cpu may not equal the current cpu. | |
| d7f50089 YY |
1105 | */ |
| 1106 | void | |
| c8fe38ae | 1107 | pmap_init_thread(thread_t td) |
| d7f50089 | 1108 | { |
| c8fe38ae MD |
1109 | /* enforce pcb placement */ |
| 1110 | td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1; | |
| 1111 | td->td_savefpu = &td->td_pcb->pcb_save; | |
| c1543a89 | 1112 | td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */ |
| d7f50089 YY |
1113 | } |
| 1114 | ||
| 1115 | /* | |
| c8fe38ae | 1116 | * This routine directly affects the fork perf for a process. |
| d7f50089 YY |
1117 | */ |
| 1118 | void | |
| c8fe38ae | 1119 | pmap_init_proc(struct proc *p) |
| d7f50089 YY |
1120 | { |
| 1121 | } | |
| 1122 | ||
| 1123 | /* | |
| c8fe38ae MD |
1124 | * Dispose the UPAGES for a process that has exited. |
| 1125 | * This routine directly impacts the exit perf of a process. | |
| d7f50089 YY |
1126 | */ |
| 1127 | void | |
| c8fe38ae | 1128 | pmap_dispose_proc(struct proc *p) |
| d7f50089 | 1129 | { |
| c8fe38ae | 1130 | KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p)); |
| d7f50089 YY |
1131 | } |
| 1132 | ||
| c8fe38ae MD |
1133 | /*************************************************** |
| 1134 | * Page table page management routines..... | |
| 1135 | ***************************************************/ | |
| 1136 | ||
| d7f50089 | 1137 | /* |
| c8fe38ae MD |
1138 | * This routine unholds page table pages, and if the hold count |
| 1139 | * drops to zero, then it decrements the wire count. | |
| d7f50089 | 1140 | */ |
| bfc09ba0 MD |
1141 | static __inline |
| 1142 | int | |
| 1143 | pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, | |
| 1144 | pmap_inval_info_t info) | |
| 1145 | { | |
| 1146 | KKASSERT(m->hold_count > 0); | |
| 1147 | if (m->hold_count > 1) { | |
| 1148 | vm_page_unhold(m); | |
| 1149 | return 0; | |
| 1150 | } else { | |
| 1151 | return _pmap_unwire_pte_hold(pmap, va, m, info); | |
| 1152 | } | |
| 1153 | } | |
| 1154 | ||
| 1155 | static | |
| 1156 | int | |
| 1157 | _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, | |
| 1158 | pmap_inval_info_t info) | |
| c8fe38ae MD |
1159 | { |
| 1160 | /* | |
| 1161 | * Wait until we can busy the page ourselves. We cannot have | |
| 700e22f7 MD |
1162 | * any active flushes if we block. We own one hold count on the |
| 1163 | * page so it cannot be freed out from under us. | |
| c8fe38ae MD |
1164 | */ |
| 1165 | if (m->flags & PG_BUSY) { | |
| 1166 | pmap_inval_flush(info); | |
| 1167 | while (vm_page_sleep_busy(m, FALSE, "pmuwpt")) | |
| 1168 | ; | |
| 1169 | } | |
| 1170 | KASSERT(m->queue == PQ_NONE, | |
| 1171 | ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m)); | |
| 1172 | ||
| 700e22f7 MD |
1173 | /* |
| 1174 | * This case can occur if new references were acquired while | |
| 1175 | * we were blocked. | |
| 1176 | */ | |
| 1177 | if (m->hold_count > 1) { | |
| 1178 | KKASSERT(m->hold_count > 1); | |
| 1179 | vm_page_unhold(m); | |
| 1180 | return 0; | |
| 1181 | } | |
| c8fe38ae | 1182 | |
| 700e22f7 MD |
1183 | /* |
| 1184 | * Unmap the page table page | |
| 1185 | */ | |
| 1186 | KKASSERT(m->hold_count == 1); | |
| 1187 | vm_page_busy(m); | |
| 1188 | pmap_inval_add(info, pmap, -1); | |
| c8fe38ae | 1189 | |
| 700e22f7 MD |
1190 | if (m->pindex >= (NUPDE + NUPDPE)) { |
| 1191 | /* PDP page */ | |
| 1192 | pml4_entry_t *pml4; | |
| 1193 | pml4 = pmap_pml4e(pmap, va); | |
| 1194 | *pml4 = 0; | |
| 1195 | } else if (m->pindex >= NUPDE) { | |
| 1196 | /* PD page */ | |
| 1197 | pdp_entry_t *pdp; | |
| 1198 | pdp = pmap_pdpe(pmap, va); | |
| 1199 | *pdp = 0; | |
| 1200 | } else { | |
| 1201 | /* PT page */ | |
| 1202 | pd_entry_t *pd; | |
| 1203 | pd = pmap_pde(pmap, va); | |
| 1204 | *pd = 0; | |
| 1205 | } | |
| c8fe38ae | 1206 | |
| 700e22f7 MD |
1207 | KKASSERT(pmap->pm_stats.resident_count > 0); |
| 1208 | --pmap->pm_stats.resident_count; | |
| 48ffc236 | 1209 | |
| 700e22f7 MD |
1210 | if (pmap->pm_ptphint == m) |
| 1211 | pmap->pm_ptphint = NULL; | |
| 1212 | ||
| 1213 | if (m->pindex < NUPDE) { | |
| 1214 | /* We just released a PT, unhold the matching PD */ | |
| 1215 | vm_page_t pdpg; | |
| 1216 | ||
| 1217 | pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME); | |
| 1218 | pmap_unwire_pte_hold(pmap, va, pdpg, info); | |
| 1219 | } | |
| 1220 | if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) { | |
| 1221 | /* We just released a PD, unhold the matching PDP */ | |
| 1222 | vm_page_t pdppg; | |
| 1223 | ||
| 1224 | pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME); | |
| 1225 | pmap_unwire_pte_hold(pmap, va, pdppg, info); | |
| c8fe38ae | 1226 | } |
| 700e22f7 MD |
1227 | |
| 1228 | /* | |
| 1229 | * This was our last hold, the page had better be unwired | |
| 1230 | * after we decrement wire_count. | |
| 1231 | * | |
| 1232 | * FUTURE NOTE: shared page directory page could result in | |
| 1233 | * multiple wire counts. | |
| 1234 | */ | |
| 1235 | vm_page_unhold(m); | |
| 1236 | --m->wire_count; | |
| 1237 | KKASSERT(m->wire_count == 0); | |
| 1238 | --vmstats.v_wire_count; | |
| 1239 | vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE); | |
| 1240 | vm_page_flash(m); | |
| 1241 | vm_page_free_zero(m); | |
| 1242 | ||
| 1243 | return 1; | |
| c8fe38ae MD |
1244 | } |
| 1245 | ||
| c8fe38ae MD |
1246 | /* |
| 1247 | * After removing a page table entry, this routine is used to | |
| 1248 | * conditionally free the page, and manage the hold/wire counts. | |
| d7f50089 | 1249 | */ |
| bfc09ba0 MD |
1250 | static |
| 1251 | int | |
| c8fe38ae MD |
1252 | pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte, |
| 1253 | pmap_inval_info_t info) | |
| 1254 | { | |
| 1255 | vm_pindex_t ptepindex; | |
| 700e22f7 | 1256 | |
| 48ffc236 | 1257 | if (va >= VM_MAX_USER_ADDRESS) |
| c8fe38ae MD |
1258 | return 0; |
| 1259 | ||
| 1260 | if (mpte == NULL) { | |
| 48ffc236 JG |
1261 | ptepindex = pmap_pde_pindex(va); |
| 1262 | #if JGHINT | |
| c8fe38ae MD |
1263 | if (pmap->pm_ptphint && |
| 1264 | (pmap->pm_ptphint->pindex == ptepindex)) { | |
| 1265 | mpte = pmap->pm_ptphint; | |
| 1266 | } else { | |
| 48ffc236 | 1267 | #endif |
| c8fe38ae | 1268 | pmap_inval_flush(info); |
| 48ffc236 | 1269 | mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex); |
| c8fe38ae | 1270 | pmap->pm_ptphint = mpte; |
| 48ffc236 | 1271 | #if JGHINT |
| c8fe38ae | 1272 | } |
| 48ffc236 | 1273 | #endif |
| c8fe38ae | 1274 | } |
| 48ffc236 | 1275 | return pmap_unwire_pte_hold(pmap, va, mpte, info); |
| c8fe38ae | 1276 | } |
| d7f50089 YY |
1277 | |
| 1278 | /* | |
| c8fe38ae MD |
1279 | * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because |
| 1280 | * it, and IdlePTD, represents the template used to update all other pmaps. | |
| 1281 | * | |
| 1282 | * On architectures where the kernel pmap is not integrated into the user | |
| 1283 | * process pmap, this pmap represents the process pmap, not the kernel pmap. | |
| 1284 | * kernel_pmap should be used to directly access the kernel_pmap. | |
| d7f50089 YY |
1285 | */ |
| 1286 | void | |
| c8fe38ae | 1287 | pmap_pinit0(struct pmap *pmap) |
| d7f50089 | 1288 | { |
| 48ffc236 | 1289 | pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys); |
| c8fe38ae MD |
1290 | pmap->pm_count = 1; |
| 1291 | pmap->pm_active = 0; | |
| 1292 | pmap->pm_ptphint = NULL; | |
| 1293 | TAILQ_INIT(&pmap->pm_pvlist); | |
| 1294 | bzero(&pmap->pm_stats, sizeof pmap->pm_stats); | |
| d7f50089 YY |
1295 | } |
| 1296 | ||
| 1297 | /* | |
| c8fe38ae MD |
1298 | * Initialize a preallocated and zeroed pmap structure, |
| 1299 | * such as one in a vmspace structure. | |
| d7f50089 YY |
1300 | */ |
| 1301 | void | |
| c8fe38ae | 1302 | pmap_pinit(struct pmap *pmap) |
| d7f50089 | 1303 | { |
| c8fe38ae MD |
1304 | vm_page_t ptdpg; |
| 1305 | ||
| 1306 | /* | |
| 1307 | * No need to allocate page table space yet but we do need a valid | |
| 1308 | * page directory table. | |
| 1309 | */ | |
| 48ffc236 JG |
1310 | if (pmap->pm_pml4 == NULL) { |
| 1311 | pmap->pm_pml4 = | |
| 1312 | (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE); | |
| c8fe38ae MD |
1313 | } |
| 1314 | ||
| 1315 | /* | |
| 1316 | * Allocate an object for the ptes | |
| 1317 | */ | |
| 1318 | if (pmap->pm_pteobj == NULL) | |
| 0a5c555b | 1319 | pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1); |
| c8fe38ae MD |
1320 | |
| 1321 | /* | |
| 1322 | * Allocate the page directory page, unless we already have | |
| 1323 | * one cached. If we used the cached page the wire_count will | |
| 1324 | * already be set appropriately. | |
| 1325 | */ | |
| 1326 | if ((ptdpg = pmap->pm_pdirm) == NULL) { | |
| 0a5c555b | 1327 | ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I, |
| c8fe38ae MD |
1328 | VM_ALLOC_NORMAL | VM_ALLOC_RETRY); |
| 1329 | pmap->pm_pdirm = ptdpg; | |
| 1330 | vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); | |
| 1331 | ptdpg->valid = VM_PAGE_BITS_ALL; | |
| 700e22f7 MD |
1332 | if (ptdpg->wire_count == 0) |
| 1333 | ++vmstats.v_wire_count; | |
| c8fe38ae | 1334 | ptdpg->wire_count = 1; |
| 48ffc236 | 1335 | pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg)); |
| c8fe38ae MD |
1336 | } |
| 1337 | if ((ptdpg->flags & PG_ZERO) == 0) | |
| 48ffc236 | 1338 | bzero(pmap->pm_pml4, PAGE_SIZE); |
| c8fe38ae | 1339 | |
| 48ffc236 JG |
1340 | pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U; |
| 1341 | pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U; | |
| c8fe38ae MD |
1342 | |
| 1343 | /* install self-referential address mapping entry */ | |
| 48ffc236 | 1344 | pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M; |
| c8fe38ae MD |
1345 | |
| 1346 | pmap->pm_count = 1; | |
| 1347 | pmap->pm_active = 0; | |
| 1348 | pmap->pm_ptphint = NULL; | |
| 1349 | TAILQ_INIT(&pmap->pm_pvlist); | |
| 1350 | bzero(&pmap->pm_stats, sizeof pmap->pm_stats); | |
| 1351 | pmap->pm_stats.resident_count = 1; | |
| d7f50089 YY |
1352 | } |
| 1353 | ||
| 1354 | /* | |
| c8fe38ae MD |
1355 | * Clean up a pmap structure so it can be physically freed. This routine |
| 1356 | * is called by the vmspace dtor function. A great deal of pmap data is | |
| 1357 | * left passively mapped to improve vmspace management so we have a bit | |
| 1358 | * of cleanup work to do here. | |
| d7f50089 YY |
1359 | */ |
| 1360 | void | |
| c8fe38ae | 1361 | pmap_puninit(pmap_t pmap) |
| d7f50089 | 1362 | { |
| c8fe38ae MD |
1363 | vm_page_t p; |
| 1364 | ||
| 1365 | KKASSERT(pmap->pm_active == 0); | |
| 1366 | if ((p = pmap->pm_pdirm) != NULL) { | |
| 48ffc236 | 1367 | KKASSERT(pmap->pm_pml4 != NULL); |
| bfc09ba0 | 1368 | KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys)); |
| 48ffc236 | 1369 | pmap_kremove((vm_offset_t)pmap->pm_pml4); |
| c8fe38ae MD |
1370 | p->wire_count--; |
| 1371 | vmstats.v_wire_count--; | |
| 1372 | KKASSERT((p->flags & PG_BUSY) == 0); | |
| 1373 | vm_page_busy(p); | |
| 1374 | vm_page_free_zero(p); | |
| 1375 | pmap->pm_pdirm = NULL; | |
| 1376 | } | |
| 48ffc236 | 1377 | if (pmap->pm_pml4) { |
| bfc09ba0 | 1378 | KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys)); |
| 48ffc236 JG |
1379 | kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE); |
| 1380 | pmap->pm_pml4 = NULL; | |
| c8fe38ae MD |
1381 | } |
| 1382 | if (pmap->pm_pteobj) { | |
| 1383 | vm_object_deallocate(pmap->pm_pteobj); | |
| 1384 | pmap->pm_pteobj = NULL; | |
| 1385 | } | |
| d7f50089 YY |
1386 | } |
| 1387 | ||
| 1388 | /* | |
| c8fe38ae MD |
1389 | * Wire in kernel global address entries. To avoid a race condition |
| 1390 | * between pmap initialization and pmap_growkernel, this procedure | |
| 1391 | * adds the pmap to the master list (which growkernel scans to update), | |
| 1392 | * then copies the template. | |
| d7f50089 YY |
1393 | */ |
| 1394 | void | |
| c8fe38ae | 1395 | pmap_pinit2(struct pmap *pmap) |
| d7f50089 | 1396 | { |
| c8fe38ae MD |
1397 | crit_enter(); |
| 1398 | TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode); | |
| 1399 | /* XXX copies current process, does not fill in MPPTDI */ | |
| c8fe38ae | 1400 | crit_exit(); |
| d7f50089 YY |
1401 | } |
| 1402 | ||
| 1403 | /* | |
| c8fe38ae MD |
1404 | * Attempt to release and free a vm_page in a pmap. Returns 1 on success, |
| 1405 | * 0 on failure (if the procedure had to sleep). | |
| d7f50089 | 1406 | * |
| c8fe38ae MD |
1407 | * When asked to remove the page directory page itself, we actually just |
| 1408 | * leave it cached so we do not have to incur the SMP inval overhead of | |
| 1409 | * removing the kernel mapping. pmap_puninit() will take care of it. | |
| d7f50089 | 1410 | */ |
| bfc09ba0 MD |
1411 | static |
| 1412 | int | |
| c8fe38ae | 1413 | pmap_release_free_page(struct pmap *pmap, vm_page_t p) |
| d7f50089 | 1414 | { |
| c8fe38ae MD |
1415 | /* |
| 1416 | * This code optimizes the case of freeing non-busy | |
| 1417 | * page-table pages. Those pages are zero now, and | |
| 1418 | * might as well be placed directly into the zero queue. | |
| 1419 | */ | |
| 1420 | if (vm_page_sleep_busy(p, FALSE, "pmaprl")) | |
| d7f50089 | 1421 | return 0; |
| d7f50089 | 1422 | |
| c8fe38ae MD |
1423 | vm_page_busy(p); |
| 1424 | ||
| 1425 | /* | |
| 1426 | * Remove the page table page from the processes address space. | |
| 1427 | */ | |
| 4a4ea614 MD |
1428 | if (p->pindex == NUPDE + NUPDPE + PML4PML4I) { |
| 1429 | /* | |
| 1430 | * We are the pml4 table itself. | |
| 1431 | */ | |
| 1432 | /* XXX anything to do here? */ | |
| 1433 | } else if (p->pindex >= (NUPDE + NUPDPE)) { | |
| 1b2e0b92 | 1434 | /* |
| 700e22f7 MD |
1435 | * Remove a PDP page from the PML4. We do not maintain |
| 1436 | * hold counts on the PML4 page. | |
| 1b2e0b92 | 1437 | */ |
| 700e22f7 MD |
1438 | pml4_entry_t *pml4; |
| 1439 | vm_page_t m4; | |
| 1440 | int idx; | |
| 1441 | ||
| 1442 | m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I); | |
| 1b2e0b92 | 1443 | KKASSERT(m4 != NULL); |
| 700e22f7 MD |
1444 | pml4 = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4)); |
| 1445 | idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG; | |
| 1b2e0b92 JG |
1446 | KKASSERT(pml4[idx] != 0); |
| 1447 | pml4[idx] = 0; | |
| 1b2e0b92 JG |
1448 | } else if (p->pindex >= NUPDE) { |
| 1449 | /* | |
| 700e22f7 MD |
1450 | * Remove a PD page from the PDP and drop the hold count |
| 1451 | * on the PDP. The PDP is left cached in the pmap if | |
| 1452 | * the hold count drops to 0 so the wire count remains | |
| 1453 | * intact. | |
| 1b2e0b92 | 1454 | */ |
| 700e22f7 MD |
1455 | vm_page_t m3; |
| 1456 | pdp_entry_t *pdp; | |
| 1457 | int idx; | |
| 1458 | ||
| 1459 | m3 = vm_page_lookup(pmap->pm_pteobj, | |
| 1460 | NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG); | |
| 1b2e0b92 | 1461 | KKASSERT(m3 != NULL); |
| 700e22f7 MD |
1462 | pdp = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3)); |
| 1463 | idx = (p->pindex - NUPDE) % NPDPEPG; | |
| 1b2e0b92 JG |
1464 | KKASSERT(pdp[idx] != 0); |
| 1465 | pdp[idx] = 0; | |
| 1466 | m3->hold_count--; | |
| 1b2e0b92 | 1467 | } else { |
| 700e22f7 MD |
1468 | /* |
| 1469 | * Remove a PT page from the PD and drop the hold count | |
| 1470 | * on the PD. The PD is left cached in the pmap if | |
| 1471 | * the hold count drops to 0 so the wire count remains | |
| 1472 | * intact. | |
| 1b2e0b92 | 1473 | */ |
| 700e22f7 MD |
1474 | vm_page_t m2; |
| 1475 | pd_entry_t *pd; | |
| 1476 | int idx; | |
| 1477 | ||
| 1478 | m2 = vm_page_lookup(pmap->pm_pteobj, | |
| 1479 | NUPDE + p->pindex / NPDEPG); | |
| 1b2e0b92 | 1480 | KKASSERT(m2 != NULL); |
| 700e22f7 MD |
1481 | pd = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2)); |
| 1482 | idx = p->pindex % NPDEPG; | |
| 1b2e0b92 JG |
1483 | pd[idx] = 0; |
| 1484 | m2->hold_count--; | |
| 1b2e0b92 | 1485 | } |
| 700e22f7 MD |
1486 | |
| 1487 | /* | |
| 1488 | * One fewer mappings in the pmap. p's hold count had better | |
| 1489 | * be zero. | |
| 1490 | */ | |
| c8fe38ae MD |
1491 | KKASSERT(pmap->pm_stats.resident_count > 0); |
| 1492 | --pmap->pm_stats.resident_count; | |
| 700e22f7 | 1493 | if (p->hold_count) |
| c8fe38ae | 1494 | panic("pmap_release: freeing held page table page"); |
| c8fe38ae MD |
1495 | if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex)) |
| 1496 | pmap->pm_ptphint = NULL; | |
| 1497 | ||
| 1b2e0b92 JG |
1498 | /* |
| 1499 | * We leave the top-level page table page cached, wired, and mapped in | |
| 1500 | * the pmap until the dtor function (pmap_puninit()) gets called. | |
| 1501 | * However, still clean it up so we can set PG_ZERO. | |
| 1502 | */ | |
| 1503 | if (p->pindex == NUPDE + NUPDPE + PML4PML4I) { | |
| 1504 | bzero(pmap->pm_pml4, PAGE_SIZE); | |
| 1505 | vm_page_flag_set(p, PG_ZERO); | |
| 1506 | vm_page_wakeup(p); | |
| 1507 | } else { | |
| 1508 | p->wire_count--; | |
| 700e22f7 | 1509 | KKASSERT(p->wire_count == 0); |
| 1b2e0b92 JG |
1510 | vmstats.v_wire_count--; |
| 1511 | /* JG eventually revert to using vm_page_free_zero() */ | |
| 1512 | vm_page_free(p); | |
| 1513 | } | |
| c8fe38ae MD |
1514 | return 1; |
| 1515 | } | |
| d7f50089 YY |
1516 | |
| 1517 | /* | |
| e8510e54 MD |
1518 | * This routine is called when various levels in the page table need to |
| 1519 | * be populated. This routine cannot fail. | |
| d7f50089 | 1520 | */ |
| bfc09ba0 MD |
1521 | static |
| 1522 | vm_page_t | |
| c8fe38ae MD |
1523 | _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex) |
| 1524 | { | |
| e8510e54 | 1525 | vm_page_t m; |
| c8fe38ae MD |
1526 | |
| 1527 | /* | |
| e8510e54 | 1528 | * Find or fabricate a new pagetable page. This will busy the page. |
| c8fe38ae MD |
1529 | */ |
| 1530 | m = vm_page_grab(pmap->pm_pteobj, ptepindex, | |
| 700e22f7 | 1531 | VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY); |
| 48ffc236 JG |
1532 | if ((m->flags & PG_ZERO) == 0) { |
| 1533 | pmap_zero_page(VM_PAGE_TO_PHYS(m)); | |
| 1534 | } | |
| 1535 | ||
| c8fe38ae MD |
1536 | KASSERT(m->queue == PQ_NONE, |
| 1537 | ("_pmap_allocpte: %p->queue != PQ_NONE", m)); | |
| 1538 | ||
| 1539 | /* | |
| 1540 | * Increment the hold count for the page we will be returning to | |
| 1541 | * the caller. | |
| 1542 | */ | |
| 1543 | m->hold_count++; | |
| 700e22f7 | 1544 | if (m->wire_count++ == 0) |
| c8fe38ae | 1545 | vmstats.v_wire_count++; |
| c8fe38ae | 1546 | |
| c8fe38ae MD |
1547 | /* |
| 1548 | * Map the pagetable page into the process address space, if | |
| 1549 | * it isn't already there. | |
| e8510e54 MD |
1550 | * |
| 1551 | * It is possible that someone else got in and mapped the page | |
| 1552 | * directory page while we were blocked, if so just unbusy and | |
| 1553 | * return the held page. | |
| c8fe38ae | 1554 | */ |
| 48ffc236 | 1555 | if (ptepindex >= (NUPDE + NUPDPE)) { |
| e8510e54 MD |
1556 | /* |
| 1557 | * Wire up a new PDP page in the PML4 | |
| 1558 | */ | |
| 48ffc236 | 1559 | vm_pindex_t pml4index; |
| 700e22f7 | 1560 | pml4_entry_t *pml4; |
| 48ffc236 | 1561 | |
| 48ffc236 JG |
1562 | pml4index = ptepindex - (NUPDE + NUPDPE); |
| 1563 | pml4 = &pmap->pm_pml4[pml4index]; | |
| e8510e54 | 1564 | if (*pml4 & PG_V) { |
| 700e22f7 MD |
1565 | if (--m->wire_count == 0) |
| 1566 | --vmstats.v_wire_count; | |
| e8510e54 MD |
1567 | vm_page_wakeup(m); |
| 1568 | return(m); | |
| 1569 | } | |
| 48ffc236 | 1570 | *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M; |
| 48ffc236 | 1571 | } else if (ptepindex >= NUPDE) { |
| e8510e54 MD |
1572 | /* |
| 1573 | * Wire up a new PD page in the PDP | |
| 1574 | */ | |
| 48ffc236 JG |
1575 | vm_pindex_t pml4index; |
| 1576 | vm_pindex_t pdpindex; | |
| e8510e54 | 1577 | vm_page_t pdppg; |
| 48ffc236 JG |
1578 | pml4_entry_t *pml4; |
| 1579 | pdp_entry_t *pdp; | |
| 1580 | ||
| 48ffc236 JG |
1581 | pdpindex = ptepindex - NUPDE; |
| 1582 | pml4index = pdpindex >> NPML4EPGSHIFT; | |
| 1583 | ||
| 1584 | pml4 = &pmap->pm_pml4[pml4index]; | |
| 1585 | if ((*pml4 & PG_V) == 0) { | |
| e8510e54 MD |
1586 | /* |
| 1587 | * Have to allocate a new PDP page, recurse. | |
| 1588 | * This always succeeds. Returned page will | |
| 1589 | * be held. | |
| 1590 | */ | |
| 1591 | pdppg = _pmap_allocpte(pmap, | |
| 1592 | NUPDE + NUPDPE + pml4index); | |
| 48ffc236 | 1593 | } else { |
| e8510e54 MD |
1594 | /* |
| 1595 | * Add a held reference to the PDP page. | |
| 1596 | */ | |
| 48ffc236 | 1597 | pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME); |
| 1b2e0b92 | 1598 | pdppg->hold_count++; |
| 48ffc236 | 1599 | } |
| c8fe38ae | 1600 | |
| e8510e54 MD |
1601 | /* |
| 1602 | * Now find the pdp_entry and map the PDP. If the PDP | |
| 1603 | * has already been mapped unwind and return the | |
| 1604 | * already-mapped PDP held. | |
| 700e22f7 MD |
1605 | * |
| 1606 | * pdppg is left held (hold_count is incremented for | |
| 1607 | * each PD in the PDP). | |
| e8510e54 MD |
1608 | */ |
| 1609 | pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME); | |
| 48ffc236 | 1610 | pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)]; |
| e8510e54 MD |
1611 | if (*pdp & PG_V) { |
| 1612 | vm_page_unhold(pdppg); | |
| 700e22f7 MD |
1613 | if (--m->wire_count == 0) |
| 1614 | --vmstats.v_wire_count; | |
| e8510e54 MD |
1615 | vm_page_wakeup(m); |
| 1616 | return(m); | |
| 1617 | } | |
| 48ffc236 | 1618 | *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M; |
| 48ffc236 | 1619 | } else { |
| e8510e54 MD |
1620 | /* |
| 1621 | * Wire up the new PT page in the PD | |
| 1622 | */ | |
| 48ffc236 JG |
1623 | vm_pindex_t pml4index; |
| 1624 | vm_pindex_t pdpindex; | |
| 1625 | pml4_entry_t *pml4; | |
| 1626 | pdp_entry_t *pdp; | |
| 1627 | pd_entry_t *pd; | |
| e8510e54 | 1628 | vm_page_t pdpg; |
| 48ffc236 | 1629 | |
| 48ffc236 JG |
1630 | pdpindex = ptepindex >> NPDPEPGSHIFT; |
| 1631 | pml4index = pdpindex >> NPML4EPGSHIFT; | |
| 1632 | ||
| e8510e54 MD |
1633 | /* |
| 1634 | * Locate the PDP page in the PML4, then the PD page in | |
| 1635 | * the PDP. If either does not exist we simply recurse | |
| 1636 | * to allocate them. | |
| 1637 | * | |
| 1638 | * We can just recurse on the PD page as it will recurse | |
| 1639 | * on the PDP if necessary. | |
| 1640 | */ | |
| 48ffc236 JG |
1641 | pml4 = &pmap->pm_pml4[pml4index]; |
| 1642 | if ((*pml4 & PG_V) == 0) { | |
| e8510e54 | 1643 | pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex); |
| 48ffc236 JG |
1644 | pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME); |
| 1645 | pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)]; | |
| c8fe38ae | 1646 | } else { |
| 48ffc236 JG |
1647 | pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME); |
| 1648 | pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)]; | |
| 1649 | if ((*pdp & PG_V) == 0) { | |
| e8510e54 | 1650 | pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex); |
| 48ffc236 | 1651 | } else { |
| 48ffc236 | 1652 | pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME); |
| 1b2e0b92 | 1653 | pdpg->hold_count++; |
| 48ffc236 | 1654 | } |
| c8fe38ae | 1655 | } |
| 48ffc236 | 1656 | |
| e8510e54 MD |
1657 | /* |
| 1658 | * Now fill in the pte in the PD. If the pte already exists | |
| 1659 | * (again, if we raced the grab), unhold pdpg and unwire | |
| 1660 | * m, returning a held m. | |
| 700e22f7 MD |
1661 | * |
| 1662 | * pdpg is left held (hold_count is incremented for | |
| 1663 | * each PT in the PD). | |
| e8510e54 MD |
1664 | */ |
| 1665 | pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME); | |
| 48ffc236 | 1666 | pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)]; |
| 700e22f7 | 1667 | if (*pd != 0) { |
| e8510e54 | 1668 | vm_page_unhold(pdpg); |
| 700e22f7 MD |
1669 | if (--m->wire_count == 0) |
| 1670 | --vmstats.v_wire_count; | |
| e8510e54 MD |
1671 | vm_page_wakeup(m); |
| 1672 | return(m); | |
| 1673 | } | |
| 700e22f7 | 1674 | *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M; |
| c8fe38ae MD |
1675 | } |
| 1676 | ||
| 48ffc236 | 1677 | /* |
| e8510e54 MD |
1678 | * We successfully loaded a PDP, PD, or PTE. Set the page table hint, |
| 1679 | * valid bits, mapped flag, unbusy, and we're done. | |
| 48ffc236 JG |
1680 | */ |
| 1681 | pmap->pm_ptphint = m; | |
| 700e22f7 | 1682 | ++pmap->pm_stats.resident_count; |
| 48ffc236 | 1683 | |
| c8fe38ae MD |
1684 | m->valid = VM_PAGE_BITS_ALL; |
| 1685 | vm_page_flag_clear(m, PG_ZERO); | |
| 1686 | vm_page_flag_set(m, PG_MAPPED); | |
| 1687 | vm_page_wakeup(m); | |
| 1688 | ||
| e8510e54 | 1689 | return (m); |
| c8fe38ae MD |
1690 | } |
| 1691 | ||
| bfc09ba0 MD |
1692 | static |
| 1693 | vm_page_t | |
| c8fe38ae | 1694 | pmap_allocpte(pmap_t pmap, vm_offset_t va) |
| d7f50089 | 1695 | { |
| c8fe38ae | 1696 | vm_pindex_t ptepindex; |
| 48ffc236 | 1697 | pd_entry_t *pd; |
| c8fe38ae MD |
1698 | vm_page_t m; |
| 1699 | ||
| 1700 | /* | |
| 1701 | * Calculate pagetable page index | |
| 1702 | */ | |
| 48ffc236 | 1703 | ptepindex = pmap_pde_pindex(va); |
| c8fe38ae MD |
1704 | |
| 1705 | /* | |
| 1706 | * Get the page directory entry | |
| 1707 | */ | |
| 48ffc236 | 1708 | pd = pmap_pde(pmap, va); |
| c8fe38ae MD |
1709 | |
| 1710 | /* | |
| 48ffc236 | 1711 | * This supports switching from a 2MB page to a |
| c8fe38ae MD |
1712 | * normal 4K page. |
| 1713 | */ | |
| 48ffc236 | 1714 | if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) { |
| 1b2e0b92 | 1715 | panic("no promotion/demotion yet"); |
| 48ffc236 JG |
1716 | *pd = 0; |
| 1717 | pd = NULL; | |
| c8fe38ae MD |
1718 | cpu_invltlb(); |
| 1719 | smp_invltlb(); | |
| 1720 | } | |
| 1721 | ||
| 1722 | /* | |
| 1723 | * If the page table page is mapped, we just increment the | |
| 1724 | * hold count, and activate it. | |
| 1725 | */ | |
| 48ffc236 JG |
1726 | if (pd != NULL && (*pd & PG_V) != 0) { |
| 1727 | /* YYY hint is used here on i386 */ | |
| 1728 | m = pmap_page_lookup( pmap->pm_pteobj, ptepindex); | |
| 1729 | pmap->pm_ptphint = m; | |
| c8fe38ae MD |
1730 | m->hold_count++; |
| 1731 | return m; | |
| 1732 | } | |
| 1733 | /* | |
| 1734 | * Here if the pte page isn't mapped, or if it has been deallocated. | |
| 1735 | */ | |
| 1736 | return _pmap_allocpte(pmap, ptepindex); | |
| d7f50089 YY |
1737 | } |
| 1738 | ||
| c8fe38ae MD |
1739 | |
| 1740 | /*************************************************** | |
| 1741 | * Pmap allocation/deallocation routines. | |
| 1742 | ***************************************************/ | |
| 1743 | ||
| d7f50089 | 1744 | /* |
| c8fe38ae MD |
1745 | * Release any resources held by the given physical map. |
| 1746 | * Called when a pmap initialized by pmap_pinit is being released. | |
| 1747 | * Should only be called if the map contains no valid mappings. | |
| d7f50089 | 1748 | */ |
| c8fe38ae | 1749 | static int pmap_release_callback(struct vm_page *p, void *data); |
| d7f50089 | 1750 | |
| c8fe38ae MD |
1751 | void |
| 1752 | pmap_release(struct pmap *pmap) | |
| d7f50089 | 1753 | { |
| c8fe38ae MD |
1754 | vm_object_t object = pmap->pm_pteobj; |
| 1755 | struct rb_vm_page_scan_info info; | |
| 1756 | ||
| 1757 | KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active)); | |
| 1758 | #if defined(DIAGNOSTIC) | |
| 1759 | if (object->ref_count != 1) | |
| 1760 | panic("pmap_release: pteobj reference count != 1"); | |
| 1761 | #endif | |
| 1762 | ||
| 1763 | info.pmap = pmap; | |
| 1764 | info.object = object; | |
| 1765 | crit_enter(); | |
| 1766 | TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode); | |
| 1767 | crit_exit(); | |
| 1768 | ||
| 1769 | do { | |
| 1770 | crit_enter(); | |
| 1771 | info.error = 0; | |
| 1772 | info.mpte = NULL; | |
| 1773 | info.limit = object->generation; | |
| 1774 | ||
| 1775 | vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, | |
| 1776 | pmap_release_callback, &info); | |
| 1777 | if (info.error == 0 && info.mpte) { | |
| 1778 | if (!pmap_release_free_page(pmap, info.mpte)) | |
| 1779 | info.error = 1; | |
| 1780 | } | |
| 1781 | crit_exit(); | |
| 1782 | } while (info.error); | |
| d7f50089 YY |
1783 | } |
| 1784 | ||
| bfc09ba0 MD |
1785 | static |
| 1786 | int | |
| c8fe38ae | 1787 | pmap_release_callback(struct vm_page *p, void *data) |
| d7f50089 | 1788 | { |
| c8fe38ae MD |
1789 | struct rb_vm_page_scan_info *info = data; |
| 1790 | ||
| 0a5c555b | 1791 | if (p->pindex == NUPDE + NUPDPE + PML4PML4I) { |
| c8fe38ae MD |
1792 | info->mpte = p; |
| 1793 | return(0); | |
| 1794 | } | |
| 1795 | if (!pmap_release_free_page(info->pmap, p)) { | |
| 1796 | info->error = 1; | |
| 1797 | return(-1); | |
| 1798 | } | |
| 1799 | if (info->object->generation != info->limit) { | |
| 1800 | info->error = 1; | |
| 1801 | return(-1); | |
| 1802 | } | |
| 1803 | return(0); | |
| d7f50089 YY |
1804 | } |
| 1805 | ||
| 1806 | /* | |
| c8fe38ae | 1807 | * Grow the number of kernel page table entries, if needed. |
| d7f50089 | 1808 | */ |
| c8fe38ae MD |
1809 | void |
| 1810 | pmap_growkernel(vm_offset_t addr) | |
| d7f50089 | 1811 | { |
| 48ffc236 | 1812 | vm_paddr_t paddr; |
| c8fe38ae MD |
1813 | vm_offset_t ptppaddr; |
| 1814 | vm_page_t nkpg; | |
| 48ffc236 JG |
1815 | pd_entry_t *pde, newpdir; |
| 1816 | pdp_entry_t newpdp; | |
| c8fe38ae MD |
1817 | |
| 1818 | crit_enter(); | |
| 1819 | if (kernel_vm_end == 0) { | |
| 1820 | kernel_vm_end = KERNBASE; | |
| 1821 | nkpt = 0; | |
| 48ffc236 | 1822 | while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) { |
| c8fe38ae MD |
1823 | kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); |
| 1824 | nkpt++; | |
| 48ffc236 JG |
1825 | if (kernel_vm_end - 1 >= kernel_map.max_offset) { |
| 1826 | kernel_vm_end = kernel_map.max_offset; | |
| 1827 | break; | |
| 1828 | } | |
| c8fe38ae MD |
1829 | } |
| 1830 | } | |
| 48ffc236 JG |
1831 | addr = roundup2(addr, PAGE_SIZE * NPTEPG); |
| 1832 | if (addr - 1 >= kernel_map.max_offset) | |
| 1833 | addr = kernel_map.max_offset; | |
| c8fe38ae | 1834 | while (kernel_vm_end < addr) { |
| 48ffc236 JG |
1835 | pde = pmap_pde(&kernel_pmap, kernel_vm_end); |
| 1836 | if (pde == NULL) { | |
| 1837 | /* We need a new PDP entry */ | |
| 1838 | nkpg = vm_page_alloc(kptobj, nkpt, | |
| 1839 | VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | |
| 1840 | | VM_ALLOC_INTERRUPT); | |
| 1841 | if (nkpg == NULL) | |
| 1842 | panic("pmap_growkernel: no memory to grow kernel"); | |
| 48ffc236 | 1843 | paddr = VM_PAGE_TO_PHYS(nkpg); |
| 7f2a2740 MD |
1844 | if ((nkpg->flags & PG_ZERO) == 0) |
| 1845 | pmap_zero_page(paddr); | |
| 1846 | vm_page_flag_clear(nkpg, PG_ZERO); | |
| 48ffc236 JG |
1847 | newpdp = (pdp_entry_t) |
| 1848 | (paddr | PG_V | PG_RW | PG_A | PG_M); | |
| 1849 | *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp; | |
| 7f2a2740 | 1850 | nkpt++; |
| 48ffc236 JG |
1851 | continue; /* try again */ |
| 1852 | } | |
| 1853 | if ((*pde & PG_V) != 0) { | |
| c8fe38ae | 1854 | kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); |
| 48ffc236 JG |
1855 | if (kernel_vm_end - 1 >= kernel_map.max_offset) { |
| 1856 | kernel_vm_end = kernel_map.max_offset; | |
| 1857 | break; | |
| 1858 | } | |
| c8fe38ae MD |
1859 | continue; |
| 1860 | } | |
| 1861 | ||
| 1862 | /* | |
| 1863 | * This index is bogus, but out of the way | |
| 1864 | */ | |
| 48ffc236 | 1865 | nkpg = vm_page_alloc(kptobj, nkpt, |
| c8fe38ae MD |
1866 | VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT); |
| 1867 | if (nkpg == NULL) | |
| 1868 | panic("pmap_growkernel: no memory to grow kernel"); | |
| 1869 | ||
| 1870 | vm_page_wire(nkpg); | |
| 1871 | ptppaddr = VM_PAGE_TO_PHYS(nkpg); | |
| 1872 | pmap_zero_page(ptppaddr); | |
| 7f2a2740 | 1873 | vm_page_flag_clear(nkpg, PG_ZERO); |
| c8fe38ae | 1874 | newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M); |
| c8fe38ae MD |
1875 | *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir; |
| 1876 | nkpt++; | |
| 1877 | ||
| 48ffc236 JG |
1878 | kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); |
| 1879 | if (kernel_vm_end - 1 >= kernel_map.max_offset) { | |
| 1880 | kernel_vm_end = kernel_map.max_offset; | |
| 1881 | break; | |
| c8fe38ae | 1882 | } |
| c8fe38ae MD |
1883 | } |
| 1884 | crit_exit(); | |
| d7f50089 YY |
1885 | } |
| 1886 | ||
| 1887 | /* | |
| c8fe38ae MD |
1888 | * Retire the given physical map from service. |
| 1889 | * Should only be called if the map contains | |
| 1890 | * no valid mappings. | |
| d7f50089 | 1891 | */ |
| c8fe38ae MD |
1892 | void |
| 1893 | pmap_destroy(pmap_t pmap) | |
| d7f50089 | 1894 | { |
| c8fe38ae MD |
1895 | int count; |
| 1896 | ||
| 1897 | if (pmap == NULL) | |
| 1898 | return; | |
| 1899 | ||
| 1900 | count = --pmap->pm_count; | |
| 1901 | if (count == 0) { | |
| 1902 | pmap_release(pmap); | |
| 1903 | panic("destroying a pmap is not yet implemented"); | |
| 1904 | } | |
| d7f50089 YY |
1905 | } |
| 1906 | ||
| 1907 | /* | |
| c8fe38ae | 1908 | * Add a reference to the specified pmap. |
| d7f50089 | 1909 | */ |
| c8fe38ae MD |
1910 | void |
| 1911 | pmap_reference(pmap_t pmap) | |
| d7f50089 | 1912 | { |
| c8fe38ae MD |
1913 | if (pmap != NULL) { |
| 1914 | pmap->pm_count++; | |
| 1915 | } | |
| d7f50089 YY |
1916 | } |
| 1917 | ||
| c8fe38ae MD |
1918 | /*************************************************** |
| 1919 | * page management routines. | |
| 1920 | ***************************************************/ | |
| d7f50089 YY |
1921 | |
| 1922 | /* | |
| 1923 | * free the pv_entry back to the free list. This function may be | |
| 1924 | * called from an interrupt. | |
| 1925 | */ | |
| bfc09ba0 MD |
1926 | static __inline |
| 1927 | void | |
| d7f50089 YY |
1928 | free_pv_entry(pv_entry_t pv) |
| 1929 | { | |
| c8fe38ae | 1930 | pv_entry_count--; |
| 48ffc236 | 1931 | KKASSERT(pv_entry_count >= 0); |
| c8fe38ae | 1932 | zfree(pvzone, pv); |
| d7f50089 YY |
1933 | } |
| 1934 | ||
| 1935 | /* | |
| 1936 | * get a new pv_entry, allocating a block from the system | |
| 1937 | * when needed. This function may be called from an interrupt. | |
| 1938 | */ | |
| bfc09ba0 MD |
1939 | static |
| 1940 | pv_entry_t | |
| d7f50089 YY |
1941 | get_pv_entry(void) |
| 1942 | { | |
| c8fe38ae MD |
1943 | pv_entry_count++; |
| 1944 | if (pv_entry_high_water && | |
| 48ffc236 JG |
1945 | (pv_entry_count > pv_entry_high_water) && |
| 1946 | (pmap_pagedaemon_waken == 0)) { | |
| c8fe38ae | 1947 | pmap_pagedaemon_waken = 1; |
| 48ffc236 | 1948 | wakeup(&vm_pages_needed); |
| c8fe38ae MD |
1949 | } |
| 1950 | return zalloc(pvzone); | |
| d7f50089 YY |
1951 | } |
| 1952 | ||
| 1953 | /* | |
| 1954 | * This routine is very drastic, but can save the system | |
| 1955 | * in a pinch. | |
| 1956 | */ | |
| 1957 | void | |
| 1958 | pmap_collect(void) | |
| 1959 | { | |
| c8fe38ae MD |
1960 | int i; |
| 1961 | vm_page_t m; | |
| 1962 | static int warningdone=0; | |
| 1963 | ||
| 1964 | if (pmap_pagedaemon_waken == 0) | |
| 1965 | return; | |
| 1966 | ||
| 1967 | if (warningdone < 5) { | |
| 1968 | kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n"); | |
| 1969 | warningdone++; | |
| 1970 | } | |
| 1971 | ||
| 1972 | for(i = 0; i < vm_page_array_size; i++) { | |
| 1973 | m = &vm_page_array[i]; | |
| 1974 | if (m->wire_count || m->hold_count || m->busy || | |
| 1975 | (m->flags & PG_BUSY)) | |
| 1976 | continue; | |
| 1977 | pmap_remove_all(m); | |
| 1978 | } | |
| 48ffc236 | 1979 | pmap_pagedaemon_waken = 0; |
| d7f50089 YY |
1980 | } |
| 1981 | ||
| c8fe38ae | 1982 | |
| d7f50089 YY |
1983 | /* |
| 1984 | * If it is the first entry on the list, it is actually | |
| 1985 | * in the header and we must copy the following entry up | |
| 1986 | * to the header. Otherwise we must search the list for | |
| 1987 | * the entry. In either case we free the now unused entry. | |
| 1988 | */ | |
| bfc09ba0 MD |
1989 | static |
| 1990 | int | |
| c8fe38ae MD |
1991 | pmap_remove_entry(struct pmap *pmap, vm_page_t m, |
| 1992 | vm_offset_t va, pmap_inval_info_t info) | |
| 1993 | { | |
| 1994 | pv_entry_t pv; | |
| 1995 | int rtval; | |
| 1996 | ||
| 1997 | crit_enter(); | |
| 1998 | if (m->md.pv_list_count < pmap->pm_stats.resident_count) { | |
| 1999 | TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { | |
| 2000 | if (pmap == pv->pv_pmap && va == pv->pv_va) | |
| 2001 | break; | |
| 2002 | } | |
| 2003 | } else { | |
| 2004 | TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) { | |
| 2005 | if (va == pv->pv_va) | |
| 2006 | break; | |
| 2007 | } | |
| 2008 | } | |
| 2009 | ||
| 2010 | rtval = 0; | |
| 48ffc236 | 2011 | /* JGXXX When can 'pv' be NULL? */ |
| c8fe38ae MD |
2012 | if (pv) { |
| 2013 | TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); | |
| 2014 | m->md.pv_list_count--; | |
| 48ffc236 | 2015 | KKASSERT(m->md.pv_list_count >= 0); |
| c8fe38ae MD |
2016 | if (TAILQ_EMPTY(&m->md.pv_list)) |
| 2017 | vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE); | |
| 2018 | TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist); | |
| 2019 | ++pmap->pm_generation; | |
| 2020 | rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info); | |
| 2021 | free_pv_entry(pv); | |
| 2022 | } | |
| 2023 | crit_exit(); | |
| 2024 | return rtval; | |
| d7f50089 YY |
2025 | } |
| 2026 | ||
| 2027 | /* | |
| c8fe38ae MD |
2028 | * Create a pv entry for page at pa for |
| 2029 | * (pmap, va). | |
| d7f50089 | 2030 | */ |
| bfc09ba0 MD |
2031 | static |
| 2032 | void | |
| d7f50089 YY |
2033 | pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m) |
| 2034 | { | |
| c8fe38ae MD |
2035 | pv_entry_t pv; |
| 2036 | ||
| 2037 | crit_enter(); | |
| 2038 | pv = get_pv_entry(); | |
| 2039 | pv->pv_va = va; | |
| 2040 | pv->pv_pmap = pmap; | |
| 2041 | pv->pv_ptem = mpte; | |
| 2042 | ||
| 2043 | TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist); | |
| 2044 | TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list); | |
| 2045 | m->md.pv_list_count++; | |
| 2046 | ||
| 2047 | crit_exit(); | |
| d7f50089 YY |
2048 | } |
| 2049 | ||
| 2050 | /* | |
| 2051 | * pmap_remove_pte: do the things to unmap a page in a process | |
| 2052 | */ | |
| bfc09ba0 MD |
2053 | static |
| 2054 | int | |
| c8fe38ae MD |
2055 | pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va, |
| 2056 | pmap_inval_info_t info) | |
| 2057 | { | |
| 2058 | pt_entry_t oldpte; | |
| 2059 | vm_page_t m; | |
| 2060 | ||
| 2061 | pmap_inval_add(info, pmap, va); | |
| 2062 | oldpte = pte_load_clear(ptq); | |
| 2063 | if (oldpte & PG_W) | |
| 2064 | pmap->pm_stats.wired_count -= 1; | |
| 2065 | /* | |
| 2066 | * Machines that don't support invlpg, also don't support | |
| 2067 | * PG_G. XXX PG_G is disabled for SMP so don't worry about | |
| 2068 | * the SMP case. | |
| 2069 | */ | |
| 2070 | if (oldpte & PG_G) | |
| 2071 | cpu_invlpg((void *)va); | |
| 2072 | KKASSERT(pmap->pm_stats.resident_count > 0); | |
| 2073 | --pmap->pm_stats.resident_count; | |
| 2074 | if (oldpte & PG_MANAGED) { | |
| 2075 | m = PHYS_TO_VM_PAGE(oldpte); | |
| 2076 | if (oldpte & PG_M) { | |
| 2077 | #if defined(PMAP_DIAGNOSTIC) | |
| 2078 | if (pmap_nw_modified((pt_entry_t) oldpte)) { | |
| 2079 | kprintf( | |
| 48ffc236 | 2080 | "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n", |
| c8fe38ae MD |
2081 | va, oldpte); |
| 2082 | } | |
| 2083 | #endif | |
| 2084 | if (pmap_track_modified(va)) | |
| 2085 | vm_page_dirty(m); | |
| 2086 | } | |
| 2087 | if (oldpte & PG_A) | |
| 2088 | vm_page_flag_set(m, PG_REFERENCED); | |
| 2089 | return pmap_remove_entry(pmap, m, va, info); | |
| 2090 | } else { | |
| 2091 | return pmap_unuse_pt(pmap, va, NULL, info); | |
| 2092 | } | |
| 2093 | ||
| d7f50089 YY |
2094 | return 0; |
| 2095 | } | |
| 2096 | ||
| 2097 | /* | |
| 2098 | * pmap_remove_page: | |
| 2099 | * | |
| 2100 | * Remove a single page from a process address space. | |
| 2101 | * | |
| 2102 | * This function may not be called from an interrupt if the pmap is | |
| 2103 | * not kernel_pmap. | |
| 2104 | */ | |
| bfc09ba0 MD |
2105 | static |
| 2106 | void | |
| c8fe38ae MD |
2107 | pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info) |
| 2108 | { | |
| 48ffc236 | 2109 | pt_entry_t *pte; |
| c8fe38ae | 2110 | |
| 48ffc236 JG |
2111 | pte = pmap_pte(pmap, va); |
| 2112 | if (pte == NULL) | |
| 2113 | return; | |
| 2114 | if ((*pte & PG_V) == 0) | |
| 2115 | return; | |
| 2116 | pmap_remove_pte(pmap, pte, va, info); | |
| d7f50089 YY |
2117 | } |
| 2118 | ||
| 2119 | /* | |
| 2120 | * pmap_remove: | |
| 2121 | * | |
| 2122 | * Remove the given range of addresses from the specified map. | |
| 2123 | * | |
| 2124 | * It is assumed that the start and end are properly | |
| 2125 | * rounded to the page size. | |
| 2126 | * | |
| 2127 | * This function may not be called from an interrupt if the pmap is | |
| 2128 | * not kernel_pmap. | |
| 2129 | */ | |
| 2130 | void | |
| 2131 | pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva) | |
| 2132 | { | |
| 48ffc236 JG |
2133 | vm_offset_t va_next; |
| 2134 | pml4_entry_t *pml4e; | |
| 2135 | pdp_entry_t *pdpe; | |
| 2136 | pd_entry_t ptpaddr, *pde; | |
| 2137 | pt_entry_t *pte; | |
| c8fe38ae MD |
2138 | struct pmap_inval_info info; |
| 2139 | ||
| 2140 | if (pmap == NULL) | |
| 2141 | return; | |
| 2142 | ||
| 2143 | if (pmap->pm_stats.resident_count == 0) | |
| 2144 | return; | |
| 2145 | ||
| 2146 | pmap_inval_init(&info); | |
| 2147 | ||
| 2148 | /* | |
| 2149 | * special handling of removing one page. a very | |
| 2150 | * common operation and easy to short circuit some | |
| 2151 | * code. | |
| 2152 | */ | |
| 48ffc236 JG |
2153 | if (sva + PAGE_SIZE == eva) { |
| 2154 | pde = pmap_pde(pmap, sva); | |
| 2155 | if (pde && (*pde & PG_PS) == 0) { | |
| 2156 | pmap_remove_page(pmap, sva, &info); | |
| 2157 | pmap_inval_flush(&info); | |
| 2158 | return; | |
| 2159 | } | |
| c8fe38ae MD |
2160 | } |
| 2161 | ||
| 48ffc236 JG |
2162 | for (; sva < eva; sva = va_next) { |
| 2163 | pml4e = pmap_pml4e(pmap, sva); | |
| 2164 | if ((*pml4e & PG_V) == 0) { | |
| 2165 | va_next = (sva + NBPML4) & ~PML4MASK; | |
| 2166 | if (va_next < sva) | |
| 2167 | va_next = eva; | |
| 2168 | continue; | |
| 2169 | } | |
| c8fe38ae | 2170 | |
| 48ffc236 JG |
2171 | pdpe = pmap_pml4e_to_pdpe(pml4e, sva); |
| 2172 | if ((*pdpe & PG_V) == 0) { | |
| 2173 | va_next = (sva + NBPDP) & ~PDPMASK; | |
| 2174 | if (va_next < sva) | |
| 2175 | va_next = eva; | |
| 2176 | continue; | |
| 2177 | } | |
| c8fe38ae MD |
2178 | |
| 2179 | /* | |
| 2180 | * Calculate index for next page table. | |
| 2181 | */ | |
| 48ffc236 JG |
2182 | va_next = (sva + NBPDR) & ~PDRMASK; |
| 2183 | if (va_next < sva) | |
| 2184 | va_next = eva; | |
| c8fe38ae | 2185 | |
| 48ffc236 JG |
2186 | pde = pmap_pdpe_to_pde(pdpe, sva); |
| 2187 | ptpaddr = *pde; | |
| c8fe38ae MD |
2188 | |
| 2189 | /* | |
| 48ffc236 | 2190 | * Weed out invalid mappings. |
| c8fe38ae MD |
2191 | */ |
| 2192 | if (ptpaddr == 0) | |
| 2193 | continue; | |
| 2194 | ||
| 2195 | /* | |
| 48ffc236 JG |
2196 | * Check for large page. |
| 2197 | */ | |
| 2198 | if ((ptpaddr & PG_PS) != 0) { | |
| 2199 | /* JG FreeBSD has more complex treatment here */ | |
| 2200 | pmap_inval_add(&info, pmap, -1); | |
| 2201 | *pde = 0; | |
| 2202 | pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE; | |
| 2203 | continue; | |
| 2204 | } | |
| 2205 | ||
| 2206 | /* | |
| c8fe38ae MD |
2207 | * Limit our scan to either the end of the va represented |
| 2208 | * by the current page table page, or to the end of the | |
| 2209 | * range being removed. | |
| 2210 | */ | |
| 48ffc236 JG |
2211 | if (va_next > eva) |
| 2212 | va_next = eva; | |
| c8fe38ae MD |
2213 | |
| 2214 | /* | |
| 2215 | * NOTE: pmap_remove_pte() can block. | |
| 2216 | */ | |
| 48ffc236 JG |
2217 | for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++, |
| 2218 | sva += PAGE_SIZE) { | |
| 2219 | if (*pte == 0) | |
| c8fe38ae | 2220 | continue; |
| 48ffc236 | 2221 | if (pmap_remove_pte(pmap, pte, sva, &info)) |
| c8fe38ae MD |
2222 | break; |
| 2223 | } | |
| 2224 | } | |
| 2225 | pmap_inval_flush(&info); | |
| d7f50089 YY |
2226 | } |
| 2227 | ||
| 2228 | /* | |
| 2229 | * pmap_remove_all: | |
| 2230 | * | |
| c8fe38ae MD |
2231 | * Removes this physical page from all physical maps in which it resides. |
| 2232 | * Reflects back modify bits to the pager. | |
| d7f50089 | 2233 | * |
| c8fe38ae | 2234 | * This routine may not be called from an interrupt. |
| d7f50089 | 2235 | */ |
| c8fe38ae | 2236 | |
| bfc09ba0 MD |
2237 | static |
| 2238 | void | |
| d7f50089 YY |
2239 | pmap_remove_all(vm_page_t m) |
| 2240 | { | |
| c8fe38ae MD |
2241 | struct pmap_inval_info info; |
| 2242 | pt_entry_t *pte, tpte; | |
| 2243 | pv_entry_t pv; | |
| 2244 | ||
| 2245 | if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) | |
| 2246 | return; | |
| 2247 | ||
| 2248 | pmap_inval_init(&info); | |
| 2249 | crit_enter(); | |
| 2250 | while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) { | |
| 2251 | KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0); | |
| 2252 | --pv->pv_pmap->pm_stats.resident_count; | |
| 2253 | ||
| 2254 | pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); | |
| 2255 | pmap_inval_add(&info, pv->pv_pmap, pv->pv_va); | |
| 2256 | tpte = pte_load_clear(pte); | |
| 2257 | ||
| 2258 | if (tpte & PG_W) | |
| 2259 | pv->pv_pmap->pm_stats.wired_count--; | |
| 2260 | ||
| 2261 | if (tpte & PG_A) | |
| 2262 | vm_page_flag_set(m, PG_REFERENCED); | |
| 2263 | ||
| 2264 | /* | |
| 2265 | * Update the vm_page_t clean and reference bits. | |
| 2266 | */ | |
| 2267 | if (tpte & PG_M) { | |
| 2268 | #if defined(PMAP_DIAGNOSTIC) | |
| 48ffc236 | 2269 | if (pmap_nw_modified(tpte)) { |
| c8fe38ae | 2270 | kprintf( |
| 48ffc236 | 2271 | "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n", |
| c8fe38ae MD |
2272 | pv->pv_va, tpte); |
| 2273 | } | |
| 2274 | #endif | |
| 2275 | if (pmap_track_modified(pv->pv_va)) | |
| 2276 | vm_page_dirty(m); | |
| 2277 | } | |
| 2278 | TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); | |
| 2279 | TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist); | |
| 2280 | ++pv->pv_pmap->pm_generation; | |
| 2281 | m->md.pv_list_count--; | |
| 48ffc236 | 2282 | KKASSERT(m->md.pv_list_count >= 0); |
| c8fe38ae MD |
2283 | if (TAILQ_EMPTY(&m->md.pv_list)) |
| 2284 | vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE); | |
| 2285 | pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info); | |
| 2286 | free_pv_entry(pv); | |
| 2287 | } | |
| 2288 | crit_exit(); | |
| 2289 | KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0); | |
| 2290 | pmap_inval_flush(&info); | |
| d7f50089 YY |
2291 | } |
| 2292 | ||
| 2293 | /* | |
| 2294 | * pmap_protect: | |
| 2295 | * | |
| 2296 | * Set the physical protection on the specified range of this map | |
| 2297 | * as requested. | |
| 2298 | * | |
| 2299 | * This function may not be called from an interrupt if the map is | |
| 2300 | * not the kernel_pmap. | |
| 2301 | */ | |
| 2302 | void | |
| 2303 | pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot) | |
| 2304 | { | |
| 48ffc236 JG |
2305 | vm_offset_t va_next; |
| 2306 | pml4_entry_t *pml4e; | |
| 2307 | pdp_entry_t *pdpe; | |
| 2308 | pd_entry_t ptpaddr, *pde; | |
| 2309 | pt_entry_t *pte; | |
| c8fe38ae MD |
2310 | pmap_inval_info info; |
| 2311 | ||
| 48ffc236 JG |
2312 | /* JG review for NX */ |
| 2313 | ||
| c8fe38ae MD |
2314 | if (pmap == NULL) |
| 2315 | return; | |
| 2316 | ||
| 2317 | if ((prot & VM_PROT_READ) == VM_PROT_NONE) { | |
| 2318 | pmap_remove(pmap, sva, eva); | |
| 2319 | return; | |
| 2320 | } | |
| 2321 | ||
| 2322 | if (prot & VM_PROT_WRITE) | |
| 2323 | return; | |
| 2324 | ||
| 2325 | pmap_inval_init(&info); | |
| 2326 | ||
| 48ffc236 | 2327 | for (; sva < eva; sva = va_next) { |
| c8fe38ae | 2328 | |
| 48ffc236 JG |
2329 | pml4e = pmap_pml4e(pmap, sva); |
| 2330 | if ((*pml4e & PG_V) == 0) { | |
| 2331 | va_next = (sva + NBPML4) & ~PML4MASK; | |
| 2332 | if (va_next < sva) | |
| 2333 | va_next = eva; | |
| 2334 | continue; | |
| 2335 | } | |
| c8fe38ae | 2336 | |
| 48ffc236 JG |
2337 | pdpe = pmap_pml4e_to_pdpe(pml4e, sva); |
| 2338 | if ((*pdpe & PG_V) == 0) { | |
| 2339 | va_next = (sva + NBPDP) & ~PDPMASK; | |
| 2340 | if (va_next < sva) | |
| 2341 | va_next = eva; | |
| 2342 | continue; | |
| 2343 | } | |
| c8fe38ae | 2344 | |
| 48ffc236 JG |
2345 | va_next = (sva + NBPDR) & ~PDRMASK; |
| 2346 | if (va_next < sva) | |
| 2347 | va_next = eva; | |
| c8fe38ae | 2348 | |
| 48ffc236 JG |
2349 | pde = pmap_pdpe_to_pde(pdpe, sva); |
| 2350 | ptpaddr = *pde; | |
| c8fe38ae | 2351 | |
| 48ffc236 JG |
2352 | /* |
| 2353 | * Check for large page. | |
| 2354 | */ | |
| 2355 | if ((ptpaddr & PG_PS) != 0) { | |
| c8fe38ae | 2356 | pmap_inval_add(&info, pmap, -1); |
| 48ffc236 | 2357 | *pde &= ~(PG_M|PG_RW); |
| c8fe38ae MD |
2358 | pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE; |
| 2359 | continue; | |
| 2360 | } | |
| 2361 | ||
| 2362 | /* | |
| 2363 | * Weed out invalid mappings. Note: we assume that the page | |
| 2364 | * directory table is always allocated, and in kernel virtual. | |
| 2365 | */ | |
| 2366 | if (ptpaddr == 0) | |
| 2367 | continue; | |
| 2368 | ||
| 48ffc236 JG |
2369 | if (va_next > eva) |
| 2370 | va_next = eva; | |
| c8fe38ae | 2371 | |
| 48ffc236 JG |
2372 | for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++, |
| 2373 | sva += PAGE_SIZE) { | |
| 2374 | pt_entry_t obits, pbits; | |
| c8fe38ae MD |
2375 | vm_page_t m; |
| 2376 | ||
| 2377 | /* | |
| 2378 | * XXX non-optimal. Note also that there can be | |
| 2379 | * no pmap_inval_flush() calls until after we modify | |
| 2380 | * ptbase[sindex] (or otherwise we have to do another | |
| 2381 | * pmap_inval_add() call). | |
| 2382 | */ | |
| 48ffc236 JG |
2383 | pmap_inval_add(&info, pmap, sva); |
| 2384 | obits = pbits = *pte; | |
| 2385 | if ((pbits & PG_V) == 0) | |
| 2386 | continue; | |
| c8fe38ae MD |
2387 | if (pbits & PG_MANAGED) { |
| 2388 | m = NULL; | |
| 2389 | if (pbits & PG_A) { | |
| 48ffc236 | 2390 | m = PHYS_TO_VM_PAGE(pbits & PG_FRAME); |
| c8fe38ae MD |
2391 | vm_page_flag_set(m, PG_REFERENCED); |
| 2392 | pbits &= ~PG_A; | |
| 2393 | } | |
| 2394 | if (pbits & PG_M) { | |
| 48ffc236 | 2395 | if (pmap_track_modified(sva)) { |
| c8fe38ae | 2396 | if (m == NULL) |
| 3cfe1a9f | 2397 | m = PHYS_TO_VM_PAGE(pbits & PG_FRAME); |
| c8fe38ae MD |
2398 | vm_page_dirty(m); |
| 2399 | pbits &= ~PG_M; | |
| 2400 | } | |
| 2401 | } | |
| 2402 | } | |
| 2403 | ||
| 2404 | pbits &= ~PG_RW; | |
| 2405 | ||
| 48ffc236 JG |
2406 | if (pbits != obits) { |
| 2407 | *pte = pbits; | |
| c8fe38ae MD |
2408 | } |
| 2409 | } | |
| 2410 | } | |
| 2411 | pmap_inval_flush(&info); | |
| d7f50089 YY |
2412 | } |
| 2413 | ||
| 2414 | /* | |
| c8fe38ae MD |
2415 | * Insert the given physical page (p) at |
| 2416 | * the specified virtual address (v) in the | |
| 2417 | * target physical map with the protection requested. | |
| d7f50089 | 2418 | * |
| c8fe38ae MD |
2419 | * If specified, the page will be wired down, meaning |
| 2420 | * that the related pte can not be reclaimed. | |
| d7f50089 | 2421 | * |
| c8fe38ae MD |
2422 | * NB: This is the only routine which MAY NOT lazy-evaluate |
| 2423 | * or lose information. That is, this routine must actually | |
| 2424 | * insert this page into the given map NOW. | |
| d7f50089 YY |
2425 | */ |
| 2426 | void | |
| 2427 | pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, | |
| 2428 | boolean_t wired) | |
| 2429 | { | |
| c8fe38ae | 2430 | vm_paddr_t pa; |
| 48ffc236 | 2431 | pd_entry_t *pde; |
| c8fe38ae MD |
2432 | pt_entry_t *pte; |
| 2433 | vm_paddr_t opa; | |
| 48ffc236 | 2434 | pt_entry_t origpte, newpte; |
| c8fe38ae MD |
2435 | vm_page_t mpte; |
| 2436 | pmap_inval_info info; | |
| 2437 | ||
| 2438 | if (pmap == NULL) | |
| 2439 | return; | |
| 2440 | ||
| 48ffc236 | 2441 | va = trunc_page(va); |
| c8fe38ae MD |
2442 | #ifdef PMAP_DIAGNOSTIC |
| 2443 | if (va >= KvaEnd) | |
| 2444 | panic("pmap_enter: toobig"); | |
| 2445 | if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS)) | |
| 48ffc236 | 2446 | panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va); |
| c8fe38ae MD |
2447 | #endif |
| 2448 | if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) { | |
| 2449 | kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n"); | |
| 48ffc236 JG |
2450 | #ifdef DDB |
| 2451 | db_print_backtrace(); | |
| 2452 | #endif | |
| c8fe38ae MD |
2453 | } |
| 2454 | if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) { | |
| 2455 | kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n"); | |
| 48ffc236 JG |
2456 | #ifdef DDB |
| 2457 | db_print_backtrace(); | |
| 2458 | #endif | |
| c8fe38ae MD |
2459 | } |
| 2460 | ||
| 2461 | /* | |
| 2462 | * In the case that a page table page is not | |
| 2463 | * resident, we are creating it here. | |
| 2464 | */ | |
| 48ffc236 | 2465 | if (va < VM_MAX_USER_ADDRESS) |
| c8fe38ae MD |
2466 | mpte = pmap_allocpte(pmap, va); |
| 2467 | else | |
| 2468 | mpte = NULL; | |
| 2469 | ||
| 2470 | pmap_inval_init(&info); | |
| 48ffc236 JG |
2471 | pde = pmap_pde(pmap, va); |
| 2472 | if (pde != NULL && (*pde & PG_V) != 0) { | |
| 2473 | if ((*pde & PG_PS) != 0) | |
| 2474 | panic("pmap_enter: attempted pmap_enter on 2MB page"); | |
| 2475 | pte = pmap_pde_to_pte(pde, va); | |
| 2476 | } else | |
| 2477 | panic("pmap_enter: invalid page directory va=%#lx", va); | |
| 2478 | ||
| 2479 | KKASSERT(pte != NULL); | |
| 2480 | pa = VM_PAGE_TO_PHYS(m); | |
| 48ffc236 | 2481 | origpte = *pte; |
| c8fe38ae MD |
2482 | opa = origpte & PG_FRAME; |
| 2483 | ||
| c8fe38ae MD |
2484 | /* |
| 2485 | * Mapping has not changed, must be protection or wiring change. | |
| 2486 | */ | |
| 2487 | if (origpte && (opa == pa)) { | |
| 2488 | /* | |
| 2489 | * Wiring change, just update stats. We don't worry about | |
| 2490 | * wiring PT pages as they remain resident as long as there | |
| 2491 | * are valid mappings in them. Hence, if a user page is wired, | |
| 2492 | * the PT page will be also. | |
| 2493 | */ | |
| 2494 | if (wired && ((origpte & PG_W) == 0)) | |
| 2495 | pmap->pm_stats.wired_count++; | |
| 2496 | else if (!wired && (origpte & PG_W)) | |
| 2497 | pmap->pm_stats.wired_count--; | |
| 2498 | ||
| 2499 | #if defined(PMAP_DIAGNOSTIC) | |
| 48ffc236 | 2500 | if (pmap_nw_modified(origpte)) { |
| c8fe38ae | 2501 | kprintf( |
| 48ffc236 | 2502 | "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n", |
| c8fe38ae MD |
2503 | va, origpte); |
| 2504 | } | |
| 2505 | #endif | |
| 2506 | ||
| 2507 | /* | |
| 2508 | * Remove the extra pte reference. Note that we cannot | |
| 2509 | * optimize the RO->RW case because we have adjusted the | |
| 2510 | * wiring count above and may need to adjust the wiring | |
| 2511 | * bits below. | |
| 2512 | */ | |
| 2513 | if (mpte) | |
| 2514 | mpte->hold_count--; | |
| 2515 | ||
| 2516 | /* | |
| 2517 | * We might be turning off write access to the page, | |
| 2518 | * so we go ahead and sense modify status. | |
| 2519 | */ | |
| 2520 | if (origpte & PG_MANAGED) { | |
| 2521 | if ((origpte & PG_M) && pmap_track_modified(va)) { | |
| 2522 | vm_page_t om; | |
| 2523 | om = PHYS_TO_VM_PAGE(opa); | |
| 2524 | vm_page_dirty(om); | |
| 2525 | } | |
| 2526 | pa |= PG_MANAGED; | |
| 2527 | KKASSERT(m->flags & PG_MAPPED); | |
| 2528 | } | |
| 2529 | goto validate; | |
| 2530 | } | |
| 2531 | /* | |
| 2532 | * Mapping has changed, invalidate old range and fall through to | |
| 2533 | * handle validating new mapping. | |
| 2534 | */ | |
| 2535 | if (opa) { | |
| 2536 | int err; | |
| 2537 | err = pmap_remove_pte(pmap, pte, va, &info); | |
| 2538 | if (err) | |
| 48ffc236 | 2539 | panic("pmap_enter: pte vanished, va: 0x%lx", va); |
| c8fe38ae MD |
2540 | } |
| 2541 | ||
| 2542 | /* | |
| 2543 | * Enter on the PV list if part of our managed memory. Note that we | |
| 2544 | * raise IPL while manipulating pv_table since pmap_enter can be | |
| 2545 | * called at interrupt time. | |
| 2546 | */ | |
| 2547 | if (pmap_initialized && | |
| 2548 | (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) { | |
| 2549 | pmap_insert_entry(pmap, va, mpte, m); | |
| 2550 | pa |= PG_MANAGED; | |
| 2551 | vm_page_flag_set(m, PG_MAPPED); | |
| 2552 | } | |
| 2553 | ||
| 2554 | /* | |
| 2555 | * Increment counters | |
| 2556 | */ | |
| 2557 | ++pmap->pm_stats.resident_count; | |
| 2558 | if (wired) | |
| 2559 | pmap->pm_stats.wired_count++; | |
| 2560 | ||
| 2561 | validate: | |
| 2562 | /* | |
| 2563 | * Now validate mapping with desired protection/wiring. | |
| 2564 | */ | |
| 48ffc236 | 2565 | newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V); |
| c8fe38ae MD |
2566 | |
| 2567 | if (wired) | |
| 2568 | newpte |= PG_W; | |
| 48ffc236 | 2569 | if (va < VM_MAX_USER_ADDRESS) |
| c8fe38ae MD |
2570 | newpte |= PG_U; |
| 2571 | if (pmap == &kernel_pmap) | |
| 2572 | newpte |= pgeflag; | |
| 2573 | ||
| 2574 | /* | |
| 2575 | * if the mapping or permission bits are different, we need | |
| 2576 | * to update the pte. | |
| 2577 | */ | |
| 2578 | if ((origpte & ~(PG_M|PG_A)) != newpte) { | |
| 2579 | pmap_inval_add(&info, pmap, va); | |
| 2580 | *pte = newpte | PG_A; | |
| 2581 | if (newpte & PG_RW) | |
| 2582 | vm_page_flag_set(m, PG_WRITEABLE); | |
| 2583 | } | |
| 2584 | KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED)); | |
| 2585 | pmap_inval_flush(&info); | |
| d7f50089 YY |
2586 | } |
| 2587 | ||
| 2588 | /* | |
| c8fe38ae MD |
2589 | * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired. |
| 2590 | * This code also assumes that the pmap has no pre-existing entry for this | |
| 2591 | * VA. | |
| d7f50089 | 2592 | * |
| c8fe38ae | 2593 | * This code currently may only be used on user pmaps, not kernel_pmap. |
| d7f50089 | 2594 | */ |
| bfc09ba0 MD |
2595 | static |
| 2596 | void | |
| c8fe38ae | 2597 | pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m) |
| d7f50089 | 2598 | { |
| c8fe38ae MD |
2599 | pt_entry_t *pte; |
| 2600 | vm_paddr_t pa; | |
| 2601 | vm_page_t mpte; | |
| 2602 | vm_pindex_t ptepindex; | |
| 48ffc236 | 2603 | pd_entry_t *ptepa; |
| c8fe38ae MD |
2604 | pmap_inval_info info; |
| 2605 | ||
| 2606 | pmap_inval_init(&info); | |
| 2607 | ||
| 2608 | if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) { | |
| 2609 | kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n"); | |
| 48ffc236 JG |
2610 | #ifdef DDB |
| 2611 | db_print_backtrace(); | |
| 2612 | #endif | |
| c8fe38ae MD |
2613 | } |
| 2614 | if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) { | |
| 2615 | kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n"); | |
| 48ffc236 JG |
2616 | #ifdef DDB |
| 2617 | db_print_backtrace(); | |
| 2618 | #endif | |
| c8fe38ae MD |
2619 | } |
| 2620 | ||
| 2621 | KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */ | |
| 2622 | ||
| 2623 | /* | |
| 2624 | * Calculate the page table page (mpte), allocating it if necessary. | |
| 2625 | * | |
| 2626 | * A held page table page (mpte), or NULL, is passed onto the | |
| 2627 | * section following. | |
| 2628 | */ | |
| 48ffc236 | 2629 | if (va < VM_MAX_USER_ADDRESS) { |
| c8fe38ae MD |
2630 | /* |
| 2631 | * Calculate pagetable page index | |
| 2632 | */ | |
| 48ffc236 | 2633 | ptepindex = pmap_pde_pindex(va); |
| c8fe38ae MD |
2634 | |
| 2635 | do { | |
| 2636 | /* | |
| 2637 | * Get the page directory entry | |
| 2638 | */ | |
| 48ffc236 | 2639 | ptepa = pmap_pde(pmap, va); |
| c8fe38ae MD |
2640 | |
| 2641 | /* | |
| 2642 | * If the page table page is mapped, we just increment | |
| 2643 | * the hold count, and activate it. | |
| 2644 | */ | |
| 48ffc236 JG |
2645 | if (ptepa && (*ptepa & PG_V) != 0) { |
| 2646 | if (*ptepa & PG_PS) | |
| 2647 | panic("pmap_enter_quick: unexpected mapping into 2MB page"); | |
| 2648 | // if (pmap->pm_ptphint && | |
| 2649 | // (pmap->pm_ptphint->pindex == ptepindex)) { | |
| 2650 | // mpte = pmap->pm_ptphint; | |
| 2651 | // } else { | |
| c8fe38ae MD |
2652 | mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex); |
| 2653 | pmap->pm_ptphint = mpte; | |
| 48ffc236 | 2654 | // } |
| c8fe38ae MD |
2655 | if (mpte) |
| 2656 | mpte->hold_count++; | |
| 2657 | } else { | |
| 2658 | mpte = _pmap_allocpte(pmap, ptepindex); | |
| 2659 | } | |
| 2660 | } while (mpte == NULL); | |
| 2661 | } else { | |
| 2662 | mpte = NULL; | |
| 2663 | /* this code path is not yet used */ | |
| 2664 | } | |
| 2665 | ||
| 2666 | /* | |
| 2667 | * With a valid (and held) page directory page, we can just use | |
| 2668 | * vtopte() to get to the pte. If the pte is already present | |
| 2669 | * we do not disturb it. | |
| 2670 | */ | |
| 2671 | pte = vtopte(va); | |
| 2672 | if (*pte & PG_V) { | |
| 2673 | if (mpte) | |
| 48ffc236 | 2674 | pmap_unwire_pte_hold(pmap, va, mpte, &info); |
| c8fe38ae MD |
2675 | pa = VM_PAGE_TO_PHYS(m); |
| 2676 | KKASSERT(((*pte ^ pa) & PG_FRAME) == 0); | |
| 2677 | return; | |
| 2678 | } | |
| 2679 | ||
| 2680 | /* | |
| 2681 | * Enter on the PV list if part of our managed memory | |
| 2682 | */ | |
| 2683 | if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) { | |
| 2684 | pmap_insert_entry(pmap, va, mpte, m); | |
| 2685 | vm_page_flag_set(m, PG_MAPPED); | |
| 2686 | } | |
| 2687 | ||
| 2688 | /* | |
| 2689 | * Increment counters | |
| 2690 | */ | |
| 2691 | ++pmap->pm_stats.resident_count; | |
| 2692 | ||
| 2693 | pa = VM_PAGE_TO_PHYS(m); | |
| 2694 | ||
| 2695 | /* | |
| 2696 | * Now validate mapping with RO protection | |
| 2697 | */ | |
| 2698 | if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) | |
| 2699 | *pte = pa | PG_V | PG_U; | |
| 2700 | else | |
| 2701 | *pte = pa | PG_V | PG_U | PG_MANAGED; | |
| 2702 | /* pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */ | |
| 2703 | pmap_inval_flush(&info); | |
| d7f50089 YY |
2704 | } |
| 2705 | ||
| 2706 | /* | |
| c8fe38ae MD |
2707 | * Make a temporary mapping for a physical address. This is only intended |
| 2708 | * to be used for panic dumps. | |
| d7f50089 | 2709 | */ |
| c1543a89 | 2710 | /* JG Needed on x86_64? */ |
| c8fe38ae MD |
2711 | void * |
| 2712 | pmap_kenter_temporary(vm_paddr_t pa, int i) | |
| d7f50089 | 2713 | { |
| c8fe38ae MD |
2714 | pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa); |
| 2715 | return ((void *)crashdumpmap); | |
| d7f50089 YY |
2716 | } |
| 2717 | ||
| c8fe38ae MD |
2718 | #define MAX_INIT_PT (96) |
| 2719 | ||
| d7f50089 YY |
2720 | /* |
| 2721 | * This routine preloads the ptes for a given object into the specified pmap. | |
| 2722 | * This eliminates the blast of soft faults on process startup and | |
| 2723 | * immediately after an mmap. | |
| 2724 | */ | |
| 2725 | static int pmap_object_init_pt_callback(vm_page_t p, void *data); | |
| 2726 | ||
| 2727 | void | |
| 2728 | pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot, | |
| 2729 | vm_object_t object, vm_pindex_t pindex, | |
| 2730 | vm_size_t size, int limit) | |
| 2731 | { | |
| c8fe38ae MD |
2732 | struct rb_vm_page_scan_info info; |
| 2733 | struct lwp *lp; | |
| 48ffc236 | 2734 | vm_size_t psize; |
| c8fe38ae MD |
2735 | |
| 2736 | /* | |
| 2737 | * We can't preinit if read access isn't set or there is no pmap | |
| 2738 | * or object. | |
| 2739 | */ | |
| 2740 | if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL) | |
| 2741 | return; | |
| 2742 | ||
| 2743 | /* | |
| 2744 | * We can't preinit if the pmap is not the current pmap | |
| 2745 | */ | |
| 2746 | lp = curthread->td_lwp; | |
| 2747 | if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace)) | |
| 2748 | return; | |
| 2749 | ||
| c1543a89 | 2750 | psize = x86_64_btop(size); |
| c8fe38ae MD |
2751 | |
| 2752 | if ((object->type != OBJT_VNODE) || | |
| 2753 | ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) && | |
| 2754 | (object->resident_page_count > MAX_INIT_PT))) { | |
| 2755 | return; | |
| 2756 | } | |
| 2757 | ||
| 2758 | if (psize + pindex > object->size) { | |
| 2759 | if (object->size < pindex) | |
| 2760 | return; | |
| 2761 | psize = object->size - pindex; | |
| 2762 | } | |
| 2763 | ||
| 2764 | if (psize == 0) | |
| 2765 | return; | |
| 2766 | ||
| 2767 | /* | |
| 2768 | * Use a red-black scan to traverse the requested range and load | |
| 2769 | * any valid pages found into the pmap. | |
| 2770 | * | |
| 2771 | * We cannot safely scan the object's memq unless we are in a | |
| 2772 | * critical section since interrupts can remove pages from objects. | |
| 2773 | */ | |
| 2774 | info.start_pindex = pindex; | |
| 2775 | info.end_pindex = pindex + psize - 1; | |
| 2776 | info.limit = limit; | |
| 2777 | info.mpte = NULL; | |
| 2778 | info.addr = addr; | |
| 2779 | info.pmap = pmap; | |
| 2780 | ||
| 2781 | crit_enter(); | |
| 2782 | vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp, | |
| 2783 | pmap_object_init_pt_callback, &info); | |
| 2784 | crit_exit(); | |
| d7f50089 YY |
2785 | } |
| 2786 | ||
| 2787 | static | |
| 2788 | int | |
| 2789 | pmap_object_init_pt_callback(vm_page_t p, void *data) | |
| 2790 | { | |
| c8fe38ae MD |
2791 | struct rb_vm_page_scan_info *info = data; |
| 2792 | vm_pindex_t rel_index; | |
| 2793 | /* | |
| 2794 | * don't allow an madvise to blow away our really | |
| 2795 | * free pages allocating pv entries. | |
| 2796 | */ | |
| 2797 | if ((info->limit & MAP_PREFAULT_MADVISE) && | |
| 2798 | vmstats.v_free_count < vmstats.v_free_reserved) { | |
| 2799 | return(-1); | |
| 2800 | } | |
| 2801 | if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && | |
| 2802 | (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { | |
| 2803 | if ((p->queue - p->pc) == PQ_CACHE) | |
| 2804 | vm_page_deactivate(p); | |
| 2805 | vm_page_busy(p); | |
| 2806 | rel_index = p->pindex - info->start_pindex; | |
| 2807 | pmap_enter_quick(info->pmap, | |
| c1543a89 | 2808 | info->addr + x86_64_ptob(rel_index), p); |
| c8fe38ae MD |
2809 | vm_page_wakeup(p); |
| 2810 | } | |
| d7f50089 YY |
2811 | return(0); |
| 2812 | } | |
| 2813 | ||
| 2814 | /* | |
| 2815 | * pmap_prefault provides a quick way of clustering pagefaults into a | |
| 2816 | * processes address space. It is a "cousin" of pmap_object_init_pt, | |
| 2817 | * except it runs at page fault time instead of mmap time. | |
| 2818 | */ | |
| 2819 | #define PFBAK 4 | |
| 2820 | #define PFFOR 4 | |
| 2821 | #define PAGEORDER_SIZE (PFBAK+PFFOR) | |
| 2822 | ||
| 2823 | static int pmap_prefault_pageorder[] = { | |
| 2824 | -PAGE_SIZE, PAGE_SIZE, | |
| 2825 | -2 * PAGE_SIZE, 2 * PAGE_SIZE, | |
| 2826 | -3 * PAGE_SIZE, 3 * PAGE_SIZE, | |
| 2827 | -4 * PAGE_SIZE, 4 * PAGE_SIZE | |
| 2828 | }; | |
| 2829 | ||
| 2830 | void | |
| 2831 | pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry) | |
| 2832 | { | |
| c8fe38ae MD |
2833 | int i; |
| 2834 | vm_offset_t starta; | |
| 2835 | vm_offset_t addr; | |
| 2836 | vm_pindex_t pindex; | |
| 2837 | vm_page_t m; | |
| 2838 | vm_object_t object; | |
| 2839 | struct lwp *lp; | |
| 2840 | ||
| 2841 | /* | |
| 2842 | * We do not currently prefault mappings that use virtual page | |
| 2843 | * tables. We do not prefault foreign pmaps. | |
| 2844 | */ | |
| 2845 | if (entry->maptype == VM_MAPTYPE_VPAGETABLE) | |
| 2846 | return; | |
| 2847 | lp = curthread->td_lwp; | |
| 2848 | if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace))) | |
| 2849 | return; | |
| 2850 | ||
| 2851 | object = entry->object.vm_object; | |
| 2852 | ||
| 2853 | starta = addra - PFBAK * PAGE_SIZE; | |
| 2854 | if (starta < entry->start) | |
| 2855 | starta = entry->start; | |
| 2856 | else if (starta > addra) | |
| 2857 | starta = 0; | |
| 2858 | ||
| 2859 | /* | |
| 2860 | * critical section protection is required to maintain the | |
| 2861 | * page/object association, interrupts can free pages and remove | |
| 2862 | * them from their objects. | |
| 2863 | */ | |
| 2864 | crit_enter(); | |
| 2865 | for (i = 0; i < PAGEORDER_SIZE; i++) { | |
| 2866 | vm_object_t lobject; | |
| 2867 | pt_entry_t *pte; | |
| 665d7d25 | 2868 | pd_entry_t *pde; |
| c8fe38ae MD |
2869 | |
| 2870 | addr = addra + pmap_prefault_pageorder[i]; | |
| 2871 | if (addr > addra + (PFFOR * PAGE_SIZE)) | |
| 2872 | addr = 0; | |
| 2873 | ||
| 2874 | if (addr < starta || addr >= entry->end) | |
| 2875 | continue; | |
| 2876 | ||
| 665d7d25 MD |
2877 | pde = pmap_pde(pmap, addr); |
| 2878 | if (pde == NULL || *pde == 0) | |
| c8fe38ae MD |
2879 | continue; |
| 2880 | ||
| 2881 | pte = vtopte(addr); | |
| 2882 | if (*pte) | |
| 2883 | continue; | |
| 2884 | ||
| 2885 | pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; | |
| 2886 | lobject = object; | |
| 2887 | ||
| 2888 | for (m = vm_page_lookup(lobject, pindex); | |
| 2889 | (!m && (lobject->type == OBJT_DEFAULT) && | |
| 2890 | (lobject->backing_object)); | |
| 2891 | lobject = lobject->backing_object | |
| 2892 | ) { | |
| 2893 | if (lobject->backing_object_offset & PAGE_MASK) | |
| 2894 | break; | |
| 2895 | pindex += (lobject->backing_object_offset >> PAGE_SHIFT); | |
| 2896 | m = vm_page_lookup(lobject->backing_object, pindex); | |
| 2897 | } | |
| 2898 | ||
| 2899 | /* | |
| 2900 | * give-up when a page is not in memory | |
| 2901 | */ | |
| 2902 | if (m == NULL) | |
| 2903 | break; | |
| 2904 | ||
| 2905 | if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && | |
| 2906 | (m->busy == 0) && | |
| 2907 | (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { | |
| 2908 | ||
| 2909 | if ((m->queue - m->pc) == PQ_CACHE) { | |
| 2910 | vm_page_deactivate(m); | |
| 2911 | } | |
| 2912 | vm_page_busy(m); | |
| 2913 | pmap_enter_quick(pmap, addr, m); | |
| 2914 | vm_page_wakeup(m); | |
| 2915 | } | |
| 2916 | } | |
| 2917 | crit_exit(); | |
| d7f50089 YY |
2918 | } |
| 2919 | ||
| 2920 | /* | |
| 2921 | * Routine: pmap_change_wiring | |
| 2922 | * Function: Change the wiring attribute for a map/virtual-address | |
| 2923 | * pair. | |
| 2924 | * In/out conditions: | |
| 2925 | * The mapping must already exist in the pmap. | |
| 2926 | */ | |
| 2927 | void | |
| 2928 | pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired) | |
| 2929 | { | |
| c8fe38ae MD |
2930 | pt_entry_t *pte; |
| 2931 | ||
| 2932 | if (pmap == NULL) | |
| 2933 | return; | |
| 2934 | ||
| 2935 | pte = pmap_pte(pmap, va); | |
| 2936 | ||
| 2937 | if (wired && !pmap_pte_w(pte)) | |
| 2938 | pmap->pm_stats.wired_count++; | |
| 2939 | else if (!wired && pmap_pte_w(pte)) | |
| 2940 | pmap->pm_stats.wired_count--; | |
| 2941 | ||
| 2942 | /* | |
| 2943 | * Wiring is not a hardware characteristic so there is no need to | |
| 2944 | * invalidate TLB. However, in an SMP environment we must use | |
| 2945 | * a locked bus cycle to update the pte (if we are not using | |
| 2946 | * the pmap_inval_*() API that is)... it's ok to do this for simple | |
| 2947 | * wiring changes. | |
| 2948 | */ | |
| 2949 | #ifdef SMP | |
| 2950 | if (wired) | |
| 71577ce5 | 2951 | atomic_set_long(pte, PG_W); |
| c8fe38ae | 2952 | else |
| 71577ce5 | 2953 | atomic_clear_long(pte, PG_W); |
| c8fe38ae MD |
2954 | #else |
| 2955 | if (wired) | |
| 71577ce5 | 2956 | atomic_set_long_nonlocked(pte, PG_W); |
| c8fe38ae | 2957 | else |
| 71577ce5 | 2958 | atomic_clear_long_nonlocked(pte, PG_W); |
| c8fe38ae | 2959 | #endif |
| d7f50089 YY |
2960 | } |
| 2961 | ||
| c8fe38ae MD |
2962 | |
| 2963 | ||
| d7f50089 YY |
2964 | /* |
| 2965 | * Copy the range specified by src_addr/len | |
| 2966 | * from the source map to the range dst_addr/len | |
| 2967 | * in the destination map. | |
| 2968 | * | |
| 2969 | * This routine is only advisory and need not do anything. | |
| 2970 | */ | |
| 2971 | void | |
| 2972 | pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, | |
| bfc09ba0 | 2973 | vm_size_t len, vm_offset_t src_addr) |
| d7f50089 | 2974 | { |
| bfc09ba0 MD |
2975 | return; |
| 2976 | #if 0 | |
| c8fe38ae MD |
2977 | pmap_inval_info info; |
| 2978 | vm_offset_t addr; | |
| 2979 | vm_offset_t end_addr = src_addr + len; | |
| 2980 | vm_offset_t pdnxt; | |
| 2981 | pd_entry_t src_frame, dst_frame; | |
| 2982 | vm_page_t m; | |
| 2983 | ||
| 2984 | if (dst_addr != src_addr) | |
| 2985 | return; | |
| 48ffc236 | 2986 | #if JGPMAP32 |
| c8fe38ae MD |
2987 | src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME; |
| 2988 | if (src_frame != (PTDpde & PG_FRAME)) { | |
| 2989 | return; | |
| 2990 | } | |
| 2991 | ||
| 2992 | dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME; | |
| 2993 | if (dst_frame != (APTDpde & PG_FRAME)) { | |
| 2994 | APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V); | |
| 2995 | /* The page directory is not shared between CPUs */ | |
| 2996 | cpu_invltlb(); | |
| 2997 | } | |
| 48ffc236 | 2998 | #endif |
| c8fe38ae MD |
2999 | pmap_inval_init(&info); |
| 3000 | pmap_inval_add(&info, dst_pmap, -1); | |
| 3001 | pmap_inval_add(&info, src_pmap, -1); | |
| 3002 | ||
| 3003 | /* | |
| 3004 | * critical section protection is required to maintain the page/object | |
| 3005 | * association, interrupts can free pages and remove them from | |
| 3006 | * their objects. | |
| 3007 | */ | |
| 3008 | crit_enter(); | |
| 3009 | for (addr = src_addr; addr < end_addr; addr = pdnxt) { | |
| 3010 | pt_entry_t *src_pte, *dst_pte; | |
| 3011 | vm_page_t dstmpte, srcmpte; | |
| 3012 | vm_offset_t srcptepaddr; | |
| 3013 | vm_pindex_t ptepindex; | |
| 3014 | ||
| 3015 | if (addr >= UPT_MIN_ADDRESS) | |
| 3016 | panic("pmap_copy: invalid to pmap_copy page tables\n"); | |
| 3017 | ||
| 3018 | /* | |
| 3019 | * Don't let optional prefaulting of pages make us go | |
| 3020 | * way below the low water mark of free pages or way | |
| 3021 | * above high water mark of used pv entries. | |
| 3022 | */ | |
| 3023 | if (vmstats.v_free_count < vmstats.v_free_reserved || | |
| 3024 | pv_entry_count > pv_entry_high_water) | |
| 3025 | break; | |
| 3026 | ||
| 3027 | pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1)); | |
| 3028 | ptepindex = addr >> PDRSHIFT; | |
| 3029 | ||
| 48ffc236 | 3030 | #if JGPMAP32 |
| c8fe38ae | 3031 | srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex]; |
| 48ffc236 | 3032 | #endif |
| c8fe38ae MD |
3033 | if (srcptepaddr == 0) |
| 3034 | continue; | |
| 3035 | ||
| 3036 | if (srcptepaddr & PG_PS) { | |
| 48ffc236 | 3037 | #if JGPMAP32 |
| c8fe38ae MD |
3038 | if (dst_pmap->pm_pdir[ptepindex] == 0) { |
| 3039 | dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr; | |
| 3040 | dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE; | |
| 3041 | } | |
| 48ffc236 | 3042 | #endif |
| c8fe38ae MD |
3043 | continue; |
| 3044 | } | |
| 3045 | ||
| 3046 | srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex); | |
| 3047 | if ((srcmpte == NULL) || (srcmpte->hold_count == 0) || | |
| 3048 | (srcmpte->flags & PG_BUSY)) { | |
| 3049 | continue; | |
| 3050 | } | |
| 3051 | ||
| 3052 | if (pdnxt > end_addr) | |
| 3053 | pdnxt = end_addr; | |
| 3054 | ||
| 3055 | src_pte = vtopte(addr); | |
| 48ffc236 | 3056 | #if JGPMAP32 |
| c8fe38ae | 3057 | dst_pte = avtopte(addr); |
| 48ffc236 | 3058 | #endif |
| c8fe38ae MD |
3059 | while (addr < pdnxt) { |
| 3060 | pt_entry_t ptetemp; | |
| 3061 | ||
| 3062 | ptetemp = *src_pte; | |
| 3063 | /* | |
| 3064 | * we only virtual copy managed pages | |
| 3065 | */ | |
| 3066 | if ((ptetemp & PG_MANAGED) != 0) { | |
| 3067 | /* | |
| 3068 | * We have to check after allocpte for the | |
| 3069 | * pte still being around... allocpte can | |
| 3070 | * block. | |
| 3071 | * | |
| 3072 | * pmap_allocpte() can block. If we lose | |
| 3073 | * our page directory mappings we stop. | |
| 3074 | */ | |
| 3075 | dstmpte = pmap_allocpte(dst_pmap, addr); | |
| 3076 | ||
| 48ffc236 | 3077 | #if JGPMAP32 |
| c8fe38ae MD |
3078 | if (src_frame != (PTDpde & PG_FRAME) || |
| 3079 | dst_frame != (APTDpde & PG_FRAME) | |
| 3080 | ) { | |
| 3081 | kprintf("WARNING: pmap_copy: detected and corrected race\n"); | |
| 3082 | pmap_unwire_pte_hold(dst_pmap, dstmpte, &info); | |
| 3083 | goto failed; | |
| 3084 | } else if ((*dst_pte == 0) && | |
| 3085 | (ptetemp = *src_pte) != 0 && | |
| 3086 | (ptetemp & PG_MANAGED)) { | |
| 3087 | /* | |
| 3088 | * Clear the modified and | |
| 3089 | * accessed (referenced) bits | |
| 3090 | * during the copy. | |
| 3091 | */ | |
| 3092 | m = PHYS_TO_VM_PAGE(ptetemp); | |
| 3093 | *dst_pte = ptetemp & ~(PG_M | PG_A); | |
| 3094 | ++dst_pmap->pm_stats.resident_count; | |
| 3095 | pmap_insert_entry(dst_pmap, addr, | |
| 3096 | dstmpte, m); | |
| 3097 | KKASSERT(m->flags & PG_MAPPED); | |
| 3098 | } else { | |
| 3099 | kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n"); | |
| 3100 | pmap_unwire_pte_hold(dst_pmap, dstmpte, &info); | |
| 3101 | goto failed; | |
| 3102 | } | |
| 48ffc236 | 3103 | #endif |
| c8fe38ae MD |
3104 | if (dstmpte->hold_count >= srcmpte->hold_count) |
| 3105 | break; | |
| 3106 | } | |
| 3107 | addr += PAGE_SIZE; | |
| 3108 | src_pte++; | |
| 3109 | dst_pte++; | |
| 3110 | } | |
| 3111 | } | |
| 3112 | failed: | |
| 3113 | crit_exit(); | |
| 3114 | pmap_inval_flush(&info); | |
| f81851b8 | 3115 | #endif |
| d7f50089 YY |
3116 | } |
| 3117 | ||
| 3118 | /* | |
| 3119 | * pmap_zero_page: | |
| 3120 | * | |
| 48ffc236 | 3121 | * Zero the specified physical page. |
| d7f50089 YY |
3122 | * |
| 3123 | * This function may be called from an interrupt and no locking is | |
| 3124 | * required. | |
| 3125 | */ | |
| 3126 | void | |
| 3127 | pmap_zero_page(vm_paddr_t phys) | |
| 3128 | { | |
| 48ffc236 | 3129 | vm_offset_t va = PHYS_TO_DMAP(phys); |
| c8fe38ae | 3130 | |
| 48ffc236 | 3131 | pagezero((void *)va); |
| d7f50089 YY |
3132 | } |
| 3133 | ||
| 3134 | /* | |
| 3135 | * pmap_page_assertzero: | |
| 3136 | * | |
| 3137 | * Assert that a page is empty, panic if it isn't. | |
| 3138 | */ | |
| 3139 | void | |
| 3140 | pmap_page_assertzero(vm_paddr_t phys) | |
| 3141 | { | |
| 48ffc236 | 3142 | vm_offset_t virt = PHYS_TO_DMAP(phys); |
| bfc09ba0 | 3143 | int i; |
| 48ffc236 | 3144 | |
| bfc09ba0 MD |
3145 | for (i = 0; i < PAGE_SIZE; i += sizeof(long)) { |
| 3146 | if (*(long *)((char *)virt + i) != 0) { | |
| 3147 | panic("pmap_page_assertzero() @ %p not zero!\n", (void *)virt); | |
| c8fe38ae MD |
3148 | } |
| 3149 | } | |
| d7f50089 YY |
3150 | } |
| 3151 | ||
| 3152 | /* | |
| 3153 | * pmap_zero_page: | |
| 3154 | * | |
| 3155 | * Zero part of a physical page by mapping it into memory and clearing | |
| 3156 | * its contents with bzero. | |
| 3157 | * | |
| 3158 | * off and size may not cover an area beyond a single hardware page. | |
| 3159 | */ | |
| 3160 | void | |
| 3161 | pmap_zero_page_area(vm_paddr_t phys, int off, int size) | |
| 3162 | { | |
| 48ffc236 | 3163 | vm_offset_t virt = PHYS_TO_DMAP(phys); |
| bfc09ba0 | 3164 | |
| 48ffc236 | 3165 | bzero((char *)virt + off, size); |
| d7f50089 YY |
3166 | } |
| 3167 | ||
| 3168 | /* | |
| 3169 | * pmap_copy_page: | |
| 3170 | * | |
| 3171 | * Copy the physical page from the source PA to the target PA. | |
| 3172 | * This function may be called from an interrupt. No locking | |
| 3173 | * is required. | |
| 3174 | */ | |
| 3175 | void | |
| 3176 | pmap_copy_page(vm_paddr_t src, vm_paddr_t dst) | |
| 3177 | { | |
| 48ffc236 | 3178 | vm_offset_t src_virt, dst_virt; |
| c8fe38ae | 3179 | |
| 48ffc236 JG |
3180 | src_virt = PHYS_TO_DMAP(src); |
| 3181 | dst_virt = PHYS_TO_DMAP(dst); | |
| bfc09ba0 | 3182 | bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE); |
| d7f50089 YY |
3183 | } |
| 3184 | ||
| 3185 | /* | |
| 3186 | * pmap_copy_page_frag: | |
| 3187 | * | |
| 3188 | * Copy the physical page from the source PA to the target PA. | |
| 3189 | * This function may be called from an interrupt. No locking | |
| 3190 | * is required. | |
| 3191 | */ | |
| 3192 | void | |
| 3193 | pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes) | |
| 3194 | { | |
| 48ffc236 | 3195 | vm_offset_t src_virt, dst_virt; |
| c8fe38ae | 3196 | |
| 48ffc236 JG |
3197 | src_virt = PHYS_TO_DMAP(src); |
| 3198 | dst_virt = PHYS_TO_DMAP(dst); | |
| bfc09ba0 | 3199 | |
| 48ffc236 JG |
3200 | bcopy((char *)src_virt + (src & PAGE_MASK), |
| 3201 | (char *)dst_virt + (dst & PAGE_MASK), | |
| c8fe38ae | 3202 | bytes); |
| d7f50089 YY |
3203 | } |
| 3204 | ||
| 3205 | /* | |
| 3206 | * Returns true if the pmap's pv is one of the first | |
| 3207 | * 16 pvs linked to from this page. This count may | |
| 3208 | * be changed upwards or downwards in the future; it | |
| 3209 | * is only necessary that true be returned for a small | |
| 3210 | * subset of pmaps for proper page aging. | |
| 3211 | */ | |
| 3212 | boolean_t | |
| 3213 | pmap_page_exists_quick(pmap_t pmap, vm_page_t m) | |
| 3214 | { | |
| c8fe38ae MD |
3215 | pv_entry_t pv; |
| 3216 | int loops = 0; | |
| 3217 | ||
| 3218 | if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) | |
| 3219 | return FALSE; | |
| 3220 | ||
| 3221 | crit_enter(); | |
| 3222 | ||
| 3223 | TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { | |
| 3224 | if (pv->pv_pmap == pmap) { | |
| 3225 | crit_exit(); | |
| 3226 | return TRUE; | |
| 3227 | } | |
| 3228 | loops++; | |
| 3229 | if (loops >= 16) | |
| 3230 | break; | |
| 3231 | } | |
| 3232 | crit_exit(); | |
| d7f50089 YY |
3233 | return (FALSE); |
| 3234 | } | |
| 3235 | ||
| 3236 | /* | |
| 3237 | * Remove all pages from specified address space | |
| 3238 | * this aids process exit speeds. Also, this code | |
| 3239 | * is special cased for current process only, but | |
| 3240 | * can have the more generic (and slightly slower) | |
| 3241 | * mode enabled. This is much faster than pmap_remove | |
| 3242 | * in the case of running down an entire address space. | |
| 3243 | */ | |
| 3244 | void | |
| 3245 | pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) | |
| 3246 | { | |
| c8fe38ae MD |
3247 | struct lwp *lp; |
| 3248 | pt_entry_t *pte, tpte; | |
| 3249 | pv_entry_t pv, npv; | |
| 3250 | vm_page_t m; | |
| 3251 | pmap_inval_info info; | |
| 3252 | int iscurrentpmap; | |
| 48ffc236 | 3253 | int save_generation; |
| c8fe38ae MD |
3254 | |
| 3255 | lp = curthread->td_lwp; | |
| 3256 | if (lp && pmap == vmspace_pmap(lp->lwp_vmspace)) | |
| 3257 | iscurrentpmap = 1; | |
| 3258 | else | |
| 3259 | iscurrentpmap = 0; | |
| 3260 | ||
| 3261 | pmap_inval_init(&info); | |
| 3262 | crit_enter(); | |
| 3263 | for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) { | |
| 3264 | if (pv->pv_va >= eva || pv->pv_va < sva) { | |
| 3265 | npv = TAILQ_NEXT(pv, pv_plist); | |
| 3266 | continue; | |
| 3267 | } | |
| 3268 | ||
| 3269 | KKASSERT(pmap == pv->pv_pmap); | |
| 3270 | ||
| 3271 | if (iscurrentpmap) | |
| 3272 | pte = vtopte(pv->pv_va); | |
| 3273 | else | |
| 3274 | pte = pmap_pte_quick(pmap, pv->pv_va); | |
| 3275 | if (pmap->pm_active) | |
| 3276 | pmap_inval_add(&info, pmap, pv->pv_va); | |
| 3277 | ||
| 3278 | /* | |
| 3279 | * We cannot remove wired pages from a process' mapping | |
| 3280 | * at this time | |
| 3281 | */ | |
| 3282 | if (*pte & PG_W) { | |
| 3283 | npv = TAILQ_NEXT(pv, pv_plist); | |
| 3284 | continue; | |
| 3285 | } | |
| 3286 | tpte = pte_load_clear(pte); | |
| 3287 | ||
| 48ffc236 | 3288 | m = PHYS_TO_VM_PAGE(tpte & PG_FRAME); |
| c8fe38ae MD |
3289 | |
| 3290 | KASSERT(m < &vm_page_array[vm_page_array_size], | |
| 48ffc236 | 3291 | ("pmap_remove_pages: bad tpte %lx", tpte)); |
| c8fe38ae MD |
3292 | |
| 3293 | KKASSERT(pmap->pm_stats.resident_count > 0); | |
| 3294 | --pmap->pm_stats.resident_count; | |
| 3295 | ||
| 3296 | /* | |
| 3297 | * Update the vm_page_t clean and reference bits. | |
| 3298 | */ | |
| 3299 | if (tpte & PG_M) { | |
| 3300 | vm_page_dirty(m); | |
| 3301 | } | |
| 3302 | ||
| 3303 | npv = TAILQ_NEXT(pv, pv_plist); | |
| 3304 | TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist); | |
| 3305 | save_generation = ++pmap->pm_generation; | |
| 3306 | ||
| 3307 | m->md.pv_list_count--; | |
| 3308 | TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); | |
| 3309 | if (TAILQ_EMPTY(&m->md.pv_list)) | |
| 3310 | vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE); | |
| 3311 | ||
| 3312 | pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info); | |
| 3313 | free_pv_entry(pv); | |
| 3314 | ||
| 3315 | /* | |
| 3316 | * Restart the scan if we blocked during the unuse or free | |
| 3317 | * calls and other removals were made. | |
| 3318 | */ | |
| 3319 | if (save_generation != pmap->pm_generation) { | |
| 3320 | kprintf("Warning: pmap_remove_pages race-A avoided\n"); | |
| 3321 | pv = TAILQ_FIRST(&pmap->pm_pvlist); | |
| 3322 | } | |
| 3323 | } | |
| 3324 | pmap_inval_flush(&info); | |
| 3325 | crit_exit(); | |
| d7f50089 YY |
3326 | } |
| 3327 | ||
| 3328 | /* | |
| c8fe38ae MD |
3329 | * pmap_testbit tests bits in pte's |
| 3330 | * note that the testbit/clearbit routines are inline, | |
| 3331 | * and a lot of things compile-time evaluate. | |
| d7f50089 | 3332 | */ |
| bfc09ba0 MD |
3333 | static |
| 3334 | boolean_t | |
| d7f50089 YY |
3335 | pmap_testbit(vm_page_t m, int bit) |
| 3336 | { | |
| c8fe38ae MD |
3337 | pv_entry_t pv; |
| 3338 | pt_entry_t *pte; | |
| 3339 | ||
| 3340 | if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) | |
| 3341 | return FALSE; | |
| 3342 | ||
| 3343 | if (TAILQ_FIRST(&m->md.pv_list) == NULL) | |
| 3344 | return FALSE; | |
| 3345 | ||
| 3346 | crit_enter(); | |
| 3347 | ||
| 3348 | TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { | |
| 3349 | /* | |
| 3350 | * if the bit being tested is the modified bit, then | |
| 3351 | * mark clean_map and ptes as never | |
| 3352 | * modified. | |
| 3353 | */ | |
| 3354 | if (bit & (PG_A|PG_M)) { | |
| 3355 | if (!pmap_track_modified(pv->pv_va)) | |
| 3356 | continue; | |
| 3357 | } | |
| 3358 | ||
| 3359 | #if defined(PMAP_DIAGNOSTIC) | |
| 48ffc236 JG |
3360 | if (pv->pv_pmap == NULL) { |
| 3361 | kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va); | |
| c8fe38ae MD |
3362 | continue; |
| 3363 | } | |
| 3364 | #endif | |
| 3365 | pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); | |
| 3366 | if (*pte & bit) { | |
| 3367 | crit_exit(); | |
| 3368 | return TRUE; | |
| 3369 | } | |
| 3370 | } | |
| 3371 | crit_exit(); | |
| d7f50089 YY |
3372 | return (FALSE); |
| 3373 | } | |
| 3374 | ||
| 3375 | /* | |
| c8fe38ae | 3376 | * this routine is used to modify bits in ptes |
| d7f50089 | 3377 | */ |
| bfc09ba0 MD |
3378 | static __inline |
| 3379 | void | |
| d7f50089 YY |
3380 | pmap_clearbit(vm_page_t m, int bit) |
| 3381 | { | |
| c8fe38ae MD |
3382 | struct pmap_inval_info info; |
| 3383 | pv_entry_t pv; | |
| 3384 | pt_entry_t *pte; | |
| 3385 | pt_entry_t pbits; | |
| 3386 | ||
| 3387 | if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) | |
| 3388 | return; | |
| 3389 | ||
| 3390 | pmap_inval_init(&info); | |
| 3391 | crit_enter(); | |
| 3392 | ||
| 3393 | /* | |
| 3394 | * Loop over all current mappings setting/clearing as appropos If | |
| 3395 | * setting RO do we need to clear the VAC? | |
| 3396 | */ | |
| 3397 | TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { | |
| 3398 | /* | |
| 3399 | * don't write protect pager mappings | |
| 3400 | */ | |
| 3401 | if (bit == PG_RW) { | |
| 3402 | if (!pmap_track_modified(pv->pv_va)) | |
| 3403 | continue; | |
| 3404 | } | |
| 3405 | ||
| 3406 | #if defined(PMAP_DIAGNOSTIC) | |
| 48ffc236 JG |
3407 | if (pv->pv_pmap == NULL) { |
| 3408 | kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va); | |
| c8fe38ae MD |
3409 | continue; |
| 3410 | } | |
| 3411 | #endif | |
| 3412 | ||
| 3413 | /* | |
| 3414 | * Careful here. We can use a locked bus instruction to | |
| 3415 | * clear PG_A or PG_M safely but we need to synchronize | |
| 3416 | * with the target cpus when we mess with PG_RW. | |
| 3417 | * | |
| 3418 | * We do not have to force synchronization when clearing | |
| 3419 | * PG_M even for PTEs generated via virtual memory maps, | |
| 3420 | * because the virtual kernel will invalidate the pmap | |
| 3421 | * entry when/if it needs to resynchronize the Modify bit. | |
| 3422 | */ | |
| 3423 | if (bit & PG_RW) | |
| 3424 | pmap_inval_add(&info, pv->pv_pmap, pv->pv_va); | |
| 3425 | pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); | |
| 3426 | again: | |
| 3427 | pbits = *pte; | |
| 3428 | if (pbits & bit) { | |
| 3429 | if (bit == PG_RW) { | |
| 3430 | if (pbits & PG_M) { | |
| 3431 | vm_page_dirty(m); | |
| 48ffc236 | 3432 | atomic_clear_long(pte, PG_M|PG_RW); |
| c8fe38ae MD |
3433 | } else { |
| 3434 | /* | |
| 3435 | * The cpu may be trying to set PG_M | |
| 3436 | * simultaniously with our clearing | |
| 3437 | * of PG_RW. | |
| 3438 | */ | |
| 48ffc236 | 3439 | if (!atomic_cmpset_long(pte, pbits, |
| c8fe38ae MD |
3440 | pbits & ~PG_RW)) |
| 3441 | goto again; | |
| 3442 | } | |
| 3443 | } else if (bit == PG_M) { | |
| 3444 | /* | |
| 3445 | * We could also clear PG_RW here to force | |
| 3446 | * a fault on write to redetect PG_M for | |
| 3447 | * virtual kernels, but it isn't necessary | |
| 3448 | * since virtual kernels invalidate the pte | |
| 3449 | * when they clear the VPTE_M bit in their | |
| 3450 | * virtual page tables. | |
| 3451 | */ | |
| 48ffc236 | 3452 | atomic_clear_long(pte, PG_M); |
| c8fe38ae | 3453 | } else { |
| 48ffc236 | 3454 | atomic_clear_long(pte, bit); |
| c8fe38ae MD |
3455 | } |
| 3456 | } | |
| 3457 | } | |
| 3458 | pmap_inval_flush(&info); | |
| 3459 | crit_exit(); | |
| d7f50089 YY |
3460 | } |
| 3461 | ||
| 3462 | /* | |
| 3463 | * pmap_page_protect: | |
| 3464 | * | |
| 3465 | * Lower the permission for all mappings to a given page. | |
| 3466 | */ | |
| 3467 | void | |
| 3468 | pmap_page_protect(vm_page_t m, vm_prot_t prot) | |
| 3469 | { | |
| 48ffc236 | 3470 | /* JG NX support? */ |
| c8fe38ae MD |
3471 | if ((prot & VM_PROT_WRITE) == 0) { |
| 3472 | if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) { | |
| 3473 | pmap_clearbit(m, PG_RW); | |
| 3474 | vm_page_flag_clear(m, PG_WRITEABLE); | |
| 3475 | } else { | |
| 3476 | pmap_remove_all(m); | |
| 3477 | } | |
| 3478 | } | |
| d7f50089 YY |
3479 | } |
| 3480 | ||
| 3481 | vm_paddr_t | |
| c8fe38ae | 3482 | pmap_phys_address(vm_pindex_t ppn) |
| d7f50089 | 3483 | { |
| c1543a89 | 3484 | return (x86_64_ptob(ppn)); |
| d7f50089 YY |
3485 | } |
| 3486 | ||
| 3487 | /* | |
| 3488 | * pmap_ts_referenced: | |
| 3489 | * | |
| 3490 | * Return a count of reference bits for a page, clearing those bits. | |
| 3491 | * It is not necessary for every reference bit to be cleared, but it | |
| 3492 | * is necessary that 0 only be returned when there are truly no | |
| 3493 | * reference bits set. | |
| 3494 | * | |
| 3495 | * XXX: The exact number of bits to check and clear is a matter that | |
| 3496 | * should be tested and standardized at some point in the future for | |
| 3497 | * optimal aging of shared pages. | |
| 3498 | */ | |
| 3499 | int | |
| 3500 | pmap_ts_referenced(vm_page_t m) | |
| 3501 | { | |
| c8fe38ae MD |
3502 | pv_entry_t pv, pvf, pvn; |
| 3503 | pt_entry_t *pte; | |
| 3504 | int rtval = 0; | |
| 3505 | ||
| 3506 | if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) | |
| 3507 | return (rtval); | |
| 3508 | ||
| 3509 | crit_enter(); | |
| 3510 | ||
| 3511 | if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) { | |
| 3512 | ||
| 3513 | pvf = pv; | |
| 3514 | ||
| 3515 | do { | |
| 3516 | pvn = TAILQ_NEXT(pv, pv_list); | |
| 3517 | ||
| 3518 | TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); | |
| 3519 | ||
| 3520 | TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list); | |
| 3521 | ||
| 3522 | if (!pmap_track_modified(pv->pv_va)) | |
| 3523 | continue; | |
| 3524 | ||
| 3525 | pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); | |
| 3526 | ||
| 3527 | if (pte && (*pte & PG_A)) { | |
| 3528 | #ifdef SMP | |
| 48ffc236 | 3529 | atomic_clear_long(pte, PG_A); |
| c8fe38ae | 3530 | #else |
| 48ffc236 | 3531 | atomic_clear_long_nonlocked(pte, PG_A); |
| c8fe38ae MD |
3532 | #endif |
| 3533 | rtval++; | |
| 3534 | if (rtval > 4) { | |
| 3535 | break; | |
| 3536 | } | |
| 3537 | } | |
| 3538 | } while ((pv = pvn) != NULL && pv != pvf); | |
| 3539 | } | |
| 3540 | crit_exit(); | |
| 3541 | ||
| 3542 | return (rtval); | |
| d7f50089 YY |
3543 | } |
| 3544 | ||
| 3545 | /* | |
| 3546 | * pmap_is_modified: | |
| 3547 | * | |
| 3548 | * Return whether or not the specified physical page was modified | |
| 3549 | * in any physical maps. | |
| 3550 | */ | |
| 3551 | boolean_t | |
| 3552 | pmap_is_modified(vm_page_t m) | |
| 3553 | { | |
| c8fe38ae | 3554 | return pmap_testbit(m, PG_M); |
| d7f50089 YY |
3555 | } |
| 3556 | ||
| 3557 | /* | |
| 3558 | * Clear the modify bits on the specified physical page. | |
| 3559 | */ | |
| 3560 | void | |
| 3561 | pmap_clear_modify(vm_page_t m) | |
| 3562 | { | |
| c8fe38ae | 3563 | pmap_clearbit(m, PG_M); |
| d7f50089 YY |
3564 | } |
| 3565 | ||
| 3566 | /* | |
| 3567 | * pmap_clear_reference: | |
| 3568 | * | |
| 3569 | * Clear the reference bit on the specified physical page. | |
| 3570 | */ | |
| 3571 | void | |
| 3572 | pmap_clear_reference(vm_page_t m) | |
| 3573 | { | |
| c8fe38ae | 3574 | pmap_clearbit(m, PG_A); |
| d7f50089 YY |
3575 | } |
| 3576 | ||
| d7f50089 YY |
3577 | /* |
| 3578 | * Miscellaneous support routines follow | |
| 3579 | */ | |
| 3580 | ||
| bfc09ba0 MD |
3581 | static |
| 3582 | void | |
| d7f50089 YY |
3583 | i386_protection_init(void) |
| 3584 | { | |
| 3585 | int *kp, prot; | |
| 3586 | ||
| 48ffc236 | 3587 | /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */ |
| d7f50089 YY |
3588 | kp = protection_codes; |
| 3589 | for (prot = 0; prot < 8; prot++) { | |
| c8fe38ae MD |
3590 | switch (prot) { |
| 3591 | case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE: | |
| 3592 | /* | |
| 3593 | * Read access is also 0. There isn't any execute bit, | |
| 3594 | * so just make it readable. | |
| 3595 | */ | |
| 3596 | case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE: | |
| 3597 | case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE: | |
| 3598 | case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE: | |
| 3599 | *kp++ = 0; | |
| 3600 | break; | |
| 3601 | case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE: | |
| 3602 | case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE: | |
| 3603 | case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE: | |
| 3604 | case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: | |
| 3605 | *kp++ = PG_RW; | |
| 3606 | break; | |
| 3607 | } | |
| d7f50089 YY |
3608 | } |
| 3609 | } | |
| 3610 | ||
| 3611 | /* | |
| 3612 | * Map a set of physical memory pages into the kernel virtual | |
| 3613 | * address space. Return a pointer to where it is mapped. This | |
| 3614 | * routine is intended to be used for mapping device memory, | |
| 3615 | * NOT real memory. | |
| 3616 | * | |
| 3617 | * NOTE: we can't use pgeflag unless we invalidate the pages one at | |
| 3618 | * a time. | |
| 3619 | */ | |
| 3620 | void * | |
| 3621 | pmap_mapdev(vm_paddr_t pa, vm_size_t size) | |
| 3622 | { | |
| 3623 | vm_offset_t va, tmpva, offset; | |
| c8fe38ae | 3624 | pt_entry_t *pte; |
| d7f50089 YY |
3625 | |
| 3626 | offset = pa & PAGE_MASK; | |
| 3627 | size = roundup(offset + size, PAGE_SIZE); | |
| 3628 | ||
| 3629 | va = kmem_alloc_nofault(&kernel_map, size); | |
| 48ffc236 | 3630 | if (va == 0) |
| d7f50089 YY |
3631 | panic("pmap_mapdev: Couldn't alloc kernel virtual memory"); |
| 3632 | ||
| 48ffc236 | 3633 | pa = pa & ~PAGE_MASK; |
| d7f50089 | 3634 | for (tmpva = va; size > 0;) { |
| c8fe38ae MD |
3635 | pte = vtopte(tmpva); |
| 3636 | *pte = pa | PG_RW | PG_V; /* | pgeflag; */ | |
| d7f50089 YY |
3637 | size -= PAGE_SIZE; |
| 3638 | tmpva += PAGE_SIZE; | |
| 3639 | pa += PAGE_SIZE; | |
| 3640 | } | |
| 3641 | cpu_invltlb(); | |
| 3642 | smp_invltlb(); | |
| 3643 | ||
| 3644 | return ((void *)(va + offset)); | |
| 057877ac JG |
3645 | } |
| 3646 | ||
| 3647 | void * | |
| 3648 | pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size) | |
| 057877ac JG |
3649 | { |
| 3650 | vm_offset_t va, tmpva, offset; | |
| 3651 | pt_entry_t *pte; | |
| 3652 | ||
| 3653 | offset = pa & PAGE_MASK; | |
| 3654 | size = roundup(offset + size, PAGE_SIZE); | |
| 3655 | ||
| 3656 | va = kmem_alloc_nofault(&kernel_map, size); | |
| 3657 | if (va == 0) | |
| 3658 | panic("pmap_mapdev: Couldn't alloc kernel virtual memory"); | |
| 3659 | ||
| 3660 | pa = pa & ~PAGE_MASK; | |
| 3661 | for (tmpva = va; size > 0;) { | |
| 3662 | pte = vtopte(tmpva); | |
| 3663 | *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */ | |
| 3664 | size -= PAGE_SIZE; | |
| 3665 | tmpva += PAGE_SIZE; | |
| 3666 | pa += PAGE_SIZE; | |
| 3667 | } | |
| 3668 | cpu_invltlb(); | |
| 3669 | smp_invltlb(); | |
| 3670 | ||
| 3671 | return ((void *)(va + offset)); | |
| d7f50089 YY |
3672 | } |
| 3673 | ||
| 3674 | void | |
| 3675 | pmap_unmapdev(vm_offset_t va, vm_size_t size) | |
| 3676 | { | |
| 3677 | vm_offset_t base, offset; | |
| 3678 | ||
| 48ffc236 | 3679 | base = va & ~PAGE_MASK; |
| d7f50089 YY |
3680 | offset = va & PAGE_MASK; |
| 3681 | size = roundup(offset + size, PAGE_SIZE); | |
| 3682 | pmap_qremove(va, size >> PAGE_SHIFT); | |
| 3683 | kmem_free(&kernel_map, base, size); | |
| 3684 | } | |
| 3685 | ||
| d7f50089 YY |
3686 | /* |
| 3687 | * perform the pmap work for mincore | |
| 3688 | */ | |
| 3689 | int | |
| 3690 | pmap_mincore(pmap_t pmap, vm_offset_t addr) | |
| 3691 | { | |
| c8fe38ae MD |
3692 | pt_entry_t *ptep, pte; |
| 3693 | vm_page_t m; | |
| 3694 | int val = 0; | |
| 3695 | ||
| 3696 | ptep = pmap_pte(pmap, addr); | |
| 3697 | if (ptep == 0) { | |
| 3698 | return 0; | |
| 3699 | } | |
| d7f50089 | 3700 | |
| c8fe38ae MD |
3701 | if ((pte = *ptep) != 0) { |
| 3702 | vm_offset_t pa; | |
| 3703 | ||
| 3704 | val = MINCORE_INCORE; | |
| 3705 | if ((pte & PG_MANAGED) == 0) | |
| 3706 | return val; | |
| 3707 | ||
| 3708 | pa = pte & PG_FRAME; | |
| 3709 | ||
| 3710 | m = PHYS_TO_VM_PAGE(pa); | |
| 3711 | ||
| 3712 | /* | |
| 3713 | * Modified by us | |
| 3714 | */ | |
| 3715 | if (pte & PG_M) | |
| 3716 | val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER; | |
| 3717 | /* | |
| 3718 | * Modified by someone | |
| 3719 | */ | |
| 3720 | else if (m->dirty || pmap_is_modified(m)) | |
| 3721 | val |= MINCORE_MODIFIED_OTHER; | |
| 3722 | /* | |
| 3723 | * Referenced by us | |
| 3724 | */ | |
| 3725 | if (pte & PG_A) | |
| 3726 | val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER; | |
| 3727 | ||
| 3728 | /* | |
| 3729 | * Referenced by someone | |
| 3730 | */ | |
| 3731 | else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) { | |
| 3732 | val |= MINCORE_REFERENCED_OTHER; | |
| 3733 | vm_page_flag_set(m, PG_REFERENCED); | |
| 3734 | } | |
| 3735 | } | |
| 3736 | return val; | |
| 3737 | } | |
| 3738 | ||
| 3739 | /* | |
| 3740 | * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new | |
| 3741 | * vmspace will be ref'd and the old one will be deref'd. | |
| 3742 | * | |
| 3743 | * The vmspace for all lwps associated with the process will be adjusted | |
| 3744 | * and cr3 will be reloaded if any lwp is the current lwp. | |
| 3745 | */ | |
| d7f50089 YY |
3746 | void |
| 3747 | pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs) | |
| 3748 | { | |
| c8fe38ae MD |
3749 | struct vmspace *oldvm; |
| 3750 | struct lwp *lp; | |
| 3751 | ||
| 3752 | crit_enter(); | |
| 3753 | oldvm = p->p_vmspace; | |
| 3754 | if (oldvm != newvm) { | |
| 3755 | p->p_vmspace = newvm; | |
| 3756 | KKASSERT(p->p_nthreads == 1); | |
| 3757 | lp = RB_ROOT(&p->p_lwp_tree); | |
| 3758 | pmap_setlwpvm(lp, newvm); | |
| 3759 | if (adjrefs) { | |
| 3760 | sysref_get(&newvm->vm_sysref); | |
| 3761 | sysref_put(&oldvm->vm_sysref); | |
| 3762 | } | |
| 3763 | } | |
| 3764 | crit_exit(); | |
| d7f50089 YY |
3765 | } |
| 3766 | ||
| c8fe38ae MD |
3767 | /* |
| 3768 | * Set the vmspace for a LWP. The vmspace is almost universally set the | |
| 3769 | * same as the process vmspace, but virtual kernels need to swap out contexts | |
| 3770 | * on a per-lwp basis. | |
| 3771 | */ | |
| d7f50089 YY |
3772 | void |
| 3773 | pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm) | |
| 3774 | { | |
| c8fe38ae MD |
3775 | struct vmspace *oldvm; |
| 3776 | struct pmap *pmap; | |
| d7f50089 | 3777 | |
| c8fe38ae MD |
3778 | crit_enter(); |
| 3779 | oldvm = lp->lwp_vmspace; | |
| 3780 | ||
| 3781 | if (oldvm != newvm) { | |
| 3782 | lp->lwp_vmspace = newvm; | |
| 3783 | if (curthread->td_lwp == lp) { | |
| 3784 | pmap = vmspace_pmap(newvm); | |
| 3785 | #if defined(SMP) | |
| 3786 | atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid); | |
| 3787 | #else | |
| 3788 | pmap->pm_active |= 1; | |
| 3789 | #endif | |
| 3790 | #if defined(SWTCH_OPTIM_STATS) | |
| 3791 | tlb_flush_count++; | |
| 3792 | #endif | |
| 48ffc236 JG |
3793 | curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4); |
| 3794 | load_cr3(curthread->td_pcb->pcb_cr3); | |
| c8fe38ae MD |
3795 | pmap = vmspace_pmap(oldvm); |
| 3796 | #if defined(SMP) | |
| 3797 | atomic_clear_int(&pmap->pm_active, | |
| 3798 | 1 << mycpu->gd_cpuid); | |
| 3799 | #else | |
| 3800 | pmap->pm_active &= ~1; | |
| 3801 | #endif | |
| 3802 | } | |
| 3803 | } | |
| 3804 | crit_exit(); | |
| 3805 | } | |
| d7f50089 YY |
3806 | |
| 3807 | vm_offset_t | |
| 3808 | pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size) | |
| 3809 | { | |
| c8fe38ae MD |
3810 | |
| 3811 | if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) { | |
| 3812 | return addr; | |
| 3813 | } | |
| 3814 | ||
| 3815 | addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1); | |
| 3816 | return addr; | |
| 3817 | } | |
| 3818 | ||
| 3819 | ||
| 3820 | #if defined(DEBUG) | |
| 3821 | ||
| 3822 | static void pads (pmap_t pm); | |
| 3823 | void pmap_pvdump (vm_paddr_t pa); | |
| 3824 | ||
| 3825 | /* print address space of pmap*/ | |
| bfc09ba0 MD |
3826 | static |
| 3827 | void | |
| c8fe38ae MD |
3828 | pads(pmap_t pm) |
| 3829 | { | |
| 3830 | vm_offset_t va; | |
| 3831 | unsigned i, j; | |
| 3832 | pt_entry_t *ptep; | |
| 3833 | ||
| 3834 | if (pm == &kernel_pmap) | |
| 3835 | return; | |
| 3836 | crit_enter(); | |
| 3837 | for (i = 0; i < NPDEPG; i++) { | |
| f81851b8 | 3838 | ; |
| c8fe38ae MD |
3839 | } |
| 3840 | crit_exit(); | |
| 3841 | ||
| d7f50089 YY |
3842 | } |
| 3843 | ||
| c8fe38ae MD |
3844 | void |
| 3845 | pmap_pvdump(vm_paddr_t pa) | |
| 3846 | { | |
| 3847 | pv_entry_t pv; | |
| 3848 | vm_page_t m; | |
| 3849 | ||
| 3850 | kprintf("pa %08llx", (long long)pa); | |
| 3851 | m = PHYS_TO_VM_PAGE(pa); | |
| 3852 | TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { | |
| 3853 | #ifdef used_to_be | |
| 3854 | kprintf(" -> pmap %p, va %x, flags %x", | |
| 3855 | (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags); | |
| 3856 | #endif | |
| 3857 | kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va); | |
| 3858 | pads(pv->pv_pmap); | |
| 3859 | } | |
| 3860 | kprintf(" "); | |
| 3861 | } | |
| 3862 | #endif |