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