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