2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991 Regents of the University of California.
6 * Copyright (c) 1994 John S. Dyson
8 * Copyright (c) 1994 David Greenman
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
13 * Science Department and William Jolitz of UUNET Technologies Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
40 * from: src/sys/i386/i386/pmap.c,v 1.250.2.8 2000/11/21 00:09:14 ps
41 * JNPR: pmap.c,v 1.11.2.1 2007/08/16 11:51:06 girish
45 * Manages physical address maps.
47 * Since the information managed by this module is
48 * also stored by the logical address mapping module,
49 * this module may throw away valid virtual-to-physical
50 * mappings at almost any time. However, invalidations
51 * of virtual-to-physical mappings must be done as
54 * In order to cope with hardware architectures which
55 * make virtual-to-physical map invalidates expensive,
56 * this module may delay invalidate or reduced protection
57 * operations until such time as they are actually
58 * necessary. This module is given full information as
59 * to which processors are currently using which maps,
60 * and to when physical maps must be made correct.
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
69 #include <sys/param.h>
70 #include <sys/systm.h>
73 #include <sys/msgbuf.h>
74 #include <sys/mutex.h>
77 #include <sys/rwlock.h>
78 #include <sys/sched.h>
80 #include <sys/sysctl.h>
81 #include <sys/vmmeter.h>
88 #include <vm/vm_param.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_extern.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_pager.h>
98 #include <machine/cache.h>
99 #include <machine/md_var.h>
100 #include <machine/tlb.h>
104 #if !defined(DIAGNOSTIC)
105 #define PMAP_INLINE __inline
111 #define PV_STAT(x) do { x ; } while (0)
113 #define PV_STAT(x) do { } while (0)
117 * Get PDEs and PTEs for user/kernel address space
119 #define pmap_seg_index(v) (((v) >> SEGSHIFT) & (NPDEPG - 1))
120 #define pmap_pde_index(v) (((v) >> PDRSHIFT) & (NPDEPG - 1))
121 #define pmap_pte_index(v) (((v) >> PAGE_SHIFT) & (NPTEPG - 1))
122 #define pmap_pde_pindex(v) ((v) >> PDRSHIFT)
125 #define NUPDE (NPDEPG * NPDEPG)
126 #define NUSERPGTBLS (NUPDE + NPDEPG)
128 #define NUPDE (NPDEPG)
129 #define NUSERPGTBLS (NUPDE)
132 #define is_kernel_pmap(x) ((x) == kernel_pmap)
134 struct pmap kernel_pmap_store;
135 pd_entry_t *kernel_segmap;
137 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
138 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
141 unsigned pmap_max_asid; /* max ASID supported by the system */
143 #define PMAP_ASID_RESERVED 0
145 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
147 static void pmap_asid_alloc(pmap_t pmap);
149 static struct rwlock_padalign pvh_global_lock;
152 * Data for the pv entry allocation mechanism
154 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
155 static int pv_entry_count;
157 static void free_pv_chunk(struct pv_chunk *pc);
158 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
159 static pv_entry_t get_pv_entry(pmap_t pmap, boolean_t try);
160 static vm_page_t pmap_pv_reclaim(pmap_t locked_pmap);
161 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
162 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
164 static vm_page_t pmap_alloc_direct_page(unsigned int index, int req);
165 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
166 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
167 static void pmap_grow_direct_page(int req);
168 static int pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
170 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
171 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va);
172 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte,
173 vm_offset_t va, vm_page_t m);
174 static void pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte);
175 static void pmap_invalidate_all(pmap_t pmap);
176 static void pmap_invalidate_page(pmap_t pmap, vm_offset_t va);
177 static void _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m);
179 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags);
180 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, u_int flags);
181 static int pmap_unuse_pt(pmap_t, vm_offset_t, pd_entry_t);
182 static pt_entry_t init_pte_prot(vm_page_t m, vm_prot_t access, vm_prot_t prot);
184 static void pmap_invalidate_page_action(void *arg);
185 static void pmap_invalidate_range_action(void *arg);
186 static void pmap_update_page_action(void *arg);
190 * This structure is for high memory (memory above 512Meg in 32 bit) support.
191 * The highmem area does not have a KSEG0 mapping, and we need a mechanism to
192 * do temporary per-CPU mappings for pmap_zero_page, pmap_copy_page etc.
194 * At bootup, we reserve 2 virtual pages per CPU for mapping highmem pages. To
195 * access a highmem physical address on a CPU, we map the physical address to
196 * the reserved virtual address for the CPU in the kernel pagetable. This is
197 * done with interrupts disabled(although a spinlock and sched_pin would be
200 struct local_sysmaps {
203 uint16_t valid1, valid2;
205 static struct local_sysmaps sysmap_lmem[MAXCPU];
208 pmap_alloc_lmem_map(void)
212 for (i = 0; i < MAXCPU; i++) {
213 sysmap_lmem[i].base = virtual_avail;
214 virtual_avail += PAGE_SIZE * 2;
215 sysmap_lmem[i].valid1 = sysmap_lmem[i].valid2 = 0;
219 static __inline vm_offset_t
220 pmap_lmem_map1(vm_paddr_t phys)
222 struct local_sysmaps *sysm;
223 pt_entry_t *pte, npte;
228 intr = intr_disable();
229 cpu = PCPU_GET(cpuid);
230 sysm = &sysmap_lmem[cpu];
231 sysm->saved_intr = intr;
233 npte = TLBLO_PA_TO_PFN(phys) | PTE_C_CACHE | PTE_D | PTE_V | PTE_G;
234 pte = pmap_pte(kernel_pmap, va);
240 static __inline vm_offset_t
241 pmap_lmem_map2(vm_paddr_t phys1, vm_paddr_t phys2)
243 struct local_sysmaps *sysm;
244 pt_entry_t *pte, npte;
245 vm_offset_t va1, va2;
249 intr = intr_disable();
250 cpu = PCPU_GET(cpuid);
251 sysm = &sysmap_lmem[cpu];
252 sysm->saved_intr = intr;
254 va2 = sysm->base + PAGE_SIZE;
255 npte = TLBLO_PA_TO_PFN(phys1) | PTE_C_CACHE | PTE_D | PTE_V | PTE_G;
256 pte = pmap_pte(kernel_pmap, va1);
258 npte = TLBLO_PA_TO_PFN(phys2) | PTE_C_CACHE | PTE_D | PTE_V | PTE_G;
259 pte = pmap_pte(kernel_pmap, va2);
267 pmap_lmem_unmap(void)
269 struct local_sysmaps *sysm;
273 cpu = PCPU_GET(cpuid);
274 sysm = &sysmap_lmem[cpu];
275 pte = pmap_pte(kernel_pmap, sysm->base);
277 tlb_invalidate_address(kernel_pmap, sysm->base);
280 pte = pmap_pte(kernel_pmap, sysm->base + PAGE_SIZE);
282 tlb_invalidate_address(kernel_pmap, sysm->base + PAGE_SIZE);
285 intr_restore(sysm->saved_intr);
287 #else /* __mips_n64 */
290 pmap_alloc_lmem_map(void)
294 static __inline vm_offset_t
295 pmap_lmem_map1(vm_paddr_t phys)
301 static __inline vm_offset_t
302 pmap_lmem_map2(vm_paddr_t phys1, vm_paddr_t phys2)
308 static __inline vm_offset_t
309 pmap_lmem_unmap(void)
314 #endif /* !__mips_n64 */
317 pmap_pte_cache_bits(vm_paddr_t pa, vm_page_t m)
321 ma = pmap_page_get_memattr(m);
322 if (ma == VM_MEMATTR_WRITE_BACK && !is_cacheable_mem(pa))
323 ma = VM_MEMATTR_UNCACHEABLE;
326 #define PMAP_PTE_SET_CACHE_BITS(pte, ps, m) { \
327 pte &= ~PTE_C_MASK; \
328 pte |= pmap_pte_cache_bits(pa, m); \
332 * Page table entry lookup routines.
334 static __inline pd_entry_t *
335 pmap_segmap(pmap_t pmap, vm_offset_t va)
338 return (&pmap->pm_segtab[pmap_seg_index(va)]);
342 static __inline pd_entry_t *
343 pmap_pdpe_to_pde(pd_entry_t *pdpe, vm_offset_t va)
347 pde = (pd_entry_t *)*pdpe;
348 return (&pde[pmap_pde_index(va)]);
351 static __inline pd_entry_t *
352 pmap_pde(pmap_t pmap, vm_offset_t va)
356 pdpe = pmap_segmap(pmap, va);
360 return (pmap_pdpe_to_pde(pdpe, va));
363 static __inline pd_entry_t *
364 pmap_pdpe_to_pde(pd_entry_t *pdpe, vm_offset_t va)
371 pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va)
374 return (pmap_segmap(pmap, va));
378 static __inline pt_entry_t *
379 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
383 pte = (pt_entry_t *)*pde;
384 return (&pte[pmap_pte_index(va)]);
388 pmap_pte(pmap_t pmap, vm_offset_t va)
392 pde = pmap_pde(pmap, va);
393 if (pde == NULL || *pde == NULL)
396 return (pmap_pde_to_pte(pde, va));
400 pmap_steal_memory(vm_size_t size)
402 vm_paddr_t bank_size, pa;
405 size = round_page(size);
406 bank_size = phys_avail[1] - phys_avail[0];
407 while (size > bank_size) {
410 for (i = 0; phys_avail[i + 2]; i += 2) {
411 phys_avail[i] = phys_avail[i + 2];
412 phys_avail[i + 1] = phys_avail[i + 3];
415 phys_avail[i + 1] = 0;
417 panic("pmap_steal_memory: out of memory");
418 bank_size = phys_avail[1] - phys_avail[0];
422 phys_avail[0] += size;
423 if (MIPS_DIRECT_MAPPABLE(pa) == 0)
424 panic("Out of memory below 512Meg?");
425 va = MIPS_PHYS_TO_DIRECT(pa);
426 bzero((caddr_t)va, size);
431 * Bootstrap the system enough to run with virtual memory. This
432 * assumes that the phys_avail array has been initialized.
435 pmap_create_kernel_pagetable(void)
447 * Allocate segment table for the kernel
449 kernel_segmap = (pd_entry_t *)pmap_steal_memory(PAGE_SIZE);
452 * Allocate second level page tables for the kernel
455 npde = howmany(NKPT, NPDEPG);
456 pdaddr = pmap_steal_memory(PAGE_SIZE * npde);
459 ptaddr = pmap_steal_memory(PAGE_SIZE * nkpt);
462 * The R[4-7]?00 stores only one copy of the Global bit in the
463 * translation lookaside buffer for each 2 page entry. Thus invalid
464 * entrys must have the Global bit set so when Entry LO and Entry HI
465 * G bits are anded together they will produce a global bit to store
468 for (i = 0, pte = (pt_entry_t *)ptaddr; i < (nkpt * NPTEPG); i++, pte++)
472 for (i = 0, npt = nkpt; npt > 0; i++) {
473 kernel_segmap[i] = (pd_entry_t)(pdaddr + i * PAGE_SIZE);
474 pde = (pd_entry_t *)kernel_segmap[i];
476 for (j = 0; j < NPDEPG && npt > 0; j++, npt--)
477 pde[j] = (pd_entry_t)(ptaddr + (i * NPDEPG + j) * PAGE_SIZE);
480 for (i = 0, j = pmap_seg_index(VM_MIN_KERNEL_ADDRESS); i < nkpt; i++, j++)
481 kernel_segmap[j] = (pd_entry_t)(ptaddr + (i * PAGE_SIZE));
484 PMAP_LOCK_INIT(kernel_pmap);
485 kernel_pmap->pm_segtab = kernel_segmap;
486 CPU_FILL(&kernel_pmap->pm_active);
487 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
488 kernel_pmap->pm_asid[0].asid = PMAP_ASID_RESERVED;
489 kernel_pmap->pm_asid[0].gen = 0;
490 kernel_vm_end += nkpt * NPTEPG * PAGE_SIZE;
497 int need_local_mappings = 0;
501 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
503 * Keep the memory aligned on page boundary.
505 phys_avail[i] = round_page(phys_avail[i]);
506 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
510 if (phys_avail[i - 2] > phys_avail[i]) {
513 ptemp[0] = phys_avail[i + 0];
514 ptemp[1] = phys_avail[i + 1];
516 phys_avail[i + 0] = phys_avail[i - 2];
517 phys_avail[i + 1] = phys_avail[i - 1];
519 phys_avail[i - 2] = ptemp[0];
520 phys_avail[i - 1] = ptemp[1];
526 * In 32 bit, we may have memory which cannot be mapped directly.
527 * This memory will need temporary mapping before it can be
530 if (!MIPS_DIRECT_MAPPABLE(phys_avail[i - 1] - 1))
531 need_local_mappings = 1;
534 * Copy the phys_avail[] array before we start stealing memory from it.
536 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
537 physmem_desc[i] = phys_avail[i];
538 physmem_desc[i + 1] = phys_avail[i + 1];
541 Maxmem = atop(phys_avail[i - 1]);
544 printf("Physical memory chunk(s):\n");
545 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
548 size = phys_avail[i + 1] - phys_avail[i];
549 printf("%#08jx - %#08jx, %ju bytes (%ju pages)\n",
550 (uintmax_t) phys_avail[i],
551 (uintmax_t) phys_avail[i + 1] - 1,
552 (uintmax_t) size, (uintmax_t) size / PAGE_SIZE);
554 printf("Maxmem is 0x%0jx\n", ptoa((uintmax_t)Maxmem));
557 * Steal the message buffer from the beginning of memory.
559 msgbufp = (struct msgbuf *)pmap_steal_memory(msgbufsize);
560 msgbufinit(msgbufp, msgbufsize);
563 * Steal thread0 kstack.
565 kstack0 = pmap_steal_memory(KSTACK_PAGES << PAGE_SHIFT);
567 virtual_avail = VM_MIN_KERNEL_ADDRESS;
568 virtual_end = VM_MAX_KERNEL_ADDRESS;
572 * Steal some virtual address space to map the pcpu area.
574 virtual_avail = roundup2(virtual_avail, PAGE_SIZE * 2);
575 pcpup = (struct pcpu *)virtual_avail;
576 virtual_avail += PAGE_SIZE * 2;
579 * Initialize the wired TLB entry mapping the pcpu region for
580 * the BSP at 'pcpup'. Up until this point we were operating
581 * with the 'pcpup' for the BSP pointing to a virtual address
582 * in KSEG0 so there was no need for a TLB mapping.
584 mips_pcpu_tlb_init(PCPU_ADDR(0));
587 printf("pcpu is available at virtual address %p.\n", pcpup);
590 if (need_local_mappings)
591 pmap_alloc_lmem_map();
592 pmap_create_kernel_pagetable();
593 pmap_max_asid = VMNUM_PIDS;
598 * Initialize the global pv list lock.
600 rw_init(&pvh_global_lock, "pmap pv global");
604 * Initialize a vm_page's machine-dependent fields.
607 pmap_page_init(vm_page_t m)
610 TAILQ_INIT(&m->md.pv_list);
611 m->md.pv_flags = VM_MEMATTR_DEFAULT << PV_MEMATTR_SHIFT;
615 * Initialize the pmap module.
616 * Called by vm_init, to initialize any structures that the pmap
617 * system needs to map virtual memory.
624 /***************************************************
625 * Low level helper routines.....
626 ***************************************************/
630 pmap_call_on_active_cpus(pmap_t pmap, void (*fn)(void *), void *arg)
632 int cpuid, cpu, self;
633 cpuset_t active_cpus;
636 if (is_kernel_pmap(pmap)) {
637 smp_rendezvous(NULL, fn, NULL, arg);
640 /* Force ASID update on inactive CPUs */
642 if (!CPU_ISSET(cpu, &pmap->pm_active))
643 pmap->pm_asid[cpu].gen = 0;
645 cpuid = PCPU_GET(cpuid);
647 * XXX: barrier/locking for active?
649 * Take a snapshot of active here, any further changes are ignored.
650 * tlb update/invalidate should be harmless on inactive CPUs
652 active_cpus = pmap->pm_active;
653 self = CPU_ISSET(cpuid, &active_cpus);
654 CPU_CLR(cpuid, &active_cpus);
655 /* Optimize for the case where this cpu is the only active one */
656 if (CPU_EMPTY(&active_cpus)) {
661 CPU_SET(cpuid, &active_cpus);
662 smp_rendezvous_cpus(active_cpus, NULL, fn, NULL, arg);
669 pmap_call_on_active_cpus(pmap_t pmap, void (*fn)(void *), void *arg)
673 if (is_kernel_pmap(pmap)) {
677 cpuid = PCPU_GET(cpuid);
678 if (!CPU_ISSET(cpuid, &pmap->pm_active))
679 pmap->pm_asid[cpuid].gen = 0;
686 pmap_invalidate_all(pmap_t pmap)
689 pmap_call_on_active_cpus(pmap,
690 (void (*)(void *))tlb_invalidate_all_user, pmap);
693 struct pmap_invalidate_page_arg {
699 pmap_invalidate_page_action(void *arg)
701 struct pmap_invalidate_page_arg *p = arg;
703 tlb_invalidate_address(p->pmap, p->va);
707 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
709 struct pmap_invalidate_page_arg arg;
713 pmap_call_on_active_cpus(pmap, pmap_invalidate_page_action, &arg);
716 struct pmap_invalidate_range_arg {
723 pmap_invalidate_range_action(void *arg)
725 struct pmap_invalidate_range_arg *p = arg;
727 tlb_invalidate_range(p->pmap, p->sva, p->eva);
731 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
733 struct pmap_invalidate_range_arg arg;
738 pmap_call_on_active_cpus(pmap, pmap_invalidate_range_action, &arg);
741 struct pmap_update_page_arg {
748 pmap_update_page_action(void *arg)
750 struct pmap_update_page_arg *p = arg;
752 tlb_update(p->pmap, p->va, p->pte);
756 pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte)
758 struct pmap_update_page_arg arg;
763 pmap_call_on_active_cpus(pmap, pmap_update_page_action, &arg);
767 * Routine: pmap_extract
769 * Extract the physical page address associated
770 * with the given map/virtual_address pair.
773 pmap_extract(pmap_t pmap, vm_offset_t va)
776 vm_offset_t retval = 0;
779 pte = pmap_pte(pmap, va);
781 retval = TLBLO_PTE_TO_PA(*pte) | (va & PAGE_MASK);
788 * Routine: pmap_extract_and_hold
790 * Atomically extract and hold the physical page
791 * with the given pmap and virtual address pair
792 * if that mapping permits the given protection.
795 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
797 pt_entry_t pte, *ptep;
798 vm_paddr_t pa, pte_pa;
805 ptep = pmap_pte(pmap, va);
808 if (pte_test(&pte, PTE_V) && (!pte_test(&pte, PTE_RO) ||
809 (prot & VM_PROT_WRITE) == 0)) {
810 pte_pa = TLBLO_PTE_TO_PA(pte);
811 if (vm_page_pa_tryrelock(pmap, pte_pa, &pa))
813 m = PHYS_TO_VM_PAGE(pte_pa);
822 /***************************************************
823 * Low level mapping routines.....
824 ***************************************************/
827 * add a wired page to the kva
830 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma)
833 pt_entry_t opte, npte;
836 printf("pmap_kenter: va: %p -> pa: %p\n", (void *)va, (void *)pa);
839 pte = pmap_pte(kernel_pmap, va);
841 npte = TLBLO_PA_TO_PFN(pa) | PTE_C(ma) | PTE_D | PTE_V | PTE_G;
843 if (pte_test(&opte, PTE_V) && opte != npte)
844 pmap_update_page(kernel_pmap, va, npte);
848 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
851 KASSERT(is_cacheable_mem(pa),
852 ("pmap_kenter: memory at 0x%lx is not cacheable", (u_long)pa));
854 pmap_kenter_attr(va, pa, VM_MEMATTR_DEFAULT);
858 * remove a page from the kernel pagetables
860 /* PMAP_INLINE */ void
861 pmap_kremove(vm_offset_t va)
866 * Write back all caches from the page being destroyed
868 mips_dcache_wbinv_range_index(va, PAGE_SIZE);
870 pte = pmap_pte(kernel_pmap, va);
872 pmap_invalidate_page(kernel_pmap, va);
876 * Used to map a range of physical addresses into kernel
877 * virtual address space.
879 * The value passed in '*virt' is a suggested virtual address for
880 * the mapping. Architectures which can support a direct-mapped
881 * physical to virtual region can return the appropriate address
882 * within that region, leaving '*virt' unchanged. Other
883 * architectures should map the pages starting at '*virt' and
884 * update '*virt' with the first usable address after the mapped
887 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
890 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
894 if (MIPS_DIRECT_MAPPABLE(end - 1))
895 return (MIPS_PHYS_TO_DIRECT(start));
898 while (start < end) {
899 pmap_kenter(va, start);
908 * Add a list of wired pages to the kva
909 * this routine is only used for temporary
910 * kernel mappings that do not need to have
911 * page modification or references recorded.
912 * Note that old mappings are simply written
913 * over. The page *must* be wired.
916 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
919 vm_offset_t origva = va;
921 for (i = 0; i < count; i++) {
922 pmap_flush_pvcache(m[i]);
923 pmap_kenter(va, VM_PAGE_TO_PHYS(m[i]));
927 mips_dcache_wbinv_range_index(origva, PAGE_SIZE*count);
931 * this routine jerks page mappings from the
932 * kernel -- it is meant only for temporary mappings.
935 pmap_qremove(vm_offset_t va, int count)
942 mips_dcache_wbinv_range_index(va, PAGE_SIZE * count);
945 pte = pmap_pte(kernel_pmap, va);
948 } while (--count > 0);
949 pmap_invalidate_range(kernel_pmap, origva, va);
952 /***************************************************
953 * Page table page management routines.....
954 ***************************************************/
957 * Decrements a page table page's wire count, which is used to record the
958 * number of valid page table entries within the page. If the wire count
959 * drops to zero, then the page table page is unmapped. Returns TRUE if the
960 * page table page was unmapped and FALSE otherwise.
962 static PMAP_INLINE boolean_t
963 pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m)
967 if (m->wire_count == 0) {
968 _pmap_unwire_ptp(pmap, va, m);
975 _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m)
979 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
981 * unmap the page table page
984 if (m->pindex < NUPDE)
985 pde = pmap_pde(pmap, va);
987 pde = pmap_segmap(pmap, va);
989 pde = pmap_pde(pmap, va);
992 pmap->pm_stats.resident_count--;
995 if (m->pindex < NUPDE) {
1000 * Recursively decrement next level pagetable refcount
1002 pdp = (pd_entry_t *)*pmap_segmap(pmap, va);
1003 pdpg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(pdp));
1004 pmap_unwire_ptp(pmap, va, pdpg);
1009 * If the page is finally unwired, simply free it.
1011 vm_page_free_zero(m);
1016 * After removing a page table entry, this routine is used to
1017 * conditionally free the page, and manage the hold/wire counts.
1020 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pd_entry_t pde)
1024 if (va >= VM_MAXUSER_ADDRESS)
1026 KASSERT(pde != 0, ("pmap_unuse_pt: pde != 0"));
1027 mpte = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(pde));
1028 return (pmap_unwire_ptp(pmap, va, mpte));
1032 pmap_pinit0(pmap_t pmap)
1036 PMAP_LOCK_INIT(pmap);
1037 pmap->pm_segtab = kernel_segmap;
1038 CPU_ZERO(&pmap->pm_active);
1039 for (i = 0; i < MAXCPU; i++) {
1040 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
1041 pmap->pm_asid[i].gen = 0;
1043 PCPU_SET(curpmap, pmap);
1044 TAILQ_INIT(&pmap->pm_pvchunk);
1045 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1049 pmap_grow_direct_page(int req)
1055 if (!vm_page_reclaim_contig(req, 1, 0, MIPS_KSEG0_LARGEST_PHYS,
1062 pmap_alloc_direct_page(unsigned int index, int req)
1066 m = vm_page_alloc_freelist(VM_FREELIST_DIRECT, req | VM_ALLOC_WIRED |
1071 if ((m->flags & PG_ZERO) == 0)
1079 * Initialize a preallocated and zeroed pmap structure,
1080 * such as one in a vmspace structure.
1083 pmap_pinit(pmap_t pmap)
1090 * allocate the page directory page
1092 req_class = VM_ALLOC_NORMAL;
1093 while ((ptdpg = pmap_alloc_direct_page(NUSERPGTBLS, req_class)) ==
1095 pmap_grow_direct_page(req_class);
1097 ptdva = MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(ptdpg));
1098 pmap->pm_segtab = (pd_entry_t *)ptdva;
1099 CPU_ZERO(&pmap->pm_active);
1100 for (i = 0; i < MAXCPU; i++) {
1101 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
1102 pmap->pm_asid[i].gen = 0;
1104 TAILQ_INIT(&pmap->pm_pvchunk);
1105 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1111 * this routine is called if the page table page is not
1115 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, u_int flags)
1122 * Find or fabricate a new pagetable page
1124 req_class = VM_ALLOC_NORMAL;
1125 if ((m = pmap_alloc_direct_page(ptepindex, req_class)) == NULL) {
1126 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
1128 rw_wunlock(&pvh_global_lock);
1129 pmap_grow_direct_page(req_class);
1130 rw_wlock(&pvh_global_lock);
1135 * Indicate the need to retry. While waiting, the page
1136 * table page may have been allocated.
1142 * Map the pagetable page into the process address space, if it
1143 * isn't already there.
1145 pageva = MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(m));
1148 if (ptepindex >= NUPDE) {
1149 pmap->pm_segtab[ptepindex - NUPDE] = (pd_entry_t)pageva;
1151 pd_entry_t *pdep, *pde;
1152 int segindex = ptepindex >> (SEGSHIFT - PDRSHIFT);
1153 int pdeindex = ptepindex & (NPDEPG - 1);
1156 pdep = &pmap->pm_segtab[segindex];
1157 if (*pdep == NULL) {
1158 /* recurse for allocating page dir */
1159 if (_pmap_allocpte(pmap, NUPDE + segindex,
1161 /* alloc failed, release current */
1162 vm_page_unwire_noq(m);
1163 vm_page_free_zero(m);
1167 pg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(*pdep));
1170 /* Next level entry */
1171 pde = (pd_entry_t *)*pdep;
1172 pde[pdeindex] = (pd_entry_t)pageva;
1175 pmap->pm_segtab[ptepindex] = (pd_entry_t)pageva;
1177 pmap->pm_stats.resident_count++;
1182 pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags)
1189 * Calculate pagetable page index
1191 ptepindex = pmap_pde_pindex(va);
1194 * Get the page directory entry
1196 pde = pmap_pde(pmap, va);
1199 * If the page table page is mapped, we just increment the hold
1200 * count, and activate it.
1202 if (pde != NULL && *pde != NULL) {
1203 m = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(*pde));
1207 * Here if the pte page isn't mapped, or if it has been
1210 m = _pmap_allocpte(pmap, ptepindex, flags);
1211 if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
1218 /***************************************************
1219 * Pmap allocation/deallocation routines.
1220 ***************************************************/
1223 * Release any resources held by the given physical map.
1224 * Called when a pmap initialized by pmap_pinit is being released.
1225 * Should only be called if the map contains no valid mappings.
1228 pmap_release(pmap_t pmap)
1233 KASSERT(pmap->pm_stats.resident_count == 0,
1234 ("pmap_release: pmap resident count %ld != 0",
1235 pmap->pm_stats.resident_count));
1237 ptdva = (vm_offset_t)pmap->pm_segtab;
1238 ptdpg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(ptdva));
1240 vm_page_unwire_noq(ptdpg);
1241 vm_page_free_zero(ptdpg);
1245 * grow the number of kernel page table entries, if needed
1248 pmap_growkernel(vm_offset_t addr)
1251 pd_entry_t *pde, *pdpe;
1255 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1256 req_class = VM_ALLOC_INTERRUPT;
1257 addr = roundup2(addr, NBSEG);
1258 if (addr - 1 >= vm_map_max(kernel_map))
1259 addr = vm_map_max(kernel_map);
1260 while (kernel_vm_end < addr) {
1261 pdpe = pmap_segmap(kernel_pmap, kernel_vm_end);
1264 /* new intermediate page table entry */
1265 nkpg = pmap_alloc_direct_page(nkpt, req_class);
1267 panic("pmap_growkernel: no memory to grow kernel");
1268 *pdpe = (pd_entry_t)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(nkpg));
1269 continue; /* try again */
1272 pde = pmap_pdpe_to_pde(pdpe, kernel_vm_end);
1274 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1275 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
1276 kernel_vm_end = vm_map_max(kernel_map);
1283 * This index is bogus, but out of the way
1285 nkpg = pmap_alloc_direct_page(nkpt, req_class);
1287 if (nkpg == NULL && vm_page_reclaim_contig(req_class, 1,
1288 0, MIPS_KSEG0_LARGEST_PHYS, PAGE_SIZE, 0))
1289 nkpg = pmap_alloc_direct_page(nkpt, req_class);
1292 panic("pmap_growkernel: no memory to grow kernel");
1294 *pde = (pd_entry_t)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(nkpg));
1297 * The R[4-7]?00 stores only one copy of the Global bit in
1298 * the translation lookaside buffer for each 2 page entry.
1299 * Thus invalid entrys must have the Global bit set so when
1300 * Entry LO and Entry HI G bits are anded together they will
1301 * produce a global bit to store in the tlb.
1303 pte = (pt_entry_t *)*pde;
1304 for (i = 0; i < NPTEPG; i++)
1307 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1308 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
1309 kernel_vm_end = vm_map_max(kernel_map);
1315 /***************************************************
1316 * page management routines.
1317 ***************************************************/
1319 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1321 CTASSERT(_NPCM == 3);
1322 CTASSERT(_NPCPV == 168);
1324 CTASSERT(_NPCM == 11);
1325 CTASSERT(_NPCPV == 336);
1328 static __inline struct pv_chunk *
1329 pv_to_chunk(pv_entry_t pv)
1332 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
1335 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1338 #define PC_FREE0_1 0xfffffffffffffffful
1339 #define PC_FREE2 0x000000fffffffffful
1341 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
1342 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
1345 static const u_long pc_freemask[_NPCM] = {
1347 PC_FREE0_1, PC_FREE0_1, PC_FREE2
1349 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1350 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1351 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1352 PC_FREE0_9, PC_FREE10
1356 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
1358 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1359 "Current number of pv entries");
1362 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1364 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1365 "Current number of pv entry chunks");
1366 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1367 "Current number of pv entry chunks allocated");
1368 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1369 "Current number of pv entry chunks frees");
1370 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1371 "Number of times tried to get a chunk page but failed.");
1373 static long pv_entry_frees, pv_entry_allocs;
1374 static int pv_entry_spare;
1376 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1377 "Current number of pv entry frees");
1378 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1379 "Current number of pv entry allocs");
1380 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1381 "Current number of spare pv entries");
1385 * We are in a serious low memory condition. Resort to
1386 * drastic measures to free some pages so we can allocate
1387 * another pv entry chunk.
1390 pmap_pv_reclaim(pmap_t locked_pmap)
1393 struct pv_chunk *pc;
1396 pt_entry_t *pte, oldpte;
1401 int bit, field, freed, idx;
1403 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1406 TAILQ_INIT(&newtail);
1407 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL) {
1408 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
1409 if (pmap != pc->pc_pmap) {
1411 pmap_invalidate_all(pmap);
1412 if (pmap != locked_pmap)
1416 /* Avoid deadlock and lock recursion. */
1417 if (pmap > locked_pmap)
1419 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
1421 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
1427 * Destroy every non-wired, 4 KB page mapping in the chunk.
1430 for (field = 0; field < _NPCM; field++) {
1431 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
1432 inuse != 0; inuse &= ~(1UL << bit)) {
1433 bit = ffsl(inuse) - 1;
1434 idx = field * sizeof(inuse) * NBBY + bit;
1435 pv = &pc->pc_pventry[idx];
1437 pde = pmap_pde(pmap, va);
1438 KASSERT(pde != NULL && *pde != 0,
1439 ("pmap_pv_reclaim: pde"));
1440 pte = pmap_pde_to_pte(pde, va);
1442 if (pte_test(&oldpte, PTE_W))
1444 if (is_kernel_pmap(pmap))
1448 m = PHYS_TO_VM_PAGE(TLBLO_PTE_TO_PA(oldpte));
1449 if (pte_test(&oldpte, PTE_D))
1451 if (m->md.pv_flags & PV_TABLE_REF)
1452 vm_page_aflag_set(m, PGA_REFERENCED);
1453 m->md.pv_flags &= ~PV_TABLE_REF;
1454 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1455 if (TAILQ_EMPTY(&m->md.pv_list))
1456 vm_page_aflag_clear(m, PGA_WRITEABLE);
1457 pc->pc_map[field] |= 1UL << bit;
1458 pmap_unuse_pt(pmap, va, *pde);
1463 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
1466 /* Every freed mapping is for a 4 KB page. */
1467 pmap->pm_stats.resident_count -= freed;
1468 PV_STAT(pv_entry_frees += freed);
1469 PV_STAT(pv_entry_spare += freed);
1470 pv_entry_count -= freed;
1471 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1472 for (field = 0; field < _NPCM; field++)
1473 if (pc->pc_map[field] != pc_freemask[field]) {
1474 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
1476 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
1479 * One freed pv entry in locked_pmap is
1482 if (pmap == locked_pmap)
1486 if (field == _NPCM) {
1487 PV_STAT(pv_entry_spare -= _NPCPV);
1488 PV_STAT(pc_chunk_count--);
1489 PV_STAT(pc_chunk_frees++);
1490 /* Entire chunk is free; return it. */
1491 m_pc = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(
1493 dump_drop_page(m_pc->phys_addr);
1498 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
1500 pmap_invalidate_all(pmap);
1501 if (pmap != locked_pmap)
1508 * free the pv_entry back to the free list
1511 free_pv_entry(pmap_t pmap, pv_entry_t pv)
1513 struct pv_chunk *pc;
1514 int bit, field, idx;
1516 rw_assert(&pvh_global_lock, RA_WLOCKED);
1517 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1518 PV_STAT(pv_entry_frees++);
1519 PV_STAT(pv_entry_spare++);
1521 pc = pv_to_chunk(pv);
1522 idx = pv - &pc->pc_pventry[0];
1523 field = idx / (sizeof(u_long) * NBBY);
1524 bit = idx % (sizeof(u_long) * NBBY);
1525 pc->pc_map[field] |= 1ul << bit;
1526 for (idx = 0; idx < _NPCM; idx++)
1527 if (pc->pc_map[idx] != pc_freemask[idx]) {
1529 * 98% of the time, pc is already at the head of the
1530 * list. If it isn't already, move it to the head.
1532 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
1534 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1535 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
1540 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1545 free_pv_chunk(struct pv_chunk *pc)
1549 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
1550 PV_STAT(pv_entry_spare -= _NPCPV);
1551 PV_STAT(pc_chunk_count--);
1552 PV_STAT(pc_chunk_frees++);
1553 /* entire chunk is free, return it */
1554 m = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS((vm_offset_t)pc));
1555 dump_drop_page(m->phys_addr);
1556 vm_page_unwire(m, PQ_NONE);
1561 * get a new pv_entry, allocating a block from the system
1565 get_pv_entry(pmap_t pmap, boolean_t try)
1567 struct pv_chunk *pc;
1570 int bit, field, idx;
1572 rw_assert(&pvh_global_lock, RA_WLOCKED);
1573 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1574 PV_STAT(pv_entry_allocs++);
1577 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
1579 for (field = 0; field < _NPCM; field++) {
1580 if (pc->pc_map[field]) {
1581 bit = ffsl(pc->pc_map[field]) - 1;
1585 if (field < _NPCM) {
1586 idx = field * sizeof(pc->pc_map[field]) * NBBY + bit;
1587 pv = &pc->pc_pventry[idx];
1588 pc->pc_map[field] &= ~(1ul << bit);
1589 /* If this was the last item, move it to tail */
1590 for (field = 0; field < _NPCM; field++)
1591 if (pc->pc_map[field] != 0) {
1592 PV_STAT(pv_entry_spare--);
1593 return (pv); /* not full, return */
1595 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1596 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
1597 PV_STAT(pv_entry_spare--);
1601 /* No free items, allocate another chunk */
1602 m = vm_page_alloc_freelist(VM_FREELIST_DIRECT, VM_ALLOC_NORMAL |
1607 PV_STAT(pc_chunk_tryfail++);
1610 m = pmap_pv_reclaim(pmap);
1614 PV_STAT(pc_chunk_count++);
1615 PV_STAT(pc_chunk_allocs++);
1616 dump_add_page(m->phys_addr);
1617 pc = (struct pv_chunk *)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(m));
1619 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
1620 for (field = 1; field < _NPCM; field++)
1621 pc->pc_map[field] = pc_freemask[field];
1622 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
1623 pv = &pc->pc_pventry[0];
1624 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
1625 PV_STAT(pv_entry_spare += _NPCPV - 1);
1630 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
1634 rw_assert(&pvh_global_lock, RA_WLOCKED);
1635 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
1636 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
1637 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
1645 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
1649 pv = pmap_pvh_remove(pvh, pmap, va);
1650 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found, pa %lx va %lx",
1651 (u_long)VM_PAGE_TO_PHYS(__containerof(pvh, struct vm_page, md)),
1653 free_pv_entry(pmap, pv);
1657 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1660 rw_assert(&pvh_global_lock, RA_WLOCKED);
1661 pmap_pvh_free(&m->md, pmap, va);
1662 if (TAILQ_EMPTY(&m->md.pv_list))
1663 vm_page_aflag_clear(m, PGA_WRITEABLE);
1667 * Conditionally create a pv entry.
1670 pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte, vm_offset_t va,
1675 rw_assert(&pvh_global_lock, RA_WLOCKED);
1676 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1677 if ((pv = get_pv_entry(pmap, TRUE)) != NULL) {
1679 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1686 * pmap_remove_pte: do the things to unmap a page in a process
1689 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
1696 rw_assert(&pvh_global_lock, RA_WLOCKED);
1697 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1700 * Write back all cache lines from the page being unmapped.
1702 mips_dcache_wbinv_range_index(va, PAGE_SIZE);
1705 if (is_kernel_pmap(pmap))
1710 if (pte_test(&oldpte, PTE_W))
1711 pmap->pm_stats.wired_count -= 1;
1713 pmap->pm_stats.resident_count -= 1;
1715 if (pte_test(&oldpte, PTE_MANAGED)) {
1716 pa = TLBLO_PTE_TO_PA(oldpte);
1717 m = PHYS_TO_VM_PAGE(pa);
1718 if (pte_test(&oldpte, PTE_D)) {
1719 KASSERT(!pte_test(&oldpte, PTE_RO),
1720 ("%s: modified page not writable: va: %p, pte: %#jx",
1721 __func__, (void *)va, (uintmax_t)oldpte));
1724 if (m->md.pv_flags & PV_TABLE_REF)
1725 vm_page_aflag_set(m, PGA_REFERENCED);
1726 m->md.pv_flags &= ~PV_TABLE_REF;
1728 pmap_remove_entry(pmap, m, va);
1730 return (pmap_unuse_pt(pmap, va, pde));
1734 * Remove a single page from a process address space
1737 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1742 rw_assert(&pvh_global_lock, RA_WLOCKED);
1743 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1744 pde = pmap_pde(pmap, va);
1745 if (pde == NULL || *pde == 0)
1747 ptq = pmap_pde_to_pte(pde, va);
1750 * If there is no pte for this address, just skip it!
1752 if (!pte_test(ptq, PTE_V))
1755 (void)pmap_remove_pte(pmap, ptq, va, *pde);
1756 pmap_invalidate_page(pmap, va);
1760 * Remove the given range of addresses from the specified map.
1762 * It is assumed that the start and end are properly
1763 * rounded to the page size.
1766 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1768 pd_entry_t *pde, *pdpe;
1770 vm_offset_t va, va_next;
1773 * Perform an unsynchronized read. This is, however, safe.
1775 if (pmap->pm_stats.resident_count == 0)
1778 rw_wlock(&pvh_global_lock);
1782 * special handling of removing one page. a very common operation
1783 * and easy to short circuit some code.
1785 if ((sva + PAGE_SIZE) == eva) {
1786 pmap_remove_page(pmap, sva);
1789 for (; sva < eva; sva = va_next) {
1790 pdpe = pmap_segmap(pmap, sva);
1793 va_next = (sva + NBSEG) & ~SEGMASK;
1799 va_next = (sva + NBPDR) & ~PDRMASK;
1803 pde = pmap_pdpe_to_pde(pdpe, sva);
1808 * Limit our scan to either the end of the va represented
1809 * by the current page table page, or to the end of the
1810 * range being removed.
1816 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1818 if (!pte_test(pte, PTE_V)) {
1819 if (va != va_next) {
1820 pmap_invalidate_range(pmap, va, sva);
1827 if (pmap_remove_pte(pmap, pte, sva, *pde)) {
1833 pmap_invalidate_range(pmap, va, sva);
1836 rw_wunlock(&pvh_global_lock);
1841 * Routine: pmap_remove_all
1843 * Removes this physical page from
1844 * all physical maps in which it resides.
1845 * Reflects back modify bits to the pager.
1848 * Original versions of this routine were very
1849 * inefficient because they iteratively called
1850 * pmap_remove (slow...)
1854 pmap_remove_all(vm_page_t m)
1859 pt_entry_t *pte, tpte;
1861 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1862 ("pmap_remove_all: page %p is not managed", m));
1863 rw_wlock(&pvh_global_lock);
1865 if (m->md.pv_flags & PV_TABLE_REF)
1866 vm_page_aflag_set(m, PGA_REFERENCED);
1868 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1873 * If it's last mapping writeback all caches from
1874 * the page being destroyed
1876 if (TAILQ_NEXT(pv, pv_list) == NULL)
1877 mips_dcache_wbinv_range_index(pv->pv_va, PAGE_SIZE);
1879 pmap->pm_stats.resident_count--;
1881 pde = pmap_pde(pmap, pv->pv_va);
1882 KASSERT(pde != NULL && *pde != 0, ("pmap_remove_all: pde"));
1883 pte = pmap_pde_to_pte(pde, pv->pv_va);
1886 if (is_kernel_pmap(pmap))
1891 if (pte_test(&tpte, PTE_W))
1892 pmap->pm_stats.wired_count--;
1895 * Update the vm_page_t clean and reference bits.
1897 if (pte_test(&tpte, PTE_D)) {
1898 KASSERT(!pte_test(&tpte, PTE_RO),
1899 ("%s: modified page not writable: va: %p, pte: %#jx",
1900 __func__, (void *)pv->pv_va, (uintmax_t)tpte));
1903 pmap_invalidate_page(pmap, pv->pv_va);
1905 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1906 pmap_unuse_pt(pmap, pv->pv_va, *pde);
1907 free_pv_entry(pmap, pv);
1911 vm_page_aflag_clear(m, PGA_WRITEABLE);
1912 m->md.pv_flags &= ~PV_TABLE_REF;
1913 rw_wunlock(&pvh_global_lock);
1917 * Set the physical protection on the
1918 * specified range of this map as requested.
1921 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1923 pt_entry_t pbits, *pte;
1924 pd_entry_t *pde, *pdpe;
1925 vm_offset_t va, va_next;
1929 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1930 pmap_remove(pmap, sva, eva);
1933 if (prot & VM_PROT_WRITE)
1937 for (; sva < eva; sva = va_next) {
1938 pdpe = pmap_segmap(pmap, sva);
1941 va_next = (sva + NBSEG) & ~SEGMASK;
1947 va_next = (sva + NBPDR) & ~PDRMASK;
1951 pde = pmap_pdpe_to_pde(pdpe, sva);
1956 * Limit our scan to either the end of the va represented
1957 * by the current page table page, or to the end of the
1958 * range being write protected.
1964 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1967 if (!pte_test(&pbits, PTE_V) || pte_test(&pbits,
1969 if (va != va_next) {
1970 pmap_invalidate_range(pmap, va, sva);
1975 pte_set(&pbits, PTE_RO);
1976 if (pte_test(&pbits, PTE_D)) {
1977 pte_clear(&pbits, PTE_D);
1978 if (pte_test(&pbits, PTE_MANAGED)) {
1979 pa = TLBLO_PTE_TO_PA(pbits);
1980 m = PHYS_TO_VM_PAGE(pa);
1987 * Unless PTE_D is set, any TLB entries
1988 * mapping "sva" don't allow write access, so
1989 * they needn't be invalidated.
1991 if (va != va_next) {
1992 pmap_invalidate_range(pmap, va, sva);
1999 pmap_invalidate_range(pmap, va, sva);
2005 * Insert the given physical page (p) at
2006 * the specified virtual address (v) in the
2007 * target physical map with the protection requested.
2009 * If specified, the page will be wired down, meaning
2010 * that the related pte can not be reclaimed.
2012 * NB: This is the only routine which MAY NOT lazy-evaluate
2013 * or lose information. That is, this routine must actually
2014 * insert this page into the given map NOW.
2017 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2018 u_int flags, int8_t psind __unused)
2022 pt_entry_t origpte, newpte;
2027 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
2028 KASSERT((m->oflags & VPO_UNMANAGED) != 0 || va < kmi.clean_sva ||
2029 va >= kmi.clean_eva,
2030 ("pmap_enter: managed mapping within the clean submap"));
2031 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
2032 VM_OBJECT_ASSERT_LOCKED(m->object);
2033 pa = VM_PAGE_TO_PHYS(m);
2034 newpte = TLBLO_PA_TO_PFN(pa) | init_pte_prot(m, flags, prot);
2035 if ((flags & PMAP_ENTER_WIRED) != 0)
2037 if (is_kernel_pmap(pmap))
2039 PMAP_PTE_SET_CACHE_BITS(newpte, pa, m);
2040 if ((m->oflags & VPO_UNMANAGED) == 0)
2041 newpte |= PTE_MANAGED;
2045 rw_wlock(&pvh_global_lock);
2049 * In the case that a page table page is not resident, we are
2052 if (va < VM_MAXUSER_ADDRESS) {
2053 mpte = pmap_allocpte(pmap, va, flags);
2055 KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
2056 ("pmap_allocpte failed with sleep allowed"));
2057 rw_wunlock(&pvh_global_lock);
2059 return (KERN_RESOURCE_SHORTAGE);
2062 pte = pmap_pte(pmap, va);
2065 * Page Directory table entry not valid, we need a new PT page
2068 panic("pmap_enter: invalid page directory, pdir=%p, va=%p",
2069 (void *)pmap->pm_segtab, (void *)va);
2073 KASSERT(!pte_test(&origpte, PTE_D | PTE_RO | PTE_V),
2074 ("pmap_enter: modified page not writable: va: %p, pte: %#jx",
2075 (void *)va, (uintmax_t)origpte));
2076 opa = TLBLO_PTE_TO_PA(origpte);
2079 * Mapping has not changed, must be protection or wiring change.
2081 if (pte_test(&origpte, PTE_V) && opa == pa) {
2083 * Wiring change, just update stats. We don't worry about
2084 * wiring PT pages as they remain resident as long as there
2085 * are valid mappings in them. Hence, if a user page is
2086 * wired, the PT page will be also.
2088 if (pte_test(&newpte, PTE_W) && !pte_test(&origpte, PTE_W))
2089 pmap->pm_stats.wired_count++;
2090 else if (!pte_test(&newpte, PTE_W) && pte_test(&origpte,
2092 pmap->pm_stats.wired_count--;
2095 * Remove extra pte reference
2100 if (pte_test(&origpte, PTE_MANAGED)) {
2101 m->md.pv_flags |= PV_TABLE_REF;
2102 if (!pte_test(&newpte, PTE_RO))
2103 vm_page_aflag_set(m, PGA_WRITEABLE);
2111 * Mapping has changed, invalidate old range and fall through to
2112 * handle validating new mapping.
2115 if (is_kernel_pmap(pmap))
2119 if (pte_test(&origpte, PTE_W))
2120 pmap->pm_stats.wired_count--;
2121 if (pte_test(&origpte, PTE_MANAGED)) {
2122 om = PHYS_TO_VM_PAGE(opa);
2123 if (pte_test(&origpte, PTE_D))
2125 if ((om->md.pv_flags & PV_TABLE_REF) != 0) {
2126 om->md.pv_flags &= ~PV_TABLE_REF;
2127 vm_page_aflag_set(om, PGA_REFERENCED);
2129 pv = pmap_pvh_remove(&om->md, pmap, va);
2130 if (!pte_test(&newpte, PTE_MANAGED))
2131 free_pv_entry(pmap, pv);
2132 if ((om->aflags & PGA_WRITEABLE) != 0 &&
2133 TAILQ_EMPTY(&om->md.pv_list))
2134 vm_page_aflag_clear(om, PGA_WRITEABLE);
2136 pmap_invalidate_page(pmap, va);
2140 KASSERT(mpte->wire_count > 0,
2141 ("pmap_enter: missing reference to page table page,"
2142 " va: %p", (void *)va));
2145 pmap->pm_stats.resident_count++;
2148 * Enter on the PV list if part of our managed memory.
2150 if (pte_test(&newpte, PTE_MANAGED)) {
2151 m->md.pv_flags |= PV_TABLE_REF;
2153 pv = get_pv_entry(pmap, FALSE);
2156 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2157 if (!pte_test(&newpte, PTE_RO))
2158 vm_page_aflag_set(m, PGA_WRITEABLE);
2162 * Increment counters
2164 if (pte_test(&newpte, PTE_W))
2165 pmap->pm_stats.wired_count++;
2170 printf("pmap_enter: va: %p -> pa: %p\n", (void *)va, (void *)pa);
2174 * if the mapping or permission bits are different, we need to
2177 if (origpte != newpte) {
2179 if (pte_test(&origpte, PTE_V)) {
2180 KASSERT(opa == pa, ("pmap_enter: invalid update"));
2181 if (pte_test(&origpte, PTE_D)) {
2182 if (pte_test(&origpte, PTE_MANAGED))
2185 pmap_update_page(pmap, va, newpte);
2190 * Sync I & D caches for executable pages. Do this only if the
2191 * target pmap belongs to the current process. Otherwise, an
2192 * unresolvable TLB miss may occur.
2194 if (!is_kernel_pmap(pmap) && (pmap == &curproc->p_vmspace->vm_pmap) &&
2195 (prot & VM_PROT_EXECUTE)) {
2196 mips_icache_sync_range(va, PAGE_SIZE);
2197 mips_dcache_wbinv_range(va, PAGE_SIZE);
2199 rw_wunlock(&pvh_global_lock);
2201 return (KERN_SUCCESS);
2205 * this code makes some *MAJOR* assumptions:
2206 * 1. Current pmap & pmap exists.
2209 * 4. No page table pages.
2210 * but is *MUCH* faster than pmap_enter...
2214 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2217 rw_wlock(&pvh_global_lock);
2219 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
2220 rw_wunlock(&pvh_global_lock);
2225 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
2226 vm_prot_t prot, vm_page_t mpte)
2228 pt_entry_t *pte, npte;
2231 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2232 (m->oflags & VPO_UNMANAGED) != 0,
2233 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2234 rw_assert(&pvh_global_lock, RA_WLOCKED);
2235 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2238 * In the case that a page table page is not resident, we are
2241 if (va < VM_MAXUSER_ADDRESS) {
2246 * Calculate pagetable page index
2248 ptepindex = pmap_pde_pindex(va);
2249 if (mpte && (mpte->pindex == ptepindex)) {
2253 * Get the page directory entry
2255 pde = pmap_pde(pmap, va);
2258 * If the page table page is mapped, we just
2259 * increment the hold count, and activate it.
2261 if (pde && *pde != 0) {
2262 mpte = PHYS_TO_VM_PAGE(
2263 MIPS_DIRECT_TO_PHYS(*pde));
2266 mpte = _pmap_allocpte(pmap, ptepindex,
2267 PMAP_ENTER_NOSLEEP);
2276 pte = pmap_pte(pmap, va);
2277 if (pte_test(pte, PTE_V)) {
2286 * Enter on the PV list if part of our managed memory.
2288 if ((m->oflags & VPO_UNMANAGED) == 0 &&
2289 !pmap_try_insert_pv_entry(pmap, mpte, va, m)) {
2291 pmap_unwire_ptp(pmap, va, mpte);
2298 * Increment counters
2300 pmap->pm_stats.resident_count++;
2302 pa = VM_PAGE_TO_PHYS(m);
2305 * Now validate mapping with RO protection
2307 npte = PTE_RO | TLBLO_PA_TO_PFN(pa) | PTE_V;
2308 if ((m->oflags & VPO_UNMANAGED) == 0)
2309 npte |= PTE_MANAGED;
2311 PMAP_PTE_SET_CACHE_BITS(npte, pa, m);
2313 if (is_kernel_pmap(pmap))
2314 *pte = npte | PTE_G;
2318 * Sync I & D caches. Do this only if the target pmap
2319 * belongs to the current process. Otherwise, an
2320 * unresolvable TLB miss may occur. */
2321 if (pmap == &curproc->p_vmspace->vm_pmap) {
2323 mips_icache_sync_range(va, PAGE_SIZE);
2324 mips_dcache_wbinv_range(va, PAGE_SIZE);
2331 * Make a temporary mapping for a physical address. This is only intended
2332 * to be used for panic dumps.
2334 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2337 pmap_kenter_temporary(vm_paddr_t pa, int i)
2342 printf("%s: ERROR!!! More than one page of virtual address mapping not supported\n",
2345 if (MIPS_DIRECT_MAPPABLE(pa)) {
2346 va = MIPS_PHYS_TO_DIRECT(pa);
2348 #ifndef __mips_n64 /* XXX : to be converted to new style */
2351 struct local_sysmaps *sysm;
2352 pt_entry_t *pte, npte;
2354 /* If this is used other than for dumps, we may need to leave
2355 * interrupts disasbled on return. If crash dumps don't work when
2356 * we get to this point, we might want to consider this (leaving things
2357 * disabled as a starting point ;-)
2359 intr = intr_disable();
2360 cpu = PCPU_GET(cpuid);
2361 sysm = &sysmap_lmem[cpu];
2362 /* Since this is for the debugger, no locks or any other fun */
2363 npte = TLBLO_PA_TO_PFN(pa) | PTE_C_CACHE | PTE_D | PTE_V |
2365 pte = pmap_pte(kernel_pmap, sysm->base);
2368 pmap_update_page(kernel_pmap, sysm->base, npte);
2373 return ((void *)va);
2377 pmap_kenter_temporary_free(vm_paddr_t pa)
2379 #ifndef __mips_n64 /* XXX : to be converted to new style */
2382 struct local_sysmaps *sysm;
2385 if (MIPS_DIRECT_MAPPABLE(pa)) {
2386 /* nothing to do for this case */
2389 #ifndef __mips_n64 /* XXX : to be converted to new style */
2390 cpu = PCPU_GET(cpuid);
2391 sysm = &sysmap_lmem[cpu];
2395 intr = intr_disable();
2396 pte = pmap_pte(kernel_pmap, sysm->base);
2398 pmap_invalidate_page(kernel_pmap, sysm->base);
2406 * Maps a sequence of resident pages belonging to the same object.
2407 * The sequence begins with the given page m_start. This page is
2408 * mapped at the given virtual address start. Each subsequent page is
2409 * mapped at a virtual address that is offset from start by the same
2410 * amount as the page is offset from m_start within the object. The
2411 * last page in the sequence is the page with the largest offset from
2412 * m_start that can be mapped at a virtual address less than the given
2413 * virtual address end. Not every virtual page between start and end
2414 * is mapped; only those for which a resident page exists with the
2415 * corresponding offset from m_start are mapped.
2418 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2419 vm_page_t m_start, vm_prot_t prot)
2422 vm_pindex_t diff, psize;
2424 VM_OBJECT_ASSERT_LOCKED(m_start->object);
2426 psize = atop(end - start);
2429 rw_wlock(&pvh_global_lock);
2431 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2432 mpte = pmap_enter_quick_locked(pmap, start + ptoa(diff), m,
2434 m = TAILQ_NEXT(m, listq);
2436 rw_wunlock(&pvh_global_lock);
2441 * pmap_object_init_pt preloads the ptes for a given object
2442 * into the specified pmap. This eliminates the blast of soft
2443 * faults on process startup and immediately after an mmap.
2446 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2447 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2449 VM_OBJECT_ASSERT_WLOCKED(object);
2450 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2451 ("pmap_object_init_pt: non-device object"));
2455 * Clear the wired attribute from the mappings for the specified range of
2456 * addresses in the given pmap. Every valid mapping within that range
2457 * must have the wired attribute set. In contrast, invalid mappings
2458 * cannot have the wired attribute set, so they are ignored.
2460 * The wired attribute of the page table entry is not a hardware feature,
2461 * so there is no need to invalidate any TLB entries.
2464 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2466 pd_entry_t *pde, *pdpe;
2468 vm_offset_t va_next;
2471 for (; sva < eva; sva = va_next) {
2472 pdpe = pmap_segmap(pmap, sva);
2474 if (*pdpe == NULL) {
2475 va_next = (sva + NBSEG) & ~SEGMASK;
2481 va_next = (sva + NBPDR) & ~PDRMASK;
2484 pde = pmap_pdpe_to_pde(pdpe, sva);
2489 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2491 if (!pte_test(pte, PTE_V))
2493 if (!pte_test(pte, PTE_W))
2494 panic("pmap_unwire: pte %#jx is missing PG_W",
2496 pte_clear(pte, PTE_W);
2497 pmap->pm_stats.wired_count--;
2504 * Copy the range specified by src_addr/len
2505 * from the source map to the range dst_addr/len
2506 * in the destination map.
2508 * This routine is only advisory and need not do anything.
2512 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2513 vm_size_t len, vm_offset_t src_addr)
2518 * pmap_zero_page zeros the specified hardware page by mapping
2519 * the page into KVM and using bzero to clear its contents.
2521 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2524 pmap_zero_page(vm_page_t m)
2527 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2529 if (MIPS_DIRECT_MAPPABLE(phys)) {
2530 va = MIPS_PHYS_TO_DIRECT(phys);
2531 bzero((caddr_t)va, PAGE_SIZE);
2532 mips_dcache_wbinv_range(va, PAGE_SIZE);
2534 va = pmap_lmem_map1(phys);
2535 bzero((caddr_t)va, PAGE_SIZE);
2536 mips_dcache_wbinv_range(va, PAGE_SIZE);
2542 * pmap_zero_page_area zeros the specified hardware page by mapping
2543 * the page into KVM and using bzero to clear its contents.
2545 * off and size may not cover an area beyond a single hardware page.
2548 pmap_zero_page_area(vm_page_t m, int off, int size)
2551 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2553 if (MIPS_DIRECT_MAPPABLE(phys)) {
2554 va = MIPS_PHYS_TO_DIRECT(phys);
2555 bzero((char *)(caddr_t)va + off, size);
2556 mips_dcache_wbinv_range(va + off, size);
2558 va = pmap_lmem_map1(phys);
2559 bzero((char *)va + off, size);
2560 mips_dcache_wbinv_range(va + off, size);
2566 * pmap_copy_page copies the specified (machine independent)
2567 * page by mapping the page into virtual memory and using
2568 * bcopy to copy the page, one machine dependent page at a
2571 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2574 pmap_copy_page(vm_page_t src, vm_page_t dst)
2576 vm_offset_t va_src, va_dst;
2577 vm_paddr_t phys_src = VM_PAGE_TO_PHYS(src);
2578 vm_paddr_t phys_dst = VM_PAGE_TO_PHYS(dst);
2580 if (MIPS_DIRECT_MAPPABLE(phys_src) && MIPS_DIRECT_MAPPABLE(phys_dst)) {
2581 /* easy case, all can be accessed via KSEG0 */
2583 * Flush all caches for VA that are mapped to this page
2584 * to make sure that data in SDRAM is up to date
2586 pmap_flush_pvcache(src);
2587 mips_dcache_wbinv_range_index(
2588 MIPS_PHYS_TO_DIRECT(phys_dst), PAGE_SIZE);
2589 va_src = MIPS_PHYS_TO_DIRECT(phys_src);
2590 va_dst = MIPS_PHYS_TO_DIRECT(phys_dst);
2591 bcopy((caddr_t)va_src, (caddr_t)va_dst, PAGE_SIZE);
2592 mips_dcache_wbinv_range(va_dst, PAGE_SIZE);
2594 va_src = pmap_lmem_map2(phys_src, phys_dst);
2595 va_dst = va_src + PAGE_SIZE;
2596 bcopy((void *)va_src, (void *)va_dst, PAGE_SIZE);
2597 mips_dcache_wbinv_range(va_dst, PAGE_SIZE);
2602 int unmapped_buf_allowed;
2605 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
2606 vm_offset_t b_offset, int xfersize)
2610 vm_offset_t a_pg_offset, b_pg_offset;
2611 vm_paddr_t a_phys, b_phys;
2614 while (xfersize > 0) {
2615 a_pg_offset = a_offset & PAGE_MASK;
2616 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
2617 a_m = ma[a_offset >> PAGE_SHIFT];
2618 a_phys = VM_PAGE_TO_PHYS(a_m);
2619 b_pg_offset = b_offset & PAGE_MASK;
2620 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
2621 b_m = mb[b_offset >> PAGE_SHIFT];
2622 b_phys = VM_PAGE_TO_PHYS(b_m);
2623 if (MIPS_DIRECT_MAPPABLE(a_phys) &&
2624 MIPS_DIRECT_MAPPABLE(b_phys)) {
2625 pmap_flush_pvcache(a_m);
2626 mips_dcache_wbinv_range_index(
2627 MIPS_PHYS_TO_DIRECT(b_phys), PAGE_SIZE);
2628 a_cp = (char *)MIPS_PHYS_TO_DIRECT(a_phys) +
2630 b_cp = (char *)MIPS_PHYS_TO_DIRECT(b_phys) +
2632 bcopy(a_cp, b_cp, cnt);
2633 mips_dcache_wbinv_range((vm_offset_t)b_cp, cnt);
2635 a_cp = (char *)pmap_lmem_map2(a_phys, b_phys);
2636 b_cp = (char *)a_cp + PAGE_SIZE;
2637 a_cp += a_pg_offset;
2638 b_cp += b_pg_offset;
2639 bcopy(a_cp, b_cp, cnt);
2640 mips_dcache_wbinv_range((vm_offset_t)b_cp, cnt);
2650 pmap_quick_enter_page(vm_page_t m)
2652 #if defined(__mips_n64)
2653 return MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(m));
2656 struct local_sysmaps *sysm;
2657 pt_entry_t *pte, npte;
2659 pa = VM_PAGE_TO_PHYS(m);
2661 if (MIPS_DIRECT_MAPPABLE(pa)) {
2662 if (pmap_page_get_memattr(m) != VM_MEMATTR_WRITE_BACK)
2663 return (MIPS_PHYS_TO_DIRECT_UNCACHED(pa));
2665 return (MIPS_PHYS_TO_DIRECT(pa));
2668 sysm = &sysmap_lmem[PCPU_GET(cpuid)];
2670 KASSERT(sysm->valid1 == 0, ("pmap_quick_enter_page: PTE busy"));
2672 pte = pmap_pte(kernel_pmap, sysm->base);
2673 npte = TLBLO_PA_TO_PFN(pa) | PTE_D | PTE_V | PTE_G;
2674 PMAP_PTE_SET_CACHE_BITS(npte, pa, m);
2678 return (sysm->base);
2683 pmap_quick_remove_page(vm_offset_t addr)
2685 mips_dcache_wbinv_range(addr, PAGE_SIZE);
2687 #if !defined(__mips_n64)
2688 struct local_sysmaps *sysm;
2691 if (addr >= MIPS_KSEG0_START && addr < MIPS_KSEG0_END)
2694 sysm = &sysmap_lmem[PCPU_GET(cpuid)];
2696 KASSERT(sysm->valid1 != 0,
2697 ("pmap_quick_remove_page: PTE not in use"));
2698 KASSERT(sysm->base == addr,
2699 ("pmap_quick_remove_page: invalid address"));
2701 pte = pmap_pte(kernel_pmap, addr);
2703 tlb_invalidate_address(kernel_pmap, addr);
2710 * Returns true if the pmap's pv is one of the first
2711 * 16 pvs linked to from this page. This count may
2712 * be changed upwards or downwards in the future; it
2713 * is only necessary that true be returned for a small
2714 * subset of pmaps for proper page aging.
2717 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2723 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2724 ("pmap_page_exists_quick: page %p is not managed", m));
2726 rw_wlock(&pvh_global_lock);
2727 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2728 if (PV_PMAP(pv) == pmap) {
2736 rw_wunlock(&pvh_global_lock);
2741 * Remove all pages from specified address space
2742 * this aids process exit speeds. Also, this code
2743 * is special cased for current process only, but
2744 * can have the more generic (and slightly slower)
2745 * mode enabled. This is much faster than pmap_remove
2746 * in the case of running down an entire address space.
2749 pmap_remove_pages(pmap_t pmap)
2752 pt_entry_t *pte, tpte;
2755 struct pv_chunk *pc, *npc;
2756 u_long inuse, bitmask;
2757 int allfree, bit, field, idx;
2759 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
2760 printf("warning: pmap_remove_pages called with non-current pmap\n");
2763 rw_wlock(&pvh_global_lock);
2765 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
2767 for (field = 0; field < _NPCM; field++) {
2768 inuse = ~pc->pc_map[field] & pc_freemask[field];
2769 while (inuse != 0) {
2770 bit = ffsl(inuse) - 1;
2771 bitmask = 1UL << bit;
2772 idx = field * sizeof(inuse) * NBBY + bit;
2773 pv = &pc->pc_pventry[idx];
2776 pde = pmap_pde(pmap, pv->pv_va);
2777 KASSERT(pde != NULL && *pde != 0,
2778 ("pmap_remove_pages: pde"));
2779 pte = pmap_pde_to_pte(pde, pv->pv_va);
2780 if (!pte_test(pte, PTE_V))
2781 panic("pmap_remove_pages: bad pte");
2785 * We cannot remove wired pages from a process' mapping at this time
2787 if (pte_test(&tpte, PTE_W)) {
2791 *pte = is_kernel_pmap(pmap) ? PTE_G : 0;
2793 m = PHYS_TO_VM_PAGE(TLBLO_PTE_TO_PA(tpte));
2795 ("pmap_remove_pages: bad tpte %#jx",
2799 * Update the vm_page_t clean and reference bits.
2801 if (pte_test(&tpte, PTE_D))
2805 PV_STAT(pv_entry_frees++);
2806 PV_STAT(pv_entry_spare++);
2808 pc->pc_map[field] |= bitmask;
2809 pmap->pm_stats.resident_count--;
2810 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2811 if (TAILQ_EMPTY(&m->md.pv_list))
2812 vm_page_aflag_clear(m, PGA_WRITEABLE);
2813 pmap_unuse_pt(pmap, pv->pv_va, *pde);
2817 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2821 pmap_invalidate_all(pmap);
2823 rw_wunlock(&pvh_global_lock);
2827 * pmap_testbit tests bits in pte's
2830 pmap_testbit(vm_page_t m, int bit)
2835 boolean_t rv = FALSE;
2837 if (m->oflags & VPO_UNMANAGED)
2840 rw_assert(&pvh_global_lock, RA_WLOCKED);
2841 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2844 pte = pmap_pte(pmap, pv->pv_va);
2845 rv = pte_test(pte, bit);
2854 * pmap_page_wired_mappings:
2856 * Return the number of managed mappings to the given physical page
2860 pmap_page_wired_mappings(vm_page_t m)
2868 if ((m->oflags & VPO_UNMANAGED) != 0)
2870 rw_wlock(&pvh_global_lock);
2871 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2874 pte = pmap_pte(pmap, pv->pv_va);
2875 if (pte_test(pte, PTE_W))
2879 rw_wunlock(&pvh_global_lock);
2884 * Clear the write and modified bits in each of the given page's mappings.
2887 pmap_remove_write(vm_page_t m)
2890 pt_entry_t pbits, *pte;
2893 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2894 ("pmap_remove_write: page %p is not managed", m));
2897 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2898 * set by another thread while the object is locked. Thus,
2899 * if PGA_WRITEABLE is clear, no page table entries need updating.
2901 VM_OBJECT_ASSERT_WLOCKED(m->object);
2902 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2904 rw_wlock(&pvh_global_lock);
2905 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2908 pte = pmap_pte(pmap, pv->pv_va);
2909 KASSERT(pte != NULL && pte_test(pte, PTE_V),
2910 ("page on pv_list has no pte"));
2912 if (pte_test(&pbits, PTE_D)) {
2913 pte_clear(&pbits, PTE_D);
2916 pte_set(&pbits, PTE_RO);
2917 if (pbits != *pte) {
2919 pmap_update_page(pmap, pv->pv_va, pbits);
2923 vm_page_aflag_clear(m, PGA_WRITEABLE);
2924 rw_wunlock(&pvh_global_lock);
2928 * pmap_ts_referenced:
2930 * Return the count of reference bits for a page, clearing all of them.
2933 pmap_ts_referenced(vm_page_t m)
2936 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2937 ("pmap_ts_referenced: page %p is not managed", m));
2938 if (m->md.pv_flags & PV_TABLE_REF) {
2939 rw_wlock(&pvh_global_lock);
2940 m->md.pv_flags &= ~PV_TABLE_REF;
2941 rw_wunlock(&pvh_global_lock);
2950 * Return whether or not the specified physical page was modified
2951 * in any physical maps.
2954 pmap_is_modified(vm_page_t m)
2958 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2959 ("pmap_is_modified: page %p is not managed", m));
2962 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2963 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
2964 * is clear, no PTEs can have PTE_D set.
2966 VM_OBJECT_ASSERT_WLOCKED(m->object);
2967 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2969 rw_wlock(&pvh_global_lock);
2970 rv = pmap_testbit(m, PTE_D);
2971 rw_wunlock(&pvh_global_lock);
2978 * pmap_is_prefaultable:
2980 * Return whether or not the specified virtual address is elgible
2984 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2992 pde = pmap_pde(pmap, addr);
2993 if (pde != NULL && *pde != 0) {
2994 pte = pmap_pde_to_pte(pde, addr);
3002 * Apply the given advice to the specified range of addresses within the
3003 * given pmap. Depending on the advice, clear the referenced and/or
3004 * modified flags in each mapping and set the mapped page's dirty field.
3007 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
3009 pd_entry_t *pde, *pdpe;
3011 vm_offset_t va, va_next;
3015 if (advice != MADV_DONTNEED && advice != MADV_FREE)
3017 rw_wlock(&pvh_global_lock);
3019 for (; sva < eva; sva = va_next) {
3020 pdpe = pmap_segmap(pmap, sva);
3023 va_next = (sva + NBSEG) & ~SEGMASK;
3029 va_next = (sva + NBPDR) & ~PDRMASK;
3033 pde = pmap_pdpe_to_pde(pdpe, sva);
3038 * Limit our scan to either the end of the va represented
3039 * by the current page table page, or to the end of the
3040 * range being write protected.
3046 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
3048 if (!pte_test(pte, PTE_MANAGED | PTE_V)) {
3049 if (va != va_next) {
3050 pmap_invalidate_range(pmap, va, sva);
3055 pa = TLBLO_PTE_TO_PA(*pte);
3056 m = PHYS_TO_VM_PAGE(pa);
3057 m->md.pv_flags &= ~PV_TABLE_REF;
3058 if (pte_test(pte, PTE_D)) {
3059 if (advice == MADV_DONTNEED) {
3061 * Future calls to pmap_is_modified()
3062 * can be avoided by making the page
3067 pte_clear(pte, PTE_D);
3073 * Unless PTE_D is set, any TLB entries
3074 * mapping "sva" don't allow write access, so
3075 * they needn't be invalidated.
3077 if (va != va_next) {
3078 pmap_invalidate_range(pmap, va, sva);
3084 pmap_invalidate_range(pmap, va, sva);
3086 rw_wunlock(&pvh_global_lock);
3091 * Clear the modify bits on the specified physical page.
3094 pmap_clear_modify(vm_page_t m)
3100 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3101 ("pmap_clear_modify: page %p is not managed", m));
3102 VM_OBJECT_ASSERT_WLOCKED(m->object);
3103 KASSERT(!vm_page_xbusied(m),
3104 ("pmap_clear_modify: page %p is exclusive busied", m));
3107 * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_D set.
3108 * If the object containing the page is locked and the page is not
3109 * write busied, then PGA_WRITEABLE cannot be concurrently set.
3111 if ((m->aflags & PGA_WRITEABLE) == 0)
3113 rw_wlock(&pvh_global_lock);
3114 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3117 pte = pmap_pte(pmap, pv->pv_va);
3118 if (pte_test(pte, PTE_D)) {
3119 pte_clear(pte, PTE_D);
3120 pmap_update_page(pmap, pv->pv_va, *pte);
3124 rw_wunlock(&pvh_global_lock);
3128 * pmap_is_referenced:
3130 * Return whether or not the specified physical page was referenced
3131 * in any physical maps.
3134 pmap_is_referenced(vm_page_t m)
3137 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3138 ("pmap_is_referenced: page %p is not managed", m));
3139 return ((m->md.pv_flags & PV_TABLE_REF) != 0);
3143 * Miscellaneous support routines follow
3147 * Map a set of physical memory pages into the kernel virtual
3148 * address space. Return a pointer to where it is mapped. This
3149 * routine is intended to be used for mapping device memory,
3152 * Use XKPHYS uncached for 64 bit, and KSEG1 where possible for 32 bit.
3155 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
3157 vm_offset_t va, tmpva, offset;
3160 * KSEG1 maps only first 512M of phys address space. For
3161 * pa > 0x20000000 we should make proper mapping * using pmap_kenter.
3163 if (MIPS_DIRECT_MAPPABLE(pa + size - 1) && ma == VM_MEMATTR_UNCACHEABLE)
3164 return ((void *)MIPS_PHYS_TO_DIRECT_UNCACHED(pa));
3166 offset = pa & PAGE_MASK;
3167 size = roundup(size + offset, PAGE_SIZE);
3169 va = kva_alloc(size);
3171 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3172 pa = trunc_page(pa);
3173 for (tmpva = va; size > 0;) {
3174 pmap_kenter_attr(tmpva, pa, ma);
3181 return ((void *)(va + offset));
3185 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3187 return pmap_mapdev_attr(pa, size, VM_MEMATTR_UNCACHEABLE);
3191 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3194 vm_offset_t base, offset;
3196 /* If the address is within KSEG1 then there is nothing to do */
3197 if (va >= MIPS_KSEG1_START && va <= MIPS_KSEG1_END)
3200 base = trunc_page(va);
3201 offset = va & PAGE_MASK;
3202 size = roundup(size + offset, PAGE_SIZE);
3203 kva_free(base, size);
3208 * perform the pmap work for mincore
3211 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
3213 pt_entry_t *ptep, pte;
3220 ptep = pmap_pte(pmap, addr);
3221 pte = (ptep != NULL) ? *ptep : 0;
3222 if (!pte_test(&pte, PTE_V)) {
3226 val = MINCORE_INCORE;
3227 if (pte_test(&pte, PTE_D))
3228 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
3229 pa = TLBLO_PTE_TO_PA(pte);
3230 if (pte_test(&pte, PTE_MANAGED)) {
3232 * This may falsely report the given address as
3233 * MINCORE_REFERENCED. Unfortunately, due to the lack of
3234 * per-PTE reference information, it is impossible to
3235 * determine if the address is MINCORE_REFERENCED.
3237 m = PHYS_TO_VM_PAGE(pa);
3238 if ((m->aflags & PGA_REFERENCED) != 0)
3239 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
3241 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
3242 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
3243 pte_test(&pte, PTE_MANAGED)) {
3244 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
3245 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
3249 PA_UNLOCK_COND(*locked_pa);
3255 pmap_activate(struct thread *td)
3257 pmap_t pmap, oldpmap;
3258 struct proc *p = td->td_proc;
3263 pmap = vmspace_pmap(p->p_vmspace);
3264 oldpmap = PCPU_GET(curpmap);
3265 cpuid = PCPU_GET(cpuid);
3268 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
3269 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
3270 pmap_asid_alloc(pmap);
3271 if (td == curthread) {
3272 PCPU_SET(segbase, pmap->pm_segtab);
3273 mips_wr_entryhi(pmap->pm_asid[cpuid].asid);
3276 PCPU_SET(curpmap, pmap);
3281 pmap_sync_icache_one(void *arg __unused)
3284 mips_icache_sync_all();
3285 mips_dcache_wbinv_all();
3289 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
3292 smp_rendezvous(NULL, pmap_sync_icache_one, NULL, NULL);
3296 * Increase the starting virtual address of the given mapping if a
3297 * different alignment might result in more superpage mappings.
3300 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
3301 vm_offset_t *addr, vm_size_t size)
3303 vm_offset_t superpage_offset;
3307 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
3308 offset += ptoa(object->pg_color);
3309 superpage_offset = offset & PDRMASK;
3310 if (size - ((PDRSIZE - superpage_offset) & PDRMASK) < PDRSIZE ||
3311 (*addr & PDRMASK) == superpage_offset)
3313 if ((*addr & PDRMASK) < superpage_offset)
3314 *addr = (*addr & ~PDRMASK) + superpage_offset;
3316 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
3320 DB_SHOW_COMMAND(ptable, ddb_pid_dump)
3323 struct thread *td = NULL;
3330 td = db_lookup_thread(addr, true);
3332 db_printf("Invalid pid or tid");
3336 if (p->p_vmspace == NULL) {
3337 db_printf("No vmspace for process");
3340 pmap = vmspace_pmap(p->p_vmspace);
3344 db_printf("pmap:%p segtab:%p asid:%x generation:%x\n",
3345 pmap, pmap->pm_segtab, pmap->pm_asid[0].asid,
3346 pmap->pm_asid[0].gen);
3347 for (i = 0; i < NPDEPG; i++) {
3352 pdpe = (pd_entry_t *)pmap->pm_segtab[i];
3355 db_printf("[%4d] %p\n", i, pdpe);
3357 for (j = 0; j < NPDEPG; j++) {
3358 pde = (pt_entry_t *)pdpe[j];
3361 db_printf("\t[%4d] %p\n", j, pde);
3365 pde = (pt_entry_t *)pdpe;
3367 for (k = 0; k < NPTEPG; k++) {
3369 if (pte == 0 || !pte_test(&pte, PTE_V))
3371 pa = TLBLO_PTE_TO_PA(pte);
3372 va = ((u_long)i << SEGSHIFT) | (j << PDRSHIFT) | (k << PAGE_SHIFT);
3373 db_printf("\t\t[%04d] va: %p pte: %8jx pa:%jx\n",
3374 k, (void *)va, (uintmax_t)pte, (uintmax_t)pa);
3382 * Allocate TLB address space tag (called ASID or TLBPID) and return it.
3383 * It takes almost as much or more time to search the TLB for a
3384 * specific ASID and flush those entries as it does to flush the entire TLB.
3385 * Therefore, when we allocate a new ASID, we just take the next number. When
3386 * we run out of numbers, we flush the TLB, increment the generation count
3387 * and start over. ASID zero is reserved for kernel use.
3390 pmap_asid_alloc(pmap)
3393 if (pmap->pm_asid[PCPU_GET(cpuid)].asid != PMAP_ASID_RESERVED &&
3394 pmap->pm_asid[PCPU_GET(cpuid)].gen == PCPU_GET(asid_generation));
3396 if (PCPU_GET(next_asid) == pmap_max_asid) {
3397 tlb_invalidate_all_user(NULL);
3398 PCPU_SET(asid_generation,
3399 (PCPU_GET(asid_generation) + 1) & ASIDGEN_MASK);
3400 if (PCPU_GET(asid_generation) == 0) {
3401 PCPU_SET(asid_generation, 1);
3403 PCPU_SET(next_asid, 1); /* 0 means invalid */
3405 pmap->pm_asid[PCPU_GET(cpuid)].asid = PCPU_GET(next_asid);
3406 pmap->pm_asid[PCPU_GET(cpuid)].gen = PCPU_GET(asid_generation);
3407 PCPU_SET(next_asid, PCPU_GET(next_asid) + 1);
3412 init_pte_prot(vm_page_t m, vm_prot_t access, vm_prot_t prot)
3416 if (!(prot & VM_PROT_WRITE))
3417 rw = PTE_V | PTE_RO;
3418 else if ((m->oflags & VPO_UNMANAGED) == 0) {
3419 if ((access & VM_PROT_WRITE) != 0)
3424 /* Needn't emulate a modified bit for unmanaged pages. */
3430 * pmap_emulate_modified : do dirty bit emulation
3432 * On SMP, update just the local TLB, other CPUs will update their
3433 * TLBs from PTE lazily, if they get the exception.
3434 * Returns 0 in case of sucess, 1 if the page is read only and we
3438 pmap_emulate_modified(pmap_t pmap, vm_offset_t va)
3443 pte = pmap_pte(pmap, va);
3445 panic("pmap_emulate_modified: can't find PTE");
3447 /* It is possible that some other CPU changed m-bit */
3448 if (!pte_test(pte, PTE_V) || pte_test(pte, PTE_D)) {
3449 tlb_update(pmap, va, *pte);
3454 if (!pte_test(pte, PTE_V) || pte_test(pte, PTE_D))
3455 panic("pmap_emulate_modified: invalid pte");
3457 if (pte_test(pte, PTE_RO)) {
3461 pte_set(pte, PTE_D);
3462 tlb_update(pmap, va, *pte);
3463 if (!pte_test(pte, PTE_MANAGED))
3464 panic("pmap_emulate_modified: unmanaged page");
3470 * Routine: pmap_kextract
3472 * Extract the physical page address associated
3476 pmap_kextract(vm_offset_t va)
3481 * First, the direct-mapped regions.
3483 #if defined(__mips_n64)
3484 if (va >= MIPS_XKPHYS_START && va < MIPS_XKPHYS_END)
3485 return (MIPS_XKPHYS_TO_PHYS(va));
3487 if (va >= MIPS_KSEG0_START && va < MIPS_KSEG0_END)
3488 return (MIPS_KSEG0_TO_PHYS(va));
3490 if (va >= MIPS_KSEG1_START && va < MIPS_KSEG1_END)
3491 return (MIPS_KSEG1_TO_PHYS(va));
3494 * User virtual addresses.
3496 if (va < VM_MAXUSER_ADDRESS) {
3499 if (curproc && curproc->p_vmspace) {
3500 ptep = pmap_pte(&curproc->p_vmspace->vm_pmap, va);
3502 return (TLBLO_PTE_TO_PA(*ptep) |
3510 * Should be kernel virtual here, otherwise fail
3512 mapped = (va >= MIPS_KSEG2_START || va < MIPS_KSEG2_END);
3513 #if defined(__mips_n64)
3514 mapped = mapped || (va >= MIPS_XKSEG_START || va < MIPS_XKSEG_END);
3523 /* Is the kernel pmap initialized? */
3524 if (!CPU_EMPTY(&kernel_pmap->pm_active)) {
3525 /* It's inside the virtual address range */
3526 ptep = pmap_pte(kernel_pmap, va);
3528 return (TLBLO_PTE_TO_PA(*ptep) |
3535 panic("%s for unknown address space %p.", __func__, (void *)va);
3540 pmap_flush_pvcache(vm_page_t m)
3545 for (pv = TAILQ_FIRST(&m->md.pv_list); pv;
3546 pv = TAILQ_NEXT(pv, pv_list)) {
3547 mips_dcache_wbinv_range_index(pv->pv_va, PAGE_SIZE);
3553 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
3557 * It appears that this function can only be called before any mappings
3558 * for the page are established. If this ever changes, this code will
3559 * need to walk the pv_list and make each of the existing mappings
3560 * uncacheable, being careful to sync caches and PTEs (and maybe
3561 * invalidate TLB?) for any current mapping it modifies.
3563 if (TAILQ_FIRST(&m->md.pv_list) != NULL)
3564 panic("Can't change memattr on page with existing mappings");
3566 /* Clean memattr portion of pv_flags */
3567 m->md.pv_flags &= ~PV_MEMATTR_MASK;
3568 m->md.pv_flags |= (ma << PV_MEMATTR_SHIFT) & PV_MEMATTR_MASK;
3571 static __inline void
3572 pmap_pte_attr(pt_entry_t *pte, vm_memattr_t ma)
3576 npte = *(u_int *)pte;
3577 npte &= ~PTE_C_MASK;
3583 pmap_change_attr(vm_offset_t sva, vm_size_t size, vm_memattr_t ma)
3585 pd_entry_t *pde, *pdpe;
3587 vm_offset_t ova, eva, va, va_next;
3598 for (; sva < eva; sva = va_next) {
3599 pdpe = pmap_segmap(pmap, sva);
3602 va_next = (sva + NBSEG) & ~SEGMASK;
3608 va_next = (sva + NBPDR) & ~PDRMASK;
3612 pde = pmap_pdpe_to_pde(pdpe, sva);
3617 * Limit our scan to either the end of the va represented
3618 * by the current page table page, or to the end of the
3619 * range being removed.
3625 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
3627 if (!pte_test(pte, PTE_V) || pte_cache_bits(pte) == ma) {
3628 if (va != va_next) {
3629 pmap_invalidate_range(pmap, va, sva);
3637 pmap_pte_attr(pte, ma);
3640 pmap_invalidate_range(pmap, va, sva);
3644 /* Flush caches to be in the safe side */
3645 mips_dcache_wbinv_range(ova, size);
3650 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
3654 case VM_MEMATTR_UNCACHEABLE:
3655 case VM_MEMATTR_WRITE_BACK:
3657 case VM_MEMATTR_WRITE_COMBINING: