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