Home | History | Annotate | Line # | Download | only in pmap
pmap_segtab.c revision 1.18
      1  1.18    simonb /*	$NetBSD: pmap_segtab.c,v 1.18 2020/08/18 11:48:21 simonb 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.18    simonb __KERNEL_RCSID(0, "$NetBSD: pmap_segtab.c,v 1.18 2020/08/18 11:48:21 simonb 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 #define __PMAP_PRIVATE
     99   1.1  christos 
    100   1.1  christos #include "opt_multiprocessor.h"
    101   1.1  christos 
    102   1.1  christos #include <sys/param.h>
    103  1.13     skrll 
    104  1.13     skrll #include <sys/atomic.h>
    105  1.13     skrll #include <sys/mutex.h>
    106  1.13     skrll #include <sys/proc.h>
    107   1.1  christos #include <sys/systm.h>
    108   1.1  christos 
    109   1.1  christos #include <uvm/uvm.h>
    110   1.1  christos 
    111   1.1  christos CTASSERT(NBPG >= sizeof(pmap_segtab_t));
    112   1.1  christos 
    113   1.1  christos struct pmap_segtab_info {
    114   1.1  christos 	pmap_segtab_t *free_segtab;	/* free list kept locally */
    115   1.1  christos #ifdef DEBUG
    116   1.1  christos 	uint32_t nget_segtab;
    117   1.1  christos 	uint32_t nput_segtab;
    118   1.1  christos 	uint32_t npage_segtab;
    119   1.1  christos #define	SEGTAB_ADD(n, v)	(pmap_segtab_info.n ## _segtab += (v))
    120   1.1  christos #else
    121   1.1  christos #define	SEGTAB_ADD(n, v)	((void) 0)
    122   1.1  christos #endif
    123   1.1  christos #ifdef PMAP_PTP_CACHE
    124   1.1  christos 	struct pgflist ptp_pgflist;	/* Keep a list of idle page tables. */
    125   1.1  christos #endif
    126   1.1  christos } pmap_segtab_info = {
    127   1.1  christos #ifdef PMAP_PTP_CACHE
    128   1.1  christos 	.ptp_pgflist = LIST_HEAD_INITIALIZER(pmap_segtab_info.ptp_pgflist),
    129   1.1  christos #endif
    130   1.1  christos };
    131   1.1  christos 
    132   1.1  christos kmutex_t pmap_segtab_lock __cacheline_aligned;
    133   1.1  christos 
    134   1.4       mrg static void
    135   1.4       mrg pmap_check_stp(pmap_segtab_t *stp, const char *caller, const char *why)
    136   1.4       mrg {
    137   1.4       mrg #ifdef DEBUG
    138   1.4       mrg 	for (size_t i = 0; i < PMAP_SEGTABSIZE; i++) {
    139   1.4       mrg 		if (stp->seg_tab[i] != 0) {
    140   1.4       mrg #ifdef DEBUG_NOISY
    141   1.4       mrg 			for (size_t j = i; j < PMAP_SEGTABSIZE; j++)
    142  1.14       rin 				printf("%s: pm_segtab.seg_tab[%zu] = %p\n",
    143  1.11     skrll 				    caller, j, stp->seg_tab[j]);
    144   1.4       mrg #endif
    145  1.14       rin 			panic("%s: pm_segtab.seg_tab[%zu] != 0 (%p): %s",
    146  1.11     skrll 			    caller, i, stp->seg_tab[i], why);
    147   1.4       mrg 		}
    148   1.4       mrg 	}
    149   1.4       mrg #endif
    150   1.4       mrg }
    151   1.4       mrg 
    152   1.1  christos static inline struct vm_page *
    153   1.1  christos pmap_pte_pagealloc(void)
    154   1.1  christos {
    155   1.1  christos 	struct vm_page *pg;
    156   1.1  christos 
    157   1.2      matt 	pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_ZERO|UVM_PGA_USERESERVE);
    158   1.1  christos 	if (pg) {
    159   1.1  christos #ifdef UVM_PAGE_TRKOWN
    160   1.1  christos 		pg->owner_tag = NULL;
    161   1.1  christos #endif
    162   1.1  christos 		UVM_PAGE_OWN(pg, "pmap-ptp");
    163   1.1  christos 	}
    164   1.1  christos 
    165   1.1  christos 	return pg;
    166   1.1  christos }
    167   1.1  christos 
    168   1.1  christos static inline pt_entry_t *
    169   1.1  christos pmap_segmap(struct pmap *pmap, vaddr_t va)
    170   1.1  christos {
    171   1.1  christos 	pmap_segtab_t *stp = pmap->pm_segtab;
    172   1.6     skrll 	KASSERTMSG(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va),
    173   1.6     skrll 	    "pmap %p va %#" PRIxVADDR, pmap, va);
    174   1.1  christos #ifdef _LP64
    175   1.1  christos 	stp = stp->seg_seg[(va >> XSEGSHIFT) & (NSEGPG - 1)];
    176   1.1  christos 	if (stp == NULL)
    177   1.1  christos 		return NULL;
    178   1.1  christos #endif
    179   1.1  christos 
    180   1.1  christos 	return stp->seg_tab[(va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1)];
    181   1.1  christos }
    182   1.1  christos 
    183   1.1  christos pt_entry_t *
    184   1.1  christos pmap_pte_lookup(pmap_t pmap, vaddr_t va)
    185   1.1  christos {
    186   1.1  christos 	pt_entry_t *pte = pmap_segmap(pmap, va);
    187   1.1  christos 	if (pte == NULL)
    188   1.1  christos 		return NULL;
    189   1.1  christos 
    190   1.1  christos 	return pte + ((va >> PGSHIFT) & (NPTEPG - 1));
    191   1.1  christos }
    192   1.1  christos 
    193   1.1  christos static void
    194   1.1  christos pmap_segtab_free(pmap_segtab_t *stp)
    195   1.1  christos {
    196   1.1  christos 	/*
    197   1.7   msaitoh 	 * Insert the segtab into the segtab freelist.
    198   1.1  christos 	 */
    199   1.1  christos 	mutex_spin_enter(&pmap_segtab_lock);
    200   1.1  christos 	stp->seg_seg[0] = pmap_segtab_info.free_segtab;
    201   1.1  christos 	pmap_segtab_info.free_segtab = stp;
    202   1.1  christos 	SEGTAB_ADD(nput, 1);
    203   1.1  christos 	mutex_spin_exit(&pmap_segtab_lock);
    204   1.1  christos }
    205   1.1  christos 
    206   1.1  christos static void
    207   1.1  christos pmap_segtab_release(pmap_t pmap, pmap_segtab_t **stp_p, bool free_stp,
    208   1.1  christos 	pte_callback_t callback, uintptr_t flags,
    209   1.1  christos 	vaddr_t va, vsize_t vinc)
    210   1.1  christos {
    211   1.1  christos 	pmap_segtab_t *stp = *stp_p;
    212   1.1  christos 
    213  1.16       mrg 	UVMHIST_FUNC(__func__);
    214  1.16       mrg 	UVMHIST_CALLARGS(pmaphist, "pm=%#jx stpp=%#jx free=%jd",
    215  1.16       mrg 	    (uintptr_t)pmap, (uintptr_t)stp_p, free_stp, 0);
    216  1.16       mrg 	UVMHIST_LOG(pmaphist, " callback=%jx flags=%jx va=%jx vinc=%jx",
    217  1.16       mrg 	    (uintptr_t)callback, flags, (uintptr_t)va, (uintptr_t)vinc);
    218   1.4       mrg 	for (size_t i = (va / vinc) & (PMAP_SEGTABSIZE - 1);
    219   1.4       mrg 	     i < PMAP_SEGTABSIZE;
    220   1.4       mrg 	     i++, va += vinc) {
    221   1.1  christos #ifdef _LP64
    222   1.1  christos 		if (vinc > NBSEG) {
    223   1.1  christos 			if (stp->seg_seg[i] != NULL) {
    224  1.16       mrg 				UVMHIST_LOG(pmaphist, " recursing", 0, 0, 0, 0);
    225   1.1  christos 				pmap_segtab_release(pmap, &stp->seg_seg[i],
    226   1.1  christos 				    true, callback, flags, va, vinc / NSEGPG);
    227   1.1  christos 				KASSERT(stp->seg_seg[i] == NULL);
    228   1.1  christos 			}
    229   1.1  christos 			continue;
    230   1.1  christos 		}
    231   1.1  christos #endif
    232   1.1  christos 		KASSERT(vinc == NBSEG);
    233   1.1  christos 
    234   1.1  christos 		/* get pointer to segment map */
    235   1.1  christos 		pt_entry_t *pte = stp->seg_tab[i];
    236   1.1  christos 		if (pte == NULL)
    237   1.1  christos 			continue;
    238   1.1  christos 
    239   1.1  christos 		/*
    240  1.18    simonb 		 * If our caller wants a callback, do so.
    241   1.1  christos 		 */
    242   1.1  christos 		if (callback != NULL) {
    243   1.1  christos 			(*callback)(pmap, va, va + vinc, pte, flags);
    244   1.1  christos 		}
    245   1.1  christos #ifdef DEBUG
    246   1.1  christos 		for (size_t j = 0; j < NPTEPG; j++) {
    247   1.2      matt 			if (!pte_zero_p(pte[j]))
    248  1.17     skrll 				panic("%s: pte entry at %p not 0 (%#"PRIxPTE")",
    249   1.2      matt 				    __func__, &pte[j], pte_value(pte[j]));
    250   1.1  christos 		}
    251   1.1  christos #endif
    252   1.2      matt 		// PMAP_UNMAP_POOLPAGE should handle any VCA issues itself
    253   1.1  christos 		paddr_t pa = PMAP_UNMAP_POOLPAGE((vaddr_t)pte);
    254   1.1  christos 		struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
    255   1.1  christos #ifdef PMAP_PTP_CACHE
    256   1.1  christos 		mutex_spin_enter(&pmap_segtab_lock);
    257  1.12        ad 		LIST_INSERT_HEAD(&pmap_segtab_info.ptp_pgflist, pg, pageq.list);
    258   1.1  christos 		mutex_spin_exit(&pmap_segtab_lock);
    259   1.1  christos #else
    260   1.1  christos 		uvm_pagefree(pg);
    261   1.1  christos #endif
    262   1.1  christos 
    263   1.1  christos 		stp->seg_tab[i] = NULL;
    264  1.16       mrg 		UVMHIST_LOG(pmaphist, " zeroing tab[%jd]", i, 0, 0, 0);
    265   1.1  christos 	}
    266   1.1  christos 
    267   1.1  christos 	if (free_stp) {
    268   1.5     skrll 		pmap_check_stp(stp, __func__,
    269   1.4       mrg 			       vinc == NBSEG ? "release seg" : "release xseg");
    270   1.1  christos 		pmap_segtab_free(stp);
    271   1.1  christos 		*stp_p = NULL;
    272   1.1  christos 	}
    273   1.1  christos }
    274   1.1  christos 
    275   1.1  christos /*
    276   1.1  christos  *	Create and return a physical map.
    277   1.1  christos  *
    278   1.1  christos  *	If the size specified for the map
    279   1.1  christos  *	is zero, the map is an actual physical
    280   1.1  christos  *	map, and may be referenced by the
    281   1.1  christos  *	hardware.
    282   1.1  christos  *
    283   1.1  christos  *	If the size specified is non-zero,
    284   1.1  christos  *	the map will be used in software only, and
    285   1.1  christos  *	is bounded by that size.
    286   1.1  christos  */
    287   1.1  christos static pmap_segtab_t *
    288   1.1  christos pmap_segtab_alloc(void)
    289   1.1  christos {
    290   1.1  christos 	pmap_segtab_t *stp;
    291   1.4       mrg 	bool found_on_freelist = false;
    292   1.1  christos 
    293   1.1  christos  again:
    294   1.1  christos 	mutex_spin_enter(&pmap_segtab_lock);
    295   1.1  christos 	if (__predict_true((stp = pmap_segtab_info.free_segtab) != NULL)) {
    296   1.1  christos 		pmap_segtab_info.free_segtab = stp->seg_seg[0];
    297   1.1  christos 		stp->seg_seg[0] = NULL;
    298   1.1  christos 		SEGTAB_ADD(nget, 1);
    299   1.4       mrg 		found_on_freelist = true;
    300   1.1  christos 	}
    301   1.1  christos 	mutex_spin_exit(&pmap_segtab_lock);
    302   1.1  christos 
    303   1.1  christos 	if (__predict_false(stp == NULL)) {
    304   1.1  christos 		struct vm_page * const stp_pg = pmap_pte_pagealloc();
    305   1.1  christos 
    306   1.1  christos 		if (__predict_false(stp_pg == NULL)) {
    307   1.1  christos 			/*
    308   1.1  christos 			 * XXX What else can we do?  Could we deadlock here?
    309   1.1  christos 			 */
    310  1.10     skrll 			uvm_wait("segtab");
    311   1.1  christos 			goto again;
    312   1.1  christos 		}
    313   1.1  christos 		SEGTAB_ADD(npage, 1);
    314   1.1  christos 		const paddr_t stp_pa = VM_PAGE_TO_PHYS(stp_pg);
    315   1.1  christos 
    316   1.1  christos 		stp = (pmap_segtab_t *)PMAP_MAP_POOLPAGE(stp_pa);
    317   1.1  christos 		const size_t n = NBPG / sizeof(*stp);
    318   1.1  christos 		if (n > 1) {
    319   1.1  christos 			/*
    320   1.1  christos 			 * link all the segtabs in this page together
    321   1.1  christos 			 */
    322   1.1  christos 			for (size_t i = 1; i < n - 1; i++) {
    323   1.1  christos 				stp[i].seg_seg[0] = &stp[i+1];
    324   1.1  christos 			}
    325   1.1  christos 			/*
    326   1.1  christos 			 * Now link the new segtabs into the free segtab list.
    327   1.1  christos 			 */
    328   1.1  christos 			mutex_spin_enter(&pmap_segtab_lock);
    329   1.1  christos 			stp[n-1].seg_seg[0] = pmap_segtab_info.free_segtab;
    330   1.1  christos 			pmap_segtab_info.free_segtab = stp + 1;
    331   1.1  christos 			SEGTAB_ADD(nput, n - 1);
    332   1.1  christos 			mutex_spin_exit(&pmap_segtab_lock);
    333   1.1  christos 		}
    334   1.1  christos 	}
    335   1.1  christos 
    336   1.4       mrg 	pmap_check_stp(stp, __func__,
    337   1.4       mrg 		       found_on_freelist ? "from free list" : "allocated");
    338   1.4       mrg 
    339   1.1  christos 	return stp;
    340   1.1  christos }
    341   1.1  christos 
    342   1.1  christos /*
    343   1.1  christos  * Allocate the top segment table for the pmap.
    344   1.1  christos  */
    345   1.1  christos void
    346   1.1  christos pmap_segtab_init(pmap_t pmap)
    347   1.1  christos {
    348   1.1  christos 
    349   1.1  christos 	pmap->pm_segtab = pmap_segtab_alloc();
    350   1.1  christos }
    351   1.1  christos 
    352   1.1  christos /*
    353   1.1  christos  *	Retire the given physical map from service.
    354   1.1  christos  *	Should only be called if the map contains
    355   1.1  christos  *	no valid mappings.
    356   1.1  christos  */
    357   1.1  christos void
    358   1.1  christos pmap_segtab_destroy(pmap_t pmap, pte_callback_t func, uintptr_t flags)
    359   1.1  christos {
    360   1.1  christos 	if (pmap->pm_segtab == NULL)
    361   1.1  christos 		return;
    362   1.1  christos 
    363   1.1  christos #ifdef _LP64
    364   1.1  christos 	const vsize_t vinc = NBXSEG;
    365   1.1  christos #else
    366   1.1  christos 	const vsize_t vinc = NBSEG;
    367   1.1  christos #endif
    368   1.1  christos 	pmap_segtab_release(pmap, &pmap->pm_segtab,
    369   1.1  christos 	    func == NULL, func, flags, pmap->pm_minaddr, vinc);
    370   1.1  christos }
    371   1.1  christos 
    372   1.1  christos /*
    373   1.1  christos  *	Make a new pmap (vmspace) active for the given process.
    374   1.1  christos  */
    375   1.1  christos void
    376   1.1  christos pmap_segtab_activate(struct pmap *pm, struct lwp *l)
    377   1.1  christos {
    378   1.1  christos 	if (l == curlwp) {
    379   1.3      matt 		struct cpu_info * const ci = l->l_cpu;
    380  1.15     skrll 		pmap_md_xtab_activate(pm, l);
    381   1.1  christos 		KASSERT(pm == l->l_proc->p_vmspace->vm_map.pmap);
    382   1.1  christos 		if (pm == pmap_kernel()) {
    383   1.3      matt 			ci->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    384   1.1  christos #ifdef _LP64
    385   1.3      matt 			ci->ci_pmap_user_seg0tab = PMAP_INVALID_SEGTAB_ADDRESS;
    386   1.1  christos #endif
    387   1.1  christos 		} else {
    388   1.3      matt 			ci->ci_pmap_user_segtab = pm->pm_segtab;
    389   1.1  christos #ifdef _LP64
    390   1.3      matt 			ci->ci_pmap_user_seg0tab = pm->pm_segtab->seg_seg[0];
    391   1.1  christos #endif
    392   1.1  christos 		}
    393   1.1  christos 	}
    394   1.1  christos }
    395   1.1  christos 
    396  1.15     skrll 
    397  1.15     skrll void
    398  1.15     skrll pmap_segtab_deactivate(pmap_t pm)
    399  1.15     skrll {
    400  1.15     skrll 
    401  1.15     skrll 	pmap_md_xtab_deactivate(pm);
    402  1.15     skrll 
    403  1.15     skrll 	curcpu()->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    404  1.15     skrll #ifdef _LP64
    405  1.15     skrll 	curcpu()->ci_pmap_user_seg0tab = NULL;
    406  1.15     skrll #endif
    407  1.15     skrll 
    408  1.15     skrll }
    409  1.15     skrll 
    410   1.1  christos /*
    411   1.1  christos  *	Act on the given range of addresses from the specified map.
    412   1.1  christos  *
    413   1.1  christos  *	It is assumed that the start and end are properly rounded to
    414   1.1  christos  *	the page size.
    415   1.1  christos  */
    416   1.1  christos void
    417   1.1  christos pmap_pte_process(pmap_t pmap, vaddr_t sva, vaddr_t eva,
    418   1.8     skrll     pte_callback_t callback, uintptr_t flags)
    419   1.1  christos {
    420   1.1  christos #if 0
    421   1.1  christos 	printf("%s: %p, %"PRIxVADDR", %"PRIxVADDR", %p, %"PRIxPTR"\n",
    422   1.1  christos 	    __func__, pmap, sva, eva, callback, flags);
    423   1.1  christos #endif
    424   1.1  christos 	while (sva < eva) {
    425   1.1  christos 		vaddr_t lastseg_va = pmap_trunc_seg(sva) + NBSEG;
    426   1.1  christos 		if (lastseg_va == 0 || lastseg_va > eva)
    427   1.1  christos 			lastseg_va = eva;
    428   1.1  christos 
    429   1.1  christos 		/*
    430   1.1  christos 		 * If VA belongs to an unallocated segment,
    431   1.1  christos 		 * skip to the next segment boundary.
    432   1.1  christos 		 */
    433   1.9     skrll 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, sva);
    434   1.9     skrll 		if (ptep != NULL) {
    435   1.1  christos 			/*
    436   1.1  christos 			 * Callback to deal with the ptes for this segment.
    437   1.1  christos 			 */
    438   1.9     skrll 			(*callback)(pmap, sva, lastseg_va, ptep, flags);
    439   1.1  christos 		}
    440   1.1  christos 		/*
    441   1.1  christos 		 * In theory we could release pages with no entries,
    442   1.1  christos 		 * but that takes more effort than we want here.
    443   1.1  christos 		 */
    444   1.1  christos 		sva = lastseg_va;
    445   1.1  christos 	}
    446   1.1  christos }
    447   1.1  christos 
    448   1.1  christos /*
    449   1.1  christos  *	Return a pointer for the pte that corresponds to the specified virtual
    450   1.1  christos  *	address (va) in the target physical map, allocating if needed.
    451   1.1  christos  */
    452   1.1  christos pt_entry_t *
    453   1.1  christos pmap_pte_reserve(pmap_t pmap, vaddr_t va, int flags)
    454   1.1  christos {
    455   1.1  christos 	pmap_segtab_t *stp = pmap->pm_segtab;
    456   1.1  christos 	pt_entry_t *pte;
    457   1.1  christos 
    458  1.16       mrg 	UVMHIST_FUNC(__func__);
    459  1.16       mrg 	UVMHIST_CALLARGS(pmaphist, "pm=%#jx va=%#jx flags=%jx",
    460  1.16       mrg 	    (uintptr_t)pmap, (uintptr_t)va, flags, 0);
    461  1.16       mrg 
    462   1.1  christos 	pte = pmap_pte_lookup(pmap, va);
    463   1.1  christos 	if (__predict_false(pte == NULL)) {
    464   1.1  christos #ifdef _LP64
    465   1.1  christos 		pmap_segtab_t ** const stp_p =
    466   1.1  christos 		    &stp->seg_seg[(va >> XSEGSHIFT) & (NSEGPG - 1)];
    467   1.1  christos 		if (__predict_false((stp = *stp_p) == NULL)) {
    468   1.1  christos 			pmap_segtab_t *nstp = pmap_segtab_alloc();
    469   1.1  christos #ifdef MULTIPROCESSOR
    470   1.1  christos 			pmap_segtab_t *ostp = atomic_cas_ptr(stp_p, NULL, nstp);
    471   1.1  christos 			if (__predict_false(ostp != NULL)) {
    472   1.4       mrg 				pmap_check_stp(nstp, __func__, "reserve");
    473   1.1  christos 				pmap_segtab_free(nstp);
    474   1.1  christos 				nstp = ostp;
    475   1.1  christos 			}
    476   1.1  christos #else
    477   1.1  christos 			*stp_p = nstp;
    478   1.1  christos #endif /* MULTIPROCESSOR */
    479   1.1  christos 			stp = nstp;
    480   1.1  christos 		}
    481   1.1  christos 		KASSERT(stp == pmap->pm_segtab->seg_seg[(va >> XSEGSHIFT) & (NSEGPG - 1)]);
    482   1.1  christos #endif /* _LP64 */
    483   1.1  christos 		struct vm_page *pg = NULL;
    484   1.1  christos #ifdef PMAP_PTP_CACHE
    485   1.1  christos 		mutex_spin_enter(&pmap_segtab_lock);
    486   1.1  christos 		if ((pg = LIST_FIRST(&pmap_segtab_info.ptp_pgflist)) != NULL) {
    487  1.12        ad 			LIST_REMOVE(pg, pageq.list);
    488   1.1  christos 			KASSERT(LIST_FIRST(&pmap_segtab_info.ptp_pgflist) != pg);
    489   1.1  christos 		}
    490   1.1  christos 		mutex_spin_exit(&pmap_segtab_lock);
    491   1.1  christos #endif
    492   1.1  christos 		if (pg == NULL)
    493   1.1  christos 			pg = pmap_pte_pagealloc();
    494   1.1  christos 		if (pg == NULL) {
    495   1.1  christos 			if (flags & PMAP_CANFAIL)
    496   1.1  christos 				return NULL;
    497   1.1  christos 			panic("%s: cannot allocate page table page "
    498   1.1  christos 			    "for va %" PRIxVADDR, __func__, va);
    499   1.1  christos 		}
    500   1.1  christos 
    501   1.1  christos 		const paddr_t pa = VM_PAGE_TO_PHYS(pg);
    502   1.2      matt 		pte = (pt_entry_t *)PMAP_MAP_POOLPAGE(pa);
    503   1.1  christos 		pt_entry_t ** const pte_p =
    504   1.1  christos 		    &stp->seg_tab[(va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1)];
    505   1.1  christos #ifdef MULTIPROCESSOR
    506   1.1  christos 		pt_entry_t *opte = atomic_cas_ptr(pte_p, NULL, pte);
    507   1.1  christos 		/*
    508   1.1  christos 		 * If another thread allocated the segtab needed for this va
    509   1.1  christos 		 * free the page we just allocated.
    510   1.1  christos 		 */
    511   1.1  christos 		if (__predict_false(opte != NULL)) {
    512   1.1  christos #ifdef PMAP_PTP_CACHE
    513   1.1  christos 			mutex_spin_enter(&pmap_segtab_lock);
    514   1.1  christos 			LIST_INSERT_HEAD(&pmap_segtab_info.ptp_pgflist,
    515  1.12        ad 			    pg, pageq.list);
    516   1.1  christos 			mutex_spin_exit(&pmap_segtab_lock);
    517   1.1  christos #else
    518   1.2      matt 			PMAP_UNMAP_POOLPAGE((vaddr_t)pte);
    519   1.1  christos 			uvm_pagefree(pg);
    520   1.1  christos #endif
    521   1.1  christos 			pte = opte;
    522   1.1  christos 		}
    523   1.1  christos #else
    524   1.1  christos 		*pte_p = pte;
    525   1.1  christos #endif
    526   1.1  christos 		KASSERT(pte == stp->seg_tab[(va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1)]);
    527  1.16       mrg 		UVMHIST_LOG(pmaphist, " set tab[%jd]=%jx",
    528  1.16       mrg 		    (va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1), pte, 0, 0);
    529   1.1  christos 
    530   1.2      matt #ifdef DEBUG
    531   1.1  christos 		for (size_t i = 0; i < NPTEPG; i++) {
    532   1.2      matt 			if (!pte_zero_p(pte[i]))
    533   1.2      matt 				panic("%s: new segmap %p not empty @ %zu",
    534   1.2      matt 				    __func__, pte, i);
    535   1.1  christos 		}
    536   1.1  christos #endif
    537   1.2      matt 		pte += (va >> PGSHIFT) & (NPTEPG - 1);
    538   1.1  christos 	}
    539   1.1  christos 
    540   1.1  christos 	return pte;
    541   1.1  christos }
    542