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