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
38 * from: src/sys/i386/i386/pmap.c,v 1.250.2.8 2000/11/21 00:09:14 ps
39 * JNPR: pmap.c,v 1.11.2.1 2007/08/16 11:51:06 girish
43 * Manages physical address maps.
45 * Since the information managed by this module is
46 * also stored by the logical address mapping module,
47 * this module may throw away valid virtual-to-physical
48 * mappings at almost any time. However, invalidations
49 * of virtual-to-physical mappings must be done as
52 * In order to cope with hardware architectures which
53 * make virtual-to-physical map invalidates expensive,
54 * this module may delay invalidate or reduced protection
55 * operations until such time as they are actually
56 * necessary. This module is given full information as
57 * to which processors are currently using which maps,
58 * and to when physical maps must be made correct.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
67 #include <sys/param.h>
68 #include <sys/systm.h>
71 #include <sys/msgbuf.h>
72 #include <sys/mutex.h>
75 #include <sys/rwlock.h>
76 #include <sys/sched.h>
80 #include <sys/cpuset.h>
82 #include <sys/sysctl.h>
83 #include <sys/vmmeter.h>
90 #include <vm/vm_param.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_pager.h>
100 #include <machine/cache.h>
101 #include <machine/md_var.h>
102 #include <machine/tlb.h>
106 #if !defined(DIAGNOSTIC)
107 #define PMAP_INLINE __inline
113 #define PV_STAT(x) do { x ; } while (0)
115 #define PV_STAT(x) do { } while (0)
119 * Get PDEs and PTEs for user/kernel address space
121 #define pmap_seg_index(v) (((v) >> SEGSHIFT) & (NPDEPG - 1))
122 #define pmap_pde_index(v) (((v) >> PDRSHIFT) & (NPDEPG - 1))
123 #define pmap_pte_index(v) (((v) >> PAGE_SHIFT) & (NPTEPG - 1))
124 #define pmap_pde_pindex(v) ((v) >> PDRSHIFT)
127 #define NUPDE (NPDEPG * NPDEPG)
128 #define NUSERPGTBLS (NUPDE + NPDEPG)
130 #define NUPDE (NPDEPG)
131 #define NUSERPGTBLS (NUPDE)
134 #define is_kernel_pmap(x) ((x) == kernel_pmap)
136 struct pmap kernel_pmap_store;
137 pd_entry_t *kernel_segmap;
139 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
140 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
143 unsigned pmap_max_asid; /* max ASID supported by the system */
145 #define PMAP_ASID_RESERVED 0
147 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
149 static void pmap_asid_alloc(pmap_t pmap);
151 static struct rwlock_padalign pvh_global_lock;
154 * Data for the pv entry allocation mechanism
156 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
157 static int pv_entry_count;
159 static void free_pv_chunk(struct pv_chunk *pc);
160 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
161 static pv_entry_t get_pv_entry(pmap_t pmap, boolean_t try);
162 static vm_page_t pmap_pv_reclaim(pmap_t locked_pmap);
163 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
164 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
166 static vm_page_t pmap_alloc_direct_page(unsigned int index, int req);
167 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
168 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
169 static void pmap_grow_direct_page(int req);
170 static int pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
172 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
173 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va);
174 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte,
175 vm_offset_t va, vm_page_t m);
176 static void pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte);
177 static void pmap_invalidate_all(pmap_t pmap);
178 static void pmap_invalidate_page(pmap_t pmap, vm_offset_t va);
179 static void _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m);
181 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags);
182 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, u_int flags);
183 static int pmap_unuse_pt(pmap_t, vm_offset_t, pd_entry_t);
184 static pt_entry_t init_pte_prot(vm_page_t m, vm_prot_t access, vm_prot_t prot);
186 static void pmap_invalidate_page_action(void *arg);
187 static void pmap_invalidate_range_action(void *arg);
188 static void pmap_update_page_action(void *arg);
192 * This structure is for high memory (memory above 512Meg in 32 bit) support.
193 * The highmem area does not have a KSEG0 mapping, and we need a mechanism to
194 * do temporary per-CPU mappings for pmap_zero_page, pmap_copy_page etc.
196 * At bootup, we reserve 2 virtual pages per CPU for mapping highmem pages. To
197 * access a highmem physical address on a CPU, we map the physical address to
198 * the reserved virtual address for the CPU in the kernel pagetable. This is
199 * done with interrupts disabled(although a spinlock and sched_pin would be
202 struct local_sysmaps {
205 uint16_t valid1, valid2;
207 static struct local_sysmaps sysmap_lmem[MAXCPU];
210 pmap_alloc_lmem_map(void)
214 for (i = 0; i < MAXCPU; i++) {
215 sysmap_lmem[i].base = virtual_avail;
216 virtual_avail += PAGE_SIZE * 2;
217 sysmap_lmem[i].valid1 = sysmap_lmem[i].valid2 = 0;
221 static __inline vm_offset_t
222 pmap_lmem_map1(vm_paddr_t phys)
224 struct local_sysmaps *sysm;
225 pt_entry_t *pte, npte;
230 intr = intr_disable();
231 cpu = PCPU_GET(cpuid);
232 sysm = &sysmap_lmem[cpu];
233 sysm->saved_intr = intr;
235 npte = TLBLO_PA_TO_PFN(phys) | PTE_C_CACHE | PTE_D | PTE_V | PTE_G;
236 pte = pmap_pte(kernel_pmap, va);
242 static __inline vm_offset_t
243 pmap_lmem_map2(vm_paddr_t phys1, vm_paddr_t phys2)
245 struct local_sysmaps *sysm;
246 pt_entry_t *pte, npte;
247 vm_offset_t va1, va2;
251 intr = intr_disable();
252 cpu = PCPU_GET(cpuid);
253 sysm = &sysmap_lmem[cpu];
254 sysm->saved_intr = intr;
256 va2 = sysm->base + PAGE_SIZE;
257 npte = TLBLO_PA_TO_PFN(phys1) | PTE_C_CACHE | PTE_D | PTE_V | PTE_G;
258 pte = pmap_pte(kernel_pmap, va1);
260 npte = TLBLO_PA_TO_PFN(phys2) | PTE_C_CACHE | PTE_D | PTE_V | PTE_G;
261 pte = pmap_pte(kernel_pmap, va2);
269 pmap_lmem_unmap(void)
271 struct local_sysmaps *sysm;
275 cpu = PCPU_GET(cpuid);
276 sysm = &sysmap_lmem[cpu];
277 pte = pmap_pte(kernel_pmap, sysm->base);
279 tlb_invalidate_address(kernel_pmap, sysm->base);
282 pte = pmap_pte(kernel_pmap, sysm->base + PAGE_SIZE);
284 tlb_invalidate_address(kernel_pmap, sysm->base + PAGE_SIZE);
287 intr_restore(sysm->saved_intr);
289 #else /* __mips_n64 */
292 pmap_alloc_lmem_map(void)
296 static __inline vm_offset_t
297 pmap_lmem_map1(vm_paddr_t phys)
303 static __inline vm_offset_t
304 pmap_lmem_map2(vm_paddr_t phys1, vm_paddr_t phys2)
310 static __inline vm_offset_t
311 pmap_lmem_unmap(void)
316 #endif /* !__mips_n64 */
319 is_cacheable_page(vm_paddr_t pa, vm_page_t m)
322 return ((m->md.pv_flags & PV_MEMATTR_UNCACHEABLE) == 0 &&
323 is_cacheable_mem(pa));
328 * Page table entry lookup routines.
330 static __inline pd_entry_t *
331 pmap_segmap(pmap_t pmap, vm_offset_t va)
334 return (&pmap->pm_segtab[pmap_seg_index(va)]);
338 static __inline pd_entry_t *
339 pmap_pdpe_to_pde(pd_entry_t *pdpe, vm_offset_t va)
343 pde = (pd_entry_t *)*pdpe;
344 return (&pde[pmap_pde_index(va)]);
347 static __inline pd_entry_t *
348 pmap_pde(pmap_t pmap, vm_offset_t va)
352 pdpe = pmap_segmap(pmap, va);
356 return (pmap_pdpe_to_pde(pdpe, va));
359 static __inline pd_entry_t *
360 pmap_pdpe_to_pde(pd_entry_t *pdpe, vm_offset_t va)
367 pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va)
370 return (pmap_segmap(pmap, va));
374 static __inline pt_entry_t *
375 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
379 pte = (pt_entry_t *)*pde;
380 return (&pte[pmap_pte_index(va)]);
384 pmap_pte(pmap_t pmap, vm_offset_t va)
388 pde = pmap_pde(pmap, va);
389 if (pde == NULL || *pde == NULL)
392 return (pmap_pde_to_pte(pde, va));
396 pmap_steal_memory(vm_size_t size)
398 vm_paddr_t bank_size, pa;
401 size = round_page(size);
402 bank_size = phys_avail[1] - phys_avail[0];
403 while (size > bank_size) {
406 for (i = 0; phys_avail[i + 2]; i += 2) {
407 phys_avail[i] = phys_avail[i + 2];
408 phys_avail[i + 1] = phys_avail[i + 3];
411 phys_avail[i + 1] = 0;
413 panic("pmap_steal_memory: out of memory");
414 bank_size = phys_avail[1] - phys_avail[0];
418 phys_avail[0] += size;
419 if (MIPS_DIRECT_MAPPABLE(pa) == 0)
420 panic("Out of memory below 512Meg?");
421 va = MIPS_PHYS_TO_DIRECT(pa);
422 bzero((caddr_t)va, size);
427 * Bootstrap the system enough to run with virtual memory. This
428 * assumes that the phys_avail array has been initialized.
431 pmap_create_kernel_pagetable(void)
443 * Allocate segment table for the kernel
445 kernel_segmap = (pd_entry_t *)pmap_steal_memory(PAGE_SIZE);
448 * Allocate second level page tables for the kernel
451 npde = howmany(NKPT, NPDEPG);
452 pdaddr = pmap_steal_memory(PAGE_SIZE * npde);
455 ptaddr = pmap_steal_memory(PAGE_SIZE * nkpt);
458 * The R[4-7]?00 stores only one copy of the Global bit in the
459 * translation lookaside buffer for each 2 page entry. Thus invalid
460 * entrys must have the Global bit set so when Entry LO and Entry HI
461 * G bits are anded together they will produce a global bit to store
464 for (i = 0, pte = (pt_entry_t *)ptaddr; i < (nkpt * NPTEPG); i++, pte++)
468 for (i = 0, npt = nkpt; npt > 0; i++) {
469 kernel_segmap[i] = (pd_entry_t)(pdaddr + i * PAGE_SIZE);
470 pde = (pd_entry_t *)kernel_segmap[i];
472 for (j = 0; j < NPDEPG && npt > 0; j++, npt--)
473 pde[j] = (pd_entry_t)(ptaddr + (i * NPDEPG + j) * PAGE_SIZE);
476 for (i = 0, j = pmap_seg_index(VM_MIN_KERNEL_ADDRESS); i < nkpt; i++, j++)
477 kernel_segmap[j] = (pd_entry_t)(ptaddr + (i * PAGE_SIZE));
480 PMAP_LOCK_INIT(kernel_pmap);
481 kernel_pmap->pm_segtab = kernel_segmap;
482 CPU_FILL(&kernel_pmap->pm_active);
483 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
484 kernel_pmap->pm_asid[0].asid = PMAP_ASID_RESERVED;
485 kernel_pmap->pm_asid[0].gen = 0;
486 kernel_vm_end += nkpt * NPTEPG * PAGE_SIZE;
493 int need_local_mappings = 0;
497 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
499 * Keep the memory aligned on page boundary.
501 phys_avail[i] = round_page(phys_avail[i]);
502 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
506 if (phys_avail[i - 2] > phys_avail[i]) {
509 ptemp[0] = phys_avail[i + 0];
510 ptemp[1] = phys_avail[i + 1];
512 phys_avail[i + 0] = phys_avail[i - 2];
513 phys_avail[i + 1] = phys_avail[i - 1];
515 phys_avail[i - 2] = ptemp[0];
516 phys_avail[i - 1] = ptemp[1];
522 * In 32 bit, we may have memory which cannot be mapped directly.
523 * This memory will need temporary mapping before it can be
526 if (!MIPS_DIRECT_MAPPABLE(phys_avail[i - 1] - 1))
527 need_local_mappings = 1;
530 * Copy the phys_avail[] array before we start stealing memory from it.
532 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
533 physmem_desc[i] = phys_avail[i];
534 physmem_desc[i + 1] = phys_avail[i + 1];
537 Maxmem = atop(phys_avail[i - 1]);
540 printf("Physical memory chunk(s):\n");
541 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
544 size = phys_avail[i + 1] - phys_avail[i];
545 printf("%#08jx - %#08jx, %ju bytes (%ju pages)\n",
546 (uintmax_t) phys_avail[i],
547 (uintmax_t) phys_avail[i + 1] - 1,
548 (uintmax_t) size, (uintmax_t) size / PAGE_SIZE);
550 printf("Maxmem is 0x%0jx\n", ptoa((uintmax_t)Maxmem));
553 * Steal the message buffer from the beginning of memory.
555 msgbufp = (struct msgbuf *)pmap_steal_memory(msgbufsize);
556 msgbufinit(msgbufp, msgbufsize);
559 * Steal thread0 kstack.
561 kstack0 = pmap_steal_memory(KSTACK_PAGES << PAGE_SHIFT);
563 virtual_avail = VM_MIN_KERNEL_ADDRESS;
564 virtual_end = VM_MAX_KERNEL_ADDRESS;
568 * Steal some virtual address space to map the pcpu area.
570 virtual_avail = roundup2(virtual_avail, PAGE_SIZE * 2);
571 pcpup = (struct pcpu *)virtual_avail;
572 virtual_avail += PAGE_SIZE * 2;
575 * Initialize the wired TLB entry mapping the pcpu region for
576 * the BSP at 'pcpup'. Up until this point we were operating
577 * with the 'pcpup' for the BSP pointing to a virtual address
578 * in KSEG0 so there was no need for a TLB mapping.
580 mips_pcpu_tlb_init(PCPU_ADDR(0));
583 printf("pcpu is available at virtual address %p.\n", pcpup);
586 if (need_local_mappings)
587 pmap_alloc_lmem_map();
588 pmap_create_kernel_pagetable();
589 pmap_max_asid = VMNUM_PIDS;
594 * Initialize the global pv list lock.
596 rw_init(&pvh_global_lock, "pmap pv global");
600 * Initialize a vm_page's machine-dependent fields.
603 pmap_page_init(vm_page_t m)
606 TAILQ_INIT(&m->md.pv_list);
611 * Initialize the pmap module.
612 * Called by vm_init, to initialize any structures that the pmap
613 * system needs to map virtual memory.
620 /***************************************************
621 * Low level helper routines.....
622 ***************************************************/
626 pmap_call_on_active_cpus(pmap_t pmap, void (*fn)(void *), void *arg)
628 int cpuid, cpu, self;
629 cpuset_t active_cpus;
632 if (is_kernel_pmap(pmap)) {
633 smp_rendezvous(NULL, fn, NULL, arg);
636 /* Force ASID update on inactive CPUs */
638 if (!CPU_ISSET(cpu, &pmap->pm_active))
639 pmap->pm_asid[cpu].gen = 0;
641 cpuid = PCPU_GET(cpuid);
643 * XXX: barrier/locking for active?
645 * Take a snapshot of active here, any further changes are ignored.
646 * tlb update/invalidate should be harmless on inactive CPUs
648 active_cpus = pmap->pm_active;
649 self = CPU_ISSET(cpuid, &active_cpus);
650 CPU_CLR(cpuid, &active_cpus);
651 /* Optimize for the case where this cpu is the only active one */
652 if (CPU_EMPTY(&active_cpus)) {
657 CPU_SET(cpuid, &active_cpus);
658 smp_rendezvous_cpus(active_cpus, NULL, fn, NULL, arg);
665 pmap_call_on_active_cpus(pmap_t pmap, void (*fn)(void *), void *arg)
669 if (is_kernel_pmap(pmap)) {
673 cpuid = PCPU_GET(cpuid);
674 if (!CPU_ISSET(cpuid, &pmap->pm_active))
675 pmap->pm_asid[cpuid].gen = 0;
682 pmap_invalidate_all(pmap_t pmap)
685 pmap_call_on_active_cpus(pmap,
686 (void (*)(void *))tlb_invalidate_all_user, pmap);
689 struct pmap_invalidate_page_arg {
695 pmap_invalidate_page_action(void *arg)
697 struct pmap_invalidate_page_arg *p = arg;
699 tlb_invalidate_address(p->pmap, p->va);
703 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
705 struct pmap_invalidate_page_arg arg;
709 pmap_call_on_active_cpus(pmap, pmap_invalidate_page_action, &arg);
712 struct pmap_invalidate_range_arg {
719 pmap_invalidate_range_action(void *arg)
721 struct pmap_invalidate_range_arg *p = arg;
723 tlb_invalidate_range(p->pmap, p->sva, p->eva);
727 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
729 struct pmap_invalidate_range_arg arg;
734 pmap_call_on_active_cpus(pmap, pmap_invalidate_range_action, &arg);
737 struct pmap_update_page_arg {
744 pmap_update_page_action(void *arg)
746 struct pmap_update_page_arg *p = arg;
748 tlb_update(p->pmap, p->va, p->pte);
752 pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte)
754 struct pmap_update_page_arg arg;
759 pmap_call_on_active_cpus(pmap, pmap_update_page_action, &arg);
763 * Routine: pmap_extract
765 * Extract the physical page address associated
766 * with the given map/virtual_address pair.
769 pmap_extract(pmap_t pmap, vm_offset_t va)
772 vm_offset_t retval = 0;
775 pte = pmap_pte(pmap, va);
777 retval = TLBLO_PTE_TO_PA(*pte) | (va & PAGE_MASK);
784 * Routine: pmap_extract_and_hold
786 * Atomically extract and hold the physical page
787 * with the given pmap and virtual address pair
788 * if that mapping permits the given protection.
791 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
793 pt_entry_t pte, *ptep;
794 vm_paddr_t pa, pte_pa;
801 ptep = pmap_pte(pmap, va);
804 if (pte_test(&pte, PTE_V) && (!pte_test(&pte, PTE_RO) ||
805 (prot & VM_PROT_WRITE) == 0)) {
806 pte_pa = TLBLO_PTE_TO_PA(pte);
807 if (vm_page_pa_tryrelock(pmap, pte_pa, &pa))
809 m = PHYS_TO_VM_PAGE(pte_pa);
818 /***************************************************
819 * Low level mapping routines.....
820 ***************************************************/
823 * add a wired page to the kva
826 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int attr)
829 pt_entry_t opte, npte;
832 printf("pmap_kenter: va: %p -> pa: %p\n", (void *)va, (void *)pa);
835 pte = pmap_pte(kernel_pmap, va);
837 npte = TLBLO_PA_TO_PFN(pa) | attr | PTE_D | PTE_V | PTE_G;
839 if (pte_test(&opte, PTE_V) && opte != npte)
840 pmap_update_page(kernel_pmap, va, npte);
844 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
847 KASSERT(is_cacheable_mem(pa),
848 ("pmap_kenter: memory at 0x%lx is not cacheable", (u_long)pa));
850 pmap_kenter_attr(va, pa, PTE_C_CACHE);
854 * remove a page from the kernel pagetables
856 /* PMAP_INLINE */ void
857 pmap_kremove(vm_offset_t va)
862 * Write back all caches from the page being destroyed
864 mips_dcache_wbinv_range_index(va, PAGE_SIZE);
866 pte = pmap_pte(kernel_pmap, va);
868 pmap_invalidate_page(kernel_pmap, va);
872 * Used to map a range of physical addresses into kernel
873 * virtual address space.
875 * The value passed in '*virt' is a suggested virtual address for
876 * the mapping. Architectures which can support a direct-mapped
877 * physical to virtual region can return the appropriate address
878 * within that region, leaving '*virt' unchanged. Other
879 * architectures should map the pages starting at '*virt' and
880 * update '*virt' with the first usable address after the mapped
883 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
886 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
890 if (MIPS_DIRECT_MAPPABLE(end - 1))
891 return (MIPS_PHYS_TO_DIRECT(start));
894 while (start < end) {
895 pmap_kenter(va, start);
904 * Add a list of wired pages to the kva
905 * this routine is only used for temporary
906 * kernel mappings that do not need to have
907 * page modification or references recorded.
908 * Note that old mappings are simply written
909 * over. The page *must* be wired.
912 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
915 vm_offset_t origva = va;
917 for (i = 0; i < count; i++) {
918 pmap_flush_pvcache(m[i]);
919 pmap_kenter(va, VM_PAGE_TO_PHYS(m[i]));
923 mips_dcache_wbinv_range_index(origva, PAGE_SIZE*count);
927 * this routine jerks page mappings from the
928 * kernel -- it is meant only for temporary mappings.
931 pmap_qremove(vm_offset_t va, int count)
938 mips_dcache_wbinv_range_index(va, PAGE_SIZE * count);
941 pte = pmap_pte(kernel_pmap, va);
944 } while (--count > 0);
945 pmap_invalidate_range(kernel_pmap, origva, va);
948 /***************************************************
949 * Page table page management routines.....
950 ***************************************************/
953 * Decrements a page table page's wire count, which is used to record the
954 * number of valid page table entries within the page. If the wire count
955 * drops to zero, then the page table page is unmapped. Returns TRUE if the
956 * page table page was unmapped and FALSE otherwise.
958 static PMAP_INLINE boolean_t
959 pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m)
963 if (m->wire_count == 0) {
964 _pmap_unwire_ptp(pmap, va, m);
971 _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m)
975 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
977 * unmap the page table page
980 if (m->pindex < NUPDE)
981 pde = pmap_pde(pmap, va);
983 pde = pmap_segmap(pmap, va);
985 pde = pmap_pde(pmap, va);
988 pmap->pm_stats.resident_count--;
991 if (m->pindex < NUPDE) {
996 * Recursively decrement next level pagetable refcount
998 pdp = (pd_entry_t *)*pmap_segmap(pmap, va);
999 pdpg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(pdp));
1000 pmap_unwire_ptp(pmap, va, pdpg);
1005 * If the page is finally unwired, simply free it.
1007 vm_page_free_zero(m);
1008 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1012 * After removing a page table entry, this routine is used to
1013 * conditionally free the page, and manage the hold/wire counts.
1016 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pd_entry_t pde)
1020 if (va >= VM_MAXUSER_ADDRESS)
1022 KASSERT(pde != 0, ("pmap_unuse_pt: pde != 0"));
1023 mpte = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(pde));
1024 return (pmap_unwire_ptp(pmap, va, mpte));
1028 pmap_pinit0(pmap_t pmap)
1032 PMAP_LOCK_INIT(pmap);
1033 pmap->pm_segtab = kernel_segmap;
1034 CPU_ZERO(&pmap->pm_active);
1035 for (i = 0; i < MAXCPU; i++) {
1036 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
1037 pmap->pm_asid[i].gen = 0;
1039 PCPU_SET(curpmap, pmap);
1040 TAILQ_INIT(&pmap->pm_pvchunk);
1041 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1045 pmap_grow_direct_page(int req)
1051 if (!vm_page_reclaim_contig(req, 1, 0, MIPS_KSEG0_LARGEST_PHYS,
1058 pmap_alloc_direct_page(unsigned int index, int req)
1062 m = vm_page_alloc_freelist(VM_FREELIST_DIRECT, req | VM_ALLOC_WIRED |
1067 if ((m->flags & PG_ZERO) == 0)
1075 * Initialize a preallocated and zeroed pmap structure,
1076 * such as one in a vmspace structure.
1079 pmap_pinit(pmap_t pmap)
1086 * allocate the page directory page
1088 req_class = VM_ALLOC_NORMAL;
1089 while ((ptdpg = pmap_alloc_direct_page(NUSERPGTBLS, req_class)) ==
1091 pmap_grow_direct_page(req_class);
1093 ptdva = MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(ptdpg));
1094 pmap->pm_segtab = (pd_entry_t *)ptdva;
1095 CPU_ZERO(&pmap->pm_active);
1096 for (i = 0; i < MAXCPU; i++) {
1097 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
1098 pmap->pm_asid[i].gen = 0;
1100 TAILQ_INIT(&pmap->pm_pvchunk);
1101 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1107 * this routine is called if the page table page is not
1111 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, u_int flags)
1118 * Find or fabricate a new pagetable page
1120 req_class = VM_ALLOC_NORMAL;
1121 if ((m = pmap_alloc_direct_page(ptepindex, req_class)) == NULL) {
1122 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
1124 rw_wunlock(&pvh_global_lock);
1125 pmap_grow_direct_page(req_class);
1126 rw_wlock(&pvh_global_lock);
1131 * Indicate the need to retry. While waiting, the page
1132 * table page may have been allocated.
1138 * Map the pagetable page into the process address space, if it
1139 * isn't already there.
1141 pageva = MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(m));
1144 if (ptepindex >= NUPDE) {
1145 pmap->pm_segtab[ptepindex - NUPDE] = (pd_entry_t)pageva;
1147 pd_entry_t *pdep, *pde;
1148 int segindex = ptepindex >> (SEGSHIFT - PDRSHIFT);
1149 int pdeindex = ptepindex & (NPDEPG - 1);
1152 pdep = &pmap->pm_segtab[segindex];
1153 if (*pdep == NULL) {
1154 /* recurse for allocating page dir */
1155 if (_pmap_allocpte(pmap, NUPDE + segindex,
1157 /* alloc failed, release current */
1159 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1160 vm_page_free_zero(m);
1164 pg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(*pdep));
1167 /* Next level entry */
1168 pde = (pd_entry_t *)*pdep;
1169 pde[pdeindex] = (pd_entry_t)pageva;
1172 pmap->pm_segtab[ptepindex] = (pd_entry_t)pageva;
1174 pmap->pm_stats.resident_count++;
1179 pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags)
1186 * Calculate pagetable page index
1188 ptepindex = pmap_pde_pindex(va);
1191 * Get the page directory entry
1193 pde = pmap_pde(pmap, va);
1196 * If the page table page is mapped, we just increment the hold
1197 * count, and activate it.
1199 if (pde != NULL && *pde != NULL) {
1200 m = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(*pde));
1204 * Here if the pte page isn't mapped, or if it has been
1207 m = _pmap_allocpte(pmap, ptepindex, flags);
1208 if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
1215 /***************************************************
1216 * Pmap allocation/deallocation routines.
1217 ***************************************************/
1220 * Release any resources held by the given physical map.
1221 * Called when a pmap initialized by pmap_pinit is being released.
1222 * Should only be called if the map contains no valid mappings.
1225 pmap_release(pmap_t pmap)
1230 KASSERT(pmap->pm_stats.resident_count == 0,
1231 ("pmap_release: pmap resident count %ld != 0",
1232 pmap->pm_stats.resident_count));
1234 ptdva = (vm_offset_t)pmap->pm_segtab;
1235 ptdpg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(ptdva));
1237 ptdpg->wire_count--;
1238 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1239 vm_page_free_zero(ptdpg);
1243 * grow the number of kernel page table entries, if needed
1246 pmap_growkernel(vm_offset_t addr)
1249 pd_entry_t *pde, *pdpe;
1253 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1254 req_class = VM_ALLOC_INTERRUPT;
1255 addr = roundup2(addr, NBSEG);
1256 if (addr - 1 >= kernel_map->max_offset)
1257 addr = kernel_map->max_offset;
1258 while (kernel_vm_end < addr) {
1259 pdpe = pmap_segmap(kernel_pmap, kernel_vm_end);
1262 /* new intermediate page table entry */
1263 nkpg = pmap_alloc_direct_page(nkpt, req_class);
1265 panic("pmap_growkernel: no memory to grow kernel");
1266 *pdpe = (pd_entry_t)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(nkpg));
1267 continue; /* try again */
1270 pde = pmap_pdpe_to_pde(pdpe, kernel_vm_end);
1272 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1273 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1274 kernel_vm_end = kernel_map->max_offset;
1281 * This index is bogus, but out of the way
1283 nkpg = pmap_alloc_direct_page(nkpt, req_class);
1285 if (nkpg == NULL && vm_page_reclaim_contig(req_class, 1,
1286 0, MIPS_KSEG0_LARGEST_PHYS, PAGE_SIZE, 0))
1287 nkpg = pmap_alloc_direct_page(nkpt, req_class);
1290 panic("pmap_growkernel: no memory to grow kernel");
1292 *pde = (pd_entry_t)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(nkpg));
1295 * The R[4-7]?00 stores only one copy of the Global bit in
1296 * the translation lookaside buffer for each 2 page entry.
1297 * Thus invalid entrys must have the Global bit set so when
1298 * Entry LO and Entry HI G bits are anded together they will
1299 * produce a global bit to store in the tlb.
1301 pte = (pt_entry_t *)*pde;
1302 for (i = 0; i < NPTEPG; i++)
1305 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1306 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1307 kernel_vm_end = kernel_map->max_offset;
1313 /***************************************************
1314 * page management routines.
1315 ***************************************************/
1317 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1319 CTASSERT(_NPCM == 3);
1320 CTASSERT(_NPCPV == 168);
1322 CTASSERT(_NPCM == 11);
1323 CTASSERT(_NPCPV == 336);
1326 static __inline struct pv_chunk *
1327 pv_to_chunk(pv_entry_t pv)
1330 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
1333 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1336 #define PC_FREE0_1 0xfffffffffffffffful
1337 #define PC_FREE2 0x000000fffffffffful
1339 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
1340 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
1343 static const u_long pc_freemask[_NPCM] = {
1345 PC_FREE0_1, PC_FREE0_1, PC_FREE2
1347 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1348 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1349 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1350 PC_FREE0_9, PC_FREE10
1354 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
1356 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1357 "Current number of pv entries");
1360 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1362 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1363 "Current number of pv entry chunks");
1364 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1365 "Current number of pv entry chunks allocated");
1366 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1367 "Current number of pv entry chunks frees");
1368 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1369 "Number of times tried to get a chunk page but failed.");
1371 static long pv_entry_frees, pv_entry_allocs;
1372 static int pv_entry_spare;
1374 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1375 "Current number of pv entry frees");
1376 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1377 "Current number of pv entry allocs");
1378 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1379 "Current number of spare pv entries");
1383 * We are in a serious low memory condition. Resort to
1384 * drastic measures to free some pages so we can allocate
1385 * another pv entry chunk.
1388 pmap_pv_reclaim(pmap_t locked_pmap)
1391 struct pv_chunk *pc;
1394 pt_entry_t *pte, oldpte;
1399 int bit, field, freed, idx;
1401 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1404 TAILQ_INIT(&newtail);
1405 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL) {
1406 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
1407 if (pmap != pc->pc_pmap) {
1409 pmap_invalidate_all(pmap);
1410 if (pmap != locked_pmap)
1414 /* Avoid deadlock and lock recursion. */
1415 if (pmap > locked_pmap)
1417 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
1419 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
1425 * Destroy every non-wired, 4 KB page mapping in the chunk.
1428 for (field = 0; field < _NPCM; field++) {
1429 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
1430 inuse != 0; inuse &= ~(1UL << bit)) {
1431 bit = ffsl(inuse) - 1;
1432 idx = field * sizeof(inuse) * NBBY + bit;
1433 pv = &pc->pc_pventry[idx];
1435 pde = pmap_pde(pmap, va);
1436 KASSERT(pde != NULL && *pde != 0,
1437 ("pmap_pv_reclaim: pde"));
1438 pte = pmap_pde_to_pte(pde, va);
1440 if (pte_test(&oldpte, PTE_W))
1442 if (is_kernel_pmap(pmap))
1446 m = PHYS_TO_VM_PAGE(TLBLO_PTE_TO_PA(oldpte));
1447 if (pte_test(&oldpte, PTE_D))
1449 if (m->md.pv_flags & PV_TABLE_REF)
1450 vm_page_aflag_set(m, PGA_REFERENCED);
1451 m->md.pv_flags &= ~PV_TABLE_REF;
1452 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1453 if (TAILQ_EMPTY(&m->md.pv_list))
1454 vm_page_aflag_clear(m, PGA_WRITEABLE);
1455 pc->pc_map[field] |= 1UL << bit;
1456 pmap_unuse_pt(pmap, va, *pde);
1461 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
1464 /* Every freed mapping is for a 4 KB page. */
1465 pmap->pm_stats.resident_count -= freed;
1466 PV_STAT(pv_entry_frees += freed);
1467 PV_STAT(pv_entry_spare += freed);
1468 pv_entry_count -= freed;
1469 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1470 for (field = 0; field < _NPCM; field++)
1471 if (pc->pc_map[field] != pc_freemask[field]) {
1472 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
1474 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
1477 * One freed pv entry in locked_pmap is
1480 if (pmap == locked_pmap)
1484 if (field == _NPCM) {
1485 PV_STAT(pv_entry_spare -= _NPCPV);
1486 PV_STAT(pc_chunk_count--);
1487 PV_STAT(pc_chunk_frees++);
1488 /* Entire chunk is free; return it. */
1489 m_pc = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(
1495 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
1497 pmap_invalidate_all(pmap);
1498 if (pmap != locked_pmap)
1505 * free the pv_entry back to the free list
1508 free_pv_entry(pmap_t pmap, pv_entry_t pv)
1510 struct pv_chunk *pc;
1511 int bit, field, idx;
1513 rw_assert(&pvh_global_lock, RA_WLOCKED);
1514 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1515 PV_STAT(pv_entry_frees++);
1516 PV_STAT(pv_entry_spare++);
1518 pc = pv_to_chunk(pv);
1519 idx = pv - &pc->pc_pventry[0];
1520 field = idx / (sizeof(u_long) * NBBY);
1521 bit = idx % (sizeof(u_long) * NBBY);
1522 pc->pc_map[field] |= 1ul << bit;
1523 for (idx = 0; idx < _NPCM; idx++)
1524 if (pc->pc_map[idx] != pc_freemask[idx]) {
1526 * 98% of the time, pc is already at the head of the
1527 * list. If it isn't already, move it to the head.
1529 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
1531 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1532 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
1537 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1542 free_pv_chunk(struct pv_chunk *pc)
1546 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
1547 PV_STAT(pv_entry_spare -= _NPCPV);
1548 PV_STAT(pc_chunk_count--);
1549 PV_STAT(pc_chunk_frees++);
1550 /* entire chunk is free, return it */
1551 m = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS((vm_offset_t)pc));
1552 vm_page_unwire(m, PQ_NONE);
1557 * get a new pv_entry, allocating a block from the system
1561 get_pv_entry(pmap_t pmap, boolean_t try)
1563 struct pv_chunk *pc;
1566 int bit, field, idx;
1568 rw_assert(&pvh_global_lock, RA_WLOCKED);
1569 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1570 PV_STAT(pv_entry_allocs++);
1573 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
1575 for (field = 0; field < _NPCM; field++) {
1576 if (pc->pc_map[field]) {
1577 bit = ffsl(pc->pc_map[field]) - 1;
1581 if (field < _NPCM) {
1582 idx = field * sizeof(pc->pc_map[field]) * NBBY + bit;
1583 pv = &pc->pc_pventry[idx];
1584 pc->pc_map[field] &= ~(1ul << bit);
1585 /* If this was the last item, move it to tail */
1586 for (field = 0; field < _NPCM; field++)
1587 if (pc->pc_map[field] != 0) {
1588 PV_STAT(pv_entry_spare--);
1589 return (pv); /* not full, return */
1591 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1592 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
1593 PV_STAT(pv_entry_spare--);
1597 /* No free items, allocate another chunk */
1598 m = vm_page_alloc_freelist(VM_FREELIST_DIRECT, VM_ALLOC_NORMAL |
1603 PV_STAT(pc_chunk_tryfail++);
1606 m = pmap_pv_reclaim(pmap);
1610 PV_STAT(pc_chunk_count++);
1611 PV_STAT(pc_chunk_allocs++);
1612 pc = (struct pv_chunk *)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(m));
1614 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
1615 for (field = 1; field < _NPCM; field++)
1616 pc->pc_map[field] = pc_freemask[field];
1617 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
1618 pv = &pc->pc_pventry[0];
1619 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
1620 PV_STAT(pv_entry_spare += _NPCPV - 1);
1625 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
1629 rw_assert(&pvh_global_lock, RA_WLOCKED);
1630 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
1631 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
1632 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
1640 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
1644 pv = pmap_pvh_remove(pvh, pmap, va);
1645 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found, pa %lx va %lx",
1646 (u_long)VM_PAGE_TO_PHYS(__containerof(pvh, struct vm_page, md)),
1648 free_pv_entry(pmap, pv);
1652 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1655 rw_assert(&pvh_global_lock, RA_WLOCKED);
1656 pmap_pvh_free(&m->md, pmap, va);
1657 if (TAILQ_EMPTY(&m->md.pv_list))
1658 vm_page_aflag_clear(m, PGA_WRITEABLE);
1662 * Conditionally create a pv entry.
1665 pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte, vm_offset_t va,
1670 rw_assert(&pvh_global_lock, RA_WLOCKED);
1671 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1672 if ((pv = get_pv_entry(pmap, TRUE)) != NULL) {
1674 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1681 * pmap_remove_pte: do the things to unmap a page in a process
1684 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
1691 rw_assert(&pvh_global_lock, RA_WLOCKED);
1692 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1695 * Write back all cache lines from the page being unmapped.
1697 mips_dcache_wbinv_range_index(va, PAGE_SIZE);
1700 if (is_kernel_pmap(pmap))
1705 if (pte_test(&oldpte, PTE_W))
1706 pmap->pm_stats.wired_count -= 1;
1708 pmap->pm_stats.resident_count -= 1;
1710 if (pte_test(&oldpte, PTE_MANAGED)) {
1711 pa = TLBLO_PTE_TO_PA(oldpte);
1712 m = PHYS_TO_VM_PAGE(pa);
1713 if (pte_test(&oldpte, PTE_D)) {
1714 KASSERT(!pte_test(&oldpte, PTE_RO),
1715 ("%s: modified page not writable: va: %p, pte: %#jx",
1716 __func__, (void *)va, (uintmax_t)oldpte));
1719 if (m->md.pv_flags & PV_TABLE_REF)
1720 vm_page_aflag_set(m, PGA_REFERENCED);
1721 m->md.pv_flags &= ~PV_TABLE_REF;
1723 pmap_remove_entry(pmap, m, va);
1725 return (pmap_unuse_pt(pmap, va, pde));
1729 * Remove a single page from a process address space
1732 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1737 rw_assert(&pvh_global_lock, RA_WLOCKED);
1738 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1739 pde = pmap_pde(pmap, va);
1740 if (pde == NULL || *pde == 0)
1742 ptq = pmap_pde_to_pte(pde, va);
1745 * If there is no pte for this address, just skip it!
1747 if (!pte_test(ptq, PTE_V))
1750 (void)pmap_remove_pte(pmap, ptq, va, *pde);
1751 pmap_invalidate_page(pmap, va);
1755 * Remove the given range of addresses from the specified map.
1757 * It is assumed that the start and end are properly
1758 * rounded to the page size.
1761 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1763 pd_entry_t *pde, *pdpe;
1765 vm_offset_t va, va_next;
1768 * Perform an unsynchronized read. This is, however, safe.
1770 if (pmap->pm_stats.resident_count == 0)
1773 rw_wlock(&pvh_global_lock);
1777 * special handling of removing one page. a very common operation
1778 * and easy to short circuit some code.
1780 if ((sva + PAGE_SIZE) == eva) {
1781 pmap_remove_page(pmap, sva);
1784 for (; sva < eva; sva = va_next) {
1785 pdpe = pmap_segmap(pmap, sva);
1788 va_next = (sva + NBSEG) & ~SEGMASK;
1794 va_next = (sva + NBPDR) & ~PDRMASK;
1798 pde = pmap_pdpe_to_pde(pdpe, sva);
1803 * Limit our scan to either the end of the va represented
1804 * by the current page table page, or to the end of the
1805 * range being removed.
1811 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1813 if (!pte_test(pte, PTE_V)) {
1814 if (va != va_next) {
1815 pmap_invalidate_range(pmap, va, sva);
1822 if (pmap_remove_pte(pmap, pte, sva, *pde)) {
1828 pmap_invalidate_range(pmap, va, sva);
1831 rw_wunlock(&pvh_global_lock);
1836 * Routine: pmap_remove_all
1838 * Removes this physical page from
1839 * all physical maps in which it resides.
1840 * Reflects back modify bits to the pager.
1843 * Original versions of this routine were very
1844 * inefficient because they iteratively called
1845 * pmap_remove (slow...)
1849 pmap_remove_all(vm_page_t m)
1854 pt_entry_t *pte, tpte;
1856 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1857 ("pmap_remove_all: page %p is not managed", m));
1858 rw_wlock(&pvh_global_lock);
1860 if (m->md.pv_flags & PV_TABLE_REF)
1861 vm_page_aflag_set(m, PGA_REFERENCED);
1863 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1868 * If it's last mapping writeback all caches from
1869 * the page being destroyed
1871 if (TAILQ_NEXT(pv, pv_list) == NULL)
1872 mips_dcache_wbinv_range_index(pv->pv_va, PAGE_SIZE);
1874 pmap->pm_stats.resident_count--;
1876 pde = pmap_pde(pmap, pv->pv_va);
1877 KASSERT(pde != NULL && *pde != 0, ("pmap_remove_all: pde"));
1878 pte = pmap_pde_to_pte(pde, pv->pv_va);
1881 if (is_kernel_pmap(pmap))
1886 if (pte_test(&tpte, PTE_W))
1887 pmap->pm_stats.wired_count--;
1890 * Update the vm_page_t clean and reference bits.
1892 if (pte_test(&tpte, PTE_D)) {
1893 KASSERT(!pte_test(&tpte, PTE_RO),
1894 ("%s: modified page not writable: va: %p, pte: %#jx",
1895 __func__, (void *)pv->pv_va, (uintmax_t)tpte));
1898 pmap_invalidate_page(pmap, pv->pv_va);
1900 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1901 pmap_unuse_pt(pmap, pv->pv_va, *pde);
1902 free_pv_entry(pmap, pv);
1906 vm_page_aflag_clear(m, PGA_WRITEABLE);
1907 m->md.pv_flags &= ~PV_TABLE_REF;
1908 rw_wunlock(&pvh_global_lock);
1912 * Set the physical protection on the
1913 * specified range of this map as requested.
1916 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1918 pt_entry_t pbits, *pte;
1919 pd_entry_t *pde, *pdpe;
1920 vm_offset_t va, va_next;
1924 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1925 pmap_remove(pmap, sva, eva);
1928 if (prot & VM_PROT_WRITE)
1932 for (; sva < eva; sva = va_next) {
1933 pdpe = pmap_segmap(pmap, sva);
1936 va_next = (sva + NBSEG) & ~SEGMASK;
1942 va_next = (sva + NBPDR) & ~PDRMASK;
1946 pde = pmap_pdpe_to_pde(pdpe, sva);
1951 * Limit our scan to either the end of the va represented
1952 * by the current page table page, or to the end of the
1953 * range being write protected.
1959 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1962 if (!pte_test(&pbits, PTE_V) || pte_test(&pbits,
1964 if (va != va_next) {
1965 pmap_invalidate_range(pmap, va, sva);
1970 pte_set(&pbits, PTE_RO);
1971 if (pte_test(&pbits, PTE_D)) {
1972 pte_clear(&pbits, PTE_D);
1973 if (pte_test(&pbits, PTE_MANAGED)) {
1974 pa = TLBLO_PTE_TO_PA(pbits);
1975 m = PHYS_TO_VM_PAGE(pa);
1982 * Unless PTE_D is set, any TLB entries
1983 * mapping "sva" don't allow write access, so
1984 * they needn't be invalidated.
1986 if (va != va_next) {
1987 pmap_invalidate_range(pmap, va, sva);
1994 pmap_invalidate_range(pmap, va, sva);
2000 * Insert the given physical page (p) at
2001 * the specified virtual address (v) in the
2002 * target physical map with the protection requested.
2004 * If specified, the page will be wired down, meaning
2005 * that the related pte can not be reclaimed.
2007 * NB: This is the only routine which MAY NOT lazy-evaluate
2008 * or lose information. That is, this routine must actually
2009 * insert this page into the given map NOW.
2012 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2013 u_int flags, int8_t psind __unused)
2017 pt_entry_t origpte, newpte;
2022 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
2023 KASSERT((m->oflags & VPO_UNMANAGED) != 0 || va < kmi.clean_sva ||
2024 va >= kmi.clean_eva,
2025 ("pmap_enter: managed mapping within the clean submap"));
2026 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
2027 VM_OBJECT_ASSERT_LOCKED(m->object);
2028 pa = VM_PAGE_TO_PHYS(m);
2029 newpte = TLBLO_PA_TO_PFN(pa) | init_pte_prot(m, flags, prot);
2030 if ((flags & PMAP_ENTER_WIRED) != 0)
2032 if (is_kernel_pmap(pmap))
2034 if (is_cacheable_page(pa, m))
2035 newpte |= PTE_C_CACHE;
2037 newpte |= PTE_C_UNCACHED;
2041 rw_wlock(&pvh_global_lock);
2045 * In the case that a page table page is not resident, we are
2048 if (va < VM_MAXUSER_ADDRESS) {
2049 mpte = pmap_allocpte(pmap, va, flags);
2051 KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
2052 ("pmap_allocpte failed with sleep allowed"));
2053 rw_wunlock(&pvh_global_lock);
2055 return (KERN_RESOURCE_SHORTAGE);
2058 pte = pmap_pte(pmap, va);
2061 * Page Directory table entry not valid, we need a new PT page
2064 panic("pmap_enter: invalid page directory, pdir=%p, va=%p",
2065 (void *)pmap->pm_segtab, (void *)va);
2069 opa = TLBLO_PTE_TO_PA(origpte);
2072 * Mapping has not changed, must be protection or wiring change.
2074 if (pte_test(&origpte, PTE_V) && opa == pa) {
2076 * Wiring change, just update stats. We don't worry about
2077 * wiring PT pages as they remain resident as long as there
2078 * are valid mappings in them. Hence, if a user page is
2079 * wired, the PT page will be also.
2081 if (pte_test(&newpte, PTE_W) && !pte_test(&origpte, PTE_W))
2082 pmap->pm_stats.wired_count++;
2083 else if (!pte_test(&newpte, PTE_W) && pte_test(&origpte,
2085 pmap->pm_stats.wired_count--;
2087 KASSERT(!pte_test(&origpte, PTE_D | PTE_RO),
2088 ("%s: modified page not writable: va: %p, pte: %#jx",
2089 __func__, (void *)va, (uintmax_t)origpte));
2092 * Remove extra pte reference
2097 if (pte_test(&origpte, PTE_MANAGED)) {
2098 m->md.pv_flags |= PV_TABLE_REF;
2100 newpte |= PTE_MANAGED;
2101 if (!pte_test(&newpte, PTE_RO))
2102 vm_page_aflag_set(m, PGA_WRITEABLE);
2110 * Mapping has changed, invalidate old range and fall through to
2111 * handle validating new mapping.
2114 if (pte_test(&origpte, PTE_W))
2115 pmap->pm_stats.wired_count--;
2117 if (pte_test(&origpte, PTE_MANAGED)) {
2118 om = PHYS_TO_VM_PAGE(opa);
2119 pv = pmap_pvh_remove(&om->md, pmap, va);
2123 KASSERT(mpte->wire_count > 0,
2124 ("pmap_enter: missing reference to page table page,"
2125 " va: %p", (void *)va));
2128 pmap->pm_stats.resident_count++;
2131 * Enter on the PV list if part of our managed memory.
2133 if ((m->oflags & VPO_UNMANAGED) == 0) {
2134 m->md.pv_flags |= PV_TABLE_REF;
2136 pv = get_pv_entry(pmap, FALSE);
2138 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2139 newpte |= PTE_MANAGED;
2140 if (!pte_test(&newpte, PTE_RO))
2141 vm_page_aflag_set(m, PGA_WRITEABLE);
2142 } else if (pv != NULL)
2143 free_pv_entry(pmap, pv);
2146 * Increment counters
2148 if (pte_test(&newpte, PTE_W))
2149 pmap->pm_stats.wired_count++;
2154 printf("pmap_enter: va: %p -> pa: %p\n", (void *)va, (void *)pa);
2158 * if the mapping or permission bits are different, we need to
2161 if (origpte != newpte) {
2163 if (pte_test(&origpte, PTE_V)) {
2164 if (pte_test(&origpte, PTE_MANAGED) && opa != pa) {
2165 if (om->md.pv_flags & PV_TABLE_REF)
2166 vm_page_aflag_set(om, PGA_REFERENCED);
2167 om->md.pv_flags &= ~PV_TABLE_REF;
2169 if (pte_test(&origpte, PTE_D)) {
2170 KASSERT(!pte_test(&origpte, PTE_RO),
2171 ("pmap_enter: modified page not writable:"
2172 " va: %p, pte: %#jx", (void *)va, (uintmax_t)origpte));
2173 if (pte_test(&origpte, PTE_MANAGED))
2176 if (pte_test(&origpte, PTE_MANAGED) &&
2177 TAILQ_EMPTY(&om->md.pv_list))
2178 vm_page_aflag_clear(om, PGA_WRITEABLE);
2179 pmap_update_page(pmap, va, newpte);
2184 * Sync I & D caches for executable pages. Do this only if the
2185 * target pmap belongs to the current process. Otherwise, an
2186 * unresolvable TLB miss may occur.
2188 if (!is_kernel_pmap(pmap) && (pmap == &curproc->p_vmspace->vm_pmap) &&
2189 (prot & VM_PROT_EXECUTE)) {
2190 mips_icache_sync_range(va, PAGE_SIZE);
2191 mips_dcache_wbinv_range(va, PAGE_SIZE);
2193 rw_wunlock(&pvh_global_lock);
2195 return (KERN_SUCCESS);
2199 * this code makes some *MAJOR* assumptions:
2200 * 1. Current pmap & pmap exists.
2203 * 4. No page table pages.
2204 * but is *MUCH* faster than pmap_enter...
2208 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2211 rw_wlock(&pvh_global_lock);
2213 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
2214 rw_wunlock(&pvh_global_lock);
2219 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
2220 vm_prot_t prot, vm_page_t mpte)
2225 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2226 (m->oflags & VPO_UNMANAGED) != 0,
2227 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2228 rw_assert(&pvh_global_lock, RA_WLOCKED);
2229 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2232 * In the case that a page table page is not resident, we are
2235 if (va < VM_MAXUSER_ADDRESS) {
2240 * Calculate pagetable page index
2242 ptepindex = pmap_pde_pindex(va);
2243 if (mpte && (mpte->pindex == ptepindex)) {
2247 * Get the page directory entry
2249 pde = pmap_pde(pmap, va);
2252 * If the page table page is mapped, we just
2253 * increment the hold count, and activate it.
2255 if (pde && *pde != 0) {
2256 mpte = PHYS_TO_VM_PAGE(
2257 MIPS_DIRECT_TO_PHYS(*pde));
2260 mpte = _pmap_allocpte(pmap, ptepindex,
2261 PMAP_ENTER_NOSLEEP);
2270 pte = pmap_pte(pmap, va);
2271 if (pte_test(pte, PTE_V)) {
2280 * Enter on the PV list if part of our managed memory.
2282 if ((m->oflags & VPO_UNMANAGED) == 0 &&
2283 !pmap_try_insert_pv_entry(pmap, mpte, va, m)) {
2285 pmap_unwire_ptp(pmap, va, mpte);
2292 * Increment counters
2294 pmap->pm_stats.resident_count++;
2296 pa = VM_PAGE_TO_PHYS(m);
2299 * Now validate mapping with RO protection
2301 *pte = PTE_RO | TLBLO_PA_TO_PFN(pa) | PTE_V;
2302 if ((m->oflags & VPO_UNMANAGED) == 0)
2303 *pte |= PTE_MANAGED;
2305 if (is_cacheable_page(pa, m))
2306 *pte |= PTE_C_CACHE;
2308 *pte |= PTE_C_UNCACHED;
2310 if (is_kernel_pmap(pmap))
2314 * Sync I & D caches. Do this only if the target pmap
2315 * belongs to the current process. Otherwise, an
2316 * unresolvable TLB miss may occur. */
2317 if (pmap == &curproc->p_vmspace->vm_pmap) {
2319 mips_icache_sync_range(va, PAGE_SIZE);
2320 mips_dcache_wbinv_range(va, PAGE_SIZE);
2327 * Make a temporary mapping for a physical address. This is only intended
2328 * to be used for panic dumps.
2330 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2333 pmap_kenter_temporary(vm_paddr_t pa, int i)
2338 printf("%s: ERROR!!! More than one page of virtual address mapping not supported\n",
2341 if (MIPS_DIRECT_MAPPABLE(pa)) {
2342 va = MIPS_PHYS_TO_DIRECT(pa);
2344 #ifndef __mips_n64 /* XXX : to be converted to new style */
2347 struct local_sysmaps *sysm;
2348 pt_entry_t *pte, npte;
2350 /* If this is used other than for dumps, we may need to leave
2351 * interrupts disasbled on return. If crash dumps don't work when
2352 * we get to this point, we might want to consider this (leaving things
2353 * disabled as a starting point ;-)
2355 intr = intr_disable();
2356 cpu = PCPU_GET(cpuid);
2357 sysm = &sysmap_lmem[cpu];
2358 /* Since this is for the debugger, no locks or any other fun */
2359 npte = TLBLO_PA_TO_PFN(pa) | PTE_C_CACHE | PTE_D | PTE_V |
2361 pte = pmap_pte(kernel_pmap, sysm->base);
2364 pmap_update_page(kernel_pmap, sysm->base, npte);
2369 return ((void *)va);
2373 pmap_kenter_temporary_free(vm_paddr_t pa)
2375 #ifndef __mips_n64 /* XXX : to be converted to new style */
2378 struct local_sysmaps *sysm;
2381 if (MIPS_DIRECT_MAPPABLE(pa)) {
2382 /* nothing to do for this case */
2385 #ifndef __mips_n64 /* XXX : to be converted to new style */
2386 cpu = PCPU_GET(cpuid);
2387 sysm = &sysmap_lmem[cpu];
2391 intr = intr_disable();
2392 pte = pmap_pte(kernel_pmap, sysm->base);
2394 pmap_invalidate_page(kernel_pmap, sysm->base);
2402 * Maps a sequence of resident pages belonging to the same object.
2403 * The sequence begins with the given page m_start. This page is
2404 * mapped at the given virtual address start. Each subsequent page is
2405 * mapped at a virtual address that is offset from start by the same
2406 * amount as the page is offset from m_start within the object. The
2407 * last page in the sequence is the page with the largest offset from
2408 * m_start that can be mapped at a virtual address less than the given
2409 * virtual address end. Not every virtual page between start and end
2410 * is mapped; only those for which a resident page exists with the
2411 * corresponding offset from m_start are mapped.
2414 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2415 vm_page_t m_start, vm_prot_t prot)
2418 vm_pindex_t diff, psize;
2420 VM_OBJECT_ASSERT_LOCKED(m_start->object);
2422 psize = atop(end - start);
2425 rw_wlock(&pvh_global_lock);
2427 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2428 mpte = pmap_enter_quick_locked(pmap, start + ptoa(diff), m,
2430 m = TAILQ_NEXT(m, listq);
2432 rw_wunlock(&pvh_global_lock);
2437 * pmap_object_init_pt preloads the ptes for a given object
2438 * into the specified pmap. This eliminates the blast of soft
2439 * faults on process startup and immediately after an mmap.
2442 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2443 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2445 VM_OBJECT_ASSERT_WLOCKED(object);
2446 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2447 ("pmap_object_init_pt: non-device object"));
2451 * Clear the wired attribute from the mappings for the specified range of
2452 * addresses in the given pmap. Every valid mapping within that range
2453 * must have the wired attribute set. In contrast, invalid mappings
2454 * cannot have the wired attribute set, so they are ignored.
2456 * The wired attribute of the page table entry is not a hardware feature,
2457 * so there is no need to invalidate any TLB entries.
2460 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2462 pd_entry_t *pde, *pdpe;
2464 vm_offset_t va_next;
2467 for (; sva < eva; sva = va_next) {
2468 pdpe = pmap_segmap(pmap, sva);
2470 if (*pdpe == NULL) {
2471 va_next = (sva + NBSEG) & ~SEGMASK;
2477 va_next = (sva + NBPDR) & ~PDRMASK;
2480 pde = pmap_pdpe_to_pde(pdpe, sva);
2485 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2487 if (!pte_test(pte, PTE_V))
2489 if (!pte_test(pte, PTE_W))
2490 panic("pmap_unwire: pte %#jx is missing PG_W",
2492 pte_clear(pte, PTE_W);
2493 pmap->pm_stats.wired_count--;
2500 * Copy the range specified by src_addr/len
2501 * from the source map to the range dst_addr/len
2502 * in the destination map.
2504 * This routine is only advisory and need not do anything.
2508 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2509 vm_size_t len, vm_offset_t src_addr)
2514 * pmap_zero_page zeros the specified hardware page by mapping
2515 * the page into KVM and using bzero to clear its contents.
2517 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2520 pmap_zero_page(vm_page_t m)
2523 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2525 if (MIPS_DIRECT_MAPPABLE(phys)) {
2526 va = MIPS_PHYS_TO_DIRECT(phys);
2527 bzero((caddr_t)va, PAGE_SIZE);
2528 mips_dcache_wbinv_range(va, PAGE_SIZE);
2530 va = pmap_lmem_map1(phys);
2531 bzero((caddr_t)va, PAGE_SIZE);
2532 mips_dcache_wbinv_range(va, PAGE_SIZE);
2538 * pmap_zero_page_area zeros the specified hardware page by mapping
2539 * the page into KVM and using bzero to clear its contents.
2541 * off and size may not cover an area beyond a single hardware page.
2544 pmap_zero_page_area(vm_page_t m, int off, int size)
2547 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2549 if (MIPS_DIRECT_MAPPABLE(phys)) {
2550 va = MIPS_PHYS_TO_DIRECT(phys);
2551 bzero((char *)(caddr_t)va + off, size);
2552 mips_dcache_wbinv_range(va + off, size);
2554 va = pmap_lmem_map1(phys);
2555 bzero((char *)va + off, size);
2556 mips_dcache_wbinv_range(va + off, size);
2562 pmap_zero_page_idle(vm_page_t m)
2565 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2567 if (MIPS_DIRECT_MAPPABLE(phys)) {
2568 va = MIPS_PHYS_TO_DIRECT(phys);
2569 bzero((caddr_t)va, PAGE_SIZE);
2570 mips_dcache_wbinv_range(va, PAGE_SIZE);
2572 va = pmap_lmem_map1(phys);
2573 bzero((caddr_t)va, PAGE_SIZE);
2574 mips_dcache_wbinv_range(va, PAGE_SIZE);
2580 * pmap_copy_page copies the specified (machine independent)
2581 * page by mapping the page into virtual memory and using
2582 * bcopy to copy the page, one machine dependent page at a
2585 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2588 pmap_copy_page(vm_page_t src, vm_page_t dst)
2590 vm_offset_t va_src, va_dst;
2591 vm_paddr_t phys_src = VM_PAGE_TO_PHYS(src);
2592 vm_paddr_t phys_dst = VM_PAGE_TO_PHYS(dst);
2594 if (MIPS_DIRECT_MAPPABLE(phys_src) && MIPS_DIRECT_MAPPABLE(phys_dst)) {
2595 /* easy case, all can be accessed via KSEG0 */
2597 * Flush all caches for VA that are mapped to this page
2598 * to make sure that data in SDRAM is up to date
2600 pmap_flush_pvcache(src);
2601 mips_dcache_wbinv_range_index(
2602 MIPS_PHYS_TO_DIRECT(phys_dst), PAGE_SIZE);
2603 va_src = MIPS_PHYS_TO_DIRECT(phys_src);
2604 va_dst = MIPS_PHYS_TO_DIRECT(phys_dst);
2605 bcopy((caddr_t)va_src, (caddr_t)va_dst, PAGE_SIZE);
2606 mips_dcache_wbinv_range(va_dst, PAGE_SIZE);
2608 va_src = pmap_lmem_map2(phys_src, phys_dst);
2609 va_dst = va_src + PAGE_SIZE;
2610 bcopy((void *)va_src, (void *)va_dst, PAGE_SIZE);
2611 mips_dcache_wbinv_range(va_dst, PAGE_SIZE);
2616 int unmapped_buf_allowed;
2619 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
2620 vm_offset_t b_offset, int xfersize)
2624 vm_offset_t a_pg_offset, b_pg_offset;
2625 vm_paddr_t a_phys, b_phys;
2628 while (xfersize > 0) {
2629 a_pg_offset = a_offset & PAGE_MASK;
2630 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
2631 a_m = ma[a_offset >> PAGE_SHIFT];
2632 a_phys = VM_PAGE_TO_PHYS(a_m);
2633 b_pg_offset = b_offset & PAGE_MASK;
2634 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
2635 b_m = mb[b_offset >> PAGE_SHIFT];
2636 b_phys = VM_PAGE_TO_PHYS(b_m);
2637 if (MIPS_DIRECT_MAPPABLE(a_phys) &&
2638 MIPS_DIRECT_MAPPABLE(b_phys)) {
2639 pmap_flush_pvcache(a_m);
2640 mips_dcache_wbinv_range_index(
2641 MIPS_PHYS_TO_DIRECT(b_phys), PAGE_SIZE);
2642 a_cp = (char *)MIPS_PHYS_TO_DIRECT(a_phys) +
2644 b_cp = (char *)MIPS_PHYS_TO_DIRECT(b_phys) +
2646 bcopy(a_cp, b_cp, cnt);
2647 mips_dcache_wbinv_range((vm_offset_t)b_cp, cnt);
2649 a_cp = (char *)pmap_lmem_map2(a_phys, b_phys);
2650 b_cp = (char *)a_cp + PAGE_SIZE;
2651 a_cp += a_pg_offset;
2652 b_cp += b_pg_offset;
2653 bcopy(a_cp, b_cp, cnt);
2654 mips_dcache_wbinv_range((vm_offset_t)b_cp, cnt);
2664 pmap_quick_enter_page(vm_page_t m)
2666 #if defined(__mips_n64)
2667 return MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(m));
2670 struct local_sysmaps *sysm;
2673 pa = VM_PAGE_TO_PHYS(m);
2675 if (MIPS_DIRECT_MAPPABLE(pa)) {
2676 if (m->md.pv_flags & PV_MEMATTR_UNCACHEABLE)
2677 return (MIPS_PHYS_TO_DIRECT_UNCACHED(pa));
2679 return (MIPS_PHYS_TO_DIRECT(pa));
2682 sysm = &sysmap_lmem[PCPU_GET(cpuid)];
2684 KASSERT(sysm->valid1 == 0, ("pmap_quick_enter_page: PTE busy"));
2686 pte = pmap_pte(kernel_pmap, sysm->base);
2687 *pte = TLBLO_PA_TO_PFN(pa) | PTE_D | PTE_V | PTE_G |
2688 (is_cacheable_page(pa, m) ? PTE_C_CACHE : PTE_C_UNCACHED);
2691 return (sysm->base);
2696 pmap_quick_remove_page(vm_offset_t addr)
2698 mips_dcache_wbinv_range(addr, PAGE_SIZE);
2700 #if !defined(__mips_n64)
2701 struct local_sysmaps *sysm;
2704 if (addr >= MIPS_KSEG0_START && addr < MIPS_KSEG0_END)
2707 sysm = &sysmap_lmem[PCPU_GET(cpuid)];
2709 KASSERT(sysm->valid1 != 0,
2710 ("pmap_quick_remove_page: PTE not in use"));
2711 KASSERT(sysm->base == addr,
2712 ("pmap_quick_remove_page: invalid address"));
2714 pte = pmap_pte(kernel_pmap, addr);
2716 tlb_invalidate_address(kernel_pmap, addr);
2723 * Returns true if the pmap's pv is one of the first
2724 * 16 pvs linked to from this page. This count may
2725 * be changed upwards or downwards in the future; it
2726 * is only necessary that true be returned for a small
2727 * subset of pmaps for proper page aging.
2730 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2736 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2737 ("pmap_page_exists_quick: page %p is not managed", m));
2739 rw_wlock(&pvh_global_lock);
2740 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2741 if (PV_PMAP(pv) == pmap) {
2749 rw_wunlock(&pvh_global_lock);
2754 * Remove all pages from specified address space
2755 * this aids process exit speeds. Also, this code
2756 * is special cased for current process only, but
2757 * can have the more generic (and slightly slower)
2758 * mode enabled. This is much faster than pmap_remove
2759 * in the case of running down an entire address space.
2762 pmap_remove_pages(pmap_t pmap)
2765 pt_entry_t *pte, tpte;
2768 struct pv_chunk *pc, *npc;
2769 u_long inuse, bitmask;
2770 int allfree, bit, field, idx;
2772 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
2773 printf("warning: pmap_remove_pages called with non-current pmap\n");
2776 rw_wlock(&pvh_global_lock);
2778 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
2780 for (field = 0; field < _NPCM; field++) {
2781 inuse = ~pc->pc_map[field] & pc_freemask[field];
2782 while (inuse != 0) {
2783 bit = ffsl(inuse) - 1;
2784 bitmask = 1UL << bit;
2785 idx = field * sizeof(inuse) * NBBY + bit;
2786 pv = &pc->pc_pventry[idx];
2789 pde = pmap_pde(pmap, pv->pv_va);
2790 KASSERT(pde != NULL && *pde != 0,
2791 ("pmap_remove_pages: pde"));
2792 pte = pmap_pde_to_pte(pde, pv->pv_va);
2793 if (!pte_test(pte, PTE_V))
2794 panic("pmap_remove_pages: bad pte");
2798 * We cannot remove wired pages from a process' mapping at this time
2800 if (pte_test(&tpte, PTE_W)) {
2804 *pte = is_kernel_pmap(pmap) ? PTE_G : 0;
2806 m = PHYS_TO_VM_PAGE(TLBLO_PTE_TO_PA(tpte));
2808 ("pmap_remove_pages: bad tpte %#jx",
2812 * Update the vm_page_t clean and reference bits.
2814 if (pte_test(&tpte, PTE_D))
2818 PV_STAT(pv_entry_frees++);
2819 PV_STAT(pv_entry_spare++);
2821 pc->pc_map[field] |= bitmask;
2822 pmap->pm_stats.resident_count--;
2823 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2824 if (TAILQ_EMPTY(&m->md.pv_list))
2825 vm_page_aflag_clear(m, PGA_WRITEABLE);
2826 pmap_unuse_pt(pmap, pv->pv_va, *pde);
2830 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2834 pmap_invalidate_all(pmap);
2836 rw_wunlock(&pvh_global_lock);
2840 * pmap_testbit tests bits in pte's
2843 pmap_testbit(vm_page_t m, int bit)
2848 boolean_t rv = FALSE;
2850 if (m->oflags & VPO_UNMANAGED)
2853 rw_assert(&pvh_global_lock, RA_WLOCKED);
2854 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2857 pte = pmap_pte(pmap, pv->pv_va);
2858 rv = pte_test(pte, bit);
2867 * pmap_page_wired_mappings:
2869 * Return the number of managed mappings to the given physical page
2873 pmap_page_wired_mappings(vm_page_t m)
2881 if ((m->oflags & VPO_UNMANAGED) != 0)
2883 rw_wlock(&pvh_global_lock);
2884 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2887 pte = pmap_pte(pmap, pv->pv_va);
2888 if (pte_test(pte, PTE_W))
2892 rw_wunlock(&pvh_global_lock);
2897 * Clear the write and modified bits in each of the given page's mappings.
2900 pmap_remove_write(vm_page_t m)
2903 pt_entry_t pbits, *pte;
2906 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2907 ("pmap_remove_write: page %p is not managed", m));
2910 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2911 * set by another thread while the object is locked. Thus,
2912 * if PGA_WRITEABLE is clear, no page table entries need updating.
2914 VM_OBJECT_ASSERT_WLOCKED(m->object);
2915 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2917 rw_wlock(&pvh_global_lock);
2918 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2921 pte = pmap_pte(pmap, pv->pv_va);
2922 KASSERT(pte != NULL && pte_test(pte, PTE_V),
2923 ("page on pv_list has no pte"));
2925 if (pte_test(&pbits, PTE_D)) {
2926 pte_clear(&pbits, PTE_D);
2929 pte_set(&pbits, PTE_RO);
2930 if (pbits != *pte) {
2932 pmap_update_page(pmap, pv->pv_va, pbits);
2936 vm_page_aflag_clear(m, PGA_WRITEABLE);
2937 rw_wunlock(&pvh_global_lock);
2941 * pmap_ts_referenced:
2943 * Return the count of reference bits for a page, clearing all of them.
2946 pmap_ts_referenced(vm_page_t m)
2949 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2950 ("pmap_ts_referenced: page %p is not managed", m));
2951 if (m->md.pv_flags & PV_TABLE_REF) {
2952 rw_wlock(&pvh_global_lock);
2953 m->md.pv_flags &= ~PV_TABLE_REF;
2954 rw_wunlock(&pvh_global_lock);
2963 * Return whether or not the specified physical page was modified
2964 * in any physical maps.
2967 pmap_is_modified(vm_page_t m)
2971 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2972 ("pmap_is_modified: page %p is not managed", m));
2975 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2976 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
2977 * is clear, no PTEs can have PTE_D set.
2979 VM_OBJECT_ASSERT_WLOCKED(m->object);
2980 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2982 rw_wlock(&pvh_global_lock);
2983 rv = pmap_testbit(m, PTE_D);
2984 rw_wunlock(&pvh_global_lock);
2991 * pmap_is_prefaultable:
2993 * Return whether or not the specified virtual address is elgible
2997 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3005 pde = pmap_pde(pmap, addr);
3006 if (pde != NULL && *pde != 0) {
3007 pte = pmap_pde_to_pte(pde, addr);
3015 * Apply the given advice to the specified range of addresses within the
3016 * given pmap. Depending on the advice, clear the referenced and/or
3017 * modified flags in each mapping and set the mapped page's dirty field.
3020 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
3022 pd_entry_t *pde, *pdpe;
3024 vm_offset_t va, va_next;
3028 if (advice != MADV_DONTNEED && advice != MADV_FREE)
3030 rw_wlock(&pvh_global_lock);
3032 for (; sva < eva; sva = va_next) {
3033 pdpe = pmap_segmap(pmap, sva);
3036 va_next = (sva + NBSEG) & ~SEGMASK;
3042 va_next = (sva + NBPDR) & ~PDRMASK;
3046 pde = pmap_pdpe_to_pde(pdpe, sva);
3051 * Limit our scan to either the end of the va represented
3052 * by the current page table page, or to the end of the
3053 * range being write protected.
3059 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
3061 if (!pte_test(pte, PTE_MANAGED | PTE_V)) {
3062 if (va != va_next) {
3063 pmap_invalidate_range(pmap, va, sva);
3068 pa = TLBLO_PTE_TO_PA(*pte);
3069 m = PHYS_TO_VM_PAGE(pa);
3070 m->md.pv_flags &= ~PV_TABLE_REF;
3071 if (pte_test(pte, PTE_D)) {
3072 if (advice == MADV_DONTNEED) {
3074 * Future calls to pmap_is_modified()
3075 * can be avoided by making the page
3080 pte_clear(pte, PTE_D);
3086 * Unless PTE_D is set, any TLB entries
3087 * mapping "sva" don't allow write access, so
3088 * they needn't be invalidated.
3090 if (va != va_next) {
3091 pmap_invalidate_range(pmap, va, sva);
3097 pmap_invalidate_range(pmap, va, sva);
3099 rw_wunlock(&pvh_global_lock);
3104 * Clear the modify bits on the specified physical page.
3107 pmap_clear_modify(vm_page_t m)
3113 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3114 ("pmap_clear_modify: page %p is not managed", m));
3115 VM_OBJECT_ASSERT_WLOCKED(m->object);
3116 KASSERT(!vm_page_xbusied(m),
3117 ("pmap_clear_modify: page %p is exclusive busied", m));
3120 * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_D set.
3121 * If the object containing the page is locked and the page is not
3122 * write busied, then PGA_WRITEABLE cannot be concurrently set.
3124 if ((m->aflags & PGA_WRITEABLE) == 0)
3126 rw_wlock(&pvh_global_lock);
3127 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3130 pte = pmap_pte(pmap, pv->pv_va);
3131 if (pte_test(pte, PTE_D)) {
3132 pte_clear(pte, PTE_D);
3133 pmap_update_page(pmap, pv->pv_va, *pte);
3137 rw_wunlock(&pvh_global_lock);
3141 * pmap_is_referenced:
3143 * Return whether or not the specified physical page was referenced
3144 * in any physical maps.
3147 pmap_is_referenced(vm_page_t m)
3150 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3151 ("pmap_is_referenced: page %p is not managed", m));
3152 return ((m->md.pv_flags & PV_TABLE_REF) != 0);
3156 * Miscellaneous support routines follow
3160 * Map a set of physical memory pages into the kernel virtual
3161 * address space. Return a pointer to where it is mapped. This
3162 * routine is intended to be used for mapping device memory,
3165 * Use XKPHYS uncached for 64 bit, and KSEG1 where possible for 32 bit.
3168 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3170 vm_offset_t va, tmpva, offset;
3173 * KSEG1 maps only first 512M of phys address space. For
3174 * pa > 0x20000000 we should make proper mapping * using pmap_kenter.
3176 if (MIPS_DIRECT_MAPPABLE(pa + size - 1))
3177 return ((void *)MIPS_PHYS_TO_DIRECT_UNCACHED(pa));
3179 offset = pa & PAGE_MASK;
3180 size = roundup(size + offset, PAGE_SIZE);
3182 va = kva_alloc(size);
3184 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3185 pa = trunc_page(pa);
3186 for (tmpva = va; size > 0;) {
3187 pmap_kenter_attr(tmpva, pa, PTE_C_UNCACHED);
3194 return ((void *)(va + offset));
3198 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3201 vm_offset_t base, offset;
3203 /* If the address is within KSEG1 then there is nothing to do */
3204 if (va >= MIPS_KSEG1_START && va <= MIPS_KSEG1_END)
3207 base = trunc_page(va);
3208 offset = va & PAGE_MASK;
3209 size = roundup(size + offset, PAGE_SIZE);
3210 kva_free(base, size);
3215 * perform the pmap work for mincore
3218 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
3220 pt_entry_t *ptep, pte;
3227 ptep = pmap_pte(pmap, addr);
3228 pte = (ptep != NULL) ? *ptep : 0;
3229 if (!pte_test(&pte, PTE_V)) {
3233 val = MINCORE_INCORE;
3234 if (pte_test(&pte, PTE_D))
3235 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
3236 pa = TLBLO_PTE_TO_PA(pte);
3237 if (pte_test(&pte, PTE_MANAGED)) {
3239 * This may falsely report the given address as
3240 * MINCORE_REFERENCED. Unfortunately, due to the lack of
3241 * per-PTE reference information, it is impossible to
3242 * determine if the address is MINCORE_REFERENCED.
3244 m = PHYS_TO_VM_PAGE(pa);
3245 if ((m->aflags & PGA_REFERENCED) != 0)
3246 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
3248 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
3249 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
3250 pte_test(&pte, PTE_MANAGED)) {
3251 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
3252 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
3256 PA_UNLOCK_COND(*locked_pa);
3262 pmap_activate(struct thread *td)
3264 pmap_t pmap, oldpmap;
3265 struct proc *p = td->td_proc;
3270 pmap = vmspace_pmap(p->p_vmspace);
3271 oldpmap = PCPU_GET(curpmap);
3272 cpuid = PCPU_GET(cpuid);
3275 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
3276 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
3277 pmap_asid_alloc(pmap);
3278 if (td == curthread) {
3279 PCPU_SET(segbase, pmap->pm_segtab);
3280 mips_wr_entryhi(pmap->pm_asid[cpuid].asid);
3283 PCPU_SET(curpmap, pmap);
3288 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
3293 * Increase the starting virtual address of the given mapping if a
3294 * different alignment might result in more superpage mappings.
3297 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
3298 vm_offset_t *addr, vm_size_t size)
3300 vm_offset_t superpage_offset;
3304 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
3305 offset += ptoa(object->pg_color);
3306 superpage_offset = offset & SEGMASK;
3307 if (size - ((NBSEG - superpage_offset) & SEGMASK) < NBSEG ||
3308 (*addr & SEGMASK) == superpage_offset)
3310 if ((*addr & SEGMASK) < superpage_offset)
3311 *addr = (*addr & ~SEGMASK) + superpage_offset;
3313 *addr = ((*addr + SEGMASK) & ~SEGMASK) + superpage_offset;
3317 DB_SHOW_COMMAND(ptable, ddb_pid_dump)
3320 struct thread *td = NULL;
3327 td = db_lookup_thread(addr, true);
3329 db_printf("Invalid pid or tid");
3333 if (p->p_vmspace == NULL) {
3334 db_printf("No vmspace for process");
3337 pmap = vmspace_pmap(p->p_vmspace);
3341 db_printf("pmap:%p segtab:%p asid:%x generation:%x\n",
3342 pmap, pmap->pm_segtab, pmap->pm_asid[0].asid,
3343 pmap->pm_asid[0].gen);
3344 for (i = 0; i < NPDEPG; i++) {
3349 pdpe = (pd_entry_t *)pmap->pm_segtab[i];
3352 db_printf("[%4d] %p\n", i, pdpe);
3354 for (j = 0; j < NPDEPG; j++) {
3355 pde = (pt_entry_t *)pdpe[j];
3358 db_printf("\t[%4d] %p\n", j, pde);
3362 pde = (pt_entry_t *)pdpe;
3364 for (k = 0; k < NPTEPG; k++) {
3366 if (pte == 0 || !pte_test(&pte, PTE_V))
3368 pa = TLBLO_PTE_TO_PA(pte);
3369 va = ((u_long)i << SEGSHIFT) | (j << PDRSHIFT) | (k << PAGE_SHIFT);
3370 db_printf("\t\t[%04d] va: %p pte: %8jx pa:%jx\n",
3371 k, (void *)va, (uintmax_t)pte, (uintmax_t)pa);
3379 * Allocate TLB address space tag (called ASID or TLBPID) and return it.
3380 * It takes almost as much or more time to search the TLB for a
3381 * specific ASID and flush those entries as it does to flush the entire TLB.
3382 * Therefore, when we allocate a new ASID, we just take the next number. When
3383 * we run out of numbers, we flush the TLB, increment the generation count
3384 * and start over. ASID zero is reserved for kernel use.
3387 pmap_asid_alloc(pmap)
3390 if (pmap->pm_asid[PCPU_GET(cpuid)].asid != PMAP_ASID_RESERVED &&
3391 pmap->pm_asid[PCPU_GET(cpuid)].gen == PCPU_GET(asid_generation));
3393 if (PCPU_GET(next_asid) == pmap_max_asid) {
3394 tlb_invalidate_all_user(NULL);
3395 PCPU_SET(asid_generation,
3396 (PCPU_GET(asid_generation) + 1) & ASIDGEN_MASK);
3397 if (PCPU_GET(asid_generation) == 0) {
3398 PCPU_SET(asid_generation, 1);
3400 PCPU_SET(next_asid, 1); /* 0 means invalid */
3402 pmap->pm_asid[PCPU_GET(cpuid)].asid = PCPU_GET(next_asid);
3403 pmap->pm_asid[PCPU_GET(cpuid)].gen = PCPU_GET(asid_generation);
3404 PCPU_SET(next_asid, PCPU_GET(next_asid) + 1);
3409 init_pte_prot(vm_page_t m, vm_prot_t access, vm_prot_t prot)
3413 if (!(prot & VM_PROT_WRITE))
3414 rw = PTE_V | PTE_RO;
3415 else if ((m->oflags & VPO_UNMANAGED) == 0) {
3416 if ((access & VM_PROT_WRITE) != 0)
3421 /* Needn't emulate a modified bit for unmanaged pages. */
3427 * pmap_emulate_modified : do dirty bit emulation
3429 * On SMP, update just the local TLB, other CPUs will update their
3430 * TLBs from PTE lazily, if they get the exception.
3431 * Returns 0 in case of sucess, 1 if the page is read only and we
3435 pmap_emulate_modified(pmap_t pmap, vm_offset_t va)
3440 pte = pmap_pte(pmap, va);
3442 panic("pmap_emulate_modified: can't find PTE");
3444 /* It is possible that some other CPU changed m-bit */
3445 if (!pte_test(pte, PTE_V) || pte_test(pte, PTE_D)) {
3446 tlb_update(pmap, va, *pte);
3451 if (!pte_test(pte, PTE_V) || pte_test(pte, PTE_D))
3452 panic("pmap_emulate_modified: invalid pte");
3454 if (pte_test(pte, PTE_RO)) {
3458 pte_set(pte, PTE_D);
3459 tlb_update(pmap, va, *pte);
3460 if (!pte_test(pte, PTE_MANAGED))
3461 panic("pmap_emulate_modified: unmanaged page");
3467 * Routine: pmap_kextract
3469 * Extract the physical page address associated
3473 pmap_kextract(vm_offset_t va)
3478 * First, the direct-mapped regions.
3480 #if defined(__mips_n64)
3481 if (va >= MIPS_XKPHYS_START && va < MIPS_XKPHYS_END)
3482 return (MIPS_XKPHYS_TO_PHYS(va));
3484 if (va >= MIPS_KSEG0_START && va < MIPS_KSEG0_END)
3485 return (MIPS_KSEG0_TO_PHYS(va));
3487 if (va >= MIPS_KSEG1_START && va < MIPS_KSEG1_END)
3488 return (MIPS_KSEG1_TO_PHYS(va));
3491 * User virtual addresses.
3493 if (va < VM_MAXUSER_ADDRESS) {
3496 if (curproc && curproc->p_vmspace) {
3497 ptep = pmap_pte(&curproc->p_vmspace->vm_pmap, va);
3499 return (TLBLO_PTE_TO_PA(*ptep) |
3507 * Should be kernel virtual here, otherwise fail
3509 mapped = (va >= MIPS_KSEG2_START || va < MIPS_KSEG2_END);
3510 #if defined(__mips_n64)
3511 mapped = mapped || (va >= MIPS_XKSEG_START || va < MIPS_XKSEG_END);
3520 /* Is the kernel pmap initialized? */
3521 if (!CPU_EMPTY(&kernel_pmap->pm_active)) {
3522 /* It's inside the virtual address range */
3523 ptep = pmap_pte(kernel_pmap, va);
3525 return (TLBLO_PTE_TO_PA(*ptep) |
3532 panic("%s for unknown address space %p.", __func__, (void *)va);
3537 pmap_flush_pvcache(vm_page_t m)
3542 for (pv = TAILQ_FIRST(&m->md.pv_list); pv;
3543 pv = TAILQ_NEXT(pv, pv_list)) {
3544 mips_dcache_wbinv_range_index(pv->pv_va, PAGE_SIZE);
3550 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
3554 * It appears that this function can only be called before any mappings
3555 * for the page are established. If this ever changes, this code will
3556 * need to walk the pv_list and make each of the existing mappings
3557 * uncacheable, being careful to sync caches and PTEs (and maybe
3558 * invalidate TLB?) for any current mapping it modifies.
3560 if (TAILQ_FIRST(&m->md.pv_list) != NULL)
3561 panic("Can't change memattr on page with existing mappings");
3564 * The only memattr we support is UNCACHEABLE, translate the (semi-)MI
3565 * representation of that into our internal flag in the page MD struct.
3567 if (ma == VM_MEMATTR_UNCACHEABLE)
3568 m->md.pv_flags |= PV_MEMATTR_UNCACHEABLE;
3570 m->md.pv_flags &= ~PV_MEMATTR_UNCACHEABLE;