Home | History | Annotate | Line # | Download | only in pmap
pmap.c revision 1.10
      1  1.10    nonaka /*	$NetBSD: pmap.c,v 1.10 2015/01/26 04:47:53 nonaka 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.10    nonaka __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.10 2015/01/26 04:47:53 nonaka 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.1  christos #include <sys/systm.h>
    106   1.1  christos #include <sys/proc.h>
    107   1.1  christos #include <sys/buf.h>
    108   1.1  christos #include <sys/pool.h>
    109   1.1  christos #include <sys/atomic.h>
    110   1.1  christos #include <sys/mutex.h>
    111   1.1  christos #include <sys/atomic.h>
    112   1.1  christos #ifdef SYSVSHM
    113   1.1  christos #include <sys/shm.h>
    114   1.1  christos #endif
    115   1.1  christos #include <sys/socketvar.h>	/* XXX: for sock_loan_thresh */
    116   1.1  christos 
    117   1.1  christos #include <uvm/uvm.h>
    118   1.1  christos 
    119   1.1  christos #define	PMAP_COUNT(name)	(pmap_evcnt_##name.ev_count++ + 0)
    120   1.1  christos #define PMAP_COUNTER(name, desc) \
    121   1.1  christos static struct evcnt pmap_evcnt_##name = \
    122   1.1  christos 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", desc); \
    123   1.1  christos EVCNT_ATTACH_STATIC(pmap_evcnt_##name)
    124   1.1  christos 
    125   1.1  christos PMAP_COUNTER(remove_kernel_calls, "remove kernel calls");
    126   1.1  christos PMAP_COUNTER(remove_kernel_pages, "kernel pages unmapped");
    127   1.1  christos PMAP_COUNTER(remove_user_calls, "remove user calls");
    128   1.1  christos PMAP_COUNTER(remove_user_pages, "user pages unmapped");
    129   1.1  christos PMAP_COUNTER(remove_flushes, "remove cache flushes");
    130   1.1  christos PMAP_COUNTER(remove_tlb_ops, "remove tlb ops");
    131   1.1  christos PMAP_COUNTER(remove_pvfirst, "remove pv first");
    132   1.1  christos PMAP_COUNTER(remove_pvsearch, "remove pv search");
    133   1.1  christos 
    134   1.1  christos PMAP_COUNTER(prefer_requests, "prefer requests");
    135   1.1  christos PMAP_COUNTER(prefer_adjustments, "prefer adjustments");
    136   1.1  christos 
    137   1.1  christos PMAP_COUNTER(idlezeroed_pages, "pages idle zeroed");
    138   1.1  christos PMAP_COUNTER(zeroed_pages, "pages zeroed");
    139   1.1  christos PMAP_COUNTER(copied_pages, "pages copied");
    140   1.1  christos 
    141   1.1  christos PMAP_COUNTER(kenter_pa, "kernel fast mapped pages");
    142   1.1  christos PMAP_COUNTER(kenter_pa_bad, "kernel fast mapped pages (bad color)");
    143   1.1  christos PMAP_COUNTER(kenter_pa_unmanaged, "kernel fast mapped unmanaged pages");
    144   1.1  christos PMAP_COUNTER(kremove_pages, "kernel fast unmapped pages");
    145   1.1  christos 
    146   1.1  christos PMAP_COUNTER(page_cache_evictions, "pages changed to uncacheable");
    147   1.1  christos PMAP_COUNTER(page_cache_restorations, "pages changed to cacheable");
    148   1.1  christos 
    149   1.1  christos PMAP_COUNTER(kernel_mappings_bad, "kernel pages mapped (bad color)");
    150   1.1  christos PMAP_COUNTER(user_mappings_bad, "user pages mapped (bad color)");
    151   1.1  christos PMAP_COUNTER(kernel_mappings, "kernel pages mapped");
    152   1.1  christos PMAP_COUNTER(user_mappings, "user pages mapped");
    153   1.1  christos PMAP_COUNTER(user_mappings_changed, "user mapping changed");
    154   1.1  christos PMAP_COUNTER(kernel_mappings_changed, "kernel mapping changed");
    155   1.1  christos PMAP_COUNTER(uncached_mappings, "uncached pages mapped");
    156   1.1  christos PMAP_COUNTER(unmanaged_mappings, "unmanaged pages mapped");
    157   1.1  christos PMAP_COUNTER(managed_mappings, "managed pages mapped");
    158   1.1  christos PMAP_COUNTER(mappings, "pages mapped");
    159   1.1  christos PMAP_COUNTER(remappings, "pages remapped");
    160   1.1  christos PMAP_COUNTER(unmappings, "pages unmapped");
    161   1.1  christos PMAP_COUNTER(primary_mappings, "page initial mappings");
    162   1.1  christos PMAP_COUNTER(primary_unmappings, "page final unmappings");
    163   1.1  christos PMAP_COUNTER(tlb_hit, "page mapping");
    164   1.1  christos 
    165   1.1  christos PMAP_COUNTER(exec_mappings, "exec pages mapped");
    166   1.1  christos PMAP_COUNTER(exec_synced_mappings, "exec pages synced");
    167   1.1  christos PMAP_COUNTER(exec_synced_remove, "exec pages synced (PR)");
    168   1.1  christos PMAP_COUNTER(exec_synced_clear_modify, "exec pages synced (CM)");
    169   1.1  christos PMAP_COUNTER(exec_synced_page_protect, "exec pages synced (PP)");
    170   1.1  christos PMAP_COUNTER(exec_synced_protect, "exec pages synced (P)");
    171   1.1  christos PMAP_COUNTER(exec_uncached_page_protect, "exec pages uncached (PP)");
    172   1.1  christos PMAP_COUNTER(exec_uncached_clear_modify, "exec pages uncached (CM)");
    173   1.1  christos PMAP_COUNTER(exec_uncached_zero_page, "exec pages uncached (ZP)");
    174   1.1  christos PMAP_COUNTER(exec_uncached_copy_page, "exec pages uncached (CP)");
    175   1.1  christos PMAP_COUNTER(exec_uncached_remove, "exec pages uncached (PR)");
    176   1.1  christos 
    177   1.1  christos PMAP_COUNTER(create, "creates");
    178   1.1  christos PMAP_COUNTER(reference, "references");
    179   1.1  christos PMAP_COUNTER(dereference, "dereferences");
    180   1.1  christos PMAP_COUNTER(destroy, "destroyed");
    181   1.1  christos PMAP_COUNTER(activate, "activations");
    182   1.1  christos PMAP_COUNTER(deactivate, "deactivations");
    183   1.1  christos PMAP_COUNTER(update, "updates");
    184   1.1  christos #ifdef MULTIPROCESSOR
    185   1.1  christos PMAP_COUNTER(shootdown_ipis, "shootdown IPIs");
    186   1.1  christos #endif
    187   1.1  christos PMAP_COUNTER(unwire, "unwires");
    188   1.1  christos PMAP_COUNTER(copy, "copies");
    189   1.1  christos PMAP_COUNTER(clear_modify, "clear_modifies");
    190   1.1  christos PMAP_COUNTER(protect, "protects");
    191   1.1  christos PMAP_COUNTER(page_protect, "page_protects");
    192   1.1  christos 
    193   1.1  christos #define PMAP_ASID_RESERVED 0
    194   1.1  christos CTASSERT(PMAP_ASID_RESERVED == 0);
    195   1.1  christos 
    196   1.1  christos /*
    197   1.1  christos  * Initialize the kernel pmap.
    198   1.1  christos  */
    199   1.1  christos #ifdef MULTIPROCESSOR
    200   1.9    nonaka #define	PMAP_SIZE	offsetof(struct pmap, pm_pai[PMAP_TLB_MAX])
    201   1.1  christos #else
    202   1.1  christos #define	PMAP_SIZE	sizeof(struct pmap)
    203   1.1  christos kmutex_t pmap_pvlist_mutex __aligned(COHERENCY_UNIT);
    204   1.1  christos #endif
    205   1.1  christos 
    206   1.1  christos struct pmap_kernel kernel_pmap_store = {
    207   1.1  christos 	.kernel_pmap = {
    208   1.1  christos 		.pm_count = 1,
    209   1.1  christos 		.pm_segtab = PMAP_INVALID_SEGTAB_ADDRESS,
    210   1.1  christos 		.pm_minaddr = VM_MIN_KERNEL_ADDRESS,
    211   1.1  christos 		.pm_maxaddr = VM_MAX_KERNEL_ADDRESS,
    212   1.1  christos 	},
    213   1.1  christos };
    214   1.1  christos 
    215   1.1  christos struct pmap * const kernel_pmap_ptr = &kernel_pmap_store.kernel_pmap;
    216   1.1  christos 
    217   1.1  christos struct pmap_limits pmap_limits;
    218   1.1  christos 
    219   1.1  christos #ifdef UVMHIST
    220   1.1  christos static struct kern_history_ent pmapexechistbuf[10000];
    221   1.1  christos static struct kern_history_ent pmaphistbuf[10000];
    222   1.8    nonaka UVMHIST_DEFINE(pmapexechist);
    223   1.8    nonaka UVMHIST_DEFINE(pmaphist);
    224   1.1  christos #endif
    225   1.1  christos 
    226   1.1  christos /*
    227   1.1  christos  * The pools from which pmap structures and sub-structures are allocated.
    228   1.1  christos  */
    229   1.1  christos struct pool pmap_pmap_pool;
    230   1.1  christos struct pool pmap_pv_pool;
    231   1.1  christos 
    232   1.1  christos #ifndef PMAP_PV_LOWAT
    233   1.1  christos #define	PMAP_PV_LOWAT	16
    234   1.1  christos #endif
    235   1.1  christos int		pmap_pv_lowat = PMAP_PV_LOWAT;
    236   1.1  christos 
    237   1.1  christos bool		pmap_initialized = false;
    238   1.1  christos #define	PMAP_PAGE_COLOROK_P(a, b) \
    239   1.1  christos 		((((int)(a) ^ (int)(b)) & pmap_page_colormask) == 0)
    240   1.1  christos u_int		pmap_page_colormask;
    241   1.1  christos 
    242   1.1  christos #define PAGE_IS_MANAGED(pa)	\
    243   1.1  christos 	(pmap_initialized == true && vm_physseg_find(atop(pa), NULL) != -1)
    244   1.1  christos 
    245   1.1  christos #define PMAP_IS_ACTIVE(pm)						\
    246   1.1  christos 	((pm) == pmap_kernel() || 					\
    247   1.1  christos 	 (pm) == curlwp->l_proc->p_vmspace->vm_map.pmap)
    248   1.1  christos 
    249   1.1  christos /* Forward function declarations */
    250   1.1  christos void pmap_remove_pv(pmap_t, vaddr_t, struct vm_page *, bool);
    251   1.1  christos void pmap_enter_pv(pmap_t, vaddr_t, struct vm_page *, 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.10    nonaka #endif	/* !MULTIPROCESSOR || !PMAP_MD_NEED_TLB_MISS_LOCK */
    270  1.10    nonaka 
    271   1.1  christos /*
    272   1.1  christos  * Misc. functions.
    273   1.1  christos  */
    274   1.1  christos 
    275   1.1  christos bool
    276   1.1  christos pmap_page_clear_attributes(struct vm_page_md *mdpg, u_int clear_attributes)
    277   1.1  christos {
    278   1.1  christos 	volatile u_int * const attrp = &mdpg->mdpg_attrs;
    279   1.1  christos #ifdef MULTIPROCESSOR
    280   1.1  christos 	for (;;) {
    281   1.1  christos 		u_int old_attr = *attrp;
    282   1.1  christos 		if ((old_attr & clear_attributes) == 0)
    283   1.1  christos 			return false;
    284   1.1  christos 		u_int new_attr = old_attr & ~clear_attributes;
    285   1.1  christos 		if (old_attr == atomic_cas_uint(attrp, old_attr, new_attr))
    286   1.1  christos 			return true;
    287   1.1  christos 	}
    288   1.1  christos #else
    289   1.1  christos 	u_int old_attr = *attrp;
    290   1.1  christos 	if ((old_attr & clear_attributes) == 0)
    291   1.1  christos 		return false;
    292   1.1  christos 	*attrp &= ~clear_attributes;
    293   1.1  christos 	return true;
    294   1.1  christos #endif
    295   1.1  christos }
    296   1.1  christos 
    297   1.1  christos void
    298   1.1  christos pmap_page_set_attributes(struct vm_page_md *mdpg, u_int set_attributes)
    299   1.1  christos {
    300   1.1  christos #ifdef MULTIPROCESSOR
    301   1.1  christos 	atomic_or_uint(&mdpg->mdpg_attrs, set_attributes);
    302   1.1  christos #else
    303   1.1  christos 	mdpg->mdpg_attrs |= set_attributes;
    304   1.1  christos #endif
    305   1.1  christos }
    306   1.1  christos 
    307   1.1  christos static void
    308   1.1  christos pmap_page_syncicache(struct vm_page *pg)
    309   1.1  christos {
    310   1.1  christos #ifndef MULTIPROCESSOR
    311   1.1  christos 	struct pmap * const curpmap = curcpu()->ci_curpm;
    312   1.1  christos #endif
    313   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    314   1.1  christos 	pv_entry_t pv = &mdpg->mdpg_first;
    315   1.2      matt 	kcpuset_t *onproc;
    316   1.2      matt #ifdef MULTIPROCESSOR
    317   1.2      matt 	kcpuset_create(&onproc, true);
    318   1.3      matt #else
    319   1.3      matt 	onproc = NULL;
    320   1.2      matt #endif
    321   1.1  christos 	(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    322   1.2      matt 
    323   1.1  christos 	if (pv->pv_pmap != NULL) {
    324   1.1  christos 		for (; pv != NULL; pv = pv->pv_next) {
    325   1.1  christos #ifdef MULTIPROCESSOR
    326   1.2      matt 			kcpuset_merge(onproc, pv->pv_pmap->pm_onproc);
    327   1.2      matt 			if (kcpuset_match(onproc, kcpuset_running)) {
    328   1.1  christos 				break;
    329   1.1  christos 			}
    330   1.1  christos #else
    331   1.1  christos 			if (pv->pv_pmap == curpmap) {
    332   1.2      matt 				onproc = curcpu()->ci_data.cpu_kcpuset;
    333   1.1  christos 				break;
    334   1.1  christos 			}
    335   1.1  christos #endif
    336   1.1  christos 		}
    337   1.1  christos 	}
    338   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    339   1.1  christos 	kpreempt_disable();
    340   1.1  christos 	pmap_md_page_syncicache(pg, onproc);
    341   1.2      matt #ifdef MULTIPROCESSOR
    342   1.2      matt 	kcpuset_destroy(onproc);
    343   1.2      matt #endif
    344   1.1  christos 	kpreempt_enable();
    345   1.1  christos }
    346   1.1  christos 
    347   1.1  christos /*
    348   1.1  christos  * Define the initial bounds of the kernel virtual address space.
    349   1.1  christos  */
    350   1.1  christos void
    351   1.1  christos pmap_virtual_space(vaddr_t *vstartp, vaddr_t *vendp)
    352   1.1  christos {
    353   1.1  christos 
    354   1.1  christos 	*vstartp = VM_MIN_KERNEL_ADDRESS;
    355   1.1  christos 	*vendp = VM_MAX_KERNEL_ADDRESS;
    356   1.1  christos }
    357   1.1  christos 
    358   1.1  christos vaddr_t
    359   1.1  christos pmap_growkernel(vaddr_t maxkvaddr)
    360   1.1  christos {
    361   1.1  christos 	vaddr_t virtual_end = pmap_limits.virtual_end;
    362   1.1  christos 	maxkvaddr = pmap_round_seg(maxkvaddr) - 1;
    363   1.1  christos 
    364   1.1  christos 	/*
    365   1.1  christos 	 * Reserve PTEs for the new KVA space.
    366   1.1  christos 	 */
    367   1.1  christos 	for (; virtual_end < maxkvaddr; virtual_end += NBSEG) {
    368   1.1  christos 		pmap_pte_reserve(pmap_kernel(), virtual_end, 0);
    369   1.1  christos 	}
    370   1.1  christos 
    371   1.1  christos 	/*
    372   1.1  christos 	 * Don't exceed VM_MAX_KERNEL_ADDRESS!
    373   1.1  christos 	 */
    374   1.1  christos 	if (virtual_end == 0 || virtual_end > VM_MAX_KERNEL_ADDRESS)
    375   1.1  christos 		virtual_end = VM_MAX_KERNEL_ADDRESS;
    376   1.1  christos 
    377   1.1  christos 	/*
    378   1.1  christos 	 * Update new end.
    379   1.1  christos 	 */
    380   1.1  christos 	pmap_limits.virtual_end = virtual_end;
    381   1.1  christos 	return virtual_end;
    382   1.1  christos }
    383   1.1  christos 
    384   1.1  christos /*
    385   1.1  christos  * Bootstrap memory allocator (alternative to vm_bootstrap_steal_memory()).
    386   1.1  christos  * This function allows for early dynamic memory allocation until the virtual
    387   1.1  christos  * memory system has been bootstrapped.  After that point, either kmem_alloc
    388   1.1  christos  * or malloc should be used.  This function works by stealing pages from the
    389   1.1  christos  * (to be) managed page pool, then implicitly mapping the pages (by using
    390   1.1  christos  * their k0seg addresses) and zeroing them.
    391   1.1  christos  *
    392   1.1  christos  * It may be used once the physical memory segments have been pre-loaded
    393   1.1  christos  * into the vm_physmem[] array.  Early memory allocation MUST use this
    394   1.1  christos  * interface!  This cannot be used after vm_page_startup(), and will
    395   1.1  christos  * generate a panic if tried.
    396   1.1  christos  *
    397   1.1  christos  * Note that this memory will never be freed, and in essence it is wired
    398   1.1  christos  * down.
    399   1.1  christos  *
    400   1.1  christos  * We must adjust *vstartp and/or *vendp iff we use address space
    401   1.1  christos  * from the kernel virtual address range defined by pmap_virtual_space().
    402   1.1  christos  */
    403   1.1  christos vaddr_t
    404   1.1  christos pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
    405   1.1  christos {
    406   1.1  christos 	u_int npgs;
    407   1.1  christos 	paddr_t pa;
    408   1.1  christos 	vaddr_t va;
    409   1.1  christos 
    410   1.1  christos 	size = round_page(size);
    411   1.1  christos 	npgs = atop(size);
    412   1.1  christos 
    413   1.1  christos 	for (u_int bank = 0; bank < vm_nphysseg; bank++) {
    414   1.1  christos 		struct vm_physseg * const seg = VM_PHYSMEM_PTR(bank);
    415   1.1  christos 		if (uvm.page_init_done == true)
    416   1.1  christos 			panic("pmap_steal_memory: called _after_ bootstrap");
    417   1.1  christos 
    418   1.1  christos 		if (seg->avail_start != seg->start ||
    419   1.1  christos 		    seg->avail_start >= seg->avail_end)
    420   1.1  christos 			continue;
    421   1.1  christos 
    422   1.1  christos 		if ((seg->avail_end - seg->avail_start) < npgs)
    423   1.1  christos 			continue;
    424   1.1  christos 
    425   1.1  christos 		/*
    426   1.1  christos 		 * There are enough pages here; steal them!
    427   1.1  christos 		 */
    428   1.1  christos 		pa = ptoa(seg->avail_start);
    429   1.1  christos 		seg->avail_start += npgs;
    430   1.1  christos 		seg->start += npgs;
    431   1.1  christos 
    432   1.1  christos 		/*
    433   1.1  christos 		 * Have we used up this segment?
    434   1.1  christos 		 */
    435   1.1  christos 		if (seg->avail_start == seg->end) {
    436   1.1  christos 			if (vm_nphysseg == 1)
    437   1.1  christos 				panic("pmap_steal_memory: out of memory!");
    438   1.1  christos 
    439   1.1  christos 			/* Remove this segment from the list. */
    440   1.1  christos 			vm_nphysseg--;
    441   1.1  christos 			if (bank < vm_nphysseg)
    442   1.1  christos 				memmove(seg, seg+1,
    443   1.1  christos 				    sizeof(*seg) * (vm_nphysseg - bank));
    444   1.1  christos 		}
    445   1.1  christos 
    446   1.1  christos 		va = pmap_md_map_poolpage(pa, size);
    447   1.1  christos 		memset((void *)va, 0, size);
    448   1.1  christos 		return va;
    449   1.1  christos 	}
    450   1.1  christos 
    451   1.1  christos 	/*
    452   1.1  christos 	 * If we got here, there was no memory left.
    453   1.1  christos 	 */
    454   1.1  christos 	panic("pmap_steal_memory: no memory to steal");
    455   1.1  christos }
    456   1.1  christos 
    457   1.1  christos /*
    458   1.1  christos  *	Initialize the pmap module.
    459   1.1  christos  *	Called by vm_init, to initialize any structures that the pmap
    460   1.1  christos  *	system needs to map virtual memory.
    461   1.1  christos  */
    462   1.1  christos void
    463   1.1  christos pmap_init(void)
    464   1.1  christos {
    465   1.1  christos 	UVMHIST_INIT_STATIC(pmapexechist, pmapexechistbuf);
    466   1.1  christos 	UVMHIST_INIT_STATIC(pmaphist, pmaphistbuf);
    467   1.1  christos 
    468   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    469   1.1  christos 
    470   1.1  christos 	/*
    471   1.1  christos 	 * Initialize the segtab lock.
    472   1.1  christos 	 */
    473   1.1  christos 	mutex_init(&pmap_segtab_lock, MUTEX_DEFAULT, IPL_HIGH);
    474   1.1  christos 
    475   1.1  christos 	/*
    476   1.1  christos 	 * Set a low water mark on the pv_entry pool, so that we are
    477   1.1  christos 	 * more likely to have these around even in extreme memory
    478   1.1  christos 	 * starvation.
    479   1.1  christos 	 */
    480   1.1  christos 	pool_setlowat(&pmap_pv_pool, pmap_pv_lowat);
    481   1.1  christos 
    482   1.1  christos 	pmap_md_init();
    483   1.1  christos 
    484   1.1  christos 	/*
    485   1.1  christos 	 * Now it is safe to enable pv entry recording.
    486   1.1  christos 	 */
    487   1.1  christos 	pmap_initialized = true;
    488   1.1  christos }
    489   1.1  christos 
    490   1.1  christos /*
    491   1.1  christos  *	Create and return a physical map.
    492   1.1  christos  *
    493   1.1  christos  *	If the size specified for the map
    494   1.1  christos  *	is zero, the map is an actual physical
    495   1.1  christos  *	map, and may be referenced by the
    496   1.1  christos  *	hardware.
    497   1.1  christos  *
    498   1.1  christos  *	If the size specified is non-zero,
    499   1.1  christos  *	the map will be used in software only, and
    500   1.1  christos  *	is bounded by that size.
    501   1.1  christos  */
    502   1.1  christos pmap_t
    503   1.1  christos pmap_create(void)
    504   1.1  christos {
    505   1.1  christos 	pmap_t pmap;
    506   1.1  christos 
    507   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    508   1.1  christos 	PMAP_COUNT(create);
    509   1.1  christos 
    510   1.1  christos 	pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
    511   1.1  christos 	memset(pmap, 0, PMAP_SIZE);
    512   1.1  christos 
    513   1.1  christos 	KASSERT(pmap->pm_pai[0].pai_link.le_prev == NULL);
    514   1.1  christos 
    515   1.1  christos 	pmap->pm_count = 1;
    516   1.1  christos 	pmap->pm_minaddr = VM_MIN_ADDRESS;
    517   1.1  christos 	pmap->pm_maxaddr = VM_MAXUSER_ADDRESS;
    518   1.1  christos 
    519   1.1  christos 	pmap_segtab_init(pmap);
    520   1.1  christos 
    521   1.5    nonaka #ifdef MULTIPROCESSOR
    522   1.5    nonaka 	kcpuset_create(&pmap->pm_active, true);
    523   1.5    nonaka 	kcpuset_create(&pmap->pm_onproc, true);
    524   1.5    nonaka #endif
    525   1.5    nonaka 
    526   1.1  christos 	UVMHIST_LOG(pmaphist, "<- pmap %p", pmap,0,0,0);
    527   1.1  christos 	return pmap;
    528   1.1  christos }
    529   1.1  christos 
    530   1.1  christos /*
    531   1.1  christos  *	Retire the given physical map from service.
    532   1.1  christos  *	Should only be called if the map contains
    533   1.1  christos  *	no valid mappings.
    534   1.1  christos  */
    535   1.1  christos void
    536   1.1  christos pmap_destroy(pmap_t pmap)
    537   1.1  christos {
    538   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    539   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0,0,0);
    540   1.1  christos 
    541   1.1  christos 	if (atomic_dec_uint_nv(&pmap->pm_count) > 0) {
    542   1.1  christos 		PMAP_COUNT(dereference);
    543   1.1  christos 		return;
    544   1.1  christos 	}
    545   1.1  christos 
    546   1.1  christos 	KASSERT(pmap->pm_count == 0);
    547   1.1  christos 	PMAP_COUNT(destroy);
    548   1.1  christos 	kpreempt_disable();
    549  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    550   1.1  christos 	pmap_tlb_asid_release_all(pmap);
    551   1.1  christos 	pmap_segtab_destroy(pmap, NULL, 0);
    552  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    553   1.1  christos 
    554   1.6    nonaka #ifdef MULTIPROCESSOR
    555   1.7    nonaka 	kcpuset_destroy(pmap->pm_active);
    556   1.7    nonaka 	kcpuset_destroy(pmap->pm_onproc);
    557   1.6    nonaka #endif
    558   1.6    nonaka 
    559   1.1  christos 	pool_put(&pmap_pmap_pool, pmap);
    560   1.1  christos 	kpreempt_enable();
    561   1.1  christos 
    562   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    563   1.1  christos }
    564   1.1  christos 
    565   1.1  christos /*
    566   1.1  christos  *	Add a reference to the specified pmap.
    567   1.1  christos  */
    568   1.1  christos void
    569   1.1  christos pmap_reference(pmap_t pmap)
    570   1.1  christos {
    571   1.1  christos 
    572   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    573   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0,0,0);
    574   1.1  christos 	PMAP_COUNT(reference);
    575   1.1  christos 
    576   1.1  christos 	if (pmap != NULL) {
    577   1.1  christos 		atomic_inc_uint(&pmap->pm_count);
    578   1.1  christos 	}
    579   1.1  christos 
    580   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    581   1.1  christos }
    582   1.1  christos 
    583   1.1  christos /*
    584   1.1  christos  *	Make a new pmap (vmspace) active for the given process.
    585   1.1  christos  */
    586   1.1  christos void
    587   1.1  christos pmap_activate(struct lwp *l)
    588   1.1  christos {
    589   1.1  christos 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    590   1.1  christos 
    591   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    592   1.1  christos 	UVMHIST_LOG(pmaphist, "(l=%p (pmap=%p))", l, pmap, 0,0);
    593   1.1  christos 	PMAP_COUNT(activate);
    594   1.1  christos 
    595   1.1  christos 	kpreempt_disable();
    596  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    597   1.1  christos 	pmap_tlb_asid_acquire(pmap, l);
    598   1.1  christos 	if (l == curlwp) {
    599   1.1  christos 		pmap_segtab_activate(pmap, l);
    600   1.1  christos 	}
    601  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    602   1.1  christos 	kpreempt_enable();
    603   1.1  christos 
    604   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    605   1.1  christos }
    606   1.1  christos 
    607   1.1  christos /*
    608   1.1  christos  *	Make a previously active pmap (vmspace) inactive.
    609   1.1  christos  */
    610   1.1  christos void
    611   1.1  christos pmap_deactivate(struct lwp *l)
    612   1.1  christos {
    613   1.1  christos 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
    614   1.1  christos 
    615   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    616   1.1  christos 	UVMHIST_LOG(pmaphist, "(l=%p (pmap=%p))", l, pmap, 0,0);
    617   1.1  christos 	PMAP_COUNT(deactivate);
    618   1.1  christos 
    619   1.1  christos 	kpreempt_disable();
    620  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    621   1.1  christos 	curcpu()->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    622   1.1  christos 	pmap_tlb_asid_deactivate(pmap);
    623  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    624   1.1  christos 	kpreempt_enable();
    625   1.1  christos 
    626   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    627   1.1  christos }
    628   1.1  christos 
    629   1.1  christos void
    630   1.1  christos pmap_update(struct pmap *pmap)
    631   1.1  christos {
    632   1.1  christos 
    633   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    634   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p)", pmap, 0,0,0);
    635   1.1  christos 	PMAP_COUNT(update);
    636   1.1  christos 
    637   1.1  christos 	kpreempt_disable();
    638   1.1  christos #if defined(MULTIPROCESSOR) && defined(PMAP_NEED_TLB_SHOOTDOWN)
    639   1.1  christos 	u_int pending = atomic_swap_uint(&pmap->pm_shootdown_pending, 0);
    640   1.1  christos 	if (pending && pmap_tlb_shootdown_bystanders(pmap))
    641   1.1  christos 		PMAP_COUNT(shootdown_ipis);
    642   1.1  christos #endif
    643  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
    644   1.1  christos #ifdef DEBUG
    645   1.1  christos 	pmap_tlb_check(pmap, pmap_md_tlb_check_entry);
    646   1.1  christos #endif /* DEBUG */
    647   1.1  christos 
    648   1.1  christos 	/*
    649   1.1  christos 	 * If pmap_remove_all was called, we deactivated ourselves and nuked
    650   1.1  christos 	 * our ASID.  Now we have to reactivate ourselves.
    651   1.1  christos 	 */
    652   1.1  christos 	if (__predict_false(pmap->pm_flags & PMAP_DEFERRED_ACTIVATE)) {
    653   1.1  christos 		pmap->pm_flags ^= PMAP_DEFERRED_ACTIVATE;
    654   1.1  christos 		pmap_tlb_asid_acquire(pmap, curlwp);
    655   1.1  christos 		pmap_segtab_activate(pmap, curlwp);
    656   1.1  christos 	}
    657  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
    658   1.1  christos 	kpreempt_enable();
    659   1.1  christos 
    660   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    661   1.1  christos }
    662   1.1  christos 
    663   1.1  christos /*
    664   1.1  christos  *	Remove the given range of addresses from the specified map.
    665   1.1  christos  *
    666   1.1  christos  *	It is assumed that the start and end are properly
    667   1.1  christos  *	rounded to the page size.
    668   1.1  christos  */
    669   1.1  christos 
    670   1.1  christos static bool
    671   1.1  christos pmap_pte_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
    672   1.1  christos 	uintptr_t flags)
    673   1.1  christos {
    674   1.1  christos 	const pt_entry_t npte = flags;
    675   1.1  christos 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    676   1.1  christos 
    677   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    678   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p %sva=%"PRIxVADDR"..%"PRIxVADDR,
    679   1.1  christos 	    pmap, (is_kernel_pmap_p ? "(kernel) " : ""), sva, eva);
    680   1.1  christos 	UVMHIST_LOG(pmaphist, "ptep=%p, flags(npte)=%#"PRIxPTR")",
    681   1.1  christos 	    ptep, flags, 0, 0);
    682   1.1  christos 
    683   1.1  christos 	KASSERT(kpreempt_disabled());
    684   1.1  christos 
    685   1.1  christos 	for (; sva < eva; sva += NBPG, ptep++) {
    686   1.1  christos 		pt_entry_t pt_entry = *ptep;
    687   1.1  christos 		if (!pte_valid_p(pt_entry))
    688   1.1  christos 			continue;
    689   1.1  christos 		if (is_kernel_pmap_p)
    690   1.1  christos 			PMAP_COUNT(remove_kernel_calls);
    691   1.1  christos 		else
    692   1.1  christos 			PMAP_COUNT(remove_user_pages);
    693   1.1  christos 		if (pte_wired_p(pt_entry))
    694   1.1  christos 			pmap->pm_stats.wired_count--;
    695   1.1  christos 		pmap->pm_stats.resident_count--;
    696   1.1  christos 		struct vm_page *pg = PHYS_TO_VM_PAGE(pte_to_paddr(pt_entry));
    697   1.1  christos 		if (__predict_true(pg != NULL)) {
    698   1.1  christos 			pmap_remove_pv(pmap, sva, pg,
    699   1.1  christos 			   pte_modified_p(pt_entry));
    700   1.1  christos 		}
    701  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
    702   1.1  christos 		*ptep = npte;
    703   1.1  christos 		/*
    704   1.1  christos 		 * Flush the TLB for the given address.
    705   1.1  christos 		 */
    706   1.1  christos 		pmap_tlb_invalidate_addr(pmap, sva);
    707  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
    708   1.1  christos 	}
    709   1.1  christos 	return false;
    710   1.1  christos }
    711   1.1  christos 
    712   1.1  christos void
    713   1.1  christos pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva)
    714   1.1  christos {
    715   1.1  christos 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    716   1.1  christos 	const pt_entry_t npte = pte_nv_entry(is_kernel_pmap_p);
    717   1.1  christos 
    718   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    719   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p, va=%#"PRIxVADDR"..%#"PRIxVADDR")",
    720   1.1  christos 	    pmap, sva, eva, 0);
    721   1.1  christos 
    722   1.1  christos 	if (is_kernel_pmap_p)
    723   1.1  christos 		PMAP_COUNT(remove_kernel_calls);
    724   1.1  christos 	else
    725   1.1  christos 		PMAP_COUNT(remove_user_calls);
    726   1.1  christos #ifdef PARANOIADIAG
    727   1.1  christos 	if (sva < pm->pm_minaddr || eva > pm->pm_maxaddr)
    728   1.1  christos 		panic("%s: va range %#"PRIxVADDR"-%#"PRIxVADDR" not in range",
    729   1.1  christos 		    __func__, sva, eva - 1);
    730   1.1  christos 	if (PMAP_IS_ACTIVE(pmap)) {
    731   1.1  christos 		struct pmap_asid_info * const pai = PMAP_PAI(pmap, curcpu());
    732   1.1  christos 		uint32_t asid = tlb_get_asid();
    733   1.1  christos 		if (asid != pai->pai_asid) {
    734   1.1  christos 			panic("%s: inconsistency for active TLB flush"
    735   1.1  christos 			    ": %d <-> %d", __func__, asid, pai->pai_asid);
    736   1.1  christos 		}
    737   1.1  christos 	}
    738   1.1  christos #endif
    739   1.1  christos 	kpreempt_disable();
    740   1.1  christos 	pmap_pte_process(pmap, sva, eva, pmap_pte_remove, npte);
    741   1.1  christos 	kpreempt_enable();
    742   1.1  christos 
    743   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    744   1.1  christos }
    745   1.1  christos 
    746   1.1  christos /*
    747   1.1  christos  *	pmap_page_protect:
    748   1.1  christos  *
    749   1.1  christos  *	Lower the permission for all mappings to a given page.
    750   1.1  christos  */
    751   1.1  christos void
    752   1.1  christos pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
    753   1.1  christos {
    754   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    755   1.1  christos 	pv_entry_t pv;
    756   1.1  christos 	vaddr_t va;
    757   1.1  christos 
    758   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    759   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR") prot=%#x)",
    760   1.1  christos 	    pg, VM_PAGE_TO_PHYS(pg), prot, 0);
    761   1.1  christos 	PMAP_COUNT(page_protect);
    762   1.1  christos 
    763   1.1  christos 	switch (prot) {
    764   1.1  christos 	case VM_PROT_READ|VM_PROT_WRITE:
    765   1.1  christos 	case VM_PROT_ALL:
    766   1.1  christos 		break;
    767   1.1  christos 
    768   1.1  christos 	/* copy_on_write */
    769   1.1  christos 	case VM_PROT_READ:
    770   1.1  christos 	case VM_PROT_READ|VM_PROT_EXECUTE:
    771   1.1  christos 		(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    772   1.1  christos 		pv = &mdpg->mdpg_first;
    773   1.1  christos 		/*
    774   1.1  christos 		 * Loop over all current mappings setting/clearing as appropriate.
    775   1.1  christos 		 */
    776   1.1  christos 		if (pv->pv_pmap != NULL) {
    777   1.1  christos 			while (pv != NULL) {
    778   1.1  christos 				const pmap_t pmap = pv->pv_pmap;
    779   1.1  christos 				const uint16_t gen = VM_PAGEMD_PVLIST_GEN(mdpg);
    780   1.1  christos 				va = pv->pv_va;
    781   1.1  christos 				VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    782   1.1  christos 				pmap_protect(pmap, va, va + PAGE_SIZE, prot);
    783   1.1  christos 				KASSERT(pv->pv_pmap == pmap);
    784   1.1  christos 				pmap_update(pmap);
    785   1.1  christos 				if (gen != VM_PAGEMD_PVLIST_LOCK(mdpg, false)) {
    786   1.1  christos 					pv = &mdpg->mdpg_first;
    787   1.1  christos 				} else {
    788   1.1  christos 					pv = pv->pv_next;
    789   1.1  christos 				}
    790   1.1  christos 			}
    791   1.1  christos 		}
    792   1.1  christos 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    793   1.1  christos 		break;
    794   1.1  christos 
    795   1.1  christos 	/* remove_all */
    796   1.1  christos 	default:
    797   1.1  christos 		/*
    798   1.1  christos 		 * Do this first so that for each unmapping, pmap_remove_pv
    799   1.1  christos 		 * won't try to sync the icache.
    800   1.1  christos 		 */
    801   1.1  christos 		if (pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE)) {
    802   1.1  christos 			UVMHIST_LOG(pmapexechist, "pg %p (pa %#"PRIxPADDR
    803   1.1  christos 			    "): execpage cleared", pg, VM_PAGE_TO_PHYS(pg),0,0);
    804   1.1  christos 			PMAP_COUNT(exec_uncached_page_protect);
    805   1.1  christos 		}
    806   1.1  christos 		(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    807   1.1  christos 		pv = &mdpg->mdpg_first;
    808   1.1  christos 		while (pv->pv_pmap != NULL) {
    809   1.1  christos 			const pmap_t pmap = pv->pv_pmap;
    810   1.1  christos 			va = pv->pv_va;
    811   1.1  christos 			VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    812   1.1  christos 			pmap_remove(pmap, va, va + PAGE_SIZE);
    813   1.1  christos 			pmap_update(pmap);
    814   1.1  christos 			(void)VM_PAGEMD_PVLIST_LOCK(mdpg, false);
    815   1.1  christos 		}
    816   1.1  christos 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
    817   1.1  christos 	}
    818   1.1  christos 
    819   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    820   1.1  christos }
    821   1.1  christos 
    822   1.1  christos static bool
    823   1.1  christos pmap_pte_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
    824   1.1  christos 	uintptr_t flags)
    825   1.1  christos {
    826   1.1  christos 	const vm_prot_t prot = (flags & VM_PROT_ALL);
    827   1.1  christos 
    828   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    829   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p %sva=%"PRIxVADDR"..%"PRIxVADDR,
    830   1.1  christos 	    pmap, (pmap == pmap_kernel() ? "(kernel) " : ""), sva, eva);
    831   1.1  christos 	UVMHIST_LOG(pmaphist, "ptep=%p, flags(npte)=%#"PRIxPTR")",
    832   1.1  christos 	    ptep, flags, 0, 0);
    833   1.1  christos 
    834   1.1  christos 	KASSERT(kpreempt_disabled());
    835   1.1  christos 	/*
    836   1.1  christos 	 * Change protection on every valid mapping within this segment.
    837   1.1  christos 	 */
    838   1.1  christos 	for (; sva < eva; sva += NBPG, ptep++) {
    839   1.1  christos 		pt_entry_t pt_entry = *ptep;
    840   1.1  christos 		if (!pte_valid_p(pt_entry))
    841   1.1  christos 			continue;
    842   1.1  christos 		struct vm_page * const pg =
    843   1.1  christos 		    PHYS_TO_VM_PAGE(pte_to_paddr(pt_entry));
    844   1.1  christos 		if (pg != NULL && pte_modified_p(pt_entry)) {
    845   1.1  christos 			struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    846   1.1  christos 			pmap_md_vca_clean(pg, sva, PMAP_WBINV);
    847   1.1  christos 			if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
    848   1.1  christos 				KASSERT(mdpg->mdpg_first.pv_pmap != NULL);
    849   1.1  christos 				if (pte_cached_p(pt_entry)) {
    850   1.1  christos 					UVMHIST_LOG(pmapexechist,
    851   1.1  christos 					    "pg %p (pa %#"PRIxPADDR"): %s",
    852   1.1  christos 					    pg, VM_PAGE_TO_PHYS(pg),
    853   1.1  christos 					    "syncicached performed", 0);
    854   1.1  christos 					pmap_page_syncicache(pg);
    855   1.1  christos 					PMAP_COUNT(exec_synced_protect);
    856   1.1  christos 				}
    857   1.1  christos 			}
    858   1.1  christos 		}
    859   1.1  christos 		pt_entry = pte_prot_downgrade(pt_entry, prot);
    860   1.1  christos 		if (*ptep != pt_entry) {
    861  1.10    nonaka 			pmap_md_tlb_miss_lock_enter();
    862   1.1  christos 			*ptep = pt_entry;
    863   1.1  christos 			/*
    864   1.1  christos 			 * Update the TLB if needed.
    865   1.1  christos 			 */
    866   1.1  christos 			pmap_tlb_update_addr(pmap, sva, pt_entry,
    867   1.1  christos 			    PMAP_TLB_NEED_IPI);
    868  1.10    nonaka 			pmap_md_tlb_miss_lock_exit();
    869   1.1  christos 		}
    870   1.1  christos 	}
    871   1.1  christos 	return false;
    872   1.1  christos }
    873   1.1  christos 
    874   1.1  christos /*
    875   1.1  christos  *	Set the physical protection on the
    876   1.1  christos  *	specified range of this map as requested.
    877   1.1  christos  */
    878   1.1  christos void
    879   1.1  christos pmap_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
    880   1.1  christos {
    881   1.1  christos 
    882   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    883   1.1  christos 	UVMHIST_LOG(pmaphist,
    884   1.1  christos 	    "  pmap=%p, va=%#"PRIxVADDR"..%#"PRIxVADDR" port=%#x)",
    885   1.1  christos 	    pmap, sva, eva, prot);
    886   1.1  christos 	PMAP_COUNT(protect);
    887   1.1  christos 
    888   1.1  christos 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
    889   1.1  christos 		pmap_remove(pmap, sva, eva);
    890   1.1  christos 		UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    891   1.1  christos 		return;
    892   1.1  christos 	}
    893   1.1  christos 
    894   1.1  christos #ifdef PARANOIADIAG
    895   1.1  christos 	if (sva < pm->pm_minaddr || eva > pm->pm_maxaddr)
    896   1.1  christos 		panic("%s: va range %#"PRIxVADDR"-%#"PRIxVADDR" not in range",
    897   1.1  christos 		    __func__, sva, eva - 1);
    898   1.1  christos 	if (PMAP_IS_ACTIVE(pmap)) {
    899   1.1  christos 		struct pmap_asid_info * const pai = PMAP_PAI(pmap, curcpu());
    900   1.1  christos 		uint32_t asid = tlb_get_asid();
    901   1.1  christos 		if (asid != pai->pai_asid) {
    902   1.1  christos 			panic("%s: inconsistency for active TLB update"
    903   1.1  christos 			    ": %d <-> %d", __func__, asid, pai->pai_asid);
    904   1.1  christos 		}
    905   1.1  christos 	}
    906   1.1  christos #endif
    907   1.1  christos 
    908   1.1  christos 	/*
    909   1.1  christos 	 * Change protection on every valid mapping within this segment.
    910   1.1  christos 	 */
    911   1.1  christos 	kpreempt_disable();
    912   1.1  christos 	pmap_pte_process(pmap, sva, eva, pmap_pte_protect, prot);
    913   1.1  christos 	kpreempt_enable();
    914   1.1  christos 
    915   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    916   1.1  christos }
    917   1.1  christos 
    918   1.1  christos #if defined(__PMAP_VIRTUAL_CACHE_ALIASES)
    919   1.1  christos /*
    920   1.1  christos  *	pmap_page_cache:
    921   1.1  christos  *
    922   1.1  christos  *	Change all mappings of a managed page to cached/uncached.
    923   1.1  christos  */
    924   1.1  christos static void
    925   1.1  christos pmap_page_cache(struct vm_page *pg, bool cached)
    926   1.1  christos {
    927   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
    928   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
    929   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR") cached=%s)",
    930   1.1  christos 	    pg, VM_PAGE_TO_PHYS(pg), cached ? "true" : "false", 0);
    931   1.1  christos 	KASSERT(kpreempt_disabled());
    932   1.1  christos 
    933   1.1  christos 	if (cached) {
    934   1.1  christos 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_UNCACHED);
    935   1.1  christos 		PMAP_COUNT(page_cache_restorations);
    936   1.1  christos 	} else {
    937   1.1  christos 		pmap_page_set_attributes(mdpg, VM_PAGEMD_UNCACHED);
    938   1.1  christos 		PMAP_COUNT(page_cache_evictions);
    939   1.1  christos 	}
    940   1.1  christos 
    941   1.1  christos 	KASSERT(VM_PAGEMD_PVLIST_LOCKED_P(mdpg));
    942   1.1  christos 	KASSERT(kpreempt_disabled());
    943   1.1  christos 	for (pv_entry_t pv = &mdpg->mdpg_first;
    944   1.1  christos 	     pv != NULL;
    945   1.1  christos 	     pv = pv->pv_next) {
    946   1.1  christos 		pmap_t pmap = pv->pv_pmap;
    947   1.1  christos 		vaddr_t va = pv->pv_va;
    948   1.1  christos 
    949   1.1  christos 		KASSERT(pmap != NULL);
    950   1.1  christos 		KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
    951   1.1  christos 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
    952   1.1  christos 		if (ptep == NULL)
    953   1.1  christos 			continue;
    954   1.1  christos 		pt_entry_t pt_entry = *ptep;
    955   1.1  christos 		if (pte_valid_p(pt_entry)) {
    956   1.1  christos 			pt_entry = pte_cached_change(pt_entry, cached);
    957  1.10    nonaka 			pmap_md_tlb_miss_lock_enter();
    958   1.1  christos 			*ptep = pt_entry;
    959   1.1  christos 			pmap_tlb_update_addr(pmap, va, pt_entry,
    960   1.1  christos 			    PMAP_TLB_NEED_IPI);
    961  1.10    nonaka 			pmap_md_tlb_miss_lock_exit();
    962   1.1  christos 		}
    963   1.1  christos 	}
    964   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
    965   1.1  christos }
    966   1.1  christos #endif	/* __PMAP_VIRTUAL_CACHE_ALIASES */
    967   1.1  christos 
    968   1.1  christos /*
    969   1.1  christos  *	Insert the given physical page (p) at
    970   1.1  christos  *	the specified virtual address (v) in the
    971   1.1  christos  *	target physical map with the protection requested.
    972   1.1  christos  *
    973   1.1  christos  *	If specified, the page will be wired down, meaning
    974   1.1  christos  *	that the related pte can not be reclaimed.
    975   1.1  christos  *
    976   1.1  christos  *	NB:  This is the only routine which MAY NOT lazy-evaluate
    977   1.1  christos  *	or lose information.  That is, this routine must actually
    978   1.1  christos  *	insert this page into the given map NOW.
    979   1.1  christos  */
    980   1.1  christos int
    981   1.1  christos pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
    982   1.1  christos {
    983   1.1  christos 	pt_entry_t npte;
    984   1.1  christos 	const bool wired = (flags & PMAP_WIRED) != 0;
    985   1.1  christos 	const bool is_kernel_pmap_p = (pmap == pmap_kernel());
    986   1.1  christos #ifdef UVMHIST
    987   1.1  christos 	struct kern_history * const histp =
    988   1.1  christos 	    ((prot & VM_PROT_EXECUTE) ? &pmapexechist : &pmaphist);
    989   1.1  christos #endif
    990   1.1  christos 
    991   1.1  christos 	UVMHIST_FUNC(__func__);
    992   1.1  christos #define VM_PROT_STRING(prot) \
    993   1.1  christos 	&"\0    (R)\0  (W)\0  (RW)\0 (X)\0  (RX)\0 (WX)\0 (RWX)\0"[UVM_PROTECTION(prot)*6]
    994   1.1  christos 	UVMHIST_CALLED(*histp);
    995   1.1  christos 	UVMHIST_LOG(*histp, "(pmap=%p, va=%#"PRIxVADDR", pa=%#"PRIxPADDR,
    996   1.1  christos 	    pmap, va, pa, 0);
    997   1.1  christos 	UVMHIST_LOG(*histp, "prot=%#x%s flags=%#x%s)",
    998   1.1  christos 	    prot, VM_PROT_STRING(prot), flags, VM_PROT_STRING(flags));
    999   1.1  christos 
   1000   1.1  christos 	const bool good_color = PMAP_PAGE_COLOROK_P(pa, va);
   1001   1.1  christos 	if (is_kernel_pmap_p) {
   1002   1.1  christos 		PMAP_COUNT(kernel_mappings);
   1003   1.1  christos 		if (!good_color)
   1004   1.1  christos 			PMAP_COUNT(kernel_mappings_bad);
   1005   1.1  christos 	} else {
   1006   1.1  christos 		PMAP_COUNT(user_mappings);
   1007   1.1  christos 		if (!good_color)
   1008   1.1  christos 			PMAP_COUNT(user_mappings_bad);
   1009   1.1  christos 	}
   1010   1.1  christos #if defined(DEBUG) || defined(DIAGNOSTIC) || defined(PARANOIADIAG)
   1011   1.1  christos 	if (va < pmap->pm_minaddr || va >= pmap->pm_maxaddr)
   1012   1.1  christos 		panic("%s: %s %#"PRIxVADDR" too big",
   1013   1.1  christos 		    __func__, is_kernel_pmap_p ? "kva" : "uva", va);
   1014   1.1  christos #endif
   1015   1.1  christos 
   1016   1.1  christos 	KASSERTMSG(prot & VM_PROT_READ,
   1017   1.1  christos 	    "%s: no READ (%#x) in prot %#x", __func__, VM_PROT_READ, prot);
   1018   1.1  christos 
   1019   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1020   1.1  christos 	struct vm_page_md *mdpg;
   1021   1.1  christos 
   1022   1.1  christos 	if (pg) {
   1023   1.1  christos 		mdpg = VM_PAGE_TO_MD(pg);
   1024   1.1  christos 		/* Set page referenced/modified status based on flags */
   1025   1.1  christos 		if (flags & VM_PROT_WRITE)
   1026   1.1  christos 			pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1027   1.1  christos 		else if (flags & VM_PROT_ALL)
   1028   1.1  christos 			pmap_page_set_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1029   1.1  christos 
   1030   1.1  christos #ifdef __PMAP_VIRTUAL_CACHE_ALIASES
   1031   1.1  christos 		if (!VM_PAGEMD_CACHED(pg))
   1032   1.1  christos 			flags |= PMAP_NOCACHE;
   1033   1.1  christos #endif
   1034   1.1  christos 
   1035   1.1  christos 		PMAP_COUNT(managed_mappings);
   1036   1.1  christos 	} else {
   1037   1.1  christos 		/*
   1038   1.1  christos 		 * Assumption: if it is not part of our managed memory
   1039   1.1  christos 		 * then it must be device memory which may be volatile.
   1040   1.1  christos 		 */
   1041   1.1  christos 		mdpg = NULL;
   1042   1.1  christos 		flags |= PMAP_NOCACHE;
   1043   1.1  christos 		PMAP_COUNT(unmanaged_mappings);
   1044   1.1  christos 	}
   1045   1.1  christos 
   1046   1.1  christos 	npte = pte_make_enter(pa, mdpg, prot, flags, is_kernel_pmap_p);
   1047   1.1  christos 
   1048   1.1  christos 	kpreempt_disable();
   1049   1.1  christos 	pt_entry_t * const ptep = pmap_pte_reserve(pmap, va, flags);
   1050   1.1  christos 	if (__predict_false(ptep == NULL)) {
   1051   1.1  christos 		kpreempt_enable();
   1052   1.1  christos 		UVMHIST_LOG(*histp, "<- ENOMEM", 0,0,0,0);
   1053   1.1  christos 		return ENOMEM;
   1054   1.1  christos 	}
   1055   1.1  christos 	pt_entry_t opte = *ptep;
   1056   1.1  christos 
   1057   1.1  christos 	/* Done after case that may sleep/return. */
   1058   1.1  christos 	if (pg)
   1059   1.1  christos 		pmap_enter_pv(pmap, va, pg, &npte);
   1060   1.1  christos 
   1061   1.1  christos 	/*
   1062   1.1  christos 	 * Now validate mapping with desired protection/wiring.
   1063   1.1  christos 	 * Assume uniform modified and referenced status for all
   1064   1.1  christos 	 * MIPS pages in a MACH page.
   1065   1.1  christos 	 */
   1066   1.1  christos 	if (wired) {
   1067   1.1  christos 		pmap->pm_stats.wired_count++;
   1068   1.1  christos 		npte = pte_wire_entry(npte);
   1069   1.1  christos 	}
   1070   1.1  christos 
   1071   1.1  christos 	UVMHIST_LOG(*histp, "new pte %#x (pa %#"PRIxPADDR")", npte, pa, 0,0);
   1072   1.1  christos 
   1073   1.1  christos 	if (pte_valid_p(opte) && pte_to_paddr(opte) != pa) {
   1074   1.1  christos 		pmap_remove(pmap, va, va + NBPG);
   1075   1.1  christos 		PMAP_COUNT(user_mappings_changed);
   1076   1.1  christos 	}
   1077   1.1  christos 
   1078   1.1  christos 	KASSERT(pte_valid_p(npte));
   1079   1.1  christos 	bool resident = pte_valid_p(opte);
   1080   1.1  christos 	if (!resident)
   1081   1.1  christos 		pmap->pm_stats.resident_count++;
   1082  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
   1083   1.1  christos 	*ptep = npte;
   1084   1.1  christos 
   1085   1.1  christos 	pmap_tlb_update_addr(pmap, va, npte,
   1086   1.1  christos 	    ((flags & VM_PROT_ALL) ? PMAP_TLB_INSERT : 0)
   1087   1.1  christos 	    | (resident ? PMAP_TLB_NEED_IPI : 0));
   1088  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
   1089   1.1  christos 	kpreempt_enable();
   1090   1.1  christos 
   1091   1.1  christos 	if (pg != NULL && (prot == (VM_PROT_READ | VM_PROT_EXECUTE))) {
   1092   1.1  christos 		KASSERT(mdpg != NULL);
   1093   1.1  christos 		PMAP_COUNT(exec_mappings);
   1094   1.1  christos 		if (!VM_PAGEMD_EXECPAGE_P(mdpg) && pte_cached_p(npte)) {
   1095   1.1  christos 			if (!pte_deferred_exec_p(npte)) {
   1096   1.1  christos 				UVMHIST_LOG(*histp,
   1097   1.1  christos 				    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1098   1.1  christos 				    va, pg, "immediate", "");
   1099   1.1  christos 				pmap_page_syncicache(pg);
   1100   1.1  christos 				pmap_page_set_attributes(mdpg,
   1101   1.1  christos 				    VM_PAGEMD_EXECPAGE);
   1102   1.1  christos 				PMAP_COUNT(exec_synced_mappings);
   1103   1.1  christos 			} else {
   1104   1.1  christos 				UVMHIST_LOG(*histp, "va=%#"PRIxVADDR
   1105   1.1  christos 				    " pg %p: %s syncicache: pte %#x",
   1106   1.1  christos 				    va, pg, "defer", npte);
   1107   1.1  christos 			}
   1108   1.1  christos 		} else {
   1109   1.1  christos 			UVMHIST_LOG(*histp,
   1110   1.1  christos 			    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1111   1.1  christos 			    va, pg, "no",
   1112   1.1  christos 			    (pte_cached_p(npte)
   1113   1.1  christos 				? " (already exec)"
   1114   1.1  christos 				: " (uncached)"));
   1115   1.1  christos 		}
   1116   1.1  christos 	} else if (pg != NULL && (prot & VM_PROT_EXECUTE)) {
   1117   1.1  christos 		KASSERT(mdpg != NULL);
   1118   1.1  christos 		KASSERT(prot & VM_PROT_WRITE);
   1119   1.1  christos 		PMAP_COUNT(exec_mappings);
   1120   1.1  christos 		pmap_page_syncicache(pg);
   1121   1.1  christos 		pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1122   1.1  christos 		UVMHIST_LOG(pmapexechist,
   1123   1.1  christos 		    "va=%#"PRIxVADDR" pg %p: %s syncicache%s",
   1124   1.1  christos 		    va, pg, "immediate", " (writeable)");
   1125   1.1  christos 	}
   1126   1.1  christos 
   1127   1.1  christos 	if (prot & VM_PROT_EXECUTE) {
   1128   1.1  christos 		UVMHIST_LOG(pmapexechist, "<- 0 (OK)", 0,0,0,0);
   1129   1.1  christos 	} else {
   1130   1.1  christos 		UVMHIST_LOG(pmaphist, "<- 0 (OK)", 0,0,0,0);
   1131   1.1  christos 	}
   1132   1.1  christos 	return 0;
   1133   1.1  christos }
   1134   1.1  christos 
   1135   1.1  christos void
   1136   1.1  christos pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   1137   1.1  christos {
   1138   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1139   1.1  christos 	struct vm_page_md *mdpg;
   1140   1.1  christos 
   1141   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1142   1.1  christos 	UVMHIST_LOG(pmaphist, "(va=%#"PRIxVADDR" pa=%#"PRIxPADDR
   1143   1.1  christos 	    ", prot=%#x, flags=%#x)", va, pa, prot, flags);
   1144   1.1  christos 	PMAP_COUNT(kenter_pa);
   1145   1.1  christos 
   1146   1.1  christos 	if (pg == NULL) {
   1147   1.1  christos 		mdpg = NULL;
   1148   1.1  christos 		PMAP_COUNT(kenter_pa_unmanaged);
   1149   1.1  christos 		flags |= PMAP_NOCACHE;
   1150   1.1  christos 	} else {
   1151   1.1  christos 		mdpg = VM_PAGE_TO_MD(pg);
   1152   1.1  christos 	}
   1153   1.1  christos 
   1154   1.1  christos 	if ((flags & PMAP_NOCACHE) == 0 && !PMAP_PAGE_COLOROK_P(pa, va))
   1155   1.1  christos 		PMAP_COUNT(kenter_pa_bad);
   1156   1.1  christos 
   1157   1.1  christos 	const pt_entry_t npte = pte_make_kenter_pa(pa, mdpg, prot, flags);
   1158   1.1  christos 	kpreempt_disable();
   1159   1.1  christos 	pt_entry_t * const ptep = pmap_pte_reserve(pmap_kernel(), va, 0);
   1160   1.1  christos 	KASSERT(ptep != NULL);
   1161   1.1  christos 	KASSERT(!pte_valid_p(*ptep));
   1162  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
   1163   1.1  christos 	*ptep = npte;
   1164   1.1  christos 	/*
   1165   1.1  christos 	 * We have the option to force this mapping into the TLB but we
   1166   1.1  christos 	 * don't.  Instead let the next reference to the page do it.
   1167   1.1  christos 	 */
   1168   1.1  christos 	pmap_tlb_update_addr(pmap_kernel(), va, npte, 0);
   1169  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
   1170   1.1  christos 	kpreempt_enable();
   1171   1.1  christos #if DEBUG > 1
   1172   1.1  christos 	for (u_int i = 0; i < PAGE_SIZE / sizeof(long); i++) {
   1173   1.1  christos 		if (((long *)va)[i] != ((long *)pa)[i])
   1174   1.1  christos 			panic("%s: contents (%lx) of va %#"PRIxVADDR
   1175   1.1  christos 			    " != contents (%lx) of pa %#"PRIxPADDR, __func__,
   1176   1.1  christos 			    ((long *)va)[i], va, ((long *)pa)[i], pa);
   1177   1.1  christos 	}
   1178   1.1  christos #endif
   1179   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
   1180   1.1  christos }
   1181   1.1  christos 
   1182   1.1  christos static bool
   1183   1.1  christos pmap_pte_kremove(pmap_t pmap, vaddr_t sva, vaddr_t eva, pt_entry_t *ptep,
   1184   1.1  christos 	uintptr_t flags)
   1185   1.1  christos {
   1186   1.1  christos 	const pt_entry_t new_pt_entry = pte_nv_entry(true);
   1187   1.1  christos 
   1188   1.1  christos 	KASSERT(kpreempt_disabled());
   1189   1.1  christos 
   1190   1.1  christos 	/*
   1191   1.1  christos 	 * Set every pt on every valid mapping within this segment.
   1192   1.1  christos 	 */
   1193   1.1  christos 	for (; sva < eva; sva += NBPG, ptep++) {
   1194   1.1  christos 		pt_entry_t pt_entry = *ptep;
   1195   1.1  christos 		if (!pte_valid_p(pt_entry)) {
   1196   1.1  christos 			continue;
   1197   1.1  christos 		}
   1198   1.1  christos 
   1199   1.1  christos 		PMAP_COUNT(kremove_pages);
   1200   1.1  christos 		struct vm_page * const pg =
   1201   1.1  christos 		    PHYS_TO_VM_PAGE(pte_to_paddr(pt_entry));
   1202   1.1  christos 		if (pg != NULL)
   1203   1.1  christos 			pmap_md_vca_clean(pg, sva, PMAP_WBINV);
   1204   1.1  christos 
   1205  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
   1206   1.1  christos 		*ptep = new_pt_entry;
   1207   1.1  christos 		pmap_tlb_invalidate_addr(pmap_kernel(), sva);
   1208  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
   1209   1.1  christos 	}
   1210   1.1  christos 
   1211   1.1  christos 	return false;
   1212   1.1  christos }
   1213   1.1  christos 
   1214   1.1  christos void
   1215   1.1  christos pmap_kremove(vaddr_t va, vsize_t len)
   1216   1.1  christos {
   1217   1.1  christos 	const vaddr_t sva = trunc_page(va);
   1218   1.1  christos 	const vaddr_t eva = round_page(va + len);
   1219   1.1  christos 
   1220   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1221   1.1  christos 	UVMHIST_LOG(pmaphist, "(va=%#"PRIxVADDR" len=%#"PRIxVSIZE")",
   1222   1.1  christos 	    va, len, 0,0);
   1223   1.1  christos 
   1224   1.1  christos 	kpreempt_disable();
   1225   1.1  christos 	pmap_pte_process(pmap_kernel(), sva, eva, pmap_pte_kremove, 0);
   1226   1.1  christos 	kpreempt_enable();
   1227   1.1  christos 
   1228   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
   1229   1.1  christos }
   1230   1.1  christos 
   1231   1.1  christos void
   1232   1.1  christos pmap_remove_all(struct pmap *pmap)
   1233   1.1  christos {
   1234   1.1  christos 	KASSERT(pmap != pmap_kernel());
   1235   1.1  christos 
   1236   1.1  christos 	kpreempt_disable();
   1237   1.1  christos 	/*
   1238   1.1  christos 	 * Free all of our ASIDs which means we can skip doing all the
   1239   1.1  christos 	 * tlb_invalidate_addrs().
   1240   1.1  christos 	 */
   1241  1.10    nonaka 	pmap_md_tlb_miss_lock_enter();
   1242   1.1  christos 	pmap_tlb_asid_deactivate(pmap);
   1243   1.1  christos 	pmap_tlb_asid_release_all(pmap);
   1244  1.10    nonaka 	pmap_md_tlb_miss_lock_exit();
   1245   1.1  christos 	pmap->pm_flags |= PMAP_DEFERRED_ACTIVATE;
   1246   1.1  christos 
   1247   1.1  christos 	kpreempt_enable();
   1248   1.1  christos }
   1249   1.1  christos 
   1250   1.1  christos /*
   1251   1.1  christos  *	Routine:	pmap_unwire
   1252   1.1  christos  *	Function:	Clear the wired attribute for a map/virtual-address
   1253   1.1  christos  *			pair.
   1254   1.1  christos  *	In/out conditions:
   1255   1.1  christos  *			The mapping must already exist in the pmap.
   1256   1.1  christos  */
   1257   1.1  christos void
   1258   1.1  christos pmap_unwire(pmap_t pmap, vaddr_t va)
   1259   1.1  christos {
   1260   1.1  christos 
   1261   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1262   1.1  christos 	UVMHIST_LOG(pmaphist, "(pmap=%p va=%#"PRIxVADDR")", pmap, va, 0,0);
   1263   1.1  christos 	PMAP_COUNT(unwire);
   1264   1.1  christos 
   1265   1.1  christos 	/*
   1266   1.1  christos 	 * Don't need to flush the TLB since PG_WIRED is only in software.
   1267   1.1  christos 	 */
   1268   1.1  christos #ifdef PARANOIADIAG
   1269   1.1  christos 	if (va < pmap->pm_minaddr || pmap->pm_maxaddr <= va)
   1270   1.1  christos 		panic("pmap_unwire");
   1271   1.1  christos #endif
   1272   1.1  christos 	kpreempt_disable();
   1273   1.1  christos 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1274   1.1  christos 	pt_entry_t pt_entry = *ptep;
   1275   1.1  christos #ifdef DIAGNOSTIC
   1276   1.1  christos 	if (ptep == NULL)
   1277   1.1  christos 		panic("%s: pmap %p va %#"PRIxVADDR" invalid STE",
   1278   1.1  christos 		    __func__, pmap, va);
   1279   1.1  christos #endif
   1280   1.1  christos 
   1281   1.1  christos #ifdef DIAGNOSTIC
   1282   1.1  christos 	if (!pte_valid_p(pt_entry))
   1283   1.1  christos 		panic("pmap_unwire: pmap %p va %#"PRIxVADDR" invalid PTE",
   1284   1.1  christos 		    pmap, va);
   1285   1.1  christos #endif
   1286   1.1  christos 
   1287   1.1  christos 	if (pte_wired_p(pt_entry)) {
   1288  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
   1289   1.1  christos 		*ptep = pte_unwire_entry(*ptep);
   1290  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
   1291   1.1  christos 		pmap->pm_stats.wired_count--;
   1292   1.1  christos 	}
   1293   1.1  christos #ifdef DIAGNOSTIC
   1294   1.1  christos 	else {
   1295   1.1  christos 		printf("%s: wiring for pmap %p va %#"PRIxVADDR" unchanged!\n",
   1296   1.1  christos 		    __func__, pmap, va);
   1297   1.1  christos 	}
   1298   1.1  christos #endif
   1299   1.1  christos 	kpreempt_enable();
   1300   1.1  christos }
   1301   1.1  christos 
   1302   1.1  christos /*
   1303   1.1  christos  *	Routine:	pmap_extract
   1304   1.1  christos  *	Function:
   1305   1.1  christos  *		Extract the physical page address associated
   1306   1.1  christos  *		with the given map/virtual_address pair.
   1307   1.1  christos  */
   1308   1.1  christos bool
   1309   1.1  christos pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap)
   1310   1.1  christos {
   1311   1.1  christos 	paddr_t pa;
   1312   1.1  christos 
   1313   1.1  christos 	//UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1314   1.1  christos 	//UVMHIST_LOG(pmaphist, "(pmap=%p va=%#"PRIxVADDR")", pmap, va, 0,0);
   1315   1.1  christos 	if (pmap == pmap_kernel()) {
   1316   1.1  christos 		if (pmap_md_direct_mapped_vaddr_p(va)) {
   1317   1.1  christos 			pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1318   1.1  christos 			goto done;
   1319   1.1  christos 		}
   1320   1.1  christos 		if (pmap_md_io_vaddr_p(va))
   1321   1.1  christos 			panic("pmap_extract: io address %#"PRIxVADDR"", va);
   1322   1.1  christos 	}
   1323   1.1  christos 	kpreempt_disable();
   1324   1.1  christos 	pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1325   1.1  christos 	if (ptep == NULL) {
   1326   1.1  christos 		//UVMHIST_LOG(pmaphist, "<- false (not in segmap)", 0,0,0,0);
   1327   1.1  christos 		kpreempt_enable();
   1328   1.1  christos 		return false;
   1329   1.1  christos 	}
   1330   1.1  christos 	if (!pte_valid_p(*ptep)) {
   1331   1.1  christos 		//UVMHIST_LOG(pmaphist, "<- false (PTE not valid)", 0,0,0,0);
   1332   1.1  christos 		kpreempt_enable();
   1333   1.1  christos 		return false;
   1334   1.1  christos 	}
   1335   1.1  christos 	pa = pte_to_paddr(*ptep) | (va & PGOFSET);
   1336   1.1  christos 	kpreempt_enable();
   1337   1.1  christos done:
   1338   1.1  christos 	if (pap != NULL) {
   1339   1.1  christos 		*pap = pa;
   1340   1.1  christos 	}
   1341   1.1  christos 	//UVMHIST_LOG(pmaphist, "<- true (pa %#"PRIxPADDR")", pa, 0,0,0);
   1342   1.1  christos 	return true;
   1343   1.1  christos }
   1344   1.1  christos 
   1345   1.1  christos /*
   1346   1.1  christos  *	Copy the range specified by src_addr/len
   1347   1.1  christos  *	from the source map to the range dst_addr/len
   1348   1.1  christos  *	in the destination map.
   1349   1.1  christos  *
   1350   1.1  christos  *	This routine is only advisory and need not do anything.
   1351   1.1  christos  */
   1352   1.1  christos void
   1353   1.1  christos pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vaddr_t dst_addr, vsize_t len,
   1354   1.1  christos     vaddr_t src_addr)
   1355   1.1  christos {
   1356   1.1  christos 
   1357   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1358   1.1  christos 	PMAP_COUNT(copy);
   1359   1.1  christos }
   1360   1.1  christos 
   1361   1.1  christos /*
   1362   1.1  christos  *	pmap_clear_reference:
   1363   1.1  christos  *
   1364   1.1  christos  *	Clear the reference bit on the specified physical page.
   1365   1.1  christos  */
   1366   1.1  christos bool
   1367   1.1  christos pmap_clear_reference(struct vm_page *pg)
   1368   1.1  christos {
   1369   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1370   1.1  christos 
   1371   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1372   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (pa %#"PRIxPADDR"))",
   1373   1.1  christos 	   pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1374   1.1  christos 
   1375   1.1  christos 	bool rv = pmap_page_clear_attributes(mdpg, VM_PAGEMD_REFERENCED);
   1376   1.1  christos 
   1377   1.1  christos 	UVMHIST_LOG(pmaphist, "<- %s", rv ? "true" : "false", 0,0,0);
   1378   1.1  christos 
   1379   1.1  christos 	return rv;
   1380   1.1  christos }
   1381   1.1  christos 
   1382   1.1  christos /*
   1383   1.1  christos  *	pmap_is_referenced:
   1384   1.1  christos  *
   1385   1.1  christos  *	Return whether or not the specified physical page is referenced
   1386   1.1  christos  *	by any physical maps.
   1387   1.1  christos  */
   1388   1.1  christos bool
   1389   1.1  christos pmap_is_referenced(struct vm_page *pg)
   1390   1.1  christos {
   1391   1.1  christos 
   1392   1.1  christos 	return VM_PAGEMD_REFERENCED_P(VM_PAGE_TO_MD(pg));
   1393   1.1  christos }
   1394   1.1  christos 
   1395   1.1  christos /*
   1396   1.1  christos  *	Clear the modify bits on the specified physical page.
   1397   1.1  christos  */
   1398   1.1  christos bool
   1399   1.1  christos pmap_clear_modify(struct vm_page *pg)
   1400   1.1  christos {
   1401   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1402   1.1  christos 	pv_entry_t pv = &mdpg->mdpg_first;
   1403   1.1  christos 	pv_entry_t pv_next;
   1404   1.1  christos 	uint16_t gen;
   1405   1.1  christos 
   1406   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1407   1.1  christos 	UVMHIST_LOG(pmaphist, "(pg=%p (%#"PRIxPADDR"))",
   1408   1.1  christos 	    pg, VM_PAGE_TO_PHYS(pg), 0,0);
   1409   1.1  christos 	PMAP_COUNT(clear_modify);
   1410   1.1  christos 
   1411   1.1  christos 	if (VM_PAGEMD_EXECPAGE_P(mdpg)) {
   1412   1.1  christos 		if (pv->pv_pmap == NULL) {
   1413   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1414   1.1  christos 			    "pg %p (pa %#"PRIxPADDR"): %s",
   1415   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg), "execpage cleared", 0);
   1416   1.1  christos 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1417   1.1  christos 			PMAP_COUNT(exec_uncached_clear_modify);
   1418   1.1  christos 		} else {
   1419   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1420   1.1  christos 			    "pg %p (pa %#"PRIxPADDR"): %s",
   1421   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg), "syncicache performed", 0);
   1422   1.1  christos 			pmap_page_syncicache(pg);
   1423   1.1  christos 			PMAP_COUNT(exec_synced_clear_modify);
   1424   1.1  christos 		}
   1425   1.1  christos 	}
   1426   1.1  christos 	if (!pmap_page_clear_attributes(mdpg, VM_PAGEMD_MODIFIED)) {
   1427   1.1  christos 		UVMHIST_LOG(pmaphist, "<- false", 0,0,0,0);
   1428   1.1  christos 		return false;
   1429   1.1  christos 	}
   1430   1.1  christos 	if (pv->pv_pmap == NULL) {
   1431   1.1  christos 		UVMHIST_LOG(pmaphist, "<- true (no mappings)", 0,0,0,0);
   1432   1.1  christos 		return true;
   1433   1.1  christos 	}
   1434   1.1  christos 
   1435   1.1  christos 	/*
   1436   1.1  christos 	 * remove write access from any pages that are dirty
   1437   1.1  christos 	 * so we can tell if they are written to again later.
   1438   1.1  christos 	 * flush the VAC first if there is one.
   1439   1.1  christos 	 */
   1440   1.1  christos 	kpreempt_disable();
   1441   1.1  christos 	gen = VM_PAGEMD_PVLIST_LOCK(mdpg, false);
   1442   1.1  christos 	for (; pv != NULL; pv = pv_next) {
   1443   1.1  christos 		pmap_t pmap = pv->pv_pmap;
   1444   1.1  christos 		vaddr_t va = pv->pv_va;
   1445   1.1  christos 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
   1446   1.1  christos 		KASSERT(ptep);
   1447   1.1  christos 		pv_next = pv->pv_next;
   1448   1.1  christos 		pt_entry_t pt_entry = pte_prot_nowrite(*ptep);
   1449   1.1  christos 		if (*ptep == pt_entry) {
   1450   1.1  christos 			continue;
   1451   1.1  christos 		}
   1452   1.1  christos 		pmap_md_vca_clean(pg, va, PMAP_WBINV);
   1453  1.10    nonaka 		pmap_md_tlb_miss_lock_enter();
   1454   1.1  christos 		*ptep = pt_entry;
   1455   1.1  christos 		VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1456   1.1  christos 		pmap_tlb_invalidate_addr(pmap, va);
   1457  1.10    nonaka 		pmap_md_tlb_miss_lock_exit();
   1458   1.1  christos 		pmap_update(pmap);
   1459   1.1  christos 		if (__predict_false(gen != VM_PAGEMD_PVLIST_LOCK(mdpg, false))) {
   1460   1.1  christos 			/*
   1461   1.1  christos 			 * The list changed!  So restart from the beginning.
   1462   1.1  christos 			 */
   1463   1.1  christos 			pv_next = &mdpg->mdpg_first;
   1464   1.1  christos 		}
   1465   1.1  christos 	}
   1466   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1467   1.1  christos 	kpreempt_enable();
   1468   1.1  christos 
   1469   1.1  christos 	UVMHIST_LOG(pmaphist, "<- true (mappings changed)", 0,0,0,0);
   1470   1.1  christos 	return true;
   1471   1.1  christos }
   1472   1.1  christos 
   1473   1.1  christos /*
   1474   1.1  christos  *	pmap_is_modified:
   1475   1.1  christos  *
   1476   1.1  christos  *	Return whether or not the specified physical page is modified
   1477   1.1  christos  *	by any physical maps.
   1478   1.1  christos  */
   1479   1.1  christos bool
   1480   1.1  christos pmap_is_modified(struct vm_page *pg)
   1481   1.1  christos {
   1482   1.1  christos 
   1483   1.1  christos 	return VM_PAGEMD_MODIFIED_P(VM_PAGE_TO_MD(pg));
   1484   1.1  christos }
   1485   1.1  christos 
   1486   1.1  christos /*
   1487   1.1  christos  *	pmap_set_modified:
   1488   1.1  christos  *
   1489   1.1  christos  *	Sets the page modified reference bit for the specified page.
   1490   1.1  christos  */
   1491   1.1  christos void
   1492   1.1  christos pmap_set_modified(paddr_t pa)
   1493   1.1  christos {
   1494   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1495   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1496   1.1  christos 	pmap_page_set_attributes(mdpg, VM_PAGEMD_MODIFIED|VM_PAGEMD_REFERENCED);
   1497   1.1  christos }
   1498   1.1  christos 
   1499   1.1  christos /******************** pv_entry management ********************/
   1500   1.1  christos 
   1501   1.1  christos static void
   1502   1.1  christos pmap_check_pvlist(struct vm_page *pg)
   1503   1.1  christos {
   1504   1.1  christos #ifdef PARANOIADIAG
   1505   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1506   1.1  christos 	pt_entry_t pv = &mdpg->mdpg_first;
   1507   1.1  christos 	if (pv->pv_pmap != NULL) {
   1508   1.1  christos 		for (; pv != NULL; pv = pv->pv_next) {
   1509   1.1  christos 			KASSERT(!pmap_md_direct_mapped_vaddr_p(pv->pv_va));
   1510   1.1  christos 		}
   1511   1.1  christos 	}
   1512   1.1  christos #endif /* PARANOIADIAG */
   1513   1.1  christos }
   1514   1.1  christos 
   1515   1.1  christos /*
   1516   1.1  christos  * Enter the pmap and virtual address into the
   1517   1.1  christos  * physical to virtual map table.
   1518   1.1  christos  */
   1519   1.1  christos void
   1520   1.1  christos pmap_enter_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, u_int *npte)
   1521   1.1  christos {
   1522   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1523   1.1  christos 	pv_entry_t pv, npv, apv;
   1524   1.1  christos 	int16_t gen;
   1525   1.4    martin 	bool first __unused = false;
   1526   1.1  christos 
   1527   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1528   1.1  christos 	UVMHIST_LOG(pmaphist,
   1529   1.1  christos 	    "(pmap=%p va=%#"PRIxVADDR" pg=%p (%#"PRIxPADDR")",
   1530   1.1  christos 	    pmap, va, pg, VM_PAGE_TO_PHYS(pg));
   1531   1.1  christos 	UVMHIST_LOG(pmaphist, "nptep=%p (%#x))", npte, *npte, 0, 0);
   1532   1.1  christos 
   1533   1.1  christos 	KASSERT(kpreempt_disabled());
   1534   1.1  christos 	KASSERT(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va));
   1535   1.1  christos 
   1536   1.1  christos 	apv = NULL;
   1537   1.1  christos 	pv = &mdpg->mdpg_first;
   1538   1.1  christos 	gen = VM_PAGEMD_PVLIST_LOCK(mdpg, true);
   1539   1.1  christos 	pmap_check_pvlist(pg);
   1540   1.1  christos again:
   1541   1.1  christos 	if (pv->pv_pmap == NULL) {
   1542   1.1  christos 		KASSERT(pv->pv_next == NULL);
   1543   1.1  christos 		/*
   1544   1.1  christos 		 * No entries yet, use header as the first entry
   1545   1.1  christos 		 */
   1546   1.1  christos 		PMAP_COUNT(primary_mappings);
   1547   1.1  christos 		PMAP_COUNT(mappings);
   1548   1.1  christos 		first = true;
   1549   1.1  christos #ifdef __PMAP_VIRTUAL_CACHE_ALIASES
   1550   1.1  christos 		pmap_page_clear_attributes(pg, VM_PAGEMD_UNCACHED);
   1551   1.1  christos #endif
   1552   1.1  christos 		pv->pv_pmap = pmap;
   1553   1.1  christos 		pv->pv_va = va;
   1554   1.1  christos 	} else {
   1555   1.1  christos 		if (pmap_md_vca_add(pg, va, npte))
   1556   1.1  christos 			goto again;
   1557   1.1  christos 
   1558   1.1  christos 		/*
   1559   1.1  christos 		 * There is at least one other VA mapping this page.
   1560   1.1  christos 		 * Place this entry after the header.
   1561   1.1  christos 		 *
   1562   1.1  christos 		 * Note: the entry may already be in the table if
   1563   1.1  christos 		 * we are only changing the protection bits.
   1564   1.1  christos 		 */
   1565   1.1  christos 
   1566   1.1  christos #ifdef PARANOIADIAG
   1567   1.1  christos 		const paddr_t pa = VM_PAGE_TO_PHYS(pg);
   1568   1.1  christos #endif
   1569   1.1  christos 		for (npv = pv; npv; npv = npv->pv_next) {
   1570   1.1  christos 			if (pmap == npv->pv_pmap && va == npv->pv_va) {
   1571   1.1  christos #ifdef PARANOIADIAG
   1572   1.1  christos 				pt_entry_t *ptep = pmap_pte_lookup(pmap, va);
   1573   1.1  christos 				pt_entry_t pt_entry = (ptep ? *ptep : 0);
   1574   1.1  christos 				if (!pte_valid_p(pt_entry)
   1575   1.1  christos 				    || pte_to_paddr(pt_entry) != pa)
   1576   1.1  christos 					printf(
   1577   1.1  christos 		"pmap_enter_pv: found va %#"PRIxVADDR" pa %#"PRIxPADDR" in pv_table but != %x\n",
   1578   1.1  christos 					    va, pa, pt_entry);
   1579   1.1  christos #endif
   1580   1.1  christos 				PMAP_COUNT(remappings);
   1581   1.1  christos 				VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1582   1.1  christos 				if (__predict_false(apv != NULL))
   1583   1.1  christos 					pmap_pv_free(apv);
   1584   1.1  christos 				return;
   1585   1.1  christos 			}
   1586   1.1  christos 		}
   1587   1.1  christos 		if (__predict_true(apv == NULL)) {
   1588   1.1  christos 			/*
   1589   1.1  christos 			 * To allocate a PV, we have to release the PVLIST lock
   1590   1.1  christos 			 * so get the page generation.  We allocate the PV, and
   1591   1.1  christos 			 * then reacquire the lock.
   1592   1.1  christos 			 */
   1593   1.1  christos 			VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1594   1.1  christos 
   1595   1.1  christos 			apv = (pv_entry_t)pmap_pv_alloc();
   1596   1.1  christos 			if (apv == NULL)
   1597   1.1  christos 				panic("pmap_enter_pv: pmap_pv_alloc() failed");
   1598   1.1  christos 
   1599   1.1  christos 			/*
   1600   1.1  christos 			 * If the generation has changed, then someone else
   1601   1.1  christos 			 * tinkered with this page so we should
   1602   1.1  christos 			 * start over.
   1603   1.1  christos 			 */
   1604   1.1  christos 			uint16_t oldgen = gen;
   1605   1.1  christos 			gen = VM_PAGEMD_PVLIST_LOCK(mdpg, true);
   1606   1.1  christos 			if (gen != oldgen)
   1607   1.1  christos 				goto again;
   1608   1.1  christos 		}
   1609   1.1  christos 		npv = apv;
   1610   1.1  christos 		apv = NULL;
   1611   1.1  christos 		npv->pv_va = va;
   1612   1.1  christos 		npv->pv_pmap = pmap;
   1613   1.1  christos 		npv->pv_next = pv->pv_next;
   1614   1.1  christos 		pv->pv_next = npv;
   1615   1.1  christos 		PMAP_COUNT(mappings);
   1616   1.1  christos 	}
   1617   1.1  christos 	pmap_check_pvlist(pg);
   1618   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1619   1.1  christos 	if (__predict_false(apv != NULL))
   1620   1.1  christos 		pmap_pv_free(apv);
   1621   1.1  christos 
   1622   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done pv=%p%s",
   1623   1.1  christos 	    pv, first ? " (first pv)" : "",0,0);
   1624   1.1  christos }
   1625   1.1  christos 
   1626   1.1  christos /*
   1627   1.1  christos  * Remove a physical to virtual address translation.
   1628   1.1  christos  * If cache was inhibited on this page, and there are no more cache
   1629   1.1  christos  * conflicts, restore caching.
   1630   1.1  christos  * Flush the cache if the last page is removed (should always be cached
   1631   1.1  christos  * at this point).
   1632   1.1  christos  */
   1633   1.1  christos void
   1634   1.1  christos pmap_remove_pv(pmap_t pmap, vaddr_t va, struct vm_page *pg, bool dirty)
   1635   1.1  christos {
   1636   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1637   1.1  christos 	pv_entry_t pv, npv;
   1638   1.1  christos 	bool last;
   1639   1.1  christos 
   1640   1.1  christos 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(pmaphist);
   1641   1.1  christos 	UVMHIST_LOG(pmaphist,
   1642   1.1  christos 	    "(pmap=%p va=%#"PRIxVADDR" pg=%p (pa %#"PRIxPADDR")\n",
   1643   1.1  christos 	    pmap, va, pg, VM_PAGE_TO_PHYS(pg));
   1644   1.1  christos 	UVMHIST_LOG(pmaphist, "dirty=%s)", dirty ? "true" : "false", 0,0,0);
   1645   1.1  christos 
   1646   1.1  christos 	KASSERT(kpreempt_disabled());
   1647   1.1  christos 	pv = &mdpg->mdpg_first;
   1648   1.1  christos 
   1649   1.1  christos 	(void)VM_PAGEMD_PVLIST_LOCK(mdpg, true);
   1650   1.1  christos 	pmap_check_pvlist(pg);
   1651   1.1  christos 
   1652   1.1  christos 	/*
   1653   1.1  christos 	 * If it is the first entry on the list, it is actually
   1654   1.1  christos 	 * in the header and we must copy the following entry up
   1655   1.1  christos 	 * to the header.  Otherwise we must search the list for
   1656   1.1  christos 	 * the entry.  In either case we free the now unused entry.
   1657   1.1  christos 	 */
   1658   1.1  christos 
   1659   1.1  christos 	last = false;
   1660   1.1  christos 	if (pmap == pv->pv_pmap && va == pv->pv_va) {
   1661   1.1  christos 		npv = pv->pv_next;
   1662   1.1  christos 		if (npv) {
   1663   1.1  christos 			*pv = *npv;
   1664   1.1  christos 			KASSERT(pv->pv_pmap != NULL);
   1665   1.1  christos 		} else {
   1666   1.1  christos #ifdef __PMAP_VIRTUAL_CACHE_ALIASES
   1667   1.1  christos 			pmap_page_clear_attributes(pg, VM_PAGEMD_UNCACHED);
   1668   1.1  christos #endif
   1669   1.1  christos 			pv->pv_pmap = NULL;
   1670   1.1  christos 			last = true;	/* Last mapping removed */
   1671   1.1  christos 		}
   1672   1.1  christos 		PMAP_COUNT(remove_pvfirst);
   1673   1.1  christos 	} else {
   1674   1.1  christos 		for (npv = pv->pv_next; npv; pv = npv, npv = npv->pv_next) {
   1675   1.1  christos 			PMAP_COUNT(remove_pvsearch);
   1676   1.1  christos 			if (pmap == npv->pv_pmap && va == npv->pv_va)
   1677   1.1  christos 				break;
   1678   1.1  christos 		}
   1679   1.1  christos 		if (npv) {
   1680   1.1  christos 			pv->pv_next = npv->pv_next;
   1681   1.1  christos 		}
   1682   1.1  christos 	}
   1683   1.1  christos 	pmap_md_vca_remove(pg, va);
   1684   1.1  christos 
   1685   1.1  christos 	pmap_check_pvlist(pg);
   1686   1.1  christos 	VM_PAGEMD_PVLIST_UNLOCK(mdpg);
   1687   1.1  christos 
   1688   1.1  christos 	/*
   1689   1.1  christos 	 * Free the pv_entry if needed.
   1690   1.1  christos 	 */
   1691   1.1  christos 	if (npv)
   1692   1.1  christos 		pmap_pv_free(npv);
   1693   1.1  christos 	if (VM_PAGEMD_EXECPAGE_P(mdpg) && dirty) {
   1694   1.1  christos 		if (last) {
   1695   1.1  christos 			/*
   1696   1.1  christos 			 * If this was the page's last mapping, we no longer
   1697   1.1  christos 			 * care about its execness.
   1698   1.1  christos 			 */
   1699   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1700   1.1  christos 			    "pg %p (pa %#"PRIxPADDR")%s: %s",
   1701   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg),
   1702   1.1  christos 			    last ? " [last mapping]" : "",
   1703   1.1  christos 			    "execpage cleared");
   1704   1.1  christos 			pmap_page_clear_attributes(mdpg, VM_PAGEMD_EXECPAGE);
   1705   1.1  christos 			PMAP_COUNT(exec_uncached_remove);
   1706   1.1  christos 		} else {
   1707   1.1  christos 			/*
   1708   1.1  christos 			 * Someone still has it mapped as an executable page
   1709   1.1  christos 			 * so we must sync it.
   1710   1.1  christos 			 */
   1711   1.1  christos 			UVMHIST_LOG(pmapexechist,
   1712   1.1  christos 			    "pg %p (pa %#"PRIxPADDR")%s: %s",
   1713   1.1  christos 			    pg, VM_PAGE_TO_PHYS(pg),
   1714   1.1  christos 			    last ? " [last mapping]" : "",
   1715   1.1  christos 			    "performed syncicache");
   1716   1.1  christos 			pmap_page_syncicache(pg);
   1717   1.1  christos 			PMAP_COUNT(exec_synced_remove);
   1718   1.1  christos 		}
   1719   1.1  christos 	}
   1720   1.1  christos 	UVMHIST_LOG(pmaphist, "<- done", 0,0,0,0);
   1721   1.1  christos }
   1722   1.1  christos 
   1723   1.1  christos #if defined(MULTIPROCESSOR)
   1724   1.1  christos struct pmap_pvlist_info {
   1725   1.1  christos 	kmutex_t *pli_locks[PAGE_SIZE / 32];
   1726   1.1  christos 	volatile u_int pli_lock_refs[PAGE_SIZE / 32];
   1727   1.1  christos 	volatile u_int pli_lock_index;
   1728   1.1  christos 	u_int pli_lock_mask;
   1729   1.1  christos } pmap_pvlist_info;
   1730   1.1  christos 
   1731   1.1  christos void
   1732   1.1  christos pmap_pvlist_lock_init(size_t cache_line_size)
   1733   1.1  christos {
   1734   1.1  christos 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   1735   1.1  christos 	const vaddr_t lock_page = uvm_pageboot_alloc(PAGE_SIZE);
   1736   1.1  christos 	vaddr_t lock_va = lock_page;
   1737   1.1  christos 	if (sizeof(kmutex_t) > cache_line_size) {
   1738   1.1  christos 		cache_line_size = roundup2(sizeof(kmutex_t), cache_line_size);
   1739   1.1  christos 	}
   1740   1.1  christos 	const size_t nlocks = PAGE_SIZE / cache_line_size;
   1741   1.1  christos 	KASSERT((nlocks & (nlocks - 1)) == 0);
   1742   1.1  christos 	/*
   1743   1.1  christos 	 * Now divide the page into a number of mutexes, one per cacheline.
   1744   1.1  christos 	 */
   1745   1.1  christos 	for (size_t i = 0; i < nlocks; lock_va += cache_line_size, i++) {
   1746   1.1  christos 		kmutex_t * const lock = (kmutex_t *)lock_va;
   1747   1.1  christos 		mutex_init(lock, MUTEX_DEFAULT, IPL_VM);
   1748   1.1  christos 		pli->pli_locks[i] = lock;
   1749   1.1  christos 	}
   1750   1.1  christos 	pli->pli_lock_mask = nlocks - 1;
   1751   1.1  christos }
   1752   1.1  christos 
   1753   1.1  christos uint16_t
   1754   1.1  christos pmap_pvlist_lock(struct vm_page_md *mdpg, bool list_change)
   1755   1.1  christos {
   1756   1.1  christos 	struct pmap_pvlist_info * const pli = &pmap_pvlist_info;
   1757   1.1  christos 	kmutex_t *lock = mdpg->mdpg_lock;
   1758   1.1  christos 	int16_t gen;
   1759   1.1  christos 
   1760   1.1  christos 	/*
   1761   1.1  christos 	 * Allocate a lock on an as-needed basis.  This will hopefully give us
   1762   1.1  christos 	 * semi-random distribution not based on page color.
   1763   1.1  christos 	 */
   1764   1.1  christos 	if (__predict_false(lock == NULL)) {
   1765   1.1  christos 		size_t locknum = atomic_add_int_nv(&pli->pli_lock_index, 37);
   1766   1.1  christos 		size_t lockid = locknum & pli->pli_lock_mask;
   1767   1.1  christos 		kmutex_t * const new_lock = pli->pli_locks[lockid];
   1768   1.1  christos 		/*
   1769   1.1  christos 		 * Set the lock.  If some other thread already did, just use
   1770   1.1  christos 		 * the one they assigned.
   1771   1.1  christos 		 */
   1772   1.1  christos 		lock = atomic_cas_ptr(&mdpg->mdpg_lock, NULL, new_lock);
   1773   1.1  christos 		if (lock == NULL) {
   1774   1.1  christos 			lock = new_lock;
   1775   1.1  christos 			atomic_inc_uint(&pli->pli_lock_refs[lockid]);
   1776   1.1  christos 		}
   1777   1.1  christos 	}
   1778   1.1  christos 
   1779   1.1  christos 	/*
   1780   1.1  christos 	 * Now finally lock the pvlists.
   1781   1.1  christos 	 */
   1782   1.1  christos 	mutex_spin_enter(lock);
   1783   1.1  christos 
   1784   1.1  christos 	/*
   1785   1.1  christos 	 * If the locker will be changing the list, increment the high 16 bits
   1786   1.1  christos 	 * of attrs so we use that as a generation number.
   1787   1.1  christos 	 */
   1788   1.1  christos 	gen = VM_PAGEMD_PVLIST_GEN(mdpg);		/* get old value */
   1789   1.1  christos 	if (list_change)
   1790   1.1  christos 		atomic_add_int(&mdpg->mdpg_attrs, 0x10000);
   1791   1.1  christos 
   1792   1.1  christos 	/*
   1793   1.1  christos 	 * Return the generation number.
   1794   1.1  christos 	 */
   1795   1.1  christos 	return gen;
   1796   1.1  christos }
   1797   1.1  christos #else /* !MULTIPROCESSOR */
   1798   1.1  christos void
   1799   1.1  christos pmap_pvlist_lock_init(size_t cache_line_size)
   1800   1.1  christos {
   1801   1.1  christos 	mutex_init(&pmap_pvlist_mutex, MUTEX_DEFAULT, IPL_VM);
   1802   1.1  christos }
   1803   1.1  christos 
   1804   1.1  christos #ifdef MODULAR
   1805   1.1  christos uint16_t
   1806   1.1  christos pmap_pvlist_lock(struct vm_page_md *mdpg, bool list_change)
   1807   1.1  christos {
   1808   1.1  christos 	/*
   1809   1.1  christos 	 * We just use a global lock.
   1810   1.1  christos 	 */
   1811   1.1  christos 	if (__predict_false(mdpg->mdpg_lock == NULL)) {
   1812   1.1  christos 		mdpg->mdpg_lock = &pmap_pvlist_mutex;
   1813   1.1  christos 	}
   1814   1.1  christos 
   1815   1.1  christos 	/*
   1816   1.1  christos 	 * Now finally lock the pvlists.
   1817   1.1  christos 	 */
   1818   1.1  christos 	mutex_spin_enter(mdpg->mdpg_lock);
   1819   1.1  christos 
   1820   1.1  christos 	return 0;
   1821   1.1  christos }
   1822   1.1  christos #endif /* MODULAR */
   1823   1.1  christos #endif /* !MULTIPROCESSOR */
   1824   1.1  christos 
   1825   1.1  christos /*
   1826   1.1  christos  * pmap_pv_page_alloc:
   1827   1.1  christos  *
   1828   1.1  christos  *	Allocate a page for the pv_entry pool.
   1829   1.1  christos  */
   1830   1.1  christos void *
   1831   1.1  christos pmap_pv_page_alloc(struct pool *pp, int flags)
   1832   1.1  christos {
   1833   1.1  christos 	struct vm_page *pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_USERESERVE);
   1834   1.1  christos 	if (pg == NULL)
   1835   1.1  christos 		return NULL;
   1836   1.1  christos 
   1837   1.1  christos 	return (void *)pmap_map_poolpage(VM_PAGE_TO_PHYS(pg));
   1838   1.1  christos }
   1839   1.1  christos 
   1840   1.1  christos /*
   1841   1.1  christos  * pmap_pv_page_free:
   1842   1.1  christos  *
   1843   1.1  christos  *	Free a pv_entry pool page.
   1844   1.1  christos  */
   1845   1.1  christos void
   1846   1.1  christos pmap_pv_page_free(struct pool *pp, void *v)
   1847   1.1  christos {
   1848   1.1  christos 	vaddr_t va = (vaddr_t)v;
   1849   1.1  christos 
   1850   1.1  christos 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   1851   1.1  christos 	const paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1852   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1853   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1854   1.1  christos 	pmap_md_vca_remove(pg, va);
   1855   1.1  christos 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   1856   1.1  christos 	uvm_pagefree(pg);
   1857   1.1  christos }
   1858   1.1  christos 
   1859   1.1  christos #ifdef PMAP_PREFER
   1860   1.1  christos /*
   1861   1.1  christos  * Find first virtual address >= *vap that doesn't cause
   1862   1.1  christos  * a cache alias conflict.
   1863   1.1  christos  */
   1864   1.1  christos void
   1865   1.1  christos pmap_prefer(vaddr_t foff, vaddr_t *vap, vsize_t sz, int td)
   1866   1.1  christos {
   1867   1.1  christos 	vaddr_t	va;
   1868   1.1  christos 	vsize_t d;
   1869   1.1  christos 	vsize_t prefer_mask = ptoa(uvmexp.colormask);
   1870   1.1  christos 
   1871   1.1  christos 	PMAP_COUNT(prefer_requests);
   1872   1.1  christos 
   1873   1.1  christos 	prefer_mask |= pmap_md_cache_prefer_mask();
   1874   1.1  christos 
   1875   1.1  christos 	if (prefer_mask) {
   1876   1.1  christos 		va = *vap;
   1877   1.1  christos 
   1878   1.1  christos 		d = foff - va;
   1879   1.1  christos 		d &= prefer_mask;
   1880   1.1  christos 		if (d) {
   1881   1.1  christos 			if (td)
   1882   1.1  christos 				*vap = trunc_page(va -((-d) & prefer_mask));
   1883   1.1  christos 			else
   1884   1.1  christos 				*vap = round_page(va + d);
   1885   1.1  christos 			PMAP_COUNT(prefer_adjustments);
   1886   1.1  christos 		}
   1887   1.1  christos 	}
   1888   1.1  christos }
   1889   1.1  christos #endif /* PMAP_PREFER */
   1890   1.1  christos 
   1891   1.1  christos #ifdef PMAP_MAP_POOLPAGE
   1892   1.1  christos vaddr_t
   1893   1.1  christos pmap_map_poolpage(paddr_t pa)
   1894   1.1  christos {
   1895   1.1  christos 
   1896   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1897   1.1  christos 	KASSERT(pg);
   1898   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1899   1.1  christos 	pmap_page_set_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   1900   1.1  christos 
   1901   1.1  christos 	const vaddr_t va = pmap_md_map_poolpage(pa, NBPG);
   1902   1.1  christos 	pmap_md_vca_add(pg, va, NULL);
   1903   1.1  christos 	return va;
   1904   1.1  christos }
   1905   1.1  christos 
   1906   1.1  christos paddr_t
   1907   1.1  christos pmap_unmap_poolpage(vaddr_t va)
   1908   1.1  christos {
   1909   1.1  christos 
   1910   1.1  christos 	KASSERT(pmap_md_direct_mapped_vaddr_p(va));
   1911   1.1  christos 	paddr_t pa = pmap_md_direct_mapped_vaddr_to_paddr(va);
   1912   1.1  christos 
   1913   1.1  christos 	struct vm_page * const pg = PHYS_TO_VM_PAGE(pa);
   1914   1.1  christos 	KASSERT(pg);
   1915   1.1  christos 	struct vm_page_md * const mdpg = VM_PAGE_TO_MD(pg);
   1916   1.1  christos 	pmap_page_clear_attributes(mdpg, VM_PAGEMD_POOLPAGE);
   1917   1.1  christos 	pmap_md_unmap_poolpage(va, NBPG);
   1918   1.1  christos 	pmap_md_vca_remove(pg, va);
   1919   1.1  christos 
   1920   1.1  christos 	return pa;
   1921   1.1  christos }
   1922   1.1  christos #endif /* PMAP_MAP_POOLPAGE */
   1923