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 * In addition to hardware address maps, this
46 * module is called upon to provide software-use-only
47 * maps which may or may not be stored in the same
48 * form as hardware maps. These pseudo-maps are
49 * used to store intermediate results from copy
50 * operations to and from address spaces.
52 * Since the information managed by this module is
53 * also stored by the logical address mapping module,
54 * this module may throw away valid virtual-to-physical
55 * mappings at almost any time. However, invalidations
56 * of virtual-to-physical mappings must be done as
59 * In order to cope with hardware architectures which
60 * make virtual-to-physical map invalidates expensive,
61 * this module may delay invalidate or reduced protection
62 * operations until such time as they are actually
63 * necessary. This module is given full information as
64 * to which processors are currently using which maps,
65 * and to when physical maps must be made correct.
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
73 #include <sys/param.h>
74 #include <sys/systm.h>
76 #include <sys/msgbuf.h>
77 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_phys.h>
88 #include <sys/mutex.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 <sys/sched.h>
103 #include <machine/cache.h>
104 #include <machine/md_var.h>
105 #include <machine/tlb.h>
109 #ifndef PMAP_SHPGPERPROC
110 #define PMAP_SHPGPERPROC 200
113 #if !defined(DIAGNOSTIC)
114 #define PMAP_INLINE __inline
120 * Get PDEs and PTEs for user/kernel address space
122 #define pmap_seg_index(v) (((v) >> SEGSHIFT) & (NPDEPG - 1))
123 #define pmap_pde_index(v) (((v) >> PDRSHIFT) & (NPDEPG - 1))
124 #define pmap_pte_index(v) (((v) >> PAGE_SHIFT) & (NPTEPG - 1))
125 #define pmap_pde_pindex(v) ((v) >> PDRSHIFT)
128 #define NUPDE (NPDEPG * NPDEPG)
129 #define NUSERPGTBLS (NUPDE + NPDEPG)
131 #define NUPDE (NPDEPG)
132 #define NUSERPGTBLS (NUPDE)
135 #define is_kernel_pmap(x) ((x) == kernel_pmap)
137 struct pmap kernel_pmap_store;
138 pd_entry_t *kernel_segmap;
140 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
141 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
144 unsigned pmap_max_asid; /* max ASID supported by the system */
146 #define PMAP_ASID_RESERVED 0
148 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
150 static void pmap_asid_alloc(pmap_t pmap);
153 * Data for the pv entry allocation mechanism
155 static uma_zone_t pvzone;
156 static struct vm_object pvzone_obj;
157 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
159 static PMAP_INLINE void free_pv_entry(pv_entry_t pv);
160 static pv_entry_t get_pv_entry(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 __inline void pmap_changebit(vm_page_t m, int bit, boolean_t setem);
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 int pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va);
168 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
169 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va);
170 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte,
171 vm_offset_t va, vm_page_t m);
172 static void pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte);
173 static void pmap_invalidate_all(pmap_t pmap);
174 static void pmap_invalidate_page(pmap_t pmap, vm_offset_t va);
175 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m);
177 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
178 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
179 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t);
180 static pt_entry_t init_pte_prot(vm_offset_t va, vm_page_t m, vm_prot_t prot);
183 static void pmap_invalidate_page_action(void *arg);
184 static void pmap_invalidate_all_action(void *arg);
185 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) |
234 PTE_D | PTE_V | PTE_G | PTE_W | PTE_C_CACHE;
235 pte = pmap_pte(kernel_pmap, va);
241 static __inline vm_offset_t
242 pmap_lmem_map2(vm_paddr_t phys1, vm_paddr_t phys2)
244 struct local_sysmaps *sysm;
245 pt_entry_t *pte, npte;
246 vm_offset_t va1, va2;
250 intr = intr_disable();
251 cpu = PCPU_GET(cpuid);
252 sysm = &sysmap_lmem[cpu];
253 sysm->saved_intr = intr;
255 va2 = sysm->base + PAGE_SIZE;
256 npte = TLBLO_PA_TO_PFN(phys1) |
257 PTE_D | PTE_V | PTE_G | PTE_W | PTE_C_CACHE;
258 pte = pmap_pte(kernel_pmap, va1);
260 npte = TLBLO_PA_TO_PFN(phys2) |
261 PTE_D | PTE_V | PTE_G | PTE_W | PTE_C_CACHE;
262 pte = pmap_pte(kernel_pmap, va2);
270 pmap_lmem_unmap(void)
272 struct local_sysmaps *sysm;
276 cpu = PCPU_GET(cpuid);
277 sysm = &sysmap_lmem[cpu];
278 pte = pmap_pte(kernel_pmap, sysm->base);
280 tlb_invalidate_address(kernel_pmap, sysm->base);
283 pte = pmap_pte(kernel_pmap, sysm->base + PAGE_SIZE);
285 tlb_invalidate_address(kernel_pmap, sysm->base + PAGE_SIZE);
288 intr_restore(sysm->saved_intr);
290 #else /* __mips_n64 */
293 pmap_alloc_lmem_map(void)
297 static __inline vm_offset_t
298 pmap_lmem_map1(vm_paddr_t phys)
304 static __inline vm_offset_t
305 pmap_lmem_map2(vm_paddr_t phys1, vm_paddr_t phys2)
311 static __inline vm_offset_t
312 pmap_lmem_unmap(void)
317 #endif /* !__mips_n64 */
320 * Page table entry lookup routines.
322 static __inline pd_entry_t *
323 pmap_segmap(pmap_t pmap, vm_offset_t va)
326 return (&pmap->pm_segtab[pmap_seg_index(va)]);
330 static __inline pd_entry_t *
331 pmap_pdpe_to_pde(pd_entry_t *pdpe, vm_offset_t va)
335 pde = (pd_entry_t *)*pdpe;
336 return (&pde[pmap_pde_index(va)]);
339 static __inline pd_entry_t *
340 pmap_pde(pmap_t pmap, vm_offset_t va)
344 pdpe = pmap_segmap(pmap, va);
345 if (pdpe == NULL || *pdpe == NULL)
348 return (pmap_pdpe_to_pde(pdpe, va));
351 static __inline pd_entry_t *
352 pmap_pdpe_to_pde(pd_entry_t *pdpe, vm_offset_t va)
359 pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va)
362 return (pmap_segmap(pmap, va));
366 static __inline pt_entry_t *
367 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
371 pte = (pt_entry_t *)*pde;
372 return (&pte[pmap_pte_index(va)]);
376 pmap_pte(pmap_t pmap, vm_offset_t va)
380 pde = pmap_pde(pmap, va);
381 if (pde == NULL || *pde == NULL)
384 return (pmap_pde_to_pte(pde, va));
388 pmap_steal_memory(vm_size_t size)
390 vm_paddr_t bank_size, pa;
393 size = round_page(size);
394 bank_size = phys_avail[1] - phys_avail[0];
395 while (size > bank_size) {
398 for (i = 0; phys_avail[i + 2]; i += 2) {
399 phys_avail[i] = phys_avail[i + 2];
400 phys_avail[i + 1] = phys_avail[i + 3];
403 phys_avail[i + 1] = 0;
405 panic("pmap_steal_memory: out of memory");
406 bank_size = phys_avail[1] - phys_avail[0];
410 phys_avail[0] += size;
411 if (MIPS_DIRECT_MAPPABLE(pa) == 0)
412 panic("Out of memory below 512Meg?");
413 va = MIPS_PHYS_TO_DIRECT(pa);
414 bzero((caddr_t)va, size);
419 * Bootstrap the system enough to run with virtual memory. This
420 * assumes that the phys_avail array has been initialized.
423 pmap_create_kernel_pagetable(void)
435 * Allocate segment table for the kernel
437 kernel_segmap = (pd_entry_t *)pmap_steal_memory(PAGE_SIZE);
440 * Allocate second level page tables for the kernel
443 npde = howmany(NKPT, NPDEPG);
444 pdaddr = pmap_steal_memory(PAGE_SIZE * npde);
447 ptaddr = pmap_steal_memory(PAGE_SIZE * nkpt);
450 * The R[4-7]?00 stores only one copy of the Global bit in the
451 * translation lookaside buffer for each 2 page entry. Thus invalid
452 * entrys must have the Global bit set so when Entry LO and Entry HI
453 * G bits are anded together they will produce a global bit to store
456 for (i = 0, pte = (pt_entry_t *)ptaddr; i < (nkpt * NPTEPG); i++, pte++)
460 for (i = 0, npt = nkpt; npt > 0; i++) {
461 kernel_segmap[i] = (pd_entry_t)(pdaddr + i * PAGE_SIZE);
462 pde = (pd_entry_t *)kernel_segmap[i];
464 for (j = 0; j < NPDEPG && npt > 0; j++, npt--)
465 pde[j] = (pd_entry_t)(ptaddr + (i * NPDEPG + j) * PAGE_SIZE);
468 for (i = 0, j = pmap_seg_index(VM_MIN_KERNEL_ADDRESS); i < nkpt; i++, j++)
469 kernel_segmap[j] = (pd_entry_t)(ptaddr + (i * PAGE_SIZE));
472 PMAP_LOCK_INIT(kernel_pmap);
473 kernel_pmap->pm_segtab = kernel_segmap;
474 CPU_FILL(&kernel_pmap->pm_active);
475 TAILQ_INIT(&kernel_pmap->pm_pvlist);
476 kernel_pmap->pm_asid[0].asid = PMAP_ASID_RESERVED;
477 kernel_pmap->pm_asid[0].gen = 0;
478 kernel_vm_end += nkpt * NPTEPG * PAGE_SIZE;
485 int need_local_mappings = 0;
489 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
491 * Keep the memory aligned on page boundary.
493 phys_avail[i] = round_page(phys_avail[i]);
494 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]);
498 if (phys_avail[i - 2] > phys_avail[i]) {
501 ptemp[0] = phys_avail[i + 0];
502 ptemp[1] = phys_avail[i + 1];
504 phys_avail[i + 0] = phys_avail[i - 2];
505 phys_avail[i + 1] = phys_avail[i - 1];
507 phys_avail[i - 2] = ptemp[0];
508 phys_avail[i - 1] = ptemp[1];
514 * In 32 bit, we may have memory which cannot be mapped directly.
515 * This memory will need temporary mapping before it can be
518 if (!MIPS_DIRECT_MAPPABLE(phys_avail[i - 1] - 1))
519 need_local_mappings = 1;
522 * Copy the phys_avail[] array before we start stealing memory from it.
524 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
525 physmem_desc[i] = phys_avail[i];
526 physmem_desc[i + 1] = phys_avail[i + 1];
529 Maxmem = atop(phys_avail[i - 1]);
532 printf("Physical memory chunk(s):\n");
533 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
536 size = phys_avail[i + 1] - phys_avail[i];
537 printf("%#08jx - %#08jx, %ju bytes (%ju pages)\n",
538 (uintmax_t) phys_avail[i],
539 (uintmax_t) phys_avail[i + 1] - 1,
540 (uintmax_t) size, (uintmax_t) size / PAGE_SIZE);
542 printf("Maxmem is 0x%0jx\n", ptoa((uintmax_t)Maxmem));
545 * Steal the message buffer from the beginning of memory.
547 msgbufp = (struct msgbuf *)pmap_steal_memory(msgbufsize);
548 msgbufinit(msgbufp, msgbufsize);
551 * Steal thread0 kstack.
553 kstack0 = pmap_steal_memory(KSTACK_PAGES << PAGE_SHIFT);
555 virtual_avail = VM_MIN_KERNEL_ADDRESS;
556 virtual_end = VM_MAX_KERNEL_ADDRESS;
560 * Steal some virtual address space to map the pcpu area.
562 virtual_avail = roundup2(virtual_avail, PAGE_SIZE * 2);
563 pcpup = (struct pcpu *)virtual_avail;
564 virtual_avail += PAGE_SIZE * 2;
567 * Initialize the wired TLB entry mapping the pcpu region for
568 * the BSP at 'pcpup'. Up until this point we were operating
569 * with the 'pcpup' for the BSP pointing to a virtual address
570 * in KSEG0 so there was no need for a TLB mapping.
572 mips_pcpu_tlb_init(PCPU_ADDR(0));
575 printf("pcpu is available at virtual address %p.\n", pcpup);
578 if (need_local_mappings)
579 pmap_alloc_lmem_map();
580 pmap_create_kernel_pagetable();
581 pmap_max_asid = VMNUM_PIDS;
587 * Initialize a vm_page's machine-dependent fields.
590 pmap_page_init(vm_page_t m)
593 TAILQ_INIT(&m->md.pv_list);
594 m->md.pv_list_count = 0;
599 * Initialize the pmap module.
600 * Called by vm_init, to initialize any structures that the pmap
601 * system needs to map virtual memory.
602 * pmap_init has been enhanced to support in a fairly consistant
603 * way, discontiguous physical memory.
610 * Initialize the address space (zone) for the pv entries. Set a
611 * high water mark so that the system can recover from excessive
612 * numbers of pv entries.
614 pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
615 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
616 pv_entry_max = PMAP_SHPGPERPROC * maxproc + cnt.v_page_count;
617 pv_entry_high_water = 9 * (pv_entry_max / 10);
618 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
621 /***************************************************
622 * Low level helper routines.....
623 ***************************************************/
626 pmap_invalidate_all_local(pmap_t pmap)
630 cpuid = PCPU_GET(cpuid);
632 if (pmap == kernel_pmap) {
633 tlb_invalidate_all();
636 if (CPU_ISSET(cpuid, &pmap->pm_active))
637 tlb_invalidate_all_user(pmap);
639 pmap->pm_asid[cpuid].gen = 0;
644 pmap_invalidate_all(pmap_t pmap)
647 smp_rendezvous(0, pmap_invalidate_all_action, 0, pmap);
651 pmap_invalidate_all_action(void *arg)
654 pmap_invalidate_all_local((pmap_t)arg);
658 pmap_invalidate_all(pmap_t pmap)
661 pmap_invalidate_all_local(pmap);
666 pmap_invalidate_page_local(pmap_t pmap, vm_offset_t va)
670 cpuid = PCPU_GET(cpuid);
672 if (is_kernel_pmap(pmap)) {
673 tlb_invalidate_address(pmap, va);
676 if (pmap->pm_asid[cpuid].gen != PCPU_GET(asid_generation))
678 else if (!CPU_ISSET(cpuid, &pmap->pm_active)) {
679 pmap->pm_asid[cpuid].gen = 0;
682 tlb_invalidate_address(pmap, va);
686 struct pmap_invalidate_page_arg {
692 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
694 struct pmap_invalidate_page_arg arg;
698 smp_rendezvous(0, pmap_invalidate_page_action, 0, &arg);
702 pmap_invalidate_page_action(void *arg)
704 struct pmap_invalidate_page_arg *p = arg;
706 pmap_invalidate_page_local(p->pmap, p->va);
710 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
713 pmap_invalidate_page_local(pmap, va);
718 pmap_update_page_local(pmap_t pmap, vm_offset_t va, pt_entry_t pte)
722 cpuid = PCPU_GET(cpuid);
724 if (is_kernel_pmap(pmap)) {
725 tlb_update(pmap, va, pte);
728 if (pmap->pm_asid[cpuid].gen != PCPU_GET(asid_generation))
730 else if (!CPU_ISSET(cpuid, &pmap->pm_active)) {
731 pmap->pm_asid[cpuid].gen = 0;
734 tlb_update(pmap, va, pte);
738 struct pmap_update_page_arg {
745 pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte)
747 struct pmap_update_page_arg arg;
752 smp_rendezvous(0, pmap_update_page_action, 0, &arg);
756 pmap_update_page_action(void *arg)
758 struct pmap_update_page_arg *p = arg;
760 pmap_update_page_local(p->pmap, p->va, p->pte);
764 pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte)
767 pmap_update_page_local(pmap, va, pte);
772 * Routine: pmap_extract
774 * Extract the physical page address associated
775 * with the given map/virtual_address pair.
778 pmap_extract(pmap_t pmap, vm_offset_t va)
781 vm_offset_t retval = 0;
784 pte = pmap_pte(pmap, va);
786 retval = TLBLO_PTE_TO_PA(*pte) | (va & PAGE_MASK);
793 * Routine: pmap_extract_and_hold
795 * Atomically extract and hold the physical page
796 * with the given pmap and virtual address pair
797 * if that mapping permits the given protection.
800 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
810 pte = *pmap_pte(pmap, va);
811 if (pte != 0 && pte_test(&pte, PTE_V) &&
812 (pte_test(&pte, PTE_D) || (prot & VM_PROT_WRITE) == 0)) {
813 if (vm_page_pa_tryrelock(pmap, TLBLO_PTE_TO_PA(pte), &pa))
816 m = PHYS_TO_VM_PAGE(TLBLO_PTE_TO_PA(pte));
824 /***************************************************
825 * Low level mapping routines.....
826 ***************************************************/
829 * add a wired page to the kva
832 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int attr)
835 pt_entry_t opte, npte;
838 printf("pmap_kenter: va: %p -> pa: %p\n", (void *)va, (void *)pa);
840 npte = TLBLO_PA_TO_PFN(pa) | PTE_D | PTE_V | PTE_G | PTE_W | attr;
842 pte = pmap_pte(kernel_pmap, va);
845 if (pte_test(&opte, PTE_V) && opte != npte)
846 pmap_update_page(kernel_pmap, va, npte);
850 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
853 KASSERT(is_cacheable_mem(pa),
854 ("pmap_kenter: memory at 0x%lx is not cacheable", (u_long)pa));
856 pmap_kenter_attr(va, pa, PTE_C_CACHE);
860 * remove a page from the kernel pagetables
862 /* PMAP_INLINE */ void
863 pmap_kremove(vm_offset_t va)
868 * Write back all caches from the page being destroyed
870 mips_dcache_wbinv_range_index(va, PAGE_SIZE);
872 pte = pmap_pte(kernel_pmap, va);
874 pmap_invalidate_page(kernel_pmap, va);
878 * Used to map a range of physical addresses into kernel
879 * virtual address space.
881 * The value passed in '*virt' is a suggested virtual address for
882 * the mapping. Architectures which can support a direct-mapped
883 * physical to virtual region can return the appropriate address
884 * within that region, leaving '*virt' unchanged. Other
885 * architectures should map the pages starting at '*virt' and
886 * update '*virt' with the first usable address after the mapped
889 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
892 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
896 if (MIPS_DIRECT_MAPPABLE(end - 1))
897 return (MIPS_PHYS_TO_DIRECT(start));
900 while (start < end) {
901 pmap_kenter(va, start);
910 * Add a list of wired pages to the kva
911 * this routine is only used for temporary
912 * kernel mappings that do not need to have
913 * page modification or references recorded.
914 * Note that old mappings are simply written
915 * over. The page *must* be wired.
918 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
921 vm_offset_t origva = va;
923 for (i = 0; i < count; i++) {
924 pmap_flush_pvcache(m[i]);
925 pmap_kenter(va, VM_PAGE_TO_PHYS(m[i]));
929 mips_dcache_wbinv_range_index(origva, PAGE_SIZE*count);
933 * this routine jerks page mappings from the
934 * kernel -- it is meant only for temporary mappings.
937 pmap_qremove(vm_offset_t va, int count)
940 * No need to wb/inv caches here,
941 * pmap_kremove will do it for us
944 while (count-- > 0) {
950 /***************************************************
951 * Page table page management routines.....
952 ***************************************************/
956 * Simplify the reference counting of page table pages. Specifically, use
957 * the page table page's wired count rather than its hold count to contain
958 * the reference count.
962 * This routine unholds page table pages, and if the hold count
963 * drops to zero, then it decrements the wire count.
965 static PMAP_INLINE int
966 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
969 if (m->wire_count == 0)
970 return (_pmap_unwire_pte_hold(pmap, va, m));
976 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
980 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
982 * unmap the page table page
985 if (m->pindex < NUPDE)
986 pde = pmap_pde(pmap, va);
988 pde = pmap_segmap(pmap, va);
990 pde = pmap_pde(pmap, va);
993 pmap->pm_stats.resident_count--;
996 if (m->pindex < NUPDE) {
1001 * Recursively decrement next level pagetable refcount
1003 pdp = (pd_entry_t *)*pmap_segmap(pmap, va);
1004 pdpg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(pdp));
1005 pmap_unwire_pte_hold(pmap, va, pdpg);
1008 if (pmap->pm_ptphint == m)
1009 pmap->pm_ptphint = NULL;
1012 * If the page is finally unwired, simply free it.
1014 vm_page_free_zero(m);
1015 atomic_subtract_int(&cnt.v_wire_count, 1);
1020 * After removing a page table entry, this routine is used to
1021 * conditionally free the page, and manage the hold/wire counts.
1024 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1029 if (va >= VM_MAXUSER_ADDRESS)
1033 ptepindex = pmap_pde_pindex(va);
1034 if (pmap->pm_ptphint &&
1035 (pmap->pm_ptphint->pindex == ptepindex)) {
1036 mpte = pmap->pm_ptphint;
1038 pteva = *pmap_pde(pmap, va);
1039 mpte = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(pteva));
1040 pmap->pm_ptphint = mpte;
1043 return (pmap_unwire_pte_hold(pmap, va, mpte));
1047 pmap_pinit0(pmap_t pmap)
1051 PMAP_LOCK_INIT(pmap);
1052 pmap->pm_segtab = kernel_segmap;
1053 CPU_ZERO(&pmap->pm_active);
1054 pmap->pm_ptphint = NULL;
1055 for (i = 0; i < MAXCPU; i++) {
1056 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
1057 pmap->pm_asid[i].gen = 0;
1059 PCPU_SET(curpmap, pmap);
1060 TAILQ_INIT(&pmap->pm_pvlist);
1061 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1065 pmap_grow_direct_page_cache()
1069 vm_contig_grow_cache(3, 0, MIPS_XKPHYS_LARGEST_PHYS);
1071 vm_contig_grow_cache(3, 0, MIPS_KSEG0_LARGEST_PHYS);
1076 pmap_alloc_direct_page(unsigned int index, int req)
1080 m = vm_page_alloc_freelist(VM_FREELIST_DIRECT, req);
1084 if ((m->flags & PG_ZERO) == 0)
1088 atomic_add_int(&cnt.v_wire_count, 1);
1094 * Initialize a preallocated and zeroed pmap structure,
1095 * such as one in a vmspace structure.
1098 pmap_pinit(pmap_t pmap)
1104 PMAP_LOCK_INIT(pmap);
1107 * allocate the page directory page
1109 while ((ptdpg = pmap_alloc_direct_page(NUSERPGTBLS, VM_ALLOC_NORMAL)) == NULL)
1110 pmap_grow_direct_page_cache();
1112 ptdva = MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(ptdpg));
1113 pmap->pm_segtab = (pd_entry_t *)ptdva;
1114 CPU_ZERO(&pmap->pm_active);
1115 pmap->pm_ptphint = NULL;
1116 for (i = 0; i < MAXCPU; i++) {
1117 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
1118 pmap->pm_asid[i].gen = 0;
1120 TAILQ_INIT(&pmap->pm_pvlist);
1121 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1127 * this routine is called if the page table page is not
1131 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags)
1136 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1137 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1138 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1141 * Find or fabricate a new pagetable page
1143 if ((m = pmap_alloc_direct_page(ptepindex, VM_ALLOC_NORMAL)) == NULL) {
1144 if (flags & M_WAITOK) {
1146 vm_page_unlock_queues();
1147 pmap_grow_direct_page_cache();
1148 vm_page_lock_queues();
1153 * Indicate the need to retry. While waiting, the page
1154 * table page may have been allocated.
1160 * Map the pagetable page into the process address space, if it
1161 * isn't already there.
1163 pageva = MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(m));
1166 if (ptepindex >= NUPDE) {
1167 pmap->pm_segtab[ptepindex - NUPDE] = (pd_entry_t)pageva;
1169 pd_entry_t *pdep, *pde;
1170 int segindex = ptepindex >> (SEGSHIFT - PDRSHIFT);
1171 int pdeindex = ptepindex & (NPDEPG - 1);
1174 pdep = &pmap->pm_segtab[segindex];
1175 if (*pdep == NULL) {
1176 /* recurse for allocating page dir */
1177 if (_pmap_allocpte(pmap, NUPDE + segindex,
1179 /* alloc failed, release current */
1181 atomic_subtract_int(&cnt.v_wire_count, 1);
1182 vm_page_free_zero(m);
1186 pg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(*pdep));
1189 /* Next level entry */
1190 pde = (pd_entry_t *)*pdep;
1191 pde[pdeindex] = (pd_entry_t)pageva;
1192 pmap->pm_ptphint = m;
1195 pmap->pm_segtab[ptepindex] = (pd_entry_t)pageva;
1197 pmap->pm_stats.resident_count++;
1200 * Set the page table hint
1202 pmap->pm_ptphint = m;
1207 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1213 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1214 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1215 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1218 * Calculate pagetable page index
1220 ptepindex = pmap_pde_pindex(va);
1223 * Get the page directory entry
1225 pde = pmap_pde(pmap, va);
1228 * If the page table page is mapped, we just increment the hold
1229 * count, and activate it.
1231 if (pde != NULL && *pde != NULL) {
1233 * In order to get the page table page, try the hint first.
1235 if (pmap->pm_ptphint &&
1236 (pmap->pm_ptphint->pindex == ptepindex)) {
1237 m = pmap->pm_ptphint;
1239 m = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(*pde));
1240 pmap->pm_ptphint = m;
1245 * Here if the pte page isn't mapped, or if it has been
1248 m = _pmap_allocpte(pmap, ptepindex, flags);
1249 if (m == NULL && (flags & M_WAITOK))
1256 /***************************************************
1257 * Pmap allocation/deallocation routines.
1258 ***************************************************/
1261 * - Merged pmap_release and pmap_release_free_page. When pmap_release is
1262 * called only the page directory page(s) can be left in the pmap pte
1263 * object, since all page table pages will have been freed by
1264 * pmap_remove_pages and pmap_remove. In addition, there can only be one
1265 * reference to the pmap and the page directory is wired, so the page(s)
1266 * can never be busy. So all there is to do is clear the magic mappings
1267 * from the page directory and free the page(s).
1272 * Release any resources held by the given physical map.
1273 * Called when a pmap initialized by pmap_pinit is being released.
1274 * Should only be called if the map contains no valid mappings.
1277 pmap_release(pmap_t pmap)
1282 KASSERT(pmap->pm_stats.resident_count == 0,
1283 ("pmap_release: pmap resident count %ld != 0",
1284 pmap->pm_stats.resident_count));
1286 ptdva = (vm_offset_t)pmap->pm_segtab;
1287 ptdpg = PHYS_TO_VM_PAGE(MIPS_DIRECT_TO_PHYS(ptdva));
1289 ptdpg->wire_count--;
1290 atomic_subtract_int(&cnt.v_wire_count, 1);
1291 vm_page_free_zero(ptdpg);
1292 PMAP_LOCK_DESTROY(pmap);
1296 * grow the number of kernel page table entries, if needed
1299 pmap_growkernel(vm_offset_t addr)
1302 pd_entry_t *pde, *pdpe;
1306 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1307 addr = roundup2(addr, NBSEG);
1308 if (addr - 1 >= kernel_map->max_offset)
1309 addr = kernel_map->max_offset;
1310 while (kernel_vm_end < addr) {
1311 pdpe = pmap_segmap(kernel_pmap, kernel_vm_end);
1314 /* new intermediate page table entry */
1315 nkpg = pmap_alloc_direct_page(nkpt, VM_ALLOC_INTERRUPT);
1317 panic("pmap_growkernel: no memory to grow kernel");
1318 *pdpe = (pd_entry_t)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(nkpg));
1319 continue; /* try again */
1322 pde = pmap_pdpe_to_pde(pdpe, kernel_vm_end);
1324 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1325 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1326 kernel_vm_end = kernel_map->max_offset;
1333 * This index is bogus, but out of the way
1335 nkpg = pmap_alloc_direct_page(nkpt, VM_ALLOC_INTERRUPT);
1337 panic("pmap_growkernel: no memory to grow kernel");
1339 *pde = (pd_entry_t)MIPS_PHYS_TO_DIRECT(VM_PAGE_TO_PHYS(nkpg));
1342 * The R[4-7]?00 stores only one copy of the Global bit in
1343 * the translation lookaside buffer for each 2 page entry.
1344 * Thus invalid entrys must have the Global bit set so when
1345 * Entry LO and Entry HI G bits are anded together they will
1346 * produce a global bit to store in the tlb.
1348 pte = (pt_entry_t *)*pde;
1349 for (i = 0; i < NPTEPG; i++)
1352 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1353 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1354 kernel_vm_end = kernel_map->max_offset;
1360 /***************************************************
1361 * page management routines.
1362 ***************************************************/
1365 * free the pv_entry back to the free list
1367 static PMAP_INLINE void
1368 free_pv_entry(pv_entry_t pv)
1372 uma_zfree(pvzone, pv);
1376 * get a new pv_entry, allocating a block from the system
1378 * the memory allocation is performed bypassing the malloc code
1379 * because of the possibility of allocations at interrupt time.
1382 get_pv_entry(pmap_t locked_pmap)
1384 static const struct timeval printinterval = { 60, 0 };
1385 static struct timeval lastprint;
1386 struct vpgqueues *vpq;
1387 pt_entry_t *pte, oldpte;
1389 pv_entry_t allocated_pv, next_pv, pv;
1393 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1394 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1395 allocated_pv = uma_zalloc(pvzone, M_NOWAIT);
1396 if (allocated_pv != NULL) {
1398 if (pv_entry_count > pv_entry_high_water)
1399 pagedaemon_wakeup();
1401 return (allocated_pv);
1404 * Reclaim pv entries: At first, destroy mappings to inactive
1405 * pages. After that, if a pv entry is still needed, destroy
1406 * mappings to active pages.
1408 if (ratecheck(&lastprint, &printinterval))
1409 printf("Approaching the limit on PV entries, "
1410 "increase the vm.pmap.shpgperproc tunable.\n");
1411 vpq = &vm_page_queues[PQ_INACTIVE];
1413 TAILQ_FOREACH(m, &vpq->pl, pageq) {
1414 if ((m->flags & PG_MARKER) != 0 || m->hold_count || m->busy)
1416 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
1419 /* Avoid deadlock and lock recursion. */
1420 if (pmap > locked_pmap)
1422 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
1424 pmap->pm_stats.resident_count--;
1425 pte = pmap_pte(pmap, va);
1426 KASSERT(pte != NULL, ("pte"));
1428 if (is_kernel_pmap(pmap))
1432 KASSERT(!pte_test(&oldpte, PTE_W),
1433 ("wired pte for unwired page"));
1434 if (m->md.pv_flags & PV_TABLE_REF)
1435 vm_page_aflag_set(m, PGA_REFERENCED);
1436 if (pte_test(&oldpte, PTE_D))
1438 pmap_invalidate_page(pmap, va);
1439 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1440 m->md.pv_list_count--;
1441 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1442 pmap_unuse_pt(pmap, va, pv->pv_ptem);
1443 if (pmap != locked_pmap)
1445 if (allocated_pv == NULL)
1450 if (TAILQ_EMPTY(&m->md.pv_list)) {
1451 vm_page_aflag_clear(m, PGA_WRITEABLE);
1452 m->md.pv_flags &= ~(PV_TABLE_REF | PV_TABLE_MOD);
1455 if (allocated_pv == NULL) {
1456 if (vpq == &vm_page_queues[PQ_INACTIVE]) {
1457 vpq = &vm_page_queues[PQ_ACTIVE];
1460 panic("get_pv_entry: increase the vm.pmap.shpgperproc tunable");
1462 return (allocated_pv);
1468 * Move pmap_collect() out of the machine-dependent code, rename it
1469 * to reflect its new location, and add page queue and flag locking.
1471 * Notes: (1) alpha, i386, and ia64 had identical implementations
1472 * of pmap_collect() in terms of machine-independent interfaces;
1473 * (2) sparc64 doesn't require it; (3) powerpc had it as a TODO.
1475 * MIPS implementation was identical to alpha [Junos 8.2]
1479 * If it is the first entry on the list, it is actually
1480 * in the header and we must copy the following entry up
1481 * to the header. Otherwise we must search the list for
1482 * the entry. In either case we free the now unused entry.
1486 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
1490 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1491 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1492 if (pvh->pv_list_count < pmap->pm_stats.resident_count) {
1493 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
1494 if (pmap == pv->pv_pmap && va == pv->pv_va)
1498 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1499 if (va == pv->pv_va)
1504 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
1505 pvh->pv_list_count--;
1506 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1512 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
1516 pv = pmap_pvh_remove(pvh, pmap, va);
1517 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found, pa %lx va %lx",
1518 (u_long)VM_PAGE_TO_PHYS(member2struct(vm_page, md, pvh)),
1524 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1527 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1528 pmap_pvh_free(&m->md, pmap, va);
1529 if (TAILQ_EMPTY(&m->md.pv_list))
1530 vm_page_aflag_clear(m, PGA_WRITEABLE);
1534 * Conditionally create a pv entry.
1537 pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte, vm_offset_t va,
1542 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1543 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1544 if (pv_entry_count < pv_entry_high_water &&
1545 (pv = uma_zalloc(pvzone, M_NOWAIT)) != NULL) {
1550 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1551 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1552 m->md.pv_list_count++;
1559 * pmap_remove_pte: do the things to unmap a page in a process
1562 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1568 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1569 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1572 if (is_kernel_pmap(pmap))
1577 if (pte_test(&oldpte, PTE_W))
1578 pmap->pm_stats.wired_count -= 1;
1580 pmap->pm_stats.resident_count -= 1;
1581 pa = TLBLO_PTE_TO_PA(oldpte);
1583 if (page_is_managed(pa)) {
1584 m = PHYS_TO_VM_PAGE(pa);
1585 if (pte_test(&oldpte, PTE_D)) {
1586 KASSERT(!pte_test(&oldpte, PTE_RO),
1587 ("%s: modified page not writable: va: %p, pte: %#jx",
1588 __func__, (void *)va, (uintmax_t)oldpte));
1591 if (m->md.pv_flags & PV_TABLE_REF)
1592 vm_page_aflag_set(m, PGA_REFERENCED);
1593 m->md.pv_flags &= ~(PV_TABLE_REF | PV_TABLE_MOD);
1595 pmap_remove_entry(pmap, m, va);
1597 return (pmap_unuse_pt(pmap, va, NULL));
1601 * Remove a single page from a process address space
1604 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1608 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1609 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1610 ptq = pmap_pte(pmap, va);
1613 * if there is no pte for this address, just skip it!!!
1615 if (!ptq || !pte_test(ptq, PTE_V)) {
1620 * Write back all caches from the page being destroyed
1622 mips_dcache_wbinv_range_index(va, PAGE_SIZE);
1625 * get a local va for mappings for this pmap.
1627 (void)pmap_remove_pte(pmap, ptq, va);
1628 pmap_invalidate_page(pmap, va);
1634 * Remove the given range of addresses from the specified map.
1636 * It is assumed that the start and end are properly
1637 * rounded to the page size.
1640 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1642 vm_offset_t va_next;
1643 pd_entry_t *pde, *pdpe;
1649 if (pmap->pm_stats.resident_count == 0)
1652 vm_page_lock_queues();
1656 * special handling of removing one page. a very common operation
1657 * and easy to short circuit some code.
1659 if ((sva + PAGE_SIZE) == eva) {
1660 pmap_remove_page(pmap, sva);
1663 for (; sva < eva; sva = va_next) {
1664 pdpe = pmap_segmap(pmap, sva);
1667 va_next = (sva + NBSEG) & ~SEGMASK;
1673 va_next = (sva + NBPDR) & ~PDRMASK;
1677 pde = pmap_pdpe_to_pde(pdpe, sva);
1682 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next;
1683 pte++, sva += PAGE_SIZE) {
1684 pmap_remove_page(pmap, sva);
1688 vm_page_unlock_queues();
1693 * Routine: pmap_remove_all
1695 * Removes this physical page from
1696 * all physical maps in which it resides.
1697 * Reflects back modify bits to the pager.
1700 * Original versions of this routine were very
1701 * inefficient because they iteratively called
1702 * pmap_remove (slow...)
1706 pmap_remove_all(vm_page_t m)
1709 pt_entry_t *pte, tpte;
1711 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1712 ("pmap_remove_all: page %p is not managed", m));
1713 vm_page_lock_queues();
1715 if (m->md.pv_flags & PV_TABLE_REF)
1716 vm_page_aflag_set(m, PGA_REFERENCED);
1718 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1719 PMAP_LOCK(pv->pv_pmap);
1722 * If it's last mapping writeback all caches from
1723 * the page being destroyed
1725 if (m->md.pv_list_count == 1)
1726 mips_dcache_wbinv_range_index(pv->pv_va, PAGE_SIZE);
1728 pv->pv_pmap->pm_stats.resident_count--;
1730 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1733 if (is_kernel_pmap(pv->pv_pmap))
1738 if (pte_test(&tpte, PTE_W))
1739 pv->pv_pmap->pm_stats.wired_count--;
1742 * Update the vm_page_t clean and reference bits.
1744 if (pte_test(&tpte, PTE_D)) {
1745 KASSERT(!pte_test(&tpte, PTE_RO),
1746 ("%s: modified page not writable: va: %p, pte: %#jx",
1747 __func__, (void *)pv->pv_va, (uintmax_t)tpte));
1750 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
1752 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1753 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1754 m->md.pv_list_count--;
1755 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1756 PMAP_UNLOCK(pv->pv_pmap);
1760 vm_page_aflag_clear(m, PGA_WRITEABLE);
1761 m->md.pv_flags &= ~(PV_TABLE_REF | PV_TABLE_MOD);
1762 vm_page_unlock_queues();
1766 * Set the physical protection on the
1767 * specified range of this map as requested.
1770 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1773 pd_entry_t *pde, *pdpe;
1774 vm_offset_t va_next;
1779 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1780 pmap_remove(pmap, sva, eva);
1783 if (prot & VM_PROT_WRITE)
1786 vm_page_lock_queues();
1788 for (; sva < eva; sva = va_next) {
1793 pdpe = pmap_segmap(pmap, sva);
1796 va_next = (sva + NBSEG) & ~SEGMASK;
1802 va_next = (sva + NBPDR) & ~PDRMASK;
1806 pde = pmap_pdpe_to_pde(pdpe, sva);
1807 if (pde == NULL || *pde == NULL)
1812 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1815 /* Skip invalid PTEs */
1816 if (!pte_test(pte, PTE_V))
1819 pa = TLBLO_PTE_TO_PA(pbits);
1820 if (page_is_managed(pa) && pte_test(&pbits, PTE_D)) {
1821 m = PHYS_TO_VM_PAGE(pa);
1823 m->md.pv_flags &= ~PV_TABLE_MOD;
1825 pte_clear(&pbits, PTE_D);
1826 pte_set(&pbits, PTE_RO);
1828 if (pbits != *pte) {
1830 pmap_update_page(pmap, sva, pbits);
1834 vm_page_unlock_queues();
1839 * Insert the given physical page (p) at
1840 * the specified virtual address (v) in the
1841 * target physical map with the protection requested.
1843 * If specified, the page will be wired down, meaning
1844 * that the related pte can not be reclaimed.
1846 * NB: This is the only routine which MAY NOT lazy-evaluate
1847 * or lose information. That is, this routine must actually
1848 * insert this page into the given map NOW.
1851 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
1852 vm_prot_t prot, boolean_t wired)
1856 pt_entry_t origpte, newpte;
1865 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
1866 KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0,
1867 ("pmap_enter: page %p is not busy", m));
1871 vm_page_lock_queues();
1875 * In the case that a page table page is not resident, we are
1878 if (va < VM_MAXUSER_ADDRESS) {
1879 mpte = pmap_allocpte(pmap, va, M_WAITOK);
1881 pte = pmap_pte(pmap, va);
1884 * Page Directory table entry not valid, we need a new PT page
1887 panic("pmap_enter: invalid page directory, pdir=%p, va=%p",
1888 (void *)pmap->pm_segtab, (void *)va);
1890 pa = VM_PAGE_TO_PHYS(m);
1893 opa = TLBLO_PTE_TO_PA(origpte);
1896 * Mapping has not changed, must be protection or wiring change.
1898 if (pte_test(&origpte, PTE_V) && opa == pa) {
1900 * Wiring change, just update stats. We don't worry about
1901 * wiring PT pages as they remain resident as long as there
1902 * are valid mappings in them. Hence, if a user page is
1903 * wired, the PT page will be also.
1905 if (wired && !pte_test(&origpte, PTE_W))
1906 pmap->pm_stats.wired_count++;
1907 else if (!wired && pte_test(&origpte, PTE_W))
1908 pmap->pm_stats.wired_count--;
1910 KASSERT(!pte_test(&origpte, PTE_D | PTE_RO),
1911 ("%s: modified page not writable: va: %p, pte: %#jx",
1912 __func__, (void *)va, (uintmax_t)origpte));
1915 * Remove extra pte reference
1920 if (page_is_managed(opa)) {
1929 * Mapping has changed, invalidate old range and fall through to
1930 * handle validating new mapping.
1933 if (pte_test(&origpte, PTE_W))
1934 pmap->pm_stats.wired_count--;
1936 if (page_is_managed(opa)) {
1937 om = PHYS_TO_VM_PAGE(opa);
1938 pv = pmap_pvh_remove(&om->md, pmap, va);
1942 KASSERT(mpte->wire_count > 0,
1943 ("pmap_enter: missing reference to page table page,"
1944 " va: %p", (void *)va));
1947 pmap->pm_stats.resident_count++;
1950 * Enter on the PV list if part of our managed memory. Note that we
1951 * raise IPL while manipulating pv_table since pmap_enter can be
1952 * called at interrupt time.
1954 if ((m->oflags & VPO_UNMANAGED) == 0) {
1955 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
1956 ("pmap_enter: managed mapping within the clean submap"));
1958 pv = get_pv_entry(pmap);
1962 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1963 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1964 m->md.pv_list_count++;
1965 } else if (pv != NULL)
1969 * Increment counters
1972 pmap->pm_stats.wired_count++;
1975 if ((access & VM_PROT_WRITE) != 0)
1976 m->md.pv_flags |= PV_TABLE_MOD | PV_TABLE_REF;
1977 rw = init_pte_prot(va, m, prot);
1980 printf("pmap_enter: va: %p -> pa: %p\n", (void *)va, (void *)pa);
1983 * Now validate mapping with desired protection/wiring.
1985 newpte = TLBLO_PA_TO_PFN(pa) | rw | PTE_V;
1987 if (is_cacheable_mem(pa))
1988 newpte |= PTE_C_CACHE;
1990 newpte |= PTE_C_UNCACHED;
1995 if (is_kernel_pmap(pmap))
1999 * if the mapping or permission bits are different, we need to
2002 if (origpte != newpte) {
2003 if (pte_test(&origpte, PTE_V)) {
2005 if (page_is_managed(opa) && (opa != pa)) {
2006 if (om->md.pv_flags & PV_TABLE_REF)
2007 vm_page_aflag_set(om, PGA_REFERENCED);
2009 ~(PV_TABLE_REF | PV_TABLE_MOD);
2011 if (pte_test(&origpte, PTE_D)) {
2012 KASSERT(!pte_test(&origpte, PTE_RO),
2013 ("pmap_enter: modified page not writable:"
2014 " va: %p, pte: %#jx", (void *)va, (uintmax_t)origpte));
2015 if (page_is_managed(opa))
2018 if (page_is_managed(opa) &&
2019 TAILQ_EMPTY(&om->md.pv_list))
2020 vm_page_aflag_clear(om, PGA_WRITEABLE);
2025 pmap_update_page(pmap, va, newpte);
2028 * Sync I & D caches for executable pages. Do this only if the
2029 * target pmap belongs to the current process. Otherwise, an
2030 * unresolvable TLB miss may occur.
2032 if (!is_kernel_pmap(pmap) && (pmap == &curproc->p_vmspace->vm_pmap) &&
2033 (prot & VM_PROT_EXECUTE)) {
2034 mips_icache_sync_range(va, PAGE_SIZE);
2035 mips_dcache_wbinv_range(va, PAGE_SIZE);
2037 vm_page_unlock_queues();
2042 * this code makes some *MAJOR* assumptions:
2043 * 1. Current pmap & pmap exists.
2046 * 4. No page table pages.
2047 * but is *MUCH* faster than pmap_enter...
2051 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2054 vm_page_lock_queues();
2056 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
2057 vm_page_unlock_queues();
2062 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
2063 vm_prot_t prot, vm_page_t mpte)
2068 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2069 (m->oflags & VPO_UNMANAGED) != 0,
2070 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2071 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2072 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2075 * In the case that a page table page is not resident, we are
2078 if (va < VM_MAXUSER_ADDRESS) {
2083 * Calculate pagetable page index
2085 ptepindex = pmap_pde_pindex(va);
2086 if (mpte && (mpte->pindex == ptepindex)) {
2090 * Get the page directory entry
2092 pde = pmap_pde(pmap, va);
2095 * If the page table page is mapped, we just
2096 * increment the hold count, and activate it.
2098 if (pde && *pde != 0) {
2099 if (pmap->pm_ptphint &&
2100 (pmap->pm_ptphint->pindex == ptepindex)) {
2101 mpte = pmap->pm_ptphint;
2103 mpte = PHYS_TO_VM_PAGE(
2104 MIPS_DIRECT_TO_PHYS(*pde));
2105 pmap->pm_ptphint = mpte;
2109 mpte = _pmap_allocpte(pmap, ptepindex,
2119 pte = pmap_pte(pmap, va);
2120 if (pte_test(pte, PTE_V)) {
2129 * Enter on the PV list if part of our managed memory.
2131 if ((m->oflags & VPO_UNMANAGED) == 0 &&
2132 !pmap_try_insert_pv_entry(pmap, mpte, va, m)) {
2134 pmap_unwire_pte_hold(pmap, va, mpte);
2141 * Increment counters
2143 pmap->pm_stats.resident_count++;
2145 pa = VM_PAGE_TO_PHYS(m);
2148 * Now validate mapping with RO protection
2150 *pte = TLBLO_PA_TO_PFN(pa) | PTE_V;
2152 if (is_cacheable_mem(pa))
2153 *pte |= PTE_C_CACHE;
2155 *pte |= PTE_C_UNCACHED;
2157 if (is_kernel_pmap(pmap))
2162 * Sync I & D caches. Do this only if the target pmap
2163 * belongs to the current process. Otherwise, an
2164 * unresolvable TLB miss may occur. */
2165 if (pmap == &curproc->p_vmspace->vm_pmap) {
2167 mips_icache_sync_range(va, PAGE_SIZE);
2168 mips_dcache_wbinv_range(va, PAGE_SIZE);
2175 * Make a temporary mapping for a physical address. This is only intended
2176 * to be used for panic dumps.
2178 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2181 pmap_kenter_temporary(vm_paddr_t pa, int i)
2186 printf("%s: ERROR!!! More than one page of virtual address mapping not supported\n",
2189 if (MIPS_DIRECT_MAPPABLE(pa)) {
2190 va = MIPS_PHYS_TO_DIRECT(pa);
2192 #ifndef __mips_n64 /* XXX : to be converted to new style */
2195 struct local_sysmaps *sysm;
2196 pt_entry_t *pte, npte;
2198 /* If this is used other than for dumps, we may need to leave
2199 * interrupts disasbled on return. If crash dumps don't work when
2200 * we get to this point, we might want to consider this (leaving things
2201 * disabled as a starting point ;-)
2203 intr = intr_disable();
2204 cpu = PCPU_GET(cpuid);
2205 sysm = &sysmap_lmem[cpu];
2206 /* Since this is for the debugger, no locks or any other fun */
2207 npte = TLBLO_PA_TO_PFN(pa) | PTE_D | PTE_V | PTE_G | PTE_W | PTE_C_CACHE;
2208 pte = pmap_pte(kernel_pmap, sysm->base);
2211 pmap_update_page(kernel_pmap, sysm->base, npte);
2216 return ((void *)va);
2220 pmap_kenter_temporary_free(vm_paddr_t pa)
2222 #ifndef __mips_n64 /* XXX : to be converted to new style */
2225 struct local_sysmaps *sysm;
2228 if (MIPS_DIRECT_MAPPABLE(pa)) {
2229 /* nothing to do for this case */
2232 #ifndef __mips_n64 /* XXX : to be converted to new style */
2233 cpu = PCPU_GET(cpuid);
2234 sysm = &sysmap_lmem[cpu];
2238 intr = intr_disable();
2239 pte = pmap_pte(kernel_pmap, sysm->base);
2241 pmap_invalidate_page(kernel_pmap, sysm->base);
2249 * Moved the code to Machine Independent
2250 * vm_map_pmap_enter()
2254 * Maps a sequence of resident pages belonging to the same object.
2255 * The sequence begins with the given page m_start. This page is
2256 * mapped at the given virtual address start. Each subsequent page is
2257 * mapped at a virtual address that is offset from start by the same
2258 * amount as the page is offset from m_start within the object. The
2259 * last page in the sequence is the page with the largest offset from
2260 * m_start that can be mapped at a virtual address less than the given
2261 * virtual address end. Not every virtual page between start and end
2262 * is mapped; only those for which a resident page exists with the
2263 * corresponding offset from m_start are mapped.
2266 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2267 vm_page_t m_start, vm_prot_t prot)
2270 vm_pindex_t diff, psize;
2272 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
2273 psize = atop(end - start);
2276 vm_page_lock_queues();
2278 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2279 mpte = pmap_enter_quick_locked(pmap, start + ptoa(diff), m,
2281 m = TAILQ_NEXT(m, listq);
2283 vm_page_unlock_queues();
2288 * pmap_object_init_pt preloads the ptes for a given object
2289 * into the specified pmap. This eliminates the blast of soft
2290 * faults on process startup and immediately after an mmap.
2293 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2294 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2296 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2297 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2298 ("pmap_object_init_pt: non-device object"));
2302 * Routine: pmap_change_wiring
2303 * Function: Change the wiring attribute for a map/virtual-address
2305 * In/out conditions:
2306 * The mapping must already exist in the pmap.
2309 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2317 pte = pmap_pte(pmap, va);
2319 if (wired && !pte_test(pte, PTE_W))
2320 pmap->pm_stats.wired_count++;
2321 else if (!wired && pte_test(pte, PTE_W))
2322 pmap->pm_stats.wired_count--;
2325 * Wiring is not a hardware characteristic so there is no need to
2329 pte_set(pte, PTE_W);
2331 pte_clear(pte, PTE_W);
2336 * Copy the range specified by src_addr/len
2337 * from the source map to the range dst_addr/len
2338 * in the destination map.
2340 * This routine is only advisory and need not do anything.
2344 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2345 vm_size_t len, vm_offset_t src_addr)
2350 * pmap_zero_page zeros the specified hardware page by mapping
2351 * the page into KVM and using bzero to clear its contents.
2353 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2356 pmap_zero_page(vm_page_t m)
2359 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2361 if (MIPS_DIRECT_MAPPABLE(phys)) {
2362 va = MIPS_PHYS_TO_DIRECT(phys);
2363 bzero((caddr_t)va, PAGE_SIZE);
2364 mips_dcache_wbinv_range(va, PAGE_SIZE);
2366 va = pmap_lmem_map1(phys);
2367 bzero((caddr_t)va, PAGE_SIZE);
2368 mips_dcache_wbinv_range(va, PAGE_SIZE);
2374 * pmap_zero_page_area zeros the specified hardware page by mapping
2375 * the page into KVM and using bzero to clear its contents.
2377 * off and size may not cover an area beyond a single hardware page.
2380 pmap_zero_page_area(vm_page_t m, int off, int size)
2383 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2385 if (MIPS_DIRECT_MAPPABLE(phys)) {
2386 va = MIPS_PHYS_TO_DIRECT(phys);
2387 bzero((char *)(caddr_t)va + off, size);
2388 mips_dcache_wbinv_range(va + off, size);
2390 va = pmap_lmem_map1(phys);
2391 bzero((char *)va + off, size);
2392 mips_dcache_wbinv_range(va + off, size);
2398 pmap_zero_page_idle(vm_page_t m)
2401 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2403 if (MIPS_DIRECT_MAPPABLE(phys)) {
2404 va = MIPS_PHYS_TO_DIRECT(phys);
2405 bzero((caddr_t)va, PAGE_SIZE);
2406 mips_dcache_wbinv_range(va, PAGE_SIZE);
2408 va = pmap_lmem_map1(phys);
2409 bzero((caddr_t)va, PAGE_SIZE);
2410 mips_dcache_wbinv_range(va, PAGE_SIZE);
2416 * pmap_copy_page copies the specified (machine independent)
2417 * page by mapping the page into virtual memory and using
2418 * bcopy to copy the page, one machine dependent page at a
2421 * Use XKPHYS for 64 bit, and KSEG0 where possible for 32 bit.
2424 pmap_copy_page(vm_page_t src, vm_page_t dst)
2426 vm_offset_t va_src, va_dst;
2427 vm_paddr_t phys_src = VM_PAGE_TO_PHYS(src);
2428 vm_paddr_t phys_dst = VM_PAGE_TO_PHYS(dst);
2430 if (MIPS_DIRECT_MAPPABLE(phys_src) && MIPS_DIRECT_MAPPABLE(phys_dst)) {
2431 /* easy case, all can be accessed via KSEG0 */
2433 * Flush all caches for VA that are mapped to this page
2434 * to make sure that data in SDRAM is up to date
2436 pmap_flush_pvcache(src);
2437 mips_dcache_wbinv_range_index(
2438 MIPS_PHYS_TO_DIRECT(phys_dst), PAGE_SIZE);
2439 va_src = MIPS_PHYS_TO_DIRECT(phys_src);
2440 va_dst = MIPS_PHYS_TO_DIRECT(phys_dst);
2441 bcopy((caddr_t)va_src, (caddr_t)va_dst, PAGE_SIZE);
2442 mips_dcache_wbinv_range(va_dst, PAGE_SIZE);
2444 va_src = pmap_lmem_map2(phys_src, phys_dst);
2445 va_dst = va_src + PAGE_SIZE;
2446 bcopy((void *)va_src, (void *)va_dst, PAGE_SIZE);
2447 mips_dcache_wbinv_range(va_dst, PAGE_SIZE);
2453 * Returns true if the pmap's pv is one of the first
2454 * 16 pvs linked to from this page. This count may
2455 * be changed upwards or downwards in the future; it
2456 * is only necessary that true be returned for a small
2457 * subset of pmaps for proper page aging.
2460 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2466 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2467 ("pmap_page_exists_quick: page %p is not managed", m));
2469 vm_page_lock_queues();
2470 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2471 if (pv->pv_pmap == pmap) {
2479 vm_page_unlock_queues();
2484 * Remove all pages from specified address space
2485 * this aids process exit speeds. Also, this code
2486 * is special cased for current process only, but
2487 * can have the more generic (and slightly slower)
2488 * mode enabled. This is much faster than pmap_remove
2489 * in the case of running down an entire address space.
2492 pmap_remove_pages(pmap_t pmap)
2494 pt_entry_t *pte, tpte;
2498 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
2499 printf("warning: pmap_remove_pages called with non-current pmap\n");
2502 vm_page_lock_queues();
2504 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv != NULL; pv = npv) {
2506 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2507 if (!pte_test(pte, PTE_V))
2508 panic("pmap_remove_pages: page on pm_pvlist has no pte");
2512 * We cannot remove wired pages from a process' mapping at this time
2514 if (pte_test(&tpte, PTE_W)) {
2515 npv = TAILQ_NEXT(pv, pv_plist);
2518 *pte = is_kernel_pmap(pmap) ? PTE_G : 0;
2520 m = PHYS_TO_VM_PAGE(TLBLO_PTE_TO_PA(tpte));
2522 ("pmap_remove_pages: bad tpte %#jx", (uintmax_t)tpte));
2524 pv->pv_pmap->pm_stats.resident_count--;
2527 * Update the vm_page_t clean and reference bits.
2529 if (pte_test(&tpte, PTE_D)) {
2532 npv = TAILQ_NEXT(pv, pv_plist);
2533 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2535 m->md.pv_list_count--;
2536 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2537 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2538 vm_page_aflag_clear(m, PGA_WRITEABLE);
2540 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2543 pmap_invalidate_all(pmap);
2545 vm_page_unlock_queues();
2549 * pmap_testbit tests bits in pte's
2550 * note that the testbit/changebit routines are inline,
2551 * and a lot of things compile-time evaluate.
2554 pmap_testbit(vm_page_t m, int bit)
2558 boolean_t rv = FALSE;
2560 if (m->oflags & VPO_UNMANAGED)
2563 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2566 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2567 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2568 PMAP_LOCK(pv->pv_pmap);
2569 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2570 rv = pte_test(pte, bit);
2571 PMAP_UNLOCK(pv->pv_pmap);
2579 * this routine is used to clear dirty bits in ptes
2581 static __inline void
2582 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2587 if (m->oflags & VPO_UNMANAGED)
2590 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2592 * Loop over all current mappings setting/clearing as appropos If
2593 * setting RO do we need to clear the VAC?
2595 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2596 PMAP_LOCK(pv->pv_pmap);
2597 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2600 pmap_update_page(pv->pv_pmap, pv->pv_va, *pte);
2602 pt_entry_t pbits = *pte;
2608 *pte = (pbits & ~PTE_D) | PTE_RO;
2610 *pte = pbits & ~bit;
2612 pmap_update_page(pv->pv_pmap, pv->pv_va, *pte);
2615 PMAP_UNLOCK(pv->pv_pmap);
2617 if (!setem && bit == PTE_D)
2618 vm_page_aflag_clear(m, PGA_WRITEABLE);
2622 * pmap_page_wired_mappings:
2624 * Return the number of managed mappings to the given physical page
2628 pmap_page_wired_mappings(vm_page_t m)
2636 if ((m->oflags & VPO_UNMANAGED) != 0)
2638 vm_page_lock_queues();
2639 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2642 pte = pmap_pte(pmap, pv->pv_va);
2643 if (pte_test(pte, PTE_W))
2647 vm_page_unlock_queues();
2652 * Clear the write and modified bits in each of the given page's mappings.
2655 pmap_remove_write(vm_page_t m)
2661 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2662 ("pmap_remove_write: page %p is not managed", m));
2665 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by
2666 * another thread while the object is locked. Thus, if PGA_WRITEABLE
2667 * is clear, no page table entries need updating.
2669 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2670 if ((m->oflags & VPO_BUSY) == 0 &&
2671 (m->aflags & PGA_WRITEABLE) == 0)
2675 * Loop over all current mappings setting/clearing as appropos.
2677 vm_page_lock_queues();
2678 for (pv = TAILQ_FIRST(&m->md.pv_list); pv; pv = npv) {
2679 npv = TAILQ_NEXT(pv, pv_plist);
2680 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2681 if (pte == NULL || !pte_test(pte, PTE_V))
2682 panic("page on pm_pvlist has no pte");
2685 pmap_protect(pv->pv_pmap, va, va + PAGE_SIZE,
2686 VM_PROT_READ | VM_PROT_EXECUTE);
2688 vm_page_aflag_clear(m, PGA_WRITEABLE);
2689 vm_page_unlock_queues();
2693 * pmap_ts_referenced:
2695 * Return the count of reference bits for a page, clearing all of them.
2698 pmap_ts_referenced(vm_page_t m)
2701 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2702 ("pmap_ts_referenced: page %p is not managed", m));
2703 if (m->md.pv_flags & PV_TABLE_REF) {
2704 vm_page_lock_queues();
2705 m->md.pv_flags &= ~PV_TABLE_REF;
2706 vm_page_unlock_queues();
2715 * Return whether or not the specified physical page was modified
2716 * in any physical maps.
2719 pmap_is_modified(vm_page_t m)
2723 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2724 ("pmap_is_modified: page %p is not managed", m));
2727 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be
2728 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
2729 * is clear, no PTEs can have PTE_D set.
2731 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2732 if ((m->oflags & VPO_BUSY) == 0 &&
2733 (m->aflags & PGA_WRITEABLE) == 0)
2735 vm_page_lock_queues();
2736 if (m->md.pv_flags & PV_TABLE_MOD)
2739 rv = pmap_testbit(m, PTE_D);
2740 vm_page_unlock_queues();
2747 * pmap_is_prefaultable:
2749 * Return whether or not the specified virtual address is elgible
2753 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2761 pde = pmap_pde(pmap, addr);
2762 if (pde != NULL && *pde != 0) {
2763 pte = pmap_pde_to_pte(pde, addr);
2771 * Clear the modify bits on the specified physical page.
2774 pmap_clear_modify(vm_page_t m)
2777 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2778 ("pmap_clear_modify: page %p is not managed", m));
2779 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2780 KASSERT((m->oflags & VPO_BUSY) == 0,
2781 ("pmap_clear_modify: page %p is busy", m));
2784 * If the page is not PGA_WRITEABLE, then no PTEs can have PTE_D set.
2785 * If the object containing the page is locked and the page is not
2786 * VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set.
2788 if ((m->aflags & PGA_WRITEABLE) == 0)
2790 vm_page_lock_queues();
2791 if (m->md.pv_flags & PV_TABLE_MOD) {
2792 pmap_changebit(m, PTE_D, FALSE);
2793 m->md.pv_flags &= ~PV_TABLE_MOD;
2795 vm_page_unlock_queues();
2799 * pmap_is_referenced:
2801 * Return whether or not the specified physical page was referenced
2802 * in any physical maps.
2805 pmap_is_referenced(vm_page_t m)
2808 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2809 ("pmap_is_referenced: page %p is not managed", m));
2810 return ((m->md.pv_flags & PV_TABLE_REF) != 0);
2814 * pmap_clear_reference:
2816 * Clear the reference bit on the specified physical page.
2819 pmap_clear_reference(vm_page_t m)
2822 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2823 ("pmap_clear_reference: page %p is not managed", m));
2824 vm_page_lock_queues();
2825 if (m->md.pv_flags & PV_TABLE_REF) {
2826 m->md.pv_flags &= ~PV_TABLE_REF;
2828 vm_page_unlock_queues();
2832 * Miscellaneous support routines follow
2836 * Map a set of physical memory pages into the kernel virtual
2837 * address space. Return a pointer to where it is mapped. This
2838 * routine is intended to be used for mapping device memory,
2843 * Map a set of physical memory pages into the kernel virtual
2844 * address space. Return a pointer to where it is mapped. This
2845 * routine is intended to be used for mapping device memory,
2848 * Use XKPHYS uncached for 64 bit, and KSEG1 where possible for 32 bit.
2851 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
2853 vm_offset_t va, tmpva, offset;
2856 * KSEG1 maps only first 512M of phys address space. For
2857 * pa > 0x20000000 we should make proper mapping * using pmap_kenter.
2859 if (MIPS_DIRECT_MAPPABLE(pa + size - 1))
2860 return ((void *)MIPS_PHYS_TO_DIRECT_UNCACHED(pa));
2862 offset = pa & PAGE_MASK;
2863 size = roundup(size + offset, PAGE_SIZE);
2865 va = kmem_alloc_nofault(kernel_map, size);
2867 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2868 pa = trunc_page(pa);
2869 for (tmpva = va; size > 0;) {
2870 pmap_kenter_attr(tmpva, pa, PTE_C_UNCACHED);
2877 return ((void *)(va + offset));
2881 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2884 vm_offset_t base, offset, tmpva;
2886 /* If the address is within KSEG1 then there is nothing to do */
2887 if (va >= MIPS_KSEG1_START && va <= MIPS_KSEG1_END)
2890 base = trunc_page(va);
2891 offset = va & PAGE_MASK;
2892 size = roundup(size + offset, PAGE_SIZE);
2893 for (tmpva = base; tmpva < base + size; tmpva += PAGE_SIZE)
2894 pmap_kremove(tmpva);
2895 kmem_free(kernel_map, base, size);
2900 * perform the pmap work for mincore
2903 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
2905 pt_entry_t *ptep, pte;
2913 ptep = pmap_pte(pmap, addr);
2914 pte = (ptep != NULL) ? *ptep : 0;
2915 if (!pte_test(&pte, PTE_V)) {
2919 val = MINCORE_INCORE;
2920 if (pte_test(&pte, PTE_D))
2921 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
2922 pa = TLBLO_PTE_TO_PA(pte);
2923 managed = page_is_managed(pa);
2926 * This may falsely report the given address as
2927 * MINCORE_REFERENCED. Unfortunately, due to the lack of
2928 * per-PTE reference information, it is impossible to
2929 * determine if the address is MINCORE_REFERENCED.
2931 m = PHYS_TO_VM_PAGE(pa);
2932 if ((m->aflags & PGA_REFERENCED) != 0)
2933 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
2935 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
2936 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
2937 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
2938 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
2942 PA_UNLOCK_COND(*locked_pa);
2948 pmap_activate(struct thread *td)
2950 pmap_t pmap, oldpmap;
2951 struct proc *p = td->td_proc;
2956 pmap = vmspace_pmap(p->p_vmspace);
2957 oldpmap = PCPU_GET(curpmap);
2958 cpuid = PCPU_GET(cpuid);
2961 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
2962 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
2963 pmap_asid_alloc(pmap);
2964 if (td == curthread) {
2965 PCPU_SET(segbase, pmap->pm_segtab);
2966 mips_wr_entryhi(pmap->pm_asid[cpuid].asid);
2969 PCPU_SET(curpmap, pmap);
2974 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
2979 * Increase the starting virtual address of the given mapping if a
2980 * different alignment might result in more superpage mappings.
2983 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
2984 vm_offset_t *addr, vm_size_t size)
2986 vm_offset_t superpage_offset;
2990 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
2991 offset += ptoa(object->pg_color);
2992 superpage_offset = offset & SEGMASK;
2993 if (size - ((NBSEG - superpage_offset) & SEGMASK) < NBSEG ||
2994 (*addr & SEGMASK) == superpage_offset)
2996 if ((*addr & SEGMASK) < superpage_offset)
2997 *addr = (*addr & ~SEGMASK) + superpage_offset;
2999 *addr = ((*addr + SEGMASK) & ~SEGMASK) + superpage_offset;
3003 * Increase the starting virtual address of the given mapping so
3004 * that it is aligned to not be the second page in a TLB entry.
3005 * This routine assumes that the length is appropriately-sized so
3006 * that the allocation does not share a TLB entry at all if required.
3009 pmap_align_tlb(vm_offset_t *addr)
3011 if ((*addr & PAGE_SIZE) == 0)
3018 DB_SHOW_COMMAND(ptable, ddb_pid_dump)
3021 struct thread *td = NULL;
3028 td = db_lookup_thread(addr, TRUE);
3030 db_printf("Invalid pid or tid");
3034 if (p->p_vmspace == NULL) {
3035 db_printf("No vmspace for process");
3038 pmap = vmspace_pmap(p->p_vmspace);
3042 db_printf("pmap:%p segtab:%p asid:%x generation:%x\n",
3043 pmap, pmap->pm_segtab, pmap->pm_asid[0].asid,
3044 pmap->pm_asid[0].gen);
3045 for (i = 0; i < NPDEPG; i++) {
3050 pdpe = (pd_entry_t *)pmap->pm_segtab[i];
3053 db_printf("[%4d] %p\n", i, pdpe);
3055 for (j = 0; j < NPDEPG; j++) {
3056 pde = (pt_entry_t *)pdpe[j];
3059 db_printf("\t[%4d] %p\n", j, pde);
3063 pde = (pt_entry_t *)pdpe;
3065 for (k = 0; k < NPTEPG; k++) {
3067 if (pte == 0 || !pte_test(&pte, PTE_V))
3069 pa = TLBLO_PTE_TO_PA(pte);
3070 va = ((u_long)i << SEGSHIFT) | (j << PDRSHIFT) | (k << PAGE_SHIFT);
3071 db_printf("\t\t[%04d] va: %p pte: %8jx pa:%jx\n",
3072 k, (void *)va, (uintmax_t)pte, (uintmax_t)pa);
3081 static void pads(pmap_t pm);
3082 void pmap_pvdump(vm_offset_t pa);
3084 /* print address space of pmap*/
3091 if (pm == kernel_pmap)
3093 for (i = 0; i < NPTEPG; i++)
3094 if (pm->pm_segtab[i])
3095 for (j = 0; j < NPTEPG; j++) {
3096 va = (i << SEGSHIFT) + (j << PAGE_SHIFT);
3097 if (pm == kernel_pmap && va < KERNBASE)
3099 if (pm != kernel_pmap &&
3100 va >= VM_MAXUSER_ADDRESS)
3102 ptep = pmap_pte(pm, va);
3103 if (pte_test(ptep, PTE_V))
3104 printf("%x:%x ", va, *(int *)ptep);
3110 pmap_pvdump(vm_offset_t pa)
3112 register pv_entry_t pv;
3115 printf("pa %x", pa);
3116 m = PHYS_TO_VM_PAGE(pa);
3117 for (pv = TAILQ_FIRST(&m->md.pv_list); pv;
3118 pv = TAILQ_NEXT(pv, pv_list)) {
3119 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
3130 * Allocate TLB address space tag (called ASID or TLBPID) and return it.
3131 * It takes almost as much or more time to search the TLB for a
3132 * specific ASID and flush those entries as it does to flush the entire TLB.
3133 * Therefore, when we allocate a new ASID, we just take the next number. When
3134 * we run out of numbers, we flush the TLB, increment the generation count
3135 * and start over. ASID zero is reserved for kernel use.
3138 pmap_asid_alloc(pmap)
3141 if (pmap->pm_asid[PCPU_GET(cpuid)].asid != PMAP_ASID_RESERVED &&
3142 pmap->pm_asid[PCPU_GET(cpuid)].gen == PCPU_GET(asid_generation));
3144 if (PCPU_GET(next_asid) == pmap_max_asid) {
3145 tlb_invalidate_all_user(NULL);
3146 PCPU_SET(asid_generation,
3147 (PCPU_GET(asid_generation) + 1) & ASIDGEN_MASK);
3148 if (PCPU_GET(asid_generation) == 0) {
3149 PCPU_SET(asid_generation, 1);
3151 PCPU_SET(next_asid, 1); /* 0 means invalid */
3153 pmap->pm_asid[PCPU_GET(cpuid)].asid = PCPU_GET(next_asid);
3154 pmap->pm_asid[PCPU_GET(cpuid)].gen = PCPU_GET(asid_generation);
3155 PCPU_SET(next_asid, PCPU_GET(next_asid) + 1);
3160 page_is_managed(vm_paddr_t pa)
3162 vm_offset_t pgnum = atop(pa);
3164 if (pgnum >= first_page) {
3167 m = PHYS_TO_VM_PAGE(pa);
3170 if ((m->oflags & VPO_UNMANAGED) == 0)
3177 init_pte_prot(vm_offset_t va, vm_page_t m, vm_prot_t prot)
3181 if (!(prot & VM_PROT_WRITE))
3182 rw = PTE_V | PTE_RO | PTE_C_CACHE;
3183 else if ((m->oflags & VPO_UNMANAGED) == 0) {
3184 if ((m->md.pv_flags & PV_TABLE_MOD) != 0)
3185 rw = PTE_V | PTE_D | PTE_C_CACHE;
3187 rw = PTE_V | PTE_C_CACHE;
3188 vm_page_aflag_set(m, PGA_WRITEABLE);
3190 /* Needn't emulate a modified bit for unmanaged pages. */
3191 rw = PTE_V | PTE_D | PTE_C_CACHE;
3196 * pmap_emulate_modified : do dirty bit emulation
3198 * On SMP, update just the local TLB, other CPUs will update their
3199 * TLBs from PTE lazily, if they get the exception.
3200 * Returns 0 in case of sucess, 1 if the page is read only and we
3204 pmap_emulate_modified(pmap_t pmap, vm_offset_t va)
3211 pte = pmap_pte(pmap, va);
3213 panic("pmap_emulate_modified: can't find PTE");
3215 /* It is possible that some other CPU changed m-bit */
3216 if (!pte_test(pte, PTE_V) || pte_test(pte, PTE_D)) {
3217 pmap_update_page_local(pmap, va, *pte);
3222 if (!pte_test(pte, PTE_V) || pte_test(pte, PTE_D))
3223 panic("pmap_emulate_modified: invalid pte");
3225 if (pte_test(pte, PTE_RO)) {
3226 /* write to read only page in the kernel */
3230 pte_set(pte, PTE_D);
3231 pmap_update_page_local(pmap, va, *pte);
3232 pa = TLBLO_PTE_TO_PA(*pte);
3233 if (!page_is_managed(pa))
3234 panic("pmap_emulate_modified: unmanaged page");
3235 m = PHYS_TO_VM_PAGE(pa);
3236 m->md.pv_flags |= (PV_TABLE_REF | PV_TABLE_MOD);
3242 * Routine: pmap_kextract
3244 * Extract the physical page address associated
3247 /* PMAP_INLINE */ vm_offset_t
3248 pmap_kextract(vm_offset_t va)
3253 * First, the direct-mapped regions.
3255 #if defined(__mips_n64)
3256 if (va >= MIPS_XKPHYS_START && va < MIPS_XKPHYS_END)
3257 return (MIPS_XKPHYS_TO_PHYS(va));
3259 if (va >= MIPS_KSEG0_START && va < MIPS_KSEG0_END)
3260 return (MIPS_KSEG0_TO_PHYS(va));
3262 if (va >= MIPS_KSEG1_START && va < MIPS_KSEG1_END)
3263 return (MIPS_KSEG1_TO_PHYS(va));
3266 * User virtual addresses.
3268 if (va < VM_MAXUSER_ADDRESS) {
3271 if (curproc && curproc->p_vmspace) {
3272 ptep = pmap_pte(&curproc->p_vmspace->vm_pmap, va);
3274 return (TLBLO_PTE_TO_PA(*ptep) |
3282 * Should be kernel virtual here, otherwise fail
3284 mapped = (va >= MIPS_KSEG2_START || va < MIPS_KSEG2_END);
3285 #if defined(__mips_n64)
3286 mapped = mapped || (va >= MIPS_XKSEG_START || va < MIPS_XKSEG_END);
3295 /* Is the kernel pmap initialized? */
3296 if (!CPU_EMPTY(&kernel_pmap->pm_active)) {
3297 /* It's inside the virtual address range */
3298 ptep = pmap_pte(kernel_pmap, va);
3300 return (TLBLO_PTE_TO_PA(*ptep) |
3307 panic("%s for unknown address space %p.", __func__, (void *)va);
3312 pmap_flush_pvcache(vm_page_t m)
3317 for (pv = TAILQ_FIRST(&m->md.pv_list); pv;
3318 pv = TAILQ_NEXT(pv, pv_list)) {
3319 mips_dcache_wbinv_range_index(pv->pv_va, PAGE_SIZE);