2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department and William Jolitz of UUNET Technologies Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * Manages physical address maps.
46 * Since the information managed by this module is also stored by the
47 * logical address mapping module, this module may throw away valid virtual
48 * to physical mappings at almost any time. However, invalidations of
49 * mappings must be done as requested.
51 * In order to cope with hardware architectures which make virtual to
52 * physical map invalidates expensive, this module may delay invalidate
53 * reduced protection operations until such time as they are actually
54 * necessary. This module is given full information as to which processors
55 * are currently using which maps, and to when physical maps must be made
59 #include "opt_kstack_pages.h"
62 #include <sys/param.h>
63 #include <sys/kernel.h>
66 #include <sys/msgbuf.h>
67 #include <sys/mutex.h>
69 #include <sys/rwlock.h>
71 #include <sys/sysctl.h>
72 #include <sys/systm.h>
73 #include <sys/vmmeter.h>
75 #include <dev/ofw/openfirm.h>
78 #include <vm/vm_param.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_extern.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_pager.h>
86 #include <vm/vm_phys.h>
88 #include <machine/cache.h>
89 #include <machine/frame.h>
90 #include <machine/instr.h>
91 #include <machine/md_var.h>
92 #include <machine/metadata.h>
93 #include <machine/ofw_mem.h>
94 #include <machine/smp.h>
95 #include <machine/tlb.h>
96 #include <machine/tte.h>
97 #include <machine/tsb.h>
98 #include <machine/ver.h>
101 * Virtual address of message buffer
103 struct msgbuf *msgbufp;
106 * Map of physical memory reagions
108 vm_paddr_t phys_avail[128];
109 static struct ofw_mem_region mra[128];
110 struct ofw_mem_region sparc64_memreg[128];
112 static struct ofw_map translations[128];
113 static int translations_size;
115 static vm_offset_t pmap_idle_map;
116 static vm_offset_t pmap_temp_map_1;
117 static vm_offset_t pmap_temp_map_2;
120 * First and last available kernel virtual addresses
122 vm_offset_t virtual_avail;
123 vm_offset_t virtual_end;
124 vm_offset_t kernel_vm_end;
126 vm_offset_t vm_max_kernel_address;
131 struct pmap kernel_pmap_store;
133 struct rwlock_padalign tte_list_global_lock;
136 * Allocate physical memory for use in pmap_bootstrap.
138 static vm_paddr_t pmap_bootstrap_alloc(vm_size_t size, uint32_t colors);
140 static void pmap_bootstrap_set_tte(struct tte *tp, u_long vpn, u_long data);
141 static void pmap_cache_remove(vm_page_t m, vm_offset_t va);
142 static int pmap_protect_tte(struct pmap *pm1, struct pmap *pm2,
143 struct tte *tp, vm_offset_t va);
144 static int pmap_unwire_tte(pmap_t pm, pmap_t pm2, struct tte *tp,
148 * Map the given physical page at the specified virtual address in the
149 * target pmap with the protection requested. If specified the page
150 * will be wired down.
152 * The page queues and pmap must be locked.
154 static int pmap_enter_locked(pmap_t pm, vm_offset_t va, vm_page_t m,
155 vm_prot_t prot, u_int flags, int8_t psind);
157 extern int tl1_dmmu_miss_direct_patch_tsb_phys_1[];
158 extern int tl1_dmmu_miss_direct_patch_tsb_phys_end_1[];
159 extern int tl1_dmmu_miss_patch_asi_1[];
160 extern int tl1_dmmu_miss_patch_quad_ldd_1[];
161 extern int tl1_dmmu_miss_patch_tsb_1[];
162 extern int tl1_dmmu_miss_patch_tsb_2[];
163 extern int tl1_dmmu_miss_patch_tsb_mask_1[];
164 extern int tl1_dmmu_miss_patch_tsb_mask_2[];
165 extern int tl1_dmmu_prot_patch_asi_1[];
166 extern int tl1_dmmu_prot_patch_quad_ldd_1[];
167 extern int tl1_dmmu_prot_patch_tsb_1[];
168 extern int tl1_dmmu_prot_patch_tsb_2[];
169 extern int tl1_dmmu_prot_patch_tsb_mask_1[];
170 extern int tl1_dmmu_prot_patch_tsb_mask_2[];
171 extern int tl1_immu_miss_patch_asi_1[];
172 extern int tl1_immu_miss_patch_quad_ldd_1[];
173 extern int tl1_immu_miss_patch_tsb_1[];
174 extern int tl1_immu_miss_patch_tsb_2[];
175 extern int tl1_immu_miss_patch_tsb_mask_1[];
176 extern int tl1_immu_miss_patch_tsb_mask_2[];
179 * If user pmap is processed with pmap_remove and with pmap_remove and the
180 * resident count drops to 0, there are no more pages to remove, so we
183 #define PMAP_REMOVE_DONE(pm) \
184 ((pm) != kernel_pmap && (pm)->pm_stats.resident_count == 0)
187 * The threshold (in bytes) above which tsb_foreach() is used in pmap_remove()
188 * and pmap_protect() instead of trying each virtual address.
190 #define PMAP_TSB_THRESH ((TSB_SIZE / 2) * PAGE_SIZE)
192 SYSCTL_NODE(_debug, OID_AUTO, pmap_stats, CTLFLAG_RD, 0, "");
194 PMAP_STATS_VAR(pmap_nenter);
195 PMAP_STATS_VAR(pmap_nenter_update);
196 PMAP_STATS_VAR(pmap_nenter_replace);
197 PMAP_STATS_VAR(pmap_nenter_new);
198 PMAP_STATS_VAR(pmap_nkenter);
199 PMAP_STATS_VAR(pmap_nkenter_oc);
200 PMAP_STATS_VAR(pmap_nkenter_stupid);
201 PMAP_STATS_VAR(pmap_nkremove);
202 PMAP_STATS_VAR(pmap_nqenter);
203 PMAP_STATS_VAR(pmap_nqremove);
204 PMAP_STATS_VAR(pmap_ncache_enter);
205 PMAP_STATS_VAR(pmap_ncache_enter_c);
206 PMAP_STATS_VAR(pmap_ncache_enter_oc);
207 PMAP_STATS_VAR(pmap_ncache_enter_cc);
208 PMAP_STATS_VAR(pmap_ncache_enter_coc);
209 PMAP_STATS_VAR(pmap_ncache_enter_nc);
210 PMAP_STATS_VAR(pmap_ncache_enter_cnc);
211 PMAP_STATS_VAR(pmap_ncache_remove);
212 PMAP_STATS_VAR(pmap_ncache_remove_c);
213 PMAP_STATS_VAR(pmap_ncache_remove_oc);
214 PMAP_STATS_VAR(pmap_ncache_remove_cc);
215 PMAP_STATS_VAR(pmap_ncache_remove_coc);
216 PMAP_STATS_VAR(pmap_ncache_remove_nc);
217 PMAP_STATS_VAR(pmap_nzero_page);
218 PMAP_STATS_VAR(pmap_nzero_page_c);
219 PMAP_STATS_VAR(pmap_nzero_page_oc);
220 PMAP_STATS_VAR(pmap_nzero_page_nc);
221 PMAP_STATS_VAR(pmap_nzero_page_area);
222 PMAP_STATS_VAR(pmap_nzero_page_area_c);
223 PMAP_STATS_VAR(pmap_nzero_page_area_oc);
224 PMAP_STATS_VAR(pmap_nzero_page_area_nc);
225 PMAP_STATS_VAR(pmap_nzero_page_idle);
226 PMAP_STATS_VAR(pmap_nzero_page_idle_c);
227 PMAP_STATS_VAR(pmap_nzero_page_idle_oc);
228 PMAP_STATS_VAR(pmap_nzero_page_idle_nc);
229 PMAP_STATS_VAR(pmap_ncopy_page);
230 PMAP_STATS_VAR(pmap_ncopy_page_c);
231 PMAP_STATS_VAR(pmap_ncopy_page_oc);
232 PMAP_STATS_VAR(pmap_ncopy_page_nc);
233 PMAP_STATS_VAR(pmap_ncopy_page_dc);
234 PMAP_STATS_VAR(pmap_ncopy_page_doc);
235 PMAP_STATS_VAR(pmap_ncopy_page_sc);
236 PMAP_STATS_VAR(pmap_ncopy_page_soc);
238 PMAP_STATS_VAR(pmap_nnew_thread);
239 PMAP_STATS_VAR(pmap_nnew_thread_oc);
241 static inline u_long dtlb_get_data(u_int tlb, u_int slot);
244 * Quick sort callout for comparing memory regions
246 static int mr_cmp(const void *a, const void *b);
247 static int om_cmp(const void *a, const void *b);
250 mr_cmp(const void *a, const void *b)
252 const struct ofw_mem_region *mra;
253 const struct ofw_mem_region *mrb;
257 if (mra->mr_start < mrb->mr_start)
259 else if (mra->mr_start > mrb->mr_start)
266 om_cmp(const void *a, const void *b)
268 const struct ofw_map *oma;
269 const struct ofw_map *omb;
273 if (oma->om_start < omb->om_start)
275 else if (oma->om_start > omb->om_start)
282 dtlb_get_data(u_int tlb, u_int slot)
287 slot = TLB_DAR_SLOT(tlb, slot);
289 * We read ASI_DTLB_DATA_ACCESS_REG twice back-to-back in order to
290 * work around errata of USIII and beyond.
293 (void)ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
294 data = ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
300 * Bootstrap the system enough to run with virtual memory.
303 pmap_bootstrap(u_int cpu_impl)
316 u_int dtlb_slots_avail;
325 * Set the kernel context.
329 colors = dcache_color_ignore != 0 ? 1 : DCACHE_COLORS;
332 * Find out what physical memory is available from the PROM and
333 * initialize the phys_avail array. This must be done before
334 * pmap_bootstrap_alloc is called.
336 if ((pmem = OF_finddevice("/memory")) == -1)
337 OF_panic("%s: finddevice /memory", __func__);
338 if ((sz = OF_getproplen(pmem, "available")) == -1)
339 OF_panic("%s: getproplen /memory/available", __func__);
340 if (sizeof(phys_avail) < sz)
341 OF_panic("%s: phys_avail too small", __func__);
342 if (sizeof(mra) < sz)
343 OF_panic("%s: mra too small", __func__);
345 if (OF_getprop(pmem, "available", mra, sz) == -1)
346 OF_panic("%s: getprop /memory/available", __func__);
348 CTR0(KTR_PMAP, "pmap_bootstrap: physical memory");
349 qsort(mra, sz, sizeof (*mra), mr_cmp);
351 getenv_quad("hw.physmem", &physmem);
352 physmem = btoc(physmem);
353 for (i = 0, j = 0; i < sz; i++, j += 2) {
354 CTR2(KTR_PMAP, "start=%#lx size=%#lx", mra[i].mr_start,
356 if (physmem != 0 && btoc(physsz + mra[i].mr_size) >= physmem) {
357 if (btoc(physsz) < physmem) {
358 phys_avail[j] = mra[i].mr_start;
359 phys_avail[j + 1] = mra[i].mr_start +
360 (ctob(physmem) - physsz);
361 physsz = ctob(physmem);
365 phys_avail[j] = mra[i].mr_start;
366 phys_avail[j + 1] = mra[i].mr_start + mra[i].mr_size;
367 physsz += mra[i].mr_size;
369 physmem = btoc(physsz);
372 * Calculate the size of kernel virtual memory, and the size and mask
373 * for the kernel TSB based on the phsyical memory size but limited
374 * by the amount of dTLB slots available for locked entries if we have
375 * to lock the TSB in the TLB (given that for spitfire-class CPUs all
376 * of the dt64 slots can hold locked entries but there is no large
377 * dTLB for unlocked ones, we don't use more than half of it for the
379 * Note that for reasons unknown OpenSolaris doesn't take advantage of
380 * ASI_ATOMIC_QUAD_LDD_PHYS on UltraSPARC-III. However, given that no
381 * public documentation is available for these, the latter just might
382 * not support it, yet.
384 if (cpu_impl == CPU_IMPL_SPARC64V ||
385 cpu_impl >= CPU_IMPL_ULTRASPARCIIIp) {
386 tsb_kernel_ldd_phys = 1;
387 virtsz = roundup(5 / 3 * physsz, PAGE_SIZE_4M <<
388 (PAGE_SHIFT - TTE_SHIFT));
390 dtlb_slots_avail = 0;
391 for (i = 0; i < dtlb_slots; i++) {
392 data = dtlb_get_data(cpu_impl ==
393 CPU_IMPL_ULTRASPARCIII ? TLB_DAR_T16 :
395 if ((data & (TD_V | TD_L)) != (TD_V | TD_L))
399 dtlb_slots_avail -= PCPU_PAGES;
401 if (cpu_impl >= CPU_IMPL_ULTRASPARCI &&
402 cpu_impl < CPU_IMPL_ULTRASPARCIII)
403 dtlb_slots_avail /= 2;
404 virtsz = roundup(physsz, PAGE_SIZE_4M <<
405 (PAGE_SHIFT - TTE_SHIFT));
406 virtsz = MIN(virtsz, (dtlb_slots_avail * PAGE_SIZE_4M) <<
407 (PAGE_SHIFT - TTE_SHIFT));
409 vm_max_kernel_address = VM_MIN_KERNEL_ADDRESS + virtsz;
410 tsb_kernel_size = virtsz >> (PAGE_SHIFT - TTE_SHIFT);
411 tsb_kernel_mask = (tsb_kernel_size >> TTE_SHIFT) - 1;
414 * Allocate the kernel TSB and lock it in the TLB if necessary.
416 pa = pmap_bootstrap_alloc(tsb_kernel_size, colors);
417 if (pa & PAGE_MASK_4M)
418 OF_panic("%s: TSB unaligned", __func__);
419 tsb_kernel_phys = pa;
420 if (tsb_kernel_ldd_phys == 0) {
422 (struct tte *)(VM_MIN_KERNEL_ADDRESS - tsb_kernel_size);
424 bzero(tsb_kernel, tsb_kernel_size);
427 (struct tte *)TLB_PHYS_TO_DIRECT(tsb_kernel_phys);
428 aszero(ASI_PHYS_USE_EC, tsb_kernel_phys, tsb_kernel_size);
432 * Allocate and map the dynamic per-CPU area for the BSP.
434 pa = pmap_bootstrap_alloc(DPCPU_SIZE, colors);
435 dpcpu0 = (void *)TLB_PHYS_TO_DIRECT(pa);
438 * Allocate and map the message buffer.
440 pa = pmap_bootstrap_alloc(msgbufsize, colors);
441 msgbufp = (struct msgbuf *)TLB_PHYS_TO_DIRECT(pa);
444 * Patch the TSB addresses and mask as well as the ASIs used to load
445 * it into the trap table.
448 #define LDDA_R_I_R(rd, imm_asi, rs1, rs2) \
449 (EIF_OP(IOP_LDST) | EIF_F3_RD(rd) | EIF_F3_OP3(INS3_LDDA) | \
450 EIF_F3_RS1(rs1) | EIF_F3_I(0) | EIF_F3_IMM_ASI(imm_asi) | \
452 #define OR_R_I_R(rd, imm13, rs1) \
453 (EIF_OP(IOP_MISC) | EIF_F3_RD(rd) | EIF_F3_OP3(INS2_OR) | \
454 EIF_F3_RS1(rs1) | EIF_F3_I(1) | EIF_IMM(imm13, 13))
455 #define SETHI(rd, imm22) \
456 (EIF_OP(IOP_FORM2) | EIF_F2_RD(rd) | EIF_F2_OP2(INS0_SETHI) | \
457 EIF_IMM((imm22) >> 10, 22))
458 #define WR_R_I(rd, imm13, rs1) \
459 (EIF_OP(IOP_MISC) | EIF_F3_RD(rd) | EIF_F3_OP3(INS2_WR) | \
460 EIF_F3_RS1(rs1) | EIF_F3_I(1) | EIF_IMM(imm13, 13))
462 #define PATCH_ASI(addr, asi) do { \
463 if (addr[0] != WR_R_I(IF_F3_RD(addr[0]), 0x0, \
464 IF_F3_RS1(addr[0]))) \
465 OF_panic("%s: patched instructions have changed", \
467 addr[0] |= EIF_IMM((asi), 13); \
471 #define PATCH_LDD(addr, asi) do { \
472 if (addr[0] != LDDA_R_I_R(IF_F3_RD(addr[0]), 0x0, \
473 IF_F3_RS1(addr[0]), IF_F3_RS2(addr[0]))) \
474 OF_panic("%s: patched instructions have changed", \
476 addr[0] |= EIF_F3_IMM_ASI(asi); \
480 #define PATCH_TSB(addr, val) do { \
481 if (addr[0] != SETHI(IF_F2_RD(addr[0]), 0x0) || \
482 addr[1] != OR_R_I_R(IF_F3_RD(addr[1]), 0x0, \
483 IF_F3_RS1(addr[1])) || \
484 addr[3] != SETHI(IF_F2_RD(addr[3]), 0x0)) \
485 OF_panic("%s: patched instructions have changed", \
487 addr[0] |= EIF_IMM((val) >> 42, 22); \
488 addr[1] |= EIF_IMM((val) >> 32, 10); \
489 addr[3] |= EIF_IMM((val) >> 10, 22); \
495 #define PATCH_TSB_MASK(addr, val) do { \
496 if (addr[0] != SETHI(IF_F2_RD(addr[0]), 0x0) || \
497 addr[1] != OR_R_I_R(IF_F3_RD(addr[1]), 0x0, \
498 IF_F3_RS1(addr[1]))) \
499 OF_panic("%s: patched instructions have changed", \
501 addr[0] |= EIF_IMM((val) >> 10, 22); \
502 addr[1] |= EIF_IMM((val), 10); \
507 if (tsb_kernel_ldd_phys == 0) {
509 ldd = ASI_NUCLEUS_QUAD_LDD;
510 off = (vm_offset_t)tsb_kernel;
512 asi = ASI_PHYS_USE_EC;
513 ldd = ASI_ATOMIC_QUAD_LDD_PHYS;
514 off = (vm_offset_t)tsb_kernel_phys;
516 PATCH_TSB(tl1_dmmu_miss_direct_patch_tsb_phys_1, tsb_kernel_phys);
517 PATCH_TSB(tl1_dmmu_miss_direct_patch_tsb_phys_end_1,
518 tsb_kernel_phys + tsb_kernel_size - 1);
519 PATCH_ASI(tl1_dmmu_miss_patch_asi_1, asi);
520 PATCH_LDD(tl1_dmmu_miss_patch_quad_ldd_1, ldd);
521 PATCH_TSB(tl1_dmmu_miss_patch_tsb_1, off);
522 PATCH_TSB(tl1_dmmu_miss_patch_tsb_2, off);
523 PATCH_TSB_MASK(tl1_dmmu_miss_patch_tsb_mask_1, tsb_kernel_mask);
524 PATCH_TSB_MASK(tl1_dmmu_miss_patch_tsb_mask_2, tsb_kernel_mask);
525 PATCH_ASI(tl1_dmmu_prot_patch_asi_1, asi);
526 PATCH_LDD(tl1_dmmu_prot_patch_quad_ldd_1, ldd);
527 PATCH_TSB(tl1_dmmu_prot_patch_tsb_1, off);
528 PATCH_TSB(tl1_dmmu_prot_patch_tsb_2, off);
529 PATCH_TSB_MASK(tl1_dmmu_prot_patch_tsb_mask_1, tsb_kernel_mask);
530 PATCH_TSB_MASK(tl1_dmmu_prot_patch_tsb_mask_2, tsb_kernel_mask);
531 PATCH_ASI(tl1_immu_miss_patch_asi_1, asi);
532 PATCH_LDD(tl1_immu_miss_patch_quad_ldd_1, ldd);
533 PATCH_TSB(tl1_immu_miss_patch_tsb_1, off);
534 PATCH_TSB(tl1_immu_miss_patch_tsb_2, off);
535 PATCH_TSB_MASK(tl1_immu_miss_patch_tsb_mask_1, tsb_kernel_mask);
536 PATCH_TSB_MASK(tl1_immu_miss_patch_tsb_mask_2, tsb_kernel_mask);
539 * Enter fake 8k pages for the 4MB kernel pages, so that
540 * pmap_kextract() will work for them.
542 for (i = 0; i < kernel_tlb_slots; i++) {
543 pa = kernel_tlbs[i].te_pa;
544 va = kernel_tlbs[i].te_va;
545 for (off = 0; off < PAGE_SIZE_4M; off += PAGE_SIZE) {
546 tp = tsb_kvtotte(va + off);
547 vpn = TV_VPN(va + off, TS_8K);
548 data = TD_V | TD_8K | TD_PA(pa + off) | TD_REF |
549 TD_SW | TD_CP | TD_CV | TD_P | TD_W;
550 pmap_bootstrap_set_tte(tp, vpn, data);
555 * Set the start and end of KVA. The kernel is loaded starting
556 * at the first available 4MB super page, so we advance to the
557 * end of the last one used for it.
559 virtual_avail = KERNBASE + kernel_tlb_slots * PAGE_SIZE_4M;
560 virtual_end = vm_max_kernel_address;
561 kernel_vm_end = vm_max_kernel_address;
564 * Allocate kva space for temporary mappings.
566 pmap_idle_map = virtual_avail;
567 virtual_avail += PAGE_SIZE * colors;
568 pmap_temp_map_1 = virtual_avail;
569 virtual_avail += PAGE_SIZE * colors;
570 pmap_temp_map_2 = virtual_avail;
571 virtual_avail += PAGE_SIZE * colors;
574 * Allocate a kernel stack with guard page for thread0 and map it
575 * into the kernel TSB. We must ensure that the virtual address is
576 * colored properly for corresponding CPUs, since we're allocating
577 * from phys_avail so the memory won't have an associated vm_page_t.
579 pa = pmap_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, colors);
581 virtual_avail += roundup(KSTACK_GUARD_PAGES, colors) * PAGE_SIZE;
582 kstack0 = virtual_avail;
583 virtual_avail += roundup(KSTACK_PAGES, colors) * PAGE_SIZE;
584 if (dcache_color_ignore == 0)
585 KASSERT(DCACHE_COLOR(kstack0) == DCACHE_COLOR(kstack0_phys),
586 ("pmap_bootstrap: kstack0 miscolored"));
587 for (i = 0; i < KSTACK_PAGES; i++) {
588 pa = kstack0_phys + i * PAGE_SIZE;
589 va = kstack0 + i * PAGE_SIZE;
590 tp = tsb_kvtotte(va);
591 vpn = TV_VPN(va, TS_8K);
592 data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_SW | TD_CP |
594 pmap_bootstrap_set_tte(tp, vpn, data);
598 * Calculate the last available physical address.
600 for (i = 0; phys_avail[i + 2] != 0; i += 2)
602 Maxmem = sparc64_btop(phys_avail[i + 1]);
605 * Add the PROM mappings to the kernel TSB.
607 if ((vmem = OF_finddevice("/virtual-memory")) == -1)
608 OF_panic("%s: finddevice /virtual-memory", __func__);
609 if ((sz = OF_getproplen(vmem, "translations")) == -1)
610 OF_panic("%s: getproplen translations", __func__);
611 if (sizeof(translations) < sz)
612 OF_panic("%s: translations too small", __func__);
613 bzero(translations, sz);
614 if (OF_getprop(vmem, "translations", translations, sz) == -1)
615 OF_panic("%s: getprop /virtual-memory/translations",
617 sz /= sizeof(*translations);
618 translations_size = sz;
619 CTR0(KTR_PMAP, "pmap_bootstrap: translations");
620 qsort(translations, sz, sizeof (*translations), om_cmp);
621 for (i = 0; i < sz; i++) {
623 "translation: start=%#lx size=%#lx tte=%#lx",
624 translations[i].om_start, translations[i].om_size,
625 translations[i].om_tte);
626 if ((translations[i].om_tte & TD_V) == 0)
628 if (translations[i].om_start < VM_MIN_PROM_ADDRESS ||
629 translations[i].om_start > VM_MAX_PROM_ADDRESS)
631 for (off = 0; off < translations[i].om_size;
633 va = translations[i].om_start + off;
634 tp = tsb_kvtotte(va);
635 vpn = TV_VPN(va, TS_8K);
636 data = ((translations[i].om_tte &
637 ~((TD_SOFT2_MASK << TD_SOFT2_SHIFT) |
638 (cpu_impl >= CPU_IMPL_ULTRASPARCI &&
639 cpu_impl < CPU_IMPL_ULTRASPARCIII ?
640 (TD_DIAG_SF_MASK << TD_DIAG_SF_SHIFT) :
641 (TD_RSVD_CH_MASK << TD_RSVD_CH_SHIFT)) |
642 (TD_SOFT_MASK << TD_SOFT_SHIFT))) | TD_EXEC) +
644 pmap_bootstrap_set_tte(tp, vpn, data);
649 * Get the available physical memory ranges from /memory/reg. These
650 * are only used for kernel dumps, but it may not be wise to do PROM
651 * calls in that situation.
653 if ((sz = OF_getproplen(pmem, "reg")) == -1)
654 OF_panic("%s: getproplen /memory/reg", __func__);
655 if (sizeof(sparc64_memreg) < sz)
656 OF_panic("%s: sparc64_memreg too small", __func__);
657 if (OF_getprop(pmem, "reg", sparc64_memreg, sz) == -1)
658 OF_panic("%s: getprop /memory/reg", __func__);
659 sparc64_nmemreg = sz / sizeof(*sparc64_memreg);
662 * Initialize the kernel pmap (which is statically allocated).
666 for (i = 0; i < MAXCPU; i++)
667 pm->pm_context[i] = TLB_CTX_KERNEL;
668 CPU_FILL(&pm->pm_active);
671 * Initialize the global tte list lock, which is more commonly
672 * known as the pmap pv global lock.
674 rw_init(&tte_list_global_lock, "pmap pv global");
677 * Flush all non-locked TLB entries possibly left over by the
680 tlb_flush_nonlocked();
684 * Map the 4MB kernel TSB pages.
694 for (i = 0; i < tsb_kernel_size; i += PAGE_SIZE_4M) {
695 va = (vm_offset_t)tsb_kernel + i;
696 pa = tsb_kernel_phys + i;
697 data = TD_V | TD_4M | TD_PA(pa) | TD_L | TD_CP | TD_CV |
699 stxa(AA_DMMU_TAR, ASI_DMMU, TLB_TAR_VA(va) |
700 TLB_TAR_CTX(TLB_CTX_KERNEL));
701 stxa_sync(0, ASI_DTLB_DATA_IN_REG, data);
706 * Set the secondary context to be the kernel context (needed for FP block
707 * operations in the kernel).
713 stxa(AA_DMMU_SCXR, ASI_DMMU, (ldxa(AA_DMMU_SCXR, ASI_DMMU) &
714 TLB_CXR_PGSZ_MASK) | TLB_CTX_KERNEL);
719 * Allocate a physical page of memory directly from the phys_avail map.
720 * Can only be called from pmap_bootstrap before avail start and end are
724 pmap_bootstrap_alloc(vm_size_t size, uint32_t colors)
729 size = roundup(size, PAGE_SIZE * colors);
730 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
731 if (phys_avail[i + 1] - phys_avail[i] < size)
734 phys_avail[i] += size;
737 OF_panic("%s: no suitable region found", __func__);
741 * Set a TTE. This function is intended as a helper when tsb_kernel is
742 * direct-mapped but we haven't taken over the trap table, yet, as it's the
743 * case when we are taking advantage of ASI_ATOMIC_QUAD_LDD_PHYS to access
747 pmap_bootstrap_set_tte(struct tte *tp, u_long vpn, u_long data)
750 if (tsb_kernel_ldd_phys == 0) {
754 stxa((vm_paddr_t)tp + offsetof(struct tte, tte_vpn),
755 ASI_PHYS_USE_EC, vpn);
756 stxa((vm_paddr_t)tp + offsetof(struct tte, tte_data),
757 ASI_PHYS_USE_EC, data);
762 * Initialize a vm_page's machine-dependent fields.
765 pmap_page_init(vm_page_t m)
768 TAILQ_INIT(&m->md.tte_list);
769 m->md.color = DCACHE_COLOR(VM_PAGE_TO_PHYS(m));
774 * Initialize the pmap module.
784 for (i = 0; i < translations_size; i++) {
785 addr = translations[i].om_start;
786 size = translations[i].om_size;
787 if ((translations[i].om_tte & TD_V) == 0)
789 if (addr < VM_MIN_PROM_ADDRESS || addr > VM_MAX_PROM_ADDRESS)
791 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
792 VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
793 if (result != KERN_SUCCESS || addr != translations[i].om_start)
794 panic("pmap_init: vm_map_find");
799 * Extract the physical page address associated with the given
800 * map/virtual_address pair.
803 pmap_extract(pmap_t pm, vm_offset_t va)
808 if (pm == kernel_pmap)
809 return (pmap_kextract(va));
811 tp = tsb_tte_lookup(pm, va);
815 pa = TTE_GET_PA(tp) | (va & TTE_GET_PAGE_MASK(tp));
821 * Atomically extract and hold the physical page with the given
822 * pmap and virtual address pair if that mapping permits the given
826 pmap_extract_and_hold(pmap_t pm, vm_offset_t va, vm_prot_t prot)
836 if (pm == kernel_pmap) {
837 if (va >= VM_MIN_DIRECT_ADDRESS) {
839 m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS(va));
840 (void)vm_page_pa_tryrelock(pm, TLB_DIRECT_TO_PHYS(va),
844 tp = tsb_kvtotte(va);
845 if ((tp->tte_data & TD_V) == 0)
849 tp = tsb_tte_lookup(pm, va);
850 if (tp != NULL && ((tp->tte_data & TD_SW) ||
851 (prot & VM_PROT_WRITE) == 0)) {
852 if (vm_page_pa_tryrelock(pm, TTE_GET_PA(tp), &pa))
854 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
863 * Extract the physical page address associated with the given kernel virtual
867 pmap_kextract(vm_offset_t va)
871 if (va >= VM_MIN_DIRECT_ADDRESS)
872 return (TLB_DIRECT_TO_PHYS(va));
873 tp = tsb_kvtotte(va);
874 if ((tp->tte_data & TD_V) == 0)
876 return (TTE_GET_PA(tp) | (va & TTE_GET_PAGE_MASK(tp)));
880 pmap_cache_enter(vm_page_t m, vm_offset_t va)
885 rw_assert(&tte_list_global_lock, RA_WLOCKED);
886 KASSERT((m->flags & PG_FICTITIOUS) == 0,
887 ("pmap_cache_enter: fake page"));
888 PMAP_STATS_INC(pmap_ncache_enter);
890 if (dcache_color_ignore != 0)
894 * Find the color for this virtual address and note the added mapping.
896 color = DCACHE_COLOR(va);
897 m->md.colors[color]++;
900 * If all existing mappings have the same color, the mapping is
903 if (m->md.color == color) {
904 KASSERT(m->md.colors[DCACHE_OTHER_COLOR(color)] == 0,
905 ("pmap_cache_enter: cacheable, mappings of other color"));
906 if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
907 PMAP_STATS_INC(pmap_ncache_enter_c);
909 PMAP_STATS_INC(pmap_ncache_enter_oc);
914 * If there are no mappings of the other color, and the page still has
915 * the wrong color, this must be a new mapping. Change the color to
916 * match the new mapping, which is cacheable. We must flush the page
917 * from the cache now.
919 if (m->md.colors[DCACHE_OTHER_COLOR(color)] == 0) {
920 KASSERT(m->md.colors[color] == 1,
921 ("pmap_cache_enter: changing color, not new mapping"));
922 dcache_page_inval(VM_PAGE_TO_PHYS(m));
924 if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
925 PMAP_STATS_INC(pmap_ncache_enter_cc);
927 PMAP_STATS_INC(pmap_ncache_enter_coc);
932 * If the mapping is already non-cacheable, just return.
934 if (m->md.color == -1) {
935 PMAP_STATS_INC(pmap_ncache_enter_nc);
939 PMAP_STATS_INC(pmap_ncache_enter_cnc);
942 * Mark all mappings as uncacheable, flush any lines with the other
943 * color out of the dcache, and set the color to none (-1).
945 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
946 atomic_clear_long(&tp->tte_data, TD_CV);
947 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
949 dcache_page_inval(VM_PAGE_TO_PHYS(m));
955 pmap_cache_remove(vm_page_t m, vm_offset_t va)
960 rw_assert(&tte_list_global_lock, RA_WLOCKED);
961 CTR3(KTR_PMAP, "pmap_cache_remove: m=%p va=%#lx c=%d", m, va,
962 m->md.colors[DCACHE_COLOR(va)]);
963 KASSERT((m->flags & PG_FICTITIOUS) == 0,
964 ("pmap_cache_remove: fake page"));
965 PMAP_STATS_INC(pmap_ncache_remove);
967 if (dcache_color_ignore != 0)
970 KASSERT(m->md.colors[DCACHE_COLOR(va)] > 0,
971 ("pmap_cache_remove: no mappings %d <= 0",
972 m->md.colors[DCACHE_COLOR(va)]));
975 * Find the color for this virtual address and note the removal of
978 color = DCACHE_COLOR(va);
979 m->md.colors[color]--;
982 * If the page is cacheable, just return and keep the same color, even
983 * if there are no longer any mappings.
985 if (m->md.color != -1) {
986 if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
987 PMAP_STATS_INC(pmap_ncache_remove_c);
989 PMAP_STATS_INC(pmap_ncache_remove_oc);
993 KASSERT(m->md.colors[DCACHE_OTHER_COLOR(color)] != 0,
994 ("pmap_cache_remove: uncacheable, no mappings of other color"));
997 * If the page is not cacheable (color is -1), and the number of
998 * mappings for this color is not zero, just return. There are
999 * mappings of the other color still, so remain non-cacheable.
1001 if (m->md.colors[color] != 0) {
1002 PMAP_STATS_INC(pmap_ncache_remove_nc);
1007 * The number of mappings for this color is now zero. Recache the
1008 * other colored mappings, and change the page color to the other
1009 * color. There should be no lines in the data cache for this page,
1010 * so flushing should not be needed.
1012 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
1013 atomic_set_long(&tp->tte_data, TD_CV);
1014 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
1016 m->md.color = DCACHE_OTHER_COLOR(color);
1018 if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
1019 PMAP_STATS_INC(pmap_ncache_remove_cc);
1021 PMAP_STATS_INC(pmap_ncache_remove_coc);
1025 * Map a wired page into kernel virtual address space.
1028 pmap_kenter(vm_offset_t va, vm_page_t m)
1035 rw_assert(&tte_list_global_lock, RA_WLOCKED);
1036 PMAP_STATS_INC(pmap_nkenter);
1037 tp = tsb_kvtotte(va);
1038 CTR4(KTR_PMAP, "pmap_kenter: va=%#lx pa=%#lx tp=%p data=%#lx",
1039 va, VM_PAGE_TO_PHYS(m), tp, tp->tte_data);
1040 if (DCACHE_COLOR(VM_PAGE_TO_PHYS(m)) != DCACHE_COLOR(va)) {
1042 "pmap_kenter: off color va=%#lx pa=%#lx o=%p ot=%d pi=%#lx",
1043 va, VM_PAGE_TO_PHYS(m), m->object,
1044 m->object ? m->object->type : -1,
1046 PMAP_STATS_INC(pmap_nkenter_oc);
1048 if ((tp->tte_data & TD_V) != 0) {
1049 om = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
1050 ova = TTE_GET_VA(tp);
1051 if (m == om && va == ova) {
1052 PMAP_STATS_INC(pmap_nkenter_stupid);
1055 TAILQ_REMOVE(&om->md.tte_list, tp, tte_link);
1056 pmap_cache_remove(om, ova);
1058 tlb_page_demap(kernel_pmap, ova);
1060 data = TD_V | TD_8K | VM_PAGE_TO_PHYS(m) | TD_REF | TD_SW | TD_CP |
1062 if (pmap_cache_enter(m, va) != 0)
1064 tp->tte_vpn = TV_VPN(va, TS_8K);
1065 tp->tte_data = data;
1066 TAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link);
1070 * Map a wired page into kernel virtual address space. This additionally
1071 * takes a flag argument which is or'ed to the TTE data. This is used by
1072 * sparc64_bus_mem_map().
1073 * NOTE: if the mapping is non-cacheable, it's the caller's responsibility
1074 * to flush entries that might still be in the cache, if applicable.
1077 pmap_kenter_flags(vm_offset_t va, vm_paddr_t pa, u_long flags)
1081 tp = tsb_kvtotte(va);
1082 CTR4(KTR_PMAP, "pmap_kenter_flags: va=%#lx pa=%#lx tp=%p data=%#lx",
1083 va, pa, tp, tp->tte_data);
1084 tp->tte_vpn = TV_VPN(va, TS_8K);
1085 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_P | flags;
1089 * Remove a wired page from kernel virtual address space.
1092 pmap_kremove(vm_offset_t va)
1097 rw_assert(&tte_list_global_lock, RA_WLOCKED);
1098 PMAP_STATS_INC(pmap_nkremove);
1099 tp = tsb_kvtotte(va);
1100 CTR3(KTR_PMAP, "pmap_kremove: va=%#lx tp=%p data=%#lx", va, tp,
1102 if ((tp->tte_data & TD_V) == 0)
1104 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
1105 TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
1106 pmap_cache_remove(m, va);
1111 * Inverse of pmap_kenter_flags, used by bus_space_unmap().
1114 pmap_kremove_flags(vm_offset_t va)
1118 tp = tsb_kvtotte(va);
1119 CTR3(KTR_PMAP, "pmap_kremove_flags: va=%#lx tp=%p data=%#lx", va, tp,
1125 * Map a range of physical addresses into kernel virtual address space.
1127 * The value passed in *virt is a suggested virtual address for the mapping.
1128 * Architectures which can support a direct-mapped physical to virtual region
1129 * can return the appropriate address within that region, leaving '*virt'
1133 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1136 return (TLB_PHYS_TO_DIRECT(start));
1140 * Map a list of wired pages into kernel virtual address space. This is
1141 * intended for temporary mappings which do not need page modification or
1142 * references recorded. Existing mappings in the region are overwritten.
1145 pmap_qenter(vm_offset_t sva, vm_page_t *m, int count)
1149 PMAP_STATS_INC(pmap_nqenter);
1151 rw_wlock(&tte_list_global_lock);
1152 while (count-- > 0) {
1153 pmap_kenter(va, *m);
1157 rw_wunlock(&tte_list_global_lock);
1158 tlb_range_demap(kernel_pmap, sva, va);
1162 * Remove page mappings from kernel virtual address space. Intended for
1163 * temporary mappings entered by pmap_qenter.
1166 pmap_qremove(vm_offset_t sva, int count)
1170 PMAP_STATS_INC(pmap_nqremove);
1172 rw_wlock(&tte_list_global_lock);
1173 while (count-- > 0) {
1177 rw_wunlock(&tte_list_global_lock);
1178 tlb_range_demap(kernel_pmap, sva, va);
1182 * Initialize the pmap associated with process 0.
1185 pmap_pinit0(pmap_t pm)
1190 for (i = 0; i < MAXCPU; i++)
1191 pm->pm_context[i] = TLB_CTX_KERNEL;
1192 CPU_ZERO(&pm->pm_active);
1194 pm->pm_tsb_obj = NULL;
1195 bzero(&pm->pm_stats, sizeof(pm->pm_stats));
1199 * Initialize a preallocated and zeroed pmap structure, such as one in a
1200 * vmspace structure.
1203 pmap_pinit(pmap_t pm)
1205 vm_page_t ma[TSB_PAGES];
1210 * Allocate KVA space for the TSB.
1212 if (pm->pm_tsb == NULL) {
1213 pm->pm_tsb = (struct tte *)kva_alloc(TSB_BSIZE);
1214 if (pm->pm_tsb == NULL)
1219 * Allocate an object for it.
1221 if (pm->pm_tsb_obj == NULL)
1222 pm->pm_tsb_obj = vm_object_allocate(OBJT_PHYS, TSB_PAGES);
1224 for (i = 0; i < MAXCPU; i++)
1225 pm->pm_context[i] = -1;
1226 CPU_ZERO(&pm->pm_active);
1228 VM_OBJECT_WLOCK(pm->pm_tsb_obj);
1229 for (i = 0; i < TSB_PAGES; i++) {
1230 m = vm_page_grab(pm->pm_tsb_obj, i, VM_ALLOC_NOBUSY |
1231 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
1232 m->valid = VM_PAGE_BITS_ALL;
1236 VM_OBJECT_WUNLOCK(pm->pm_tsb_obj);
1237 pmap_qenter((vm_offset_t)pm->pm_tsb, ma, TSB_PAGES);
1239 bzero(&pm->pm_stats, sizeof(pm->pm_stats));
1244 * Release any resources held by the given physical map.
1245 * Called when a pmap initialized by pmap_pinit is being released.
1246 * Should only be called if the map contains no valid mappings.
1249 pmap_release(pmap_t pm)
1257 CTR2(KTR_PMAP, "pmap_release: ctx=%#x tsb=%p",
1258 pm->pm_context[curcpu], pm->pm_tsb);
1259 KASSERT(pmap_resident_count(pm) == 0,
1260 ("pmap_release: resident pages %ld != 0",
1261 pmap_resident_count(pm)));
1264 * After the pmap was freed, it might be reallocated to a new process.
1265 * When switching, this might lead us to wrongly assume that we need
1266 * not switch contexts because old and new pmap pointer are equal.
1267 * Therefore, make sure that this pmap is not referenced by any PCPU
1268 * pointer any more. This could happen in two cases:
1269 * - A process that referenced the pmap is currently exiting on a CPU.
1270 * However, it is guaranteed to not switch in any more after setting
1271 * its state to PRS_ZOMBIE.
1272 * - A process that referenced this pmap ran on a CPU, but we switched
1273 * to a kernel thread, leaving the pmap pointer unchanged.
1277 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)
1278 atomic_cmpset_rel_ptr((uintptr_t *)&pc->pc_pmap,
1279 (uintptr_t)pm, (uintptr_t)NULL);
1283 if (PCPU_GET(pmap) == pm)
1284 PCPU_SET(pmap, NULL);
1288 pmap_qremove((vm_offset_t)pm->pm_tsb, TSB_PAGES);
1289 obj = pm->pm_tsb_obj;
1290 VM_OBJECT_WLOCK(obj);
1291 KASSERT(obj->ref_count == 1, ("pmap_release: tsbobj ref count != 1"));
1292 while (!TAILQ_EMPTY(&obj->memq)) {
1293 m = TAILQ_FIRST(&obj->memq);
1296 atomic_subtract_int(&cnt.v_wire_count, 1);
1297 vm_page_free_zero(m);
1299 VM_OBJECT_WUNLOCK(obj);
1303 * Grow the number of kernel page table entries. Unneeded.
1306 pmap_growkernel(vm_offset_t addr)
1309 panic("pmap_growkernel: can't grow kernel");
1313 pmap_remove_tte(struct pmap *pm, struct pmap *pm2, struct tte *tp,
1319 rw_assert(&tte_list_global_lock, RA_WLOCKED);
1320 data = atomic_readandclear_long(&tp->tte_data);
1321 if ((data & TD_FAKE) == 0) {
1322 m = PHYS_TO_VM_PAGE(TD_PA(data));
1323 TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
1324 if ((data & TD_WIRED) != 0)
1325 pm->pm_stats.wired_count--;
1326 if ((data & TD_PV) != 0) {
1327 if ((data & TD_W) != 0)
1329 if ((data & TD_REF) != 0)
1330 vm_page_aflag_set(m, PGA_REFERENCED);
1331 if (TAILQ_EMPTY(&m->md.tte_list))
1332 vm_page_aflag_clear(m, PGA_WRITEABLE);
1333 pm->pm_stats.resident_count--;
1335 pmap_cache_remove(m, va);
1338 if (PMAP_REMOVE_DONE(pm))
1344 * Remove the given range of addresses from the specified map.
1347 pmap_remove(pmap_t pm, vm_offset_t start, vm_offset_t end)
1352 CTR3(KTR_PMAP, "pmap_remove: ctx=%#lx start=%#lx end=%#lx",
1353 pm->pm_context[curcpu], start, end);
1354 if (PMAP_REMOVE_DONE(pm))
1356 rw_wlock(&tte_list_global_lock);
1358 if (end - start > PMAP_TSB_THRESH) {
1359 tsb_foreach(pm, NULL, start, end, pmap_remove_tte);
1360 tlb_context_demap(pm);
1362 for (va = start; va < end; va += PAGE_SIZE)
1363 if ((tp = tsb_tte_lookup(pm, va)) != NULL &&
1364 !pmap_remove_tte(pm, NULL, tp, va))
1366 tlb_range_demap(pm, start, end - 1);
1369 rw_wunlock(&tte_list_global_lock);
1373 pmap_remove_all(vm_page_t m)
1380 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1381 ("pmap_remove_all: page %p is not managed", m));
1382 rw_wlock(&tte_list_global_lock);
1383 for (tp = TAILQ_FIRST(&m->md.tte_list); tp != NULL; tp = tpn) {
1384 tpn = TAILQ_NEXT(tp, tte_link);
1385 if ((tp->tte_data & TD_PV) == 0)
1387 pm = TTE_GET_PMAP(tp);
1388 va = TTE_GET_VA(tp);
1390 if ((tp->tte_data & TD_WIRED) != 0)
1391 pm->pm_stats.wired_count--;
1392 if ((tp->tte_data & TD_REF) != 0)
1393 vm_page_aflag_set(m, PGA_REFERENCED);
1394 if ((tp->tte_data & TD_W) != 0)
1396 tp->tte_data &= ~TD_V;
1397 tlb_page_demap(pm, va);
1398 TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
1399 pm->pm_stats.resident_count--;
1400 pmap_cache_remove(m, va);
1404 vm_page_aflag_clear(m, PGA_WRITEABLE);
1405 rw_wunlock(&tte_list_global_lock);
1409 pmap_protect_tte(struct pmap *pm, struct pmap *pm2, struct tte *tp,
1415 PMAP_LOCK_ASSERT(pm, MA_OWNED);
1416 data = atomic_clear_long(&tp->tte_data, TD_SW | TD_W);
1417 if ((data & (TD_PV | TD_W)) == (TD_PV | TD_W)) {
1418 m = PHYS_TO_VM_PAGE(TD_PA(data));
1425 * Set the physical protection on the specified range of this map as requested.
1428 pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1433 CTR4(KTR_PMAP, "pmap_protect: ctx=%#lx sva=%#lx eva=%#lx prot=%#lx",
1434 pm->pm_context[curcpu], sva, eva, prot);
1436 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1437 pmap_remove(pm, sva, eva);
1441 if (prot & VM_PROT_WRITE)
1445 if (eva - sva > PMAP_TSB_THRESH) {
1446 tsb_foreach(pm, NULL, sva, eva, pmap_protect_tte);
1447 tlb_context_demap(pm);
1449 for (va = sva; va < eva; va += PAGE_SIZE)
1450 if ((tp = tsb_tte_lookup(pm, va)) != NULL)
1451 pmap_protect_tte(pm, NULL, tp, va);
1452 tlb_range_demap(pm, sva, eva - 1);
1458 * Map the given physical page at the specified virtual address in the
1459 * target pmap with the protection requested. If specified the page
1460 * will be wired down.
1463 pmap_enter(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1464 u_int flags, int8_t psind)
1468 rw_wlock(&tte_list_global_lock);
1470 rv = pmap_enter_locked(pm, va, m, prot, flags, psind);
1471 rw_wunlock(&tte_list_global_lock);
1477 * Map the given physical page at the specified virtual address in the
1478 * target pmap with the protection requested. If specified the page
1479 * will be wired down.
1481 * The page queues and pmap must be locked.
1484 pmap_enter_locked(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1485 u_int flags, int8_t psind __unused)
1493 rw_assert(&tte_list_global_lock, RA_WLOCKED);
1494 PMAP_LOCK_ASSERT(pm, MA_OWNED);
1495 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
1496 VM_OBJECT_ASSERT_LOCKED(m->object);
1497 PMAP_STATS_INC(pmap_nenter);
1498 pa = VM_PAGE_TO_PHYS(m);
1499 wired = (flags & PMAP_ENTER_WIRED) != 0;
1502 * If this is a fake page from the device_pager, but it covers actual
1503 * physical memory, convert to the real backing page.
1505 if ((m->flags & PG_FICTITIOUS) != 0) {
1506 real = vm_phys_paddr_to_vm_page(pa);
1512 "pmap_enter_locked: ctx=%p m=%p va=%#lx pa=%#lx prot=%#x wired=%d",
1513 pm->pm_context[curcpu], m, va, pa, prot, wired);
1516 * If there is an existing mapping, and the physical address has not
1517 * changed, must be protection or wiring change.
1519 if ((tp = tsb_tte_lookup(pm, va)) != NULL && TTE_GET_PA(tp) == pa) {
1520 CTR0(KTR_PMAP, "pmap_enter_locked: update");
1521 PMAP_STATS_INC(pmap_nenter_update);
1524 * Wiring change, just update stats.
1527 if ((tp->tte_data & TD_WIRED) == 0) {
1528 tp->tte_data |= TD_WIRED;
1529 pm->pm_stats.wired_count++;
1532 if ((tp->tte_data & TD_WIRED) != 0) {
1533 tp->tte_data &= ~TD_WIRED;
1534 pm->pm_stats.wired_count--;
1539 * Save the old bits and clear the ones we're interested in.
1541 data = tp->tte_data;
1542 tp->tte_data &= ~(TD_EXEC | TD_SW | TD_W);
1545 * If we're turning off write permissions, sense modify status.
1547 if ((prot & VM_PROT_WRITE) != 0) {
1548 tp->tte_data |= TD_SW;
1550 tp->tte_data |= TD_W;
1551 if ((m->oflags & VPO_UNMANAGED) == 0)
1552 vm_page_aflag_set(m, PGA_WRITEABLE);
1553 } else if ((data & TD_W) != 0)
1557 * If we're turning on execute permissions, flush the icache.
1559 if ((prot & VM_PROT_EXECUTE) != 0) {
1560 if ((data & TD_EXEC) == 0)
1561 icache_page_inval(pa);
1562 tp->tte_data |= TD_EXEC;
1566 * Delete the old mapping.
1568 tlb_page_demap(pm, TTE_GET_VA(tp));
1571 * If there is an existing mapping, but its for a different
1572 * physical address, delete the old mapping.
1575 CTR0(KTR_PMAP, "pmap_enter_locked: replace");
1576 PMAP_STATS_INC(pmap_nenter_replace);
1577 pmap_remove_tte(pm, NULL, tp, va);
1578 tlb_page_demap(pm, va);
1580 CTR0(KTR_PMAP, "pmap_enter_locked: new");
1581 PMAP_STATS_INC(pmap_nenter_new);
1585 * Now set up the data and install the new mapping.
1587 data = TD_V | TD_8K | TD_PA(pa);
1588 if (pm == kernel_pmap)
1590 if ((prot & VM_PROT_WRITE) != 0) {
1592 if ((m->oflags & VPO_UNMANAGED) == 0)
1593 vm_page_aflag_set(m, PGA_WRITEABLE);
1595 if (prot & VM_PROT_EXECUTE) {
1597 icache_page_inval(pa);
1601 * If its wired update stats. We also don't need reference or
1602 * modify tracking for wired mappings, so set the bits now.
1605 pm->pm_stats.wired_count++;
1606 data |= TD_REF | TD_WIRED;
1607 if ((prot & VM_PROT_WRITE) != 0)
1611 tsb_tte_enter(pm, m, va, TS_8K, data);
1614 return (KERN_SUCCESS);
1618 * Maps a sequence of resident pages belonging to the same object.
1619 * The sequence begins with the given page m_start. This page is
1620 * mapped at the given virtual address start. Each subsequent page is
1621 * mapped at a virtual address that is offset from start by the same
1622 * amount as the page is offset from m_start within the object. The
1623 * last page in the sequence is the page with the largest offset from
1624 * m_start that can be mapped at a virtual address less than the given
1625 * virtual address end. Not every virtual page between start and end
1626 * is mapped; only those for which a resident page exists with the
1627 * corresponding offset from m_start are mapped.
1630 pmap_enter_object(pmap_t pm, vm_offset_t start, vm_offset_t end,
1631 vm_page_t m_start, vm_prot_t prot)
1634 vm_pindex_t diff, psize;
1636 VM_OBJECT_ASSERT_LOCKED(m_start->object);
1638 psize = atop(end - start);
1640 rw_wlock(&tte_list_global_lock);
1642 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1643 pmap_enter_locked(pm, start + ptoa(diff), m, prot &
1644 (VM_PROT_READ | VM_PROT_EXECUTE), 0, 0);
1645 m = TAILQ_NEXT(m, listq);
1647 rw_wunlock(&tte_list_global_lock);
1652 pmap_enter_quick(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot)
1655 rw_wlock(&tte_list_global_lock);
1657 pmap_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1659 rw_wunlock(&tte_list_global_lock);
1664 pmap_object_init_pt(pmap_t pm, vm_offset_t addr, vm_object_t object,
1665 vm_pindex_t pindex, vm_size_t size)
1668 VM_OBJECT_ASSERT_WLOCKED(object);
1669 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
1670 ("pmap_object_init_pt: non-device object"));
1674 pmap_unwire_tte(pmap_t pm, pmap_t pm2, struct tte *tp, vm_offset_t va)
1677 PMAP_LOCK_ASSERT(pm, MA_OWNED);
1678 if ((tp->tte_data & TD_WIRED) == 0)
1679 panic("pmap_unwire_tte: tp %p is missing TD_WIRED", tp);
1680 atomic_clear_long(&tp->tte_data, TD_WIRED);
1681 pm->pm_stats.wired_count--;
1686 * Clear the wired attribute from the mappings for the specified range of
1687 * addresses in the given pmap. Every valid mapping within that range must
1688 * have the wired attribute set. In contrast, invalid mappings cannot have
1689 * the wired attribute set, so they are ignored.
1691 * The wired attribute of the translation table entry is not a hardware
1692 * feature, so there is no need to invalidate any TLB entries.
1695 pmap_unwire(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1701 if (eva - sva > PMAP_TSB_THRESH)
1702 tsb_foreach(pm, NULL, sva, eva, pmap_unwire_tte);
1704 for (va = sva; va < eva; va += PAGE_SIZE)
1705 if ((tp = tsb_tte_lookup(pm, va)) != NULL)
1706 pmap_unwire_tte(pm, NULL, tp, va);
1712 pmap_copy_tte(pmap_t src_pmap, pmap_t dst_pmap, struct tte *tp,
1718 if ((tp->tte_data & TD_FAKE) != 0)
1720 if (tsb_tte_lookup(dst_pmap, va) == NULL) {
1721 data = tp->tte_data &
1722 ~(TD_PV | TD_REF | TD_SW | TD_CV | TD_W);
1723 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
1724 tsb_tte_enter(dst_pmap, m, va, TS_8K, data);
1730 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
1731 vm_size_t len, vm_offset_t src_addr)
1736 if (dst_addr != src_addr)
1738 rw_wlock(&tte_list_global_lock);
1739 if (dst_pmap < src_pmap) {
1740 PMAP_LOCK(dst_pmap);
1741 PMAP_LOCK(src_pmap);
1743 PMAP_LOCK(src_pmap);
1744 PMAP_LOCK(dst_pmap);
1746 if (len > PMAP_TSB_THRESH) {
1747 tsb_foreach(src_pmap, dst_pmap, src_addr, src_addr + len,
1749 tlb_context_demap(dst_pmap);
1751 for (va = src_addr; va < src_addr + len; va += PAGE_SIZE)
1752 if ((tp = tsb_tte_lookup(src_pmap, va)) != NULL)
1753 pmap_copy_tte(src_pmap, dst_pmap, tp, va);
1754 tlb_range_demap(dst_pmap, src_addr, src_addr + len - 1);
1756 rw_wunlock(&tte_list_global_lock);
1757 PMAP_UNLOCK(src_pmap);
1758 PMAP_UNLOCK(dst_pmap);
1762 pmap_zero_page(vm_page_t m)
1768 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1769 ("pmap_zero_page: fake page"));
1770 PMAP_STATS_INC(pmap_nzero_page);
1771 pa = VM_PAGE_TO_PHYS(m);
1772 if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa)) {
1773 PMAP_STATS_INC(pmap_nzero_page_c);
1774 va = TLB_PHYS_TO_DIRECT(pa);
1775 cpu_block_zero((void *)va, PAGE_SIZE);
1776 } else if (m->md.color == -1) {
1777 PMAP_STATS_INC(pmap_nzero_page_nc);
1778 aszero(ASI_PHYS_USE_EC, pa, PAGE_SIZE);
1780 PMAP_STATS_INC(pmap_nzero_page_oc);
1781 PMAP_LOCK(kernel_pmap);
1782 va = pmap_temp_map_1 + (m->md.color * PAGE_SIZE);
1783 tp = tsb_kvtotte(va);
1784 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1785 tp->tte_vpn = TV_VPN(va, TS_8K);
1786 cpu_block_zero((void *)va, PAGE_SIZE);
1787 tlb_page_demap(kernel_pmap, va);
1788 PMAP_UNLOCK(kernel_pmap);
1793 pmap_zero_page_area(vm_page_t m, int off, int size)
1799 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1800 ("pmap_zero_page_area: fake page"));
1801 KASSERT(off + size <= PAGE_SIZE, ("pmap_zero_page_area: bad off/size"));
1802 PMAP_STATS_INC(pmap_nzero_page_area);
1803 pa = VM_PAGE_TO_PHYS(m);
1804 if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa)) {
1805 PMAP_STATS_INC(pmap_nzero_page_area_c);
1806 va = TLB_PHYS_TO_DIRECT(pa);
1807 bzero((void *)(va + off), size);
1808 } else if (m->md.color == -1) {
1809 PMAP_STATS_INC(pmap_nzero_page_area_nc);
1810 aszero(ASI_PHYS_USE_EC, pa + off, size);
1812 PMAP_STATS_INC(pmap_nzero_page_area_oc);
1813 PMAP_LOCK(kernel_pmap);
1814 va = pmap_temp_map_1 + (m->md.color * PAGE_SIZE);
1815 tp = tsb_kvtotte(va);
1816 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1817 tp->tte_vpn = TV_VPN(va, TS_8K);
1818 bzero((void *)(va + off), size);
1819 tlb_page_demap(kernel_pmap, va);
1820 PMAP_UNLOCK(kernel_pmap);
1825 pmap_zero_page_idle(vm_page_t m)
1831 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1832 ("pmap_zero_page_idle: fake page"));
1833 PMAP_STATS_INC(pmap_nzero_page_idle);
1834 pa = VM_PAGE_TO_PHYS(m);
1835 if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa)) {
1836 PMAP_STATS_INC(pmap_nzero_page_idle_c);
1837 va = TLB_PHYS_TO_DIRECT(pa);
1838 cpu_block_zero((void *)va, PAGE_SIZE);
1839 } else if (m->md.color == -1) {
1840 PMAP_STATS_INC(pmap_nzero_page_idle_nc);
1841 aszero(ASI_PHYS_USE_EC, pa, PAGE_SIZE);
1843 PMAP_STATS_INC(pmap_nzero_page_idle_oc);
1844 va = pmap_idle_map + (m->md.color * PAGE_SIZE);
1845 tp = tsb_kvtotte(va);
1846 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1847 tp->tte_vpn = TV_VPN(va, TS_8K);
1848 cpu_block_zero((void *)va, PAGE_SIZE);
1849 tlb_page_demap(kernel_pmap, va);
1854 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
1862 KASSERT((mdst->flags & PG_FICTITIOUS) == 0,
1863 ("pmap_copy_page: fake dst page"));
1864 KASSERT((msrc->flags & PG_FICTITIOUS) == 0,
1865 ("pmap_copy_page: fake src page"));
1866 PMAP_STATS_INC(pmap_ncopy_page);
1867 pdst = VM_PAGE_TO_PHYS(mdst);
1868 psrc = VM_PAGE_TO_PHYS(msrc);
1869 if (dcache_color_ignore != 0 ||
1870 (msrc->md.color == DCACHE_COLOR(psrc) &&
1871 mdst->md.color == DCACHE_COLOR(pdst))) {
1872 PMAP_STATS_INC(pmap_ncopy_page_c);
1873 vdst = TLB_PHYS_TO_DIRECT(pdst);
1874 vsrc = TLB_PHYS_TO_DIRECT(psrc);
1875 cpu_block_copy((void *)vsrc, (void *)vdst, PAGE_SIZE);
1876 } else if (msrc->md.color == -1 && mdst->md.color == -1) {
1877 PMAP_STATS_INC(pmap_ncopy_page_nc);
1878 ascopy(ASI_PHYS_USE_EC, psrc, pdst, PAGE_SIZE);
1879 } else if (msrc->md.color == -1) {
1880 if (mdst->md.color == DCACHE_COLOR(pdst)) {
1881 PMAP_STATS_INC(pmap_ncopy_page_dc);
1882 vdst = TLB_PHYS_TO_DIRECT(pdst);
1883 ascopyfrom(ASI_PHYS_USE_EC, psrc, (void *)vdst,
1886 PMAP_STATS_INC(pmap_ncopy_page_doc);
1887 PMAP_LOCK(kernel_pmap);
1888 vdst = pmap_temp_map_1 + (mdst->md.color * PAGE_SIZE);
1889 tp = tsb_kvtotte(vdst);
1891 TD_V | TD_8K | TD_PA(pdst) | TD_CP | TD_CV | TD_W;
1892 tp->tte_vpn = TV_VPN(vdst, TS_8K);
1893 ascopyfrom(ASI_PHYS_USE_EC, psrc, (void *)vdst,
1895 tlb_page_demap(kernel_pmap, vdst);
1896 PMAP_UNLOCK(kernel_pmap);
1898 } else if (mdst->md.color == -1) {
1899 if (msrc->md.color == DCACHE_COLOR(psrc)) {
1900 PMAP_STATS_INC(pmap_ncopy_page_sc);
1901 vsrc = TLB_PHYS_TO_DIRECT(psrc);
1902 ascopyto((void *)vsrc, ASI_PHYS_USE_EC, pdst,
1905 PMAP_STATS_INC(pmap_ncopy_page_soc);
1906 PMAP_LOCK(kernel_pmap);
1907 vsrc = pmap_temp_map_1 + (msrc->md.color * PAGE_SIZE);
1908 tp = tsb_kvtotte(vsrc);
1910 TD_V | TD_8K | TD_PA(psrc) | TD_CP | TD_CV | TD_W;
1911 tp->tte_vpn = TV_VPN(vsrc, TS_8K);
1912 ascopyto((void *)vsrc, ASI_PHYS_USE_EC, pdst,
1914 tlb_page_demap(kernel_pmap, vsrc);
1915 PMAP_UNLOCK(kernel_pmap);
1918 PMAP_STATS_INC(pmap_ncopy_page_oc);
1919 PMAP_LOCK(kernel_pmap);
1920 vdst = pmap_temp_map_1 + (mdst->md.color * PAGE_SIZE);
1921 tp = tsb_kvtotte(vdst);
1923 TD_V | TD_8K | TD_PA(pdst) | TD_CP | TD_CV | TD_W;
1924 tp->tte_vpn = TV_VPN(vdst, TS_8K);
1925 vsrc = pmap_temp_map_2 + (msrc->md.color * PAGE_SIZE);
1926 tp = tsb_kvtotte(vsrc);
1928 TD_V | TD_8K | TD_PA(psrc) | TD_CP | TD_CV | TD_W;
1929 tp->tte_vpn = TV_VPN(vsrc, TS_8K);
1930 cpu_block_copy((void *)vsrc, (void *)vdst, PAGE_SIZE);
1931 tlb_page_demap(kernel_pmap, vdst);
1932 tlb_page_demap(kernel_pmap, vsrc);
1933 PMAP_UNLOCK(kernel_pmap);
1937 int unmapped_buf_allowed;
1940 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
1941 vm_offset_t b_offset, int xfersize)
1944 panic("pmap_copy_pages: not implemented");
1948 * Returns true if the pmap's pv is one of the first
1949 * 16 pvs linked to from this page. This count may
1950 * be changed upwards or downwards in the future; it
1951 * is only necessary that true be returned for a small
1952 * subset of pmaps for proper page aging.
1955 pmap_page_exists_quick(pmap_t pm, vm_page_t m)
1961 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1962 ("pmap_page_exists_quick: page %p is not managed", m));
1965 rw_wlock(&tte_list_global_lock);
1966 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
1967 if ((tp->tte_data & TD_PV) == 0)
1969 if (TTE_GET_PMAP(tp) == pm) {
1976 rw_wunlock(&tte_list_global_lock);
1981 * Return the number of managed mappings to the given physical page
1985 pmap_page_wired_mappings(vm_page_t m)
1991 if ((m->oflags & VPO_UNMANAGED) != 0)
1993 rw_wlock(&tte_list_global_lock);
1994 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link)
1995 if ((tp->tte_data & (TD_PV | TD_WIRED)) == (TD_PV | TD_WIRED))
1997 rw_wunlock(&tte_list_global_lock);
2002 * Remove all pages from specified address space, this aids process exit
2003 * speeds. This is much faster than pmap_remove in the case of running down
2004 * an entire address space. Only works for the current pmap.
2007 pmap_remove_pages(pmap_t pm)
2013 * Returns TRUE if the given page has a managed mapping.
2016 pmap_page_is_mapped(vm_page_t m)
2022 if ((m->oflags & VPO_UNMANAGED) != 0)
2024 rw_wlock(&tte_list_global_lock);
2025 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link)
2026 if ((tp->tte_data & TD_PV) != 0) {
2030 rw_wunlock(&tte_list_global_lock);
2035 * Return a count of reference bits for a page, clearing those bits.
2036 * It is not necessary for every reference bit to be cleared, but it
2037 * is necessary that 0 only be returned when there are truly no
2038 * reference bits set.
2040 * XXX: The exact number of bits to check and clear is a matter that
2041 * should be tested and standardized at some point in the future for
2042 * optimal aging of shared pages.
2045 pmap_ts_referenced(vm_page_t m)
2053 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2054 ("pmap_ts_referenced: page %p is not managed", m));
2056 rw_wlock(&tte_list_global_lock);
2057 if ((tp = TAILQ_FIRST(&m->md.tte_list)) != NULL) {
2060 tpn = TAILQ_NEXT(tp, tte_link);
2061 TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
2062 TAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link);
2063 if ((tp->tte_data & TD_PV) == 0)
2065 data = atomic_clear_long(&tp->tte_data, TD_REF);
2066 if ((data & TD_REF) != 0 && ++count > 4)
2068 } while ((tp = tpn) != NULL && tp != tpf);
2070 rw_wunlock(&tte_list_global_lock);
2075 pmap_is_modified(vm_page_t m)
2080 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2081 ("pmap_is_modified: page %p is not managed", m));
2085 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2086 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
2087 * is clear, no TTEs can have TD_W set.
2089 VM_OBJECT_ASSERT_WLOCKED(m->object);
2090 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2092 rw_wlock(&tte_list_global_lock);
2093 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2094 if ((tp->tte_data & TD_PV) == 0)
2096 if ((tp->tte_data & TD_W) != 0) {
2101 rw_wunlock(&tte_list_global_lock);
2106 * pmap_is_prefaultable:
2108 * Return whether or not the specified virtual address is elgible
2112 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2117 rv = tsb_tte_lookup(pmap, addr) == NULL;
2123 * Return whether or not the specified physical page was referenced
2124 * in any physical maps.
2127 pmap_is_referenced(vm_page_t m)
2132 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2133 ("pmap_is_referenced: page %p is not managed", m));
2135 rw_wlock(&tte_list_global_lock);
2136 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2137 if ((tp->tte_data & TD_PV) == 0)
2139 if ((tp->tte_data & TD_REF) != 0) {
2144 rw_wunlock(&tte_list_global_lock);
2149 * This function is advisory.
2152 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
2157 pmap_clear_modify(vm_page_t m)
2162 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2163 ("pmap_clear_modify: page %p is not managed", m));
2164 VM_OBJECT_ASSERT_WLOCKED(m->object);
2165 KASSERT(!vm_page_xbusied(m),
2166 ("pmap_clear_modify: page %p is exclusive busied", m));
2169 * If the page is not PGA_WRITEABLE, then no TTEs can have TD_W set.
2170 * If the object containing the page is locked and the page is not
2171 * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
2173 if ((m->aflags & PGA_WRITEABLE) == 0)
2175 rw_wlock(&tte_list_global_lock);
2176 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2177 if ((tp->tte_data & TD_PV) == 0)
2179 data = atomic_clear_long(&tp->tte_data, TD_W);
2180 if ((data & TD_W) != 0)
2181 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
2183 rw_wunlock(&tte_list_global_lock);
2187 pmap_remove_write(vm_page_t m)
2192 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2193 ("pmap_remove_write: page %p is not managed", m));
2196 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2197 * set by another thread while the object is locked. Thus,
2198 * if PGA_WRITEABLE is clear, no page table entries need updating.
2200 VM_OBJECT_ASSERT_WLOCKED(m->object);
2201 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2203 rw_wlock(&tte_list_global_lock);
2204 TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2205 if ((tp->tte_data & TD_PV) == 0)
2207 data = atomic_clear_long(&tp->tte_data, TD_SW | TD_W);
2208 if ((data & TD_W) != 0) {
2210 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
2213 vm_page_aflag_clear(m, PGA_WRITEABLE);
2214 rw_wunlock(&tte_list_global_lock);
2218 pmap_mincore(pmap_t pm, vm_offset_t addr, vm_paddr_t *locked_pa)
2226 * Activate a user pmap. The pmap must be activated before its address space
2227 * can be accessed in any way.
2230 pmap_activate(struct thread *td)
2237 vm = td->td_proc->p_vmspace;
2238 pm = vmspace_pmap(vm);
2240 context = PCPU_GET(tlb_ctx);
2241 if (context == PCPU_GET(tlb_ctx_max)) {
2243 context = PCPU_GET(tlb_ctx_min);
2245 PCPU_SET(tlb_ctx, context + 1);
2247 pm->pm_context[curcpu] = context;
2249 CPU_SET_ATOMIC(PCPU_GET(cpuid), &pm->pm_active);
2250 atomic_store_acq_ptr((uintptr_t *)PCPU_PTR(pmap), (uintptr_t)pm);
2252 CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
2256 stxa(AA_DMMU_TSB, ASI_DMMU, pm->pm_tsb);
2257 stxa(AA_IMMU_TSB, ASI_IMMU, pm->pm_tsb);
2258 stxa(AA_DMMU_PCXR, ASI_DMMU, (ldxa(AA_DMMU_PCXR, ASI_DMMU) &
2259 TLB_CXR_PGSZ_MASK) | context);
2265 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
2271 * Increase the starting virtual address of the given mapping if a
2272 * different alignment might result in more superpage mappings.
2275 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
2276 vm_offset_t *addr, vm_size_t size)