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