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