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