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