Home | History | Annotate | Line # | Download | only in pmap
pmap_segtab.c revision 1.19
      1 /*	$NetBSD: pmap_segtab.c,v 1.19 2020/08/20 05:54:32 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center and by Chris G. Demetriou.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1992, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * This code is derived from software contributed to Berkeley by
     38  * the Systems Programming Group of the University of Utah Computer
     39  * Science Department and Ralph Campbell.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)pmap.c	8.4 (Berkeley) 1/26/94
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 
     70 __KERNEL_RCSID(0, "$NetBSD: pmap_segtab.c,v 1.19 2020/08/20 05:54:32 mrg Exp $");
     71 
     72 /*
     73  *	Manages physical address maps.
     74  *
     75  *	In addition to hardware address maps, this
     76  *	module is called upon to provide software-use-only
     77  *	maps which may or may not be stored in the same
     78  *	form as hardware maps.  These pseudo-maps are
     79  *	used to store intermediate results from copy
     80  *	operations to and from address spaces.
     81  *
     82  *	Since the information managed by this module is
     83  *	also stored by the logical address mapping module,
     84  *	this module may throw away valid virtual-to-physical
     85  *	mappings at almost any time.  However, invalidations
     86  *	of virtual-to-physical mappings must be done as
     87  *	requested.
     88  *
     89  *	In order to cope with hardware architectures which
     90  *	make virtual-to-physical map invalidates expensive,
     91  *	this module may delay invalidate or reduced protection
     92  *	operations until such time as they are actually
     93  *	necessary.  This module is given full information as
     94  *	to which processors are currently using which maps,
     95  *	and to when physical maps must be made correct.
     96  */
     97 
     98 #define __PMAP_PRIVATE
     99 
    100 #include "opt_multiprocessor.h"
    101 
    102 #include <sys/param.h>
    103 
    104 #include <sys/atomic.h>
    105 #include <sys/mutex.h>
    106 #include <sys/proc.h>
    107 #include <sys/systm.h>
    108 
    109 #include <uvm/uvm.h>
    110 
    111 CTASSERT(NBPG >= sizeof(pmap_segtab_t));
    112 
    113 struct pmap_segtab_info {
    114 	pmap_segtab_t *free_segtab;	/* free list kept locally */
    115 #ifdef DEBUG
    116 	uint32_t nget_segtab;
    117 	uint32_t nput_segtab;
    118 	uint32_t npage_segtab;
    119 #define	SEGTAB_ADD(n, v)	(pmap_segtab_info.n ## _segtab += (v))
    120 #else
    121 #define	SEGTAB_ADD(n, v)	((void) 0)
    122 #endif
    123 #ifdef PMAP_PTP_CACHE
    124 	struct pgflist ptp_pgflist;	/* Keep a list of idle page tables. */
    125 #endif
    126 } pmap_segtab_info = {
    127 #ifdef PMAP_PTP_CACHE
    128 	.ptp_pgflist = LIST_HEAD_INITIALIZER(pmap_segtab_info.ptp_pgflist),
    129 #endif
    130 };
    131 
    132 kmutex_t pmap_segtab_lock __cacheline_aligned;
    133 
    134 /*
    135  * Check that a seg_tab[] array is empty.
    136  *
    137  * This is used when allocating or freeing a pmap_segtab_t.  The stp
    138  * should be unused -- meaning, none of the seg_tab[] pointers are
    139  * not NULL, as it transitions from either freshly allocated segtab from
    140  * pmap pool, an unused allocated page segtab alloc from the SMP case,
    141  * where two CPUs attempt to allocate the same underlying segtab, the
    142  * release of a segtab entry to the freelist, or for SMP, where reserve
    143  * also frees a freshly allocated but unused entry.
    144  */
    145 static void
    146 pmap_check_stp(pmap_segtab_t *stp, const char *caller, const char *why)
    147 {
    148 #ifdef DEBUG
    149 	for (size_t i = 0; i < PMAP_SEGTABSIZE; i++) {
    150 		if (stp->seg_tab[i] != NULL) {
    151 #define DEBUG_NOISY
    152 #ifdef DEBUG_NOISY
    153 			UVMHIST_FUNC(__func__);
    154 			UVMHIST_CALLARGS(pmapsegtabhist, "stp=%#jx",
    155 			    (uintptr_t)stp, 0, 0, 0);
    156 			for (size_t j = i; j < PMAP_SEGTABSIZE; j++)
    157 				if (stp->seg_tab[j] != NULL)
    158 					printf("%s: stp->seg_tab[%zu] = %p\n",
    159 					    caller, j, stp->seg_tab[j]);
    160 #endif
    161 			panic("%s: pm_segtab.seg_tab[%zu] != 0 (%p): %s",
    162 			    caller, i, stp->seg_tab[i], why);
    163 		}
    164 	}
    165 #endif
    166 }
    167 
    168 /*
    169  * Check that an array of ptes is actually zero.
    170  */
    171 static void
    172 pmap_check_ptes(pt_entry_t *pte, const char *caller)
    173 {
    174 #ifdef DEBUG
    175 	for (size_t i = 0; i < NPTEPG; i++)
    176 		if (!pte_zero_p(pte[i])) {
    177 #ifdef DEBUG_NOISY
    178 			UVMHIST_FUNC(__func__);
    179 			UVMHIST_CALLARGS(pmapsegtabhist, "pte=%#jx",
    180 			    (uintptr_t)pte, 0, 0, 0);
    181 			for (size_t j = i + 1; j < NPTEPG; j++)
    182 				if (!pte_zero_p(pte[j]))
    183 					UVMHIST_LOG(pmapsegtabhist,
    184 					    "pte[%zu] = %#"PRIxPTE,
    185 					    j, pte_value(pte[j]), 0, 0);
    186 #endif
    187 			panic("%s: pte[%ju] entry at %pu not 0 (%#"PRIxPTE")",
    188 			      caller, i, &pte[i], pte_value(pte[i]));
    189 		}
    190 #endif
    191 }
    192 
    193 static inline struct vm_page *
    194 pmap_pte_pagealloc(void)
    195 {
    196 	struct vm_page *pg;
    197 
    198 	pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_ZERO|UVM_PGA_USERESERVE);
    199 	if (pg) {
    200 #ifdef UVM_PAGE_TRKOWN
    201 		pg->owner_tag = NULL;
    202 #endif
    203 		UVM_PAGE_OWN(pg, "pmap-ptp");
    204 	}
    205 
    206 	return pg;
    207 }
    208 
    209 static inline pt_entry_t *
    210 pmap_segmap(struct pmap *pmap, vaddr_t va)
    211 {
    212 	pmap_segtab_t *stp = pmap->pm_segtab;
    213 	KASSERTMSG(pmap != pmap_kernel() || !pmap_md_direct_mapped_vaddr_p(va),
    214 	    "pmap %p va %#" PRIxVADDR, pmap, va);
    215 #ifdef _LP64
    216 	stp = stp->seg_seg[(va >> XSEGSHIFT) & (NSEGPG - 1)];
    217 	if (stp == NULL)
    218 		return NULL;
    219 #endif
    220 
    221 	return stp->seg_tab[(va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1)];
    222 }
    223 
    224 pt_entry_t *
    225 pmap_pte_lookup(pmap_t pmap, vaddr_t va)
    226 {
    227 	pt_entry_t *pte = pmap_segmap(pmap, va);
    228 	if (pte == NULL)
    229 		return NULL;
    230 
    231 	return pte + ((va >> PGSHIFT) & (NPTEPG - 1));
    232 }
    233 
    234 /*
    235  * Insert the segtab into the segtab freelist.
    236  */
    237 static void
    238 pmap_segtab_free(pmap_segtab_t *stp)
    239 {
    240 	UVMHIST_FUNC(__func__);
    241 
    242 	UVMHIST_CALLARGS(pmapsegtabhist, "stp=%#jx", stp, 0, 0, 0);
    243 
    244 	mutex_spin_enter(&pmap_segtab_lock);
    245 	stp->seg_seg[0] = pmap_segtab_info.free_segtab;
    246 	pmap_segtab_info.free_segtab = stp;
    247 	SEGTAB_ADD(nput, 1);
    248 	mutex_spin_exit(&pmap_segtab_lock);
    249 }
    250 
    251 static void
    252 pmap_segtab_release(pmap_t pmap, pmap_segtab_t **stp_p, bool free_stp,
    253 	pte_callback_t callback, uintptr_t flags,
    254 	vaddr_t va, vsize_t vinc)
    255 {
    256 	pmap_segtab_t *stp = *stp_p;
    257 
    258 	UVMHIST_FUNC(__func__);
    259 	UVMHIST_CALLARGS(pmapsegtabhist, "pm=%#jx stpp=%#jx free=%jd",
    260 	    (uintptr_t)pmap, (uintptr_t)stp_p, free_stp, 0);
    261 	UVMHIST_LOG(pmapsegtabhist, " callback=%jx flags=%jx va=%jx vinc=%jx",
    262 	    (uintptr_t)callback, flags, (uintptr_t)va, (uintptr_t)vinc);
    263 	for (size_t i = (va / vinc) & (PMAP_SEGTABSIZE - 1);
    264 	     i < PMAP_SEGTABSIZE;
    265 	     i++, va += vinc) {
    266 #ifdef _LP64
    267 		if (vinc > NBSEG) {
    268 			if (stp->seg_seg[i] != NULL) {
    269 				UVMHIST_LOG(pmapsegtabhist,
    270 				    " recursing %jd", i, 0, 0, 0);
    271 				pmap_segtab_release(pmap, &stp->seg_seg[i],
    272 				    true, callback, flags, va, vinc / NSEGPG);
    273 				KASSERT(stp->seg_seg[i] == NULL);
    274 			}
    275 			continue;
    276 		}
    277 #endif
    278 		KASSERT(vinc == NBSEG);
    279 
    280 		/* get pointer to segment map */
    281 		pt_entry_t *pte = stp->seg_tab[i];
    282 		if (pte == NULL)
    283 			continue;
    284 		pmap_check_ptes(pte, __func__);
    285 
    286 #if defined(__mips_n64) && PAGE_SIZE == 8192
    287 		/*
    288 		 * XXX This is evil.  If vinc is 1000000 we are in
    289 		 * the last level, and this pte should be page aligned.
    290 		 */
    291 		if (vinc == 0x1000000 && ((uintptr_t)pte & PAGE_MASK) != 0) {
    292 			panic("%s: pte entry at %p not page aligned",
    293 			    __func__, pte);
    294 		}
    295 #endif
    296 
    297 		/*
    298 		 * If our caller wants a callback, do so.
    299 		 */
    300 		if (callback != NULL) {
    301 			(*callback)(pmap, va, va + vinc, pte, flags);
    302 		}
    303 
    304 		// PMAP_UNMAP_POOLPAGE should handle any VCA issues itself
    305 		paddr_t pa = PMAP_UNMAP_POOLPAGE((vaddr_t)pte);
    306 		struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
    307 #ifdef PMAP_PTP_CACHE
    308 		mutex_spin_enter(&pmap_segtab_lock);
    309 		LIST_INSERT_HEAD(&pmap_segtab_info.ptp_pgflist, pg, pageq.list);
    310 		mutex_spin_exit(&pmap_segtab_lock);
    311 #else
    312 		uvm_pagefree(pg);
    313 #endif
    314 
    315 		stp->seg_tab[i] = NULL;
    316 		UVMHIST_LOG(pmapsegtabhist, " zeroing tab[%jd]", i, 0, 0, 0);
    317 	}
    318 
    319 	if (free_stp) {
    320 		pmap_check_stp(stp, __func__,
    321 			       vinc == NBSEG ? "release seg" : "release xseg");
    322 		pmap_segtab_free(stp);
    323 		*stp_p = NULL;
    324 	}
    325 }
    326 
    327 /*
    328  *	Create and return a physical map.
    329  *
    330  *	If the size specified for the map
    331  *	is zero, the map is an actual physical
    332  *	map, and may be referenced by the
    333  *	hardware.
    334  *
    335  *	If the size specified is non-zero,
    336  *	the map will be used in software only, and
    337  *	is bounded by that size.
    338  */
    339 static pmap_segtab_t *
    340 pmap_segtab_alloc(void)
    341 {
    342 	pmap_segtab_t *stp;
    343 	bool found_on_freelist = false;
    344 
    345 	UVMHIST_FUNC(__func__);
    346  again:
    347 	mutex_spin_enter(&pmap_segtab_lock);
    348 	if (__predict_true((stp = pmap_segtab_info.free_segtab) != NULL)) {
    349 		pmap_segtab_info.free_segtab = stp->seg_seg[0];
    350 		stp->seg_seg[0] = NULL;
    351 		SEGTAB_ADD(nget, 1);
    352 		found_on_freelist = true;
    353 		UVMHIST_CALLARGS(pmapsegtabhist, "freelist stp=%#jx", stp, 0, 0, 0);
    354 	}
    355 	mutex_spin_exit(&pmap_segtab_lock);
    356 
    357 	if (__predict_false(stp == NULL)) {
    358 		struct vm_page * const stp_pg = pmap_pte_pagealloc();
    359 
    360 		if (__predict_false(stp_pg == NULL)) {
    361 			/*
    362 			 * XXX What else can we do?  Could we deadlock here?
    363 			 */
    364 			uvm_wait("segtab");
    365 			goto again;
    366 		}
    367 		SEGTAB_ADD(npage, 1);
    368 		const paddr_t stp_pa = VM_PAGE_TO_PHYS(stp_pg);
    369 
    370 		stp = (pmap_segtab_t *)PMAP_MAP_POOLPAGE(stp_pa);
    371 		UVMHIST_CALLARGS(pmapsegtabhist, "new stp=%#jx", stp, 0, 0, 0);
    372 		const size_t n = NBPG / sizeof(*stp);
    373 		if (n > 1) {
    374 			/*
    375 			 * link all the segtabs in this page together
    376 			 */
    377 			for (size_t i = 1; i < n - 1; i++) {
    378 				stp[i].seg_seg[0] = &stp[i+1];
    379 			}
    380 			/*
    381 			 * Now link the new segtabs into the free segtab list.
    382 			 */
    383 			mutex_spin_enter(&pmap_segtab_lock);
    384 			stp[n-1].seg_seg[0] = pmap_segtab_info.free_segtab;
    385 			pmap_segtab_info.free_segtab = stp + 1;
    386 			SEGTAB_ADD(nput, n - 1);
    387 			mutex_spin_exit(&pmap_segtab_lock);
    388 		}
    389 	}
    390 
    391 	pmap_check_stp(stp, __func__,
    392 		       found_on_freelist ? "from free list" : "allocated");
    393 
    394 	return stp;
    395 }
    396 
    397 /*
    398  * Allocate the top segment table for the pmap.
    399  */
    400 void
    401 pmap_segtab_init(pmap_t pmap)
    402 {
    403 
    404 	pmap->pm_segtab = pmap_segtab_alloc();
    405 }
    406 
    407 /*
    408  *	Retire the given physical map from service.
    409  *	Should only be called if the map contains
    410  *	no valid mappings.
    411  */
    412 void
    413 pmap_segtab_destroy(pmap_t pmap, pte_callback_t func, uintptr_t flags)
    414 {
    415 	if (pmap->pm_segtab == NULL)
    416 		return;
    417 
    418 #ifdef _LP64
    419 	const vsize_t vinc = NBXSEG;
    420 #else
    421 	const vsize_t vinc = NBSEG;
    422 #endif
    423 	pmap_segtab_release(pmap, &pmap->pm_segtab,
    424 	    func == NULL, func, flags, pmap->pm_minaddr, vinc);
    425 }
    426 
    427 /*
    428  *	Make a new pmap (vmspace) active for the given process.
    429  */
    430 void
    431 pmap_segtab_activate(struct pmap *pm, struct lwp *l)
    432 {
    433 	if (l == curlwp) {
    434 		struct cpu_info * const ci = l->l_cpu;
    435 		pmap_md_xtab_activate(pm, l);
    436 		KASSERT(pm == l->l_proc->p_vmspace->vm_map.pmap);
    437 		if (pm == pmap_kernel()) {
    438 			ci->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    439 #ifdef _LP64
    440 			ci->ci_pmap_user_seg0tab = PMAP_INVALID_SEGTAB_ADDRESS;
    441 #endif
    442 		} else {
    443 			ci->ci_pmap_user_segtab = pm->pm_segtab;
    444 #ifdef _LP64
    445 			ci->ci_pmap_user_seg0tab = pm->pm_segtab->seg_seg[0];
    446 #endif
    447 		}
    448 	}
    449 }
    450 
    451 
    452 void
    453 pmap_segtab_deactivate(pmap_t pm)
    454 {
    455 
    456 	pmap_md_xtab_deactivate(pm);
    457 
    458 	curcpu()->ci_pmap_user_segtab = PMAP_INVALID_SEGTAB_ADDRESS;
    459 #ifdef _LP64
    460 	curcpu()->ci_pmap_user_seg0tab = NULL;
    461 #endif
    462 
    463 }
    464 
    465 /*
    466  *	Act on the given range of addresses from the specified map.
    467  *
    468  *	It is assumed that the start and end are properly rounded to
    469  *	the page size.
    470  */
    471 void
    472 pmap_pte_process(pmap_t pmap, vaddr_t sva, vaddr_t eva,
    473     pte_callback_t callback, uintptr_t flags)
    474 {
    475 #if 0
    476 	printf("%s: %p, %"PRIxVADDR", %"PRIxVADDR", %p, %"PRIxPTR"\n",
    477 	    __func__, pmap, sva, eva, callback, flags);
    478 #endif
    479 	while (sva < eva) {
    480 		vaddr_t lastseg_va = pmap_trunc_seg(sva) + NBSEG;
    481 		if (lastseg_va == 0 || lastseg_va > eva)
    482 			lastseg_va = eva;
    483 
    484 		/*
    485 		 * If VA belongs to an unallocated segment,
    486 		 * skip to the next segment boundary.
    487 		 */
    488 		pt_entry_t * const ptep = pmap_pte_lookup(pmap, sva);
    489 		if (ptep != NULL) {
    490 			/*
    491 			 * Callback to deal with the ptes for this segment.
    492 			 */
    493 			(*callback)(pmap, sva, lastseg_va, ptep, flags);
    494 		}
    495 		/*
    496 		 * In theory we could release pages with no entries,
    497 		 * but that takes more effort than we want here.
    498 		 */
    499 		sva = lastseg_va;
    500 	}
    501 }
    502 
    503 /*
    504  *	Return a pointer for the pte that corresponds to the specified virtual
    505  *	address (va) in the target physical map, allocating if needed.
    506  */
    507 pt_entry_t *
    508 pmap_pte_reserve(pmap_t pmap, vaddr_t va, int flags)
    509 {
    510 	pmap_segtab_t *stp = pmap->pm_segtab;
    511 	pt_entry_t *pte;
    512 	UVMHIST_FUNC(__func__);
    513 
    514 	pte = pmap_pte_lookup(pmap, va);
    515 	if (__predict_false(pte == NULL)) {
    516 #ifdef _LP64
    517 		pmap_segtab_t ** const stp_p =
    518 		    &stp->seg_seg[(va >> XSEGSHIFT) & (NSEGPG - 1)];
    519 		if (__predict_false((stp = *stp_p) == NULL)) {
    520 			pmap_segtab_t *nstp = pmap_segtab_alloc();
    521 #ifdef MULTIPROCESSOR
    522 			pmap_segtab_t *ostp = atomic_cas_ptr(stp_p, NULL, nstp);
    523 			if (__predict_false(ostp != NULL)) {
    524 				pmap_check_stp(nstp, __func__, "reserve");
    525 				pmap_segtab_free(nstp);
    526 				nstp = ostp;
    527 			}
    528 #else
    529 			*stp_p = nstp;
    530 #endif /* MULTIPROCESSOR */
    531 			stp = nstp;
    532 		}
    533 		KASSERT(stp == pmap->pm_segtab->seg_seg[(va >> XSEGSHIFT) & (NSEGPG - 1)]);
    534 #endif /* _LP64 */
    535 		struct vm_page *pg = NULL;
    536 #ifdef PMAP_PTP_CACHE
    537 		mutex_spin_enter(&pmap_segtab_lock);
    538 		if ((pg = LIST_FIRST(&pmap_segtab_info.ptp_pgflist)) != NULL) {
    539 			LIST_REMOVE(pg, pageq.list);
    540 			KASSERT(LIST_FIRST(&pmap_segtab_info.ptp_pgflist) != pg);
    541 		}
    542 		mutex_spin_exit(&pmap_segtab_lock);
    543 #endif
    544 		if (pg == NULL)
    545 			pg = pmap_pte_pagealloc();
    546 		if (pg == NULL) {
    547 			if (flags & PMAP_CANFAIL)
    548 				return NULL;
    549 			panic("%s: cannot allocate page table page "
    550 			    "for va %" PRIxVADDR, __func__, va);
    551 		}
    552 
    553 		const paddr_t pa = VM_PAGE_TO_PHYS(pg);
    554 		pte = (pt_entry_t *)PMAP_MAP_POOLPAGE(pa);
    555 		pt_entry_t ** const pte_p =
    556 		    &stp->seg_tab[(va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1)];
    557 #ifdef MULTIPROCESSOR
    558 		pt_entry_t *opte = atomic_cas_ptr(pte_p, NULL, pte);
    559 		/*
    560 		 * If another thread allocated the segtab needed for this va
    561 		 * free the page we just allocated.
    562 		 */
    563 		if (__predict_false(opte != NULL)) {
    564 #ifdef PMAP_PTP_CACHE
    565 			mutex_spin_enter(&pmap_segtab_lock);
    566 			LIST_INSERT_HEAD(&pmap_segtab_info.ptp_pgflist,
    567 			    pg, pageq.list);
    568 			mutex_spin_exit(&pmap_segtab_lock);
    569 #else
    570 			PMAP_UNMAP_POOLPAGE((vaddr_t)pte);
    571 			uvm_pagefree(pg);
    572 #endif
    573 			pte = opte;
    574 		}
    575 #else
    576 		*pte_p = pte;
    577 #endif
    578 		KASSERT(pte == stp->seg_tab[(va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1)]);
    579 		UVMHIST_CALLARGS(pmapsegtabhist, "pm=%#jx va=%#jx -> tab[%jd]=%jx",
    580 		    (uintptr_t)pmap, (uintptr_t)va,
    581 		    (va >> SEGSHIFT) & (PMAP_SEGTABSIZE - 1), pte);
    582 
    583 		pmap_check_ptes(pte, __func__);
    584 		pte += (va >> PGSHIFT) & (NPTEPG - 1);
    585 	}
    586 
    587 	return pte;
    588 }
    589