Home | History | Annotate | Line # | Download | only in pmap
pmap.c revision 1.22
      1  1.22      matt /*	$NetBSD: pmap.c,v 1.22 2016/09/16 17:27:09 matt Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  christos  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1  christos  * NASA Ames Research Center and by Chris G. Demetriou.
     10   1.1  christos  *
     11   1.1  christos  * Redistribution and use in source and binary forms, with or without
     12   1.1  christos  * modification, are permitted provided that the following conditions
     13   1.1  christos  * are met:
     14   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  christos  *    documentation and/or other materials provided with the distribution.
     19   1.1  christos  *
     20   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  christos  */
     32   1.1  christos 
     33   1.1  christos /*
     34   1.1  christos  * Copyright (c) 1992, 1993
     35   1.1  christos  *	The Regents of the University of California.  All rights reserved.
     36   1.1  christos  *
     37   1.1  christos  * This code is derived from software contributed to Berkeley by
     38   1.1  christos  * the Systems Programming Group of the University of Utah Computer
     39   1.1  christos  * Science Department and Ralph Campbell.
     40   1.1  christos  *
     41   1.1  christos  * Redistribution and use in source and binary forms, with or without
     42   1.1  christos  * modification, are permitted provided that the following conditions
     43   1.1  christos  * are met:
     44   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     45   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     46   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     48   1.1  christos  *    documentation and/or other materials provided with the distribution.
     49   1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     50   1.1  christos  *    may be used to endorse or promote products derived from this software
     51   1.1  christos  *    without specific prior written permission.
     52   1.1  christos  *
     53   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63   1.1  christos  * SUCH DAMAGE.
     64   1.1  christos  *
     65   1.1  christos  *	@(#)pmap.c	8.4 (Berkeley) 1/26/94
     66   1.1  christos  */
     67   1.1  christos 
     68   1.1  christos #include <sys/cdefs.h>
     69   1.1  christos 
     70  1.22      matt __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.22 2016/09/16 17:27:09 matt Exp $");
     71   1.1  christos 
     72   1.1  christos /*
     73   1.1  christos  *	Manages physical address maps.
     74   1.1  christos  *
     75   1.1  christos  *	In addition to hardware address maps, this
     76   1.1  christos  *	module is called upon to provide software-use-only
     77   1.1  christos  *	maps which may or may not be stored in the same
     78   1.1  christos  *	form as hardware maps.  These pseudo-maps are
     79   1.1  christos  *	used to store intermediate results from copy
     80   1.1  christos  *	operations to and from address spaces.
     81   1.1  christos  *
     82   1.1  christos  *	Since the information managed by this module is
     83   1.1  christos  *	also stored by the logical address mapping module,
     84   1.1  christos  *	this module may throw away valid virtual-to-physical
     85   1.1  christos  *	mappings at almost any time.  However, invalidations
     86   1.1  christos  *	of virtual-to-physical mappings must be done as
     87   1.1  christos  *	requested.
     88   1.1  christos  *
     89   1.1  christos  *	In order to cope with hardware architectures which
     90   1.1  christos  *	make virtual-to-physical map invalidates expensive,
     91   1.1  christos  *	this module may delay invalidate or reduced protection
     92   1.1  christos  *	operations until such time as they are actually
     93   1.1  christos  *	necessary.  This module is given full information as
     94   1.1  christos  *	to which processors are currently using which maps,
     95   1.1  christos  *	and to when physical maps must be made correct.
     96   1.1  christos  */
     97   1.1  christos 
     98   1.1  christos #include "opt_modular.h"
     99   1.1  christos #include "opt_multiprocessor.h"
    100   1.1  christos #include "opt_sysv.h"
    101   1.1  christos 
    102   1.1  christos #define __PMAP_PRIVATE
    103   1.1  christos 
    104   1.1  christos #include <sys/param.h>
    105  1.15      matt #include <sys/atomic.h>
    106   1.1  christos #include <sys/buf.h>
    107  1.15      matt #include <sys/cpu.h>
    108  1.15      matt #include <sys/mutex.h>
    109   1.1  christos #include <sys/pool.h>
    110   1.1  christos #include <sys/atomic.h>
    111   1.1  christos #include <sys/mutex.h>
    112   1.1  christos #include <sys/atomic.h>
    113   1.1  christos 
    114   1.1  christos #include <uvm/uvm.h>
    115   1.1  christos 
    116  1.15      matt #if defined(MULTIPROCESSOR) && defined(PMAP_VIRTUAL_CACHE_ALIASES) \
    117  1.15      matt     && !defined(PMAP_NO_PV_UNCACHED)
    118  1.15      matt #error PMAP_VIRTUAL_CACHE_ALIASES with MULTIPROCESSOR requires \
    119  1.15      matt  PMAP_NO_PV_UNCACHED to be defined
    120  1.15      matt #endif
    121   1.1  christos 
    122   1.1  christos PMAP_COUNTER(remove_kernel_calls, "remove kernel calls");
    123   1.1  christos PMAP_COUNTER(remove_kernel_pages, "kernel pages unmapped");
    124   1.1  christos PMAP_COUNTER(remove_user_calls, "remove user calls");
    125   1.1  christos PMAP_COUNTER(remove_user_pages, "user pages unmapped");
    126   1.1  christos PMAP_COUNTER(remove_flushes, "remove cache flushes");
    127   1.1  christos PMAP_COUNTER(remove_tlb_ops, "remove tlb ops");
    128   1.1  christos PMAP_COUNTER(remove_pvfirst, "remove pv first");
    129   1.1  christos PMAP_COUNTER(remove_pvsearch, "remove pv search");
    130   1.1  christos 
    131   1.1  christos PMAP_COUNTER(prefer_requests, "prefer requests");
    132   1.1  christos PMAP_COUNTER(prefer_adjustments, "prefer adjustments");
    133   1.1  christos 
    134   1.1  christos PMAP_COUNTER(idlezeroed_pages, "pages idle zeroed");
    135   1.1  christos 
    136   1.1  christos PMAP_COUNTER(kenter_pa, "kernel fast mapped pages");
    137   1.1  christos PMAP_COUNTER(kenter_pa_bad, "kernel fast mapped pages (bad color)");
    138   1.1  christos PMAP_COUNTER(kenter_pa_unmanaged, "kernel fast mapped unmanaged pages");
    139   1.1  christos PMAP_COUNTER(kremove_pages, "kernel fast unmapped pages");
    140   1.1  christos 
    141   1.1  christos PMAP_COUNTER(page_cache_evictions, "pages changed to uncacheable");
    142   1.1  christos PMAP_COUNTER(page_cache_restorations, "pages changed to cacheable");
    143   1.1  christos 
    144   1.1  christos PMAP_COUNTER(kernel_mappings_bad, "kernel pages mapped (bad color)");
    145   1.1  christos PMAP_COUNTER(user_mappings_bad, "user pages mapped (bad color)");
    146   1.1  christos PMAP_COUNTER(kernel_mappings, "kernel pages mapped");
    147   1.1  christos PMAP_COUNTER(user_mappings, "user pages mapped");
    148   1.1  christos PMAP_COUNTER(user_mappings_changed, "user mapping changed");
    149   1.1  christos PMAP_COUNTER(kernel_mappings_changed, "kernel mapping changed");
    150   1.1  christos PMAP_COUNTER(uncached_mappings, "uncached pages mapped");
    151   1.1  christos PMAP_COUNTER(unmanaged_mappings, "unmanaged pages mapped");
    152   1.1  christos PMAP_COUNTER(managed_mappings, "managed pages mapped");
    153   1.1  christos PMAP_COUNTER(mappings, "pages mapped");
    154   1.1  christos PMAP_COUNTER(remappings, "pages remapped");
    155   1.1  christos PMAP_COUNTER(unmappings, "pages unmapped");
    156   1.1  christos PMAP_COUNTER(primary_mappings, "page initial mappings");
    157   1.1  christos PMAP_COUNTER(primary_unmappings, "page final unmappings");
    158   1.1  christos PMAP_COUNTER(tlb_hit, "page mapping");
    159   1.1  christos 
    160   1.1  christos PMAP_COUNTER(exec_mappings, "exec pages mapped");
    161   1.1  christos PMAP_COUNTER(exec_synced_mappings, "exec pages synced");
    162   1.1  christos PMAP_COUNTER(exec_synced_remove, "exec pages synced (PR)");
    163   1.1  christos PMAP_COUNTER(exec_synced_clear_modify, "exec pages synced (CM)");
    164   1.1  christos PMAP_COUNTER(exec_synced_page_protect, "exec pages synced (PP)");
    165   1.1  christos PMAP_COUNTER(exec_synced_protect, "exec pages synced (P)");
    166   1.1  christos PMAP_COUNTER(exec_uncached_page_protect, "exec pages uncached (PP)");
    167   1.1  christos PMAP_COUNTER(exec_uncached_clear_modify, "exec pages uncached (CM)");
    168   1.1  christos PMAP_COUNTER(exec_uncached_zero_page, "exec pages uncached (ZP)");
    169   1.1  christos PMAP_COUNTER(exec_uncached_copy_page, "exec pages uncached (CP)");
    170   1.1  christos PMAP_COUNTER(exec_uncached_remove, "exec pages uncached (PR)");
    171   1.1  christos 
    172   1.1  christos PMAP_COUNTER(create, "creates");
    173   1.1  christos PMAP_COUNTER(reference, "references");
    174   1.1  christos PMAP_COUNTER(dereference, "dereferences");
    175   1.1  christos PMAP_COUNTER(destroy, "destroyed");
    176   1.1  christos PMAP_COUNTER(activate, "activations");
    177   1.1  christos PMAP_COUNTER(deactivate, "deactivations");
    178   1.1  christos PMAP_COUNTER(update, "updates");
    179   1.1  christos #ifdef MULTIPROCESSOR
    180   1.1  christos PMAP_COUNTER(shootdown_ipis, "shootdown IPIs");
    181   1.1  christos #endif
    182   1.1  christos PMAP_COUNTER(unwire, "unwires");
    183   1.1  christos PMAP_COUNTER(copy, "copies");
    184   1.1  christos PMAP_COUNTER(clear_modify, "clear_modifies");
    185   1.1  christos PMAP_COUNTER(protect, "protects");
    186   1.1  christos PMAP_COUNTER(page_protect, "page_protects");
    187   1.1  christos 
    188   1.1  christos #define PMAP_ASID_RESERVED 0
    189   1.1  christos CTASSERT(PMAP_ASID_RESERVED == 0);
    190   1.1  christos 
    191  1.15      matt #ifndef PMAP_SEGTAB_ALIGN
    192  1.15      matt #define PMAP_SEGTAB_ALIGN	/* nothing */
    193  1.15      matt #endif
    194  1.15      matt #ifdef _LP64
    195  1.15      matt pmap_segtab_t	pmap_kstart_segtab PMAP_SEGTAB_ALIGN; /* first mid-level segtab for kernel */
    196  1.15      matt #endif
    197  1.15      matt pmap_segtab_t	pmap_kern_segtab PMAP_SEGTAB_ALIGN = { /* top level segtab for kernel */
    198  1.15      matt #ifdef _LP64
    199  1.15      matt 	.seg_seg[(VM_MIN_KERNEL_ADDRESS & XSEGOFSET) >> SEGSHIFT] = &pmap_kstart_segtab,
    200   1.1  christos #endif
    201  1.15      matt };
    202   1.1  christos 
    203   1.1  christos struct pmap_kernel kernel_pmap_store = {
    204   1.1  christos 	.kernel_pmap = {
    205   1.1  christos 		.pm_count = 1,
    206  1.15      matt 		.pm_segtab = &pmap_kern_segtab,
    207   1.1  christos 		.pm_minaddr = VM_MIN_KERNEL_ADDRESS,
    208   1.1  christos 		.pm_maxaddr = VM_MAX_KERNEL_ADDRESS,
    209   1.1  christos 	},
    210   1.1  christos };
    211   1.1  christos 
    212   1.1  christos struct pmap * const kernel_pmap_ptr = &kernel_pmap_store.kernel_pmap;
    213   1.1  christos 
    214  1.15      matt struct pmap_limits pmap_limits = {	/* VA and PA limits */
    215  1.12      matt 	.virtual_start = VM_MIN_KERNEL_ADDRESS,
    216  1.12      matt };
    217   1.1  christos 
    218   1.1  christos #ifdef UVMHIST
    219   1.1  christos static struct kern_history_ent pmapexechistbuf[10000];
    220   1.1  christos static struct kern_history_ent pmaphistbuf[10000];
    221   1.8    nonaka UVMHIST_DEFINE(pmapexechist);
    222   1.8    nonaka UVMHIST_DEFINE(pmaphist);
    223   1.1  christos #endif
    224   1.1  christos 
    225   1.1  christos /*
    226   1.1  christos  * The pools from which pmap structures and sub-structures are allocated.
    227   1.1  christos  */
    228   1.1  christos struct pool pmap_pmap_pool;
    229   1.1  christos struct pool pmap_pv_pool;
    230   1.1  christos 
    231   1.1  christos #ifndef PMAP_PV_LOWAT
    232   1.1  christos #define	PMAP_PV_LOWAT	16
    233   1.1  christos #endif
    234  1.15      matt int	pmap_pv_lowat = PMAP_PV_LOWAT;
    235   1.1  christos 
    236  1.15      matt bool	pmap_initialized = false;
    237   1.1  christos #define	PMAP_PAGE_COLOROK_P(a, b) \
    238   1.1  christos 		((((int)(a) ^ (int)(b)) & pmap_page_colormask) == 0)
    239  1.15      matt u_int	pmap_page_colormask;
    240   1.1  christos 
    241  1.15      matt #define PAGE_IS_MANAGED(pa)	(pmap_initialized && uvm_pageismanaged(pa))
    242   1.1  christos 
    243   1.1  christos #define PMAP_IS_ACTIVE(pm)						\
    244   1.1  christos 	((pm) == pmap_kernel() || 					\
    245   1.1  christos 	 (pm) == curlwp->l_proc->p_vmspace->vm_map.pmap)
    246   1.1  christos 
    247   1.1  christos /* Forward function declarations */
    248  1.15      matt void pmap_page_remove(struct vm_page *);
    249  1.15      matt static void pmap_pvlist_check(struct vm_page_md *);
    250   1.1  christos void pmap_remove_pv(pmap_t, vaddr_t, struct vm_page *, bool);
    251  1.15      matt void pmap_enter_pv(pmap_t, vaddr_t, struct vm_page *, pt_entry_t *, u_int);
    252   1.1  christos 
    253   1.1  christos /*
    254   1.1  christos  * PV table management functions.
    255   1.1  christos  */
    256   1.1  christos void	*pmap_pv_page_alloc(struct pool *, int);
    257   1.1  christos void	pmap_pv_page_free(struct pool *, void *);
    258   1.1  christos 
    259   1.1  christos struct pool_allocator pmap_pv_page_allocator = {
    260   1.1  christos 	pmap_pv_page_alloc, pmap_pv_page_free, 0,
    261   1.1  christos };
    262   1.1  christos 
    263   1.1  christos #define	pmap_pv_alloc()		pool_get(&pmap_pv_pool, PR_NOWAIT)
    264   1.1  christos #define	pmap_pv_free(pv)	pool_put(&pmap_pv_pool, (pv))
    265   1.1  christos 
    266  1.10    nonaka #if !defined(MULTIPROCESSOR) || !defined(PMAP_MD_NEED_TLB_MISS_LOCK)
    267  1.10    nonaka #define	pmap_md_tlb_miss_lock_enter()	do { } while(/*CONSTCOND*/0)
    268  1.10    nonaka #define	pmap_md_tlb_miss_lock_exit()	do { } while(/*CONSTCOND*/0)
    269  1.15      matt #endif /* !MULTIPROCESSOR || !PMAP_MD_NEED_TLB_MISS_LOCK */
    270  1.15      matt 
    271  1.15      matt #ifndef MULTIPROCESSOR
    272  1.15      matt kmutex_t pmap_pvlist_mutex	__cacheline_aligned;
    273  1.15      matt #endif
    274  1.15      matt 
    275  1.15      matt /*
    276  1.15      matt  * Debug functions.
    277  1.15      matt  */
    278  1.15      matt 
    279  1.19  jakllsch #ifdef DEBUG
    280  1.15      matt static inline void
    281  1.15      matt pmap_asid_check(pmap_t pm, const char *func)
    282  1.15      matt {
    283  1.15      matt 	if (!PMAP_IS_ACTIVE(pm))
    284  1.15      matt 		return;
    285  1.15      matt 
    286  1.15      matt 	struct pmap_asid_info * const pai = PMAP_PAI(pm, cpu_tlb_info(curcpu()));
    287  1.15      matt 	tlb_asid_t asid = tlb_get_asid();
    288  1.15      matt 	if (asid != pai->pai_asid)
    289  1.15      matt 		panic("%s: inconsistency for active TLB update: %u <-> %u",
    290  1.15      matt 		    func, asid, pai->pai_asid);
    291  1.19  jakllsch }
    292  1.15      matt #endif
    293  1.15      matt 
    294  1.15      matt static void
    295  1.15      matt pmap_addr_range_check(pmap_t pmap, vaddr_t sva, vaddr_t eva, const char *func)
    296  1.15      matt {
    297  1.15      matt #ifdef DEBUG
    298  1.15      matt 	if (pmap == pmap_kernel()) {
    299  1.15      matt 		if (sva < VM_MIN_KERNEL_ADDRESS)
    300  1.15      matt 			panic("%s: kva %#"PRIxVADDR" not in range",
    301  1.15      matt 			    func, sva);
    302  1.15      matt 		if (eva >= pmap_limits.virtual_end)
    303  1.15      matt 			panic("%s: kva %#"PRIxVADDR" not in range",
    304  1.15      matt 			    func, eva);
    305  1.15      matt 	} else {
    306  1.15      matt 		if (eva > VM_MAXUSER_ADDRESS)
    307  1.15      matt 			panic("%s: uva %#"PRIxVADDR" not in range",
    308  1.15      matt 			    func, eva);
    309  1.15      matt 		pmap_asid_check(pmap, func);
    310  1.15      matt 	}
    311  1.15      matt #endif
    312  1.15      matt }
    313  1.10    nonaka 
    314   1.1  christos /*
    315   1.1  christos  * Misc. functions.
    316   1.1  christos  */
    317   1.1  christos 
    318   1.1  christos bool
    319   1.1  christos pmap_page_clear_attributes(struct vm_page_md *mdpg, u_int clear_attributes)
    320   1.1  christos {
    321  1.15      matt 	volatile unsigned long * const attrp = &mdpg->mdpg_attrs;
    322   1.1  christos #ifdef MULTIPROCESSOR
    323   1.1  christos 	for (;;) {
    324   1.1  christos 		u_int old_attr = *attrp;
    325   1.1  christos 		if ((old_attr & clear_attributes) == 0)
    326   1.1  christos 			return false;
    327   1.1  christos 		u_int new_attr = old_attr & ~clear_attributes;
    328  1.15      matt 		if (old_attr == atomic_cas_ulong(attrp, old_attr, new_attr))
    329   1.1  christos 			return true;
    330   1.1  christos 	}
    331   1.1  christos #else
    332  1.15      matt 	unsigned long old_attr = *attrp;
    333   1.1  christos 	if ((old_attr & clear_attributes) == 0)
    334   1.1  christos 		return false;
    335   1.1  christos 	*attrp &= ~clear_attributes;
    336   1.1  christos 	return true;
    337   1.1  christos #endif
    338   1.1  christos }
    339   1.1  christos 
    340   1.1  christos void
    341   1.1  christos pmap_page_set_attributes(struct vm_page_md *mdpg, u_int set_attributes)
    342   1.1  christos {
    343   1.1  christos #ifdef MULTIPROCESSOR
    344  1.15      matt 	atomic_or_ulong(&mdpg->mdpg_attrs, set_attributes);
    345   1.1  christos #else
    346   1.1  christos 	mdpg->mdpg_attrs |= set_attributes;
    347   1.1  christos #endif
    348   1.1  christos }
    349   1.1  christos 
    350   1.1  christos static void
    351   1.1  christos pmap_page_syncicache(struct vm_page *pg)
    352   1.1  christos {
    353   1.1  christos #ifndef MULTIPROCESSOR
    354  1.15      matt 	struct pmap * const curpmap = curlwp->l_proc->p_vmspace->vm_map.pmap;
    355   1.1  christos #endif
    356   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    357   1.1  christos 	pv_entry_t pv = &mdpg->mdpg_first;
    358   1.2      matt 	kcpuset_t *onproc;
    359   1.2      matt #ifdef MULTIPROCESSOR
    360   1.2      matt 	kcpuset_create(&onproc, true);
    361  1.15      matt 	KASSERT(onproc != NULL);
    362   1.3      matt #else
    363   1.3      matt 	onproc = NULL;
    364   1.2      matt #endif
    365  1.15      matt 	VM_PAGEMD_PVLIST_READLOCK(mdpg);
    366  1.15      matt 	pmap_pvlist_check(mdpg);
    367   1.2      matt 
    368   1.1  christos 	if (pv->pv_pmap != NULL) {
    369   1.1  christos 		for (; pv != NULL; pv = pv->pv_next) {
    370   1.1  christos #ifdef MULTIPROCESSOR
    371   1.2      matt 			kcpuset_merge(onproc, pv->pv_pmap->pm_onproc);
    372   1.2      matt 			if (kcpuset_match(onproc, kcpuset_running)) {
    373   1.1  christos 				break;
    374   1.1  christos 			}
    375   1.1  christos #else
    376   1.1  christos 			if (pv->pv_pmap == curpmap) {
    377   1.2      matt 				onproc = curcpu()->ci_data.cpu_kcpuset;
    378   1.1  christos 				break;
    379   1.1  christos 			}
    380   1.1  christos #endif
    381   1.1  christos 		}
    382   1.1  christos 	}
    383  1.15      matt 	pmap_pvlist_check(mdpg);
    384   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    385   1.1  christos 	kpreempt_disable();
    386   1.1  christos 	pmap_md_page_syncicache(pg, onproc);
    387  1.15      matt 	kpreempt_enable();
    388   1.2      matt #ifdef MULTIPROCESSOR
    389   1.2      matt 	kcpuset_destroy(onproc);
    390   1.2      matt #endif
    391   1.1  christos }
    392   1.1  christos 
    393   1.1  christos /*
    394   1.1  christos  * Define the initial bounds of the kernel virtual address space.
    395   1.1  christos  */
    396   1.1  christos void
    397   1.1  christos pmap_virtual_space(vaddr_t *vstartp, vaddr_t *vendp)
    398   1.1  christos {
    399   1.1  christos 
    400  1.12      matt 	*vstartp = pmap_limits.virtual_start;
    401  1.12      matt 	*vendp = pmap_limits.virtual_end;
    402   1.1  christos }
    403   1.1  christos 
    404   1.1  christos vaddr_t
    405   1.1  christos pmap_growkernel(vaddr_t maxkvaddr)
    406   1.1  christos {
    407  1.14   msaitoh 	vaddr_t virtual_end = pmap_limits.virtual_end;
    408   1.1  christos 	maxkvaddr = pmap_round_seg(maxkvaddr) - 1;
    409   1.1  christos 
    410   1.1  christos 	/*
    411   1.1  christos 	 * Reserve PTEs for the new KVA space.
    412   1.1  christos 	 */
    413   1.1  christos 	for (; virtual_end < maxkvaddr; virtual_end += NBSEG) {
    414   1.1  christos 		pmap_pte_reserve(pmap_kernel(), virtual_end, 0);
    415   1.1  christos 	}
    416   1.1  christos 
    417   1.1  christos 	/*
    418   1.1  christos 	 * Don't exceed VM_MAX_KERNEL_ADDRESS!
    419   1.1  christos 	 */
    420   1.1  christos 	if (virtual_end == 0 || virtual_end > VM_MAX_KERNEL_ADDRESS)
    421   1.1  christos 		virtual_end = VM_MAX_KERNEL_ADDRESS;
    422   1.1  christos 
    423   1.1  christos 	/*
    424   1.1  christos 	 * Update new end.
    425   1.1  christos 	 */
    426   1.1  christos 	pmap_limits.virtual_end = virtual_end;
    427   1.1  christos 	return virtual_end;
    428   1.1  christos }
    429   1.1  christos 
    430   1.1  christos /*
    431   1.1  christos  * Bootstrap memory allocator (alternative to vm_bootstrap_steal_memory()).
    432   1.1  christos  * This function allows for early dynamic memory allocation until the virtual
    433   1.1  christos  * memory system has been bootstrapped.  After that point, either kmem_alloc
    434   1.1  christos  * or malloc should be used.  This function works by stealing pages from the
    435   1.1  christos  * (to be) managed page pool, then implicitly mapping the pages (by using
    436   1.1  christos  * their k0seg addresses) and zeroing them.
    437   1.1  christos  *
    438   1.1  christos  * It may be used once the physical memory segments have been pre-loaded
    439   1.1  christos  * into the vm_physmem[] array.  Early memory allocation MUST use this
    440   1.1  christos  * interface!  This cannot be used after vm_page_startup(), and will
    441   1.1  christos  * generate a panic if tried.
    442   1.1  christos  *
    443   1.1  christos  * Note that this memory will never be freed, and in essence it is wired
    444   1.1  christos  * down.
    445   1.1  christos  *
    446   1.1  christos  * We must adjust *vstartp and/or *vendp iff we use address space
    447   1.1  christos  * from the kernel virtual address range defined by pmap_virtual_space().
    448   1.1  christos  */
    449   1.1  christos vaddr_t
    450   1.1  christos pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
    451   1.1  christos {
    452  1.15      matt 	size_t npgs;
    453   1.1  christos 	paddr_t pa;
    454   1.1  christos 	vaddr_t va;
    455  1.15      matt 	struct vm_physseg *maybe_seg = NULL;
    456  1.15      matt 	u_int maybe_bank = vm_nphysseg;
    457   1.1  christos 
    458   1.1  christos 	size = round_page(size);
    459   1.1  christos 	npgs = atop(size);
    460   1.1  christos 
    461  1.15      matt 	aprint_debug("%s: need %zu pages\n", __func__, npgs);
    462  1.15      matt 
    463   1.1  christos 	for (u_int bank = 0; bank < vm_nphysseg; bank++) {
    464   1.1  christos 		struct vm_physseg * const seg = VM_PHYSMEM_PTR(bank);
    465   1.1  christos 		if (uvm.page_init_done == true)
    466   1.1  christos 			panic("pmap_steal_memory: called _after_ bootstrap");
    467   1.1  christos 
    468  1.15      matt 		aprint_debug("%s: seg %u: %#"PRIxPADDR" %#"PRIxPADDR" %#"PRIxPADDR" %#"PRIxPADDR"\n",
    469  1.15      matt 		    __func__, bank,
    470  1.15      matt 		    seg->avail_start, seg->start,
    471  1.15      matt 		    seg->avail_end, seg->end);
    472  1.15      matt 
    473  1.15      matt 		if (seg->avail_start != seg->start
    474  1.15      matt 		    || seg->avail_start >= seg->avail_end) {
    475  1.15      matt 			aprint_debug("%s: seg %u: bad start\n", __func__, bank);
    476   1.1  christos 			continue;
    477  1.15      matt 		}
    478   1.1  christos 
    479  1.15      matt 		if (seg->avail_end - seg->avail_start < npgs) {
    480  1.15      matt 			aprint_debug("%s: seg %u: too small for %zu pages\n",
    481  1.15      matt 			    __func__, bank, npgs);
    482   1.1  christos 			continue;
    483  1.15      matt 		}
    484  1.15      matt 
    485  1.15      matt 		if (!pmap_md_ok_to_steal_p(seg, npgs)) {
    486  1.15      matt 			continue;
    487  1.15      matt 		}
    488  1.15      matt 
    489  1.15      matt 		/*
    490  1.15      matt 		 * Always try to allocate from the segment with the least
    491  1.15      matt 		 * amount of space left.
    492  1.15      matt 		 */
    493  1.15      matt #define VM_PHYSMEM_SPACE(s)	((s)->avail_end - (s)->avail_start)
    494  1.17     skrll 		if (maybe_seg == NULL
    495  1.15      matt 		    || VM_PHYSMEM_SPACE(seg) < VM_PHYSMEM_SPACE(maybe_seg)) {
    496  1.15      matt 			maybe_seg = seg;
    497  1.15      matt 			maybe_bank = bank;
    498  1.15      matt 		}
    499  1.15      matt 	}
    500  1.15      matt 
    501  1.15      matt 	if (maybe_seg) {
    502  1.15      matt 		struct vm_physseg * const seg = maybe_seg;
    503  1.15      matt 		u_int bank = maybe_bank;
    504   1.1  christos 
    505   1.1  christos 		/*
    506   1.1  christos 		 * There are enough pages here; steal them!
    507   1.1  christos 		 */
    508   1.1  christos 		pa = ptoa(seg->avail_start);
    509   1.1  christos 		seg->avail_start += npgs;
    510   1.1  christos 		seg->start += npgs;
    511   1.1  christos 
    512   1.1  christos 		/*
    513   1.1  christos 		 * Have we used up this segment?
    514   1.1  christos 		 */
    515   1.1  christos 		if (seg->avail_start == seg->end) {
    516   1.1  christos 			if (vm_nphysseg == 1)
    517   1.1  christos 				panic("pmap_steal_memory: out of memory!");
    518   1.1  christos 
    519  1.15      matt 			aprint_debug("%s: seg %u: %zu pages stolen (removed)\n",
    520  1.15      matt 			    __func__, bank, npgs);
    521   1.1  christos 			/* Remove this segment from the list. */
    522   1.1  christos 			vm_nphysseg--;
    523  1.15      matt 			for (u_int x = bank; x < vm_nphysseg; x++) {
    524  1.15      matt 				/* structure copy */
    525  1.15      matt 				VM_PHYSMEM_PTR_SWAP(x, x + 1);
    526  1.15      matt 			}
    527  1.15      matt 		} else {
    528  1.15      matt 			aprint_debug("%s: seg %u: %zu pages stolen (%#"PRIxPADDR" left)\n",
    529  1.15      matt 			    __func__, bank, npgs, VM_PHYSMEM_SPACE(seg));
    530   1.1  christos 		}
    531   1.1  christos 
    532   1.1  christos 		va = pmap_md_map_poolpage(pa, size);
    533   1.1  christos 		memset((void *)va, 0, size);
    534   1.1  christos 		return va;
    535   1.1  christos 	}
    536   1.1  christos 
    537   1.1  christos 	/*
    538   1.1  christos 	 * If we got here, there was no memory left.
    539   1.1  christos 	 */
    540  1.15      matt 	panic("pmap_steal_memory: no memory to steal %zu pages", npgs);
    541   1.1  christos }
    542   1.1  christos 
    543   1.1  christos /*
    544   1.1  christos  *	Initialize the pmap module.
    545   1.1  christos  *	Called by vm_init, to initialize any structures that the pmap
    546   1.1  christos  *	system needs to map virtual memory.
    547   1.1  christos  */
    548   1.1  christos void
    549   1.1  christos pmap_init(void)
    550   1.1  christos {
    551   1.1  christos 	UVMHIST_INIT_STATIC(pmapexechist, pmapexechistbuf);
    552   1.1  christos 	UVMHIST_INIT_STATIC(pmaphist, pmaphistbuf);
    553   1.1  christos 
    554   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    555   1.1  christos 
    556   1.1  christos 	/*
    557   1.1  christos 	 * Initialize the segtab lock.
    558   1.1  christos 	 */
    559   1.1  christos 	mutex_init(&pmap_segtab_lock, MUTEX_DEFAULT, IPL_HIGH);
    560   1.1  christos 
    561   1.1  christos 	/*
    562   1.1  christos 	 * Set a low water mark on the pv_entry pool, so that we are
    563   1.1  christos 	 * more likely to have these around even in extreme memory
    564   1.1  christos 	 * starvation.
    565   1.1  christos 	 */
    566   1.1  christos 	pool_setlowat(&pmap_pv_pool, pmap_pv_lowat);
    567   1.1  christos 
    568  1.15      matt 	/*
    569  1.15      matt 	 * Set the page colormask but allow pmap_md_init to override it.
    570  1.15      matt 	 */
    571  1.15      matt 	pmap_page_colormask = ptoa(uvmexp.colormask);
    572  1.15      matt 
    573   1.1  christos 	pmap_md_init();
    574   1.1  christos 
    575   1.1  christos 	/*
    576   1.1  christos 	 * Now it is safe to enable pv entry recording.
    577   1.1  christos 	 */
    578   1.1  christos 	pmap_initialized = true;
    579   1.1  christos }
    580   1.1  christos 
    581   1.1  christos /*
    582   1.1  christos  *	Create and return a physical map.
    583   1.1  christos  *
    584   1.1  christos  *	If the size specified for the map
    585   1.1  christos  *	is zero, the map is an actual physical
    586   1.1  christos  *	map, and may be referenced by the
    587   1.1  christos  *	hardware.
    588   1.1  christos  *
    589   1.1  christos  *	If the size specified is non-zero,
    590   1.1  christos  *	the map will be used in software only, and
    591   1.1  christos  *	is bounded by that size.
    592   1.1  christos  */
    593   1.1  christos pmap_t
    594   1.1  christos pmap_create(void)
    595   1.1  christos {
    596   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    597   1.1  christos 	PMAP_COUNT(create);
    598   1.1  christos 
    599  1.15      matt 	pmap_t pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
    600   1.1  christos 	memset(pmap, 0, PMAP_SIZE);
    601   1.1  christos 
    602   1.1  christos 	KASSERT(pmap->pm_pai[0].pai_link.le_prev == NULL);
    603   1.1  christos 
    604   1.1  christos 	pmap->pm_count = 1;
    605   1.1  christos 	pmap->pm_minaddr = VM_MIN_ADDRESS;
    606   1.1  christos 	pmap->pm_maxaddr = VM_MAXUSER_ADDRESS;
    607   1.1  christos 
    608   1.1  christos 	pmap_segtab_init(pmap);
    609   1.1  christos 
    610   1.5    nonaka #ifdef MULTIPROCESSOR
    611   1.5    nonaka 	kcpuset_create(&pmap->pm_active, true);
    612   1.5    nonaka 	kcpuset_create(&pmap->pm_onproc, true);
    613  1.15      matt 	KASSERT(pmap->pm_active != NULL);
    614  1.15      matt 	KASSERT(pmap->pm_onproc != NULL);
    615   1.5    nonaka #endif
    616   1.5    nonaka 
    617  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done (pmap=%p)", pmap, 0, 0, 0);
    618  1.15      matt 
    619   1.1  christos 	return pmap;
    620   1.1  christos }
    621   1.1  christos 
    622   1.1  christos /*
    623   1.1  christos  *	Retire the given physical map from service.
    624   1.1  christos  *	Should only be called if the map contains
    625   1.1  christos  *	no valid mappings.
    626   1.1  christos  */
    627   1.1  christos void
    628   1.1  christos pmap_destroy(pmap_t pmap)
    629   1.1  christos {
    630   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    631  1.15      matt 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0, 0, 0);
    632   1.1  christos 
    633   1.1  christos 	if (atomic_dec_uint_nv(&pmap->pm_count) > 0) {
    634   1.1  christos 		PMAP_COUNT(dereference);
    635  1.15      matt 		UVMHIST_LOG(pmaphist, " <-- done (deref)", 0, 0, 0, 0);
    636   1.1  christos 		return;
    637   1.1  christos 	}
    638   1.1  christos 
    639  1.15      matt 	PMAP_COUNT(destroy);
    640   1.1  christos 	KASSERT(pmap->pm_count == 0);
    641   1.1  christos 	kpreempt_disable();
    642  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    643   1.1  christos 	pmap_tlb_asid_release_all(pmap);
    644   1.1  christos 	pmap_segtab_destroy(pmap, NULL, 0);
    645  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    646   1.1  christos 
    647   1.6    nonaka #ifdef MULTIPROCESSOR
    648   1.7    nonaka 	kcpuset_destroy(pmap->pm_active);
    649   1.7    nonaka 	kcpuset_destroy(pmap->pm_onproc);
    650  1.15      matt 	pmap->pm_active = NULL;
    651  1.15      matt 	pmap->pm_onproc = NULL;
    652   1.6    nonaka #endif
    653   1.6    nonaka 
    654   1.1  christos 	pool_put(&pmap_pmap_pool, pmap);
    655   1.1  christos 	kpreempt_enable();
    656   1.1  christos 
    657  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done (freed)", 0, 0, 0, 0);
    658   1.1  christos }
    659   1.1  christos 
    660   1.1  christos /*
    661   1.1  christos  *	Add a reference to the specified pmap.
    662   1.1  christos  */
    663   1.1  christos void
    664   1.1  christos pmap_reference(pmap_t pmap)
    665   1.1  christos {
    666   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    667  1.15      matt 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0, 0, 0);
    668   1.1  christos 	PMAP_COUNT(reference);
    669   1.1  christos 
    670   1.1  christos 	if (pmap != NULL) {
    671   1.1  christos 		atomic_inc_uint(&pmap->pm_count);
    672   1.1  christos 	}
    673   1.1  christos 
    674  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    675   1.1  christos }
    676   1.1  christos 
    677   1.1  christos /*
    678   1.1  christos  *	Make a new pmap (vmspace) active for the given process.
    679   1.1  christos  */
    680   1.1  christos void
    681   1.1  christos pmap_activate(struct lwp *l)
    682   1.1  christos {
    683   1.1  christos 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    684   1.1  christos 
    685   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    686  1.15      matt 	UVMHIST_LOG(pmaphist, "(l=%p (pmap=%p))", l, pmap, 0, 0);
    687   1.1  christos 	PMAP_COUNT(activate);
    688   1.1  christos 
    689   1.1  christos 	kpreempt_disable();
    690  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    691   1.1  christos 	pmap_tlb_asid_acquire(pmap, l);
    692   1.1  christos 	if (l == curlwp) {
    693   1.1  christos 		pmap_segtab_activate(pmap, l);
    694   1.1  christos 	}
    695  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    696   1.1  christos 	kpreempt_enable();
    697   1.1  christos 
    698  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    699  1.15      matt }
    700  1.15      matt 
    701  1.15      matt /*
    702  1.15      matt  * Remove this page from all physical maps in which it resides.
    703  1.15      matt  * Reflects back modify bits to the pager.
    704  1.15      matt  */
    705  1.15      matt void
    706  1.15      matt pmap_page_remove(struct vm_page *pg)
    707  1.15      matt {
    708  1.15      matt 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    709  1.15      matt 
    710  1.15      matt 	kpreempt_disable();
    711  1.15      matt 	VM_PAGEMD_PVLIST_LOCK(mdpg);
    712  1.15      matt 	pmap_pvlist_check(mdpg);
    713  1.15      matt 
    714  1.15      matt 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    715  1.15      matt 
    716  1.22      matt 	UVMHIST_LOG(pmapexechist, "pg %p (pa %#"PRIxPADDR")%s: %s",
    717  1.22      matt 	    pg, VM_PAGE_TO_PHYS(pg), " [page removed]", "execpage cleared");
    718  1.22      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
    719  1.22      matt 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE|VM_PAGEMD_UNCACHED);
    720  1.22      matt #else
    721  1.22      matt 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
    722  1.22      matt #endif
    723  1.22      matt 	PMAP_COUNT(exec_uncached_remove);
    724  1.22      matt 
    725  1.15      matt 	pv_entry_t pv = &mdpg->mdpg_first;
    726  1.15      matt 	if (pv->pv_pmap == NULL) {
    727  1.15      matt 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    728  1.15      matt 		kpreempt_enable();
    729  1.15      matt 		UVMHIST_LOG(pmaphist, " <-- done (empty)", 0, 0, 0, 0);
    730  1.15      matt 		return;
    731  1.15      matt 	}
    732  1.15      matt 
    733  1.15      matt 	pv_entry_t npv;
    734  1.15      matt 	pv_entry_t pvp = NULL;
    735  1.15      matt 
    736  1.15      matt 	for (; pv != NULL; pv = npv) {
    737  1.15      matt 		npv = pv->pv_next;
    738  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
    739  1.15      matt 		if (pv->pv_va & PV_KENTER) {
    740  1.15      matt 			UVMHIST_LOG(pmaphist, " pv %p pmap %p va %"
    741  1.15      matt 			    PRIxVADDR" skip", pv, pv->pv_pmap, pv->pv_va, 0);
    742  1.15      matt 
    743  1.15      matt 			KASSERT(pv->pv_pmap == pmap_kernel());
    744  1.15      matt 
    745  1.15      matt 			/* Assume no more - it'll get fixed if there are */
    746  1.15      matt 			pv->pv_next = NULL;
    747  1.15      matt 
    748  1.15      matt 			/*
    749  1.15      matt 			 * pvp is non-null when we already have a PV_KENTER
    750  1.15      matt 			 * pv in pvh_first; otherwise we haven't seen a
    751  1.15      matt 			 * PV_KENTER pv and we need to copy this one to
    752  1.15      matt 			 * pvh_first
    753  1.15      matt 			 */
    754  1.15      matt 			if (pvp) {
    755  1.15      matt 				/*
    756  1.15      matt 				 * The previous PV_KENTER pv needs to point to
    757  1.15      matt 				 * this PV_KENTER pv
    758  1.15      matt 				 */
    759  1.15      matt 				pvp->pv_next = pv;
    760  1.15      matt 			} else {
    761  1.15      matt 				pv_entry_t fpv = &mdpg->mdpg_first;
    762  1.15      matt 				*fpv = *pv;
    763  1.15      matt 				KASSERT(fpv->pv_pmap == pmap_kernel());
    764  1.15      matt 			}
    765  1.15      matt 			pvp = pv;
    766  1.15      matt 			continue;
    767  1.15      matt 		}
    768  1.15      matt #endif
    769  1.15      matt 		const pmap_t pmap = pv->pv_pmap;
    770  1.15      matt 		vaddr_t va = trunc_page(pv->pv_va);
    771  1.15      matt 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
    772  1.15      matt 		KASSERTMSG(ptep != NULL, "%#"PRIxVADDR " %#"PRIxVADDR, va,
    773  1.15      matt 		    pmap_limits.virtual_end);
    774  1.15      matt 		pt_entry_t pte = *ptep;
    775  1.15      matt 		UVMHIST_LOG(pmaphist, " pv %p pmap %p va %"PRIxVADDR
    776  1.15      matt 		    " pte %#"PRIxPTE, pv, pmap, va, pte_value(pte));
    777  1.15      matt 		if (!pte_valid_p(pte))
    778  1.15      matt 			continue;
    779  1.15      matt 		const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    780  1.15      matt 		if (is_kernel_pmap_p) {
    781  1.15      matt 			PMAP_COUNT(remove_kernel_pages);
    782  1.15      matt 		} else {
    783  1.15      matt 			PMAP_COUNT(remove_user_pages);
    784  1.15      matt 		}
    785  1.15      matt 		if (pte_wired_p(pte))
    786  1.15      matt 			pmap->pm_stats.wired_count--;
    787  1.15      matt 		pmap->pm_stats.resident_count--;
    788  1.15      matt 
    789  1.15      matt 		pmap_md_tlb_miss_lock_enter();
    790  1.15      matt 		const pt_entry_t npte = pte_nv_entry(is_kernel_pmap_p);
    791  1.15      matt 		*ptep = npte;
    792  1.15      matt 		/*
    793  1.15      matt 		 * Flush the TLB for the given address.
    794  1.15      matt 		 */
    795  1.15      matt 		pmap_tlb_invalidate_addr(pmap, va);
    796  1.15      matt 		pmap_md_tlb_miss_lock_exit();
    797  1.15      matt 
    798  1.15      matt 		/*
    799  1.15      matt 		 * non-null means this is a non-pvh_first pv, so we should
    800  1.15      matt 		 * free it.
    801  1.15      matt 		 */
    802  1.15      matt 		if (pvp) {
    803  1.15      matt 			KASSERT(pvp->pv_pmap == pmap_kernel());
    804  1.15      matt 			KASSERT(pvp->pv_next == NULL);
    805  1.15      matt 			pmap_pv_free(pv);
    806  1.15      matt 		} else {
    807  1.15      matt 			pv->pv_pmap = NULL;
    808  1.15      matt 			pv->pv_next = NULL;
    809  1.15      matt 		}
    810  1.15      matt 	}
    811  1.15      matt 
    812  1.15      matt 	pmap_pvlist_check(mdpg);
    813  1.15      matt 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    814  1.15      matt 	kpreempt_enable();
    815  1.15      matt 
    816  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    817   1.1  christos }
    818   1.1  christos 
    819  1.15      matt 
    820   1.1  christos /*
    821   1.1  christos  *	Make a previously active pmap (vmspace) inactive.
    822   1.1  christos  */
    823   1.1  christos void
    824   1.1  christos pmap_deactivate(struct lwp *l)
    825   1.1  christos {
    826   1.1  christos 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    827   1.1  christos 
    828   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    829  1.15      matt 	UVMHIST_LOG(pmaphist, "(l=%p (pmap=%p))", l, pmap, 0, 0);
    830   1.1  christos 	PMAP_COUNT(deactivate);
    831   1.1  christos 
    832   1.1  christos 	kpreempt_disable();
    833  1.15      matt 	KASSERT(l == curlwp || l->l_cpu == curlwp->l_cpu);
    834  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    835   1.1  christos 	curcpu()->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    836  1.15      matt #ifdef _LP64
    837  1.15      matt 	curcpu()->ci_pmap_user_seg0tab = NULL;
    838  1.15      matt #endif
    839   1.1  christos 	pmap_tlb_asid_deactivate(pmap);
    840  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    841   1.1  christos 	kpreempt_enable();
    842   1.1  christos 
    843  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    844   1.1  christos }
    845   1.1  christos 
    846   1.1  christos void
    847   1.1  christos pmap_update(struct pmap *pmap)
    848   1.1  christos {
    849   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    850  1.15      matt 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0, 0, 0);
    851   1.1  christos 	PMAP_COUNT(update);
    852   1.1  christos 
    853   1.1  christos 	kpreempt_disable();
    854  1.18     skrll #if defined(MULTIPROCESSOR) && defined(PMAP_TLB_NEED_SHOOTDOWN)
    855   1.1  christos 	u_int pending = atomic_swap_uint(&pmap->pm_shootdown_pending, 0);
    856   1.1  christos 	if (pending && pmap_tlb_shootdown_bystanders(pmap))
    857   1.1  christos 		PMAP_COUNT(shootdown_ipis);
    858   1.1  christos #endif
    859  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    860  1.11    nonaka #if defined(DEBUG) && !defined(MULTIPROCESSOR)
    861   1.1  christos 	pmap_tlb_check(pmap, pmap_md_tlb_check_entry);
    862   1.1  christos #endif /* DEBUG */
    863   1.1  christos 
    864   1.1  christos 	/*
    865   1.1  christos 	 * If pmap_remove_all was called, we deactivated ourselves and nuked
    866   1.1  christos 	 * our ASID.  Now we have to reactivate ourselves.
    867   1.1  christos 	 */
    868   1.1  christos 	if (__predict_false(pmap->pm_flags & PMAP_DEFERRED_ACTIVATE)) {
    869   1.1  christos 		pmap->pm_flags ^= PMAP_DEFERRED_ACTIVATE;
    870   1.1  christos 		pmap_tlb_asid_acquire(pmap, curlwp);
    871   1.1  christos 		pmap_segtab_activate(pmap, curlwp);
    872   1.1  christos 	}
    873  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    874   1.1  christos 	kpreempt_enable();
    875   1.1  christos 
    876  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done%s",
    877  1.15      matt 	    (pmap == pmap_kernel()) ? " (kernel)" : "", 0, 0, 0);
    878   1.1  christos }
    879   1.1  christos 
    880   1.1  christos /*
    881   1.1  christos  *	Remove the given range of addresses from the specified map.
    882   1.1  christos  *
    883   1.1  christos  *	It is assumed that the start and end are properly
    884   1.1  christos  *	rounded to the page size.
    885   1.1  christos  */
    886   1.1  christos 
    887   1.1  christos static bool
    888   1.1  christos pmap_pte_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
    889   1.1  christos 	uintptr_t flags)
    890   1.1  christos {
    891   1.1  christos 	const pt_entry_t npte = flags;
    892   1.1  christos 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    893   1.1  christos 
    894   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    895  1.15      matt 	UVMHIST_LOG(pmaphist, "(pmap=%p %sva=%#"PRIxVADDR"..%#"PRIxVADDR,
    896   1.1  christos 	    pmap, (is_kernel_pmap_p ? "(kernel) " : ""), sva, eva);
    897   1.1  christos 	UVMHIST_LOG(pmaphist, "ptep=%p, flags(npte)=%#"PRIxPTR")",
    898   1.1  christos 	    ptep, flags, 0, 0);
    899   1.1  christos 
    900   1.1  christos 	KASSERT(kpreempt_disabled());
    901   1.1  christos 
    902   1.1  christos 	for (; sva < eva; sva += NBPG, ptep++) {
    903  1.15      matt 		const pt_entry_t pte = *ptep;
    904  1.15      matt 		if (!pte_valid_p(pte))
    905   1.1  christos 			continue;
    906  1.15      matt 		if (is_kernel_pmap_p) {
    907  1.15      matt 			PMAP_COUNT(remove_kernel_pages);
    908  1.15      matt 		} else {
    909   1.1  christos 			PMAP_COUNT(remove_user_pages);
    910  1.15      matt 		}
    911  1.15      matt 		if (pte_wired_p(pte))
    912   1.1  christos 			pmap->pm_stats.wired_count--;
    913   1.1  christos 		pmap->pm_stats.resident_count--;
    914  1.15      matt 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pte_to_paddr(pte));
    915   1.1  christos 		if (__predict_true(pg != NULL)) {
    916  1.15      matt 			pmap_remove_pv(pmap, sva, pg, pte_modified_p(pte));
    917   1.1  christos 		}
    918  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
    919   1.1  christos 		*ptep = npte;
    920   1.1  christos 		/*
    921   1.1  christos 		 * Flush the TLB for the given address.
    922   1.1  christos 		 */
    923   1.1  christos 		pmap_tlb_invalidate_addr(pmap, sva);
    924  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
    925   1.1  christos 	}
    926  1.15      matt 
    927  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    928  1.15      matt 
    929   1.1  christos 	return false;
    930   1.1  christos }
    931   1.1  christos 
    932   1.1  christos void
    933   1.1  christos pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva)
    934   1.1  christos {
    935   1.1  christos 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    936   1.1  christos 	const pt_entry_t npte = pte_nv_entry(is_kernel_pmap_p);
    937   1.1  christos 
    938   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    939   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p, va=%#"PRIxVADDR"..%#"PRIxVADDR")",
    940   1.1  christos 	    pmap, sva, eva, 0);
    941   1.1  christos 
    942  1.15      matt 	if (is_kernel_pmap_p) {
    943   1.1  christos 		PMAP_COUNT(remove_kernel_calls);
    944  1.15      matt 	} else {
    945   1.1  christos 		PMAP_COUNT(remove_user_calls);
    946   1.1  christos 	}
    947  1.15      matt #ifdef PMAP_FAULTINFO
    948  1.15      matt 	curpcb->pcb_faultinfo.pfi_faultaddr = 0;
    949  1.15      matt 	curpcb->pcb_faultinfo.pfi_repeats = 0;
    950  1.15      matt 	curpcb->pcb_faultinfo.pfi_faultpte = NULL;
    951   1.1  christos #endif
    952   1.1  christos 	kpreempt_disable();
    953  1.15      matt 	pmap_addr_range_check(pmap, sva, eva, __func__);
    954   1.1  christos 	pmap_pte_process(pmap, sva, eva, pmap_pte_remove, npte);
    955   1.1  christos 	kpreempt_enable();
    956   1.1  christos 
    957  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
    958   1.1  christos }
    959   1.1  christos 
    960   1.1  christos /*
    961   1.1  christos  *	pmap_page_protect:
    962   1.1  christos  *
    963   1.1  christos  *	Lower the permission for all mappings to a given page.
    964   1.1  christos  */
    965   1.1  christos void
    966   1.1  christos pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
    967   1.1  christos {
    968   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    969   1.1  christos 	pv_entry_t pv;
    970   1.1  christos 	vaddr_t va;
    971   1.1  christos 
    972   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    973   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR") prot=%#x)",
    974   1.1  christos 	    pg, VM_PAGE_TO_PHYS(pg), prot, 0);
    975   1.1  christos 	PMAP_COUNT(page_protect);
    976   1.1  christos 
    977   1.1  christos 	switch (prot) {
    978   1.1  christos 	case VM_PROT_READ|VM_PROT_WRITE:
    979   1.1  christos 	case VM_PROT_ALL:
    980   1.1  christos 		break;
    981   1.1  christos 
    982   1.1  christos 	/* copy_on_write */
    983   1.1  christos 	case VM_PROT_READ:
    984   1.1  christos 	case VM_PROT_READ|VM_PROT_EXECUTE:
    985   1.1  christos 		pv = &mdpg->mdpg_first;
    986  1.15      matt 		kpreempt_disable();
    987  1.15      matt 		VM_PAGEMD_PVLIST_READLOCK(mdpg);
    988  1.15      matt 		pmap_pvlist_check(mdpg);
    989   1.1  christos 		/*
    990  1.15      matt 		 * Loop over all current mappings setting/clearing as apropos.
    991   1.1  christos 		 */
    992   1.1  christos 		if (pv->pv_pmap != NULL) {
    993   1.1  christos 			while (pv != NULL) {
    994  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
    995  1.15      matt 				if (pv->pv_va & PV_KENTER) {
    996  1.15      matt 					pv = pv->pv_next;
    997  1.15      matt 					continue;
    998  1.15      matt 				}
    999  1.15      matt #endif
   1000   1.1  christos 				const pmap_t pmap = pv->pv_pmap;
   1001  1.15      matt 				va = trunc_page(pv->pv_va);
   1002  1.15      matt 				const uintptr_t gen =
   1003  1.15      matt 				    VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1004   1.1  christos 				pmap_protect(pmap, va, va + PAGE_SIZE, prot);
   1005   1.1  christos 				KASSERT(pv->pv_pmap == pmap);
   1006   1.1  christos 				pmap_update(pmap);
   1007  1.15      matt 				if (gen != VM_PAGEMD_PVLIST_READLOCK(mdpg)) {
   1008   1.1  christos 					pv = &mdpg->mdpg_first;
   1009   1.1  christos 				} else {
   1010   1.1  christos 					pv = pv->pv_next;
   1011   1.1  christos 				}
   1012  1.15      matt 				pmap_pvlist_check(mdpg);
   1013   1.1  christos 			}
   1014   1.1  christos 		}
   1015  1.15      matt 		pmap_pvlist_check(mdpg);
   1016   1.1  christos 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1017  1.15      matt 		kpreempt_enable();
   1018   1.1  christos 		break;
   1019   1.1  christos 
   1020   1.1  christos 	/* remove_all */
   1021   1.1  christos 	default:
   1022  1.15      matt 		pmap_page_remove(pg);
   1023   1.1  christos 	}
   1024   1.1  christos 
   1025  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1026   1.1  christos }
   1027   1.1  christos 
   1028   1.1  christos static bool
   1029   1.1  christos pmap_pte_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
   1030   1.1  christos 	uintptr_t flags)
   1031   1.1  christos {
   1032   1.1  christos 	const vm_prot_t prot = (flags & VM_PROT_ALL);
   1033   1.1  christos 
   1034   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1035  1.15      matt 	UVMHIST_LOG(pmaphist, "(pmap=%p %sva=%#"PRIxVADDR"..%#"PRIxVADDR,
   1036   1.1  christos 	    pmap, (pmap == pmap_kernel() ? "(kernel) " : ""), sva, eva);
   1037   1.1  christos 	UVMHIST_LOG(pmaphist, "ptep=%p, flags(npte)=%#"PRIxPTR")",
   1038   1.1  christos 	    ptep, flags, 0, 0);
   1039   1.1  christos 
   1040   1.1  christos 	KASSERT(kpreempt_disabled());
   1041   1.1  christos 	/*
   1042   1.1  christos 	 * Change protection on every valid mapping within this segment.
   1043   1.1  christos 	 */
   1044   1.1  christos 	for (; sva < eva; sva += NBPG, ptep++) {
   1045  1.15      matt 		pt_entry_t pte = *ptep;
   1046  1.15      matt 		if (!pte_valid_p(pte))
   1047   1.1  christos 			continue;
   1048  1.15      matt 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pte_to_paddr(pte));
   1049  1.15      matt 		if (pg != NULL && pte_modified_p(pte)) {
   1050   1.1  christos 			struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1051   1.1  christos 			if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
   1052   1.1  christos 				KASSERT(mdpg->mdpg_first.pv_pmap != NULL);
   1053  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1054  1.15      matt 				if (VM_PAGEMD_CACHED_P(mdpg)) {
   1055  1.15      matt #endif
   1056   1.1  christos 					UVMHIST_LOG(pmapexechist,
   1057   1.1  christos 					    "pg %p (pa %#"PRIxPADDR"): %s",
   1058   1.1  christos 					    pg, VM_PAGE_TO_PHYS(pg),
   1059   1.1  christos 					    "syncicached performed", 0);
   1060   1.1  christos 					pmap_page_syncicache(pg);
   1061   1.1  christos 					PMAP_COUNT(exec_synced_protect);
   1062  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1063   1.1  christos 				}
   1064  1.15      matt #endif
   1065   1.1  christos 			}
   1066   1.1  christos 		}
   1067  1.15      matt 		pte = pte_prot_downgrade(pte, prot);
   1068  1.15      matt 		if (*ptep != pte) {
   1069  1.10    nonaka 			pmap_md_tlb_miss_lock_enter();
   1070  1.15      matt 			*ptep = pte;
   1071   1.1  christos 			/*
   1072   1.1  christos 			 * Update the TLB if needed.
   1073   1.1  christos 			 */
   1074  1.15      matt 			pmap_tlb_update_addr(pmap, sva, pte, PMAP_TLB_NEED_IPI);
   1075  1.10    nonaka 			pmap_md_tlb_miss_lock_exit();
   1076   1.1  christos 		}
   1077   1.1  christos 	}
   1078  1.15      matt 
   1079  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1080  1.15      matt 
   1081   1.1  christos 	return false;
   1082   1.1  christos }
   1083   1.1  christos 
   1084   1.1  christos /*
   1085   1.1  christos  *	Set the physical protection on the
   1086   1.1  christos  *	specified range of this map as requested.
   1087   1.1  christos  */
   1088   1.1  christos void
   1089   1.1  christos pmap_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   1090   1.1  christos {
   1091   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1092   1.1  christos 	UVMHIST_LOG(pmaphist,
   1093  1.15      matt 	    "(pmap=%p, va=%#"PRIxVADDR"..%#"PRIxVADDR", prot=%u)",
   1094   1.1  christos 	    pmap, sva, eva, prot);
   1095   1.1  christos 	PMAP_COUNT(protect);
   1096   1.1  christos 
   1097   1.1  christos 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
   1098   1.1  christos 		pmap_remove(pmap, sva, eva);
   1099  1.15      matt 		UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1100   1.1  christos 		return;
   1101   1.1  christos 	}
   1102   1.1  christos 
   1103   1.1  christos 	/*
   1104   1.1  christos 	 * Change protection on every valid mapping within this segment.
   1105   1.1  christos 	 */
   1106   1.1  christos 	kpreempt_disable();
   1107  1.15      matt 	pmap_addr_range_check(pmap, sva, eva, __func__);
   1108   1.1  christos 	pmap_pte_process(pmap, sva, eva, pmap_pte_protect, prot);
   1109   1.1  christos 	kpreempt_enable();
   1110   1.1  christos 
   1111  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1112   1.1  christos }
   1113   1.1  christos 
   1114  1.15      matt #if defined(PMAP_VIRTUAL_CACHE_ALIASES) && !defined(PMAP_NO_PV_UNCACHED)
   1115   1.1  christos /*
   1116   1.1  christos  *	pmap_page_cache:
   1117   1.1  christos  *
   1118   1.1  christos  *	Change all mappings of a managed page to cached/uncached.
   1119   1.1  christos  */
   1120  1.15      matt void
   1121   1.1  christos pmap_page_cache(struct vm_page *pg, bool cached)
   1122   1.1  christos {
   1123   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1124  1.15      matt 
   1125   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1126   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR") cached=%s)",
   1127   1.1  christos 	    pg, VM_PAGE_TO_PHYS(pg), cached ? "true" : "false", 0);
   1128  1.15      matt 
   1129   1.1  christos 	KASSERT(kpreempt_disabled());
   1130  1.15      matt 	KASSERT(VM_PAGEMD_PVLIST_LOCKED_P(mdpg));
   1131   1.1  christos 
   1132   1.1  christos 	if (cached) {
   1133   1.1  christos 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_UNCACHED);
   1134   1.1  christos 		PMAP_COUNT(page_cache_restorations);
   1135   1.1  christos 	} else {
   1136   1.1  christos 		pmap_page_set_attributes(mdpg, VM_PAGEMD_UNCACHED);
   1137   1.1  christos 		PMAP_COUNT(page_cache_evictions);
   1138   1.1  christos 	}
   1139   1.1  christos 
   1140  1.15      matt 	for (pv_entry_t pv = &mdpg->mdpg_first; pv != NULL; pv = pv->pv_next) {
   1141   1.1  christos 		pmap_t pmap = pv->pv_pmap;
   1142  1.15      matt 		vaddr_t va = trunc_page(pv->pv_va);
   1143   1.1  christos 
   1144   1.1  christos 		KASSERT(pmap != NULL);
   1145   1.1  christos 		KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
   1146   1.1  christos 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1147   1.1  christos 		if (ptep == NULL)
   1148   1.1  christos 			continue;
   1149  1.15      matt 		pt_entry_t pte = *ptep;
   1150  1.15      matt 		if (pte_valid_p(pte)) {
   1151  1.15      matt 			pte = pte_cached_change(pte, cached);
   1152  1.10    nonaka 			pmap_md_tlb_miss_lock_enter();
   1153  1.15      matt 			*ptep = pte;
   1154  1.15      matt 			pmap_tlb_update_addr(pmap, va, pte, PMAP_TLB_NEED_IPI);
   1155  1.10    nonaka 			pmap_md_tlb_miss_lock_exit();
   1156   1.1  christos 		}
   1157   1.1  christos 	}
   1158  1.15      matt 
   1159  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1160   1.1  christos }
   1161  1.15      matt #endif	/* PMAP_VIRTUAL_CACHE_ALIASES && !PMAP_NO_PV_UNCACHED */
   1162   1.1  christos 
   1163   1.1  christos /*
   1164   1.1  christos  *	Insert the given physical page (p) at
   1165   1.1  christos  *	the specified virtual address (v) in the
   1166   1.1  christos  *	target physical map with the protection requested.
   1167   1.1  christos  *
   1168   1.1  christos  *	If specified, the page will be wired down, meaning
   1169   1.1  christos  *	that the related pte can not be reclaimed.
   1170   1.1  christos  *
   1171   1.1  christos  *	NB:  This is the only routine which MAY NOT lazy-evaluate
   1172   1.1  christos  *	or lose information.  That is, this routine must actually
   1173   1.1  christos  *	insert this page into the given map NOW.
   1174   1.1  christos  */
   1175   1.1  christos int
   1176   1.1  christos pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   1177   1.1  christos {
   1178   1.1  christos 	const bool wired = (flags & PMAP_WIRED) != 0;
   1179   1.1  christos 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
   1180  1.15      matt 	u_int update_flags = (flags & VM_PROT_ALL) != 0 ? PMAP_TLB_INSERT : 0;
   1181   1.1  christos #ifdef UVMHIST
   1182  1.15      matt 	struct kern_history * const histp =
   1183   1.1  christos 	    ((prot & VM_PROT_EXECUTE) ? &pmapexechist : &pmaphist);
   1184   1.1  christos #endif
   1185   1.1  christos 
   1186  1.15      matt 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(*histp);
   1187   1.1  christos #define VM_PROT_STRING(prot) \
   1188  1.15      matt 	&"\0     " \
   1189  1.15      matt 	 "(R)\0  " \
   1190  1.15      matt 	 "(W)\0  " \
   1191  1.15      matt 	 "(RW)\0 " \
   1192  1.15      matt 	 "(X)\0  " \
   1193  1.15      matt 	 "(RX)\0 " \
   1194  1.15      matt 	 "(WX)\0 " \
   1195  1.15      matt 	 "(RWX)\0"[UVM_PROTECTION(prot)*6]
   1196   1.1  christos 	UVMHIST_LOG(*histp, "(pmap=%p, va=%#"PRIxVADDR", pa=%#"PRIxPADDR,
   1197   1.1  christos 	    pmap, va, pa, 0);
   1198   1.1  christos 	UVMHIST_LOG(*histp, "prot=%#x%s flags=%#x%s)",
   1199   1.1  christos 	    prot, VM_PROT_STRING(prot), flags, VM_PROT_STRING(flags));
   1200   1.1  christos 
   1201   1.1  christos 	const bool good_color = PMAP_PAGE_COLOROK_P(pa, va);
   1202   1.1  christos 	if (is_kernel_pmap_p) {
   1203   1.1  christos 		PMAP_COUNT(kernel_mappings);
   1204   1.1  christos 		if (!good_color)
   1205   1.1  christos 			PMAP_COUNT(kernel_mappings_bad);
   1206   1.1  christos 	} else {
   1207   1.1  christos 		PMAP_COUNT(user_mappings);
   1208   1.1  christos 		if (!good_color)
   1209   1.1  christos 			PMAP_COUNT(user_mappings_bad);
   1210   1.1  christos 	}
   1211  1.15      matt 	pmap_addr_range_check(pmap, va, va, __func__);
   1212   1.1  christos 
   1213  1.15      matt 	KASSERTMSG(prot & VM_PROT_READ, "no READ (%#x) in prot %#x",
   1214  1.15      matt 	    VM_PROT_READ, prot);
   1215   1.1  christos 
   1216   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1217  1.15      matt 	struct vm_page_md * const mdpg = (pg ? VM_PAGE_TO_MD(pg) : NULL);
   1218   1.1  christos 
   1219   1.1  christos 	if (pg) {
   1220   1.1  christos 		/* Set page referenced/modified status based on flags */
   1221  1.15      matt 		if (flags & VM_PROT_WRITE) {
   1222   1.1  christos 			pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1223  1.15      matt 		} else if (flags & VM_PROT_ALL) {
   1224   1.1  christos 			pmap_page_set_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1225  1.15      matt 		}
   1226   1.1  christos 
   1227  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1228  1.15      matt 		if (!VM_PAGEMD_CACHED_P(mdpg)) {
   1229   1.1  christos 			flags |= PMAP_NOCACHE;
   1230  1.15      matt 			PMAP_COUNT(uncached_mappings);
   1231  1.15      matt 		}
   1232   1.1  christos #endif
   1233   1.1  christos 
   1234   1.1  christos 		PMAP_COUNT(managed_mappings);
   1235   1.1  christos 	} else {
   1236   1.1  christos 		/*
   1237   1.1  christos 		 * Assumption: if it is not part of our managed memory
   1238   1.1  christos 		 * then it must be device memory which may be volatile.
   1239   1.1  christos 		 */
   1240  1.15      matt 		if ((flags & PMAP_CACHE_MASK) == 0)
   1241  1.15      matt 			flags |= PMAP_NOCACHE;
   1242   1.1  christos 		PMAP_COUNT(unmanaged_mappings);
   1243   1.1  christos 	}
   1244   1.1  christos 
   1245  1.15      matt 	pt_entry_t npte = pte_make_enter(pa, mdpg, prot, flags,
   1246  1.15      matt 	    is_kernel_pmap_p);
   1247   1.1  christos 
   1248   1.1  christos 	kpreempt_disable();
   1249  1.15      matt 
   1250   1.1  christos 	pt_entry_t * const ptep = pmap_pte_reserve(pmap, va, flags);
   1251   1.1  christos 	if (__predict_false(ptep == NULL)) {
   1252   1.1  christos 		kpreempt_enable();
   1253  1.15      matt 		UVMHIST_LOG(*histp, " <-- ENOMEM", 0, 0, 0, 0);
   1254   1.1  christos 		return ENOMEM;
   1255   1.1  christos 	}
   1256  1.15      matt 	const pt_entry_t opte = *ptep;
   1257   1.1  christos 
   1258   1.1  christos 	/* Done after case that may sleep/return. */
   1259   1.1  christos 	if (pg)
   1260  1.15      matt 		pmap_enter_pv(pmap, va, pg, &npte, 0);
   1261   1.1  christos 
   1262   1.1  christos 	/*
   1263   1.1  christos 	 * Now validate mapping with desired protection/wiring.
   1264   1.1  christos 	 * Assume uniform modified and referenced status for all
   1265   1.1  christos 	 * MIPS pages in a MACH page.
   1266   1.1  christos 	 */
   1267   1.1  christos 	if (wired) {
   1268   1.1  christos 		pmap->pm_stats.wired_count++;
   1269   1.1  christos 		npte = pte_wire_entry(npte);
   1270   1.1  christos 	}
   1271   1.1  christos 
   1272  1.15      matt 	UVMHIST_LOG(*histp, "new pte %#"PRIxPTE" (pa %#"PRIxPADDR")",
   1273  1.15      matt 	    pte_value(npte), pa, 0, 0);
   1274   1.1  christos 
   1275   1.1  christos 	if (pte_valid_p(opte) && pte_to_paddr(opte) != pa) {
   1276   1.1  christos 		pmap_remove(pmap, va, va + NBPG);
   1277   1.1  christos 		PMAP_COUNT(user_mappings_changed);
   1278   1.1  christos 	}
   1279   1.1  christos 
   1280   1.1  christos 	KASSERT(pte_valid_p(npte));
   1281  1.15      matt 	const bool resident = pte_valid_p(opte);
   1282  1.15      matt 	if (resident) {
   1283  1.15      matt 		update_flags |= PMAP_TLB_NEED_IPI;
   1284  1.15      matt 	} else {
   1285   1.1  christos 		pmap->pm_stats.resident_count++;
   1286  1.15      matt 	}
   1287  1.15      matt 
   1288  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
   1289   1.1  christos 	*ptep = npte;
   1290  1.15      matt 	pmap_tlb_update_addr(pmap, va, npte, update_flags);
   1291  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
   1292   1.1  christos 	kpreempt_enable();
   1293   1.1  christos 
   1294   1.1  christos 	if (pg != NULL && (prot == (VM_PROT_READ | VM_PROT_EXECUTE))) {
   1295   1.1  christos 		KASSERT(mdpg != NULL);
   1296   1.1  christos 		PMAP_COUNT(exec_mappings);
   1297   1.1  christos 		if (!VM_PAGEMD_EXECPAGE_P(mdpg) && pte_cached_p(npte)) {
   1298   1.1  christos 			if (!pte_deferred_exec_p(npte)) {
   1299   1.1  christos 				UVMHIST_LOG(*histp,
   1300   1.1  christos 				    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1301   1.1  christos 				    va, pg, "immediate", "");
   1302   1.1  christos 				pmap_page_syncicache(pg);
   1303   1.1  christos 				pmap_page_set_attributes(mdpg,
   1304   1.1  christos 				    VM_PAGEMD_EXECPAGE);
   1305   1.1  christos 				PMAP_COUNT(exec_synced_mappings);
   1306   1.1  christos 			} else {
   1307   1.1  christos 				UVMHIST_LOG(*histp, "va=%#"PRIxVADDR
   1308   1.1  christos 				    " pg %p: %s syncicache: pte %#x",
   1309   1.1  christos 				    va, pg, "defer", npte);
   1310   1.1  christos 			}
   1311   1.1  christos 		} else {
   1312   1.1  christos 			UVMHIST_LOG(*histp,
   1313   1.1  christos 			    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1314   1.1  christos 			    va, pg, "no",
   1315   1.1  christos 			    (pte_cached_p(npte)
   1316   1.1  christos 				? " (already exec)"
   1317   1.1  christos 				: " (uncached)"));
   1318   1.1  christos 		}
   1319   1.1  christos 	} else if (pg != NULL && (prot & VM_PROT_EXECUTE)) {
   1320   1.1  christos 		KASSERT(mdpg != NULL);
   1321   1.1  christos 		KASSERT(prot & VM_PROT_WRITE);
   1322   1.1  christos 		PMAP_COUNT(exec_mappings);
   1323   1.1  christos 		pmap_page_syncicache(pg);
   1324   1.1  christos 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1325  1.15      matt 		UVMHIST_LOG(*histp,
   1326   1.1  christos 		    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1327   1.1  christos 		    va, pg, "immediate", " (writeable)");
   1328   1.1  christos 	}
   1329   1.1  christos 
   1330  1.15      matt 	UVMHIST_LOG(*histp, " <-- 0 (OK)", 0, 0, 0, 0);
   1331   1.1  christos 	return 0;
   1332   1.1  christos }
   1333   1.1  christos 
   1334   1.1  christos void
   1335   1.1  christos pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   1336   1.1  christos {
   1337  1.15      matt 	pmap_t pmap = pmap_kernel();
   1338   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1339  1.15      matt 	struct vm_page_md * const mdpg = (pg ? VM_PAGE_TO_MD(pg) : NULL);
   1340   1.1  christos 
   1341   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1342  1.15      matt 	UVMHIST_LOG(pmaphist,
   1343  1.15      matt 	    "(va=%#"PRIxVADDR", pa=%#"PRIxPADDR", prot=%u, flags=%#x)",
   1344  1.15      matt 	    va, pa, prot, flags);
   1345   1.1  christos 	PMAP_COUNT(kenter_pa);
   1346   1.1  christos 
   1347  1.15      matt 	if (mdpg == NULL) {
   1348   1.1  christos 		PMAP_COUNT(kenter_pa_unmanaged);
   1349  1.15      matt 		if ((flags & PMAP_CACHE_MASK) == 0)
   1350  1.15      matt 			flags |= PMAP_NOCACHE;
   1351   1.1  christos 	} else {
   1352  1.15      matt 		if ((flags & PMAP_NOCACHE) == 0 && !PMAP_PAGE_COLOROK_P(pa, va))
   1353  1.15      matt 			PMAP_COUNT(kenter_pa_bad);
   1354   1.1  christos 	}
   1355   1.1  christos 
   1356  1.15      matt 	pt_entry_t npte = pte_make_kenter_pa(pa, mdpg, prot, flags);
   1357   1.1  christos 	kpreempt_disable();
   1358  1.15      matt 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1359  1.15      matt 	KASSERTMSG(ptep != NULL, "%#"PRIxVADDR " %#"PRIxVADDR, va,
   1360  1.15      matt 	    pmap_limits.virtual_end);
   1361   1.1  christos 	KASSERT(!pte_valid_p(*ptep));
   1362  1.15      matt 
   1363  1.15      matt 	/*
   1364  1.15      matt 	 * No need to track non-managed pages or PMAP_KMPAGEs pages for aliases
   1365  1.15      matt 	 */
   1366  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1367  1.20      matt 	if (pg != NULL && (flags & PMAP_KMPAGE) == 0
   1368  1.20      matt 	    && pmap_md_virtual_cache_aliasing_p()) {
   1369  1.15      matt 		pmap_enter_pv(pmap, va, pg, &npte, PV_KENTER);
   1370  1.15      matt 	}
   1371  1.15      matt #endif
   1372  1.15      matt 
   1373   1.1  christos 	/*
   1374   1.1  christos 	 * We have the option to force this mapping into the TLB but we
   1375   1.1  christos 	 * don't.  Instead let the next reference to the page do it.
   1376   1.1  christos 	 */
   1377  1.15      matt 	pmap_md_tlb_miss_lock_enter();
   1378  1.15      matt 	*ptep = npte;
   1379   1.1  christos 	pmap_tlb_update_addr(pmap_kernel(), va, npte, 0);
   1380  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
   1381   1.1  christos 	kpreempt_enable();
   1382   1.1  christos #if DEBUG > 1
   1383   1.1  christos 	for (u_int i = 0; i < PAGE_SIZE / sizeof(long); i++) {
   1384   1.1  christos 		if (((long *)va)[i] != ((long *)pa)[i])
   1385   1.1  christos 			panic("%s: contents (%lx) of va %#"PRIxVADDR
   1386   1.1  christos 			    " != contents (%lx) of pa %#"PRIxPADDR, __func__,
   1387   1.1  christos 			    ((long *)va)[i], va, ((long *)pa)[i], pa);
   1388   1.1  christos 	}
   1389   1.1  christos #endif
   1390  1.15      matt 
   1391  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done (ptep=%p)", ptep, 0, 0, 0);
   1392   1.1  christos }
   1393   1.1  christos 
   1394  1.15      matt /*
   1395  1.15      matt  *	Remove the given range of addresses from the kernel map.
   1396  1.15      matt  *
   1397  1.15      matt  *	It is assumed that the start and end are properly
   1398  1.15      matt  *	rounded to the page size.
   1399  1.15      matt  */
   1400  1.15      matt 
   1401   1.1  christos static bool
   1402   1.1  christos pmap_pte_kremove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
   1403   1.1  christos 	uintptr_t flags)
   1404   1.1  christos {
   1405  1.15      matt 	const pt_entry_t new_pte = pte_nv_entry(true);
   1406  1.15      matt 
   1407  1.15      matt 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1408  1.15      matt 	UVMHIST_LOG(pmaphist,
   1409  1.15      matt 	    "(pmap=%p, sva=%#"PRIxVADDR", eva=%#"PRIxVADDR", ptep=%p)",
   1410  1.15      matt 	    pmap, sva, eva, ptep);
   1411   1.1  christos 
   1412   1.1  christos 	KASSERT(kpreempt_disabled());
   1413   1.1  christos 
   1414   1.1  christos 	for (; sva < eva; sva += NBPG, ptep++) {
   1415  1.15      matt 		pt_entry_t pte = *ptep;
   1416  1.15      matt 		if (!pte_valid_p(pte))
   1417   1.1  christos 			continue;
   1418   1.1  christos 
   1419   1.1  christos 		PMAP_COUNT(kremove_pages);
   1420  1.21       mrg #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1421  1.15      matt 		struct vm_page * const pg = PHYS_TO_VM_PAGE(pte_to_paddr(pte));
   1422  1.20      matt 		if (pg != NULL && pmap_md_virtual_cache_aliasing_p()) {
   1423  1.15      matt 			pmap_remove_pv(pmap, sva, pg, !pte_readonly_p(pte));
   1424  1.15      matt 		}
   1425  1.20      matt #endif
   1426   1.1  christos 
   1427  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
   1428  1.15      matt 		*ptep = new_pte;
   1429  1.15      matt 		pmap_tlb_invalidate_addr(pmap, sva);
   1430  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
   1431   1.1  christos 	}
   1432   1.1  christos 
   1433  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1434  1.15      matt 
   1435   1.1  christos 	return false;
   1436   1.1  christos }
   1437   1.1  christos 
   1438   1.1  christos void
   1439   1.1  christos pmap_kremove(vaddr_t va, vsize_t len)
   1440   1.1  christos {
   1441   1.1  christos 	const vaddr_t sva = trunc_page(va);
   1442   1.1  christos 	const vaddr_t eva = round_page(va + len);
   1443   1.1  christos 
   1444   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1445  1.15      matt 	UVMHIST_LOG(pmaphist, "(va=%#"PRIxVADDR", len=%#"PRIxVSIZE")",
   1446  1.15      matt 	    va, len, 0, 0);
   1447   1.1  christos 
   1448   1.1  christos 	kpreempt_disable();
   1449   1.1  christos 	pmap_pte_process(pmap_kernel(), sva, eva, pmap_pte_kremove, 0);
   1450   1.1  christos 	kpreempt_enable();
   1451   1.1  christos 
   1452  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1453   1.1  christos }
   1454   1.1  christos 
   1455   1.1  christos void
   1456   1.1  christos pmap_remove_all(struct pmap *pmap)
   1457   1.1  christos {
   1458  1.15      matt 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1459  1.15      matt 	UVMHIST_LOG(pmaphist, "(pm=%p)", pmap, 0, 0, 0);
   1460  1.15      matt 
   1461   1.1  christos 	KASSERT(pmap != pmap_kernel());
   1462   1.1  christos 
   1463   1.1  christos 	kpreempt_disable();
   1464   1.1  christos 	/*
   1465   1.1  christos 	 * Free all of our ASIDs which means we can skip doing all the
   1466   1.1  christos 	 * tlb_invalidate_addrs().
   1467   1.1  christos 	 */
   1468  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
   1469  1.15      matt #ifdef MULTIPROCESSOR
   1470  1.15      matt 	// This should be the last CPU with this pmap onproc
   1471  1.15      matt 	KASSERT(!kcpuset_isotherset(pmap->pm_onproc, cpu_index(curcpu())));
   1472  1.15      matt 	if (kcpuset_isset(pmap->pm_onproc, cpu_index(curcpu())))
   1473  1.15      matt #endif
   1474  1.15      matt 		pmap_tlb_asid_deactivate(pmap);
   1475  1.15      matt #ifdef MULTIPROCESSOR
   1476  1.15      matt 	KASSERT(kcpuset_iszero(pmap->pm_onproc));
   1477  1.15      matt #endif
   1478   1.1  christos 	pmap_tlb_asid_release_all(pmap);
   1479  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
   1480   1.1  christos 	pmap->pm_flags |= PMAP_DEFERRED_ACTIVATE;
   1481   1.1  christos 
   1482  1.15      matt #ifdef PMAP_FAULTINFO
   1483  1.15      matt 	curpcb->pcb_faultinfo.pfi_faultaddr = 0;
   1484  1.15      matt 	curpcb->pcb_faultinfo.pfi_repeats = 0;
   1485  1.15      matt 	curpcb->pcb_faultinfo.pfi_faultpte = NULL;
   1486  1.15      matt #endif
   1487   1.1  christos 	kpreempt_enable();
   1488  1.15      matt 
   1489  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1490   1.1  christos }
   1491   1.1  christos 
   1492   1.1  christos /*
   1493   1.1  christos  *	Routine:	pmap_unwire
   1494   1.1  christos  *	Function:	Clear the wired attribute for a map/virtual-address
   1495   1.1  christos  *			pair.
   1496   1.1  christos  *	In/out conditions:
   1497   1.1  christos  *			The mapping must already exist in the pmap.
   1498   1.1  christos  */
   1499   1.1  christos void
   1500   1.1  christos pmap_unwire(pmap_t pmap, vaddr_t va)
   1501   1.1  christos {
   1502   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1503  1.15      matt 	UVMHIST_LOG(pmaphist, "(pmap=%p, va=%#"PRIxVADDR")", pmap, va, 0, 0);
   1504   1.1  christos 	PMAP_COUNT(unwire);
   1505   1.1  christos 
   1506   1.1  christos 	/*
   1507   1.1  christos 	 * Don't need to flush the TLB since PG_WIRED is only in software.
   1508   1.1  christos 	 */
   1509   1.1  christos 	kpreempt_disable();
   1510  1.15      matt 	pmap_addr_range_check(pmap, va, va, __func__);
   1511   1.1  christos 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1512  1.15      matt 	KASSERTMSG(ptep != NULL, "pmap %p va %#"PRIxVADDR" invalid STE",
   1513  1.15      matt 	    pmap, va);
   1514  1.15      matt 	pt_entry_t pte = *ptep;
   1515  1.15      matt 	KASSERTMSG(pte_valid_p(pte),
   1516  1.15      matt 	    "pmap %p va %#"PRIxVADDR" invalid PTE %#"PRIxPTE" @ %p",
   1517  1.15      matt 	    pmap, va, pte_value(pte), ptep);
   1518   1.1  christos 
   1519  1.15      matt 	if (pte_wired_p(pte)) {
   1520  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
   1521  1.15      matt 		*ptep = pte_unwire_entry(pte);
   1522  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
   1523   1.1  christos 		pmap->pm_stats.wired_count--;
   1524   1.1  christos 	}
   1525   1.1  christos #ifdef DIAGNOSTIC
   1526   1.1  christos 	else {
   1527   1.1  christos 		printf("%s: wiring for pmap %p va %#"PRIxVADDR" unchanged!\n",
   1528   1.1  christos 		    __func__, pmap, va);
   1529   1.1  christos 	}
   1530   1.1  christos #endif
   1531   1.1  christos 	kpreempt_enable();
   1532  1.15      matt 
   1533  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   1534   1.1  christos }
   1535   1.1  christos 
   1536   1.1  christos /*
   1537   1.1  christos  *	Routine:	pmap_extract
   1538   1.1  christos  *	Function:
   1539   1.1  christos  *		Extract the physical page address associated
   1540   1.1  christos  *		with the given map/virtual_address pair.
   1541   1.1  christos  */
   1542   1.1  christos bool
   1543   1.1  christos pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap)
   1544   1.1  christos {
   1545   1.1  christos 	paddr_t pa;
   1546   1.1  christos 
   1547   1.1  christos 	if (pmap == pmap_kernel()) {
   1548   1.1  christos 		if (pmap_md_direct_mapped_vaddr_p(va)) {
   1549   1.1  christos 			pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1550   1.1  christos 			goto done;
   1551   1.1  christos 		}
   1552   1.1  christos 		if (pmap_md_io_vaddr_p(va))
   1553   1.1  christos 			panic("pmap_extract: io address %#"PRIxVADDR"", va);
   1554  1.15      matt 
   1555  1.15      matt 		if (va >= pmap_limits.virtual_end)
   1556  1.15      matt 			panic("%s: illegal kernel mapped address %#"PRIxVADDR,
   1557  1.15      matt 			    __func__, va);
   1558   1.1  christos 	}
   1559   1.1  christos 	kpreempt_disable();
   1560  1.15      matt 	const pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1561  1.15      matt 	if (ptep == NULL || !pte_valid_p(*ptep)) {
   1562   1.1  christos 		kpreempt_enable();
   1563   1.1  christos 		return false;
   1564   1.1  christos 	}
   1565   1.1  christos 	pa = pte_to_paddr(*ptep) | (va & PGOFSET);
   1566   1.1  christos 	kpreempt_enable();
   1567   1.1  christos done:
   1568   1.1  christos 	if (pap != NULL) {
   1569   1.1  christos 		*pap = pa;
   1570   1.1  christos 	}
   1571   1.1  christos 	return true;
   1572   1.1  christos }
   1573   1.1  christos 
   1574   1.1  christos /*
   1575   1.1  christos  *	Copy the range specified by src_addr/len
   1576   1.1  christos  *	from the source map to the range dst_addr/len
   1577   1.1  christos  *	in the destination map.
   1578   1.1  christos  *
   1579   1.1  christos  *	This routine is only advisory and need not do anything.
   1580   1.1  christos  */
   1581   1.1  christos void
   1582   1.1  christos pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vaddr_t dst_addr, vsize_t len,
   1583   1.1  christos     vaddr_t src_addr)
   1584   1.1  christos {
   1585   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1586   1.1  christos 	PMAP_COUNT(copy);
   1587   1.1  christos }
   1588   1.1  christos 
   1589   1.1  christos /*
   1590   1.1  christos  *	pmap_clear_reference:
   1591   1.1  christos  *
   1592   1.1  christos  *	Clear the reference bit on the specified physical page.
   1593   1.1  christos  */
   1594   1.1  christos bool
   1595   1.1  christos pmap_clear_reference(struct vm_page *pg)
   1596   1.1  christos {
   1597   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1598   1.1  christos 
   1599   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1600   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR"))",
   1601   1.1  christos 	   pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1602   1.1  christos 
   1603   1.1  christos 	bool rv = pmap_page_clear_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1604   1.1  christos 
   1605  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- %s", rv ? "true" : "false", 0, 0, 0);
   1606   1.1  christos 
   1607   1.1  christos 	return rv;
   1608   1.1  christos }
   1609   1.1  christos 
   1610   1.1  christos /*
   1611   1.1  christos  *	pmap_is_referenced:
   1612   1.1  christos  *
   1613   1.1  christos  *	Return whether or not the specified physical page is referenced
   1614   1.1  christos  *	by any physical maps.
   1615   1.1  christos  */
   1616   1.1  christos bool
   1617   1.1  christos pmap_is_referenced(struct vm_page *pg)
   1618   1.1  christos {
   1619   1.1  christos 	return VM_PAGEMD_REFERENCED_P(VM_PAGE_TO_MD(pg));
   1620   1.1  christos }
   1621   1.1  christos 
   1622   1.1  christos /*
   1623   1.1  christos  *	Clear the modify bits on the specified physical page.
   1624   1.1  christos  */
   1625   1.1  christos bool
   1626   1.1  christos pmap_clear_modify(struct vm_page *pg)
   1627   1.1  christos {
   1628   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1629   1.1  christos 	pv_entry_t pv = &mdpg->mdpg_first;
   1630   1.1  christos 	pv_entry_t pv_next;
   1631   1.1  christos 
   1632   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1633   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (%#"PRIxPADDR"))",
   1634   1.1  christos 	    pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1635   1.1  christos 	PMAP_COUNT(clear_modify);
   1636   1.1  christos 
   1637   1.1  christos 	if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
   1638   1.1  christos 		if (pv->pv_pmap == NULL) {
   1639   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1640   1.1  christos 			    "pg %p (pa %#"PRIxPADDR"): %s",
   1641   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg), "execpage cleared", 0);
   1642   1.1  christos 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1643   1.1  christos 			PMAP_COUNT(exec_uncached_clear_modify);
   1644   1.1  christos 		} else {
   1645   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1646   1.1  christos 			    "pg %p (pa %#"PRIxPADDR"): %s",
   1647   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg), "syncicache performed", 0);
   1648   1.1  christos 			pmap_page_syncicache(pg);
   1649   1.1  christos 			PMAP_COUNT(exec_synced_clear_modify);
   1650   1.1  christos 		}
   1651   1.1  christos 	}
   1652   1.1  christos 	if (!pmap_page_clear_attributes(mdpg, VM_PAGEMD_MODIFIED)) {
   1653  1.15      matt 		UVMHIST_LOG(pmaphist, " <-- false", 0, 0, 0, 0);
   1654   1.1  christos 		return false;
   1655   1.1  christos 	}
   1656   1.1  christos 	if (pv->pv_pmap == NULL) {
   1657  1.15      matt 		UVMHIST_LOG(pmaphist, " <-- true (no mappings)", 0, 0, 0, 0);
   1658   1.1  christos 		return true;
   1659   1.1  christos 	}
   1660   1.1  christos 
   1661   1.1  christos 	/*
   1662   1.1  christos 	 * remove write access from any pages that are dirty
   1663   1.1  christos 	 * so we can tell if they are written to again later.
   1664   1.1  christos 	 * flush the VAC first if there is one.
   1665   1.1  christos 	 */
   1666   1.1  christos 	kpreempt_disable();
   1667  1.15      matt 	KASSERT(!VM_PAGEMD_PVLIST_LOCKED_P(mdpg));
   1668  1.15      matt 	VM_PAGEMD_PVLIST_READLOCK(mdpg);
   1669  1.15      matt 	pmap_pvlist_check(mdpg);
   1670   1.1  christos 	for (; pv != NULL; pv = pv_next) {
   1671   1.1  christos 		pmap_t pmap = pv->pv_pmap;
   1672  1.15      matt 		vaddr_t va = trunc_page(pv->pv_va);
   1673  1.15      matt 
   1674  1.15      matt 		pv_next = pv->pv_next;
   1675  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1676  1.15      matt 		if (pv->pv_va & PV_KENTER)
   1677  1.15      matt 			continue;
   1678  1.15      matt #endif
   1679   1.1  christos 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1680   1.1  christos 		KASSERT(ptep);
   1681  1.15      matt 		pt_entry_t pte = pte_prot_nowrite(*ptep);
   1682  1.15      matt 		if (*ptep == pte) {
   1683   1.1  christos 			continue;
   1684   1.1  christos 		}
   1685  1.15      matt 		KASSERT(pte_valid_p(pte));
   1686  1.15      matt 		const uintptr_t gen = VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1687  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
   1688  1.15      matt 		*ptep = pte;
   1689   1.1  christos 		pmap_tlb_invalidate_addr(pmap, va);
   1690  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
   1691   1.1  christos 		pmap_update(pmap);
   1692  1.15      matt 		if (__predict_false(gen != VM_PAGEMD_PVLIST_READLOCK(mdpg))) {
   1693   1.1  christos 			/*
   1694   1.1  christos 			 * The list changed!  So restart from the beginning.
   1695   1.1  christos 			 */
   1696   1.1  christos 			pv_next = &mdpg->mdpg_first;
   1697  1.15      matt 			pmap_pvlist_check(mdpg);
   1698   1.1  christos 		}
   1699   1.1  christos 	}
   1700  1.15      matt 	pmap_pvlist_check(mdpg);
   1701   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1702   1.1  christos 	kpreempt_enable();
   1703   1.1  christos 
   1704  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- true (mappings changed)", 0, 0, 0, 0);
   1705   1.1  christos 	return true;
   1706   1.1  christos }
   1707   1.1  christos 
   1708   1.1  christos /*
   1709   1.1  christos  *	pmap_is_modified:
   1710   1.1  christos  *
   1711   1.1  christos  *	Return whether or not the specified physical page is modified
   1712   1.1  christos  *	by any physical maps.
   1713   1.1  christos  */
   1714   1.1  christos bool
   1715   1.1  christos pmap_is_modified(struct vm_page *pg)
   1716   1.1  christos {
   1717   1.1  christos 	return VM_PAGEMD_MODIFIED_P(VM_PAGE_TO_MD(pg));
   1718   1.1  christos }
   1719   1.1  christos 
   1720   1.1  christos /*
   1721   1.1  christos  *	pmap_set_modified:
   1722   1.1  christos  *
   1723   1.1  christos  *	Sets the page modified reference bit for the specified page.
   1724   1.1  christos  */
   1725   1.1  christos void
   1726   1.1  christos pmap_set_modified(paddr_t pa)
   1727   1.1  christos {
   1728   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1729   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1730   1.1  christos 	pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1731   1.1  christos }
   1732   1.1  christos 
   1733   1.1  christos /******************** pv_entry management ********************/
   1734   1.1  christos 
   1735   1.1  christos static void
   1736  1.15      matt pmap_pvlist_check(struct vm_page_md *mdpg)
   1737   1.1  christos {
   1738  1.15      matt #ifdef DEBUG
   1739  1.15      matt 	pv_entry_t pv = &mdpg->mdpg_first;
   1740   1.1  christos 	if (pv->pv_pmap != NULL) {
   1741  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1742  1.15      matt 		const u_int colormask = uvmexp.colormask;
   1743  1.15      matt 		u_int colors = 0;
   1744  1.15      matt #endif
   1745   1.1  christos 		for (; pv != NULL; pv = pv->pv_next) {
   1746  1.15      matt 			KASSERT(pv->pv_pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(pv->pv_va));
   1747  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1748  1.15      matt 			colors |= __BIT(atop(pv->pv_va) & colormask);
   1749  1.15      matt #endif
   1750   1.1  christos 		}
   1751  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1752  1.15      matt 		// Assert there if there more than 1 color mapped, that they
   1753  1.15      matt 		// are uncached.
   1754  1.15      matt 		KASSERTMSG(!pmap_md_virtual_cache_aliasing_p()
   1755  1.15      matt 		    || colors == 0 || (colors & (colors-1)) == 0
   1756  1.15      matt 		    || VM_PAGEMD_UNCACHED_P(mdpg), "colors=%#x uncached=%u",
   1757  1.15      matt 		    colors, VM_PAGEMD_UNCACHED_P(mdpg));
   1758  1.15      matt #endif
   1759   1.1  christos 	}
   1760  1.15      matt #endif /* DEBUG */
   1761   1.1  christos }
   1762   1.1  christos 
   1763   1.1  christos /*
   1764   1.1  christos  * Enter the pmap and virtual address into the
   1765   1.1  christos  * physical to virtual map table.
   1766   1.1  christos  */
   1767   1.1  christos void
   1768  1.15      matt pmap_enter_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, pt_entry_t *nptep,
   1769  1.15      matt     u_int flags)
   1770   1.1  christos {
   1771   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1772   1.1  christos 	pv_entry_t pv, npv, apv;
   1773  1.15      matt #ifdef UVMHIST
   1774  1.15      matt 	bool first = false;
   1775  1.15      matt #endif
   1776   1.1  christos 
   1777   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1778   1.1  christos 	UVMHIST_LOG(pmaphist,
   1779   1.1  christos 	    "(pmap=%p va=%#"PRIxVADDR" pg=%p (%#"PRIxPADDR")",
   1780   1.1  christos 	    pmap, va, pg, VM_PAGE_TO_PHYS(pg));
   1781  1.15      matt 	UVMHIST_LOG(pmaphist, "nptep=%p (%#"PRIxPTE"))",
   1782  1.15      matt 	    nptep, pte_value(*nptep), 0, 0);
   1783   1.1  christos 
   1784   1.1  christos 	KASSERT(kpreempt_disabled());
   1785   1.1  christos 	KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
   1786  1.15      matt 	KASSERTMSG(pmap != pmap_kernel() || !pmap_md_io_vaddr_p(va),
   1787  1.15      matt 	    "va %#"PRIxVADDR, va);
   1788   1.1  christos 
   1789   1.1  christos 	apv = NULL;
   1790  1.15      matt 	VM_PAGEMD_PVLIST_LOCK(mdpg);
   1791  1.15      matt again:
   1792   1.1  christos 	pv = &mdpg->mdpg_first;
   1793  1.15      matt 	pmap_pvlist_check(mdpg);
   1794   1.1  christos 	if (pv->pv_pmap == NULL) {
   1795   1.1  christos 		KASSERT(pv->pv_next == NULL);
   1796   1.1  christos 		/*
   1797   1.1  christos 		 * No entries yet, use header as the first entry
   1798   1.1  christos 		 */
   1799   1.1  christos 		PMAP_COUNT(primary_mappings);
   1800   1.1  christos 		PMAP_COUNT(mappings);
   1801  1.15      matt #ifdef UVMHIST
   1802   1.1  christos 		first = true;
   1803  1.15      matt #endif
   1804  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1805  1.15      matt 		KASSERT(VM_PAGEMD_CACHED_P(mdpg));
   1806  1.15      matt 		// If the new mapping has an incompatible color the last
   1807  1.15      matt 		// mapping of this page, clean the page before using it.
   1808  1.15      matt 		if (!PMAP_PAGE_COLOROK_P(va, pv->pv_va)) {
   1809  1.15      matt 			pmap_md_vca_clean(pg, PMAP_WBINV);
   1810  1.15      matt 		}
   1811   1.1  christos #endif
   1812   1.1  christos 		pv->pv_pmap = pmap;
   1813  1.15      matt 		pv->pv_va = va | flags;
   1814   1.1  christos 	} else {
   1815  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1816  1.15      matt 		if (pmap_md_vca_add(pg, va, nptep)) {
   1817   1.1  christos 			goto again;
   1818  1.15      matt 		}
   1819  1.15      matt #endif
   1820   1.1  christos 
   1821   1.1  christos 		/*
   1822   1.1  christos 		 * There is at least one other VA mapping this page.
   1823   1.1  christos 		 * Place this entry after the header.
   1824   1.1  christos 		 *
   1825   1.1  christos 		 * Note: the entry may already be in the table if
   1826   1.1  christos 		 * we are only changing the protection bits.
   1827   1.1  christos 		 */
   1828   1.1  christos 
   1829   1.1  christos #ifdef PARANOIADIAG
   1830   1.1  christos 		const paddr_t pa = VM_PAGE_TO_PHYS(pg);
   1831   1.1  christos #endif
   1832   1.1  christos 		for (npv = pv; npv; npv = npv->pv_next) {
   1833  1.15      matt 			if (pmap == npv->pv_pmap
   1834  1.15      matt 			    && va == trunc_page(npv->pv_va)) {
   1835   1.1  christos #ifdef PARANOIADIAG
   1836   1.1  christos 				pt_entry_t *ptep = pmap_pte_lookup(pmap, va);
   1837  1.15      matt 				pt_entry_t pte = (ptep != NULL) ? *ptep : 0;
   1838  1.15      matt 				if (!pte_valid_p(pte) || pte_to_paddr(pte) != pa)
   1839  1.15      matt 					printf("%s: found va %#"PRIxVADDR
   1840  1.15      matt 					    " pa %#"PRIxPADDR
   1841  1.15      matt 					    " in pv_table but != %#"PRIxPTE"\n",
   1842  1.15      matt 					    __func__, va, pa, pte_value(pte));
   1843   1.1  christos #endif
   1844   1.1  christos 				PMAP_COUNT(remappings);
   1845   1.1  christos 				VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1846   1.1  christos 				if (__predict_false(apv != NULL))
   1847   1.1  christos 					pmap_pv_free(apv);
   1848  1.15      matt 
   1849  1.15      matt 				UVMHIST_LOG(pmaphist, " <-- done pv=%p%s",
   1850  1.15      matt 				    pv, " (reused)", 0, 0);
   1851   1.1  christos 				return;
   1852   1.1  christos 			}
   1853   1.1  christos 		}
   1854   1.1  christos 		if (__predict_true(apv == NULL)) {
   1855   1.1  christos 			/*
   1856   1.1  christos 			 * To allocate a PV, we have to release the PVLIST lock
   1857   1.1  christos 			 * so get the page generation.  We allocate the PV, and
   1858  1.15      matt 			 * then reacquire the lock.
   1859   1.1  christos 			 */
   1860  1.15      matt 			pmap_pvlist_check(mdpg);
   1861  1.15      matt 			const uintptr_t gen = VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1862   1.1  christos 
   1863   1.1  christos 			apv = (pv_entry_t)pmap_pv_alloc();
   1864   1.1  christos 			if (apv == NULL)
   1865   1.1  christos 				panic("pmap_enter_pv: pmap_pv_alloc() failed");
   1866   1.1  christos 
   1867   1.1  christos 			/*
   1868   1.1  christos 			 * If the generation has changed, then someone else
   1869  1.15      matt 			 * tinkered with this page so we should start over.
   1870   1.1  christos 			 */
   1871  1.15      matt 			if (gen != VM_PAGEMD_PVLIST_LOCK(mdpg))
   1872   1.1  christos 				goto again;
   1873   1.1  christos 		}
   1874   1.1  christos 		npv = apv;
   1875   1.1  christos 		apv = NULL;
   1876  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1877  1.15      matt 		/*
   1878  1.15      matt 		 * If need to deal with virtual cache aliases, keep mappings
   1879  1.15      matt 		 * in the kernel pmap at the head of the list.  This allows
   1880  1.15      matt 		 * the VCA code to easily use them for cache operations if
   1881  1.15      matt 		 * present.
   1882  1.15      matt 		 */
   1883  1.15      matt 		pmap_t kpmap = pmap_kernel();
   1884  1.15      matt 		if (pmap != kpmap) {
   1885  1.15      matt 			while (pv->pv_pmap == kpmap && pv->pv_next != NULL) {
   1886  1.15      matt 				pv = pv->pv_next;
   1887  1.15      matt 			}
   1888  1.15      matt 		}
   1889  1.15      matt #endif
   1890  1.15      matt 		npv->pv_va = va | flags;
   1891   1.1  christos 		npv->pv_pmap = pmap;
   1892   1.1  christos 		npv->pv_next = pv->pv_next;
   1893   1.1  christos 		pv->pv_next = npv;
   1894   1.1  christos 		PMAP_COUNT(mappings);
   1895   1.1  christos 	}
   1896  1.15      matt 	pmap_pvlist_check(mdpg);
   1897   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1898   1.1  christos 	if (__predict_false(apv != NULL))
   1899   1.1  christos 		pmap_pv_free(apv);
   1900   1.1  christos 
   1901  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done pv=%p%s",
   1902   1.1  christos 	    pv, first ? " (first pv)" : "",0,0);
   1903   1.1  christos }
   1904   1.1  christos 
   1905   1.1  christos /*
   1906   1.1  christos  * Remove a physical to virtual address translation.
   1907   1.1  christos  * If cache was inhibited on this page, and there are no more cache
   1908   1.1  christos  * conflicts, restore caching.
   1909   1.1  christos  * Flush the cache if the last page is removed (should always be cached
   1910   1.1  christos  * at this point).
   1911   1.1  christos  */
   1912   1.1  christos void
   1913   1.1  christos pmap_remove_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, bool dirty)
   1914   1.1  christos {
   1915   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1916   1.1  christos 	pv_entry_t pv, npv;
   1917   1.1  christos 	bool last;
   1918   1.1  christos 
   1919   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1920   1.1  christos 	UVMHIST_LOG(pmaphist,
   1921  1.15      matt 	    "(pmap=%p, va=%#"PRIxVADDR", pg=%p (pa %#"PRIxPADDR")",
   1922   1.1  christos 	    pmap, va, pg, VM_PAGE_TO_PHYS(pg));
   1923  1.15      matt 	UVMHIST_LOG(pmaphist, "dirty=%s)", dirty ? "true" : "false", 0, 0, 0);
   1924   1.1  christos 
   1925   1.1  christos 	KASSERT(kpreempt_disabled());
   1926  1.15      matt 	KASSERT((va & PAGE_MASK) == 0);
   1927   1.1  christos 	pv = &mdpg->mdpg_first;
   1928   1.1  christos 
   1929  1.15      matt 	VM_PAGEMD_PVLIST_LOCK(mdpg);
   1930  1.15      matt 	pmap_pvlist_check(mdpg);
   1931   1.1  christos 
   1932   1.1  christos 	/*
   1933   1.1  christos 	 * If it is the first entry on the list, it is actually
   1934   1.1  christos 	 * in the header and we must copy the following entry up
   1935   1.1  christos 	 * to the header.  Otherwise we must search the list for
   1936   1.1  christos 	 * the entry.  In either case we free the now unused entry.
   1937   1.1  christos 	 */
   1938   1.1  christos 
   1939   1.1  christos 	last = false;
   1940  1.15      matt 	if (pmap == pv->pv_pmap && va == trunc_page(pv->pv_va)) {
   1941   1.1  christos 		npv = pv->pv_next;
   1942   1.1  christos 		if (npv) {
   1943   1.1  christos 			*pv = *npv;
   1944   1.1  christos 			KASSERT(pv->pv_pmap != NULL);
   1945   1.1  christos 		} else {
   1946  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1947  1.15      matt 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_UNCACHED);
   1948   1.1  christos #endif
   1949   1.1  christos 			pv->pv_pmap = NULL;
   1950   1.1  christos 			last = true;	/* Last mapping removed */
   1951   1.1  christos 		}
   1952   1.1  christos 		PMAP_COUNT(remove_pvfirst);
   1953   1.1  christos 	} else {
   1954   1.1  christos 		for (npv = pv->pv_next; npv; pv = npv, npv = npv->pv_next) {
   1955   1.1  christos 			PMAP_COUNT(remove_pvsearch);
   1956  1.15      matt 			if (pmap == npv->pv_pmap && va == trunc_page(npv->pv_va))
   1957   1.1  christos 				break;
   1958   1.1  christos 		}
   1959   1.1  christos 		if (npv) {
   1960   1.1  christos 			pv->pv_next = npv->pv_next;
   1961   1.1  christos 		}
   1962   1.1  christos 	}
   1963   1.1  christos 
   1964  1.15      matt 	pmap_pvlist_check(mdpg);
   1965   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1966   1.1  christos 
   1967  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   1968  1.15      matt 	pmap_md_vca_remove(pg, va, dirty, last);
   1969  1.15      matt #endif
   1970  1.15      matt 
   1971   1.1  christos 	/*
   1972   1.1  christos 	 * Free the pv_entry if needed.
   1973   1.1  christos 	 */
   1974   1.1  christos 	if (npv)
   1975   1.1  christos 		pmap_pv_free(npv);
   1976   1.1  christos 	if (VM_PAGEMD_EXECPAGE_P(mdpg) && dirty) {
   1977   1.1  christos 		if (last) {
   1978   1.1  christos 			/*
   1979   1.1  christos 			 * If this was the page's last mapping, we no longer
   1980   1.1  christos 			 * care about its execness.
   1981   1.1  christos 			 */
   1982   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1983   1.1  christos 			    "pg %p (pa %#"PRIxPADDR")%s: %s",
   1984   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg),
   1985   1.1  christos 			    last ? " [last mapping]" : "",
   1986   1.1  christos 			    "execpage cleared");
   1987   1.1  christos 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1988   1.1  christos 			PMAP_COUNT(exec_uncached_remove);
   1989   1.1  christos 		} else {
   1990   1.1  christos 			/*
   1991   1.1  christos 			 * Someone still has it mapped as an executable page
   1992   1.1  christos 			 * so we must sync it.
   1993   1.1  christos 			 */
   1994   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1995   1.1  christos 			    "pg %p (pa %#"PRIxPADDR")%s: %s",
   1996   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg),
   1997   1.1  christos 			    last ? " [last mapping]" : "",
   1998   1.1  christos 			    "performed syncicache");
   1999   1.1  christos 			pmap_page_syncicache(pg);
   2000   1.1  christos 			PMAP_COUNT(exec_synced_remove);
   2001   1.1  christos 		}
   2002   1.1  christos 	}
   2003  1.15      matt 
   2004  1.15      matt 	UVMHIST_LOG(pmaphist, " <-- done", 0, 0, 0, 0);
   2005   1.1  christos }
   2006   1.1  christos 
   2007   1.1  christos #if defined(MULTIPROCESSOR)
   2008   1.1  christos struct pmap_pvlist_info {
   2009   1.1  christos 	kmutex_t *pli_locks[PAGE_SIZE / 32];
   2010   1.1  christos 	volatile u_int pli_lock_refs[PAGE_SIZE / 32];
   2011   1.1  christos 	volatile u_int pli_lock_index;
   2012   1.1  christos 	u_int pli_lock_mask;
   2013   1.1  christos } pmap_pvlist_info;
   2014   1.1  christos 
   2015   1.1  christos void
   2016   1.1  christos pmap_pvlist_lock_init(size_t cache_line_size)
   2017   1.1  christos {
   2018   1.1  christos 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   2019   1.1  christos 	const vaddr_t lock_page = uvm_pageboot_alloc(PAGE_SIZE);
   2020   1.1  christos 	vaddr_t lock_va = lock_page;
   2021   1.1  christos 	if (sizeof(kmutex_t) > cache_line_size) {
   2022   1.1  christos 		cache_line_size = roundup2(sizeof(kmutex_t), cache_line_size);
   2023   1.1  christos 	}
   2024   1.1  christos 	const size_t nlocks = PAGE_SIZE / cache_line_size;
   2025   1.1  christos 	KASSERT((nlocks & (nlocks - 1)) == 0);
   2026   1.1  christos 	/*
   2027   1.1  christos 	 * Now divide the page into a number of mutexes, one per cacheline.
   2028   1.1  christos 	 */
   2029   1.1  christos 	for (size_t i = 0; i < nlocks; lock_va += cache_line_size, i++) {
   2030   1.1  christos 		kmutex_t * const lock = (kmutex_t *)lock_va;
   2031  1.15      matt 		mutex_init(lock, MUTEX_DEFAULT, IPL_HIGH);
   2032   1.1  christos 		pli->pli_locks[i] = lock;
   2033   1.1  christos 	}
   2034   1.1  christos 	pli->pli_lock_mask = nlocks - 1;
   2035   1.1  christos }
   2036   1.1  christos 
   2037  1.15      matt kmutex_t *
   2038  1.15      matt pmap_pvlist_lock_addr(struct vm_page_md *mdpg)
   2039   1.1  christos {
   2040   1.1  christos 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   2041   1.1  christos 	kmutex_t *lock = mdpg->mdpg_lock;
   2042   1.1  christos 
   2043   1.1  christos 	/*
   2044   1.1  christos 	 * Allocate a lock on an as-needed basis.  This will hopefully give us
   2045   1.1  christos 	 * semi-random distribution not based on page color.
   2046   1.1  christos 	 */
   2047   1.1  christos 	if (__predict_false(lock == NULL)) {
   2048   1.1  christos 		size_t locknum = atomic_add_int_nv(&pli->pli_lock_index, 37);
   2049   1.1  christos 		size_t lockid = locknum & pli->pli_lock_mask;
   2050   1.1  christos 		kmutex_t * const new_lock = pli->pli_locks[lockid];
   2051   1.1  christos 		/*
   2052   1.1  christos 		 * Set the lock.  If some other thread already did, just use
   2053   1.1  christos 		 * the one they assigned.
   2054   1.1  christos 		 */
   2055   1.1  christos 		lock = atomic_cas_ptr(&mdpg->mdpg_lock, NULL, new_lock);
   2056   1.1  christos 		if (lock == NULL) {
   2057   1.1  christos 			lock = new_lock;
   2058   1.1  christos 			atomic_inc_uint(&pli->pli_lock_refs[lockid]);
   2059   1.1  christos 		}
   2060   1.1  christos 	}
   2061   1.1  christos 
   2062   1.1  christos 	/*
   2063  1.15      matt 	 * Now finally provide the lock.
   2064   1.1  christos 	 */
   2065  1.15      matt 	return lock;
   2066   1.1  christos }
   2067   1.1  christos #else /* !MULTIPROCESSOR */
   2068   1.1  christos void
   2069   1.1  christos pmap_pvlist_lock_init(size_t cache_line_size)
   2070   1.1  christos {
   2071  1.15      matt 	mutex_init(&pmap_pvlist_mutex, MUTEX_DEFAULT, IPL_HIGH);
   2072   1.1  christos }
   2073   1.1  christos 
   2074   1.1  christos #ifdef MODULAR
   2075  1.15      matt kmutex_t *
   2076  1.15      matt pmap_pvlist_lock_addr(struct vm_page_md *mdpg)
   2077   1.1  christos {
   2078   1.1  christos 	/*
   2079   1.1  christos 	 * We just use a global lock.
   2080   1.1  christos 	 */
   2081   1.1  christos 	if (__predict_false(mdpg->mdpg_lock == NULL)) {
   2082   1.1  christos 		mdpg->mdpg_lock = &pmap_pvlist_mutex;
   2083   1.1  christos 	}
   2084   1.1  christos 
   2085   1.1  christos 	/*
   2086  1.15      matt 	 * Now finally provide the lock.
   2087   1.1  christos 	 */
   2088  1.15      matt 	return mdpg->mdpg_lock;
   2089   1.1  christos }
   2090   1.1  christos #endif /* MODULAR */
   2091   1.1  christos #endif /* !MULTIPROCESSOR */
   2092   1.1  christos 
   2093   1.1  christos /*
   2094   1.1  christos  * pmap_pv_page_alloc:
   2095   1.1  christos  *
   2096   1.1  christos  *	Allocate a page for the pv_entry pool.
   2097   1.1  christos  */
   2098   1.1  christos void *
   2099   1.1  christos pmap_pv_page_alloc(struct pool *pp, int flags)
   2100   1.1  christos {
   2101  1.15      matt 	struct vm_page * const pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_USERESERVE);
   2102   1.1  christos 	if (pg == NULL)
   2103   1.1  christos 		return NULL;
   2104   1.1  christos 
   2105   1.1  christos 	return (void *)pmap_map_poolpage(VM_PAGE_TO_PHYS(pg));
   2106   1.1  christos }
   2107   1.1  christos 
   2108   1.1  christos /*
   2109   1.1  christos  * pmap_pv_page_free:
   2110   1.1  christos  *
   2111   1.1  christos  *	Free a pv_entry pool page.
   2112   1.1  christos  */
   2113   1.1  christos void
   2114   1.1  christos pmap_pv_page_free(struct pool *pp, void *v)
   2115   1.1  christos {
   2116   1.1  christos 	vaddr_t va = (vaddr_t)v;
   2117   1.1  christos 
   2118   1.1  christos 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   2119   1.1  christos 	const paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   2120   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   2121  1.15      matt 	KASSERT(pg != NULL);
   2122  1.15      matt #ifdef PMAP_VIRTUAL_CACHE_ALIASES
   2123  1.15      matt 	kpreempt_disable();
   2124  1.15      matt 	pmap_md_vca_remove(pg, va, true, true);
   2125  1.15      matt 	kpreempt_enable();
   2126  1.15      matt #endif
   2127  1.15      matt 	pmap_page_clear_attributes(VM_PAGE_TO_MD(pg), VM_PAGEMD_POOLPAGE);
   2128   1.1  christos 	uvm_pagefree(pg);
   2129   1.1  christos }
   2130   1.1  christos 
   2131   1.1  christos #ifdef PMAP_PREFER
   2132   1.1  christos /*
   2133   1.1  christos  * Find first virtual address >= *vap that doesn't cause
   2134   1.1  christos  * a cache alias conflict.
   2135   1.1  christos  */
   2136   1.1  christos void
   2137   1.1  christos pmap_prefer(vaddr_t foff, vaddr_t *vap, vsize_t sz, int td)
   2138   1.1  christos {
   2139   1.1  christos 	vsize_t prefer_mask = ptoa(uvmexp.colormask);
   2140   1.1  christos 
   2141   1.1  christos 	PMAP_COUNT(prefer_requests);
   2142   1.1  christos 
   2143   1.1  christos 	prefer_mask |= pmap_md_cache_prefer_mask();
   2144   1.1  christos 
   2145   1.1  christos 	if (prefer_mask) {
   2146  1.15      matt 		vaddr_t	va = *vap;
   2147  1.15      matt 		vsize_t d = (foff - va) & prefer_mask;
   2148   1.1  christos 		if (d) {
   2149   1.1  christos 			if (td)
   2150  1.15      matt 				*vap = trunc_page(va - ((-d) & prefer_mask));
   2151   1.1  christos 			else
   2152   1.1  christos 				*vap = round_page(va + d);
   2153   1.1  christos 			PMAP_COUNT(prefer_adjustments);
   2154   1.1  christos 		}
   2155   1.1  christos 	}
   2156   1.1  christos }
   2157   1.1  christos #endif /* PMAP_PREFER */
   2158   1.1  christos 
   2159   1.1  christos #ifdef PMAP_MAP_POOLPAGE
   2160   1.1  christos vaddr_t
   2161   1.1  christos pmap_map_poolpage(paddr_t pa)
   2162   1.1  christos {
   2163   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   2164   1.1  christos 	KASSERT(pg);
   2165   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   2166   1.1  christos 	pmap_page_set_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   2167   1.1  christos 
   2168  1.15      matt 	return pmap_md_map_poolpage(pa, NBPG);
   2169   1.1  christos }
   2170   1.1  christos 
   2171   1.1  christos paddr_t
   2172   1.1  christos pmap_unmap_poolpage(vaddr_t va)
   2173   1.1  christos {
   2174   1.1  christos 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   2175   1.1  christos 	paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   2176   1.1  christos 
   2177   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   2178  1.15      matt 	KASSERT(pg != NULL);
   2179  1.15      matt 	pmap_page_clear_attributes(VM_PAGE_TO_MD(pg), VM_PAGEMD_POOLPAGE);
   2180   1.1  christos 	pmap_md_unmap_poolpage(va, NBPG);
   2181   1.1  christos 
   2182   1.1  christos 	return pa;
   2183   1.1  christos }
   2184   1.1  christos #endif /* PMAP_MAP_POOLPAGE */
   2185