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