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