2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
58 * Author: Avadis Tevanian, Jr., Michael Wayne Young
59 * (These guys wrote the Vax version)
61 * Physical Map management code for Intel i386, i486, and i860.
63 * Manages physical address maps.
65 * In addition to hardware address maps, this
66 * module is called upon to provide software-use-only
67 * maps which may or may not be stored in the same
68 * form as hardware maps. These pseudo-maps are
69 * used to store intermediate results from copy
70 * operations to and from address spaces.
72 * Since the information managed by this module is
73 * also stored by the logical address mapping module,
74 * this module may throw away valid virtual-to-physical
75 * mappings at almost any time. However, invalidations
76 * of virtual-to-physical mappings must be done as
79 * In order to cope with hardware architectures which
80 * make virtual-to-physical map invalidates expensive,
81 * this module may delay invalidate or reduced protection
82 * operations until such time as they are actually
83 * necessary. This module is given full information as
84 * to which processors are currently using which maps,
85 * and to when physical maps must be made correct.
93 #include <mach_ldebug.h>
95 #include <mach/machine/vm_types.h>
97 #include <mach/boolean.h>
98 #include <kern/thread.h>
99 #include <kern/zalloc.h>
101 #include <kern/lock.h>
102 #include <kern/spl.h>
105 #include <vm/vm_map.h>
106 #include <vm/vm_kern.h>
107 #include <mach/vm_param.h>
108 #include <mach/vm_prot.h>
109 #include <vm/vm_object.h>
110 #include <vm/vm_page.h>
112 #include <mach/machine/vm_param.h>
113 #include <machine/thread.h>
115 #include <kern/misc_protos.h> /* prototyping */
116 #include <i386/misc_protos.h>
118 #include <i386/cpuid.h>
121 #include <ddb/db_command.h>
122 #include <ddb/db_output.h>
123 #include <ddb/db_sym.h>
124 #include <ddb/db_print.h>
125 #endif /* MACH_KDB */
127 #include <kern/xpr.h>
130 #include <i386/AT386/mp/mp_events.h>
134 * Forward declarations for internal functions.
140 extern void pmap_remove_range(
146 void phys_attribute_clear(
150 boolean_t
phys_attribute_test(
154 void pmap_set_modify(vm_offset_t phys
);
156 void phys_attribute_set(
162 void set_dirbase(vm_offset_t dirbase
);
163 #endif /* set_dirbase */
165 #define PA_TO_PTE(pa) (pa_to_pte((pa) - VM_MIN_KERNEL_ADDRESS))
166 #define iswired(pte) ((pte) & INTEL_PTE_WIRED)
168 pmap_t real_pmap
[NCPUS
];
170 #define WRITE_PTE(pte_p, pte_entry) *(pte_p) = (pte_entry);
171 #define WRITE_PTE_FAST(pte_p, pte_entry) *(pte_p) = (pte_entry);
174 * Private data structures.
178 * For each vm_page_t, there is a list of all currently
179 * valid virtual mappings of that page. An entry is
180 * a pv_entry_t; the list is the pv_table.
183 typedef struct pv_entry
{
184 struct pv_entry
*next
; /* next pv_entry */
185 pmap_t pmap
; /* pmap where mapping lies */
186 vm_offset_t va
; /* virtual address for mapping */
189 #define PV_ENTRY_NULL ((pv_entry_t) 0)
191 pv_entry_t pv_head_table
; /* array of entries, one per page */
194 * pv_list entries are kept on a list that can only be accessed
195 * with the pmap system locked (at SPLVM, not in the cpus_active set).
196 * The list is refilled from the pv_list_zone if it becomes empty.
198 pv_entry_t pv_free_list
; /* free list at SPLVM */
199 decl_simple_lock_data(,pv_free_list_lock
)
201 #define PV_ALLOC(pv_e) { \
202 simple_lock(&pv_free_list_lock); \
203 if ((pv_e = pv_free_list) != 0) { \
204 pv_free_list = pv_e->next; \
206 simple_unlock(&pv_free_list_lock); \
209 #define PV_FREE(pv_e) { \
210 simple_lock(&pv_free_list_lock); \
211 pv_e->next = pv_free_list; \
212 pv_free_list = pv_e; \
213 simple_unlock(&pv_free_list_lock); \
216 zone_t pv_list_zone
; /* zone of pv_entry structures */
219 * Each entry in the pv_head_table is locked by a bit in the
220 * pv_lock_table. The lock bits are accessed by the physical
221 * address of the page they lock.
224 char *pv_lock_table
; /* pointer to array of bits */
225 #define pv_lock_table_size(n) (((n)+BYTE_SIZE-1)/BYTE_SIZE)
228 * First and last physical addresses that we maintain any information
229 * for. Initialized to zero so that pmap operations done before
230 * pmap_init won't touch any non-existent structures.
232 vm_offset_t vm_first_phys
= (vm_offset_t
) 0;
233 vm_offset_t vm_last_phys
= (vm_offset_t
) 0;
234 boolean_t pmap_initialized
= FALSE
;/* Has pmap_init completed? */
237 * Index into pv_head table, its lock bits, and the modify/reference
238 * bits starting at vm_first_phys.
241 #define pa_index(pa) (atop(pa - vm_first_phys))
243 #define pai_to_pvh(pai) (&pv_head_table[pai])
244 #define lock_pvh_pai(pai) bit_lock(pai, (void *)pv_lock_table)
245 #define unlock_pvh_pai(pai) bit_unlock(pai, (void *)pv_lock_table)
248 * Array of physical page attribites for managed pages.
249 * One byte per physical page.
251 char *pmap_phys_attributes
;
254 * Physical page attributes. Copy bits from PTE definition.
256 #define PHYS_MODIFIED INTEL_PTE_MOD /* page modified */
257 #define PHYS_REFERENCED INTEL_PTE_REF /* page referenced */
260 * Amount of virtual memory mapped by one
261 * page-directory entry.
263 #define PDE_MAPPED_SIZE (pdetova(1))
266 * We allocate page table pages directly from the VM system
267 * through this object. It maps physical memory.
269 vm_object_t pmap_object
= VM_OBJECT_NULL
;
272 * Locking and TLB invalidation
278 * There are two structures in the pmap module that need locking:
279 * the pmaps themselves, and the per-page pv_lists (which are locked
280 * by locking the pv_lock_table entry that corresponds to the pv_head
281 * for the list in question.) Most routines want to lock a pmap and
282 * then do operations in it that require pv_list locking -- however
283 * pmap_remove_all and pmap_copy_on_write operate on a physical page
284 * basis and want to do the locking in the reverse order, i.e. lock
285 * a pv_list and then go through all the pmaps referenced by that list.
286 * To protect against deadlock between these two cases, the pmap_lock
287 * is used. There are three different locking protocols as a result:
289 * 1. pmap operations only (pmap_extract, pmap_access, ...) Lock only
292 * 2. pmap-based operations (pmap_enter, pmap_remove, ...) Get a read
293 * lock on the pmap_lock (shared read), then lock the pmap
294 * and finally the pv_lists as needed [i.e. pmap lock before
297 * 3. pv_list-based operations (pmap_remove_all, pmap_copy_on_write, ...)
298 * Get a write lock on the pmap_lock (exclusive write); this
299 * also guaranteees exclusive access to the pv_lists. Lock the
302 * At no time may any routine hold more than one pmap lock or more than
303 * one pv_list lock. Because interrupt level routines can allocate
304 * mbufs and cause pmap_enter's, the pmap_lock and the lock on the
305 * kernel_pmap can only be held at splhigh.
310 * We raise the interrupt level to splhigh, to block interprocessor
311 * interrupts during pmap operations. We must take the CPU out of
312 * the cpus_active set while interrupts are blocked.
314 #define SPLVM(spl) { \
316 mp_disable_preemption(); \
317 i_bit_clear(cpu_number(), &cpus_active); \
318 mp_enable_preemption(); \
321 #define SPLX(spl) { \
322 mp_disable_preemption(); \
323 i_bit_set(cpu_number(), &cpus_active); \
324 mp_enable_preemption(); \
329 * Lock on pmap system
331 lock_t pmap_system_lock
;
333 #define PMAP_READ_LOCK(pmap, spl) { \
335 lock_read(&pmap_system_lock); \
336 simple_lock(&(pmap)->lock); \
339 #define PMAP_WRITE_LOCK(spl) { \
341 lock_write(&pmap_system_lock); \
344 #define PMAP_READ_UNLOCK(pmap, spl) { \
345 simple_unlock(&(pmap)->lock); \
346 lock_read_done(&pmap_system_lock); \
350 #define PMAP_WRITE_UNLOCK(spl) { \
351 lock_write_done(&pmap_system_lock); \
355 #define PMAP_WRITE_TO_READ_LOCK(pmap) { \
356 simple_lock(&(pmap)->lock); \
357 lock_write_to_read(&pmap_system_lock); \
360 #define LOCK_PVH(index) lock_pvh_pai(index)
362 #define UNLOCK_PVH(index) unlock_pvh_pai(index)
364 #define PMAP_FLUSH_TLBS() \
367 i386_signal_cpus(MP_TLB_FLUSH); \
370 #define PMAP_RELOAD_TLBS() { \
371 i386_signal_cpus(MP_TLB_RELOAD); \
372 set_cr3(kernel_pmap->pdirbase); \
375 #define PMAP_INVALIDATE_PAGE(map, addr) { \
376 if (map == kernel_pmap) \
377 invlpg((vm_offset_t) addr); \
380 i386_signal_cpus(MP_TLB_FLUSH); \
383 #else /* NCPUS > 1 */
386 #define SPLVM(spl) { (spl) = splhigh(); }
387 #define SPLX(spl) splx (spl)
393 #define PMAP_READ_LOCK(pmap, spl) SPLVM(spl)
394 #define PMAP_WRITE_LOCK(spl) SPLVM(spl)
395 #define PMAP_READ_UNLOCK(pmap, spl) SPLX(spl)
396 #define PMAP_WRITE_UNLOCK(spl) SPLX(spl)
397 #define PMAP_WRITE_TO_READ_LOCK(pmap)
400 #define LOCK_PVH(index) disable_preemption()
401 #define UNLOCK_PVH(index) enable_preemption()
403 #define LOCK_PVH(index)
404 #define UNLOCK_PVH(index)
407 #define PMAP_FLUSH_TLBS() flush_tlb()
408 #define PMAP_RELOAD_TLBS() set_cr3(kernel_pmap->pdirbase)
409 #define PMAP_INVALIDATE_PAGE(map, addr) { \
410 if (map == kernel_pmap) \
411 invlpg((vm_offset_t) addr); \
416 #endif /* NCPUS > 1 */
418 #define MAX_TBIS_SIZE 32 /* > this -> TBIA */ /* XXX */
422 * Structures to keep track of pending TLB invalidations
426 volatile boolean_t cpu_update_needed
[NCPUS
];
429 #endif /* NCPUS > 1 */
432 * Other useful macros.
434 #define current_pmap() (vm_map_pmap(current_act()->map))
435 #define pmap_in_use(pmap, cpu) (((pmap)->cpus_using & (1 << (cpu))) != 0)
437 struct pmap kernel_pmap_store
;
440 struct zone
*pmap_zone
; /* zone of pmap structures */
442 int pmap_debug
= 0; /* flag for debugging prints */
443 int ptes_per_vm_page
; /* number of hardware ptes needed
444 to map one VM page. */
445 unsigned int inuse_ptepages_count
= 0; /* debugging */
448 * Pmap cache. Cache is threaded through ref_count field of pmap.
449 * Max will eventually be constant -- variable for experimentation.
451 int pmap_cache_max
= 32;
452 int pmap_alloc_chunk
= 8;
453 pmap_t pmap_cache_list
;
454 int pmap_cache_count
;
455 decl_simple_lock_data(,pmap_cache_lock
)
457 extern vm_offset_t hole_start
, hole_end
;
462 * Page directory for kernel.
464 pt_entry_t
*kpde
= 0; /* set by start.s - keep out of bss */
467 #define PMAP_ALIAS_MAX 32
473 #define PMAP_ALIAS_COOKIE 0xdeadbeef
474 } pmap_aliasbuf
[PMAP_ALIAS_MAX
];
475 int pmap_alias_index
= 0;
476 extern vm_offset_t
get_rpc();
478 #endif /* DEBUG_ALIAS */
481 * Given an offset and a map, compute the address of the
482 * pte. If the address is invalid with respect to the map
483 * then PT_ENTRY_NULL is returned (and the map may need to grow).
485 * This is only used in machine-dependent code.
490 register pmap_t pmap
,
491 register vm_offset_t addr
)
493 register pt_entry_t
*ptp
;
494 register pt_entry_t pte
;
496 pte
= pmap
->dirbase
[pdenum(pmap
, addr
)];
497 if ((pte
& INTEL_PTE_VALID
) == 0)
498 return(PT_ENTRY_NULL
);
499 ptp
= (pt_entry_t
*)ptetokv(pte
);
500 return(&ptp
[ptenum(addr
)]);
504 #define pmap_pde(pmap, addr) (&(pmap)->dirbase[pdenum(pmap, addr)])
506 #define DEBUG_PTE_PAGE 0
513 register pt_entry_t
*pte
, *epte
;
516 /* check the use and wired counts */
517 if (ptep
== PTE_PAGE_NULL
)
519 pte
= pmap_pte(ptep
->pmap
, ptep
->va
);
520 epte
= pte
+ INTEL_PGBYTES
/sizeof(pt_entry_t
);
529 pte
+= ptes_per_vm_page
;
532 if (ctu
!= ptep
->use_count
|| ctw
!= ptep
->wired_count
) {
533 printf("use %d wired %d - actual use %d wired %d\n",
534 ptep
->use_count
, ptep
->wired_count
, ctu
, ctw
);
538 #endif /* DEBUG_PTE_PAGE */
541 * Map memory at initialization. The physical addresses being
542 * mapped are not managed and are never unmapped.
544 * For now, VM is already on, we only need to map the
549 register vm_offset_t virt
,
550 register vm_offset_t start
,
551 register vm_offset_t end
,
552 register vm_prot_t prot
)
557 while (start
< end
) {
558 pmap_enter(kernel_pmap
, virt
, start
, prot
, 0, FALSE
);
566 * Back-door routine for mapping kernel VM at initialization.
567 * Useful for mapping memory outside the range
568 * Sets no-cache, A, D.
569 * [vm_first_phys, vm_last_phys) (i.e., devices).
570 * Otherwise like pmap_map.
574 register vm_offset_t virt
,
575 register vm_offset_t start
,
576 register vm_offset_t end
,
579 register pt_entry_t
template;
580 register pt_entry_t
*pte
;
582 template = pa_to_pte(start
)
588 if (prot
& VM_PROT_WRITE
)
589 template |= INTEL_PTE_WRITE
;
591 while (start
< end
) {
592 pte
= pmap_pte(kernel_pmap
, virt
);
593 if (pte
== PT_ENTRY_NULL
)
594 panic("pmap_map_bd: Invalid kernel address\n");
595 WRITE_PTE_FAST(pte
, template)
596 pte_increment_pa(template);
607 extern char *first_avail
;
608 extern vm_offset_t virtual_avail
, virtual_end
;
609 extern vm_offset_t avail_start
, avail_end
, avail_next
;
612 * Bootstrap the system enough to run with virtual memory.
613 * Map the kernel's code and data, and allocate the system page table.
614 * Called with mapping OFF. Page_size must already be set.
617 * load_start: PA where kernel was loaded
618 * avail_start PA of first available physical page -
619 * after kernel page tables
620 * avail_end PA of last available physical page
621 * virtual_avail VA of first available page -
622 * after kernel page tables
623 * virtual_end VA of last available page -
624 * end of kernel address space
626 * &start_text start of kernel text
627 * &etext end of kernel text
632 vm_offset_t load_start
)
634 vm_offset_t va
, tva
, paddr
;
636 pt_entry_t
*pde
, *pte
, *ptend
;
637 vm_size_t morevm
; /* VM space for kernel map */
640 * Set ptes_per_vm_page for general use.
642 ptes_per_vm_page
= PAGE_SIZE
/ INTEL_PGBYTES
;
645 * The kernel's pmap is statically allocated so we don't
646 * have to use pmap_create, which is unlikely to work
647 * correctly at this part of the boot sequence.
650 kernel_pmap
= &kernel_pmap_store
;
653 lock_init(&pmap_system_lock
,
654 FALSE
, /* NOT a sleep lock */
657 #endif /* NCPUS > 1 */
659 simple_lock_init(&kernel_pmap
->lock
, ETAP_VM_PMAP_KERNEL
);
660 simple_lock_init(&pv_free_list_lock
, ETAP_VM_PMAP_FREE
);
662 kernel_pmap
->ref_count
= 1;
665 * The kernel page directory has been allocated;
666 * its virtual address is in kpde.
668 * Enough kernel page table pages have been allocated
669 * to map low system memory, kernel text, kernel data/bss,
670 * kdb's symbols, and the page directory and page tables.
672 * No other physical memory has been allocated.
676 * Start mapping virtual memory to physical memory, 1-1,
677 * at end of mapped memory.
680 virtual_avail
= phystokv(avail_start
);
681 virtual_end
= phystokv(avail_end
);
684 pde
+= pdenum(kernel_pmap
, virtual_avail
);
686 if (pte_to_pa(*pde
) == 0) {
687 /* This pte has not been allocated */
691 pte
= (pt_entry_t
*)ptetokv(*pde
);
692 /* first pte of page */
693 ptend
= pte
+NPTES
; /* last pte of page */
694 pte
+= ptenum(virtual_avail
); /* point to pte that
695 maps first avail VA */
696 pde
++; /* point pde to first empty slot */
699 template = pa_to_pte(avail_start
)
703 for (va
= virtual_avail
; va
< virtual_end
; va
+= INTEL_PGBYTES
) {
705 pte
= (pt_entry_t
*)phystokv(virtual_avail
);
707 virtual_avail
= (vm_offset_t
)ptend
;
708 if (virtual_avail
== hole_start
)
709 virtual_avail
= hole_end
;
710 *pde
= PA_TO_PTE((vm_offset_t
) pte
)
715 WRITE_PTE_FAST(pte
, template)
717 pte_increment_pa(template);
720 avail_start
= virtual_avail
- VM_MIN_KERNEL_ADDRESS
;
721 avail_next
= avail_start
;
724 * Figure out maximum kernel address.
725 * Kernel virtual space is:
726 * - at least three times physical memory
727 * - at least VM_MIN_KERNEL_ADDRESS
728 * - limited by VM_MAX_KERNEL_ADDRESS
731 morevm
= 3*avail_end
;
732 if (virtual_end
+ morevm
> VM_MAX_KERNEL_ADDRESS
)
733 morevm
= VM_MAX_KERNEL_ADDRESS
- virtual_end
+ 1;
736 * startup requires additional virtual memory (for tables, buffers,
737 * etc.). The kd driver may also require some of that memory to
738 * access the graphics board.
741 *(int *)&template = 0;
744 * Leave room for kernel-loaded servers, which have been linked at
745 * addresses from VM_MIN_KERNEL_LOADED_ADDRESS to
746 * VM_MAX_KERNEL_LOADED_ADDRESS.
748 if (virtual_end
+ morevm
< VM_MAX_KERNEL_LOADED_ADDRESS
+ 1)
749 morevm
= VM_MAX_KERNEL_LOADED_ADDRESS
+ 1 - virtual_end
;
752 virtual_end
+= morevm
;
753 for (tva
= va
; tva
< virtual_end
; tva
+= INTEL_PGBYTES
) {
755 pmap_next_page(&paddr
);
756 pte
= (pt_entry_t
*)phystokv(paddr
);
758 *pde
= PA_TO_PTE((vm_offset_t
) pte
)
763 WRITE_PTE_FAST(pte
, template)
769 /* Push the virtual avail address above hole_end */
770 if (virtual_avail
< hole_end
)
771 virtual_avail
= hole_end
;
777 virtual_end
= va
+ morevm
;
782 * invalidate user virtual addresses
786 pdenum(kernel_pmap
,VM_MIN_KERNEL_ADDRESS
)*sizeof(pt_entry_t
));
787 kernel_pmap
->dirbase
= kpde
;
788 printf("Kernel virtual space from 0x%x to 0x%x.\n",
789 VM_MIN_KERNEL_ADDRESS
, virtual_end
);
791 avail_start
= avail_next
;
792 printf("Available physical space from 0x%x to 0x%x\n",
793 avail_start
, avail_end
);
795 kernel_pmap
->pdirbase
= kvtophys((vm_offset_t
)kernel_pmap
->dirbase
);
804 *startp
= virtual_avail
;
809 * Initialize the pmap module.
810 * Called by vm_init, to initialize any structures that the pmap
811 * system needs to map virtual memory.
816 register long npages
;
818 register vm_size_t s
;
822 * Allocate memory for the pv_head_table and its lock bits,
823 * the modify bit array, and the pte_page table.
826 npages
= atop(avail_end
- avail_start
);
827 s
= (vm_size_t
) (sizeof(struct pv_entry
) * npages
828 + pv_lock_table_size(npages
)
832 if (kmem_alloc_wired(kernel_map
, &addr
, s
) != KERN_SUCCESS
)
835 memset((char *)addr
, 0, s
);
838 * Allocate the structures first to preserve word-alignment.
840 pv_head_table
= (pv_entry_t
) addr
;
841 addr
= (vm_offset_t
) (pv_head_table
+ npages
);
843 pv_lock_table
= (char *) addr
;
844 addr
= (vm_offset_t
) (pv_lock_table
+ pv_lock_table_size(npages
));
846 pmap_phys_attributes
= (char *) addr
;
849 * Create the zone of physical maps,
850 * and of the physical-to-virtual entries.
852 s
= (vm_size_t
) sizeof(struct pmap
);
853 pmap_zone
= zinit(s
, 400*s
, 4096, "pmap"); /* XXX */
854 s
= (vm_size_t
) sizeof(struct pv_entry
);
855 pv_list_zone
= zinit(s
, 10000*s
, 4096, "pv_list"); /* XXX */
858 * Only now, when all of the data structures are allocated,
859 * can we set vm_first_phys and vm_last_phys. If we set them
860 * too soon, the kmem_alloc_wired above will try to use these
861 * data structures and blow up.
864 vm_first_phys
= avail_start
;
865 vm_last_phys
= avail_end
;
866 pmap_initialized
= TRUE
;
869 * Initializie pmap cache.
871 pmap_cache_list
= PMAP_NULL
;
872 pmap_cache_count
= 0;
873 simple_lock_init(&pmap_cache_lock
, ETAP_VM_PMAP_CACHE
);
877 #define pmap_valid_page(x) ((avail_start <= x) && (x < avail_end))
880 #define valid_page(x) (pmap_initialized && pmap_valid_page(x))
891 assert(phys
!= vm_page_fictitious_addr
);
892 if (!pmap_initialized
)
895 if (!pmap_valid_page(phys
))
898 PMAP_WRITE_LOCK(spl
);
900 pai
= pa_index(phys
);
901 pv_h
= pai_to_pvh(pai
);
903 result
= (pv_h
->pmap
== PMAP_NULL
);
904 PMAP_WRITE_UNLOCK(spl
);
910 * Create and return a physical map.
912 * If the size specified for the map
913 * is zero, the map is an actual physical
914 * map, and may be referenced by the
917 * If the size specified is non-zero,
918 * the map will be used in software only, and
919 * is bounded by that size.
926 register pmap_statistics_t stats
;
929 * A software use-only map doesn't even need a map.
937 * Try to get cached pmap, if this fails,
938 * allocate a pmap struct from the pmap_zone. Then allocate
939 * the page descriptor table from the pd_zone.
942 simple_lock(&pmap_cache_lock
);
943 while ((p
= pmap_cache_list
) == PMAP_NULL
) {
945 vm_offset_t dirbases
;
948 simple_unlock(&pmap_cache_lock
);
952 * XXX NEEDS MP DOING ALLOC logic so that if multiple processors
953 * XXX get here, only one allocates a chunk of pmaps.
954 * (for now we'll just let it go - safe but wasteful)
959 * Allocate a chunck of pmaps. Single kmem_alloc_wired
960 * operation reduces kernel map fragmentation.
963 if (kmem_alloc_wired(kernel_map
, &dirbases
,
964 pmap_alloc_chunk
* INTEL_PGBYTES
)
966 panic("pmap_create.1");
968 for (i
= pmap_alloc_chunk
; i
> 0 ; i
--) {
969 p
= (pmap_t
) zalloc(pmap_zone
);
971 panic("pmap_create.2");
974 * Initialize pmap. Don't bother with
975 * ref count as cache list is threaded
976 * through it. It'll be set on cache removal.
978 p
->dirbase
= (pt_entry_t
*) dirbases
;
979 dirbases
+= INTEL_PGBYTES
;
980 memcpy(p
->dirbase
, kpde
, INTEL_PGBYTES
);
981 p
->pdirbase
= kvtophys((vm_offset_t
)p
->dirbase
);
983 simple_lock_init(&p
->lock
, ETAP_VM_PMAP
);
987 * Initialize statistics.
990 stats
->resident_count
= 0;
991 stats
->wired_count
= 0;
996 simple_lock(&pmap_cache_lock
);
997 p
->ref_count
= (int) pmap_cache_list
;
1000 simple_unlock(&pmap_cache_lock
);
1002 simple_lock(&pmap_cache_lock
);
1005 assert(p
->stats
.resident_count
== 0);
1006 assert(p
->stats
.wired_count
== 0);
1007 p
->stats
.resident_count
= 0;
1008 p
->stats
.wired_count
= 0;
1010 pmap_cache_list
= (pmap_t
) p
->ref_count
;
1013 simple_unlock(&pmap_cache_lock
);
1019 * Retire the given physical map from service.
1020 * Should only be called if the map contains
1021 * no valid mappings.
1028 register pt_entry_t
*pdep
;
1029 register vm_offset_t pa
;
1032 register vm_page_t m
;
1038 simple_lock(&p
->lock
);
1041 register int my_cpu
;
1043 mp_disable_preemption();
1044 my_cpu
= cpu_number();
1047 * If some cpu is not using the physical pmap pointer that it
1048 * is supposed to be (see set_dirbase), we might be using the
1049 * pmap that is being destroyed! Make sure we are
1050 * physically on the right pmap:
1054 if (real_pmap
[my_cpu
] == p
) {
1055 PMAP_CPU_CLR(p
, my_cpu
);
1056 real_pmap
[my_cpu
] = kernel_pmap
;
1059 mp_enable_preemption();
1061 simple_unlock(&p
->lock
);
1065 return; /* still in use */
1069 * Free the memory maps, then the
1073 while (pdep
< &p
->dirbase
[pdenum(p
, LINEAR_KERNEL_ADDRESS
)]) {
1074 if (*pdep
& INTEL_PTE_VALID
) {
1075 pa
= pte_to_pa(*pdep
);
1076 vm_object_lock(pmap_object
);
1077 m
= vm_page_lookup(pmap_object
, pa
);
1078 if (m
== VM_PAGE_NULL
)
1079 panic("pmap_destroy: pte page not in object");
1080 vm_page_lock_queues();
1082 inuse_ptepages_count
--;
1083 vm_object_unlock(pmap_object
);
1084 vm_page_unlock_queues();
1087 * Clear pdes, this might be headed for the cache.
1089 c
= ptes_per_vm_page
;
1096 pdep
+= ptes_per_vm_page
;
1100 assert(p
->stats
.resident_count
== 0);
1101 assert(p
->stats
.wired_count
== 0);
1104 * Add to cache if not already full
1106 simple_lock(&pmap_cache_lock
);
1107 if (pmap_cache_count
<= pmap_cache_max
) {
1108 p
->ref_count
= (int) pmap_cache_list
;
1109 pmap_cache_list
= p
;
1111 simple_unlock(&pmap_cache_lock
);
1114 simple_unlock(&pmap_cache_lock
);
1115 kmem_free(kernel_map
, (vm_offset_t
)p
->dirbase
, INTEL_PGBYTES
);
1116 zfree(pmap_zone
, (vm_offset_t
) p
);
1121 * Add a reference to the specified pmap.
1130 if (p
!= PMAP_NULL
) {
1132 simple_lock(&p
->lock
);
1134 simple_unlock(&p
->lock
);
1140 * Remove a range of hardware page-table entries.
1141 * The entries given are the first (inclusive)
1142 * and last (exclusive) entries for the VM pages.
1143 * The virtual address is the va for the first pte.
1145 * The pmap must be locked.
1146 * If the pmap is not the kernel pmap, the range must lie
1147 * entirely within one pte-page. This is NOT checked.
1148 * Assumes that the pte-page exists.
1159 register pt_entry_t
*cpte
;
1160 int num_removed
, num_unwired
;
1165 if (pmap
!= kernel_pmap
)
1166 ptep_check(get_pte_page(spte
));
1167 #endif /* DEBUG_PTE_PAGE */
1171 for (cpte
= spte
; cpte
< epte
;
1172 cpte
+= ptes_per_vm_page
, va
+= PAGE_SIZE
) {
1174 pa
= pte_to_pa(*cpte
);
1182 if (!valid_page(pa
)) {
1185 * Outside range of managed physical memory.
1186 * Just remove the mappings.
1188 register int i
= ptes_per_vm_page
;
1189 register pt_entry_t
*lpte
= cpte
;
1201 * Get the modify and reference bits.
1205 register pt_entry_t
*lpte
;
1207 i
= ptes_per_vm_page
;
1210 pmap_phys_attributes
[pai
] |=
1211 *lpte
& (PHYS_MODIFIED
|PHYS_REFERENCED
);
1218 * Remove the mapping from the pvlist for
1219 * this physical page.
1222 register pv_entry_t pv_h
, prev
, cur
;
1224 pv_h
= pai_to_pvh(pai
);
1225 if (pv_h
->pmap
== PMAP_NULL
) {
1226 panic("pmap_remove: null pv_list!");
1228 if (pv_h
->va
== va
&& pv_h
->pmap
== pmap
) {
1230 * Header is the pv_entry. Copy the next one
1231 * to header and free the next one (we cannot
1235 if (cur
!= PV_ENTRY_NULL
) {
1240 pv_h
->pmap
= PMAP_NULL
;
1247 if ((cur
= prev
->next
) == PV_ENTRY_NULL
) {
1248 panic("pmap-remove: mapping not in pv_list!");
1250 } while (cur
->va
!= va
|| cur
->pmap
!= pmap
);
1251 prev
->next
= cur
->next
;
1261 assert(pmap
->stats
.resident_count
>= num_removed
);
1262 pmap
->stats
.resident_count
-= num_removed
;
1263 assert(pmap
->stats
.wired_count
>= num_unwired
);
1264 pmap
->stats
.wired_count
-= num_unwired
;
1268 * Remove phys addr if mapped in specified map
1272 pmap_remove_some_phys(
1274 vm_offset_t phys_addr
)
1277 /* Implement to support working set code */
1283 * Remove the given range of addresses
1284 * from the specified map.
1286 * It is assumed that the start and end are properly
1287 * rounded to the hardware page size.
1297 register pt_entry_t
*pde
;
1298 register pt_entry_t
*spte
, *epte
;
1301 if (map
== PMAP_NULL
)
1304 PMAP_READ_LOCK(map
, spl
);
1306 pde
= pmap_pde(map
, s
);
1309 l
= (s
+ PDE_MAPPED_SIZE
) & ~(PDE_MAPPED_SIZE
-1);
1312 if (*pde
& INTEL_PTE_VALID
) {
1313 spte
= (pt_entry_t
*)ptetokv(*pde
);
1314 spte
= &spte
[ptenum(s
)];
1315 epte
= &spte
[intel_btop(l
-s
)];
1316 pmap_remove_range(map
, s
, spte
, epte
);
1324 PMAP_READ_UNLOCK(map
, spl
);
1328 * Routine: pmap_page_protect
1331 * Lower the permission for all mappings to a given
1339 pv_entry_t pv_h
, prev
;
1340 register pv_entry_t pv_e
;
1341 register pt_entry_t
*pte
;
1343 register pmap_t pmap
;
1347 assert(phys
!= vm_page_fictitious_addr
);
1348 if (!valid_page(phys
)) {
1350 * Not a managed page.
1356 * Determine the new protection.
1360 case VM_PROT_READ
|VM_PROT_EXECUTE
:
1364 return; /* nothing to do */
1371 * Lock the pmap system first, since we will be changing
1375 PMAP_WRITE_LOCK(spl
);
1377 pai
= pa_index(phys
);
1378 pv_h
= pai_to_pvh(pai
);
1381 * Walk down PV list, changing or removing all mappings.
1382 * We do not have to lock the pv_list because we have
1383 * the entire pmap system locked.
1385 if (pv_h
->pmap
!= PMAP_NULL
) {
1391 * Lock the pmap to block pmap_extract and similar routines.
1393 simple_lock(&pmap
->lock
);
1396 register vm_offset_t va
;
1399 pte
= pmap_pte(pmap
, va
);
1402 * Consistency checks.
1404 /* assert(*pte & INTEL_PTE_VALID); XXX */
1405 /* assert(pte_to_phys(*pte) == phys); */
1408 * Invalidate TLBs for all CPUs using this mapping.
1410 PMAP_INVALIDATE_PAGE(pmap
, va
);
1414 * Remove the mapping if new protection is NONE
1415 * or if write-protecting a kernel mapping.
1417 if (remove
|| pmap
== kernel_pmap
) {
1419 * Remove the mapping, collecting any modify bits.
1422 register int i
= ptes_per_vm_page
;
1425 pmap_phys_attributes
[pai
] |=
1426 *pte
& (PHYS_MODIFIED
|PHYS_REFERENCED
);
1431 assert(pmap
->stats
.resident_count
>= 1);
1432 pmap
->stats
.resident_count
--;
1435 * Remove the pv_entry.
1439 * Fix up head later.
1441 pv_h
->pmap
= PMAP_NULL
;
1445 * Delete this entry.
1447 prev
->next
= pv_e
->next
;
1455 register int i
= ptes_per_vm_page
;
1458 *pte
&= ~INTEL_PTE_WRITE
;
1468 simple_unlock(&pmap
->lock
);
1470 } while ((pv_e
= prev
->next
) != PV_ENTRY_NULL
);
1473 * If pv_head mapping was removed, fix it up.
1475 if (pv_h
->pmap
== PMAP_NULL
) {
1477 if (pv_e
!= PV_ENTRY_NULL
) {
1484 PMAP_WRITE_UNLOCK(spl
);
1488 * Set the physical protection on the
1489 * specified range of this map as requested.
1490 * Will not increase permissions.
1499 register pt_entry_t
*pde
;
1500 register pt_entry_t
*spte
, *epte
;
1505 if (map
== PMAP_NULL
)
1509 * Determine the new protection.
1513 case VM_PROT_READ
|VM_PROT_EXECUTE
:
1515 case VM_PROT_READ
|VM_PROT_WRITE
:
1517 return; /* nothing to do */
1519 pmap_remove(map
, s
, e
);
1524 * If write-protecting in the kernel pmap,
1525 * remove the mappings; the i386 ignores
1526 * the write-permission bit in kernel mode.
1528 * XXX should be #if'd for i386
1531 if (cpuid_family
== CPUID_FAMILY_386
)
1532 if (map
== kernel_pmap
) {
1533 pmap_remove(map
, s
, e
);
1538 simple_lock(&map
->lock
);
1541 pde
= pmap_pde(map
, s
);
1543 l
= (s
+ PDE_MAPPED_SIZE
) & ~(PDE_MAPPED_SIZE
-1);
1546 if (*pde
& INTEL_PTE_VALID
) {
1547 spte
= (pt_entry_t
*)ptetokv(*pde
);
1548 spte
= &spte
[ptenum(s
)];
1549 epte
= &spte
[intel_btop(l
-s
)];
1551 while (spte
< epte
) {
1552 if (*spte
& INTEL_PTE_VALID
)
1553 *spte
&= ~INTEL_PTE_WRITE
;
1563 simple_unlock(&map
->lock
);
1570 * Insert the given physical page (p) at
1571 * the specified virtual address (v) in the
1572 * target physical map with the protection requested.
1574 * If specified, the page will be wired down, meaning
1575 * that the related pte cannot be reclaimed.
1577 * NB: This is the only routine which MAY NOT lazy-evaluate
1578 * or lose information. That is, this routine must actually
1579 * insert this page into the given map NOW.
1583 register pmap_t pmap
,
1585 register vm_offset_t pa
,
1590 register pt_entry_t
*pte
;
1591 register pv_entry_t pv_h
;
1592 register int i
, pai
;
1594 pt_entry_t
template;
1598 XPR(0x80000000, "%x/%x: pmap_enter %x/%x/%x\n",
1599 current_thread()->top_act
,
1603 assert(pa
!= vm_page_fictitious_addr
);
1605 printf("pmap(%x, %x)\n", v
, pa
);
1606 if (pmap
== PMAP_NULL
)
1609 if (cpuid_family
== CPUID_FAMILY_386
)
1610 if (pmap
== kernel_pmap
&& (prot
& VM_PROT_WRITE
) == 0
1611 && !wired
/* hack for io_wire */ ) {
1613 * Because the 386 ignores write protection in kernel mode,
1614 * we cannot enter a read-only kernel mapping, and must
1615 * remove an existing mapping if changing it.
1617 * XXX should be #if'd for i386
1619 PMAP_READ_LOCK(pmap
, spl
);
1621 pte
= pmap_pte(pmap
, v
);
1622 if (pte
!= PT_ENTRY_NULL
&& pte_to_pa(*pte
) != 0) {
1624 * Invalidate the translation buffer,
1625 * then remove the mapping.
1627 PMAP_INVALIDATE_PAGE(pmap
, v
);
1628 pmap_remove_range(pmap
, v
, pte
,
1629 pte
+ ptes_per_vm_page
);
1631 PMAP_READ_UNLOCK(pmap
, spl
);
1636 * Must allocate a new pvlist entry while we're unlocked;
1637 * zalloc may cause pageout (which will lock the pmap system).
1638 * If we determine we need a pvlist entry, we will unlock
1639 * and allocate one. Then we will retry, throughing away
1640 * the allocated entry later (if we no longer need it).
1642 pv_e
= PV_ENTRY_NULL
;
1644 PMAP_READ_LOCK(pmap
, spl
);
1647 * Expand pmap to include this pte. Assume that
1648 * pmap is always expanded to include enough hardware
1649 * pages to map one VM page.
1652 while ((pte
= pmap_pte(pmap
, v
)) == PT_ENTRY_NULL
) {
1654 * Must unlock to expand the pmap.
1656 PMAP_READ_UNLOCK(pmap
, spl
);
1658 pmap_expand(pmap
, v
);
1660 PMAP_READ_LOCK(pmap
, spl
);
1663 * Special case if the physical page is already mapped
1666 old_pa
= pte_to_pa(*pte
);
1669 * May be changing its wired attribute or protection
1672 template = pa_to_pte(pa
) | INTEL_PTE_VALID
;
1673 if (pmap
!= kernel_pmap
)
1674 template |= INTEL_PTE_USER
;
1675 if (prot
& VM_PROT_WRITE
)
1676 template |= INTEL_PTE_WRITE
;
1678 template |= INTEL_PTE_WIRED
;
1680 pmap
->stats
.wired_count
++;
1683 if (iswired(*pte
)) {
1684 assert(pmap
->stats
.wired_count
>= 1);
1685 pmap
->stats
.wired_count
--;
1689 PMAP_INVALIDATE_PAGE(pmap
, v
);
1691 i
= ptes_per_vm_page
;
1693 if (*pte
& INTEL_PTE_MOD
)
1694 template |= INTEL_PTE_MOD
;
1695 WRITE_PTE(pte
, template)
1697 pte_increment_pa(template);
1704 * Outline of code from here:
1705 * 1) If va was mapped, update TLBs, remove the mapping
1706 * and remove old pvlist entry.
1707 * 2) Add pvlist entry for new mapping
1708 * 3) Enter new mapping.
1710 * SHARING_FAULTS complicates this slightly in that it cannot
1711 * replace the mapping, but must remove it (because adding the
1712 * pvlist entry for the new mapping may remove others), and
1713 * hence always enters the new mapping at step 3)
1715 * If the old physical page is not managed step 1) is skipped
1716 * (except for updating the TLBs), and the mapping is
1717 * overwritten at step 3). If the new physical page is not
1718 * managed, step 2) is skipped.
1721 if (old_pa
!= (vm_offset_t
) 0) {
1723 PMAP_INVALIDATE_PAGE(pmap
, v
);
1726 if (pmap
!= kernel_pmap
)
1727 ptep_check(get_pte_page(pte
));
1728 #endif /* DEBUG_PTE_PAGE */
1731 * Don't do anything to pages outside valid memory here.
1732 * Instead convince the code that enters a new mapping
1733 * to overwrite the old one.
1736 if (valid_page(old_pa
)) {
1738 pai
= pa_index(old_pa
);
1741 assert(pmap
->stats
.resident_count
>= 1);
1742 pmap
->stats
.resident_count
--;
1743 if (iswired(*pte
)) {
1744 assert(pmap
->stats
.wired_count
>= 1);
1745 pmap
->stats
.wired_count
--;
1747 i
= ptes_per_vm_page
;
1749 pmap_phys_attributes
[pai
] |=
1750 *pte
& (PHYS_MODIFIED
|PHYS_REFERENCED
);
1753 pte_increment_pa(template);
1757 * Put pte back to beginning of page since it'll be
1758 * used later to enter the new page.
1760 pte
-= ptes_per_vm_page
;
1763 * Remove the mapping from the pvlist for
1764 * this physical page.
1767 register pv_entry_t prev
, cur
;
1769 pv_h
= pai_to_pvh(pai
);
1770 if (pv_h
->pmap
== PMAP_NULL
) {
1771 panic("pmap_enter: null pv_list!");
1773 if (pv_h
->va
== v
&& pv_h
->pmap
== pmap
) {
1775 * Header is the pv_entry. Copy the next one
1776 * to header and free the next one (we cannot
1780 if (cur
!= PV_ENTRY_NULL
) {
1785 pv_h
->pmap
= PMAP_NULL
;
1792 if ((cur
= prev
->next
) == PV_ENTRY_NULL
) {
1793 panic("pmap_enter: mapping not in pv_list!");
1795 } while (cur
->va
!= v
|| cur
->pmap
!= pmap
);
1796 prev
->next
= cur
->next
;
1805 * old_pa is not managed. Pretend it's zero so code
1806 * at Step 3) will enter new mapping (overwriting old
1807 * one). Do removal part of accounting.
1809 old_pa
= (vm_offset_t
) 0;
1810 assert(pmap
->stats
.resident_count
>= 1);
1811 pmap
->stats
.resident_count
--;
1812 if (iswired(*pte
)) {
1813 assert(pmap
->stats
.wired_count
>= 1);
1814 pmap
->stats
.wired_count
--;
1819 if (valid_page(pa
)) {
1822 * Step 2) Enter the mapping in the PV list for this
1832 * We can return here from the sharing fault code below
1833 * in case we removed the only entry on the pv list and thus
1834 * must enter the new one in the list header.
1836 #endif /* SHARING_FAULTS */
1838 pv_h
= pai_to_pvh(pai
);
1840 if (pv_h
->pmap
== PMAP_NULL
) {
1846 pv_h
->next
= PV_ENTRY_NULL
;
1852 * check that this mapping is not already there
1853 * or there is no alias for this mapping in the same map
1855 pv_entry_t e
= pv_h
;
1856 while (e
!= PV_ENTRY_NULL
) {
1857 if (e
->pmap
== pmap
&& e
->va
== v
)
1858 panic("pmap_enter: already in pv_list");
1866 * do sharing faults.
1867 * if we find an entry on this pv list in the same address
1868 * space, remove it. we know there will not be more
1871 pv_entry_t e
= pv_h
;
1874 while (e
!= PV_ENTRY_NULL
) {
1875 if (e
->pmap
== pmap
) {
1877 * Remove it, drop pv list lock first.
1881 opte
= pmap_pte(pmap
, e
->va
);
1882 assert(opte
!= PT_ENTRY_NULL
);
1884 * Invalidate the translation buffer,
1885 * then remove the mapping.
1887 PMAP_INVALIDATE_PAGE(pmap
, e
->va
);
1888 pmap_remove_range(pmap
, e
->va
, opte
,
1889 opte
+ ptes_per_vm_page
);
1891 * We could have remove the head entry,
1892 * so there could be no more entries
1893 * and so we have to use the pv head entry.
1894 * so, go back to the top and try the entry
1903 * check that this mapping is not already there
1906 while (e
!= PV_ENTRY_NULL
) {
1907 if (e
->pmap
== pmap
)
1908 panic("pmap_enter: alias in pv_list");
1912 #endif /* SHARING_FAULTS */
1916 * check for aliases within the same address space.
1918 pv_entry_t e
= pv_h
;
1919 vm_offset_t rpc
= get_rpc();
1921 while (e
!= PV_ENTRY_NULL
) {
1922 if (e
->pmap
== pmap
) {
1924 * log this entry in the alias ring buffer
1925 * if it's not there already.
1927 struct pmap_alias
*pma
;
1931 for (ii
= 0; ii
< pmap_alias_index
; ii
++) {
1932 if (pmap_aliasbuf
[ii
].rpc
== rpc
) {
1933 /* found it in the log already */
1939 pma
= &pmap_aliasbuf
[pmap_alias_index
];
1943 pma
->cookie
= PMAP_ALIAS_COOKIE
;
1944 if (++pmap_alias_index
>= PMAP_ALIAS_MAX
)
1945 panic("pmap_enter: exhausted alias log");
1951 #endif /* DEBUG_ALIAS */
1953 * Add new pv_entry after header.
1955 if (pv_e
== PV_ENTRY_NULL
) {
1957 if (pv_e
== PV_ENTRY_NULL
) {
1959 PMAP_READ_UNLOCK(pmap
, spl
);
1964 pv_e
= (pv_entry_t
) zalloc(pv_list_zone
);
1970 pv_e
->next
= pv_h
->next
;
1973 * Remember that we used the pvlist entry.
1975 pv_e
= PV_ENTRY_NULL
;
1981 * Step 3) Enter and count the mapping.
1984 pmap
->stats
.resident_count
++;
1987 * Build a template to speed up entering -
1988 * only the pfn changes.
1990 template = pa_to_pte(pa
) | INTEL_PTE_VALID
;
1991 if (pmap
!= kernel_pmap
)
1992 template |= INTEL_PTE_USER
;
1993 if (prot
& VM_PROT_WRITE
)
1994 template |= INTEL_PTE_WRITE
;
1996 template |= INTEL_PTE_WIRED
;
1997 pmap
->stats
.wired_count
++;
1999 i
= ptes_per_vm_page
;
2001 WRITE_PTE(pte
, template)
2003 pte_increment_pa(template);
2006 if (pv_e
!= PV_ENTRY_NULL
) {
2010 PMAP_READ_UNLOCK(pmap
, spl
);
2014 * Routine: pmap_change_wiring
2015 * Function: Change the wiring attribute for a map/virtual-address
2017 * In/out conditions:
2018 * The mapping must already exist in the pmap.
2022 register pmap_t map
,
2026 register pt_entry_t
*pte
;
2032 * We must grab the pmap system lock because we may
2033 * change a pte_page queue.
2035 PMAP_READ_LOCK(map
, spl
);
2037 if ((pte
= pmap_pte(map
, v
)) == PT_ENTRY_NULL
)
2038 panic("pmap_change_wiring: pte missing");
2040 if (wired
&& !iswired(*pte
)) {
2042 * wiring down mapping
2044 map
->stats
.wired_count
++;
2045 i
= ptes_per_vm_page
;
2047 *pte
++ |= INTEL_PTE_WIRED
;
2050 else if (!wired
&& iswired(*pte
)) {
2054 assert(map
->stats
.wired_count
>= 1);
2055 map
->stats
.wired_count
--;
2056 i
= ptes_per_vm_page
;
2058 *pte
++ &= ~INTEL_PTE_WIRED
;
2062 PMAP_READ_UNLOCK(map
, spl
);
2071 * Routine: pmap_extract
2073 * Extract the physical page address associated
2074 * with the given map/virtual_address pair.
2079 register pmap_t pmap
,
2082 register pt_entry_t
*pte
;
2083 register vm_offset_t pa
;
2087 simple_lock(&pmap
->lock
);
2088 if ((pte
= pmap_pte(pmap
, va
)) == PT_ENTRY_NULL
)
2089 pa
= (vm_offset_t
) 0;
2090 else if (!(*pte
& INTEL_PTE_VALID
))
2091 pa
= (vm_offset_t
) 0;
2093 pa
= pte_to_pa(*pte
) + (va
& INTEL_OFFMASK
);
2094 simple_unlock(&pmap
->lock
);
2100 * Routine: pmap_expand
2102 * Expands a pmap to be able to map the specified virtual address.
2104 * Allocates new virtual memory for the P0 or P1 portion of the
2105 * pmap, then re-maps the physical pages that were in the old
2106 * pmap to be in the new pmap.
2108 * Must be called with the pmap system and the pmap unlocked,
2109 * since these must be unlocked to use vm_allocate or vm_deallocate.
2110 * Thus it must be called in a loop that checks whether the map
2111 * has been expanded enough.
2112 * (We won't loop forever, since page tables aren't shrunk.)
2116 register pmap_t map
,
2117 register vm_offset_t v
)
2120 register vm_page_t m
;
2121 register vm_offset_t pa
;
2125 if (map
== kernel_pmap
)
2126 panic("pmap_expand");
2129 * We cannot allocate the pmap_object in pmap_init,
2130 * because it is called before the zone package is up.
2131 * Allocate it now if it is missing.
2133 if (pmap_object
== VM_OBJECT_NULL
)
2134 pmap_object
= vm_object_allocate(avail_end
);
2137 * Allocate a VM page for the level 2 page table entries.
2139 while ((m
= vm_page_grab()) == VM_PAGE_NULL
)
2143 * Map the page to its physical address so that it
2144 * can be found later.
2147 vm_object_lock(pmap_object
);
2148 vm_page_insert(m
, pmap_object
, pa
);
2149 vm_page_lock_queues();
2151 inuse_ptepages_count
++;
2152 vm_object_unlock(pmap_object
);
2153 vm_page_unlock_queues();
2158 memset((void *)phystokv(pa
), 0, PAGE_SIZE
);
2160 PMAP_READ_LOCK(map
, spl
);
2162 * See if someone else expanded us first
2164 if (pmap_pte(map
, v
) != PT_ENTRY_NULL
) {
2165 PMAP_READ_UNLOCK(map
, spl
);
2166 vm_object_lock(pmap_object
);
2167 vm_page_lock_queues();
2169 inuse_ptepages_count
--;
2170 vm_page_unlock_queues();
2171 vm_object_unlock(pmap_object
);
2176 * Set the page directory entry for this page table.
2177 * If we have allocated more than one hardware page,
2178 * set several page directory entries.
2181 i
= ptes_per_vm_page
;
2182 pdp
= &map
->dirbase
[pdenum(map
, v
) & ~(i
-1)];
2184 *pdp
= pa_to_pte(pa
)
2189 pa
+= INTEL_PGBYTES
;
2192 PMAP_READ_UNLOCK(map
, spl
);
2197 * Copy the range specified by src_addr/len
2198 * from the source map to the range dst_addr/len
2199 * in the destination map.
2201 * This routine is only advisory and need not do anything.
2208 vm_offset_t dst_addr
,
2210 vm_offset_t src_addr
)
2213 dst_pmap
++; src_pmap
++; dst_addr
++; len
++; src_addr
++;
2222 * Routine: pmap_collect
2224 * Garbage collects the physical map system for
2225 * pages which are no longer used.
2226 * Success need not be guaranteed -- that is, there
2227 * may well be pages which are not referenced, but
2228 * others may be collected.
2230 * Called by the pageout daemon when pages are scarce.
2236 register pt_entry_t
*pdp
, *ptp
;
2245 if (p
== kernel_pmap
)
2249 * Garbage collect map.
2251 PMAP_READ_LOCK(p
, spl
);
2254 for (pdp
= p
->dirbase
;
2255 pdp
< &p
->dirbase
[pdenum(p
, LINEAR_KERNEL_ADDRESS
)];
2256 pdp
+= ptes_per_vm_page
)
2258 if (*pdp
& INTEL_PTE_VALID
)
2259 if(*pdp
& INTEL_PTE_REF
) {
2260 *pdp
&= ~INTEL_PTE_REF
;
2264 pa
= pte_to_pa(*pdp
);
2265 ptp
= (pt_entry_t
*)phystokv(pa
);
2266 eptp
= ptp
+ NPTES
*ptes_per_vm_page
;
2269 * If the pte page has any wired mappings, we cannot
2274 register pt_entry_t
*ptep
;
2275 for (ptep
= ptp
; ptep
< eptp
; ptep
++) {
2276 if (iswired(*ptep
)) {
2284 * Remove the virtual addresses mapped by this pte page.
2286 pmap_remove_range(p
,
2287 pdetova(pdp
- p
->dirbase
),
2292 * Invalidate the page directory pointer.
2295 register int i
= ptes_per_vm_page
;
2296 register pt_entry_t
*pdep
= pdp
;
2302 PMAP_READ_UNLOCK(p
, spl
);
2305 * And free the pte page itself.
2308 register vm_page_t m
;
2310 vm_object_lock(pmap_object
);
2311 m
= vm_page_lookup(pmap_object
, pa
);
2312 if (m
== VM_PAGE_NULL
)
2313 panic("pmap_collect: pte page not in object");
2314 vm_page_lock_queues();
2316 inuse_ptepages_count
--;
2317 vm_page_unlock_queues();
2318 vm_object_unlock(pmap_object
);
2321 PMAP_READ_LOCK(p
, spl
);
2325 PMAP_READ_UNLOCK(p
, spl
);
2331 * Routine: pmap_kernel
2333 * Returns the physical map handle for the kernel.
2339 return (kernel_pmap
);
2344 * pmap_zero_page zeros the specified (machine independent) page.
2345 * See machine/phys.c or machine/phys.s for implementation.
2350 register vm_offset_t phys
)
2354 assert(phys
!= vm_page_fictitious_addr
);
2355 i
= PAGE_SIZE
/ INTEL_PGBYTES
;
2356 phys
= intel_pfn(phys
);
2364 * pmap_copy_page copies the specified (machine independent) page.
2365 * See machine/phys.c or machine/phys.s for implementation.
2375 assert(src
!= vm_page_fictitious_addr
);
2376 assert(dst
!= vm_page_fictitious_addr
);
2377 i
= PAGE_SIZE
/ INTEL_PGBYTES
;
2380 copy_phys(intel_pfn(src
), intel_pfn(dst
));
2381 src
+= INTEL_PGBYTES
;
2382 dst
+= INTEL_PGBYTES
;
2388 * Routine: pmap_pageable
2390 * Make the specified pages (by pmap, offset)
2391 * pageable (or not) as requested.
2393 * A page which is not pageable may not take
2394 * a fault; therefore, its page table entry
2395 * must remain valid for the duration.
2397 * This routine is merely advisory; pmap_enter
2398 * will specify that these pages are to be wired
2399 * down (or not) as appropriate.
2409 pmap
++; start
++; end
++; pageable
++;
2414 * Clear specified attribute bits.
2417 phys_attribute_clear(
2422 register pv_entry_t pv_e
;
2423 register pt_entry_t
*pte
;
2425 register pmap_t pmap
;
2428 assert(phys
!= vm_page_fictitious_addr
);
2429 if (!valid_page(phys
)) {
2431 * Not a managed page.
2437 * Lock the pmap system first, since we will be changing
2441 PMAP_WRITE_LOCK(spl
);
2443 pai
= pa_index(phys
);
2444 pv_h
= pai_to_pvh(pai
);
2447 * Walk down PV list, clearing all modify or reference bits.
2448 * We do not have to lock the pv_list because we have
2449 * the entire pmap system locked.
2451 if (pv_h
->pmap
!= PMAP_NULL
) {
2453 * There are some mappings.
2455 for (pv_e
= pv_h
; pv_e
!= PV_ENTRY_NULL
; pv_e
= pv_e
->next
) {
2459 * Lock the pmap to block pmap_extract and similar routines.
2461 simple_lock(&pmap
->lock
);
2464 register vm_offset_t va
;
2467 pte
= pmap_pte(pmap
, va
);
2471 * Consistency checks.
2473 assert(*pte
& INTEL_PTE_VALID
);
2474 /* assert(pte_to_phys(*pte) == phys); */
2478 * Invalidate TLBs for all CPUs using this mapping.
2480 PMAP_INVALIDATE_PAGE(pmap
, va
);
2484 * Clear modify or reference bits.
2487 register int i
= ptes_per_vm_page
;
2492 simple_unlock(&pmap
->lock
);
2496 pmap_phys_attributes
[pai
] &= ~bits
;
2498 PMAP_WRITE_UNLOCK(spl
);
2502 * Check specified attribute bits.
2505 phys_attribute_test(
2510 register pv_entry_t pv_e
;
2511 register pt_entry_t
*pte
;
2513 register pmap_t pmap
;
2516 assert(phys
!= vm_page_fictitious_addr
);
2517 if (!valid_page(phys
)) {
2519 * Not a managed page.
2525 * Lock the pmap system first, since we will be checking
2529 PMAP_WRITE_LOCK(spl
);
2531 pai
= pa_index(phys
);
2532 pv_h
= pai_to_pvh(pai
);
2534 if (pmap_phys_attributes
[pai
] & bits
) {
2535 PMAP_WRITE_UNLOCK(spl
);
2540 * Walk down PV list, checking all mappings.
2541 * We do not have to lock the pv_list because we have
2542 * the entire pmap system locked.
2544 if (pv_h
->pmap
!= PMAP_NULL
) {
2546 * There are some mappings.
2548 for (pv_e
= pv_h
; pv_e
!= PV_ENTRY_NULL
; pv_e
= pv_e
->next
) {
2552 * Lock the pmap to block pmap_extract and similar routines.
2554 simple_lock(&pmap
->lock
);
2557 register vm_offset_t va
;
2560 pte
= pmap_pte(pmap
, va
);
2564 * Consistency checks.
2566 assert(*pte
& INTEL_PTE_VALID
);
2567 /* assert(pte_to_phys(*pte) == phys); */
2572 * Check modify or reference bits.
2575 register int i
= ptes_per_vm_page
;
2578 if (*pte
++ & bits
) {
2579 simple_unlock(&pmap
->lock
);
2580 PMAP_WRITE_UNLOCK(spl
);
2585 simple_unlock(&pmap
->lock
);
2588 PMAP_WRITE_UNLOCK(spl
);
2593 * Set specified attribute bits.
2602 assert(phys
!= vm_page_fictitious_addr
);
2603 if (!valid_page(phys
)) {
2605 * Not a managed page.
2611 * Lock the pmap system and set the requested bits in
2612 * the phys attributes array. Don't need to bother with
2613 * ptes because the test routine looks here first.
2616 PMAP_WRITE_LOCK(spl
);
2617 pmap_phys_attributes
[pa_index(phys
)] |= bits
;
2618 PMAP_WRITE_UNLOCK(spl
);
2622 * Set the modify bit on the specified physical page.
2625 void pmap_set_modify(
2626 register vm_offset_t phys
)
2628 phys_attribute_set(phys
, PHYS_MODIFIED
);
2632 * Clear the modify bits on the specified physical page.
2637 register vm_offset_t phys
)
2639 phys_attribute_clear(phys
, PHYS_MODIFIED
);
2645 * Return whether or not the specified physical page is modified
2646 * by any physical maps.
2651 register vm_offset_t phys
)
2653 return (phys_attribute_test(phys
, PHYS_MODIFIED
));
2657 * pmap_clear_reference:
2659 * Clear the reference bit on the specified physical page.
2663 pmap_clear_reference(
2666 phys_attribute_clear(phys
, PHYS_REFERENCED
);
2670 * pmap_is_referenced:
2672 * Return whether or not the specified physical page is referenced
2673 * by any physical maps.
2680 return (phys_attribute_test(phys
, PHYS_REFERENCED
));
2684 * Set the modify bit on the specified range
2685 * of this map as requested.
2687 * This optimization stands only if each time the dirty bit
2688 * in vm_page_t is tested, it is also tested in the pmap.
2697 register pt_entry_t
*pde
;
2698 register pt_entry_t
*spte
, *epte
;
2701 if (map
== PMAP_NULL
)
2704 PMAP_READ_LOCK(map
, spl
);
2706 pde
= pmap_pde(map
, s
);
2707 while (s
&& s
< e
) {
2708 l
= (s
+ PDE_MAPPED_SIZE
) & ~(PDE_MAPPED_SIZE
-1);
2711 if (*pde
& INTEL_PTE_VALID
) {
2712 spte
= (pt_entry_t
*)ptetokv(*pde
);
2714 spte
= &spte
[ptenum(s
)];
2715 epte
= &spte
[intel_btop(l
-s
)];
2717 epte
= &spte
[intel_btop(PDE_MAPPED_SIZE
)];
2718 spte
= &spte
[ptenum(s
)];
2720 while (spte
< epte
) {
2721 if (*spte
& INTEL_PTE_VALID
) {
2722 *spte
|= (INTEL_PTE_MOD
| INTEL_PTE_WRITE
);
2731 PMAP_READ_UNLOCK(map
, spl
);
2736 invalidate_icache(vm_offset_t addr
, unsigned cnt
, int phys
)
2741 flush_dcache(vm_offset_t addr
, unsigned count
, int phys
)
2749 pmap_wait_for_clear()
2751 register int my_cpu
;
2753 register pmap_t my_pmap
;
2755 mp_disable_preemption();
2756 my_cpu
= cpu_number();
2759 my_pmap
= real_pmap
[my_cpu
];
2761 if (!(my_pmap
&& pmap_in_use(my_pmap
, my_cpu
)))
2762 my_pmap
= kernel_pmap
;
2765 * Raise spl to splhigh (above splip) to block out pmap_extract
2766 * from IO code (which would put this cpu back in the active
2772 * Wait for any pmap updates in progress, on either user
2775 while (*(volatile hw_lock_t
)&my_pmap
->lock
.interlock
||
2776 *(volatile hw_lock_t
)&kernel_pmap
->lock
.interlock
) {
2781 mp_enable_preemption();
2785 pmap_flush_tlb_interrupt(void) {
2786 pmap_wait_for_clear();
2792 pmap_reload_tlb_interrupt(void) {
2793 pmap_wait_for_clear();
2795 set_cr3(kernel_pmap
->pdirbase
);
2799 #endif /* NCPUS > 1 */
2803 /* show phys page mappings and attributes */
2805 extern void db_show_page(vm_offset_t pa
);
2808 db_show_page(vm_offset_t pa
)
2815 pv_h
= pai_to_pvh(pai
);
2817 attr
= pmap_phys_attributes
[pai
];
2818 printf("phys page %x ", pa
);
2819 if (attr
& PHYS_MODIFIED
)
2820 printf("modified, ");
2821 if (attr
& PHYS_REFERENCED
)
2822 printf("referenced, ");
2823 if (pv_h
->pmap
|| pv_h
->next
)
2824 printf(" mapped at\n");
2826 printf(" not mapped\n");
2827 for (; pv_h
; pv_h
= pv_h
->next
)
2829 printf("%x in pmap %x\n", pv_h
->va
, pv_h
->pmap
);
2832 #endif /* MACH_KDB */
2835 void db_kvtophys(vm_offset_t
);
2836 void db_show_vaddrs(pt_entry_t
*);
2839 * print out the results of kvtophys(arg)
2845 db_printf("0x%x", kvtophys(vaddr
));
2849 * Walk the pages tables.
2853 pt_entry_t
*dirbase
)
2855 pt_entry_t
*ptep
, *pdep
, tmp
;
2856 int x
, y
, pdecnt
, ptecnt
;
2859 dirbase
= kernel_pmap
->dirbase
;
2862 db_printf("need a dirbase...\n");
2865 dirbase
= (pt_entry_t
*) ((unsigned long) dirbase
& ~INTEL_OFFMASK
);
2867 db_printf("dirbase: 0x%x\n", dirbase
);
2869 pdecnt
= ptecnt
= 0;
2871 for (y
= 0; y
< NPDES
; y
++, pdep
++) {
2872 if (((tmp
= *pdep
) & INTEL_PTE_VALID
) == 0) {
2876 ptep
= (pt_entry_t
*) ((*pdep
) & ~INTEL_OFFMASK
);
2877 db_printf("dir[%4d]: 0x%x\n", y
, *pdep
);
2878 for (x
= 0; x
< NPTES
; x
++, ptep
++) {
2879 if (((tmp
= *ptep
) & INTEL_PTE_VALID
) == 0) {
2883 db_printf(" tab[%4d]: 0x%x, va=0x%x, pa=0x%x\n",
2886 (y
<< 22) | (x
<< 12),
2887 *ptep
& ~INTEL_OFFMASK
);
2891 db_printf("total: %d tables, %d page table entries.\n", pdecnt
, ptecnt
);
2894 #endif /* MACH_KDB */
2896 #include <mach_vm_debug.h>
2898 #include <vm/vm_debug.h>
2901 pmap_list_resident_pages(
2902 register pmap_t pmap
,
2903 register vm_offset_t
*listp
,
2908 #endif /* MACH_VM_DEBUG */
2914 * BSD support routine to reassign virtual addresses.
2918 pmap_movepage(unsigned long from
, unsigned long to
, vm_size_t size
)
2921 pt_entry_t
*pte
, saved_pte
;
2922 /* Lock the kernel map */
2926 PMAP_READ_LOCK(kernel_pmap
, spl
);
2927 pte
= pmap_pte(kernel_pmap
, from
);
2929 panic("pmap_pagemove from pte NULL");
2931 PMAP_READ_UNLOCK(kernel_pmap
, spl
);
2933 pmap_enter(kernel_pmap
, to
, i386_trunc_page(*pte
),
2934 VM_PROT_READ
|VM_PROT_WRITE
, 0, *pte
& INTEL_PTE_WIRED
);
2936 pmap_remove(kernel_pmap
, from
, from
+PAGE_SIZE
);
2938 PMAP_READ_LOCK(kernel_pmap
, spl
);
2939 pte
= pmap_pte(kernel_pmap
, to
);
2941 panic("pmap_pagemove 'to' pte NULL");
2944 PMAP_READ_UNLOCK(kernel_pmap
, spl
);
2951 /* Get the processors to update the TLBs */
2956 kern_return_t
bmapvideo(vm_offset_t
*info
);
2957 kern_return_t
bmapvideo(vm_offset_t
*info
) {
2959 extern struct vc_info vinfo
;
2960 #ifdef NOTIMPLEMENTED
2961 (void)copyout((char *)&vinfo
, (char *)info
, sizeof(struct vc_info
)); /* Copy out the video info */
2963 return KERN_SUCCESS
;
2966 kern_return_t
bmapmap(vm_offset_t va
, vm_offset_t pa
, vm_size_t size
, vm_prot_t prot
, int attr
);
2967 kern_return_t
bmapmap(vm_offset_t va
, vm_offset_t pa
, vm_size_t size
, vm_prot_t prot
, int attr
) {
2969 #ifdef NOTIMPLEMENTED
2970 pmap_map_block(current_act()->task
->map
->pmap
, va
, pa
, size
, prot
, attr
); /* Map it in */
2972 return KERN_SUCCESS
;
2975 kern_return_t
bmapmapr(vm_offset_t va
);
2976 kern_return_t
bmapmapr(vm_offset_t va
) {
2978 #ifdef NOTIMPLEMENTED
2979 mapping_remove(current_act()->task
->map
->pmap
, va
); /* Remove map */
2981 return KERN_SUCCESS
;
2985 /* temporary workaround */
2987 coredumpok(vm_map_t map
, vm_offset_t va
)
2990 ptep
= pmap_pte(map
->pmap
, va
);
2991 if (0 == ptep
) return FALSE
;
2992 return ((*ptep
& (INTEL_PTE_NCACHE
|INTEL_PTE_WIRED
)) != (INTEL_PTE_NCACHE
|INTEL_PTE_WIRED
));