kernel: Remove some unused variables.
[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");
921c891e
MD
214static int pmap_mmu_optimize = 0;
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 */
841#ifdef SMP
842 pgeflag = 0;
843#else
844 if (cpu_feature & CPUID_PGE)
845 pgeflag = PG_G;
846#endif
847
848/*
849 * Initialize the 4MB page size flag
850 */
851 pseflag = 0;
852/*
853 * The 4MB page version of the initial
854 * kernel page mapping.
855 */
856 pdir4mb = 0;
857
858#if !defined(DISABLE_PSE)
859 if (cpu_feature & CPUID_PSE) {
860 pt_entry_t ptditmp;
861 /*
862 * Note that we have enabled PSE mode
863 */
864 pseflag = PG_PS;
b2b3ffcd 865 ptditmp = *(PTmap + x86_64_btop(KERNBASE));
c8fe38ae
MD
866 ptditmp &= ~(NBPDR - 1);
867 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
868 pdir4mb = ptditmp;
869
870#ifndef SMP
871 /*
872 * Enable the PSE mode. If we are SMP we can't do this
873 * now because the APs will not be able to use it when
874 * they boot up.
875 */
876 load_cr4(rcr4() | CR4_PSE);
877
878 /*
879 * We can do the mapping here for the single processor
880 * case. We simply ignore the old page table page from
881 * now on.
882 */
883 /*
884 * For SMP, we still need 4K pages to bootstrap APs,
885 * PSE will be enabled as soon as all APs are up.
886 */
887 PTD[KPTDI] = (pd_entry_t)ptditmp;
c8fe38ae
MD
888 cpu_invltlb();
889#endif
890 }
891#endif
c8fe38ae 892 cpu_invltlb();
d7f50089
YY
893}
894
c8fe38ae 895#ifdef SMP
d7f50089 896/*
c8fe38ae 897 * Set 4mb pdir for mp startup
d7f50089
YY
898 */
899void
c8fe38ae
MD
900pmap_set_opt(void)
901{
902 if (pseflag && (cpu_feature & CPUID_PSE)) {
903 load_cr4(rcr4() | CR4_PSE);
904 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
c8fe38ae
MD
905 cpu_invltlb();
906 }
907 }
d7f50089 908}
c8fe38ae 909#endif
d7f50089 910
c8fe38ae
MD
911/*
912 * Initialize the pmap module.
913 * Called by vm_init, to initialize any structures that the pmap
914 * system needs to map virtual memory.
915 * pmap_init has been enhanced to support in a fairly consistant
916 * way, discontiguous physical memory.
d7f50089
YY
917 */
918void
c8fe38ae 919pmap_init(void)
d7f50089 920{
c8fe38ae
MD
921 int i;
922 int initial_pvs;
923
924 /*
c8fe38ae
MD
925 * Allocate memory for random pmap data structures. Includes the
926 * pv_head_table.
927 */
928
701c977e 929 for (i = 0; i < vm_page_array_size; i++) {
c8fe38ae
MD
930 vm_page_t m;
931
932 m = &vm_page_array[i];
933 TAILQ_INIT(&m->md.pv_list);
c8fe38ae
MD
934 }
935
936 /*
937 * init the pv free list
938 */
939 initial_pvs = vm_page_array_size;
940 if (initial_pvs < MINPV)
941 initial_pvs = MINPV;
942 pvzone = &pvzone_store;
948209ce
MD
943 pvinit = (void *)kmem_alloc(&kernel_map,
944 initial_pvs * sizeof (struct pv_entry));
945 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry),
946 pvinit, initial_pvs);
c8fe38ae
MD
947
948 /*
949 * Now it is safe to enable pv_table recording.
950 */
951 pmap_initialized = TRUE;
d7f50089
YY
952}
953
c8fe38ae
MD
954/*
955 * Initialize the address space (zone) for the pv_entries. Set a
956 * high water mark so that the system can recover from excessive
957 * numbers of pv entries.
958 */
d7f50089 959void
c8fe38ae 960pmap_init2(void)
d7f50089 961{
c8fe38ae 962 int shpgperproc = PMAP_SHPGPERPROC;
948209ce 963 int entry_max;
c8fe38ae
MD
964
965 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
966 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
967 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
968 pv_entry_high_water = 9 * (pv_entry_max / 10);
948209ce
MD
969
970 /*
971 * Subtract out pages already installed in the zone (hack)
972 */
973 entry_max = pv_entry_max - vm_page_array_size;
974 if (entry_max <= 0)
975 entry_max = 1;
976
977 zinitna(pvzone, &pvzone_obj, NULL, 0, entry_max, ZONE_INTERRUPT, 1);
d7f50089
YY
978}
979
c8fe38ae
MD
980
981/***************************************************
982 * Low level helper routines.....
983 ***************************************************/
984
c8fe38ae
MD
985/*
986 * this routine defines the region(s) of memory that should
987 * not be tested for the modified bit.
988 */
bfc09ba0
MD
989static __inline
990int
701c977e 991pmap_track_modified(vm_pindex_t pindex)
d7f50089 992{
701c977e 993 vm_offset_t va = (vm_offset_t)pindex << PAGE_SHIFT;
c8fe38ae
MD
994 if ((va < clean_sva) || (va >= clean_eva))
995 return 1;
996 else
997 return 0;
d7f50089
YY
998}
999
d7f50089 1000/*
10d6182e 1001 * Extract the physical page address associated with the map/VA pair.
701c977e 1002 * The page must be wired for this to work reliably.
c8fe38ae 1003 *
701c977e
MD
1004 * XXX for the moment we're using pv_find() instead of pv_get(), as
1005 * callers might be expecting non-blocking operation.
d7f50089 1006 */
c8fe38ae
MD
1007vm_paddr_t
1008pmap_extract(pmap_t pmap, vm_offset_t va)
d7f50089 1009{
48ffc236 1010 vm_paddr_t rtval;
701c977e
MD
1011 pv_entry_t pt_pv;
1012 pt_entry_t *ptep;
c8fe38ae 1013
48ffc236 1014 rtval = 0;
701c977e
MD
1015 if (va >= VM_MAX_USER_ADDRESS) {
1016 /*
1017 * Kernel page directories might be direct-mapped and
1018 * there is typically no PV tracking of pte's
1019 */
1020 pd_entry_t *pt;
1021
1022 pt = pmap_pt(pmap, va);
1023 if (pt && (*pt & PG_V)) {
1024 if (*pt & PG_PS) {
1025 rtval = *pt & PG_PS_FRAME;
1026 rtval |= va & PDRMASK;
48ffc236 1027 } else {
eb010d6e 1028 ptep = pmap_pt_to_pte(*pt, va);
701c977e
MD
1029 if (*pt & PG_V) {
1030 rtval = *ptep & PG_FRAME;
1031 rtval |= va & PAGE_MASK;
1032 }
1033 }
1034 }
1035 } else {
1036 /*
1037 * User pages currently do not direct-map the page directory
1038 * and some pages might not used managed PVs. But all PT's
1039 * will have a PV.
1040 */
1041 pt_pv = pv_find(pmap, pmap_pt_pindex(va));
1042 if (pt_pv) {
1043 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
1044 if (*ptep & PG_V) {
1045 rtval = *ptep & PG_FRAME;
1046 rtval |= va & PAGE_MASK;
48ffc236 1047 }
701c977e 1048 pv_drop(pt_pv);
c8fe38ae 1049 }
c8fe38ae 1050 }
48ffc236
JG
1051 return rtval;
1052}
1053
1054/*
10d6182e 1055 * Extract the physical page address associated kernel virtual address.
48ffc236
JG
1056 */
1057vm_paddr_t
1058pmap_kextract(vm_offset_t va)
48ffc236 1059{
701c977e 1060 pd_entry_t pt; /* pt entry in pd */
48ffc236
JG
1061 vm_paddr_t pa;
1062
1063 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
1064 pa = DMAP_TO_PHYS(va);
1065 } else {
701c977e
MD
1066 pt = *vtopt(va);
1067 if (pt & PG_PS) {
1068 pa = (pt & PG_PS_FRAME) | (va & PDRMASK);
48ffc236
JG
1069 } else {
1070 /*
1071 * Beware of a concurrent promotion that changes the
1072 * PDE at this point! For example, vtopte() must not
1073 * be used to access the PTE because it would use the
1074 * new PDE. It is, however, safe to use the old PDE
1075 * because the page table page is preserved by the
1076 * promotion.
1077 */
eb010d6e 1078 pa = *pmap_pt_to_pte(pt, va);
48ffc236
JG
1079 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1080 }
1081 }
1082 return pa;
d7f50089
YY
1083}
1084
c8fe38ae
MD
1085/***************************************************
1086 * Low level mapping routines.....
1087 ***************************************************/
1088
d7f50089 1089/*
c8fe38ae
MD
1090 * Routine: pmap_kenter
1091 * Function:
1092 * Add a wired page to the KVA
1093 * NOTE! note that in order for the mapping to take effect -- you
1094 * should do an invltlb after doing the pmap_kenter().
d7f50089 1095 */
c8fe38ae 1096void
d7f50089
YY
1097pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1098{
c8fe38ae
MD
1099 pt_entry_t *pte;
1100 pt_entry_t npte;
1101 pmap_inval_info info;
1102
701c977e 1103 pmap_inval_init(&info); /* XXX remove */
c8fe38ae
MD
1104 npte = pa | PG_RW | PG_V | pgeflag;
1105 pte = vtopte(va);
701c977e 1106 pmap_inval_interlock(&info, &kernel_pmap, va); /* XXX remove */
c8fe38ae 1107 *pte = npte;
701c977e
MD
1108 pmap_inval_deinterlock(&info, &kernel_pmap); /* XXX remove */
1109 pmap_inval_done(&info); /* XXX remove */
d7f50089
YY
1110}
1111
1112/*
c8fe38ae
MD
1113 * Routine: pmap_kenter_quick
1114 * Function:
1115 * Similar to pmap_kenter(), except we only invalidate the
1116 * mapping on the current CPU.
d7f50089
YY
1117 */
1118void
c8fe38ae
MD
1119pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
1120{
1121 pt_entry_t *pte;
1122 pt_entry_t npte;
1123
1124 npte = pa | PG_RW | PG_V | pgeflag;
1125 pte = vtopte(va);
1126 *pte = npte;
1127 cpu_invlpg((void *)va);
1128}
1129
1130void
d7f50089
YY
1131pmap_kenter_sync(vm_offset_t va)
1132{
c8fe38ae
MD
1133 pmap_inval_info info;
1134
1135 pmap_inval_init(&info);
c2fb025d
MD
1136 pmap_inval_interlock(&info, &kernel_pmap, va);
1137 pmap_inval_deinterlock(&info, &kernel_pmap);
1138 pmap_inval_done(&info);
d7f50089
YY
1139}
1140
d7f50089
YY
1141void
1142pmap_kenter_sync_quick(vm_offset_t va)
1143{
c8fe38ae 1144 cpu_invlpg((void *)va);
d7f50089
YY
1145}
1146
d7f50089 1147/*
c8fe38ae 1148 * remove a page from the kernel pagetables
d7f50089
YY
1149 */
1150void
c8fe38ae 1151pmap_kremove(vm_offset_t va)
d7f50089 1152{
c8fe38ae
MD
1153 pt_entry_t *pte;
1154 pmap_inval_info info;
1155
1156 pmap_inval_init(&info);
1157 pte = vtopte(va);
c2fb025d 1158 pmap_inval_interlock(&info, &kernel_pmap, va);
52bb73bc 1159 (void)pte_load_clear(pte);
c2fb025d
MD
1160 pmap_inval_deinterlock(&info, &kernel_pmap);
1161 pmap_inval_done(&info);
c8fe38ae
MD
1162}
1163
1164void
1165pmap_kremove_quick(vm_offset_t va)
1166{
1167 pt_entry_t *pte;
1168 pte = vtopte(va);
52bb73bc 1169 (void)pte_load_clear(pte);
c8fe38ae 1170 cpu_invlpg((void *)va);
d7f50089
YY
1171}
1172
1173/*
c8fe38ae 1174 * XXX these need to be recoded. They are not used in any critical path.
d7f50089
YY
1175 */
1176void
c8fe38ae 1177pmap_kmodify_rw(vm_offset_t va)
d7f50089 1178{
701c977e 1179 atomic_set_long(vtopte(va), PG_RW);
c8fe38ae 1180 cpu_invlpg((void *)va);
d7f50089
YY
1181}
1182
c8fe38ae
MD
1183void
1184pmap_kmodify_nc(vm_offset_t va)
1185{
701c977e 1186 atomic_set_long(vtopte(va), PG_N);
c8fe38ae
MD
1187 cpu_invlpg((void *)va);
1188}
d7f50089
YY
1189
1190/*
ad54aa11
MD
1191 * Used to map a range of physical addresses into kernel virtual
1192 * address space during the low level boot, typically to map the
1193 * dump bitmap, message buffer, and vm_page_array.
c8fe38ae 1194 *
ad54aa11
MD
1195 * These mappings are typically made at some pointer after the end of the
1196 * kernel text+data.
1197 *
1198 * We could return PHYS_TO_DMAP(start) here and not allocate any
1199 * via (*virtp), but then kmem from userland and kernel dumps won't
1200 * have access to the related pointers.
d7f50089
YY
1201 */
1202vm_offset_t
8e5e6f1b 1203pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
d7f50089 1204{
ad54aa11
MD
1205 vm_offset_t va;
1206 vm_offset_t va_start;
1207
1208 /*return PHYS_TO_DMAP(start);*/
1209
1210 va_start = *virtp;
1211 va = va_start;
1212
1213 while (start < end) {
1214 pmap_kenter_quick(va, start);
1215 va += PAGE_SIZE;
1216 start += PAGE_SIZE;
1217 }
1218 *virtp = va;
1219 return va_start;
d7f50089
YY
1220}
1221
c8fe38ae 1222
d7f50089 1223/*
c8fe38ae
MD
1224 * Add a list of wired pages to the kva
1225 * this routine is only used for temporary
1226 * kernel mappings that do not need to have
1227 * page modification or references recorded.
1228 * Note that old mappings are simply written
1229 * over. The page *must* be wired.
d7f50089
YY
1230 */
1231void
c8fe38ae 1232pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
d7f50089 1233{
c8fe38ae
MD
1234 vm_offset_t end_va;
1235
1236 end_va = va + count * PAGE_SIZE;
1237
1238 while (va < end_va) {
1239 pt_entry_t *pte;
1240
1241 pte = vtopte(va);
1242 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1243 cpu_invlpg((void *)va);
1244 va += PAGE_SIZE;
1245 m++;
1246 }
7d4d6fdb 1247 smp_invltlb();
c8fe38ae
MD
1248}
1249
d7f50089 1250/*
7155fc7d 1251 * This routine jerks page mappings from the
c8fe38ae 1252 * kernel -- it is meant only for temporary mappings.
7155fc7d
MD
1253 *
1254 * MPSAFE, INTERRUPT SAFE (cluster callback)
d7f50089 1255 */
c8fe38ae
MD
1256void
1257pmap_qremove(vm_offset_t va, int count)
d7f50089 1258{
c8fe38ae
MD
1259 vm_offset_t end_va;
1260
48ffc236 1261 end_va = va + count * PAGE_SIZE;
c8fe38ae
MD
1262
1263 while (va < end_va) {
1264 pt_entry_t *pte;
1265
1266 pte = vtopte(va);
52bb73bc 1267 (void)pte_load_clear(pte);
c8fe38ae
MD
1268 cpu_invlpg((void *)va);
1269 va += PAGE_SIZE;
1270 }
c8fe38ae 1271 smp_invltlb();
d7f50089
YY
1272}
1273
1274/*
c8fe38ae
MD
1275 * Create a new thread and optionally associate it with a (new) process.
1276 * NOTE! the new thread's cpu may not equal the current cpu.
d7f50089
YY
1277 */
1278void
c8fe38ae 1279pmap_init_thread(thread_t td)
d7f50089 1280{
d1368d1a 1281 /* enforce pcb placement & alignment */
c8fe38ae 1282 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
d1368d1a 1283 td->td_pcb = (struct pcb *)((intptr_t)td->td_pcb & ~(intptr_t)0xF);
c8fe38ae 1284 td->td_savefpu = &td->td_pcb->pcb_save;
d1368d1a 1285 td->td_sp = (char *)td->td_pcb; /* no -16 */
d7f50089
YY
1286}
1287
1288/*
c8fe38ae 1289 * This routine directly affects the fork perf for a process.
d7f50089
YY
1290 */
1291void
c8fe38ae 1292pmap_init_proc(struct proc *p)
d7f50089
YY
1293{
1294}
1295
1296/*
c8fe38ae
MD
1297 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1298 * it, and IdlePTD, represents the template used to update all other pmaps.
1299 *
1300 * On architectures where the kernel pmap is not integrated into the user
1301 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1302 * kernel_pmap should be used to directly access the kernel_pmap.
d7f50089
YY
1303 */
1304void
c8fe38ae 1305pmap_pinit0(struct pmap *pmap)
d7f50089 1306{
48ffc236 1307 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
c8fe38ae
MD
1308 pmap->pm_count = 1;
1309 pmap->pm_active = 0;
701c977e
MD
1310 pmap->pm_pvhint = NULL;
1311 RB_INIT(&pmap->pm_pvroot);
b12defdc
MD
1312 spin_init(&pmap->pm_spin);
1313 lwkt_token_init(&pmap->pm_token, "pmap_tok");
c8fe38ae 1314 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
d7f50089
YY
1315}
1316
1317/*
c8fe38ae
MD
1318 * Initialize a preallocated and zeroed pmap structure,
1319 * such as one in a vmspace structure.
d7f50089 1320 */
921c891e
MD
1321static void
1322pmap_pinit_simple(struct pmap *pmap)
d7f50089 1323{
701c977e
MD
1324 /*
1325 * Misc initialization
1326 */
1327 pmap->pm_count = 1;
1328 pmap->pm_active = 0;
1329 pmap->pm_pvhint = NULL;
921c891e
MD
1330 pmap->pm_flags = PMAP_FLAG_SIMPLE;
1331
1332 /*
1333 * Don't blow up locks/tokens on re-use (XXX fix/use drop code
1334 * for this).
1335 */
701c977e
MD
1336 if (pmap->pm_pmlpv == NULL) {
1337 RB_INIT(&pmap->pm_pvroot);
1338 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1339 spin_init(&pmap->pm_spin);
1340 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1341 }
921c891e
MD
1342}
1343
1344void
1345pmap_pinit(struct pmap *pmap)
1346{
1347 pv_entry_t pv;
1348 int j;
1349
1350 pmap_pinit_simple(pmap);
1351 pmap->pm_flags &= ~PMAP_FLAG_SIMPLE;
c8fe38ae
MD
1352
1353 /*
1354 * No need to allocate page table space yet but we do need a valid
1355 * page directory table.
1356 */
48ffc236
JG
1357 if (pmap->pm_pml4 == NULL) {
1358 pmap->pm_pml4 =
1359 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
c8fe38ae
MD
1360 }
1361
1362 /*
701c977e
MD
1363 * Allocate the page directory page, which wires it even though
1364 * it isn't being entered into some higher level page table (it
1365 * being the highest level). If one is already cached we don't
1366 * have to do anything.
c8fe38ae 1367 */
701c977e
MD
1368 if ((pv = pmap->pm_pmlpv) == NULL) {
1369 pv = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
1370 pmap->pm_pmlpv = pv;
b12defdc 1371 pmap_kenter((vm_offset_t)pmap->pm_pml4,
701c977e
MD
1372 VM_PAGE_TO_PHYS(pv->pv_m));
1373 pv_put(pv);
33fb3ba1
MD
1374
1375 /*
1376 * Install DMAP and KMAP.
1377 */
1378 for (j = 0; j < NDMPML4E; ++j) {
1379 pmap->pm_pml4[DMPML4I + j] =
1380 (DMPDPphys + ((vm_paddr_t)j << PML4SHIFT)) |
1381 PG_RW | PG_V | PG_U;
1382 }
701c977e 1383 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
701c977e 1384
33fb3ba1
MD
1385 /*
1386 * install self-referential address mapping entry
1387 */
701c977e
MD
1388 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(pv->pv_m) |
1389 PG_V | PG_RW | PG_A | PG_M;
1390 } else {
1391 KKASSERT(pv->pv_m->flags & PG_MAPPED);
1392 KKASSERT(pv->pv_m->flags & PG_WRITEABLE);
b12defdc 1393 }
993bac44
MD
1394 KKASSERT(pmap->pm_pml4[255] == 0);
1395 KKASSERT(RB_ROOT(&pmap->pm_pvroot) == pv);
1396 KKASSERT(pv->pv_entry.rbe_left == NULL);
1397 KKASSERT(pv->pv_entry.rbe_right == NULL);
d7f50089
YY
1398}
1399
1400/*
c8fe38ae
MD
1401 * Clean up a pmap structure so it can be physically freed. This routine
1402 * is called by the vmspace dtor function. A great deal of pmap data is
1403 * left passively mapped to improve vmspace management so we have a bit
1404 * of cleanup work to do here.
d7f50089
YY
1405 */
1406void
c8fe38ae 1407pmap_puninit(pmap_t pmap)
d7f50089 1408{
701c977e 1409 pv_entry_t pv;
c8fe38ae
MD
1410 vm_page_t p;
1411
1412 KKASSERT(pmap->pm_active == 0);
701c977e
MD
1413 if ((pv = pmap->pm_pmlpv) != NULL) {
1414 if (pv_hold_try(pv) == 0)
1415 pv_lock(pv);
52bb73bc 1416 p = pmap_remove_pv_page(pv);
701c977e 1417 pv_free(pv);
48ffc236 1418 pmap_kremove((vm_offset_t)pmap->pm_pml4);
b12defdc 1419 vm_page_busy_wait(p, FALSE, "pgpun");
701c977e 1420 KKASSERT(p->flags & (PG_FICTITIOUS|PG_UNMANAGED));
b12defdc 1421 vm_page_unwire(p, 0);
701c977e
MD
1422 vm_page_flag_clear(p, PG_MAPPED | PG_WRITEABLE);
1423
1424 /*
1425 * XXX eventually clean out PML4 static entries and
1426 * use vm_page_free_zero()
1427 */
1428 vm_page_free(p);
1429 pmap->pm_pmlpv = NULL;
c8fe38ae 1430 }
48ffc236 1431 if (pmap->pm_pml4) {
bfc09ba0 1432 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
48ffc236
JG
1433 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1434 pmap->pm_pml4 = NULL;
c8fe38ae 1435 }
701c977e
MD
1436 KKASSERT(pmap->pm_stats.resident_count == 0);
1437 KKASSERT(pmap->pm_stats.wired_count == 0);
d7f50089
YY
1438}
1439
1440/*
c8fe38ae
MD
1441 * Wire in kernel global address entries. To avoid a race condition
1442 * between pmap initialization and pmap_growkernel, this procedure
1443 * adds the pmap to the master list (which growkernel scans to update),
1444 * then copies the template.
d7f50089
YY
1445 */
1446void
c8fe38ae 1447pmap_pinit2(struct pmap *pmap)
d7f50089 1448{
b12defdc 1449 spin_lock(&pmap_spin);
c8fe38ae 1450 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
b12defdc 1451 spin_unlock(&pmap_spin);
d7f50089
YY
1452}
1453
1454/*
701c977e
MD
1455 * This routine is called when various levels in the page table need to
1456 * be populated. This routine cannot fail.
d7f50089 1457 *
701c977e
MD
1458 * This function returns two locked pv_entry's, one representing the
1459 * requested pv and one representing the requested pv's parent pv. If
1460 * the pv did not previously exist it will be mapped into its parent
1461 * and wired, otherwise no additional wire count will be added.
d7f50089 1462 */
bfc09ba0 1463static
701c977e
MD
1464pv_entry_t
1465pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, pv_entry_t *pvpp)
d7f50089 1466{
701c977e
MD
1467 pt_entry_t *ptep;
1468 pv_entry_t pv;
1469 pv_entry_t pvp;
1470 vm_pindex_t pt_pindex;
1471 vm_page_t m;
1472 int isnew;
921c891e 1473 int ispt;
701c977e 1474
c8fe38ae 1475 /*
701c977e
MD
1476 * If the pv already exists and we aren't being asked for the
1477 * parent page table page we can just return it. A locked+held pv
1478 * is returned.
c8fe38ae 1479 */
921c891e 1480 ispt = 0;
701c977e
MD
1481 pv = pv_alloc(pmap, ptepindex, &isnew);
1482 if (isnew == 0 && pvpp == NULL)
1483 return(pv);
1484
1485 /*
1486 * This is a new PV, we have to resolve its parent page table and
1487 * add an additional wiring to the page if necessary.
1488 */
1489
1490 /*
1491 * Special case terminal PVs. These are not page table pages so
1492 * no vm_page is allocated (the caller supplied the vm_page). If
1493 * pvpp is non-NULL we are being asked to also removed the pt_pv
1494 * for this pv.
1495 *
1496 * Note that pt_pv's are only returned for user VAs. We assert that
1497 * a pt_pv is not being requested for kernel VAs.
1498 */
1499 if (ptepindex < pmap_pt_pindex(0)) {
1500 if (ptepindex >= NUPTE_USER)
1501 KKASSERT(pvpp == NULL);
1502 else
1503 KKASSERT(pvpp != NULL);
1504 if (pvpp) {
1505 pt_pindex = NUPTE_TOTAL + (ptepindex >> NPTEPGSHIFT);
1506 pvp = pmap_allocpte(pmap, pt_pindex, NULL);
1507 if (isnew)
1508 vm_page_wire_quick(pvp->pv_m);
1509 *pvpp = pvp;
1510 } else {
1511 pvp = NULL;
1512 }
1513 return(pv);
b12defdc 1514 }
c8fe38ae
MD
1515
1516 /*
701c977e
MD
1517 * Non-terminal PVs allocate a VM page to represent the page table,
1518 * so we have to resolve pvp and calculate ptepindex for the pvp
1519 * and then for the page table entry index in the pvp for
1520 * fall-through.
c8fe38ae 1521 */
701c977e 1522 if (ptepindex < pmap_pd_pindex(0)) {
4a4ea614 1523 /*
701c977e 1524 * pv is PT, pvp is PD
4a4ea614 1525 */
701c977e
MD
1526 ptepindex = (ptepindex - pmap_pt_pindex(0)) >> NPDEPGSHIFT;
1527 ptepindex += NUPTE_TOTAL + NUPT_TOTAL;
1528 pvp = pmap_allocpte(pmap, ptepindex, NULL);
1529 if (!isnew)
1530 goto notnew;
1531
1b2e0b92 1532 /*
701c977e 1533 * PT index in PD
1b2e0b92 1534 */
701c977e
MD
1535 ptepindex = pv->pv_pindex - pmap_pt_pindex(0);
1536 ptepindex &= ((1ul << NPDEPGSHIFT) - 1);
921c891e 1537 ispt = 1;
701c977e 1538 } else if (ptepindex < pmap_pdp_pindex(0)) {
1b2e0b92 1539 /*
701c977e 1540 * pv is PD, pvp is PDP
921c891e
MD
1541 *
1542 * SIMPLE PMAP NOTE: Simple pmaps do not allocate above
1543 * the PD.
1b2e0b92 1544 */
701c977e
MD
1545 ptepindex = (ptepindex - pmap_pd_pindex(0)) >> NPDPEPGSHIFT;
1546 ptepindex += NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL;
921c891e
MD
1547
1548 if (pmap->pm_flags & PMAP_FLAG_SIMPLE) {
1549 KKASSERT(pvpp == NULL);
1550 pvp = NULL;
1551 } else {
1552 pvp = pmap_allocpte(pmap, ptepindex, NULL);
1553 }
701c977e
MD
1554 if (!isnew)
1555 goto notnew;
1556
1557 /*
1558 * PD index in PDP
1559 */
1560 ptepindex = pv->pv_pindex - pmap_pd_pindex(0);
1561 ptepindex &= ((1ul << NPDPEPGSHIFT) - 1);
1562 } else if (ptepindex < pmap_pml4_pindex()) {
700e22f7 1563 /*
701c977e 1564 * pv is PDP, pvp is the root pml4 table
1b2e0b92 1565 */
701c977e
MD
1566 pvp = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
1567 if (!isnew)
1568 goto notnew;
700e22f7 1569
701c977e
MD
1570 /*
1571 * PDP index in PML4
1572 */
1573 ptepindex = pv->pv_pindex - pmap_pdp_pindex(0);
1574 ptepindex &= ((1ul << NPML4EPGSHIFT) - 1);
1575 } else {
1576 /*
1577 * pv represents the top-level PML4, there is no parent.
1578 */
1579 pvp = NULL;
1580 if (!isnew)
1581 goto notnew;
1b2e0b92 1582 }
700e22f7
MD
1583
1584 /*
701c977e
MD
1585 * This code is only reached if isnew is TRUE and this is not a
1586 * terminal PV. We need to allocate a vm_page for the page table
1587 * at this level and enter it into the parent page table.
1588 *
1589 * page table pages are marked PG_WRITEABLE and PG_MAPPED.
1b2e0b92 1590 */
701c977e
MD
1591 for (;;) {
1592 m = vm_page_alloc(NULL, pv->pv_pindex,
1593 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM |
1594 VM_ALLOC_INTERRUPT);
1595 if (m)
1596 break;
1597 vm_wait(0);
1b2e0b92 1598 }
701c977e
MD
1599 vm_page_spin_lock(m);
1600 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1601 pv->pv_m = m;
1602 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1603 vm_page_spin_unlock(m);
1604 vm_page_unmanage(m); /* m must be spinunlocked */
1605
1606 if ((m->flags & PG_ZERO) == 0) {
1607 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1608 }
1609#ifdef PMAP_DEBUG
1610 else {
1611 pmap_page_assertzero(VM_PAGE_TO_PHYS(m));
1612 }
1613#endif
1614 m->valid = VM_PAGE_BITS_ALL;
1615 vm_page_flag_clear(m, PG_ZERO);
1616 vm_page_wire(m); /* wire for mapping in parent */
1617
1618 /*
1619 * Wire the page into pvp, bump the wire-count for pvp's page table
1620 * page. Bump the resident_count for the pmap. There is no pvp
1621 * for the top level, address the pm_pml4[] array directly.
1622 *
1623 * If the caller wants the parent we return it, otherwise
1624 * we just put it away.
1625 *
1626 * No interlock is needed for pte 0 -> non-zero.
921c891e
MD
1627 *
1628 * In the situation where *ptep is valid we might have an unmanaged
1629 * page table page shared from another page table which we need to
1630 * unshare before installing our private page table page.
701c977e
MD
1631 */
1632 if (pvp) {
701c977e 1633 ptep = pv_pte_lookup(pvp, ptepindex);
921c891e
MD
1634 if (*ptep & PG_V) {
1635 pt_entry_t pte;
1636 pmap_inval_info info;
1637
1638 kprintf("pmap_allocpte: restate shared pg table pg\n");
1639
1640 if (ispt == 0) {
1641 panic("pmap_allocpte: unexpected pte %p/%d",
1642 pvp, (int)ptepindex);
1643 }
1644 pmap_inval_init(&info);
1645 pmap_inval_interlock(&info, pmap, (vm_offset_t)-1);
1646 pte = pte_load_clear(ptep);
1647 pmap_inval_deinterlock(&info, pmap);
1648 pmap_inval_done(&info);
1649 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte & PG_FRAME)))
1650 panic("pmap_allocpte: shared pgtable pg bad wirecount");
1651 } else {
1652 vm_page_wire_quick(pvp->pv_m);
1653 }
701c977e
MD
1654 *ptep = VM_PAGE_TO_PHYS(m) | (PG_U | PG_RW | PG_V |
1655 PG_A | PG_M);
1656 }
1657 vm_page_wakeup(m);
1658notnew:
1659 if (pvpp)
1660 *pvpp = pvp;
1661 else if (pvp)
1662 pv_put(pvp);
1663 return (pv);
1664}
d7f50089
YY
1665
1666/*
921c891e
MD
1667 * This version of pmap_allocpte() checks for possible segment optimizations
1668 * that would allow page-table sharing. It can be called for terminal
1669 * page or page table page ptepindex's.
1670 *
1671 * The function is called with page table page ptepindex's for fictitious
1672 * and unmanaged terminal pages. That is, we don't want to allocate a
1673 * terminal pv, we just want the pt_pv. pvpp is usually passed as NULL
1674 * for this case.
1675 *
1676 * This function can return a pv and *pvpp associated with the passed in pmap
1677 * OR a pv and *pvpp associated with the shared pmap. In the latter case
1678 * an unmanaged page table page will be entered into the pass in pmap.
1679 */
1680static
1681pv_entry_t
1682pmap_allocpte_seg(pmap_t pmap, vm_pindex_t ptepindex, pv_entry_t *pvpp,
1683 vm_map_entry_t entry, vm_offset_t va)
1684{
1685 struct pmap_inval_info info;
1686 vm_object_t object;
1687 pmap_t obpmap;
1688 pmap_t *obpmapp;
1689 vm_offset_t b;
1690 pv_entry_t pte_pv; /* in original or shared pmap */
1691 pv_entry_t pt_pv; /* in original or shared pmap */
1692 pv_entry_t proc_pd_pv; /* in original pmap */
1693 pv_entry_t proc_pt_pv; /* in original pmap */
1694 pv_entry_t xpv; /* PT in shared pmap */
1695 pd_entry_t *pt; /* PT entry in PD of original pmap */
1696 pd_entry_t opte; /* contents of *pt */
1697 pd_entry_t npte; /* contents of *pt */
1698 vm_page_t m;
1699
1700 /*
1701 * Basic tests, require a non-NULL vm_map_entry, require proper
1702 * alignment and type for the vm_map_entry, require that the
1703 * underlying object already be allocated.
1704 *
1705 * We currently allow any type of object to use this optimization.
1706 * The object itself does NOT have to be sized to a multiple of the
1707 * segment size, but the memory mapping does.
1708 */
1709 if (entry == NULL ||
1710 pmap_mmu_optimize == 0 || /* not enabled */
1711 ptepindex >= pmap_pd_pindex(0) || /* not terminal */
1712 entry->inheritance != VM_INHERIT_SHARE || /* not shared */
1713 entry->maptype != VM_MAPTYPE_NORMAL || /* weird map type */
1714 entry->object.vm_object == NULL || /* needs VM object */
1715 (entry->offset & SEG_MASK) || /* must be aligned */
1716 (entry->start & SEG_MASK)) {
1717 return(pmap_allocpte(pmap, ptepindex, pvpp));
1718 }
1719
1720 /*
1721 * Make sure the full segment can be represented.
1722 */
1723 b = va & ~(vm_offset_t)SEG_MASK;
1724 if (b < entry->start && b + SEG_SIZE > entry->end)
1725 return(pmap_allocpte(pmap, ptepindex, pvpp));
1726
1727 /*
1728 * If the full segment can be represented dive the VM object's
1729 * shared pmap, allocating as required.
1730 */
1731 object = entry->object.vm_object;
1732
1733 if (entry->protection & VM_PROT_WRITE)
1734 obpmapp = &object->md.pmap_rw;
1735 else
1736 obpmapp = &object->md.pmap_ro;
1737
1738 /*
1739 * We allocate what appears to be a normal pmap but because portions
1740 * of this pmap are shared with other unrelated pmaps we have to
1741 * set pm_active to point to all cpus.
1742 *
1743 * XXX Currently using pmap_spin to interlock the update, can't use
1744 * vm_object_hold/drop because the token might already be held
1745 * shared OR exclusive and we don't know.
1746 */
1747 while ((obpmap = *obpmapp) == NULL) {
1748 obpmap = kmalloc(sizeof(*obpmap), M_OBJPMAP, M_WAITOK|M_ZERO);
1749 pmap_pinit_simple(obpmap);
1750 pmap_pinit2(obpmap);
1751 spin_lock(&pmap_spin);
1752 if (*obpmapp != NULL) {
1753 /*
1754 * Handle race
1755 */
1756 spin_unlock(&pmap_spin);
1757 pmap_release(obpmap);
1758 pmap_puninit(obpmap);
1759 kfree(obpmap, M_OBJPMAP);
1760 } else {
1761 obpmap->pm_active = smp_active_mask;
1762 *obpmapp = obpmap;
1763 spin_unlock(&pmap_spin);
1764 }
1765 }
1766
1767 /*
1768 * Layering is: PTE, PT, PD, PDP, PML4. We have to return the
1769 * pte/pt using the shared pmap from the object but also adjust
1770 * the process pmap's page table page as a side effect.
1771 */
1772
1773 /*
1774 * Resolve the terminal PTE and PT in the shared pmap. This is what
1775 * we will return. This is true if ptepindex represents a terminal
1776 * page, otherwise pte_pv is actually the PT and pt_pv is actually
1777 * the PD.
1778 */
1779 pt_pv = NULL;
1780 pte_pv = pmap_allocpte(obpmap, ptepindex, &pt_pv);
1781 if (ptepindex >= pmap_pt_pindex(0))
1782 xpv = pte_pv;
1783 else
1784 xpv = pt_pv;
1785
1786 /*
1787 * Resolve the PD in the process pmap so we can properly share the
1788 * page table page. Lock order is bottom-up (leaf first)!
1789 *
1790 * NOTE: proc_pt_pv can be NULL.
1791 */
1792 proc_pt_pv = pv_get(pmap, pmap_pt_pindex(b));
1793 proc_pd_pv = pmap_allocpte(pmap, pmap_pd_pindex(b), NULL);
1794
1795 /*
1796 * xpv is the page table page pv from the shared object
1797 * (for convenience).
1798 *
1799 * Calculate the pte value for the PT to load into the process PD.
1800 * If we have to change it we must properly dispose of the previous
1801 * entry.
1802 */
1803 pt = pv_pte_lookup(proc_pd_pv, pmap_pt_index(b));
1804 npte = VM_PAGE_TO_PHYS(xpv->pv_m) |
1805 (PG_U | PG_RW | PG_V | PG_A | PG_M);
99c2cc55
MD
1806
1807 /*
1808 * Dispose of previous entry if it was local to the process pmap.
1809 * (This should zero-out *pt)
1810 */
1811 if (proc_pt_pv) {
01d2a79f 1812 pmap_release_pv(proc_pt_pv, proc_pd_pv);
99c2cc55
MD
1813 proc_pt_pv = NULL;
1814 /* relookup */
1815 pt = pv_pte_lookup(proc_pd_pv, pmap_pt_index(b));
1816 }
1817
1818 /*
1819 * Handle remaining cases.
1820 */
921c891e
MD
1821 if (*pt == 0) {
1822 *pt = npte;
1823 vm_page_wire_quick(xpv->pv_m);
1824 vm_page_wire_quick(proc_pd_pv->pv_m);
1825 atomic_add_long(&pmap->pm_stats.resident_count, 1);
1826 } else if (*pt != npte) {
1827 pmap_inval_init(&info);
1828 pmap_inval_interlock(&info, pmap, (vm_offset_t)-1);
921c891e 1829
99c2cc55
MD
1830 opte = pte_load_clear(pt);
1831 KKASSERT(opte && opte != npte);
1832
1833 *pt = npte;
1834 vm_page_wire_quick(xpv->pv_m); /* pgtable pg that is npte */
1835
1836 /*
1837 * Clean up opte, bump the wire_count for the process
1838 * PD page representing the new entry if it was
1839 * previously empty.
1840 *
1841 * If the entry was not previously empty and we have
1842 * a PT in the proc pmap then opte must match that
1843 * pt. The proc pt must be retired (this is done
1844 * later on in this procedure).
1845 *
1846 * NOTE: replacing valid pte, wire_count on proc_pd_pv
1847 * stays the same.
1848 */
1849 KKASSERT(opte & PG_V);
1850 m = PHYS_TO_VM_PAGE(opte & PG_FRAME);
1851 if (vm_page_unwire_quick(m)) {
1852 panic("pmap_allocpte_seg: "
1853 "bad wire count %p",
1854 m);
921c891e 1855 }
99c2cc55 1856
921c891e
MD
1857 pmap_inval_deinterlock(&info, pmap);
1858 pmap_inval_done(&info);
921c891e
MD
1859 }
1860
1861 /*
1862 * The existing process page table was replaced and must be destroyed
1863 * here.
1864 */
1865 if (proc_pd_pv)
1866 pv_put(proc_pd_pv);
921c891e
MD
1867 if (pvpp)
1868 *pvpp = pt_pv;
1869 else
1870 pv_put(pt_pv);
1871
1872 return (pte_pv);
1873}
1874
1875/*
701c977e
MD
1876 * Release any resources held by the given physical map.
1877 *
1878 * Called when a pmap initialized by pmap_pinit is being released. Should
1879 * only be called if the map contains no valid mappings.
b12defdc 1880 *
701c977e 1881 * Caller must hold pmap->pm_token
d7f50089 1882 */
701c977e
MD
1883struct pmap_release_info {
1884 pmap_t pmap;
1885 int retry;
1886};
1887
1888static int pmap_release_callback(pv_entry_t pv, void *data);
1889
1890void
1891pmap_release(struct pmap *pmap)
c8fe38ae 1892{
701c977e
MD
1893 struct pmap_release_info info;
1894
1895 KASSERT(pmap->pm_active == 0,
1896 ("pmap still active! %016jx", (uintmax_t)pmap->pm_active));
701c977e
MD
1897
1898 spin_lock(&pmap_spin);
1899 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1900 spin_unlock(&pmap_spin);
c8fe38ae
MD
1901
1902 /*
701c977e
MD
1903 * Pull pv's off the RB tree in order from low to high and release
1904 * each page.
c8fe38ae 1905 */
701c977e
MD
1906 info.pmap = pmap;
1907 do {
1908 info.retry = 0;
1909 spin_lock(&pmap->pm_spin);
1910 RB_SCAN(pv_entry_rb_tree, &pmap->pm_pvroot, NULL,
1911 pmap_release_callback, &info);
1912 spin_unlock(&pmap->pm_spin);
1913 } while (info.retry);
1914
a5fc46c9
MD
1915
1916 /*
701c977e
MD
1917 * One resident page (the pml4 page) should remain.
1918 * No wired pages should remain.
a5fc46c9 1919 */
921c891e
MD
1920 KKASSERT(pmap->pm_stats.resident_count ==
1921 ((pmap->pm_flags & PMAP_FLAG_SIMPLE) ? 0 : 1));
1922
701c977e
MD
1923 KKASSERT(pmap->pm_stats.wired_count == 0);
1924}
1925
1926static int
1927pmap_release_callback(pv_entry_t pv, void *data)
1928{
1929 struct pmap_release_info *info = data;
1930 pmap_t pmap = info->pmap;
921c891e 1931 int r;
701c977e
MD
1932
1933 if (pv_hold_try(pv)) {
1934 spin_unlock(&pmap->pm_spin);
1935 } else {
1936 spin_unlock(&pmap->pm_spin);
1937 pv_lock(pv);
1938 if (pv->pv_pmap != pmap) {
1939 pv_put(pv);
1940 spin_lock(&pmap->pm_spin);
1941 info->retry = 1;
1942 return(-1);
a5fc46c9 1943 }
48ffc236 1944 }
01d2a79f 1945 r = pmap_release_pv(pv, NULL);
921c891e
MD
1946 spin_lock(&pmap->pm_spin);
1947 return(r);
1948}
1949
1950/*
1951 * Called with held (i.e. also locked) pv. This function will dispose of
1952 * the lock along with the pv.
01d2a79f
MD
1953 *
1954 * If the caller already holds the locked parent page table for pv it
1955 * must pass it as pvp, allowing us to avoid a deadlock, else it can
1956 * pass NULL for pvp.
921c891e
MD
1957 */
1958static int
01d2a79f 1959pmap_release_pv(pv_entry_t pv, pv_entry_t pvp)
921c891e
MD
1960{
1961 vm_page_t p;
48ffc236 1962
701c977e
MD
1963 /*
1964 * The pmap is currently not spinlocked, pv is held+locked.
1965 * Remove the pv's page from its parent's page table. The
1966 * parent's page table page's wire_count will be decremented.
1967 */
01d2a79f 1968 pmap_remove_pv_pte(pv, pvp, NULL);
c8fe38ae
MD
1969
1970 /*
701c977e
MD
1971 * Terminal pvs are unhooked from their vm_pages. Because
1972 * terminal pages aren't page table pages they aren't wired
1973 * by us, so we have to be sure not to unwire them either.
c8fe38ae 1974 */
701c977e 1975 if (pv->pv_pindex < pmap_pt_pindex(0)) {
52bb73bc 1976 pmap_remove_pv_page(pv);
701c977e
MD
1977 goto skip;
1978 }
c8fe38ae 1979
c8fe38ae 1980 /*
701c977e
MD
1981 * We leave the top-level page table page cached, wired, and
1982 * mapped in the pmap until the dtor function (pmap_puninit())
1983 * gets called.
e8510e54 1984 *
701c977e
MD
1985 * Since we are leaving the top-level pv intact we need
1986 * to break out of what would otherwise be an infinite loop.
c8fe38ae 1987 */
701c977e
MD
1988 if (pv->pv_pindex == pmap_pml4_pindex()) {
1989 pv_put(pv);
701c977e
MD
1990 return(-1);
1991 }
1992
1993 /*
1994 * For page table pages (other than the top-level page),
1995 * remove and free the vm_page. The representitive mapping
1996 * removed above by pmap_remove_pv_pte() did not undo the
1997 * last wire_count so we have to do that as well.
1998 */
52bb73bc 1999 p = pmap_remove_pv_page(pv);
701c977e 2000 vm_page_busy_wait(p, FALSE, "pmaprl");
701c977e
MD
2001 if (p->wire_count != 1) {
2002 kprintf("p->wire_count was %016lx %d\n",
2003 pv->pv_pindex, p->wire_count);
2004 }
2005 KKASSERT(p->wire_count == 1);
2006 KKASSERT(p->flags & PG_UNMANAGED);
2007
2008 vm_page_unwire(p, 0);
2009 KKASSERT(p->wire_count == 0);
921c891e
MD
2010
2011 /*
2012 * Theoretically this page, if not the pml4 page, should contain
2013 * all-zeros. But its just too dangerous to mark it PG_ZERO. Free
2014 * normally.
2015 */
701c977e
MD
2016 vm_page_free(p);
2017skip:
2018 pv_free(pv);
921c891e 2019 return 0;
701c977e
MD
2020}
2021
2022/*
2023 * This function will remove the pte associated with a pv from its parent.
2024 * Terminal pv's are supported. The removal will be interlocked if info
2025 * is non-NULL. The caller must dispose of pv instead of just unlocking
2026 * it.
2027 *
2028 * The wire count will be dropped on the parent page table. The wire
2029 * count on the page being removed (pv->pv_m) from the parent page table
2030 * is NOT touched. Note that terminal pages will not have any additional
2031 * wire counts while page table pages will have at least one representing
2032 * the mapping, plus others representing sub-mappings.
2033 *
2034 * NOTE: Cannot be called on kernel page table pages, only KVM terminal
2035 * pages and user page table and terminal pages.
2036 *
2037 * The pv must be locked.
2038 *
2039 * XXX must lock parent pv's if they exist to remove pte XXX
2040 */
2041static
2042void
2043pmap_remove_pv_pte(pv_entry_t pv, pv_entry_t pvp, struct pmap_inval_info *info)
2044{
2045 vm_pindex_t ptepindex = pv->pv_pindex;
2046 pmap_t pmap = pv->pv_pmap;
2047 vm_page_t p;
2048 int gotpvp = 0;
48ffc236 2049
701c977e 2050 KKASSERT(pmap);
48ffc236 2051
701c977e 2052 if (ptepindex == pmap_pml4_pindex()) {
b12defdc 2053 /*
701c977e 2054 * We are the top level pml4 table, there is no parent.
b12defdc 2055 */
701c977e
MD
2056 p = pmap->pm_pmlpv->pv_m;
2057 } else if (ptepindex >= pmap_pdp_pindex(0)) {
e8510e54 2058 /*
701c977e
MD
2059 * Remove a PDP page from the pml4e. This can only occur
2060 * with user page tables. We do not have to lock the
2061 * pml4 PV so just ignore pvp.
e8510e54 2062 */
701c977e
MD
2063 vm_pindex_t pml4_pindex;
2064 vm_pindex_t pdp_index;
2065 pml4_entry_t *pdp;
2066
2067 pdp_index = ptepindex - pmap_pdp_pindex(0);
2068 if (pvp == NULL) {
2069 pml4_pindex = pmap_pml4_pindex();
2070 pvp = pv_get(pv->pv_pmap, pml4_pindex);
921c891e 2071 KKASSERT(pvp);
701c977e 2072 gotpvp = 1;
e8510e54 2073 }
701c977e
MD
2074 pdp = &pmap->pm_pml4[pdp_index & ((1ul << NPML4EPGSHIFT) - 1)];
2075 KKASSERT((*pdp & PG_V) != 0);
2076 p = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
2077 *pdp = 0;
2078 KKASSERT(info == NULL);
2079 } else if (ptepindex >= pmap_pd_pindex(0)) {
e8510e54 2080 /*
921c891e
MD
2081 * Remove a PD page from the pdp
2082 *
2083 * SIMPLE PMAP NOTE: Non-existant pvp's are ok in the case
2084 * of a simple pmap because it stops at
2085 * the PD page.
e8510e54 2086 */
701c977e
MD
2087 vm_pindex_t pdp_pindex;
2088 vm_pindex_t pd_index;
2089 pdp_entry_t *pd;
48ffc236 2090
701c977e 2091 pd_index = ptepindex - pmap_pd_pindex(0);
48ffc236 2092
701c977e
MD
2093 if (pvp == NULL) {
2094 pdp_pindex = NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL +
2095 (pd_index >> NPML4EPGSHIFT);
2096 pvp = pv_get(pv->pv_pmap, pdp_pindex);
921c891e
MD
2097 if (pvp)
2098 gotpvp = 1;
2099 }
2100 if (pvp) {
2101 pd = pv_pte_lookup(pvp, pd_index &
2102 ((1ul << NPDPEPGSHIFT) - 1));
2103 KKASSERT((*pd & PG_V) != 0);
2104 p = PHYS_TO_VM_PAGE(*pd & PG_FRAME);
2105 *pd = 0;
2106 } else {
2107 KKASSERT(pmap->pm_flags & PMAP_FLAG_SIMPLE);
2108 p = pv->pv_m; /* degenerate test later */
701c977e 2109 }
701c977e
MD
2110 KKASSERT(info == NULL);
2111 } else if (ptepindex >= pmap_pt_pindex(0)) {
e8510e54 2112 /*
701c977e 2113 * Remove a PT page from the pd
e8510e54 2114 */
701c977e
MD
2115 vm_pindex_t pd_pindex;
2116 vm_pindex_t pt_index;
2117 pd_entry_t *pt;
b12defdc 2118
701c977e
MD
2119 pt_index = ptepindex - pmap_pt_pindex(0);
2120
2121 if (pvp == NULL) {
2122 pd_pindex = NUPTE_TOTAL + NUPT_TOTAL +
2123 (pt_index >> NPDPEPGSHIFT);
2124 pvp = pv_get(pv->pv_pmap, pd_pindex);
921c891e 2125 KKASSERT(pvp);
701c977e
MD
2126 gotpvp = 1;
2127 }
2128 pt = pv_pte_lookup(pvp, pt_index & ((1ul << NPDPEPGSHIFT) - 1));
2129 KKASSERT((*pt & PG_V) != 0);
2130 p = PHYS_TO_VM_PAGE(*pt & PG_FRAME);
2131 *pt = 0;
2132 KKASSERT(info == NULL);
2133 } else {
b12defdc 2134 /*
701c977e 2135 * Remove a PTE from the PT page
b12defdc 2136 *
701c977e
MD
2137 * NOTE: pv's must be locked bottom-up to avoid deadlocking.
2138 * pv is a pte_pv so we can safely lock pt_pv.
b12defdc 2139 */
701c977e
MD
2140 vm_pindex_t pt_pindex;
2141 pt_entry_t *ptep;
2142 pt_entry_t pte;
2143 vm_offset_t va;
b12defdc 2144
701c977e
MD
2145 pt_pindex = ptepindex >> NPTEPGSHIFT;
2146 va = (vm_offset_t)ptepindex << PAGE_SHIFT;
2147
2148 if (ptepindex >= NUPTE_USER) {
2149 ptep = vtopte(ptepindex << PAGE_SHIFT);
2150 KKASSERT(pvp == NULL);
c8fe38ae 2151 } else {
701c977e
MD
2152 if (pvp == NULL) {
2153 pt_pindex = NUPTE_TOTAL +
2154 (ptepindex >> NPDPEPGSHIFT);
2155 pvp = pv_get(pv->pv_pmap, pt_pindex);
921c891e 2156 KKASSERT(pvp);
701c977e
MD
2157 gotpvp = 1;
2158 }
2159 ptep = pv_pte_lookup(pvp, ptepindex &
2160 ((1ul << NPDPEPGSHIFT) - 1));
c8fe38ae 2161 }
701c977e
MD
2162
2163 if (info)
2164 pmap_inval_interlock(info, pmap, va);
2165 pte = pte_load_clear(ptep);
2166 if (info)
2167 pmap_inval_deinterlock(info, pmap);
52bb73bc
MD
2168 else
2169 cpu_invlpg((void *)va);
48ffc236 2170
e8510e54 2171 /*
701c977e 2172 * Now update the vm_page_t
e8510e54 2173 */
701c977e
MD
2174 if ((pte & (PG_MANAGED|PG_V)) != (PG_MANAGED|PG_V)) {
2175 kprintf("remove_pte badpte %016lx %016lx %d\n",
2176 pte, pv->pv_pindex,
2177 pv->pv_pindex < pmap_pt_pindex(0));
2178 }
2179 /*KKASSERT((pte & (PG_MANAGED|PG_V)) == (PG_MANAGED|PG_V));*/
2180 p = PHYS_TO_VM_PAGE(pte & PG_FRAME);
2181
2182 if (pte & PG_M) {
2183 if (pmap_track_modified(ptepindex))
2184 vm_page_dirty(p);
2185 }
2186 if (pte & PG_A) {
2187 vm_page_flag_set(p, PG_REFERENCED);
e8510e54 2188 }
701c977e
MD
2189 if (pte & PG_W)
2190 atomic_add_long(&pmap->pm_stats.wired_count, -1);
2191 if (pte & PG_G)
2192 cpu_invlpg((void *)va);
c8fe38ae
MD
2193 }
2194
48ffc236 2195 /*
701c977e
MD
2196 * Unwire the parent page table page. The wire_count cannot go below
2197 * 1 here because the parent page table page is itself still mapped.
2198 *
2199 * XXX remove the assertions later.
48ffc236 2200 */
701c977e
MD
2201 KKASSERT(pv->pv_m == p);
2202 if (pvp && vm_page_unwire_quick(pvp->pv_m))
2203 panic("pmap_remove_pv_pte: Insufficient wire_count");
c8fe38ae 2204
701c977e
MD
2205 if (gotpvp)
2206 pv_put(pvp);
c8fe38ae
MD
2207}
2208
bfc09ba0
MD
2209static
2210vm_page_t
52bb73bc 2211pmap_remove_pv_page(pv_entry_t pv)
d7f50089 2212{
c8fe38ae
MD
2213 vm_page_t m;
2214
701c977e 2215 m = pv->pv_m;
701c977e
MD
2216 KKASSERT(m);
2217 vm_page_spin_lock(m);
2218 pv->pv_m = NULL;
2219 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
c8fe38ae 2220 /*
701c977e
MD
2221 if (m->object)
2222 atomic_add_int(&m->object->agg_pv_list_count, -1);
2223 */
2224 if (TAILQ_EMPTY(&m->md.pv_list))
2225 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2226 vm_page_spin_unlock(m);
52bb73bc 2227 return(m);
d7f50089
YY
2228}
2229
2230/*
c8fe38ae 2231 * Grow the number of kernel page table entries, if needed.
a8cf2878
MD
2232 *
2233 * This routine is always called to validate any address space
2234 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
2235 * space below KERNBASE.
d7f50089 2236 */
c8fe38ae 2237void
a8cf2878 2238pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
d7f50089 2239{
48ffc236 2240 vm_paddr_t paddr;
c8fe38ae
MD
2241 vm_offset_t ptppaddr;
2242 vm_page_t nkpg;
701c977e
MD
2243 pd_entry_t *pt, newpt;
2244 pdp_entry_t newpd;
a8cf2878 2245 int update_kernel_vm_end;
c8fe38ae 2246
a8cf2878
MD
2247 /*
2248 * bootstrap kernel_vm_end on first real VM use
2249 */
c8fe38ae 2250 if (kernel_vm_end == 0) {
791c6551 2251 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
c8fe38ae 2252 nkpt = 0;
701c977e 2253 while ((*pmap_pt(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
a8cf2878
MD
2254 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
2255 ~(PAGE_SIZE * NPTEPG - 1);
c8fe38ae 2256 nkpt++;
48ffc236
JG
2257 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
2258 kernel_vm_end = kernel_map.max_offset;
2259 break;
2260 }
c8fe38ae
MD
2261 }
2262 }
a8cf2878
MD
2263
2264 /*
2265 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
2266 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
2267 * do not want to force-fill 128G worth of page tables.
2268 */
2269 if (kstart < KERNBASE) {
2270 if (kstart > kernel_vm_end)
2271 kstart = kernel_vm_end;
2272 KKASSERT(kend <= KERNBASE);
2273 update_kernel_vm_end = 1;
2274 } else {
2275 update_kernel_vm_end = 0;
2276 }
2277
2278 kstart = rounddown2(kstart, PAGE_SIZE * NPTEPG);
2279 kend = roundup2(kend, PAGE_SIZE * NPTEPG);
2280
2281 if (kend - 1 >= kernel_map.max_offset)
2282 kend = kernel_map.max_offset;
2283
2284 while (kstart < kend) {
701c977e
MD
2285 pt = pmap_pt(&kernel_pmap, kstart);
2286 if (pt == NULL) {
48ffc236 2287 /* We need a new PDP entry */
701c977e 2288 nkpg = vm_page_alloc(NULL, nkpt,
a8cf2878
MD
2289 VM_ALLOC_NORMAL |
2290 VM_ALLOC_SYSTEM |
2291 VM_ALLOC_INTERRUPT);
2292 if (nkpg == NULL) {
2293 panic("pmap_growkernel: no memory to grow "
2294 "kernel");
2295 }
48ffc236 2296 paddr = VM_PAGE_TO_PHYS(nkpg);
7f2a2740
MD
2297 if ((nkpg->flags & PG_ZERO) == 0)
2298 pmap_zero_page(paddr);
2299 vm_page_flag_clear(nkpg, PG_ZERO);
701c977e 2300 newpd = (pdp_entry_t)
48ffc236 2301 (paddr | PG_V | PG_RW | PG_A | PG_M);
701c977e 2302 *pmap_pd(&kernel_pmap, kstart) = newpd;
7f2a2740 2303 nkpt++;
48ffc236
JG
2304 continue; /* try again */
2305 }
701c977e 2306 if ((*pt & PG_V) != 0) {
a8cf2878
MD
2307 kstart = (kstart + PAGE_SIZE * NPTEPG) &
2308 ~(PAGE_SIZE * NPTEPG - 1);
2309 if (kstart - 1 >= kernel_map.max_offset) {
2310 kstart = kernel_map.max_offset;
48ffc236
JG
2311 break;
2312 }
c8fe38ae
MD
2313 continue;
2314 }
2315
2316 /*
2317 * This index is bogus, but out of the way
2318 */
701c977e 2319 nkpg = vm_page_alloc(NULL, nkpt,
a8cf2878
MD
2320 VM_ALLOC_NORMAL |
2321 VM_ALLOC_SYSTEM |
2322 VM_ALLOC_INTERRUPT);
c8fe38ae
MD
2323 if (nkpg == NULL)
2324 panic("pmap_growkernel: no memory to grow kernel");
2325
2326 vm_page_wire(nkpg);
2327 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2328 pmap_zero_page(ptppaddr);
7f2a2740 2329 vm_page_flag_clear(nkpg, PG_ZERO);
701c977e
MD
2330 newpt = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2331 *pmap_pt(&kernel_pmap, kstart) = newpt;
c8fe38ae
MD
2332 nkpt++;
2333
a8cf2878
MD
2334 kstart = (kstart + PAGE_SIZE * NPTEPG) &
2335 ~(PAGE_SIZE * NPTEPG - 1);
2336
2337 if (kstart - 1 >= kernel_map.max_offset) {
2338 kstart = kernel_map.max_offset;
48ffc236 2339 break;
c8fe38ae 2340 }
c8fe38ae 2341 }
a8cf2878
MD
2342
2343 /*
2344 * Only update kernel_vm_end for areas below KERNBASE.
2345 */
2346 if (update_kernel_vm_end && kernel_vm_end < kstart)
2347 kernel_vm_end = kstart;
d7f50089
YY
2348}
2349
2350/*
921c891e 2351 * Add a reference to the specified pmap.
d7f50089 2352 */
c8fe38ae 2353void
921c891e 2354pmap_reference(pmap_t pmap)
d7f50089 2355{
921c891e
MD
2356 if (pmap != NULL) {
2357 lwkt_gettoken(&pmap->pm_token);
2358 ++pmap->pm_count;
2359 lwkt_reltoken(&pmap->pm_token);
c8fe38ae 2360 }
d7f50089
YY
2361}
2362
c8fe38ae 2363void
921c891e 2364pmap_drop(pmap_t pmap)
d7f50089 2365{
c8fe38ae 2366 if (pmap != NULL) {
b12defdc 2367 lwkt_gettoken(&pmap->pm_token);
921c891e 2368 --pmap->pm_count;
b12defdc 2369 lwkt_reltoken(&pmap->pm_token);
c8fe38ae 2370 }
d7f50089
YY
2371}
2372
c8fe38ae 2373/***************************************************
701c977e 2374 * page management routines.
c8fe38ae 2375 ***************************************************/
d7f50089
YY
2376
2377/*
701c977e 2378 * Hold a pv without locking it
d7f50089 2379 */
701c977e
MD
2380static void
2381pv_hold(pv_entry_t pv)
d7f50089 2382{
701c977e
MD
2383 u_int count;
2384
2385 if (atomic_cmpset_int(&pv->pv_hold, 0, 1))
2386 return;
2387
2388 for (;;) {
2389 count = pv->pv_hold;
2390 cpu_ccfence();
2391 if (atomic_cmpset_int(&pv->pv_hold, count, count + 1))
2392 return;
2393 /* retry */
2394 }
d7f50089
YY
2395}
2396
2397/*
701c977e
MD
2398 * Hold a pv_entry, preventing its destruction. TRUE is returned if the pv
2399 * was successfully locked, FALSE if it wasn't. The caller must dispose of
2400 * the pv properly.
2401 *
2402 * Either the pmap->pm_spin or the related vm_page_spin (if traversing a
2403 * pv list via its page) must be held by the caller.
d7f50089 2404 */
701c977e
MD
2405static int
2406_pv_hold_try(pv_entry_t pv PMAP_DEBUG_DECL)
d7f50089 2407{
701c977e
MD
2408 u_int count;
2409
2410 if (atomic_cmpset_int(&pv->pv_hold, 0, PV_HOLD_LOCKED | 1)) {
2411#ifdef PMAP_DEBUG
2412 pv->pv_func = func;
2413 pv->pv_line = lineno;
2414#endif
2415 return TRUE;
2416 }
2417
2418 for (;;) {
2419 count = pv->pv_hold;
2420 cpu_ccfence();
2421 if ((count & PV_HOLD_LOCKED) == 0) {
2422 if (atomic_cmpset_int(&pv->pv_hold, count,
2423 (count + 1) | PV_HOLD_LOCKED)) {
2424#ifdef PMAP_DEBUG
2425 pv->pv_func = func;
2426 pv->pv_line = lineno;
2427#endif
2428 return TRUE;
2429 }
2430 } else {
2431 if (atomic_cmpset_int(&pv->pv_hold, count, count + 1))
2432 return FALSE;
2433 }
2434 /* retry */
c8fe38ae 2435 }
d7f50089
YY
2436}
2437
2438/*
701c977e
MD
2439 * Drop a previously held pv_entry which could not be locked, allowing its
2440 * destruction.
2441 *
2442 * Must not be called with a spinlock held as we might zfree() the pv if it
2443 * is no longer associated with a pmap and this was the last hold count.
d7f50089 2444 */
701c977e
MD
2445static void
2446pv_drop(pv_entry_t pv)
d7f50089 2447{
701c977e 2448 u_int count;
c8fe38ae 2449
701c977e
MD
2450 if (atomic_cmpset_int(&pv->pv_hold, 1, 0)) {
2451 if (pv->pv_pmap == NULL)
2452 zfree(pvzone, pv);
c8fe38ae 2453 return;
c8fe38ae
MD
2454 }
2455
701c977e
MD
2456 for (;;) {
2457 count = pv->pv_hold;
2458 cpu_ccfence();
2459 KKASSERT((count & PV_HOLD_MASK) > 0);
2460 KKASSERT((count & (PV_HOLD_LOCKED | PV_HOLD_MASK)) !=
2461 (PV_HOLD_LOCKED | 1));
2462 if (atomic_cmpset_int(&pv->pv_hold, count, count - 1)) {
2463 if (count == 1 && pv->pv_pmap == NULL)
2464 zfree(pvzone, pv);
2465 return;
b12defdc 2466 }
701c977e 2467 /* retry */
c8fe38ae 2468 }
d7f50089 2469}
c8fe38ae 2470
d7f50089 2471/*
701c977e 2472 * Find or allocate the requested PV entry, returning a locked pv
d7f50089 2473 */
bfc09ba0 2474static
701c977e
MD
2475pv_entry_t
2476_pv_alloc(pmap_t pmap, vm_pindex_t pindex, int *isnew PMAP_DEBUG_DECL)
c8fe38ae
MD
2477{
2478 pv_entry_t pv;
701c977e 2479 pv_entry_t pnew = NULL;
c8fe38ae 2480
701c977e
MD
2481 spin_lock(&pmap->pm_spin);
2482 for (;;) {
2483 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex) {
2484 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot,
2485 pindex);
c8fe38ae 2486 }
701c977e
MD
2487 if (pv == NULL) {
2488 if (pnew == NULL) {
2489 spin_unlock(&pmap->pm_spin);
2490 pnew = zalloc(pvzone);
2491 spin_lock(&pmap->pm_spin);
2492 continue;
2493 }
2494 pnew->pv_pmap = pmap;
2495 pnew->pv_pindex = pindex;
2496 pnew->pv_hold = PV_HOLD_LOCKED | 1;
2497#ifdef PMAP_DEBUG
2498 pnew->pv_func = func;
2499 pnew->pv_line = lineno;
2500#endif
2501 pv_entry_rb_tree_RB_INSERT(&pmap->pm_pvroot, pnew);
2502 atomic_add_long(&pmap->pm_stats.resident_count, 1);
2503 spin_unlock(&pmap->pm_spin);
2504 *isnew = 1;
2505 return(pnew);
2506 }
2507 if (pnew) {
2508 spin_unlock(&pmap->pm_spin);
2509 zfree(pvzone, pnew);
2510 pnew = NULL;
2511 spin_lock(&pmap->pm_spin);
2512 continue;
2513 }
2514 if (_pv_hold_try(pv PMAP_DEBUG_COPY)) {
2515 spin_unlock(&pmap->pm_spin);
2516 *isnew = 0;
2517 return(pv);
2518 }
2519 spin_unlock(&pmap->pm_spin);
2520 _pv_lock(pv PMAP_DEBUG_COPY);
2521 if (pv->pv_pmap == pmap && pv->pv_pindex == pindex) {
2522 *isnew = 0;
2523 return(pv);
2524 }
2525 pv_put(pv);
2526 spin_lock(&pmap->pm_spin);
2527 }
c8fe38ae 2528
5926987a 2529
701c977e 2530}
b12defdc 2531
701c977e
MD
2532/*
2533 * Find the requested PV entry, returning a locked+held pv or NULL
2534 */
2535static
2536pv_entry_t
2537_pv_get(pmap_t pmap, vm_pindex_t pindex PMAP_DEBUG_DECL)
2538{
2539 pv_entry_t pv;
5926987a 2540
701c977e
MD
2541 spin_lock(&pmap->pm_spin);
2542 for (;;) {
2543 /*
2544 * Shortcut cache
2545 */
2546 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex) {
2547 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot,
2548 pindex);
2549 }
2550 if (pv == NULL) {
2551 spin_unlock(&pmap->pm_spin);
2552 return NULL;
2553 }
2554 if (_pv_hold_try(pv PMAP_DEBUG_COPY)) {
2555 pv_cache(pv, pindex);
2556 spin_unlock(&pmap->pm_spin);
2557 return(pv);
2558 }
2559 spin_unlock(&pmap->pm_spin);
2560 _pv_lock(pv PMAP_DEBUG_COPY);
2561 if (pv->pv_pmap == pmap && pv->pv_pindex == pindex)
2562 return(pv);
2563 pv_put(pv);
2564 spin_lock(&pmap->pm_spin);
2565 }
d7f50089
YY
2566}
2567
2568/*
701c977e
MD
2569 * Lookup, hold, and attempt to lock (pmap,pindex).
2570 *
2571 * If the entry does not exist NULL is returned and *errorp is set to 0
a5fc46c9 2572 *
701c977e
MD
2573 * If the entry exists and could be successfully locked it is returned and
2574 * errorp is set to 0.
2575 *
2576 * If the entry exists but could NOT be successfully locked it is returned
2577 * held and *errorp is set to 1.
d7f50089 2578 */
bfc09ba0 2579static
701c977e
MD
2580pv_entry_t
2581pv_get_try(pmap_t pmap, vm_pindex_t pindex, int *errorp)
d7f50089 2582{
c8fe38ae
MD
2583 pv_entry_t pv;
2584
701c977e
MD
2585 spin_lock(&pmap->pm_spin);
2586 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex)
2587 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
2588 if (pv == NULL) {
2589 spin_unlock(&pmap->pm_spin);
2590 *errorp = 0;
2591 return NULL;
2592 }
2593 if (pv_hold_try(pv)) {
2594 pv_cache(pv, pindex);
2595 spin_unlock(&pmap->pm_spin);
2596 *errorp = 0;
2597 return(pv); /* lock succeeded */
2598 }
2599 spin_unlock(&pmap->pm_spin);
2600 *errorp = 1;
2601 return (pv); /* lock failed */
d7f50089
YY
2602}
2603
2604/*
701c977e 2605 * Find the requested PV entry, returning a held pv or NULL
d7f50089 2606 */
bfc09ba0 2607static
701c977e
MD
2608pv_entry_t
2609pv_find(pmap_t pmap, vm_pindex_t pindex)
c8fe38ae 2610{
701c977e 2611 pv_entry_t pv;
c8fe38ae 2612
701c977e 2613 spin_lock(&pmap->pm_spin);
b12defdc 2614
701c977e
MD
2615 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex)
2616 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
2617 if (pv == NULL) {
2618 spin_unlock(&pmap->pm_spin);
2619 return NULL;
2620 }
2621 pv_hold(pv);
2622 pv_cache(pv, pindex);
2623 spin_unlock(&pmap->pm_spin);
2624 return(pv);
2625}
2626
2627/*
2628 * Lock a held pv, keeping the hold count
2629 */
2630static
2631void
2632_pv_lock(pv_entry_t pv PMAP_DEBUG_DECL)
2633{
2634 u_int count;
2635
2636 for (;;) {
2637 count = pv->pv_hold;
2638 cpu_ccfence();
2639 if ((count & PV_HOLD_LOCKED) == 0) {
2640 if (atomic_cmpset_int(&pv->pv_hold, count,
2641 count | PV_HOLD_LOCKED)) {
2642#ifdef PMAP_DEBUG
2643 pv->pv_func = func;
2644 pv->pv_line = lineno;
2645#endif
2646 return;
c8fe38ae 2647 }
701c977e
MD
2648 continue;
2649 }
2650 tsleep_interlock(pv, 0);
2651 if (atomic_cmpset_int(&pv->pv_hold, count,
2652 count | PV_HOLD_WAITING)) {
2653#ifdef PMAP_DEBUG
2654 kprintf("pv waiting on %s:%d\n",
2655 pv->pv_func, pv->pv_line);
c8fe38ae 2656#endif
701c977e 2657 tsleep(pv, PINTERLOCKED, "pvwait", hz);
c8fe38ae 2658 }
701c977e 2659 /* retry */
b12defdc 2660 }
701c977e 2661}
c8fe38ae 2662
701c977e
MD
2663/*
2664 * Unlock a held and locked pv, keeping the hold count.
2665 */
2666static
2667void
2668pv_unlock(pv_entry_t pv)
2669{
2670 u_int count;
2671
2672 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 1, 1))
2673 return;
2674
2675 for (;;) {
2676 count = pv->pv_hold;
2677 cpu_ccfence();
2678 KKASSERT((count & (PV_HOLD_LOCKED|PV_HOLD_MASK)) >=
2679 (PV_HOLD_LOCKED | 1));
2680 if (atomic_cmpset_int(&pv->pv_hold, count,
2681 count &
2682 ~(PV_HOLD_LOCKED | PV_HOLD_WAITING))) {
2683 if (count & PV_HOLD_WAITING)
2684 wakeup(pv);
2685 break;
2686 }
7ab91d55 2687 }
d7f50089
YY
2688}
2689
2690/*
701c977e
MD
2691 * Unlock and drop a pv. If the pv is no longer associated with a pmap
2692 * and the hold count drops to zero we will free it.
d7f50089 2693 *
701c977e
MD
2694 * Caller should not hold any spin locks. We are protected from hold races
2695 * by virtue of holds only occuring only with a pmap_spin or vm_page_spin
2696 * lock held. A pv cannot be located otherwise.
d7f50089 2697 */
bfc09ba0
MD
2698static
2699void
701c977e 2700pv_put(pv_entry_t pv)
c8fe38ae 2701{
701c977e
MD
2702 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 1, 0)) {
2703 if (pv->pv_pmap == NULL)
2704 zfree(pvzone, pv);
2705 return;
2706 }
2707 pv_unlock(pv);
2708 pv_drop(pv);
2709}
c8fe38ae 2710
701c977e
MD
2711/*
2712 * Unlock, drop, and free a pv, destroying it. The pv is removed from its
2713 * pmap. Any pte operations must have already been completed.
2714 */
2715static
2716void
2717pv_free(pv_entry_t pv)
2718{
2719 pmap_t pmap;
b12defdc 2720
701c977e
MD
2721 KKASSERT(pv->pv_m == NULL);
2722 if ((pmap = pv->pv_pmap) != NULL) {
2723 spin_lock(&pmap->pm_spin);
2724 pv_entry_rb_tree_RB_REMOVE(&pmap->pm_pvroot, pv);
2725 if (pmap->pm_pvhint == pv)
2726 pmap->pm_pvhint = NULL;
2727 atomic_add_long(&pmap->pm_stats.resident_count, -1);
2728 pv->pv_pmap = NULL;
2729 pv->pv_pindex = 0;
2730 spin_unlock(&pmap->pm_spin);
2731 }
2732 pv_put(pv);
2733}
2734
2735/*
2736 * This routine is very drastic, but can save the system
2737 * in a pinch.
2738 */
2739void
2740pmap_collect(void)
2741{
2742 int i;
2743 vm_page_t m;
2744 static int warningdone=0;
2745
2746 if (pmap_pagedaemon_waken == 0)
48ffc236 2747 return;
701c977e
MD
2748 pmap_pagedaemon_waken = 0;
2749 if (warningdone < 5) {
2750 kprintf("pmap_collect: collecting pv entries -- "
2751 "suggest increasing PMAP_SHPGPERPROC\n");
2752 warningdone++;
2753 }
2754
2755 for (i = 0; i < vm_page_array_size; i++) {
2756 m = &vm_page_array[i];
2757 if (m->wire_count || m->hold_count)
2758 continue;
2759 if (vm_page_busy_try(m, TRUE) == 0) {
2760 if (m->wire_count == 0 && m->hold_count == 0) {
2761 pmap_remove_all(m);
2762 }
2763 vm_page_wakeup(m);
2764 }
2765 }
d7f50089
YY
2766}
2767
2768/*
701c977e 2769 * Scan the pmap for active page table entries and issue a callback.
921c891e
MD
2770 * The callback must dispose of pte_pv, whos PTE entry is at *ptep in
2771 * its parent page table.
d7f50089 2772 *
fb4ca018 2773 * pte_pv will be NULL if the page or page table is unmanaged.
921c891e 2774 * pt_pv will point to the page table page containing the pte for the page.
701c977e 2775 *
921c891e
MD
2776 * NOTE! If we come across an unmanaged page TABLE (verses an unmanaged page),
2777 * we pass a NULL pte_pv and we pass a pt_pv pointing to the passed
2778 * process pmap's PD and page to the callback function. This can be
2779 * confusing because the pt_pv is really a pd_pv, and the target page
2780 * table page is simply aliased by the pmap and not owned by it.
d7f50089 2781 *
701c977e 2782 * It is assumed that the start and end are properly rounded to the page size.
fb4ca018
MD
2783 *
2784 * It is assumed that PD pages and above are managed and thus in the RB tree,
2785 * allowing us to use RB_SCAN from the PD pages down for ranged scans.
2786 */
2787struct pmap_scan_info {
2788 struct pmap *pmap;
2789 vm_offset_t sva;
2790 vm_offset_t eva;
2791 vm_pindex_t sva_pd_pindex;
2792 vm_pindex_t eva_pd_pindex;
9df83100 2793 void (*func)(pmap_t, struct pmap_scan_info *,
fb4ca018
MD
2794 pv_entry_t, pv_entry_t, int, vm_offset_t,
2795 pt_entry_t *, void *);
2796 void *arg;
9df83100 2797 int doinval;
fb4ca018
MD
2798 struct pmap_inval_info inval;
2799};
2800
2801static int pmap_scan_cmp(pv_entry_t pv, void *data);
2802static int pmap_scan_callback(pv_entry_t pv, void *data);
2803
701c977e 2804static void
fb4ca018 2805pmap_scan(struct pmap_scan_info *info)
701c977e 2806{
fb4ca018 2807 struct pmap *pmap = info->pmap;
701c977e
MD
2808 pv_entry_t pd_pv; /* A page directory PV */
2809 pv_entry_t pt_pv; /* A page table PV */
2810 pv_entry_t pte_pv; /* A page table entry PV */
2811 pt_entry_t *ptep;
fb4ca018 2812 struct pv_entry dummy_pv;
c8fe38ae
MD
2813
2814 if (pmap == NULL)
2815 return;
2816
701c977e
MD
2817 /*
2818 * Hold the token for stability; if the pmap is empty we have nothing
2819 * to do.
2820 */
b12defdc 2821 lwkt_gettoken(&pmap->pm_token);
701c977e 2822#if 0
10d6182e 2823 if (pmap->pm_stats.resident_count == 0) {
b12defdc 2824 lwkt_reltoken(&pmap->pm_token);
c8fe38ae 2825 return;
10d6182e 2826 }
701c977e 2827#endif
c8fe38ae 2828
fb4ca018 2829 pmap_inval_init(&info->inval);
c8fe38ae
MD
2830
2831 /*
fb4ca018 2832 * Special handling for scanning one page, which is a very common
701c977e 2833 * operation (it is?).
fb4ca018 2834 *
701c977e 2835 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4
c8fe38ae 2836 */
fb4ca018
MD
2837 if (info->sva + PAGE_SIZE == info->eva) {
2838 if (info->sva >= VM_MAX_USER_ADDRESS) {
701c977e
MD
2839 /*
2840 * Kernel mappings do not track wire counts on
fb4ca018
MD
2841 * page table pages and only maintain pd_pv and
2842 * pte_pv levels so pmap_scan() works.
701c977e
MD
2843 */
2844 pt_pv = NULL;
fb4ca018
MD
2845 pte_pv = pv_get(pmap, pmap_pte_pindex(info->sva));
2846 ptep = vtopte(info->sva);
701c977e
MD
2847 } else {
2848 /*
921c891e
MD
2849 * User pages which are unmanaged will not have a
2850 * pte_pv. User page table pages which are unmanaged
2851 * (shared from elsewhere) will also not have a pt_pv.
2852 * The func() callback will pass both pte_pv and pt_pv
2853 * as NULL in that case.
701c977e 2854 */
fb4ca018
MD
2855 pte_pv = pv_get(pmap, pmap_pte_pindex(info->sva));
2856 pt_pv = pv_get(pmap, pmap_pt_pindex(info->sva));
701c977e
MD
2857 if (pt_pv == NULL) {
2858 KKASSERT(pte_pv == NULL);
fb4ca018 2859 pd_pv = pv_get(pmap, pmap_pd_pindex(info->sva));
921c891e
MD
2860 if (pd_pv) {
2861 ptep = pv_pte_lookup(pd_pv,
fb4ca018 2862 pmap_pt_index(info->sva));
921c891e 2863 if (*ptep) {
9df83100 2864 info->func(pmap, info,
921c891e 2865 NULL, pd_pv, 1,
fb4ca018
MD
2866 info->sva, ptep,
2867 info->arg);
921c891e
MD
2868 }
2869 pv_put(pd_pv);
2870 }
701c977e
MD
2871 goto fast_skip;
2872 }
fb4ca018 2873 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(info->sva));
701c977e
MD
2874 }
2875 if (*ptep == 0) {
f2c5d4ab
MD
2876 /*
2877 * Unlike the pv_find() case below we actually
2878 * acquired a locked pv in this case so any
2879 * race should have been resolved. It is expected
2880 * to not exist.
2881 */
701c977e
MD
2882 KKASSERT(pte_pv == NULL);
2883 } else if (pte_pv) {
23b4bd44
MD
2884 KASSERT((*ptep & (PG_MANAGED|PG_V)) == (PG_MANAGED|
2885 PG_V),
2886 ("bad *ptep %016lx sva %016lx pte_pv %p",
fb4ca018 2887 *ptep, info->sva, pte_pv));
9df83100 2888 info->func(pmap, info, pte_pv, pt_pv, 0,
fb4ca018 2889 info->sva, ptep, info->arg);
701c977e 2890 } else {
23b4bd44
MD
2891 KASSERT((*ptep & (PG_MANAGED|PG_V)) == PG_V,
2892 ("bad *ptep %016lx sva %016lx pte_pv NULL",
fb4ca018 2893 *ptep, info->sva));
9df83100 2894 info->func(pmap, info, NULL, pt_pv, 0,
fb4ca018 2895 info->sva, ptep, info->arg);
48ffc236 2896 }
701c977e
MD
2897 if (pt_pv)
2898 pv_put(pt_pv);
2899fast_skip:
fb4ca018 2900 pmap_inval_done(&info->inval);
701c977e
MD
2901 lwkt_reltoken(&pmap->pm_token);
2902 return;
c8fe38ae
MD
2903 }
2904
701c977e 2905 /*
fb4ca018
MD
2906 * Nominal scan case, RB_SCAN() for PD pages and iterate from
2907 * there.
2908 */
2909 info->sva_pd_pindex = pmap_pd_pindex(info->sva);
2910 info->eva_pd_pindex = pmap_pd_pindex(info->eva + NBPDP - 1);
2911
2912 if (info->sva >= VM_MAX_USER_ADDRESS) {
2913 /*
2914 * The kernel does not currently maintain any pv_entry's for
2915 * higher-level page tables.
2916 */
2917 bzero(&dummy_pv, sizeof(dummy_pv));
2918 dummy_pv.pv_pindex = info->sva_pd_pindex;
2919 spin_lock(&pmap->pm_spin);
2920 while (dummy_pv.pv_pindex < info->eva_pd_pindex) {
2921 pmap_scan_callback(&dummy_pv, info);
2922 ++dummy_pv.pv_pindex;
2923 }
2924 spin_unlock(&pmap->pm_spin);
2925 } else {
2926 /*
2927 * User page tables maintain local PML4, PDP, and PD
2928 * pv_entry's at the very least. PT pv's might be
2929 * unmanaged and thus not exist. PTE pv's might be
2930 * unmanaged and thus not exist.
2931 */
2932 spin_lock(&pmap->pm_spin);
2933 pv_entry_rb_tree_RB_SCAN(&pmap->pm_pvroot,
2934 pmap_scan_cmp, pmap_scan_callback, info);
2935 spin_unlock(&pmap->pm_spin);
2936 }
2937 pmap_inval_done(&info->inval);
2938 lwkt_reltoken(&pmap->pm_token);
2939}
2940
2941/*
2942 * WARNING! pmap->pm_spin held
2943 */
2944static int
2945pmap_scan_cmp(pv_entry_t pv, void *data)
2946{
2947 struct pmap_scan_info *info = data;
2948 if (pv->pv_pindex < info->sva_pd_pindex)
2949 return(-1);
2950 if (pv->pv_pindex >= info->eva_pd_pindex)
2951 return(1);
2952 return(0);
2953}
2954
2955/*
2956 * WARNING! pmap->pm_spin held
2957 */
2958static int
2959pmap_scan_callback(pv_entry_t pv, void *data)
2960{
2961 struct pmap_scan_info *info = data;
2962 struct pmap *pmap = info->pmap;
2963 pv_entry_t pd_pv; /* A page directory PV */
2964 pv_entry_t pt_pv; /* A page table PV */
2965 pv_entry_t pte_pv; /* A page table entry PV */
2966 pt_entry_t *ptep;
2967 vm_offset_t sva;
2968 vm_offset_t eva;
2969 vm_offset_t va_next;
2970 vm_pindex_t pd_pindex;
2971 int error;
2972
2973 /*
2974 * Pull the PD pindex from the pv before releasing the spinlock.
2975 *
2976 * WARNING: pv is faked for kernel pmap scans.
2977 */
2978 pd_pindex = pv->pv_pindex;
2979 spin_unlock(&pmap->pm_spin);
2980 pv = NULL; /* invalid after spinlock unlocked */
2981
2982 /*
2983 * Calculate the page range within the PD. SIMPLE pmaps are
2984 * direct-mapped for the entire 2^64 address space. Normal pmaps
2985 * reflect the user and kernel address space which requires
2986 * cannonicalization w/regards to converting pd_pindex's back
2987 * into addresses.
2988 */
2989 sva = (pd_pindex - NUPTE_TOTAL - NUPT_TOTAL) << PDPSHIFT;
2990 if ((pmap->pm_flags & PMAP_FLAG_SIMPLE) == 0 &&
2991 (sva & PML4_SIGNMASK)) {
2992 sva |= PML4_SIGNMASK;
2993 }
2994 eva = sva + NBPDP; /* can overflow */
2995 if (sva < info->sva)
2996 sva = info->sva;
2997 if (eva < info->sva || eva > info->eva)
2998 eva = info->eva;
2999
3000 /*
701c977e
MD
3001 * NOTE: kernel mappings do not track page table pages, only
3002 * terminal pages.
3003 *
3004 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4.
3005 * However, for the scan to be efficient we try to
3006 * cache items top-down.
3007 */
701c977e
MD
3008 pd_pv = NULL;
3009 pt_pv = NULL;
3010
48ffc236 3011 for (; sva < eva; sva = va_next) {
701c977e
MD
3012 if (sva >= VM_MAX_USER_ADDRESS) {
3013 if (pt_pv) {
3014 pv_put(pt_pv);
3015 pt_pv = NULL;
3016 }
3017 goto kernel_skip;
3018 }
3019
3020 /*
fb4ca018
MD
3021 * PD cache (degenerate case if we skip). It is possible
3022 * for the PD to not exist due to races. This is ok.
701c977e
MD
3023 */
3024 if (pd_pv == NULL) {
701c977e
MD
3025 pd_pv = pv_get(pmap, pmap_pd_pindex(sva));
3026 } else if (pd_pv->pv_pindex != pmap_pd_pindex(sva)) {
3027 pv_put(pd_pv);
701c977e
MD
3028 pd_pv = pv_get(pmap, pmap_pd_pindex(sva));
3029 }
3030 if (pd_pv == NULL) {
48ffc236
JG
3031 va_next = (sva + NBPDP) & ~PDPMASK;
3032 if (va_next < sva)
3033 va_next = eva;
3034 continue;
3035 }
c8fe38ae
MD
3036
3037 /*
701c977e 3038 * PT cache
c8fe38ae 3039 */
701c977e 3040 if (pt_pv == NULL) {
701c977e
MD
3041 if (pd_pv) {
3042 pv_put(pd_pv);
3043 pd_pv = NULL;
3044 }
3045 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
3046 } else if (pt_pv->pv_pindex != pmap_pt_pindex(sva)) {
701c977e
MD
3047 if (pd_pv) {
3048 pv_put(pd_pv);
3049 pd_pv = NULL;
3050 }
3051 pv_put(pt_pv);
3052 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
3053 }
c8fe38ae
MD
3054
3055 /*
921c891e
MD
3056 * If pt_pv is NULL we either have an shared page table
3057 * page and must issue a callback specific to that case,
3058 * or there is no page table page.
3059 *
3060 * Either way we can skip the page table page.
c8fe38ae 3061 */
701c977e 3062 if (pt_pv == NULL) {
921c891e
MD
3063 /*
3064 * Possible unmanaged (shared from another pmap)
3065 * page table page.
3066 */
3067 if (pd_pv == NULL)
3068 pd_pv = pv_get(pmap, pmap_pd_pindex(sva));
3069 KKASSERT(pd_pv != NULL);
3070 ptep = pv_pte_lookup(pd_pv, pmap_pt_index(sva));
3071 if (*ptep & PG_V) {
9df83100 3072 info->func(pmap, info, NULL, pd_pv, 1,
fb4ca018 3073 sva, ptep, info->arg);
921c891e
MD
3074 }
3075
3076 /*
3077 * Done, move to next page table page.
3078 */
701c977e
MD
3079 va_next = (sva + NBPDR) & ~PDRMASK;
3080 if (va_next < sva)
3081 va_next = eva;
c8fe38ae 3082 continue;
701c977e 3083 }
c8fe38ae
MD
3084
3085 /*
701c977e
MD
3086 * From this point in the loop testing pt_pv for non-NULL
3087 * means we are in UVM, else if it is NULL we are in KVM.
fb4ca018
MD
3088 *
3089 * Limit our scan to either the end of the va represented
3090 * by the current page table page, or to the end of the
3091 * range being removed.
48ffc236 3092 */
701c977e
MD
3093kernel_skip:
3094 va_next = (sva + NBPDR) & ~PDRMASK;
3095 if (va_next < sva)
3096 va_next = eva;
fb4ca018
MD
3097 if (va_next > eva)
3098 va_next = eva;
48ffc236
JG
3099
3100 /*
701c977e
MD
3101 * Scan the page table for pages. Some pages may not be
3102 * managed (might not have a pv_entry).
3103 *
3104 * There is no page table management for kernel pages so
3105 * pt_pv will be NULL in that case, but otherwise pt_pv
3106 * is non-NULL, locked, and referenced.
c8fe38ae 3107 */
c8fe38ae 3108
f2c5d4ab
MD
3109 /*
3110 * At this point a non-NULL pt_pv means a UVA, and a NULL
3111 * pt_pv means a KVA.
3112 */
701c977e
MD
3113 if (pt_pv)
3114 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(sva));
3115 else
3116 ptep = vtopte(sva);
3117
3118 while (sva < va_next) {
f2c5d4ab 3119 /*
90244566
MD
3120 * Acquire the related pte_pv, if any. If *ptep == 0
3121 * the related pte_pv should not exist, but if *ptep
3122 * is not zero the pte_pv may or may not exist (e.g.
3123 * will not exist for an unmanaged page).
f2c5d4ab 3124 *
90244566
MD
3125 * However a multitude of races are possible here.
3126 *
3127 * In addition, the (pt_pv, pte_pv) lock order is
3128 * backwards, so we have to be careful in aquiring
3129 * a properly locked pte_pv.
f2c5d4ab 3130 */
701c977e
MD
3131 if (pt_pv) {
3132 pte_pv = pv_get_try(pmap, pmap_pte_pindex(sva),
3133 &error);
3134 if (error) {
701c977e
MD
3135 if (pd_pv) {
3136 pv_put(pd_pv);
3137 pd_pv = NULL;
3138 }
3139 pv_put(pt_pv); /* must be non-NULL */
3140 pt_pv = NULL;
3141 pv_lock(pte_pv); /* safe to block now */
3142 pv_put(pte_pv);
3143 pte_pv = NULL;
3144 pt_pv = pv_get(pmap,
3145 pmap_pt_pindex(sva));
921c891e
MD
3146 /*
3147 * pt_pv reloaded, need new ptep
3148 */
3149 KKASSERT(pt_pv != NULL);
3150 ptep = pv_pte_lookup(pt_pv,
3151 pmap_pte_index(sva));
701c977e
MD
3152 continue;
3153 }
3154 } else {
3155 pte_pv = pv_get(pmap, pmap_pte_pindex(sva));
3156 }
3157
3158 /*
90244566 3159 * Ok, if *ptep == 0 we had better NOT have a pte_pv.
a505393f
MD
3160 */
3161 if (*ptep == 0) {
3162 if (pte_pv) {
90244566
MD
3163 kprintf("Unexpected non-NULL pte_pv "
3164 "%p pt_pv %p *ptep = %016lx\n",
3165 pte_pv, pt_pv, *ptep);
3166 panic("Unexpected non-NULL pte_pv");
a505393f 3167 }
90244566
MD
3168 sva += PAGE_SIZE;
3169 ++ptep;
a505393f
MD
3170 continue;
3171 }
3172
3173 /*
90244566
MD
3174 * Ready for the callback. The locked pte_pv (if any)
3175 * is consumed by the callback. pte_pv will exist if
3176 * the page is managed, and will not exist if it
3177 * isn't.
701c977e
MD
3178 */
3179 if (pte_pv) {
23b4bd44
MD
3180 KASSERT((*ptep & (PG_MANAGED|PG_V)) ==
3181 (PG_MANAGED|PG_V),
3182 ("bad *ptep %016lx sva %016lx "
3183 "pte_pv %p",
3184 *ptep, sva, pte_pv));
9df83100 3185 info->func(pmap, info, pte_pv, pt_pv, 0,
fb4ca018 3186 sva, ptep, info->arg);
701c977e 3187 } else {
23b4bd44
MD
3188 KASSERT((*ptep & (PG_MANAGED|PG_V)) ==
3189 PG_V,
3190 ("bad *ptep %016lx sva %016lx "
3191 "pte_pv NULL",
3192 *ptep, sva));
9df83100 3193 info->func(pmap, info, NULL, pt_pv, 0,
fb4ca018 3194 sva, ptep, info->arg);
701c977e 3195 }
f2c5d4ab 3196 pte_pv = NULL;
701c977e
MD
3197 sva += PAGE_SIZE;
3198 ++ptep;
c8fe38ae 3199 }
9df83100 3200 lwkt_yield();
c8fe38ae 3201 }
701c977e
MD
3202 if (pd_pv) {
3203 pv_put(pd_pv);
3204 pd_pv = NULL;
3205 }
3206 if (pt_pv) {
3207 pv_put(pt_pv);
3208 pt_pv = NULL;
3209 }
9df83100 3210 lwkt_yield();
fb4ca018
MD
3211
3212 /*
3213 * Relock before returning.
3214 */
3215 spin_lock(&pmap->pm_spin);
3216 return (0);
701c977e
MD
3217}
3218
3219void
3220pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
3221{
fb4ca018
MD
3222 struct pmap_scan_info info;
3223
3224 info.pmap = pmap;
3225 info.sva = sva;
3226 info.eva = eva;
3227 info.func = pmap_remove_callback;
3228 info.arg = NULL;
9df83100 3229 info.doinval = 1; /* normal remove requires pmap inval */
fb4ca018 3230 pmap_scan(&info);
701c977e
MD
3231}
3232
3233static void
9df83100
MD
3234pmap_remove_noinval(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
3235{
3236 struct pmap_scan_info info;
3237
3238 info.pmap = pmap;
3239 info.sva = sva;
3240 info.eva = eva;
3241 info.func = pmap_remove_callback;
3242 info.arg = NULL;
3243 info.doinval = 0; /* normal remove requires pmap inval */
3244 pmap_scan(&info);
3245}
3246
3247static void
3248pmap_remove_callback(pmap_t pmap, struct pmap_scan_info *info,
921c891e
MD
3249 pv_entry_t pte_pv, pv_entry_t pt_pv, int sharept,
3250 vm_offset_t va, pt_entry_t *ptep, void *arg __unused)
701c977e
MD
3251{
3252 pt_entry_t pte;
3253
3254 if (pte_pv) {
3255 /*
3256 * This will also drop pt_pv's wire_count. Note that
3257 * terminal pages are not wired based on mmu presence.
3258 */
9df83100
MD
3259 if (info->doinval)
3260 pmap_remove_pv_pte(pte_pv, pt_pv, &info->inval);
3261 else
3262 pmap_remove_pv_pte(pte_pv, pt_pv, NULL);
52bb73bc 3263 pmap_remove_pv_page(pte_pv);
701c977e 3264 pv_free(pte_pv);
921c891e 3265 } else if (sharept == 0) {
701c977e 3266 /*
921c891e
MD
3267 * Unmanaged page
3268 *
701c977e
MD
3269 * pt_pv's wire_count is still bumped by unmanaged pages
3270 * so we must decrement it manually.
3271 */
9df83100
MD
3272 if (info->doinval)
3273 pmap_inval_interlock(&info->inval, pmap, va);
701c977e 3274 pte = pte_load_clear(ptep);
9df83100
MD
3275 if (info->doinval)
3276 pmap_inval_deinterlock(&info->inval, pmap);
701c977e
MD
3277 if (pte & PG_W)
3278 atomic_add_long(&pmap->pm_stats.wired_count, -1);
3279 atomic_add_long(&pmap->pm_stats.resident_count, -1);
921c891e 3280 if (vm_page_unwire_quick(pt_pv->pv_m))
701c977e 3281 panic("pmap_remove: insufficient wirecount");
921c891e
MD
3282 } else {
3283 /*
3284 * Unmanaged page table, pt_pv is actually the pd_pv
3285 * for our pmap (not the share object pmap).
3286 *
3287 * We have to unwire the target page table page and we
3288 * have to unwire our page directory page.
3289 */
9df83100
MD
3290 if (info->doinval)
3291 pmap_inval_interlock(&info->inval, pmap, va);
921c891e 3292 pte = pte_load_clear(ptep);
9df83100
MD
3293 if (info->doinval)
3294 pmap_inval_deinterlock(&info->inval, pmap);
921c891e
MD
3295 atomic_add_long(&pmap->pm_stats.resident_count, -1);
3296 if (vm_page_unwire_quick(PHYS_TO_VM_PAGE(pte & PG_FRAME)))
3297 panic("pmap_remove: shared pgtable1 bad wirecount");
3298 if (vm_page_unwire_quick(pt_pv->pv_m))
3299 panic("pmap_remove: shared pgtable2 bad wirecount");
701c977e 3300 }
d7f50089
YY
3301}
3302
3303/*
b12defdc
MD
3304 * Removes this physical page from all physical maps in which it resides.
3305 * Reflects back modify bits to the pager.
d7f50089 3306 *
b12defdc 3307 * This routine may not be called from an interrupt.
d7f50089 3308 */
bfc09ba0
MD
3309static
3310void
d7f50089
YY
3311pmap_remove_all(vm_page_t m)
3312{
c8fe38ae 3313 struct pmap_inval_info info;
c8fe38ae
MD
3314 pv_entry_t pv;
3315
3316 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3317 return;
3318
3319 pmap_inval_init(&info);
701c977e 3320 vm_page_spin_lock(m);
c8fe38ae 3321 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
701c977e
MD
3322 KKASSERT(pv->pv_m == m);
3323 if (pv_hold_try(pv)) {
3324 vm_page_spin_unlock(m);
3325 } else {
3326 vm_page_spin_unlock(m);
3327 pv_lock(pv);
3328 if (pv->pv_m != m) {
3329 pv_put(pv);
3330 vm_page_spin_lock(m);
3331 continue;
3332 }
b12defdc 3333 }
b12defdc 3334 /*
701c977e 3335 * Holding no spinlocks, pv is locked.
b12defdc 3336 */
701c977e 3337 pmap_remove_pv_pte(pv, NULL, &info);
52bb73bc 3338 pmap_remove_pv_page(pv);
701c977e 3339 pv_free(pv);
b12defdc 3340 vm_page_spin_lock(m);
c8fe38ae 3341 }
c8fe38ae 3342 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
52bb73bc 3343 vm_page_spin_unlock(m);
c2fb025d 3344 pmap_inval_done(&info);
d7f50089
YY
3345}
3346
3347/*
921c891e
MD
3348 * Set the physical protection on the specified range of this map
3349 * as requested. This function is typically only used for debug watchpoints
3350 * and COW pages.
d7f50089 3351 *
921c891e
MD
3352 * This function may not be called from an interrupt if the map is
3353 * not the kernel_pmap.
d7f50089 3354 *
921c891e 3355 * NOTE! For shared page table pages we just unmap the page.
d7f50089
YY
3356 */
3357void
3358pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3359{
fb4ca018 3360 struct pmap_scan_info info;
48ffc236
JG
3361 /* JG review for NX */
3362
c8fe38ae
MD
3363 if (pmap == NULL)
3364 return;
c8fe38ae
MD
3365 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
3366 pmap_remove(pmap, sva, eva);
3367 return;
3368 }
c8fe38ae
MD
3369 if (prot & VM_PROT_WRITE)
3370 return;
fb4ca018
MD
3371 info.pmap = pmap;
3372 info.sva = sva;
3373 info.eva = eva;
3374 info.func = pmap_protect_callback;
3375 info.arg = &prot;
9df83100 3376 info.doinval = 1;
fb4ca018 3377 pmap_scan(&info);
701c977e 3378}
c8fe38ae 3379
701c977e
MD
3380static
3381void
9df83100 3382pmap_protect_callback(pmap_t pmap, struct pmap_scan_info *info,
921c891e
MD
3383 pv_entry_t pte_pv, pv_entry_t pt_pv, int sharept,
3384 vm_offset_t va, pt_entry_t *ptep, void *arg __unused)
701c977e
MD
3385{
3386 pt_entry_t pbits;
3387 pt_entry_t cbits;
921c891e 3388 pt_entry_t pte;
701c977e 3389 vm_page_t m;
c8fe38ae 3390
701c977e
MD
3391 /*
3392 * XXX non-optimal.
3393 */
9df83100 3394</