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$");
72 #include "opt_msgbuf.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
76 #include <sys/msgbuf.h>
77 #include <sys/vmmeter.h>
81 #include <vm/vm_param.h>
83 #include <sys/mutex.h>
84 #include <vm/vm_kern.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_extern.h>
89 #include <vm/vm_pageout.h>
90 #include <vm/vm_pager.h>
93 #include <sys/sched.h>
98 #include <machine/cache.h>
99 #include <machine/pltfm.h>
100 #include <machine/md_var.h>
102 #if defined(DIAGNOSTIC)
103 #define PMAP_DIAGNOSTIC
108 #ifndef PMAP_SHPGPERPROC
109 #define PMAP_SHPGPERPROC 200
112 #if !defined(PMAP_DIAGNOSTIC)
113 #define PMAP_INLINE __inline
119 * Get PDEs and PTEs for user/kernel address space
121 #define pmap_pde(m, v) (&((m)->pm_segtab[(vm_offset_t)(v) >> SEGSHIFT]))
122 #define segtab_pde(m, v) (m[(vm_offset_t)(v) >> SEGSHIFT])
124 #define pmap_pte_w(pte) ((*(int *)pte & PTE_W) != 0)
125 #define pmap_pde_v(pte) ((*(int *)pte) != 0)
126 #define pmap_pte_m(pte) ((*(int *)pte & PTE_M) != 0)
127 #define pmap_pte_v(pte) ((*(int *)pte & PTE_V) != 0)
129 #define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PTE_W):(*(int *)pte &= ~PTE_W))
130 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
132 #define MIPS_SEGSIZE (1L << SEGSHIFT)
133 #define mips_segtrunc(va) ((va) & ~(MIPS_SEGSIZE-1))
134 #define pmap_TLB_invalidate_all() MIPS_TBIAP()
135 #define pmap_va_asid(pmap, va) ((va) | ((pmap)->pm_asid[PCPU_GET(cpuid)].asid << VMTLB_PID_SHIFT))
136 #define is_kernel_pmap(x) ((x) == kernel_pmap)
138 struct pmap kernel_pmap_store;
139 pd_entry_t *kernel_segmap;
141 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
142 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
145 unsigned pmap_max_asid; /* max ASID supported by the system */
148 #define PMAP_ASID_RESERVED 0
151 vm_offset_t kernel_vm_end;
153 static void pmap_asid_alloc(pmap_t pmap);
156 * Data for the pv entry allocation mechanism
158 static uma_zone_t pvzone;
159 static struct vm_object pvzone_obj;
160 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
162 struct fpage fpages_shared[FPAGES_SHARED];
164 struct sysmaps sysmaps_pcpu[MAXCPU];
166 static PMAP_INLINE void free_pv_entry(pv_entry_t pv);
167 static pv_entry_t get_pv_entry(pmap_t locked_pmap);
168 static __inline void pmap_changebit(vm_page_t m, int bit, boolean_t setem);
170 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
171 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
172 static int pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va);
173 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
174 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va);
175 static boolean_t pmap_testbit(vm_page_t m, int bit);
177 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
178 vm_page_t m, boolean_t wired);
179 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte,
180 vm_offset_t va, vm_page_t m);
182 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
184 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
185 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t);
186 static int init_pte_prot(vm_offset_t va, vm_page_t m, vm_prot_t prot);
187 static void pmap_TLB_invalidate_kernel(vm_offset_t);
188 static void pmap_TLB_update_kernel(vm_offset_t, pt_entry_t);
189 static void pmap_init_fpage(void);
192 static void pmap_invalidate_page_action(void *arg);
193 static void pmap_invalidate_all_action(void *arg);
194 static void pmap_update_page_action(void *arg);
198 struct local_sysmaps {
204 uint16_t valid1, valid2;
207 /* This structure is for large memory
208 * above 512Meg. We can't (in 32 bit mode)
209 * just use the direct mapped MIPS_CACHED_TO_PHYS()
210 * macros since we can't see the memory and must
211 * map it in when we need to access it. In 64
212 * bit mode this goes away.
214 static struct local_sysmaps sysmap_lmem[MAXCPU];
215 caddr_t virtual_sys_start = (caddr_t)0;
218 pmap_segmap(pmap_t pmap, vm_offset_t va)
221 return (pmap->pm_segtab[((vm_offset_t)(va) >> SEGSHIFT)]);
223 return ((pd_entry_t)0);
229 * Extract the page table entry associated
230 * with the given map/virtual_address pair.
233 pmap_pte(pmap_t pmap, vm_offset_t va)
238 pdeaddr = (pt_entry_t *)pmap_segmap(pmap, va);
240 return pdeaddr + vad_to_pte_offset(va);
243 return ((pt_entry_t *)0);
248 pmap_steal_memory(vm_size_t size)
253 size = round_page(size);
255 bank_size = phys_avail[1] - phys_avail[0];
256 while (size > bank_size) {
259 for (i = 0; phys_avail[i + 2]; i += 2) {
260 phys_avail[i] = phys_avail[i + 2];
261 phys_avail[i + 1] = phys_avail[i + 3];
264 phys_avail[i + 1] = 0;
266 panic("pmap_steal_memory: out of memory");
267 bank_size = phys_avail[1] - phys_avail[0];
271 phys_avail[0] += size;
272 if (pa >= MIPS_KSEG0_LARGEST_PHYS) {
273 panic("Out of memory below 512Meg?");
275 va = MIPS_PHYS_TO_CACHED(pa);
276 bzero((caddr_t)va, size);
281 * Bootstrap the system enough to run with virtual memory. This
282 * assumes that the phys_avail array has been initialized.
290 int memory_larger_than_512meg = 0;
294 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
295 if (phys_avail[i + 1] >= MIPS_KSEG0_LARGEST_PHYS) {
296 memory_larger_than_512meg++;
300 if (phys_avail[i - 2] > phys_avail[i]) {
304 ptemp[0] = phys_avail[i + 0];
305 ptemp[1] = phys_avail[i + 1];
307 phys_avail[i + 0] = phys_avail[i - 2];
308 phys_avail[i + 1] = phys_avail[i - 1];
310 phys_avail[i - 2] = ptemp[0];
311 phys_avail[i - 1] = ptemp[1];
317 printf("Physical memory chunk(s):\n");
318 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
321 size = phys_avail[i + 1] - phys_avail[i];
322 printf("%#08jx - %#08jx, %ju bytes (%ju pages)\n",
323 (uintmax_t) phys_avail[i],
324 (uintmax_t) phys_avail[i + 1] - 1,
325 (uintmax_t) size, (uintmax_t) size / PAGE_SIZE);
329 * Steal the message buffer from the beginning of memory.
331 msgbufp = (struct msgbuf *)pmap_steal_memory(MSGBUF_SIZE);
332 msgbufinit(msgbufp, MSGBUF_SIZE);
334 /* Steal memory for the dynamic per-cpu area. */
335 dpcpu_init((void *)pmap_steal_memory(DPCPU_SIZE), 0);
338 * Steal thread0 kstack.
340 kstack0 = pmap_steal_memory(KSTACK_PAGES << PAGE_SHIFT);
343 virtual_avail = VM_MIN_KERNEL_ADDRESS + VM_KERNEL_ALLOC_OFFSET;
344 virtual_end = VM_MAX_KERNEL_ADDRESS;
347 * Steal some virtual space that will not be in kernel_segmap. This
348 * va memory space will be used to map in kernel pages that are
349 * outside the 512Meg region. Note that we only do this steal when
350 * we do have memory in this region, that way for systems with
351 * smaller memory we don't "steal" any va ranges :-)
353 if (memory_larger_than_512meg) {
354 for (i = 0; i < MAXCPU; i++) {
355 sysmap_lmem[i].CMAP1 = PTE_G;
356 sysmap_lmem[i].CMAP2 = PTE_G;
357 sysmap_lmem[i].CADDR1 = (caddr_t)virtual_avail;
358 virtual_avail += PAGE_SIZE;
359 sysmap_lmem[i].CADDR2 = (caddr_t)virtual_avail;
360 virtual_avail += PAGE_SIZE;
361 sysmap_lmem[i].valid1 = sysmap_lmem[i].valid2 = 0;
362 PMAP_LGMEM_LOCK_INIT(&sysmap_lmem[i]);
365 virtual_sys_start = (caddr_t)virtual_avail;
367 * Allocate segment table for the kernel
369 kernel_segmap = (pd_entry_t *)pmap_steal_memory(PAGE_SIZE);
372 * Allocate second level page tables for the kernel
375 if (memory_larger_than_512meg) {
377 * If we have a large memory system we CANNOT afford to hit
378 * pmap_growkernel() and allocate memory. Since we MAY end
379 * up with a page that is NOT mappable. For that reason we
380 * up front grab more. Normall NKPT is 120 (YMMV see pmap.h)
381 * this gives us 480meg of kernel virtual addresses at the
382 * cost of 120 pages (each page gets us 4 Meg). Since the
383 * kernel starts at virtual_avail, we can use this to
384 * calculate how many entris are left from there to the end
385 * of the segmap, we want to allocate all of it, which would
386 * be somewhere above 0xC0000000 - 0xFFFFFFFF which results
387 * in about 256 entries or so instead of the 120.
389 nkpt = (PAGE_SIZE / sizeof(pd_entry_t)) - (virtual_avail >> SEGSHIFT);
391 pgtab = (pt_entry_t *)pmap_steal_memory(PAGE_SIZE * nkpt);
394 * The R[4-7]?00 stores only one copy of the Global bit in the
395 * translation lookaside buffer for each 2 page entry. Thus invalid
396 * entrys must have the Global bit set so when Entry LO and Entry HI
397 * G bits are anded together they will produce a global bit to store
400 for (i = 0, pte = pgtab; i < (nkpt * NPTEPG); i++, pte++)
403 printf("Va=0x%x Ve=%x\n", virtual_avail, virtual_end);
405 * The segment table contains the KVA of the pages in the second
408 printf("init kernel_segmap va >> = %d nkpt:%d\n",
409 (virtual_avail >> SEGSHIFT),
411 for (i = 0, j = (virtual_avail >> SEGSHIFT); i < nkpt; i++, j++)
412 kernel_segmap[j] = (pd_entry_t)(pgtab + (i * NPTEPG));
414 for (i = 0; phys_avail[i + 2]; i += 2)
416 printf("avail_start:0x%x avail_end:0x%x\n",
417 phys_avail[0], phys_avail[i + 1]);
420 * The kernel's pmap is statically allocated so we don't have to use
421 * pmap_create, which is unlikely to work correctly at this part of
422 * the boot sequence (XXX and which no longer exists).
424 PMAP_LOCK_INIT(kernel_pmap);
425 kernel_pmap->pm_segtab = kernel_segmap;
426 kernel_pmap->pm_active = ~0;
427 TAILQ_INIT(&kernel_pmap->pm_pvlist);
428 kernel_pmap->pm_asid[PCPU_GET(cpuid)].asid = PMAP_ASID_RESERVED;
429 kernel_pmap->pm_asid[PCPU_GET(cpuid)].gen = 0;
430 pmap_max_asid = VMNUM_PIDS;
435 * Initialize a vm_page's machine-dependent fields.
438 pmap_page_init(vm_page_t m)
441 TAILQ_INIT(&m->md.pv_list);
442 m->md.pv_list_count = 0;
447 * Initialize the pmap module.
448 * Called by vm_init, to initialize any structures that the pmap
449 * system needs to map virtual memory.
450 * pmap_init has been enhanced to support in a fairly consistant
451 * way, discontiguous physical memory.
457 if (need_wired_tlb_page_pool)
460 * Initialize the address space (zone) for the pv entries. Set a
461 * high water mark so that the system can recover from excessive
462 * numbers of pv entries.
464 pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
465 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
466 pv_entry_max = PMAP_SHPGPERPROC * maxproc + cnt.v_page_count;
467 pv_entry_high_water = 9 * (pv_entry_max / 10);
468 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
471 /***************************************************
472 * Low level helper routines.....
473 ***************************************************/
475 #if defined(PMAP_DIAGNOSTIC)
478 * This code checks for non-writeable/modified pages.
479 * This should be an invalid condition.
482 pmap_nw_modified(pt_entry_t pte)
484 if ((pte & (PTE_M | PTE_RO)) == (PTE_M | PTE_RO))
493 pmap_invalidate_all(pmap_t pmap)
496 smp_rendezvous(0, pmap_invalidate_all_action, 0, (void *)pmap);
500 pmap_invalidate_all_action(void *arg)
502 pmap_t pmap = (pmap_t)arg;
506 if (pmap->pm_active & PCPU_GET(cpumask)) {
507 pmap_TLB_invalidate_all();
509 pmap->pm_asid[PCPU_GET(cpuid)].gen = 0;
512 struct pmap_invalidate_page_arg {
518 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
521 struct pmap_invalidate_page_arg arg;
526 smp_rendezvous(0, pmap_invalidate_page_action, 0, (void *)&arg);
530 pmap_invalidate_page_action(void *arg)
532 pmap_t pmap = ((struct pmap_invalidate_page_arg *)arg)->pmap;
533 vm_offset_t va = ((struct pmap_invalidate_page_arg *)arg)->va;
537 if (is_kernel_pmap(pmap)) {
538 pmap_TLB_invalidate_kernel(va);
541 if (pmap->pm_asid[PCPU_GET(cpuid)].gen != PCPU_GET(asid_generation))
543 else if (!(pmap->pm_active & PCPU_GET(cpumask))) {
544 pmap->pm_asid[PCPU_GET(cpuid)].gen = 0;
547 va = pmap_va_asid(pmap, (va & ~PGOFSET));
552 pmap_TLB_invalidate_kernel(vm_offset_t va)
557 va = va | (pid << VMTLB_PID_SHIFT);
561 struct pmap_update_page_arg {
568 pmap_update_page(pmap_t pmap, vm_offset_t va, pt_entry_t pte)
571 struct pmap_update_page_arg arg;
577 smp_rendezvous(0, pmap_update_page_action, 0, (void *)&arg);
581 pmap_update_page_action(void *arg)
583 pmap_t pmap = ((struct pmap_update_page_arg *)arg)->pmap;
584 vm_offset_t va = ((struct pmap_update_page_arg *)arg)->va;
585 pt_entry_t pte = ((struct pmap_update_page_arg *)arg)->pte;
588 if (is_kernel_pmap(pmap)) {
589 pmap_TLB_update_kernel(va, pte);
592 if (pmap->pm_asid[PCPU_GET(cpuid)].gen != PCPU_GET(asid_generation))
594 else if (!(pmap->pm_active & PCPU_GET(cpumask))) {
595 pmap->pm_asid[PCPU_GET(cpuid)].gen = 0;
598 va = pmap_va_asid(pmap, va);
599 MachTLBUpdate(va, pte);
603 pmap_TLB_update_kernel(vm_offset_t va, pt_entry_t pte)
608 va = va | (pid << VMTLB_PID_SHIFT);
610 MachTLBUpdate(va, pte);
614 * Routine: pmap_extract
616 * Extract the physical page address associated
617 * with the given map/virtual_address pair.
620 pmap_extract(pmap_t pmap, vm_offset_t va)
623 vm_offset_t retval = 0;
626 pte = pmap_pte(pmap, va);
628 retval = mips_tlbpfn_to_paddr(*pte) | (va & PAGE_MASK);
635 * Routine: pmap_extract_and_hold
637 * Atomically extract and hold the physical page
638 * with the given pmap and virtual address pair
639 * if that mapping permits the given protection.
642 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
648 vm_page_lock_queues();
651 pte = *pmap_pte(pmap, va);
652 if (pte != 0 && pmap_pte_v(&pte) &&
653 ((pte & PTE_RW) || (prot & VM_PROT_WRITE) == 0)) {
654 m = PHYS_TO_VM_PAGE(mips_tlbpfn_to_paddr(pte));
657 vm_page_unlock_queues();
662 /***************************************************
663 * Low level mapping routines.....
664 ***************************************************/
667 * add a wired page to the kva
669 /* PMAP_INLINE */ void
670 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
672 register pt_entry_t *pte;
673 pt_entry_t npte, opte;
676 printf("pmap_kenter: va: 0x%08x -> pa: 0x%08x\n", va, pa);
678 npte = mips_paddr_to_tlbpfn(pa) | PTE_RW | PTE_V | PTE_G | PTE_W;
680 if (is_cacheable_mem(pa))
683 npte |= PTE_UNCACHED;
685 pte = pmap_pte(kernel_pmap, va);
689 pmap_update_page(kernel_pmap, va, npte);
693 * remove a page from the kernel pagetables
695 /* PMAP_INLINE */ void
696 pmap_kremove(vm_offset_t va)
698 register pt_entry_t *pte;
700 pte = pmap_pte(kernel_pmap, va);
702 pmap_invalidate_page(kernel_pmap, va);
706 * Used to map a range of physical addresses into kernel
707 * virtual address space.
709 * The value passed in '*virt' is a suggested virtual address for
710 * the mapping. Architectures which can support a direct-mapped
711 * physical to virtual region can return the appropriate address
712 * within that region, leaving '*virt' unchanged. Other
713 * architectures should map the pages starting at '*virt' and
714 * update '*virt' with the first usable address after the mapped
718 pmap_map(vm_offset_t *virt, vm_offset_t start, vm_offset_t end, int prot)
723 while (start < end) {
724 pmap_kenter(va, start);
733 * Add a list of wired pages to the kva
734 * this routine is only used for temporary
735 * kernel mappings that do not need to have
736 * page modification or references recorded.
737 * Note that old mappings are simply written
738 * over. The page *must* be wired.
741 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
745 for (i = 0; i < count; i++) {
746 pmap_kenter(va, VM_PAGE_TO_PHYS(m[i]));
752 * this routine jerks page mappings from the
753 * kernel -- it is meant only for temporary mappings.
756 pmap_qremove(vm_offset_t va, int count)
758 while (count-- > 0) {
764 /***************************************************
765 * Page table page management routines.....
766 ***************************************************/
769 * floating pages (FPAGES) management routines
771 * FPAGES are the reserved virtual memory areas which can be
772 * mapped to any physical memory. This gets used typically
773 * in the following functions:
780 * Create the floating pages, aka FPAGES!
787 struct sysmaps *sysmaps;
790 * We allocate a total of (FPAGES*MAXCPU + FPAGES_SHARED + 1) pages
791 * at first. FPAGES & FPAGES_SHARED should be EVEN Then we'll adjust
792 * 'kva' to be even-page aligned so that the fpage area can be wired
793 * in the TLB with a single TLB entry.
795 kva = kmem_alloc_nofault(kernel_map,
796 (FPAGES * MAXCPU + 1 + FPAGES_SHARED) * PAGE_SIZE);
797 if ((void *)kva == NULL)
798 panic("pmap_init_fpage: fpage allocation failed");
801 * Make up start at an even page number so we can wire down the
802 * fpage area in the tlb with a single tlb entry.
804 if ((((vm_offset_t)kva) >> PGSHIFT) & 1) {
806 * 'kva' is not even-page aligned. Adjust it and free the
807 * first page which is unused.
809 kmem_free(kernel_map, (vm_offset_t)kva, NBPG);
810 kva = ((vm_offset_t)kva) + NBPG;
813 * 'kva' is even page aligned. We don't need the last page,
816 kmem_free(kernel_map, ((vm_offset_t)kva) + FSPACE, NBPG);
819 for (i = 0; i < MAXCPU; i++) {
820 sysmaps = &sysmaps_pcpu[i];
821 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
823 /* Assign FPAGES pages to the CPU */
824 for (j = 0; j < FPAGES; j++)
825 sysmaps->fp[j].kva = kva + (j) * PAGE_SIZE;
826 kva = ((vm_offset_t)kva) + (FPAGES * PAGE_SIZE);
830 * An additional 2 pages are needed, one for pmap_zero_page_idle()
831 * and one for coredump. These pages are shared by all cpu's
833 fpages_shared[PMAP_FPAGE3].kva = kva;
834 fpages_shared[PMAP_FPAGE_KENTER_TEMP].kva = kva + PAGE_SIZE;
838 * Map the page to the fpage virtual address as specified thru' fpage id
841 pmap_map_fpage(vm_paddr_t pa, struct fpage *fp, boolean_t check_unmaped)
844 register pt_entry_t *pte;
847 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
849 * Check if the fpage is free
852 if (check_unmaped == TRUE)
853 pmap_unmap_fpage(pa, fp);
855 panic("pmap_map_fpage: fpage is busy");
860 npte = mips_paddr_to_tlbpfn(pa) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
861 pte = pmap_pte(kernel_pmap, kva);
864 pmap_TLB_update_kernel(kva, npte);
870 * Unmap the page from the fpage virtual address as specified thru' fpage id
873 pmap_unmap_fpage(vm_paddr_t pa, struct fpage *fp)
876 register pt_entry_t *pte;
878 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
880 * Check if the fpage is busy
883 panic("pmap_unmap_fpage: fpage is free");
887 pte = pmap_pte(kernel_pmap, kva);
889 pmap_TLB_invalidate_kernel(kva);
894 * Should there be any flush operation at the end?
900 * Simplify the reference counting of page table pages. Specifically, use
901 * the page table page's wired count rather than its hold count to contain
902 * the reference count.
906 * This routine unholds page table pages, and if the hold count
907 * drops to zero, then it decrements the wire count.
910 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
914 * unmap the page table page
916 pmap->pm_segtab[m->pindex] = 0;
917 --pmap->pm_stats.resident_count;
919 if (pmap->pm_ptphint == m)
920 pmap->pm_ptphint = NULL;
923 * If the page is finally unwired, simply free it.
925 vm_page_free_zero(m);
926 atomic_subtract_int(&cnt.v_wire_count, 1);
930 static PMAP_INLINE int
931 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
934 if (m->wire_count == 0)
935 return (_pmap_unwire_pte_hold(pmap, m));
941 * After removing a page table entry, this routine is used to
942 * conditionally free the page, and manage the hold/wire counts.
945 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
950 if (va >= VM_MAXUSER_ADDRESS)
954 ptepindex = (va >> SEGSHIFT);
955 if (pmap->pm_ptphint &&
956 (pmap->pm_ptphint->pindex == ptepindex)) {
957 mpte = pmap->pm_ptphint;
959 pteva = *pmap_pde(pmap, va);
960 mpte = PHYS_TO_VM_PAGE(MIPS_CACHED_TO_PHYS(pteva));
961 pmap->pm_ptphint = mpte;
964 return pmap_unwire_pte_hold(pmap, mpte);
968 pmap_pinit0(pmap_t pmap)
972 PMAP_LOCK_INIT(pmap);
973 pmap->pm_segtab = kernel_segmap;
975 pmap->pm_ptphint = NULL;
976 for (i = 0; i < MAXCPU; i++) {
977 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
978 pmap->pm_asid[i].gen = 0;
980 PCPU_SET(curpmap, pmap);
981 TAILQ_INIT(&pmap->pm_pvlist);
982 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
986 * Initialize a preallocated and zeroed pmap structure,
987 * such as one in a vmspace structure.
990 pmap_pinit(pmap_t pmap)
996 PMAP_LOCK_INIT(pmap);
998 req = VM_ALLOC_NOOBJ | VM_ALLOC_NORMAL | VM_ALLOC_WIRED |
1001 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
1002 if (need_wired_tlb_page_pool)
1003 req |= VM_ALLOC_WIRED_TLB_PG_POOL;
1006 * allocate the page directory page
1008 while ((ptdpg = vm_page_alloc(NULL, NUSERPGTBLS, req)) == NULL)
1011 ptdpg->valid = VM_PAGE_BITS_ALL;
1013 pmap->pm_segtab = (pd_entry_t *)
1014 MIPS_PHYS_TO_CACHED(VM_PAGE_TO_PHYS(ptdpg));
1015 if ((ptdpg->flags & PG_ZERO) == 0)
1016 bzero(pmap->pm_segtab, PAGE_SIZE);
1018 pmap->pm_active = 0;
1019 pmap->pm_ptphint = NULL;
1020 for (i = 0; i < MAXCPU; i++) {
1021 pmap->pm_asid[i].asid = PMAP_ASID_RESERVED;
1022 pmap->pm_asid[i].gen = 0;
1024 TAILQ_INIT(&pmap->pm_pvlist);
1025 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1031 * this routine is called if the page table page is not
1035 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags)
1037 vm_offset_t pteva, ptepa;
1041 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1042 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1043 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1045 req = VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_NOOBJ;
1046 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
1047 if (need_wired_tlb_page_pool)
1048 req |= VM_ALLOC_WIRED_TLB_PG_POOL;
1051 * Find or fabricate a new pagetable page
1053 if ((m = vm_page_alloc(NULL, ptepindex, req)) == NULL) {
1054 if (flags & M_WAITOK) {
1056 vm_page_unlock_queues();
1058 vm_page_lock_queues();
1062 * Indicate the need to retry. While waiting, the page
1063 * table page may have been allocated.
1067 if ((m->flags & PG_ZERO) == 0)
1070 KASSERT(m->queue == PQ_NONE,
1071 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1074 * Map the pagetable page into the process address space, if it
1075 * isn't already there.
1078 pmap->pm_stats.resident_count++;
1080 ptepa = VM_PAGE_TO_PHYS(m);
1081 pteva = MIPS_PHYS_TO_CACHED(ptepa);
1082 pmap->pm_segtab[ptepindex] = (pd_entry_t)pteva;
1085 * Set the page table hint
1087 pmap->pm_ptphint = m;
1090 * Kernel page tables are allocated in pmap_bootstrap() or
1091 * pmap_growkernel().
1093 if (is_kernel_pmap(pmap))
1094 panic("_pmap_allocpte() called for kernel pmap\n");
1096 m->valid = VM_PAGE_BITS_ALL;
1097 vm_page_flag_clear(m, PG_ZERO);
1103 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1109 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1110 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1111 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1114 * Calculate pagetable page index
1116 ptepindex = va >> SEGSHIFT;
1119 * Get the page directory entry
1121 pteva = (vm_offset_t)pmap->pm_segtab[ptepindex];
1124 * If the page table page is mapped, we just increment the hold
1125 * count, and activate it.
1129 * In order to get the page table page, try the hint first.
1131 if (pmap->pm_ptphint &&
1132 (pmap->pm_ptphint->pindex == ptepindex)) {
1133 m = pmap->pm_ptphint;
1135 m = PHYS_TO_VM_PAGE(MIPS_CACHED_TO_PHYS(pteva));
1136 pmap->pm_ptphint = m;
1141 * Here if the pte page isn't mapped, or if it has been
1144 m = _pmap_allocpte(pmap, ptepindex, flags);
1145 if (m == NULL && (flags & M_WAITOK))
1152 /***************************************************
1153 * Pmap allocation/deallocation routines.
1154 ***************************************************/
1157 * - Merged pmap_release and pmap_release_free_page. When pmap_release is
1158 * called only the page directory page(s) can be left in the pmap pte
1159 * object, since all page table pages will have been freed by
1160 * pmap_remove_pages and pmap_remove. In addition, there can only be one
1161 * reference to the pmap and the page directory is wired, so the page(s)
1162 * can never be busy. So all there is to do is clear the magic mappings
1163 * from the page directory and free the page(s).
1168 * Release any resources held by the given physical map.
1169 * Called when a pmap initialized by pmap_pinit is being released.
1170 * Should only be called if the map contains no valid mappings.
1173 pmap_release(pmap_t pmap)
1177 KASSERT(pmap->pm_stats.resident_count == 0,
1178 ("pmap_release: pmap resident count %ld != 0",
1179 pmap->pm_stats.resident_count));
1181 ptdpg = PHYS_TO_VM_PAGE(MIPS_CACHED_TO_PHYS(pmap->pm_segtab));
1182 ptdpg->wire_count--;
1183 atomic_subtract_int(&cnt.v_wire_count, 1);
1184 vm_page_free_zero(ptdpg);
1188 * grow the number of kernel page table entries, if needed
1191 pmap_growkernel(vm_offset_t addr)
1193 vm_offset_t ptppaddr;
1198 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1199 if (kernel_vm_end == 0) {
1200 kernel_vm_end = VM_MIN_KERNEL_ADDRESS + VM_KERNEL_ALLOC_OFFSET;
1202 while (segtab_pde(kernel_segmap, kernel_vm_end)) {
1203 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1204 ~(PAGE_SIZE * NPTEPG - 1);
1206 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1207 kernel_vm_end = kernel_map->max_offset;
1212 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1213 if (addr - 1 >= kernel_map->max_offset)
1214 addr = kernel_map->max_offset;
1215 while (kernel_vm_end < addr) {
1216 if (segtab_pde(kernel_segmap, kernel_vm_end)) {
1217 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1218 ~(PAGE_SIZE * NPTEPG - 1);
1219 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1220 kernel_vm_end = kernel_map->max_offset;
1226 * This index is bogus, but out of the way
1228 req = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ;
1229 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
1230 if (need_wired_tlb_page_pool)
1231 req |= VM_ALLOC_WIRED_TLB_PG_POOL;
1233 nkpg = vm_page_alloc(NULL, nkpt, req);
1235 panic("pmap_growkernel: no memory to grow kernel");
1239 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1240 if (ptppaddr >= MIPS_KSEG0_LARGEST_PHYS) {
1242 * We need to do something here, but I am not sure
1243 * what. We can access anything in the 0 - 512Meg
1244 * region, but if we get a page to go in the kernel
1245 * segmap that is outside of of that we really need
1246 * to have another mapping beyond the temporary ones
1247 * I have. Not sure how to do this yet. FIXME FIXME.
1249 panic("Gak, can't handle a k-page table outside of lower 512Meg");
1251 pte = (pt_entry_t *)MIPS_PHYS_TO_CACHED(ptppaddr);
1252 segtab_pde(kernel_segmap, kernel_vm_end) = (pd_entry_t)pte;
1255 * The R[4-7]?00 stores only one copy of the Global bit in
1256 * the translation lookaside buffer for each 2 page entry.
1257 * Thus invalid entrys must have the Global bit set so when
1258 * Entry LO and Entry HI G bits are anded together they will
1259 * produce a global bit to store in the tlb.
1261 for (i = 0; i < NPTEPG; i++, pte++)
1264 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1265 ~(PAGE_SIZE * NPTEPG - 1);
1266 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1267 kernel_vm_end = kernel_map->max_offset;
1273 /***************************************************
1274 * page management routines.
1275 ***************************************************/
1278 * free the pv_entry back to the free list
1280 static PMAP_INLINE void
1281 free_pv_entry(pv_entry_t pv)
1285 uma_zfree(pvzone, pv);
1289 * get a new pv_entry, allocating a block from the system
1291 * the memory allocation is performed bypassing the malloc code
1292 * because of the possibility of allocations at interrupt time.
1295 get_pv_entry(pmap_t locked_pmap)
1297 static const struct timeval printinterval = { 60, 0 };
1298 static struct timeval lastprint;
1299 struct vpgqueues *vpq;
1300 pt_entry_t *pte, oldpte;
1302 pv_entry_t allocated_pv, next_pv, pv;
1306 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1307 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1308 allocated_pv = uma_zalloc(pvzone, M_NOWAIT);
1309 if (allocated_pv != NULL) {
1311 if (pv_entry_count > pv_entry_high_water)
1312 pagedaemon_wakeup();
1314 return (allocated_pv);
1317 * Reclaim pv entries: At first, destroy mappings to inactive
1318 * pages. After that, if a pv entry is still needed, destroy
1319 * mappings to active pages.
1321 if (ratecheck(&lastprint, &printinterval))
1322 printf("Approaching the limit on PV entries, "
1323 "increase the vm.pmap.shpgperproc tunable.\n");
1324 vpq = &vm_page_queues[PQ_INACTIVE];
1326 TAILQ_FOREACH(m, &vpq->pl, pageq) {
1327 if (m->hold_count || m->busy)
1329 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
1332 /* Avoid deadlock and lock recursion. */
1333 if (pmap > locked_pmap)
1335 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
1337 pmap->pm_stats.resident_count--;
1338 pte = pmap_pte(pmap, va);
1339 KASSERT(pte != NULL, ("pte"));
1340 oldpte = loadandclear((u_int *)pte);
1341 if (is_kernel_pmap(pmap))
1343 KASSERT((oldpte & PTE_W) == 0,
1344 ("wired pte for unwired page"));
1345 if (m->md.pv_flags & PV_TABLE_REF)
1346 vm_page_flag_set(m, PG_REFERENCED);
1349 pmap_invalidate_page(pmap, va);
1350 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1351 m->md.pv_list_count--;
1352 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1353 if (TAILQ_EMPTY(&m->md.pv_list)) {
1354 vm_page_flag_clear(m, PG_WRITEABLE);
1355 m->md.pv_flags &= ~(PV_TABLE_REF | PV_TABLE_MOD);
1357 pmap_unuse_pt(pmap, va, pv->pv_ptem);
1358 if (pmap != locked_pmap)
1360 if (allocated_pv == NULL)
1366 if (allocated_pv == NULL) {
1367 if (vpq == &vm_page_queues[PQ_INACTIVE]) {
1368 vpq = &vm_page_queues[PQ_ACTIVE];
1371 panic("get_pv_entry: increase the vm.pmap.shpgperproc tunable");
1373 return (allocated_pv);
1379 * Move pmap_collect() out of the machine-dependent code, rename it
1380 * to reflect its new location, and add page queue and flag locking.
1382 * Notes: (1) alpha, i386, and ia64 had identical implementations
1383 * of pmap_collect() in terms of machine-independent interfaces;
1384 * (2) sparc64 doesn't require it; (3) powerpc had it as a TODO.
1386 * MIPS implementation was identical to alpha [Junos 8.2]
1390 * If it is the first entry on the list, it is actually
1391 * in the header and we must copy the following entry up
1392 * to the header. Otherwise we must search the list for
1393 * the entry. In either case we free the now unused entry.
1397 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1401 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1402 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1403 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1404 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1405 if (pmap == pv->pv_pmap && va == pv->pv_va)
1409 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1410 if (va == pv->pv_va)
1415 KASSERT(pv != NULL, ("pmap_remove_entry: pv not found"));
1416 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1417 m->md.pv_list_count--;
1418 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1419 vm_page_flag_clear(m, PG_WRITEABLE);
1421 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1426 * Create a pv entry for page at pa for
1430 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m,
1435 pv = get_pv_entry(pmap);
1439 pv->pv_wired = wired;
1441 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1442 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1443 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1444 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1445 m->md.pv_list_count++;
1449 * Conditionally create a pv entry.
1452 pmap_try_insert_pv_entry(pmap_t pmap, vm_page_t mpte, vm_offset_t va,
1457 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1458 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1459 if (pv_entry_count < pv_entry_high_water &&
1460 (pv = uma_zalloc(pvzone, M_NOWAIT)) != NULL) {
1465 pv->pv_wired = FALSE;
1466 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1467 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1468 m->md.pv_list_count++;
1475 * pmap_remove_pte: do the things to unmap a page in a process
1478 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1484 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1485 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1487 oldpte = loadandclear((u_int *)ptq);
1488 if (is_kernel_pmap(pmap))
1492 pmap->pm_stats.wired_count -= 1;
1494 pmap->pm_stats.resident_count -= 1;
1495 pa = mips_tlbpfn_to_paddr(oldpte);
1497 if (page_is_managed(pa)) {
1498 m = PHYS_TO_VM_PAGE(pa);
1499 if (oldpte & PTE_M) {
1500 #if defined(PMAP_DIAGNOSTIC)
1501 if (pmap_nw_modified(oldpte)) {
1503 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1509 if (m->md.pv_flags & PV_TABLE_REF)
1510 vm_page_flag_set(m, PG_REFERENCED);
1511 m->md.pv_flags &= ~(PV_TABLE_REF | PV_TABLE_MOD);
1513 pmap_remove_entry(pmap, m, va);
1515 return pmap_unuse_pt(pmap, va, NULL);
1519 * Remove a single page from a process address space
1522 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1524 register pt_entry_t *ptq;
1526 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1527 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1528 ptq = pmap_pte(pmap, va);
1531 * if there is no pte for this address, just skip it!!!
1533 if (!ptq || !pmap_pte_v(ptq)) {
1537 * get a local va for mappings for this pmap.
1539 (void)pmap_remove_pte(pmap, ptq, va);
1540 pmap_invalidate_page(pmap, va);
1546 * Remove the given range of addresses from the specified map.
1548 * It is assumed that the start and end are properly
1549 * rounded to the page size.
1552 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1554 vm_offset_t va, nva;
1559 if (pmap->pm_stats.resident_count == 0)
1562 vm_page_lock_queues();
1566 * special handling of removing one page. a very common operation
1567 * and easy to short circuit some code.
1569 if ((sva + PAGE_SIZE) == eva) {
1570 pmap_remove_page(pmap, sva);
1573 for (va = sva; va < eva; va = nva) {
1574 if (!*pmap_pde(pmap, va)) {
1575 nva = mips_segtrunc(va + MIPS_SEGSIZE);
1578 pmap_remove_page(pmap, va);
1579 nva = va + PAGE_SIZE;
1583 vm_page_unlock_queues();
1588 * Routine: pmap_remove_all
1590 * Removes this physical page from
1591 * all physical maps in which it resides.
1592 * Reflects back modify bits to the pager.
1595 * Original versions of this routine were very
1596 * inefficient because they iteratively called
1597 * pmap_remove (slow...)
1601 pmap_remove_all(vm_page_t m)
1603 register pv_entry_t pv;
1604 register pt_entry_t *pte, tpte;
1606 KASSERT((m->flags & PG_FICTITIOUS) == 0,
1607 ("pmap_remove_all: page %p is fictitious", m));
1608 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1610 if (m->md.pv_flags & PV_TABLE_REF)
1611 vm_page_flag_set(m, PG_REFERENCED);
1613 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1614 PMAP_LOCK(pv->pv_pmap);
1615 pv->pv_pmap->pm_stats.resident_count--;
1617 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1619 tpte = loadandclear((u_int *)pte);
1620 if (is_kernel_pmap(pv->pv_pmap))
1624 pv->pv_pmap->pm_stats.wired_count--;
1627 * Update the vm_page_t clean and reference bits.
1630 #if defined(PMAP_DIAGNOSTIC)
1631 if (pmap_nw_modified(tpte)) {
1633 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1639 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
1641 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1642 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1643 m->md.pv_list_count--;
1644 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1645 PMAP_UNLOCK(pv->pv_pmap);
1649 vm_page_flag_clear(m, PG_WRITEABLE);
1650 m->md.pv_flags &= ~(PV_TABLE_REF | PV_TABLE_MOD);
1654 * Set the physical protection on the
1655 * specified range of this map as requested.
1658 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1665 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1666 pmap_remove(pmap, sva, eva);
1669 if (prot & VM_PROT_WRITE)
1672 vm_page_lock_queues();
1675 pt_entry_t pbits, obits;
1680 * If segment table entry is empty, skip this segment.
1682 if (!*pmap_pde(pmap, sva)) {
1683 sva = mips_segtrunc(sva + MIPS_SEGSIZE);
1687 * If pte is invalid, skip this page
1689 pte = pmap_pte(pmap, sva);
1690 if (!pmap_pte_v(pte)) {
1695 obits = pbits = *pte;
1696 pa = mips_tlbpfn_to_paddr(pbits);
1698 if (page_is_managed(pa)) {
1699 m = PHYS_TO_VM_PAGE(pa);
1700 if (m->md.pv_flags & PV_TABLE_REF) {
1701 vm_page_flag_set(m, PG_REFERENCED);
1702 m->md.pv_flags &= ~PV_TABLE_REF;
1704 if (pbits & PTE_M) {
1706 m->md.pv_flags &= ~PV_TABLE_MOD;
1709 pbits = (pbits & ~PTE_M) | PTE_RO;
1711 if (pbits != *pte) {
1712 if (!atomic_cmpset_int((u_int *)pte, obits, pbits))
1714 pmap_update_page(pmap, sva, pbits);
1718 vm_page_unlock_queues();
1723 * Insert the given physical page (p) at
1724 * the specified virtual address (v) in the
1725 * target physical map with the protection requested.
1727 * If specified, the page will be wired down, meaning
1728 * that the related pte can not be reclaimed.
1730 * NB: This is the only routine which MAY NOT lazy-evaluate
1731 * or lose information. That is, this routine must actually
1732 * insert this page into the given map NOW.
1735 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
1736 vm_prot_t prot, boolean_t wired)
1738 vm_offset_t pa, opa;
1739 register pt_entry_t *pte;
1740 pt_entry_t origpte, newpte;
1748 #ifdef PMAP_DIAGNOSTIC
1749 if (va > VM_MAX_KERNEL_ADDRESS)
1750 panic("pmap_enter: toobig");
1755 vm_page_lock_queues();
1759 * In the case that a page table page is not resident, we are
1762 if (va < VM_MAXUSER_ADDRESS) {
1763 mpte = pmap_allocpte(pmap, va, M_WAITOK);
1765 pte = pmap_pte(pmap, va);
1768 * Page Directory table entry not valid, we need a new PT page
1771 panic("pmap_enter: invalid page directory, pdir=%p, va=0x%x\n",
1772 (void *)pmap->pm_segtab, va);
1774 pa = VM_PAGE_TO_PHYS(m);
1777 opa = mips_tlbpfn_to_paddr(origpte);
1780 * Mapping has not changed, must be protection or wiring change.
1782 if ((origpte & PTE_V) && (opa == pa)) {
1784 * Wiring change, just update stats. We don't worry about
1785 * wiring PT pages as they remain resident as long as there
1786 * are valid mappings in them. Hence, if a user page is
1787 * wired, the PT page will be also.
1789 if (wired && ((origpte & PTE_W) == 0))
1790 pmap->pm_stats.wired_count++;
1791 else if (!wired && (origpte & PTE_W))
1792 pmap->pm_stats.wired_count--;
1794 #if defined(PMAP_DIAGNOSTIC)
1795 if (pmap_nw_modified(origpte)) {
1797 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
1803 * Remove extra pte reference
1809 * We might be turning off write access to the page, so we
1810 * go ahead and sense modify status.
1812 if (page_is_managed(opa)) {
1818 * Mapping has changed, invalidate old range and fall through to
1819 * handle validating new mapping.
1822 if (origpte & PTE_W)
1823 pmap->pm_stats.wired_count--;
1825 if (page_is_managed(opa)) {
1826 om = PHYS_TO_VM_PAGE(opa);
1827 pmap_remove_entry(pmap, om, va);
1831 KASSERT(mpte->wire_count > 0,
1832 ("pmap_enter: missing reference to page table page,"
1836 pmap->pm_stats.resident_count++;
1839 * Enter on the PV list if part of our managed memory. Note that we
1840 * raise IPL while manipulating pv_table since pmap_enter can be
1841 * called at interrupt time.
1843 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
1844 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
1845 ("pmap_enter: managed mapping within the clean submap"));
1846 pmap_insert_entry(pmap, va, mpte, m, wired);
1849 * Increment counters
1852 pmap->pm_stats.wired_count++;
1855 if ((access & VM_PROT_WRITE) != 0)
1856 m->md.pv_flags |= PV_TABLE_MOD | PV_TABLE_REF;
1857 rw = init_pte_prot(va, m, prot);
1860 printf("pmap_enter: va: 0x%08x -> pa: 0x%08x\n", va, pa);
1863 * Now validate mapping with desired protection/wiring.
1865 newpte = mips_paddr_to_tlbpfn(pa) | rw | PTE_V;
1867 if (is_cacheable_mem(pa))
1868 newpte |= PTE_CACHE;
1870 newpte |= PTE_UNCACHED;
1875 if (is_kernel_pmap(pmap)) {
1880 * if the mapping or permission bits are different, we need to
1883 if (origpte != newpte) {
1884 if (origpte & PTE_V) {
1886 if (page_is_managed(opa) && (opa != pa)) {
1887 if (om->md.pv_flags & PV_TABLE_REF)
1888 vm_page_flag_set(om, PG_REFERENCED);
1890 ~(PV_TABLE_REF | PV_TABLE_MOD);
1892 if (origpte & PTE_M) {
1893 KASSERT((origpte & PTE_RW),
1894 ("pmap_enter: modified page not writable:"
1895 " va: 0x%x, pte: 0x%lx", va, origpte));
1896 if (page_is_managed(opa))
1903 pmap_update_page(pmap, va, newpte);
1906 * Sync I & D caches for executable pages. Do this only if the the
1907 * target pmap belongs to the current process. Otherwise, an
1908 * unresolvable TLB miss may occur.
1910 if (!is_kernel_pmap(pmap) && (pmap == &curproc->p_vmspace->vm_pmap) &&
1911 (prot & VM_PROT_EXECUTE)) {
1912 mips_icache_sync_range(va, NBPG);
1913 mips_dcache_wbinv_range(va, NBPG);
1915 vm_page_unlock_queues();
1920 * this code makes some *MAJOR* assumptions:
1921 * 1. Current pmap & pmap exists.
1924 * 4. No page table pages.
1925 * but is *MUCH* faster than pmap_enter...
1929 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
1933 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
1938 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
1939 vm_prot_t prot, vm_page_t mpte)
1944 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
1945 (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0,
1946 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
1947 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1948 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1951 * In the case that a page table page is not resident, we are
1954 if (va < VM_MAXUSER_ADDRESS) {
1959 * Calculate pagetable page index
1961 ptepindex = va >> SEGSHIFT;
1962 if (mpte && (mpte->pindex == ptepindex)) {
1966 * Get the page directory entry
1968 pteva = (vm_offset_t)pmap->pm_segtab[ptepindex];
1971 * If the page table page is mapped, we just
1972 * increment the hold count, and activate it.
1975 if (pmap->pm_ptphint &&
1976 (pmap->pm_ptphint->pindex == ptepindex)) {
1977 mpte = pmap->pm_ptphint;
1979 mpte = PHYS_TO_VM_PAGE(MIPS_CACHED_TO_PHYS(pteva));
1980 pmap->pm_ptphint = mpte;
1984 mpte = _pmap_allocpte(pmap, ptepindex,
1994 pte = pmap_pte(pmap, va);
1995 if (pmap_pte_v(pte)) {
2004 * Enter on the PV list if part of our managed memory.
2006 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0 &&
2007 !pmap_try_insert_pv_entry(pmap, mpte, va, m)) {
2009 pmap_unwire_pte_hold(pmap, mpte);
2016 * Increment counters
2018 pmap->pm_stats.resident_count++;
2020 pa = VM_PAGE_TO_PHYS(m);
2023 * Now validate mapping with RO protection
2025 *pte = mips_paddr_to_tlbpfn(pa) | PTE_V;
2027 if (is_cacheable_mem(pa))
2030 *pte |= PTE_UNCACHED;
2032 if (is_kernel_pmap(pmap))
2037 * Sync I & D caches. Do this only if the the target pmap
2038 * belongs to the current process. Otherwise, an
2039 * unresolvable TLB miss may occur. */
2040 if (pmap == &curproc->p_vmspace->vm_pmap) {
2042 mips_icache_sync_range(va, NBPG);
2043 mips_dcache_wbinv_range(va, NBPG);
2050 * Make a temporary mapping for a physical address. This is only intended
2051 * to be used for panic dumps.
2054 pmap_kenter_temporary(vm_paddr_t pa, int i)
2059 printf("%s: ERROR!!! More than one page of virtual address mapping not supported\n",
2062 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
2063 if (need_wired_tlb_page_pool) {
2064 va = pmap_map_fpage(pa, &fpages_shared[PMAP_FPAGE_KENTER_TEMP],
2068 if (pa < MIPS_KSEG0_LARGEST_PHYS) {
2069 va = MIPS_PHYS_TO_CACHED(pa);
2072 struct local_sysmaps *sysm;
2074 cpu = PCPU_GET(cpuid);
2075 sysm = &sysmap_lmem[cpu];
2076 /* Since this is for the debugger, no locks or any other fun */
2077 sysm->CMAP1 = mips_paddr_to_tlbpfn(pa) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2078 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR1, sysm->CMAP1);
2080 va = (vm_offset_t)sysm->CADDR1;
2082 return ((void *)va);
2086 pmap_kenter_temporary_free(vm_paddr_t pa)
2089 struct local_sysmaps *sysm;
2091 if (pa < MIPS_KSEG0_LARGEST_PHYS) {
2092 /* nothing to do for this case */
2095 cpu = PCPU_GET(cpuid);
2096 sysm = &sysmap_lmem[cpu];
2098 pmap_TLB_invalidate_kernel((vm_offset_t)sysm->CADDR1);
2105 * Moved the code to Machine Independent
2106 * vm_map_pmap_enter()
2110 * Maps a sequence of resident pages belonging to the same object.
2111 * The sequence begins with the given page m_start. This page is
2112 * mapped at the given virtual address start. Each subsequent page is
2113 * mapped at a virtual address that is offset from start by the same
2114 * amount as the page is offset from m_start within the object. The
2115 * last page in the sequence is the page with the largest offset from
2116 * m_start that can be mapped at a virtual address less than the given
2117 * virtual address end. Not every virtual page between start and end
2118 * is mapped; only those for which a resident page exists with the
2119 * corresponding offset from m_start are mapped.
2122 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2123 vm_page_t m_start, vm_prot_t prot)
2126 vm_pindex_t diff, psize;
2128 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
2129 psize = atop(end - start);
2133 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2134 mpte = pmap_enter_quick_locked(pmap, start + ptoa(diff), m,
2136 m = TAILQ_NEXT(m, listq);
2142 * pmap_object_init_pt preloads the ptes for a given object
2143 * into the specified pmap. This eliminates the blast of soft
2144 * faults on process startup and immediately after an mmap.
2147 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2148 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2150 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2151 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2152 ("pmap_object_init_pt: non-device object"));
2156 * Routine: pmap_change_wiring
2157 * Function: Change the wiring attribute for a map/virtual-address
2159 * In/out conditions:
2160 * The mapping must already exist in the pmap.
2163 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2165 register pt_entry_t *pte;
2171 pte = pmap_pte(pmap, va);
2173 if (wired && !pmap_pte_w(pte))
2174 pmap->pm_stats.wired_count++;
2175 else if (!wired && pmap_pte_w(pte))
2176 pmap->pm_stats.wired_count--;
2179 * Wiring is not a hardware characteristic so there is no need to
2182 pmap_pte_set_w(pte, wired);
2187 * Copy the range specified by src_addr/len
2188 * from the source map to the range dst_addr/len
2189 * in the destination map.
2191 * This routine is only advisory and need not do anything.
2195 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2196 vm_size_t len, vm_offset_t src_addr)
2201 * pmap_zero_page zeros the specified hardware page by mapping
2202 * the page into KVM and using bzero to clear its contents.
2205 pmap_zero_page(vm_page_t m)
2208 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2210 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
2211 if (need_wired_tlb_page_pool) {
2213 struct sysmaps *sysmaps;
2215 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2216 mtx_lock(&sysmaps->lock);
2219 fp1 = &sysmaps->fp[PMAP_FPAGE1];
2220 va = pmap_map_fpage(phys, fp1, FALSE);
2221 bzero((caddr_t)va, PAGE_SIZE);
2222 pmap_unmap_fpage(phys, fp1);
2224 mtx_unlock(&sysmaps->lock);
2226 * Should you do cache flush?
2230 if (phys < MIPS_KSEG0_LARGEST_PHYS) {
2232 va = MIPS_PHYS_TO_UNCACHED(phys);
2234 bzero((caddr_t)va, PAGE_SIZE);
2235 mips_dcache_wbinv_range(va, PAGE_SIZE);
2238 struct local_sysmaps *sysm;
2240 cpu = PCPU_GET(cpuid);
2241 sysm = &sysmap_lmem[cpu];
2242 PMAP_LGMEM_LOCK(sysm);
2244 sysm->CMAP1 = mips_paddr_to_tlbpfn(phys) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2245 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR1, sysm->CMAP1);
2247 bzero(sysm->CADDR1, PAGE_SIZE);
2248 pmap_TLB_invalidate_kernel((vm_offset_t)sysm->CADDR1);
2252 PMAP_LGMEM_UNLOCK(sysm);
2258 * pmap_zero_page_area zeros the specified hardware page by mapping
2259 * the page into KVM and using bzero to clear its contents.
2261 * off and size may not cover an area beyond a single hardware page.
2264 pmap_zero_page_area(vm_page_t m, int off, int size)
2267 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2269 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
2270 if (need_wired_tlb_page_pool) {
2272 struct sysmaps *sysmaps;
2274 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2275 mtx_lock(&sysmaps->lock);
2278 fp1 = &sysmaps->fp[PMAP_FPAGE1];
2279 va = pmap_map_fpage(phys, fp1, FALSE);
2280 bzero((caddr_t)va + off, size);
2281 pmap_unmap_fpage(phys, fp1);
2284 mtx_unlock(&sysmaps->lock);
2287 if (phys < MIPS_KSEG0_LARGEST_PHYS) {
2288 va = MIPS_PHYS_TO_UNCACHED(phys);
2289 bzero((char *)(caddr_t)va + off, size);
2290 mips_dcache_wbinv_range(va + off, size);
2293 struct local_sysmaps *sysm;
2295 cpu = PCPU_GET(cpuid);
2296 sysm = &sysmap_lmem[cpu];
2297 PMAP_LGMEM_LOCK(sysm);
2299 sysm->CMAP1 = mips_paddr_to_tlbpfn(phys) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2300 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR1, sysm->CMAP1);
2302 bzero((char *)sysm->CADDR1 + off, size);
2303 pmap_TLB_invalidate_kernel((vm_offset_t)sysm->CADDR1);
2307 PMAP_LGMEM_UNLOCK(sysm);
2312 pmap_zero_page_idle(vm_page_t m)
2315 vm_paddr_t phys = VM_PAGE_TO_PHYS(m);
2317 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
2318 if (need_wired_tlb_page_pool) {
2320 va = pmap_map_fpage(phys, &fpages_shared[PMAP_FPAGE3], FALSE);
2321 bzero((caddr_t)va, PAGE_SIZE);
2322 pmap_unmap_fpage(phys, &fpages_shared[PMAP_FPAGE3]);
2326 if (phys < MIPS_KSEG0_LARGEST_PHYS) {
2327 va = MIPS_PHYS_TO_UNCACHED(phys);
2328 bzero((caddr_t)va, PAGE_SIZE);
2329 mips_dcache_wbinv_range(va, PAGE_SIZE);
2332 struct local_sysmaps *sysm;
2334 cpu = PCPU_GET(cpuid);
2335 sysm = &sysmap_lmem[cpu];
2336 PMAP_LGMEM_LOCK(sysm);
2338 sysm->CMAP1 = mips_paddr_to_tlbpfn(phys) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2339 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR1, sysm->CMAP1);
2341 bzero(sysm->CADDR1, PAGE_SIZE);
2342 pmap_TLB_invalidate_kernel((vm_offset_t)sysm->CADDR1);
2346 PMAP_LGMEM_UNLOCK(sysm);
2352 * pmap_copy_page copies the specified (machine independent)
2353 * page by mapping the page into virtual memory and using
2354 * bcopy to copy the page, one machine dependent page at a
2358 pmap_copy_page(vm_page_t src, vm_page_t dst)
2360 vm_offset_t va_src, va_dst;
2361 vm_paddr_t phy_src = VM_PAGE_TO_PHYS(src);
2362 vm_paddr_t phy_dst = VM_PAGE_TO_PHYS(dst);
2365 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
2366 if (need_wired_tlb_page_pool) {
2367 struct fpage *fp1, *fp2;
2368 struct sysmaps *sysmaps;
2370 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2371 mtx_lock(&sysmaps->lock);
2374 fp1 = &sysmaps->fp[PMAP_FPAGE1];
2375 fp2 = &sysmaps->fp[PMAP_FPAGE2];
2377 va_src = pmap_map_fpage(phy_src, fp1, FALSE);
2378 va_dst = pmap_map_fpage(phy_dst, fp2, FALSE);
2380 bcopy((caddr_t)va_src, (caddr_t)va_dst, PAGE_SIZE);
2382 pmap_unmap_fpage(phy_src, fp1);
2383 pmap_unmap_fpage(phy_dst, fp2);
2385 mtx_unlock(&sysmaps->lock);
2388 * Should you flush the cache?
2393 if ((phy_src < MIPS_KSEG0_LARGEST_PHYS) && (phy_dst < MIPS_KSEG0_LARGEST_PHYS)) {
2394 /* easy case, all can be accessed via KSEG0 */
2395 va_src = MIPS_PHYS_TO_CACHED(phy_src);
2396 va_dst = MIPS_PHYS_TO_CACHED(phy_dst);
2397 bcopy((caddr_t)va_src, (caddr_t)va_dst, PAGE_SIZE);
2400 struct local_sysmaps *sysm;
2402 cpu = PCPU_GET(cpuid);
2403 sysm = &sysmap_lmem[cpu];
2404 PMAP_LGMEM_LOCK(sysm);
2406 if (phy_src < MIPS_KSEG0_LARGEST_PHYS) {
2407 /* one side needs mapping - dest */
2408 va_src = MIPS_PHYS_TO_CACHED(phy_src);
2409 sysm->CMAP2 = mips_paddr_to_tlbpfn(phy_dst) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2410 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR2, sysm->CMAP2);
2412 va_dst = (vm_offset_t)sysm->CADDR2;
2413 } else if (phy_dst < MIPS_KSEG0_LARGEST_PHYS) {
2414 /* one side needs mapping - src */
2415 va_dst = MIPS_PHYS_TO_CACHED(phy_dst);
2416 sysm->CMAP1 = mips_paddr_to_tlbpfn(phy_src) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2417 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR1, sysm->CMAP1);
2418 va_src = (vm_offset_t)sysm->CADDR1;
2421 /* all need mapping */
2422 sysm->CMAP1 = mips_paddr_to_tlbpfn(phy_src) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2423 sysm->CMAP2 = mips_paddr_to_tlbpfn(phy_dst) | PTE_RW | PTE_V | PTE_G | PTE_W | PTE_CACHE;
2424 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR1, sysm->CMAP1);
2425 pmap_TLB_update_kernel((vm_offset_t)sysm->CADDR2, sysm->CMAP2);
2426 sysm->valid1 = sysm->valid2 = 1;
2427 va_src = (vm_offset_t)sysm->CADDR1;
2428 va_dst = (vm_offset_t)sysm->CADDR2;
2430 bcopy((void *)va_src, (void *)va_dst, PAGE_SIZE);
2432 pmap_TLB_invalidate_kernel((vm_offset_t)sysm->CADDR1);
2437 pmap_TLB_invalidate_kernel((vm_offset_t)sysm->CADDR2);
2442 PMAP_LGMEM_UNLOCK(sysm);
2448 * Returns true if the pmap's pv is one of the first
2449 * 16 pvs linked to from this page. This count may
2450 * be changed upwards or downwards in the future; it
2451 * is only necessary that true be returned for a small
2452 * subset of pmaps for proper page aging.
2455 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2460 if (m->flags & PG_FICTITIOUS)
2463 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2464 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2465 if (pv->pv_pmap == pmap) {
2476 * Remove all pages from specified address space
2477 * this aids process exit speeds. Also, this code
2478 * is special cased for current process only, but
2479 * can have the more generic (and slightly slower)
2480 * mode enabled. This is much faster than pmap_remove
2481 * in the case of running down an entire address space.
2484 pmap_remove_pages(pmap_t pmap)
2486 pt_entry_t *pte, tpte;
2490 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
2491 printf("warning: pmap_remove_pages called with non-current pmap\n");
2494 vm_page_lock_queues();
2497 //XXX need to be TAILQ_FOREACH_SAFE ?
2498 for (pv = TAILQ_FIRST(&pmap->pm_pvlist);
2502 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2503 if (!pmap_pte_v(pte))
2504 panic("pmap_remove_pages: page on pm_pvlist has no pte\n");
2508 * We cannot remove wired pages from a process' mapping at this time
2511 npv = TAILQ_NEXT(pv, pv_plist);
2514 *pte = is_kernel_pmap(pmap) ? PTE_G : 0;
2516 m = PHYS_TO_VM_PAGE(mips_tlbpfn_to_paddr(tpte));
2518 KASSERT(m < &vm_page_array[vm_page_array_size],
2519 ("pmap_remove_pages: bad tpte %lx", tpte));
2521 pv->pv_pmap->pm_stats.resident_count--;
2524 * Update the vm_page_t clean and reference bits.
2529 npv = TAILQ_NEXT(pv, pv_plist);
2530 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2532 m->md.pv_list_count--;
2533 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2534 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2535 vm_page_flag_clear(m, PG_WRITEABLE);
2537 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2541 pmap_invalidate_all(pmap);
2543 vm_page_unlock_queues();
2547 * pmap_testbit tests bits in pte's
2548 * note that the testbit/changebit routines are inline,
2549 * and a lot of things compile-time evaluate.
2552 pmap_testbit(vm_page_t m, int bit)
2556 boolean_t rv = FALSE;
2558 if (m->flags & PG_FICTITIOUS)
2561 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2564 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2565 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2566 #if defined(PMAP_DIAGNOSTIC)
2568 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2572 PMAP_LOCK(pv->pv_pmap);
2573 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2574 rv = (*pte & bit) != 0;
2575 PMAP_UNLOCK(pv->pv_pmap);
2583 * this routine is used to modify bits in ptes
2585 static __inline void
2586 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2588 register pv_entry_t pv;
2589 register pt_entry_t *pte;
2591 if (m->flags & PG_FICTITIOUS)
2594 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2596 * Loop over all current mappings setting/clearing as appropos If
2597 * setting RO do we need to clear the VAC?
2599 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2600 #if defined(PMAP_DIAGNOSTIC)
2602 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2607 PMAP_LOCK(pv->pv_pmap);
2608 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2612 pmap_update_page(pv->pv_pmap, pv->pv_va, *pte);
2614 vm_offset_t pbits = *(vm_offset_t *)pte;
2617 if (bit == PTE_RW) {
2618 if (pbits & PTE_M) {
2621 *(int *)pte = (pbits & ~(PTE_M | PTE_RW)) |
2624 *(int *)pte = pbits & ~bit;
2626 pmap_update_page(pv->pv_pmap, pv->pv_va, *pte);
2629 PMAP_UNLOCK(pv->pv_pmap);
2631 if (!setem && bit == PTE_RW)
2632 vm_page_flag_clear(m, PG_WRITEABLE);
2636 * pmap_page_wired_mappings:
2638 * Return the number of managed mappings to the given physical page
2642 pmap_page_wired_mappings(vm_page_t m)
2648 if ((m->flags & PG_FICTITIOUS) != 0)
2650 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2651 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
2658 * Clear the write and modified bits in each of the given page's mappings.
2661 pmap_remove_write(vm_page_t m)
2667 if ((m->flags & PG_WRITEABLE) == 0)
2671 * Loop over all current mappings setting/clearing as appropos.
2673 for (pv = TAILQ_FIRST(&m->md.pv_list); pv; pv = npv) {
2674 npv = TAILQ_NEXT(pv, pv_plist);
2675 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2677 if ((pte == NULL) || !mips_pg_v(*pte))
2678 panic("page on pm_pvlist has no pte\n");
2681 pmap_protect(pv->pv_pmap, va, va + PAGE_SIZE,
2682 VM_PROT_READ | VM_PROT_EXECUTE);
2684 vm_page_flag_clear(m, PG_WRITEABLE);
2688 * pmap_ts_referenced:
2690 * Return the count of reference bits for a page, clearing all of them.
2693 pmap_ts_referenced(vm_page_t m)
2695 if (m->flags & PG_FICTITIOUS)
2698 if (m->md.pv_flags & PV_TABLE_REF) {
2699 m->md.pv_flags &= ~PV_TABLE_REF;
2708 * Return whether or not the specified physical page was modified
2709 * in any physical maps.
2712 pmap_is_modified(vm_page_t m)
2714 if (m->flags & PG_FICTITIOUS)
2717 if (m->md.pv_flags & PV_TABLE_MOD)
2720 return pmap_testbit(m, PTE_M);
2726 * pmap_is_prefaultable:
2728 * Return whether or not the specified virtual address is elgible
2732 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2739 if (*pmap_pde(pmap, addr)) {
2740 pte = pmap_pte(pmap, addr);
2748 * Clear the modify bits on the specified physical page.
2751 pmap_clear_modify(vm_page_t m)
2753 if (m->flags & PG_FICTITIOUS)
2755 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2756 if (m->md.pv_flags & PV_TABLE_MOD) {
2757 pmap_changebit(m, PTE_M, FALSE);
2758 m->md.pv_flags &= ~PV_TABLE_MOD;
2763 * pmap_clear_reference:
2765 * Clear the reference bit on the specified physical page.
2768 pmap_clear_reference(vm_page_t m)
2770 if (m->flags & PG_FICTITIOUS)
2773 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2774 if (m->md.pv_flags & PV_TABLE_REF) {
2775 m->md.pv_flags &= ~PV_TABLE_REF;
2780 * Miscellaneous support routines follow
2784 * Map a set of physical memory pages into the kernel virtual
2785 * address space. Return a pointer to where it is mapped. This
2786 * routine is intended to be used for mapping device memory,
2791 * Map a set of physical memory pages into the kernel virtual
2792 * address space. Return a pointer to where it is mapped. This
2793 * routine is intended to be used for mapping device memory,
2797 pmap_mapdev(vm_offset_t pa, vm_size_t size)
2799 vm_offset_t va, tmpva, offset;
2802 * KSEG1 maps only first 512M of phys address space. For
2803 * pa > 0x20000000 we should make proper mapping * using pmap_kenter.
2805 if (pa + size < MIPS_KSEG0_LARGEST_PHYS)
2806 return (void *)MIPS_PHYS_TO_KSEG1(pa);
2808 offset = pa & PAGE_MASK;
2809 size = roundup(size, PAGE_SIZE);
2811 va = kmem_alloc_nofault(kernel_map, size);
2813 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2814 for (tmpva = va; size > 0;) {
2815 pmap_kenter(tmpva, pa);
2822 return ((void *)(va + offset));
2826 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2831 * perform the pmap work for mincore
2834 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2837 pt_entry_t *ptep, pte;
2842 ptep = pmap_pte(pmap, addr);
2843 pte = (ptep != NULL) ? *ptep : 0;
2846 if (mips_pg_v(pte)) {
2849 val = MINCORE_INCORE;
2850 pa = mips_tlbpfn_to_paddr(pte);
2851 if (!page_is_managed(pa))
2854 m = PHYS_TO_VM_PAGE(pa);
2860 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
2862 * Modified by someone
2865 vm_page_lock_queues();
2866 if (m->dirty || pmap_is_modified(m))
2867 val |= MINCORE_MODIFIED_OTHER;
2868 vm_page_unlock_queues();
2871 * Referenced by us or someone
2873 vm_page_lock_queues();
2874 if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
2875 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
2876 vm_page_flag_set(m, PG_REFERENCED);
2878 vm_page_unlock_queues();
2884 pmap_activate(struct thread *td)
2886 pmap_t pmap, oldpmap;
2887 struct proc *p = td->td_proc;
2891 pmap = vmspace_pmap(p->p_vmspace);
2892 oldpmap = PCPU_GET(curpmap);
2895 atomic_clear_32(&oldpmap->pm_active, PCPU_GET(cpumask));
2896 atomic_set_32(&pmap->pm_active, PCPU_GET(cpumask));
2897 pmap_asid_alloc(pmap);
2898 if (td == curthread) {
2899 PCPU_SET(segbase, pmap->pm_segtab);
2900 MachSetPID(pmap->pm_asid[PCPU_GET(cpuid)].asid);
2902 PCPU_SET(curpmap, pmap);
2907 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
2912 * Increase the starting virtual address of the given mapping if a
2913 * different alignment might result in more superpage mappings.
2916 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
2917 vm_offset_t *addr, vm_size_t size)
2919 vm_offset_t superpage_offset;
2923 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
2924 offset += ptoa(object->pg_color);
2925 superpage_offset = offset & SEGOFSET;
2926 if (size - ((NBSEG - superpage_offset) & SEGOFSET) < NBSEG ||
2927 (*addr & SEGOFSET) == superpage_offset)
2929 if ((*addr & SEGOFSET) < superpage_offset)
2930 *addr = (*addr & ~SEGOFSET) + superpage_offset;
2932 *addr = ((*addr + SEGOFSET) & ~SEGOFSET) + superpage_offset;
2935 int pmap_pid_dump(int pid);
2938 pmap_pid_dump(int pid)
2945 sx_slock(&allproc_lock);
2946 LIST_FOREACH(p, &allproc, p_list) {
2947 if (p->p_pid != pid)
2953 printf("vmspace is %p\n",
2956 pmap = vmspace_pmap(p->p_vmspace);
2957 printf("pmap asid:%x generation:%x\n",
2958 pmap->pm_asid[0].asid,
2959 pmap->pm_asid[0].gen);
2960 for (i = 0; i < NUSERPGTBLS; i++) {
2963 unsigned base = i << SEGSHIFT;
2965 pde = &pmap->pm_segtab[i];
2966 if (pde && pmap_pde_v(pde)) {
2967 for (j = 0; j < 1024; j++) {
2968 unsigned va = base +
2971 pte = pmap_pte(pmap, va);
2972 if (pte && pmap_pte_v(pte)) {
2976 pa = mips_tlbpfn_to_paddr(*pte);
2977 m = PHYS_TO_VM_PAGE(pa);
2978 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
2996 printf("Process pid:%d has no vm_space\n", pid);
3000 sx_sunlock(&allproc_lock);
3007 static void pads(pmap_t pm);
3008 void pmap_pvdump(vm_offset_t pa);
3010 /* print address space of pmap*/
3017 if (pm == kernel_pmap)
3019 for (i = 0; i < NPTEPG; i++)
3020 if (pm->pm_segtab[i])
3021 for (j = 0; j < NPTEPG; j++) {
3022 va = (i << SEGSHIFT) + (j << PAGE_SHIFT);
3023 if (pm == kernel_pmap && va < KERNBASE)
3025 if (pm != kernel_pmap &&
3026 va >= VM_MAXUSER_ADDRESS)
3028 ptep = pmap_pte(pm, va);
3029 if (pmap_pte_v(ptep))
3030 printf("%x:%x ", va, *(int *)ptep);
3036 pmap_pvdump(vm_offset_t pa)
3038 register pv_entry_t pv;
3041 printf("pa %x", pa);
3042 m = PHYS_TO_VM_PAGE(pa);
3043 for (pv = TAILQ_FIRST(&m->md.pv_list); pv;
3044 pv = TAILQ_NEXT(pv, pv_list)) {
3045 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
3056 * Allocate TLB address space tag (called ASID or TLBPID) and return it.
3057 * It takes almost as much or more time to search the TLB for a
3058 * specific ASID and flush those entries as it does to flush the entire TLB.
3059 * Therefore, when we allocate a new ASID, we just take the next number. When
3060 * we run out of numbers, we flush the TLB, increment the generation count
3061 * and start over. ASID zero is reserved for kernel use.
3064 pmap_asid_alloc(pmap)
3067 if (pmap->pm_asid[PCPU_GET(cpuid)].asid != PMAP_ASID_RESERVED &&
3068 pmap->pm_asid[PCPU_GET(cpuid)].gen == PCPU_GET(asid_generation));
3070 if (PCPU_GET(next_asid) == pmap_max_asid) {
3072 PCPU_SET(asid_generation,
3073 (PCPU_GET(asid_generation) + 1) & ASIDGEN_MASK);
3074 if (PCPU_GET(asid_generation) == 0) {
3075 PCPU_SET(asid_generation, 1);
3077 PCPU_SET(next_asid, 1); /* 0 means invalid */
3079 pmap->pm_asid[PCPU_GET(cpuid)].asid = PCPU_GET(next_asid);
3080 pmap->pm_asid[PCPU_GET(cpuid)].gen = PCPU_GET(asid_generation);
3081 PCPU_SET(next_asid, PCPU_GET(next_asid) + 1);
3085 if (pmapdebug & (PDB_FOLLOW | PDB_TLBPID)) {
3087 printf("pmap_asid_alloc: curproc %d '%s' ",
3088 curproc->p_pid, curproc->p_comm);
3090 printf("pmap_asid_alloc: curproc <none> ");
3091 printf("segtab %p asid %d\n", pmap->pm_segtab,
3092 pmap->pm_asid[PCPU_GET(cpuid)].asid);
3098 page_is_managed(vm_offset_t pa)
3100 vm_offset_t pgnum = mips_btop(pa);
3102 if (pgnum >= first_page && (pgnum < (first_page + vm_page_array_size))) {
3105 m = PHYS_TO_VM_PAGE(pa);
3106 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0)
3113 init_pte_prot(vm_offset_t va, vm_page_t m, vm_prot_t prot)
3117 if (!(prot & VM_PROT_WRITE))
3120 if (va >= VM_MIN_KERNEL_ADDRESS) {
3122 * Don't bother to trap on kernel writes, just
3123 * record page as dirty.
3127 } else if ((m->md.pv_flags & PV_TABLE_MOD) ||
3128 m->dirty == VM_PAGE_BITS_ALL)
3132 vm_page_flag_set(m, PG_WRITEABLE);
3138 * pmap_page_is_free:
3140 * Called when a page is freed to allow pmap to clean up
3141 * any extra state associated with the page. In this case
3142 * clear modified/referenced bits.
3145 pmap_page_is_free(vm_page_t m)
3152 * pmap_set_modified:
3154 * Sets the page modified and reference bits for the specified page.
3157 pmap_set_modified(vm_offset_t pa)
3160 PHYS_TO_VM_PAGE(pa)->md.pv_flags |= (PV_TABLE_REF | PV_TABLE_MOD);
3163 #include <machine/db_machdep.h>
3166 * Dump the translation buffer (TLB) in readable form.
3170 db_dump_tlb(int first, int last)
3177 while (tlbno <= last) {
3178 MachTLBRead(tlbno, &tlb);
3179 if (tlb.tlb_lo0 & PTE_V || tlb.tlb_lo1 & PTE_V) {
3180 printf("TLB %2d vad 0x%08x ", tlbno, (tlb.tlb_hi & 0xffffff00));
3182 printf("TLB*%2d vad 0x%08x ", tlbno, (tlb.tlb_hi & 0xffffff00));
3184 printf("0=0x%08x ", pfn_to_vad(tlb.tlb_lo0));
3185 printf("%c", tlb.tlb_lo0 & PTE_M ? 'M' : ' ');
3186 printf("%c", tlb.tlb_lo0 & PTE_G ? 'G' : ' ');
3187 printf(" atr %x ", (tlb.tlb_lo0 >> 3) & 7);
3188 printf("1=0x%08x ", pfn_to_vad(tlb.tlb_lo1));
3189 printf("%c", tlb.tlb_lo1 & PTE_M ? 'M' : ' ');
3190 printf("%c", tlb.tlb_lo1 & PTE_G ? 'G' : ' ');
3191 printf(" atr %x ", (tlb.tlb_lo1 >> 3) & 7);
3192 printf(" sz=%x pid=%x\n", tlb.tlb_mask,
3193 (tlb.tlb_hi & 0x000000ff)
3200 #include <sys/kernel.h>
3201 #include <ddb/ddb.h>
3203 DB_SHOW_COMMAND(tlb, ddb_dump_tlb)
3205 db_dump_tlb(0, num_tlbentries - 1);
3211 * Routine: pmap_kextract
3213 * Extract the physical page address associated
3216 /* PMAP_INLINE */ vm_offset_t
3217 pmap_kextract(vm_offset_t va)
3221 if (va < MIPS_CACHED_MEMORY_ADDR) {
3222 /* user virtual address */
3225 if (curproc && curproc->p_vmspace) {
3226 ptep = pmap_pte(&curproc->p_vmspace->vm_pmap, va);
3228 pa = mips_tlbpfn_to_paddr(*ptep) |
3231 } else if (va >= MIPS_CACHED_MEMORY_ADDR &&
3232 va < MIPS_UNCACHED_MEMORY_ADDR)
3233 pa = MIPS_CACHED_TO_PHYS(va);
3234 else if (va >= MIPS_UNCACHED_MEMORY_ADDR &&
3235 va < MIPS_KSEG2_START)
3236 pa = MIPS_UNCACHED_TO_PHYS(va);
3237 #ifdef VM_ALLOC_WIRED_TLB_PG_POOL
3238 else if (need_wired_tlb_page_pool && ((va >= VM_MIN_KERNEL_ADDRESS) &&
3239 (va < (VM_MIN_KERNEL_ADDRESS + VM_KERNEL_ALLOC_OFFSET))))
3240 pa = MIPS_CACHED_TO_PHYS(va);
3242 else if (va >= MIPS_KSEG2_START && va < VM_MAX_KERNEL_ADDRESS) {
3245 /* Is the kernel pmap initialized? */
3246 if (kernel_pmap->pm_active) {
3247 if (va >= (vm_offset_t)virtual_sys_start) {
3248 /* Its inside the virtual address range */
3249 ptep = pmap_pte(kernel_pmap, va);
3251 pa = mips_tlbpfn_to_paddr(*ptep) |
3257 * its inside the special mapping area, I
3258 * don't think this should happen, but if it
3259 * does I want it toa all work right :-)
3260 * Note if it does happen, we assume the
3261 * caller has the lock? FIXME, this needs to
3262 * be checked FIXEM - RRS.
3264 for (i = 0; i < MAXCPU; i++) {
3265 if ((sysmap_lmem[i].valid1) && ((vm_offset_t)sysmap_lmem[i].CADDR1 == va)) {
3266 pa = mips_tlbpfn_to_paddr(sysmap_lmem[i].CMAP1);
3269 if ((sysmap_lmem[i].valid2) && ((vm_offset_t)sysmap_lmem[i].CADDR2 == va)) {
3270 pa = mips_tlbpfn_to_paddr(sysmap_lmem[i].CMAP2);