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>
119 #include <i386/cpu_number.h>
120 #include <i386/machine_cpu.h>
123 #include <ddb/db_command.h>
124 #include <ddb/db_output.h>
125 #include <ddb/db_sym.h>
126 #include <ddb/db_print.h>
127 #endif /* MACH_KDB */
129 #include <kern/xpr.h>
132 #include <i386/mp_events.h>
136 * Forward declarations for internal functions.
142 extern void pmap_remove_range(
148 void phys_attribute_clear(
152 boolean_t
phys_attribute_test(
156 void pmap_set_modify(ppnum_t pn
);
158 void phys_attribute_set(
164 void set_dirbase(vm_offset_t dirbase
);
165 #endif /* set_dirbase */
167 #define PA_TO_PTE(pa) (pa_to_pte((pa) - VM_MIN_KERNEL_ADDRESS))
168 #define iswired(pte) ((pte) & INTEL_PTE_WIRED)
170 pmap_t real_pmap
[NCPUS
];
172 #define WRITE_PTE(pte_p, pte_entry) *(pte_p) = (pte_entry);
173 #define WRITE_PTE_FAST(pte_p, pte_entry) *(pte_p) = (pte_entry);
175 #define value_64bit(value) ((value) & 0xFFFFFFFF00000000LL)
176 #define low32(x) ((unsigned int)((x) & 0x00000000ffffffffLL))
179 * Private data structures.
183 * For each vm_page_t, there is a list of all currently
184 * valid virtual mappings of that page. An entry is
185 * a pv_entry_t; the list is the pv_table.
188 typedef struct pv_entry
{
189 struct pv_entry
*next
; /* next pv_entry */
190 pmap_t pmap
; /* pmap where mapping lies */
191 vm_offset_t va
; /* virtual address for mapping */
194 #define PV_ENTRY_NULL ((pv_entry_t) 0)
196 pv_entry_t pv_head_table
; /* array of entries, one per page */
199 * pv_list entries are kept on a list that can only be accessed
200 * with the pmap system locked (at SPLVM, not in the cpus_active set).
201 * The list is refilled from the pv_list_zone if it becomes empty.
203 pv_entry_t pv_free_list
; /* free list at SPLVM */
204 decl_simple_lock_data(,pv_free_list_lock
)
206 #define PV_ALLOC(pv_e) { \
207 simple_lock(&pv_free_list_lock); \
208 if ((pv_e = pv_free_list) != 0) { \
209 pv_free_list = pv_e->next; \
211 simple_unlock(&pv_free_list_lock); \
214 #define PV_FREE(pv_e) { \
215 simple_lock(&pv_free_list_lock); \
216 pv_e->next = pv_free_list; \
217 pv_free_list = pv_e; \
218 simple_unlock(&pv_free_list_lock); \
221 zone_t pv_list_zone
; /* zone of pv_entry structures */
224 * Each entry in the pv_head_table is locked by a bit in the
225 * pv_lock_table. The lock bits are accessed by the physical
226 * address of the page they lock.
229 char *pv_lock_table
; /* pointer to array of bits */
230 #define pv_lock_table_size(n) (((n)+BYTE_SIZE-1)/BYTE_SIZE)
233 * First and last physical addresses that we maintain any information
234 * for. Initialized to zero so that pmap operations done before
235 * pmap_init won't touch any non-existent structures.
237 vm_offset_t vm_first_phys
= (vm_offset_t
) 0;
238 vm_offset_t vm_last_phys
= (vm_offset_t
) 0;
239 boolean_t pmap_initialized
= FALSE
;/* Has pmap_init completed? */
242 * Index into pv_head table, its lock bits, and the modify/reference
243 * bits starting at vm_first_phys.
246 #define pa_index(pa) (atop(pa - vm_first_phys))
248 #define pai_to_pvh(pai) (&pv_head_table[pai])
249 #define lock_pvh_pai(pai) bit_lock(pai, (void *)pv_lock_table)
250 #define unlock_pvh_pai(pai) bit_unlock(pai, (void *)pv_lock_table)
253 * Array of physical page attribites for managed pages.
254 * One byte per physical page.
256 char *pmap_phys_attributes
;
259 * Physical page attributes. Copy bits from PTE definition.
261 #define PHYS_MODIFIED INTEL_PTE_MOD /* page modified */
262 #define PHYS_REFERENCED INTEL_PTE_REF /* page referenced */
263 #define PHYS_NCACHE INTEL_PTE_NCACHE
266 * Amount of virtual memory mapped by one
267 * page-directory entry.
269 #define PDE_MAPPED_SIZE (pdetova(1))
272 * We allocate page table pages directly from the VM system
273 * through this object. It maps physical memory.
275 vm_object_t pmap_object
= VM_OBJECT_NULL
;
278 * Locking and TLB invalidation
284 * There are two structures in the pmap module that need locking:
285 * the pmaps themselves, and the per-page pv_lists (which are locked
286 * by locking the pv_lock_table entry that corresponds to the pv_head
287 * for the list in question.) Most routines want to lock a pmap and
288 * then do operations in it that require pv_list locking -- however
289 * pmap_remove_all and pmap_copy_on_write operate on a physical page
290 * basis and want to do the locking in the reverse order, i.e. lock
291 * a pv_list and then go through all the pmaps referenced by that list.
292 * To protect against deadlock between these two cases, the pmap_lock
293 * is used. There are three different locking protocols as a result:
295 * 1. pmap operations only (pmap_extract, pmap_access, ...) Lock only
298 * 2. pmap-based operations (pmap_enter, pmap_remove, ...) Get a read
299 * lock on the pmap_lock (shared read), then lock the pmap
300 * and finally the pv_lists as needed [i.e. pmap lock before
303 * 3. pv_list-based operations (pmap_remove_all, pmap_copy_on_write, ...)
304 * Get a write lock on the pmap_lock (exclusive write); this
305 * also guaranteees exclusive access to the pv_lists. Lock the
308 * At no time may any routine hold more than one pmap lock or more than
309 * one pv_list lock. Because interrupt level routines can allocate
310 * mbufs and cause pmap_enter's, the pmap_lock and the lock on the
311 * kernel_pmap can only be held at splhigh.
316 * We raise the interrupt level to splvm, to block interprocessor
317 * interrupts during pmap operations. We must take the CPU out of
318 * the cpus_active set while interrupts are blocked.
320 #define SPLVM(spl) { \
322 mp_disable_preemption(); \
323 i_bit_clear(cpu_number(), &cpus_active); \
324 mp_enable_preemption(); \
327 #define SPLX(spl) { \
328 mp_disable_preemption(); \
329 i_bit_set(cpu_number(), &cpus_active); \
330 mp_enable_preemption(); \
335 * Lock on pmap system
337 lock_t pmap_system_lock
;
339 #define PMAP_READ_LOCK(pmap, spl) { \
341 lock_read(&pmap_system_lock); \
342 simple_lock(&(pmap)->lock); \
345 #define PMAP_WRITE_LOCK(spl) { \
347 lock_write(&pmap_system_lock); \
350 #define PMAP_READ_UNLOCK(pmap, spl) { \
351 simple_unlock(&(pmap)->lock); \
352 lock_read_done(&pmap_system_lock); \
356 #define PMAP_WRITE_UNLOCK(spl) { \
357 lock_write_done(&pmap_system_lock); \
361 #define PMAP_WRITE_TO_READ_LOCK(pmap) { \
362 simple_lock(&(pmap)->lock); \
363 lock_write_to_read(&pmap_system_lock); \
366 #define LOCK_PVH(index) lock_pvh_pai(index)
368 #define UNLOCK_PVH(index) unlock_pvh_pai(index)
371 extern int max_lock_loops
;
372 #define LOOP_VAR int loop_count = 0
373 #define LOOP_CHECK(msg, pmap) \
374 if (loop_count++ > max_lock_loops) { \
375 mp_disable_preemption(); \
376 kprintf("%s: cpu %d pmap %x, cpus_active %d\n", \
377 msg, cpu_number(), pmap, cpus_active); \
378 Debugger("deadlock detection"); \
379 mp_enable_preemption(); \
382 #else /* USLOCK_DEBUG */
384 #define LOOP_CHECK(msg, pmap)
385 #endif /* USLOCK_DEBUG */
387 #define PMAP_UPDATE_TLBS(pmap, s, e) \
392 mp_disable_preemption(); \
393 cpu_mask = 1 << cpu_number(); \
395 /* Since the pmap is locked, other updates are locked */ \
396 /* out, and any pmap_activate has finished. */ \
398 /* find other cpus using the pmap */ \
399 users = (pmap)->cpus_using & ~cpu_mask; \
402 /* signal them, and wait for them to finish */ \
403 /* using the pmap */ \
404 signal_cpus(users, (pmap), (s), (e)); \
405 while (((pmap)->cpus_using & cpus_active & ~cpu_mask)) { \
406 LOOP_CHECK("PMAP_UPDATE_TLBS", pmap); \
410 /* invalidate our own TLB if pmap is in use */ \
412 if ((pmap)->cpus_using & cpu_mask) { \
413 INVALIDATE_TLB((pmap), (s), (e)); \
416 mp_enable_preemption(); \
419 #else /* NCPUS > 1 */
422 #define SPLVM(spl) { (spl) = splhigh(); }
423 #define SPLX(spl) splx (spl)
429 #define PMAP_READ_LOCK(pmap, spl) SPLVM(spl)
430 #define PMAP_WRITE_LOCK(spl) SPLVM(spl)
431 #define PMAP_READ_UNLOCK(pmap, spl) SPLX(spl)
432 #define PMAP_WRITE_UNLOCK(spl) SPLX(spl)
433 #define PMAP_WRITE_TO_READ_LOCK(pmap)
436 #define LOCK_PVH(index) disable_preemption()
437 #define UNLOCK_PVH(index) enable_preemption()
439 #define LOCK_PVH(index)
440 #define UNLOCK_PVH(index)
443 #define PMAP_FLUSH_TLBS() flush_tlb()
444 #define PMAP_RELOAD_TLBS() set_cr3(kernel_pmap->pdirbase)
445 #define PMAP_INVALIDATE_PAGE(map, saddr, eaddr) { \
446 if (map == kernel_pmap) \
447 invlpg((vm_offset_t) saddr); \
452 #endif /* NCPUS > 1 */
454 #define MAX_TBIS_SIZE 32 /* > this -> TBIA */ /* XXX */
456 #define INVALIDATE_TLB(m, s, e) { \
462 * Structures to keep track of pending TLB invalidations
466 volatile boolean_t cpu_update_needed
[NCPUS
];
468 #define UPDATE_LIST_SIZE 4
470 struct pmap_update_item
{
471 pmap_t pmap
; /* pmap to invalidate */
472 vm_offset_t start
; /* start address to invalidate */
473 vm_offset_t end
; /* end address to invalidate */
476 typedef struct pmap_update_item
*pmap_update_item_t
;
479 * List of pmap updates. If the list overflows,
480 * the last entry is changed to invalidate all.
482 struct pmap_update_list
{
483 decl_simple_lock_data(,lock
)
485 struct pmap_update_item item
[UPDATE_LIST_SIZE
];
487 typedef struct pmap_update_list
*pmap_update_list_t
;
489 struct pmap_update_list cpu_update_list
[NCPUS
];
491 extern void signal_cpus(
497 #endif /* NCPUS > 1 */
500 * Other useful macros.
502 #define current_pmap() (vm_map_pmap(current_act()->map))
503 #define pmap_in_use(pmap, cpu) (((pmap)->cpus_using & (1 << (cpu))) != 0)
505 struct pmap kernel_pmap_store
;
508 struct zone
*pmap_zone
; /* zone of pmap structures */
510 int pmap_debug
= 0; /* flag for debugging prints */
511 int ptes_per_vm_page
; /* number of hardware ptes needed
512 to map one VM page. */
513 unsigned int inuse_ptepages_count
= 0; /* debugging */
516 * Pmap cache. Cache is threaded through ref_count field of pmap.
517 * Max will eventually be constant -- variable for experimentation.
519 int pmap_cache_max
= 32;
520 int pmap_alloc_chunk
= 8;
521 pmap_t pmap_cache_list
;
522 int pmap_cache_count
;
523 decl_simple_lock_data(,pmap_cache_lock
)
525 extern vm_offset_t hole_start
, hole_end
;
530 * Page directory for kernel.
532 pt_entry_t
*kpde
= 0; /* set by start.s - keep out of bss */
535 #define PMAP_ALIAS_MAX 32
541 #define PMAP_ALIAS_COOKIE 0xdeadbeef
542 } pmap_aliasbuf
[PMAP_ALIAS_MAX
];
543 int pmap_alias_index
= 0;
544 extern vm_offset_t
get_rpc();
546 #endif /* DEBUG_ALIAS */
549 * Given an offset and a map, compute the address of the
550 * pte. If the address is invalid with respect to the map
551 * then PT_ENTRY_NULL is returned (and the map may need to grow).
553 * This is only used in machine-dependent code.
558 register pmap_t pmap
,
559 register vm_offset_t addr
)
561 register pt_entry_t
*ptp
;
562 register pt_entry_t pte
;
564 pte
= pmap
->dirbase
[pdenum(pmap
, addr
)];
565 if ((pte
& INTEL_PTE_VALID
) == 0)
566 return(PT_ENTRY_NULL
);
567 ptp
= (pt_entry_t
*)ptetokv(pte
);
568 return(&ptp
[ptenum(addr
)]);
572 #define pmap_pde(pmap, addr) (&(pmap)->dirbase[pdenum(pmap, addr)])
574 #define DEBUG_PTE_PAGE 0
581 register pt_entry_t
*pte
, *epte
;
584 /* check the use and wired counts */
585 if (ptep
== PTE_PAGE_NULL
)
587 pte
= pmap_pte(ptep
->pmap
, ptep
->va
);
588 epte
= pte
+ INTEL_PGBYTES
/sizeof(pt_entry_t
);
597 pte
+= ptes_per_vm_page
;
600 if (ctu
!= ptep
->use_count
|| ctw
!= ptep
->wired_count
) {
601 printf("use %d wired %d - actual use %d wired %d\n",
602 ptep
->use_count
, ptep
->wired_count
, ctu
, ctw
);
606 #endif /* DEBUG_PTE_PAGE */
609 * Map memory at initialization. The physical addresses being
610 * mapped are not managed and are never unmapped.
612 * For now, VM is already on, we only need to map the
617 register vm_offset_t virt
,
618 register vm_offset_t start
,
619 register vm_offset_t end
,
620 register vm_prot_t prot
)
625 while (start
< end
) {
626 pmap_enter(kernel_pmap
, virt
, (ppnum_t
)i386_btop(start
), prot
, 0, FALSE
);
634 * Back-door routine for mapping kernel VM at initialization.
635 * Useful for mapping memory outside the range
636 * Sets no-cache, A, D.
637 * [vm_first_phys, vm_last_phys) (i.e., devices).
638 * Otherwise like pmap_map.
642 register vm_offset_t virt
,
643 register vm_offset_t start
,
644 register vm_offset_t end
,
647 register pt_entry_t
template;
648 register pt_entry_t
*pte
;
650 template = pa_to_pte(start
)
656 if (prot
& VM_PROT_WRITE
)
657 template |= INTEL_PTE_WRITE
;
659 while (start
< end
) {
660 pte
= pmap_pte(kernel_pmap
, virt
);
661 if (pte
== PT_ENTRY_NULL
)
662 panic("pmap_map_bd: Invalid kernel address\n");
663 WRITE_PTE_FAST(pte
, template)
664 pte_increment_pa(template);
674 extern char *first_avail
;
675 extern vm_offset_t virtual_avail
, virtual_end
;
676 extern vm_offset_t avail_start
, avail_end
, avail_next
;
679 * Bootstrap the system enough to run with virtual memory.
680 * Map the kernel's code and data, and allocate the system page table.
681 * Called with mapping OFF. Page_size must already be set.
684 * load_start: PA where kernel was loaded
685 * avail_start PA of first available physical page -
686 * after kernel page tables
687 * avail_end PA of last available physical page
688 * virtual_avail VA of first available page -
689 * after kernel page tables
690 * virtual_end VA of last available page -
691 * end of kernel address space
693 * &start_text start of kernel text
694 * &etext end of kernel text
699 vm_offset_t load_start
)
701 vm_offset_t va
, tva
, paddr
;
704 pt_entry_t
*pde
, *pte
, *ptend
;
705 vm_size_t morevm
; /* VM space for kernel map */
707 vm_last_addr
= VM_MAX_KERNEL_ADDRESS
; /* Set the highest address known to VM */
710 * Set ptes_per_vm_page for general use.
712 ptes_per_vm_page
= PAGE_SIZE
/ INTEL_PGBYTES
;
715 * The kernel's pmap is statically allocated so we don't
716 * have to use pmap_create, which is unlikely to work
717 * correctly at this part of the boot sequence.
720 kernel_pmap
= &kernel_pmap_store
;
723 lock_init(&pmap_system_lock
,
724 FALSE
, /* NOT a sleep lock */
727 #endif /* NCPUS > 1 */
729 simple_lock_init(&kernel_pmap
->lock
, ETAP_VM_PMAP_KERNEL
);
730 simple_lock_init(&pv_free_list_lock
, ETAP_VM_PMAP_FREE
);
732 kernel_pmap
->ref_count
= 1;
735 * The kernel page directory has been allocated;
736 * its virtual address is in kpde.
738 * Enough kernel page table pages have been allocated
739 * to map low system memory, kernel text, kernel data/bss,
740 * kdb's symbols, and the page directory and page tables.
742 * No other physical memory has been allocated.
746 * Start mapping virtual memory to physical memory, 1-1,
747 * at end of mapped memory.
750 virtual_avail
= phystokv(avail_start
);
751 virtual_end
= phystokv(avail_end
);
754 pde
+= pdenum(kernel_pmap
, virtual_avail
);
756 if (pte_to_pa(*pde
) == 0) {
757 /* This pte has not been allocated */
761 pte
= (pt_entry_t
*)ptetokv(*pde
);
762 /* first pte of page */
763 ptend
= pte
+NPTES
; /* last pte of page */
764 pte
+= ptenum(virtual_avail
); /* point to pte that
765 maps first avail VA */
766 pde
++; /* point pde to first empty slot */
769 template = pa_to_pte(avail_start
)
773 for (va
= virtual_avail
; va
< virtual_end
; va
+= INTEL_PGBYTES
) {
775 pte
= (pt_entry_t
*)phystokv(virtual_avail
);
777 virtual_avail
= (vm_offset_t
)ptend
;
778 if (virtual_avail
== hole_start
)
779 virtual_avail
= hole_end
;
780 *pde
= PA_TO_PTE((vm_offset_t
) pte
)
785 WRITE_PTE_FAST(pte
, template)
787 pte_increment_pa(template);
790 avail_start
= virtual_avail
- VM_MIN_KERNEL_ADDRESS
;
791 avail_next
= avail_start
;
794 * Figure out maximum kernel address.
795 * Kernel virtual space is:
796 * - at least three times physical memory
797 * - at least VM_MIN_KERNEL_ADDRESS
798 * - limited by VM_MAX_KERNEL_ADDRESS
801 morevm
= 3*avail_end
;
802 if (virtual_end
+ morevm
> VM_MAX_KERNEL_ADDRESS
)
803 morevm
= VM_MAX_KERNEL_ADDRESS
- virtual_end
+ 1;
806 * startup requires additional virtual memory (for tables, buffers,
807 * etc.). The kd driver may also require some of that memory to
808 * access the graphics board.
811 *(int *)&template = 0;
814 * Leave room for kernel-loaded servers, which have been linked at
815 * addresses from VM_MIN_KERNEL_LOADED_ADDRESS to
816 * VM_MAX_KERNEL_LOADED_ADDRESS.
818 if (virtual_end
+ morevm
< VM_MAX_KERNEL_LOADED_ADDRESS
+ 1)
819 morevm
= VM_MAX_KERNEL_LOADED_ADDRESS
+ 1 - virtual_end
;
821 virtual_end
+= morevm
;
822 for (tva
= va
; tva
< virtual_end
; tva
+= INTEL_PGBYTES
) {
825 paddr
= i386_ptob(pn
);
826 pte
= (pt_entry_t
*)phystokv(paddr
);
828 *pde
= PA_TO_PTE((vm_offset_t
) pte
)
833 WRITE_PTE_FAST(pte
, template)
839 /* Push the virtual avail address above hole_end */
840 if (virtual_avail
< hole_end
)
841 virtual_avail
= hole_end
;
847 virtual_end
= va
+ morevm
;
852 * invalidate user virtual addresses
856 pdenum(kernel_pmap
,VM_MIN_KERNEL_ADDRESS
)*sizeof(pt_entry_t
));
857 kernel_pmap
->dirbase
= kpde
;
858 printf("Kernel virtual space from 0x%x to 0x%x.\n",
859 VM_MIN_KERNEL_ADDRESS
, virtual_end
);
861 avail_start
= avail_next
;
862 printf("Available physical space from 0x%x to 0x%x\n",
863 avail_start
, avail_end
);
865 kernel_pmap
->pdirbase
= kvtophys((vm_offset_t
)kernel_pmap
->dirbase
);
867 if (cpuid_features() & CPUID_FEATURE_PAT
)
873 asm volatile("rdmsr" : "=A" (pat
) : "c" (msr
));
875 pat
&= ~(0xfULL
<< 48);
876 pat
|= 0x01ULL
<< 48;
878 asm volatile("wrmsr" :: "A" (pat
), "c" (msr
));
887 *startp
= virtual_avail
;
892 * Initialize the pmap module.
893 * Called by vm_init, to initialize any structures that the pmap
894 * system needs to map virtual memory.
899 register long npages
;
901 register vm_size_t s
;
905 * Allocate memory for the pv_head_table and its lock bits,
906 * the modify bit array, and the pte_page table.
909 npages
= atop(avail_end
- avail_start
);
910 s
= (vm_size_t
) (sizeof(struct pv_entry
) * npages
911 + pv_lock_table_size(npages
)
915 if (kmem_alloc_wired(kernel_map
, &addr
, s
) != KERN_SUCCESS
)
918 memset((char *)addr
, 0, s
);
921 * Allocate the structures first to preserve word-alignment.
923 pv_head_table
= (pv_entry_t
) addr
;
924 addr
= (vm_offset_t
) (pv_head_table
+ npages
);
926 pv_lock_table
= (char *) addr
;
927 addr
= (vm_offset_t
) (pv_lock_table
+ pv_lock_table_size(npages
));
929 pmap_phys_attributes
= (char *) addr
;
932 * Create the zone of physical maps,
933 * and of the physical-to-virtual entries.
935 s
= (vm_size_t
) sizeof(struct pmap
);
936 pmap_zone
= zinit(s
, 400*s
, 4096, "pmap"); /* XXX */
937 s
= (vm_size_t
) sizeof(struct pv_entry
);
938 pv_list_zone
= zinit(s
, 10000*s
, 4096, "pv_list"); /* XXX */
942 * Set up the pmap request lists
944 for (i
= 0; i
< NCPUS
; i
++) {
945 pmap_update_list_t up
= &cpu_update_list
[i
];
947 simple_lock_init(&up
->lock
, ETAP_VM_PMAP_UPDATE
);
950 #endif /* NCPUS > 1 */
953 * Only now, when all of the data structures are allocated,
954 * can we set vm_first_phys and vm_last_phys. If we set them
955 * too soon, the kmem_alloc_wired above will try to use these
956 * data structures and blow up.
959 vm_first_phys
= avail_start
;
960 vm_last_phys
= avail_end
;
961 pmap_initialized
= TRUE
;
964 * Initializie pmap cache.
966 pmap_cache_list
= PMAP_NULL
;
967 pmap_cache_count
= 0;
968 simple_lock_init(&pmap_cache_lock
, ETAP_VM_PMAP_CACHE
);
972 #define pmap_valid_page(x) ((avail_start <= x) && (x < avail_end))
975 #define valid_page(x) (pmap_initialized && pmap_valid_page(x))
987 assert(pn
!= vm_page_fictitious_addr
);
988 phys
= (vm_offset_t
)i386_ptob(pn
);
989 if (!pmap_initialized
)
992 if (!pmap_valid_page(phys
))
995 PMAP_WRITE_LOCK(spl
);
997 pai
= pa_index(phys
);
998 pv_h
= pai_to_pvh(pai
);
1000 result
= (pv_h
->pmap
== PMAP_NULL
);
1001 PMAP_WRITE_UNLOCK(spl
);
1007 * Create and return a physical map.
1009 * If the size specified for the map
1010 * is zero, the map is an actual physical
1011 * map, and may be referenced by the
1014 * If the size specified is non-zero,
1015 * the map will be used in software only, and
1016 * is bounded by that size.
1023 register pmap_statistics_t stats
;
1026 * A software use-only map doesn't even need a map.
1034 * Try to get cached pmap, if this fails,
1035 * allocate a pmap struct from the pmap_zone. Then allocate
1036 * the page descriptor table from the pd_zone.
1039 simple_lock(&pmap_cache_lock
);
1040 while ((p
= pmap_cache_list
) == PMAP_NULL
) {
1042 vm_offset_t dirbases
;
1045 simple_unlock(&pmap_cache_lock
);
1049 * XXX NEEDS MP DOING ALLOC logic so that if multiple processors
1050 * XXX get here, only one allocates a chunk of pmaps.
1051 * (for now we'll just let it go - safe but wasteful)
1056 * Allocate a chunck of pmaps. Single kmem_alloc_wired
1057 * operation reduces kernel map fragmentation.
1060 if (kmem_alloc_wired(kernel_map
, &dirbases
,
1061 pmap_alloc_chunk
* INTEL_PGBYTES
)
1063 panic("pmap_create.1");
1065 for (i
= pmap_alloc_chunk
; i
> 0 ; i
--) {
1066 p
= (pmap_t
) zalloc(pmap_zone
);
1068 panic("pmap_create.2");
1071 * Initialize pmap. Don't bother with
1072 * ref count as cache list is threaded
1073 * through it. It'll be set on cache removal.
1075 p
->dirbase
= (pt_entry_t
*) dirbases
;
1076 dirbases
+= INTEL_PGBYTES
;
1077 memcpy(p
->dirbase
, kpde
, INTEL_PGBYTES
);
1078 p
->pdirbase
= kvtophys((vm_offset_t
)p
->dirbase
);
1080 simple_lock_init(&p
->lock
, ETAP_VM_PMAP
);
1084 * Initialize statistics.
1087 stats
->resident_count
= 0;
1088 stats
->wired_count
= 0;
1093 simple_lock(&pmap_cache_lock
);
1094 p
->ref_count
= (int) pmap_cache_list
;
1095 pmap_cache_list
= p
;
1097 simple_unlock(&pmap_cache_lock
);
1099 simple_lock(&pmap_cache_lock
);
1102 p
->stats
.resident_count
= 0;
1103 p
->stats
.wired_count
= 0;
1105 pmap_cache_list
= (pmap_t
) p
->ref_count
;
1108 simple_unlock(&pmap_cache_lock
);
1114 * Retire the given physical map from service.
1115 * Should only be called if the map contains
1116 * no valid mappings.
1123 register pt_entry_t
*pdep
;
1124 register vm_offset_t pa
;
1127 register vm_page_t m
;
1133 simple_lock(&p
->lock
);
1136 register int my_cpu
;
1138 mp_disable_preemption();
1139 my_cpu
= cpu_number();
1142 * If some cpu is not using the physical pmap pointer that it
1143 * is supposed to be (see set_dirbase), we might be using the
1144 * pmap that is being destroyed! Make sure we are
1145 * physically on the right pmap:
1149 /* force pmap/cr3 update */
1152 VM_MAX_KERNEL_ADDRESS
);
1153 #endif /* NCPUS > 1 */
1155 if (real_pmap
[my_cpu
] == p
) {
1156 PMAP_CPU_CLR(p
, my_cpu
);
1157 real_pmap
[my_cpu
] = kernel_pmap
;
1158 set_cr3(kernel_pmap
->pdirbase
);
1160 mp_enable_preemption();
1162 simple_unlock(&p
->lock
);
1166 return; /* still in use */
1170 * Free the memory maps, then the
1174 while (pdep
< &p
->dirbase
[pdenum(p
, LINEAR_KERNEL_ADDRESS
)]) {
1175 if (*pdep
& INTEL_PTE_VALID
) {
1176 pa
= pte_to_pa(*pdep
);
1177 vm_object_lock(pmap_object
);
1178 m
= vm_page_lookup(pmap_object
, pa
);
1179 if (m
== VM_PAGE_NULL
)
1180 panic("pmap_destroy: pte page not in object");
1181 vm_page_lock_queues();
1183 inuse_ptepages_count
--;
1184 vm_object_unlock(pmap_object
);
1185 vm_page_unlock_queues();
1188 * Clear pdes, this might be headed for the cache.
1190 c
= ptes_per_vm_page
;
1197 pdep
+= ptes_per_vm_page
;
1203 * XXX These asserts fail on system shutdown.
1205 assert(p->stats.resident_count == 0);
1206 assert(p->stats.wired_count == 0);
1211 * Add to cache if not already full
1213 simple_lock(&pmap_cache_lock
);
1214 if (pmap_cache_count
<= pmap_cache_max
) {
1215 p
->ref_count
= (int) pmap_cache_list
;
1216 pmap_cache_list
= p
;
1218 simple_unlock(&pmap_cache_lock
);
1221 simple_unlock(&pmap_cache_lock
);
1222 kmem_free(kernel_map
, (vm_offset_t
)p
->dirbase
, INTEL_PGBYTES
);
1223 zfree(pmap_zone
, (vm_offset_t
) p
);
1228 * Add a reference to the specified pmap.
1237 if (p
!= PMAP_NULL
) {
1239 simple_lock(&p
->lock
);
1241 simple_unlock(&p
->lock
);
1247 * Remove a range of hardware page-table entries.
1248 * The entries given are the first (inclusive)
1249 * and last (exclusive) entries for the VM pages.
1250 * The virtual address is the va for the first pte.
1252 * The pmap must be locked.
1253 * If the pmap is not the kernel pmap, the range must lie
1254 * entirely within one pte-page. This is NOT checked.
1255 * Assumes that the pte-page exists.
1266 register pt_entry_t
*cpte
;
1267 int num_removed
, num_unwired
;
1272 if (pmap
!= kernel_pmap
)
1273 ptep_check(get_pte_page(spte
));
1274 #endif /* DEBUG_PTE_PAGE */
1278 for (cpte
= spte
; cpte
< epte
;
1279 cpte
+= ptes_per_vm_page
, va
+= PAGE_SIZE
) {
1281 pa
= pte_to_pa(*cpte
);
1289 if (!valid_page(pa
)) {
1292 * Outside range of managed physical memory.
1293 * Just remove the mappings.
1295 register int i
= ptes_per_vm_page
;
1296 register pt_entry_t
*lpte
= cpte
;
1308 * Get the modify and reference bits.
1312 register pt_entry_t
*lpte
;
1314 i
= ptes_per_vm_page
;
1317 pmap_phys_attributes
[pai
] |=
1318 *lpte
& (PHYS_MODIFIED
|PHYS_REFERENCED
);
1325 * Remove the mapping from the pvlist for
1326 * this physical page.
1329 register pv_entry_t pv_h
, prev
, cur
;
1331 pv_h
= pai_to_pvh(pai
);
1332 if (pv_h
->pmap
== PMAP_NULL
) {
1333 panic("pmap_remove: null pv_list!");
1335 if (pv_h
->va
== va
&& pv_h
->pmap
== pmap
) {
1337 * Header is the pv_entry. Copy the next one
1338 * to header and free the next one (we cannot
1342 if (cur
!= PV_ENTRY_NULL
) {
1347 pv_h
->pmap
= PMAP_NULL
;
1354 if ((cur
= prev
->next
) == PV_ENTRY_NULL
) {
1355 panic("pmap-remove: mapping not in pv_list!");
1357 } while (cur
->va
!= va
|| cur
->pmap
!= pmap
);
1358 prev
->next
= cur
->next
;
1368 assert(pmap
->stats
.resident_count
>= num_removed
);
1369 pmap
->stats
.resident_count
-= num_removed
;
1370 assert(pmap
->stats
.wired_count
>= num_unwired
);
1371 pmap
->stats
.wired_count
-= num_unwired
;
1375 * Remove phys addr if mapped in specified map
1379 pmap_remove_some_phys(
1384 /* Implement to support working set code */
1390 * Remove the given range of addresses
1391 * from the specified map.
1393 * It is assumed that the start and end are properly
1394 * rounded to the hardware page size.
1405 register pt_entry_t
*pde
;
1406 register pt_entry_t
*spte
, *epte
;
1410 if (map
== PMAP_NULL
)
1413 PMAP_READ_LOCK(map
, spl
);
1415 if (value_64bit(s64
) || value_64bit(e64
)) {
1416 panic("pmap_remove addr overflow");
1419 s
= (vm_offset_t
)low32(s64
);
1420 e
= (vm_offset_t
)low32(e64
);
1423 * Invalidate the translation buffer first
1425 PMAP_UPDATE_TLBS(map
, s
, e
);
1427 pde
= pmap_pde(map
, s
);
1430 l
= (s
+ PDE_MAPPED_SIZE
) & ~(PDE_MAPPED_SIZE
-1);
1433 if (*pde
& INTEL_PTE_VALID
) {
1434 spte
= (pt_entry_t
*)ptetokv(*pde
);
1435 spte
= &spte
[ptenum(s
)];
1436 epte
= &spte
[intel_btop(l
-s
)];
1437 pmap_remove_range(map
, s
, spte
, epte
);
1443 PMAP_READ_UNLOCK(map
, spl
);
1447 * Routine: pmap_page_protect
1450 * Lower the permission for all mappings to a given
1458 pv_entry_t pv_h
, prev
;
1459 register pv_entry_t pv_e
;
1460 register pt_entry_t
*pte
;
1462 register pmap_t pmap
;
1467 assert(pn
!= vm_page_fictitious_addr
);
1468 phys
= (vm_offset_t
)i386_ptob(pn
);
1469 if (!valid_page(phys
)) {
1471 * Not a managed page.
1477 * Determine the new protection.
1481 case VM_PROT_READ
|VM_PROT_EXECUTE
:
1485 return; /* nothing to do */
1492 * Lock the pmap system first, since we will be changing
1496 PMAP_WRITE_LOCK(spl
);
1498 pai
= pa_index(phys
);
1499 pv_h
= pai_to_pvh(pai
);
1502 * Walk down PV list, changing or removing all mappings.
1503 * We do not have to lock the pv_list because we have
1504 * the entire pmap system locked.
1506 if (pv_h
->pmap
!= PMAP_NULL
) {
1512 * Lock the pmap to block pmap_extract and similar routines.
1514 simple_lock(&pmap
->lock
);
1517 register vm_offset_t va
;
1520 pte
= pmap_pte(pmap
, va
);
1523 * Consistency checks.
1525 /* assert(*pte & INTEL_PTE_VALID); XXX */
1526 /* assert(pte_to_phys(*pte) == phys); */
1529 * Invalidate TLBs for all CPUs using this mapping.
1531 PMAP_UPDATE_TLBS(pmap
, va
, va
+ PAGE_SIZE
);
1535 * Remove the mapping if new protection is NONE
1536 * or if write-protecting a kernel mapping.
1538 if (remove
|| pmap
== kernel_pmap
) {
1540 * Remove the mapping, collecting any modify bits.
1543 register int i
= ptes_per_vm_page
;
1546 pmap_phys_attributes
[pai
] |=
1547 *pte
& (PHYS_MODIFIED
|PHYS_REFERENCED
);
1552 assert(pmap
->stats
.resident_count
>= 1);
1553 pmap
->stats
.resident_count
--;
1556 * Remove the pv_entry.
1560 * Fix up head later.
1562 pv_h
->pmap
= PMAP_NULL
;
1566 * Delete this entry.
1568 prev
->next
= pv_e
->next
;
1576 register int i
= ptes_per_vm_page
;
1579 *pte
&= ~INTEL_PTE_WRITE
;
1589 simple_unlock(&pmap
->lock
);
1591 } while ((pv_e
= prev
->next
) != PV_ENTRY_NULL
);
1594 * If pv_head mapping was removed, fix it up.
1596 if (pv_h
->pmap
== PMAP_NULL
) {
1598 if (pv_e
!= PV_ENTRY_NULL
) {
1605 PMAP_WRITE_UNLOCK(spl
);
1609 * Set the physical protection on the
1610 * specified range of this map as requested.
1611 * Will not increase permissions.
1620 register pt_entry_t
*pde
;
1621 register pt_entry_t
*spte
, *epte
;
1626 if (map
== PMAP_NULL
)
1630 * Determine the new protection.
1634 case VM_PROT_READ
|VM_PROT_EXECUTE
:
1636 case VM_PROT_READ
|VM_PROT_WRITE
:
1638 return; /* nothing to do */
1640 pmap_remove(map
, (addr64_t
)s
, (addr64_t
)e
);
1645 * If write-protecting in the kernel pmap,
1646 * remove the mappings; the i386 ignores
1647 * the write-permission bit in kernel mode.
1649 * XXX should be #if'd for i386
1652 if (cpuid_family() == CPUID_FAMILY_386
)
1653 if (map
== kernel_pmap
) {
1654 pmap_remove(map
, (addr64_t
)s
, (addr64_t
)e
);
1659 simple_lock(&map
->lock
);
1662 * Invalidate the translation buffer first
1664 PMAP_UPDATE_TLBS(map
, s
, e
);
1666 pde
= pmap_pde(map
, s
);
1668 l
= (s
+ PDE_MAPPED_SIZE
) & ~(PDE_MAPPED_SIZE
-1);
1671 if (*pde
& INTEL_PTE_VALID
) {
1672 spte
= (pt_entry_t
*)ptetokv(*pde
);
1673 spte
= &spte
[ptenum(s
)];
1674 epte
= &spte
[intel_btop(l
-s
)];
1676 while (spte
< epte
) {
1677 if (*spte
& INTEL_PTE_VALID
)
1678 *spte
&= ~INTEL_PTE_WRITE
;
1686 simple_unlock(&map
->lock
);
1693 * Insert the given physical page (p) at
1694 * the specified virtual address (v) in the
1695 * target physical map with the protection requested.
1697 * If specified, the page will be wired down, meaning
1698 * that the related pte cannot be reclaimed.
1700 * NB: This is the only routine which MAY NOT lazy-evaluate
1701 * or lose information. That is, this routine must actually
1702 * insert this page into the given map NOW.
1706 register pmap_t pmap
,
1713 register pt_entry_t
*pte
;
1714 register pv_entry_t pv_h
;
1715 register int i
, pai
;
1717 pt_entry_t
template;
1720 vm_offset_t pa
= (vm_offset_t
)i386_ptob(pn
);
1722 XPR(0x80000000, "%x/%x: pmap_enter %x/%x/%x\n",
1723 current_thread()->top_act
,
1727 assert(pn
!= vm_page_fictitious_addr
);
1729 printf("pmap(%x, %x)\n", v
, pn
);
1730 if (pmap
== PMAP_NULL
)
1733 if (cpuid_family() == CPUID_FAMILY_386
)
1734 if (pmap
== kernel_pmap
&& (prot
& VM_PROT_WRITE
) == 0
1735 && !wired
/* hack for io_wire */ ) {
1737 * Because the 386 ignores write protection in kernel mode,
1738 * we cannot enter a read-only kernel mapping, and must
1739 * remove an existing mapping if changing it.
1741 * XXX should be #if'd for i386
1743 PMAP_READ_LOCK(pmap
, spl
);
1745 pte
= pmap_pte(pmap
, v
);
1746 if (pte
!= PT_ENTRY_NULL
&& pte_to_pa(*pte
) != 0) {
1748 * Invalidate the translation buffer,
1749 * then remove the mapping.
1751 PMAP_UPDATE_TLBS(pmap
, v
, v
+ PAGE_SIZE
);
1752 pmap_remove_range(pmap
, v
, pte
,
1753 pte
+ ptes_per_vm_page
);
1755 PMAP_READ_UNLOCK(pmap
, spl
);
1760 * Must allocate a new pvlist entry while we're unlocked;
1761 * zalloc may cause pageout (which will lock the pmap system).
1762 * If we determine we need a pvlist entry, we will unlock
1763 * and allocate one. Then we will retry, throughing away
1764 * the allocated entry later (if we no longer need it).
1766 pv_e
= PV_ENTRY_NULL
;
1768 PMAP_READ_LOCK(pmap
, spl
);
1771 * Expand pmap to include this pte. Assume that
1772 * pmap is always expanded to include enough hardware
1773 * pages to map one VM page.
1776 while ((pte
= pmap_pte(pmap
, v
)) == PT_ENTRY_NULL
) {
1778 * Must unlock to expand the pmap.
1780 PMAP_READ_UNLOCK(pmap
, spl
);
1782 pmap_expand(pmap
, v
);
1784 PMAP_READ_LOCK(pmap
, spl
);
1787 * Special case if the physical page is already mapped
1790 old_pa
= pte_to_pa(*pte
);
1793 * May be changing its wired attribute or protection
1796 template = pa_to_pte(pa
) | INTEL_PTE_VALID
;
1798 if(flags
& VM_MEM_NOT_CACHEABLE
) {
1799 if(!(flags
& VM_MEM_GUARDED
))
1800 template |= INTEL_PTE_PTA
;
1801 template |= INTEL_PTE_NCACHE
;
1804 if (pmap
!= kernel_pmap
)
1805 template |= INTEL_PTE_USER
;
1806 if (prot
& VM_PROT_WRITE
)
1807 template |= INTEL_PTE_WRITE
;
1809 template |= INTEL_PTE_WIRED
;
1811 pmap
->stats
.wired_count
++;
1814 if (iswired(*pte
)) {
1815 assert(pmap
->stats
.wired_count
>= 1);
1816 pmap
->stats
.wired_count
--;
1820 PMAP_UPDATE_TLBS(pmap
, v
, v
+ PAGE_SIZE
);
1821 i
= ptes_per_vm_page
;
1823 if (*pte
& INTEL_PTE_MOD
)
1824 template |= INTEL_PTE_MOD
;
1825 WRITE_PTE(pte
, template)
1827 pte_increment_pa(template);
1834 * Outline of code from here:
1835 * 1) If va was mapped, update TLBs, remove the mapping
1836 * and remove old pvlist entry.
1837 * 2) Add pvlist entry for new mapping
1838 * 3) Enter new mapping.
1840 * SHARING_FAULTS complicates this slightly in that it cannot
1841 * replace the mapping, but must remove it (because adding the
1842 * pvlist entry for the new mapping may remove others), and
1843 * hence always enters the new mapping at step 3)
1845 * If the old physical page is not managed step 1) is skipped
1846 * (except for updating the TLBs), and the mapping is
1847 * overwritten at step 3). If the new physical page is not
1848 * managed, step 2) is skipped.
1851 if (old_pa
!= (vm_offset_t
) 0) {
1853 PMAP_UPDATE_TLBS(pmap
, v
, v
+ PAGE_SIZE
);
1856 if (pmap
!= kernel_pmap
)
1857 ptep_check(get_pte_page(pte
));
1858 #endif /* DEBUG_PTE_PAGE */
1861 * Don't do anything to pages outside valid memory here.
1862 * Instead convince the code that enters a new mapping
1863 * to overwrite the old one.
1866 if (valid_page(old_pa
)) {
1868 pai
= pa_index(old_pa
);
1871 assert(pmap
->stats
.resident_count
>= 1);
1872 pmap
->stats
.resident_count
--;
1873 if (iswired(*pte
)) {
1874 assert(pmap
->stats
.wired_count
>= 1);
1875 pmap
->stats
.wired_count
--;
1877 i
= ptes_per_vm_page
;
1879 pmap_phys_attributes
[pai
] |=
1880 *pte
& (PHYS_MODIFIED
|PHYS_REFERENCED
);
1883 pte_increment_pa(template);
1887 * Put pte back to beginning of page since it'll be
1888 * used later to enter the new page.
1890 pte
-= ptes_per_vm_page
;
1893 * Remove the mapping from the pvlist for
1894 * this physical page.
1897 register pv_entry_t prev
, cur
;
1899 pv_h
= pai_to_pvh(pai
);
1900 if (pv_h
->pmap
== PMAP_NULL
) {
1901 panic("pmap_enter: null pv_list!");
1903 if (pv_h
->va
== v
&& pv_h
->pmap
== pmap
) {
1905 * Header is the pv_entry. Copy the next one
1906 * to header and free the next one (we cannot
1910 if (cur
!= PV_ENTRY_NULL
) {
1915 pv_h
->pmap
= PMAP_NULL
;
1922 if ((cur
= prev
->next
) == PV_ENTRY_NULL
) {
1923 panic("pmap_enter: mapping not in pv_list!");
1925 } while (cur
->va
!= v
|| cur
->pmap
!= pmap
);
1926 prev
->next
= cur
->next
;
1935 * old_pa is not managed. Pretend it's zero so code
1936 * at Step 3) will enter new mapping (overwriting old
1937 * one). Do removal part of accounting.
1939 old_pa
= (vm_offset_t
) 0;
1940 assert(pmap
->stats
.resident_count
>= 1);
1941 pmap
->stats
.resident_count
--;
1942 if (iswired(*pte
)) {
1943 assert(pmap
->stats
.wired_count
>= 1);
1944 pmap
->stats
.wired_count
--;
1949 if (valid_page(pa
)) {
1952 * Step 2) Enter the mapping in the PV list for this
1962 * We can return here from the sharing fault code below
1963 * in case we removed the only entry on the pv list and thus
1964 * must enter the new one in the list header.
1966 #endif /* SHARING_FAULTS */
1968 pv_h
= pai_to_pvh(pai
);
1970 if (pv_h
->pmap
== PMAP_NULL
) {
1976 pv_h
->next
= PV_ENTRY_NULL
;
1982 * check that this mapping is not already there
1983 * or there is no alias for this mapping in the same map
1985 pv_entry_t e
= pv_h
;
1986 while (e
!= PV_ENTRY_NULL
) {
1987 if (e
->pmap
== pmap
&& e
->va
== v
)
1988 panic("pmap_enter: already in pv_list");
1996 * do sharing faults.
1997 * if we find an entry on this pv list in the same address
1998 * space, remove it. we know there will not be more
2001 pv_entry_t e
= pv_h
;
2004 while (e
!= PV_ENTRY_NULL
) {
2005 if (e
->pmap
== pmap
) {
2007 * Remove it, drop pv list lock first.
2011 opte
= pmap_pte(pmap
, e
->va
);
2012 assert(opte
!= PT_ENTRY_NULL
);
2014 * Invalidate the translation buffer,
2015 * then remove the mapping.
2017 PMAP_UPDATE_TLBS(pmap
, e
->va
, e
->va
+ PAGE_SIZE
);
2018 pmap_remove_range(pmap
, e
->va
, opte
,
2019 opte
+ ptes_per_vm_page
);
2021 * We could have remove the head entry,
2022 * so there could be no more entries
2023 * and so we have to use the pv head entry.
2024 * so, go back to the top and try the entry
2033 * check that this mapping is not already there
2036 while (e
!= PV_ENTRY_NULL
) {
2037 if (e
->pmap
== pmap
)
2038 panic("pmap_enter: alias in pv_list");
2042 #endif /* SHARING_FAULTS */
2046 * check for aliases within the same address space.
2048 pv_entry_t e
= pv_h
;
2049 vm_offset_t rpc
= get_rpc();
2051 while (e
!= PV_ENTRY_NULL
) {
2052 if (e
->pmap
== pmap
) {
2054 * log this entry in the alias ring buffer
2055 * if it's not there already.
2057 struct pmap_alias
*pma
;
2061 for (ii
= 0; ii
< pmap_alias_index
; ii
++) {
2062 if (pmap_aliasbuf
[ii
].rpc
== rpc
) {
2063 /* found it in the log already */
2069 pma
= &pmap_aliasbuf
[pmap_alias_index
];
2073 pma
->cookie
= PMAP_ALIAS_COOKIE
;
2074 if (++pmap_alias_index
>= PMAP_ALIAS_MAX
)
2075 panic("pmap_enter: exhausted alias log");
2081 #endif /* DEBUG_ALIAS */
2083 * Add new pv_entry after header.
2085 if (pv_e
== PV_ENTRY_NULL
) {
2087 if (pv_e
== PV_ENTRY_NULL
) {
2089 PMAP_READ_UNLOCK(pmap
, spl
);
2094 pv_e
= (pv_entry_t
) zalloc(pv_list_zone
);
2100 pv_e
->next
= pv_h
->next
;
2103 * Remember that we used the pvlist entry.
2105 pv_e
= PV_ENTRY_NULL
;
2111 * Step 3) Enter and count the mapping.
2114 pmap
->stats
.resident_count
++;
2117 * Build a template to speed up entering -
2118 * only the pfn changes.
2120 template = pa_to_pte(pa
) | INTEL_PTE_VALID
;
2122 if(flags
& VM_MEM_NOT_CACHEABLE
) {
2123 if(!(flags
& VM_MEM_GUARDED
))
2124 template |= INTEL_PTE_PTA
;
2125 template |= INTEL_PTE_NCACHE
;
2128 if (pmap
!= kernel_pmap
)
2129 template |= INTEL_PTE_USER
;
2130 if (prot
& VM_PROT_WRITE
)
2131 template |= INTEL_PTE_WRITE
;
2133 template |= INTEL_PTE_WIRED
;
2134 pmap
->stats
.wired_count
++;
2136 i
= ptes_per_vm_page
;
2138 WRITE_PTE(pte
, template)
2140 pte_increment_pa(template);
2143 if (pv_e
!= PV_ENTRY_NULL
) {
2147 PMAP_READ_UNLOCK(pmap
, spl
);
2151 * Routine: pmap_change_wiring
2152 * Function: Change the wiring attribute for a map/virtual-address
2154 * In/out conditions:
2155 * The mapping must already exist in the pmap.
2159 register pmap_t map
,
2163 register pt_entry_t
*pte
;
2169 * We must grab the pmap system lock because we may
2170 * change a pte_page queue.
2172 PMAP_READ_LOCK(map
, spl
);
2174 if ((pte
= pmap_pte(map
, v
)) == PT_ENTRY_NULL
)
2175 panic("pmap_change_wiring: pte missing");
2177 if (wired
&& !iswired(*pte
)) {
2179 * wiring down mapping
2181 map
->stats
.wired_count
++;
2182 i
= ptes_per_vm_page
;
2184 *pte
++ |= INTEL_PTE_WIRED
;
2187 else if (!wired
&& iswired(*pte
)) {
2191 assert(map
->stats
.wired_count
>= 1);
2192 map
->stats
.wired_count
--;
2193 i
= ptes_per_vm_page
;
2195 *pte
++ &= ~INTEL_PTE_WIRED
;
2199 PMAP_READ_UNLOCK(map
, spl
);
2208 pmap_find_phys(pmap_t pmap
, addr64_t va
)
2214 if (value_64bit(va
)) panic("pmap_find_phys 64 bit value");
2215 a32
= (vm_offset_t
)low32(va
);
2216 ptp
= pmap_pte(pmap
, a32
);
2217 if (PT_ENTRY_NULL
== ptp
)
2219 ppn
= (ppnum_t
)i386_btop(pte_to_pa(*ptp
));
2224 * Routine: pmap_extract
2226 * Extract the physical page address associated
2227 * with the given map/virtual_address pair.
2232 register pmap_t pmap
,
2235 register pt_entry_t
*pte
;
2236 register vm_offset_t pa
;
2240 simple_lock(&pmap
->lock
);
2241 if ((pte
= pmap_pte(pmap
, va
)) == PT_ENTRY_NULL
)
2242 pa
= (vm_offset_t
) 0;
2243 else if (!(*pte
& INTEL_PTE_VALID
))
2244 pa
= (vm_offset_t
) 0;
2246 pa
= pte_to_pa(*pte
) + (va
& INTEL_OFFMASK
);
2247 simple_unlock(&pmap
->lock
);
2253 * Routine: pmap_expand
2255 * Expands a pmap to be able to map the specified virtual address.
2257 * Allocates new virtual memory for the P0 or P1 portion of the
2258 * pmap, then re-maps the physical pages that were in the old
2259 * pmap to be in the new pmap.
2261 * Must be called with the pmap system and the pmap unlocked,
2262 * since these must be unlocked to use vm_allocate or vm_deallocate.
2263 * Thus it must be called in a loop that checks whether the map
2264 * has been expanded enough.
2265 * (We won't loop forever, since page tables aren't shrunk.)
2269 register pmap_t map
,
2270 register vm_offset_t v
)
2273 register vm_page_t m
;
2274 register vm_offset_t pa
;
2279 if (map
== kernel_pmap
)
2280 panic("pmap_expand");
2283 * We cannot allocate the pmap_object in pmap_init,
2284 * because it is called before the zone package is up.
2285 * Allocate it now if it is missing.
2287 if (pmap_object
== VM_OBJECT_NULL
)
2288 pmap_object
= vm_object_allocate(avail_end
);
2291 * Allocate a VM page for the level 2 page table entries.
2293 while ((m
= vm_page_grab()) == VM_PAGE_NULL
)
2297 * Map the page to its physical address so that it
2298 * can be found later.
2302 vm_object_lock(pmap_object
);
2303 vm_page_insert(m
, pmap_object
, (vm_object_offset_t
)pa
);
2304 vm_page_lock_queues();
2306 inuse_ptepages_count
++;
2307 vm_object_unlock(pmap_object
);
2308 vm_page_unlock_queues();
2313 memset((void *)phystokv(pa
), 0, PAGE_SIZE
);
2315 PMAP_READ_LOCK(map
, spl
);
2317 * See if someone else expanded us first
2319 if (pmap_pte(map
, v
) != PT_ENTRY_NULL
) {
2320 PMAP_READ_UNLOCK(map
, spl
);
2321 vm_object_lock(pmap_object
);
2322 vm_page_lock_queues();
2324 inuse_ptepages_count
--;
2325 vm_page_unlock_queues();
2326 vm_object_unlock(pmap_object
);
2331 * Set the page directory entry for this page table.
2332 * If we have allocated more than one hardware page,
2333 * set several page directory entries.
2336 i
= ptes_per_vm_page
;
2337 pdp
= &map
->dirbase
[pdenum(map
, v
) & ~(i
-1)];
2339 *pdp
= pa_to_pte(pa
)
2344 pa
+= INTEL_PGBYTES
;
2347 PMAP_READ_UNLOCK(map
, spl
);
2352 * Copy the range specified by src_addr/len
2353 * from the source map to the range dst_addr/len
2354 * in the destination map.
2356 * This routine is only advisory and need not do anything.
2363 vm_offset_t dst_addr
,
2365 vm_offset_t src_addr
)
2368 dst_pmap
++; src_pmap
++; dst_addr
++; len
++; src_addr
++;
2374 * pmap_sync_caches_phys(ppnum_t pa)
2376 * Invalidates all of the instruction cache on a physical page and
2377 * pushes any dirty data from the data cache for the same physical page
2380 void pmap_sync_caches_phys(ppnum_t pa
)
2382 // if (!(cpuid_features() & CPUID_FEATURE_SS))
2384 __asm__
volatile("wbinvd");
2393 * Routine: pmap_collect
2395 * Garbage collects the physical map system for
2396 * pages which are no longer used.
2397 * Success need not be guaranteed -- that is, there
2398 * may well be pages which are not referenced, but
2399 * others may be collected.
2401 * Called by the pageout daemon when pages are scarce.
2407 register pt_entry_t
*pdp
, *ptp
;
2416 if (p
== kernel_pmap
)
2420 * Garbage collect map.
2422 PMAP_READ_LOCK(p
, spl
);
2423 PMAP_UPDATE_TLBS(p
, VM_MIN_ADDRESS
, VM_MAX_ADDRESS
);
2425 for (pdp
= p
->dirbase
;
2426 pdp
< &p
->dirbase
[pdenum(p
, LINEAR_KERNEL_ADDRESS
)];
2427 pdp
+= ptes_per_vm_page
)
2429 if (*pdp
& INTEL_PTE_VALID
)
2430 if(*pdp
& INTEL_PTE_REF
) {
2431 *pdp
&= ~INTEL_PTE_REF
;
2435 pa
= pte_to_pa(*pdp
);
2436 ptp
= (pt_entry_t
*)phystokv(pa
);
2437 eptp
= ptp
+ NPTES
*ptes_per_vm_page
;
2440 * If the pte page has any wired mappings, we cannot
2445 register pt_entry_t
*ptep
;
2446 for (ptep
= ptp
; ptep
< eptp
; ptep
++) {
2447 if (iswired(*ptep
)) {
2455 * Remove the virtual addresses mapped by this pte page.
2457 pmap_remove_range(p
,
2458 pdetova(pdp
- p
->dirbase
),
2463 * Invalidate the page directory pointer.
2466 register int i
= ptes_per_vm_page
;
2467 register pt_entry_t
*pdep
= pdp
;
2473 PMAP_READ_UNLOCK(p
, spl
);
2476 * And free the pte page itself.
2479 register vm_page_t m
;
2481 vm_object_lock(pmap_object
);
2482 m
= vm_page_lookup(pmap_object
, pa
);
2483 if (m
== VM_PAGE_NULL
)
2484 panic("pmap_collect: pte page not in object");
2485 vm_page_lock_queues();
2487 inuse_ptepages_count
--;
2488 vm_page_unlock_queues();
2489 vm_object_unlock(pmap_object
);
2492 PMAP_READ_LOCK(p
, spl
);
2496 PMAP_READ_UNLOCK(p
, spl
);
2502 * Routine: pmap_kernel
2504 * Returns the physical map handle for the kernel.
2510 return (kernel_pmap
);
2515 * pmap_zero_page zeros the specified (machine independent) page.
2516 * See machine/phys.c or machine/phys.s for implementation.
2521 register vm_offset_t phys
)
2525 assert(phys
!= vm_page_fictitious_addr
);
2526 i
= PAGE_SIZE
/ INTEL_PGBYTES
;
2527 phys
= intel_pfn(phys
);
2535 * pmap_copy_page copies the specified (machine independent) page.
2536 * See machine/phys.c or machine/phys.s for implementation.
2546 assert(src
!= vm_page_fictitious_addr
);
2547 assert(dst
!= vm_page_fictitious_addr
);
2548 i
= PAGE_SIZE
/ INTEL_PGBYTES
;
2551 copy_phys(intel_pfn(src
), intel_pfn(dst
));
2552 src
+= INTEL_PGBYTES
;
2553 dst
+= INTEL_PGBYTES
;
2559 * Routine: pmap_pageable
2561 * Make the specified pages (by pmap, offset)
2562 * pageable (or not) as requested.
2564 * A page which is not pageable may not take
2565 * a fault; therefore, its page table entry
2566 * must remain valid for the duration.
2568 * This routine is merely advisory; pmap_enter
2569 * will specify that these pages are to be wired
2570 * down (or not) as appropriate.
2580 pmap
++; start
++; end
++; pageable
++;
2585 * Clear specified attribute bits.
2588 phys_attribute_clear(
2593 register pv_entry_t pv_e
;
2594 register pt_entry_t
*pte
;
2596 register pmap_t pmap
;
2599 assert(phys
!= vm_page_fictitious_addr
);
2600 if (!valid_page(phys
)) {
2602 * Not a managed page.
2608 * Lock the pmap system first, since we will be changing
2612 PMAP_WRITE_LOCK(spl
);
2614 pai
= pa_index(phys
);
2615 pv_h
= pai_to_pvh(pai
);
2618 * Walk down PV list, clearing all modify or reference bits.
2619 * We do not have to lock the pv_list because we have
2620 * the entire pmap system locked.
2622 if (pv_h
->pmap
!= PMAP_NULL
) {
2624 * There are some mappings.
2626 for (pv_e
= pv_h
; pv_e
!= PV_ENTRY_NULL
; pv_e
= pv_e
->next
) {
2630 * Lock the pmap to block pmap_extract and similar routines.
2632 simple_lock(&pmap
->lock
);
2635 register vm_offset_t va
;
2638 pte
= pmap_pte(pmap
, va
);
2642 * Consistency checks.
2644 assert(*pte
& INTEL_PTE_VALID
);
2645 /* assert(pte_to_phys(*pte) == phys); */
2649 * Invalidate TLBs for all CPUs using this mapping.
2651 PMAP_UPDATE_TLBS(pmap
, va
, va
+ PAGE_SIZE
);
2655 * Clear modify or reference bits.
2658 register int i
= ptes_per_vm_page
;
2663 simple_unlock(&pmap
->lock
);
2667 pmap_phys_attributes
[pai
] &= ~bits
;
2669 PMAP_WRITE_UNLOCK(spl
);
2673 * Check specified attribute bits.
2676 phys_attribute_test(
2681 register pv_entry_t pv_e
;
2682 register pt_entry_t
*pte
;
2684 register pmap_t pmap
;
2687 assert(phys
!= vm_page_fictitious_addr
);
2688 if (!valid_page(phys
)) {
2690 * Not a managed page.
2696 * Lock the pmap system first, since we will be checking
2700 PMAP_WRITE_LOCK(spl
);
2702 pai
= pa_index(phys
);
2703 pv_h
= pai_to_pvh(pai
);
2705 if (pmap_phys_attributes
[pai
] & bits
) {
2706 PMAP_WRITE_UNLOCK(spl
);
2711 * Walk down PV list, checking all mappings.
2712 * We do not have to lock the pv_list because we have
2713 * the entire pmap system locked.
2715 if (pv_h
->pmap
!= PMAP_NULL
) {
2717 * There are some mappings.
2719 for (pv_e
= pv_h
; pv_e
!= PV_ENTRY_NULL
; pv_e
= pv_e
->next
) {
2723 * Lock the pmap to block pmap_extract and similar routines.
2725 simple_lock(&pmap
->lock
);
2728 register vm_offset_t va
;
2731 pte
= pmap_pte(pmap
, va
);
2735 * Consistency checks.
2737 assert(*pte
& INTEL_PTE_VALID
);
2738 /* assert(pte_to_phys(*pte) == phys); */
2743 * Check modify or reference bits.
2746 register int i
= ptes_per_vm_page
;
2749 if (*pte
++ & bits
) {
2750 simple_unlock(&pmap
->lock
);
2751 PMAP_WRITE_UNLOCK(spl
);
2756 simple_unlock(&pmap
->lock
);
2759 PMAP_WRITE_UNLOCK(spl
);
2764 * Set specified attribute bits.
2773 assert(phys
!= vm_page_fictitious_addr
);
2774 if (!valid_page(phys
)) {
2776 * Not a managed page.
2782 * Lock the pmap system and set the requested bits in
2783 * the phys attributes array. Don't need to bother with
2784 * ptes because the test routine looks here first.
2787 PMAP_WRITE_LOCK(spl
);
2788 pmap_phys_attributes
[pa_index(phys
)] |= bits
;
2789 PMAP_WRITE_UNLOCK(spl
);
2793 * Set the modify bit on the specified physical page.
2796 void pmap_set_modify(
2799 vm_offset_t phys
= (vm_offset_t
)i386_ptob(pn
);
2800 phys_attribute_set(phys
, PHYS_MODIFIED
);
2804 * Clear the modify bits on the specified physical page.
2811 vm_offset_t phys
= (vm_offset_t
)i386_ptob(pn
);
2812 phys_attribute_clear(phys
, PHYS_MODIFIED
);
2818 * Return whether or not the specified physical page is modified
2819 * by any physical maps.
2826 vm_offset_t phys
= (vm_offset_t
)i386_ptob(pn
);
2827 return (phys_attribute_test(phys
, PHYS_MODIFIED
));
2831 * pmap_clear_reference:
2833 * Clear the reference bit on the specified physical page.
2837 pmap_clear_reference(
2840 vm_offset_t phys
= (vm_offset_t
)i386_ptob(pn
);
2841 phys_attribute_clear(phys
, PHYS_REFERENCED
);
2845 * pmap_is_referenced:
2847 * Return whether or not the specified physical page is referenced
2848 * by any physical maps.
2855 vm_offset_t phys
= (vm_offset_t
)i386_ptob(pn
);
2856 return (phys_attribute_test(phys
, PHYS_REFERENCED
));
2860 * Set the modify bit on the specified range
2861 * of this map as requested.
2863 * This optimization stands only if each time the dirty bit
2864 * in vm_page_t is tested, it is also tested in the pmap.
2873 register pt_entry_t
*pde
;
2874 register pt_entry_t
*spte
, *epte
;
2877 if (map
== PMAP_NULL
)
2880 PMAP_READ_LOCK(map
, spl
);
2883 * Invalidate the translation buffer first
2885 PMAP_UPDATE_TLBS(map
, s
, e
);
2887 pde
= pmap_pde(map
, s
);
2888 while (s
&& s
< e
) {
2889 l
= (s
+ PDE_MAPPED_SIZE
) & ~(PDE_MAPPED_SIZE
-1);
2892 if (*pde
& INTEL_PTE_VALID
) {
2893 spte
= (pt_entry_t
*)ptetokv(*pde
);
2895 spte
= &spte
[ptenum(s
)];
2896 epte
= &spte
[intel_btop(l
-s
)];
2898 epte
= &spte
[intel_btop(PDE_MAPPED_SIZE
)];
2899 spte
= &spte
[ptenum(s
)];
2901 while (spte
< epte
) {
2902 if (*spte
& INTEL_PTE_VALID
) {
2903 *spte
|= (INTEL_PTE_MOD
| INTEL_PTE_WRITE
);
2911 PMAP_READ_UNLOCK(map
, spl
);
2916 invalidate_icache(vm_offset_t addr
, unsigned cnt
, int phys
)
2921 flush_dcache(vm_offset_t addr
, unsigned count
, int phys
)
2928 * TLB Coherence Code (TLB "shootdown" code)
2930 * Threads that belong to the same task share the same address space and
2931 * hence share a pmap. However, they may run on distinct cpus and thus
2932 * have distinct TLBs that cache page table entries. In order to guarantee
2933 * the TLBs are consistent, whenever a pmap is changed, all threads that
2934 * are active in that pmap must have their TLB updated. To keep track of
2935 * this information, the set of cpus that are currently using a pmap is
2936 * maintained within each pmap structure (cpus_using). Pmap_activate() and
2937 * pmap_deactivate add and remove, respectively, a cpu from this set.
2938 * Since the TLBs are not addressable over the bus, each processor must
2939 * flush its own TLB; a processor that needs to invalidate another TLB
2940 * needs to interrupt the processor that owns that TLB to signal the
2943 * Whenever a pmap is updated, the lock on that pmap is locked, and all
2944 * cpus using the pmap are signaled to invalidate. All threads that need
2945 * to activate a pmap must wait for the lock to clear to await any updates
2946 * in progress before using the pmap. They must ACQUIRE the lock to add
2947 * their cpu to the cpus_using set. An implicit assumption made
2948 * throughout the TLB code is that all kernel code that runs at or higher
2949 * than splvm blocks out update interrupts, and that such code does not
2950 * touch pageable pages.
2952 * A shootdown interrupt serves another function besides signaling a
2953 * processor to invalidate. The interrupt routine (pmap_update_interrupt)
2954 * waits for the both the pmap lock (and the kernel pmap lock) to clear,
2955 * preventing user code from making implicit pmap updates while the
2956 * sending processor is performing its update. (This could happen via a
2957 * user data write reference that turns on the modify bit in the page
2958 * table). It must wait for any kernel updates that may have started
2959 * concurrently with a user pmap update because the IPC code
2961 * Spinning on the VALUES of the locks is sufficient (rather than
2962 * having to acquire the locks) because any updates that occur subsequent
2963 * to finding the lock unlocked will be signaled via another interrupt.
2964 * (This assumes the interrupt is cleared before the low level interrupt code
2965 * calls pmap_update_interrupt()).
2967 * The signaling processor must wait for any implicit updates in progress
2968 * to terminate before continuing with its update. Thus it must wait for an
2969 * acknowledgement of the interrupt from each processor for which such
2970 * references could be made. For maintaining this information, a set
2971 * cpus_active is used. A cpu is in this set if and only if it can
2972 * use a pmap. When pmap_update_interrupt() is entered, a cpu is removed from
2973 * this set; when all such cpus are removed, it is safe to update.
2975 * Before attempting to acquire the update lock on a pmap, a cpu (A) must
2976 * be at least at the priority of the interprocessor interrupt
2977 * (splip<=splvm). Otherwise, A could grab a lock and be interrupted by a
2978 * kernel update; it would spin forever in pmap_update_interrupt() trying
2979 * to acquire the user pmap lock it had already acquired. Furthermore A
2980 * must remove itself from cpus_active. Otherwise, another cpu holding
2981 * the lock (B) could be in the process of sending an update signal to A,
2982 * and thus be waiting for A to remove itself from cpus_active. If A is
2983 * spinning on the lock at priority this will never happen and a deadlock
2988 * Signal another CPU that it must flush its TLB
2997 register int which_cpu
, j
;
2998 register pmap_update_list_t update_list_p
;
3000 while ((which_cpu
= ffs((unsigned long)use_list
)) != 0) {
3001 which_cpu
-= 1; /* convert to 0 origin */
3003 update_list_p
= &cpu_update_list
[which_cpu
];
3004 simple_lock(&update_list_p
->lock
);
3006 j
= update_list_p
->count
;
3007 if (j
>= UPDATE_LIST_SIZE
) {
3009 * list overflowed. Change last item to
3010 * indicate overflow.
3012 update_list_p
->item
[UPDATE_LIST_SIZE
-1].pmap
= kernel_pmap
;
3013 update_list_p
->item
[UPDATE_LIST_SIZE
-1].start
= VM_MIN_ADDRESS
;
3014 update_list_p
->item
[UPDATE_LIST_SIZE
-1].end
= VM_MAX_KERNEL_ADDRESS
;
3017 update_list_p
->item
[j
].pmap
= pmap
;
3018 update_list_p
->item
[j
].start
= start
;
3019 update_list_p
->item
[j
].end
= end
;
3020 update_list_p
->count
= j
+1;
3022 cpu_update_needed
[which_cpu
] = TRUE
;
3023 simple_unlock(&update_list_p
->lock
);
3025 /* if its the kernel pmap, ignore cpus_idle */
3026 if (((cpus_idle
& (1 << which_cpu
)) == 0) ||
3027 (pmap
== kernel_pmap
) || real_pmap
[which_cpu
] == pmap
)
3029 i386_signal_cpu(which_cpu
, MP_TLB_FLUSH
, ASYNC
);
3031 use_list
&= ~(1 << which_cpu
);
3036 process_pmap_updates(
3037 register pmap_t my_pmap
)
3039 register int my_cpu
;
3040 register pmap_update_list_t update_list_p
;
3042 register pmap_t pmap
;
3044 mp_disable_preemption();
3045 my_cpu
= cpu_number();
3046 update_list_p
= &cpu_update_list
[my_cpu
];
3047 simple_lock(&update_list_p
->lock
);
3049 for (j
= 0; j
< update_list_p
->count
; j
++) {
3050 pmap
= update_list_p
->item
[j
].pmap
;
3051 if (pmap
== my_pmap
||
3052 pmap
== kernel_pmap
) {
3054 if (pmap
->ref_count
<= 0) {
3055 PMAP_CPU_CLR(pmap
, my_cpu
);
3056 real_pmap
[my_cpu
] = kernel_pmap
;
3057 set_cr3(kernel_pmap
->pdirbase
);
3059 INVALIDATE_TLB(pmap
,
3060 update_list_p
->item
[j
].start
,
3061 update_list_p
->item
[j
].end
);
3064 update_list_p
->count
= 0;
3065 cpu_update_needed
[my_cpu
] = FALSE
;
3066 simple_unlock(&update_list_p
->lock
);
3067 mp_enable_preemption();
3071 * Interrupt routine for TBIA requested from other processor.
3072 * This routine can also be called at all interrupts time if
3073 * the cpu was idle. Some driver interrupt routines might access
3074 * newly allocated vm. (This is the case for hd)
3077 pmap_update_interrupt(void)
3079 register int my_cpu
;
3081 register pmap_t my_pmap
;
3083 mp_disable_preemption();
3084 my_cpu
= cpu_number();
3087 * Raise spl to splvm (above splip) to block out pmap_extract
3088 * from IO code (which would put this cpu back in the active
3093 my_pmap
= real_pmap
[my_cpu
];
3095 if (!(my_pmap
&& pmap_in_use(my_pmap
, my_cpu
)))
3096 my_pmap
= kernel_pmap
;
3102 * Indicate that we're not using either user or kernel
3105 i_bit_clear(my_cpu
, &cpus_active
);
3108 * Wait for any pmap updates in progress, on either user
3111 while (*(volatile hw_lock_t
)&my_pmap
->lock
.interlock
||
3112 *(volatile hw_lock_t
)&kernel_pmap
->lock
.interlock
) {
3113 LOOP_CHECK("pmap_update_interrupt", my_pmap
);
3117 process_pmap_updates(my_pmap
);
3119 i_bit_set(my_cpu
, &cpus_active
);
3121 } while (cpu_update_needed
[my_cpu
]);
3124 mp_enable_preemption();
3126 #endif /* NCPUS > 1 */
3130 /* show phys page mappings and attributes */
3132 extern void db_show_page(vm_offset_t pa
);
3135 db_show_page(vm_offset_t pa
)
3142 pv_h
= pai_to_pvh(pai
);
3144 attr
= pmap_phys_attributes
[pai
];
3145 printf("phys page %x ", pa
);
3146 if (attr
& PHYS_MODIFIED
)
3147 printf("modified, ");
3148 if (attr
& PHYS_REFERENCED
)
3149 printf("referenced, ");
3150 if (pv_h
->pmap
|| pv_h
->next
)
3151 printf(" mapped at\n");
3153 printf(" not mapped\n");
3154 for (; pv_h
; pv_h
= pv_h
->next
)
3156 printf("%x in pmap %x\n", pv_h
->va
, pv_h
->pmap
);
3159 #endif /* MACH_KDB */
3162 void db_kvtophys(vm_offset_t
);
3163 void db_show_vaddrs(pt_entry_t
*);
3166 * print out the results of kvtophys(arg)
3172 db_printf("0x%x", kvtophys(vaddr
));
3176 * Walk the pages tables.
3180 pt_entry_t
*dirbase
)
3182 pt_entry_t
*ptep
, *pdep
, tmp
;
3183 int x
, y
, pdecnt
, ptecnt
;
3186 dirbase
= kernel_pmap
->dirbase
;
3189 db_printf("need a dirbase...\n");
3192 dirbase
= (pt_entry_t
*) ((unsigned long) dirbase
& ~INTEL_OFFMASK
);
3194 db_printf("dirbase: 0x%x\n", dirbase
);
3196 pdecnt
= ptecnt
= 0;
3198 for (y
= 0; y
< NPDES
; y
++, pdep
++) {
3199 if (((tmp
= *pdep
) & INTEL_PTE_VALID
) == 0) {
3203 ptep
= (pt_entry_t
*) ((*pdep
) & ~INTEL_OFFMASK
);
3204 db_printf("dir[%4d]: 0x%x\n", y
, *pdep
);
3205 for (x
= 0; x
< NPTES
; x
++, ptep
++) {
3206 if (((tmp
= *ptep
) & INTEL_PTE_VALID
) == 0) {
3210 db_printf(" tab[%4d]: 0x%x, va=0x%x, pa=0x%x\n",
3213 (y
<< 22) | (x
<< 12),
3214 *ptep
& ~INTEL_OFFMASK
);
3218 db_printf("total: %d tables, %d page table entries.\n", pdecnt
, ptecnt
);
3221 #endif /* MACH_KDB */
3223 #include <mach_vm_debug.h>
3225 #include <vm/vm_debug.h>
3228 pmap_list_resident_pages(
3229 register pmap_t pmap
,
3230 register vm_offset_t
*listp
,
3235 #endif /* MACH_VM_DEBUG */
3241 * BSD support routine to reassign virtual addresses.
3245 pmap_movepage(unsigned long from
, unsigned long to
, vm_size_t size
)
3248 pt_entry_t
*pte
, saved_pte
;
3250 /* Lock the kernel map */
3251 PMAP_READ_LOCK(kernel_pmap
, spl
);
3255 pte
= pmap_pte(kernel_pmap
, from
);
3257 panic("pmap_pagemove from pte NULL");
3259 PMAP_READ_UNLOCK(kernel_pmap
, spl
);
3261 pmap_enter(kernel_pmap
, to
, (ppnum_t
)i386_btop(i386_trunc_page(*pte
)),
3262 VM_PROT_READ
|VM_PROT_WRITE
, 0, *pte
& INTEL_PTE_WIRED
);
3264 pmap_remove(kernel_pmap
, (addr64_t
)from
, (addr64_t
)(from
+PAGE_SIZE
));
3266 PMAP_READ_LOCK(kernel_pmap
, spl
);
3267 pte
= pmap_pte(kernel_pmap
, to
);
3269 panic("pmap_pagemove 'to' pte NULL");
3278 /* Get the processors to update the TLBs */
3279 PMAP_UPDATE_TLBS(kernel_pmap
, from
, from
+size
);
3280 PMAP_UPDATE_TLBS(kernel_pmap
, to
, to
+size
);
3282 PMAP_READ_UNLOCK(kernel_pmap
, spl
);
3286 kern_return_t
bmapvideo(vm_offset_t
*info
);
3287 kern_return_t
bmapvideo(vm_offset_t
*info
) {
3289 extern struct vc_info vinfo
;
3290 #ifdef NOTIMPLEMENTED
3291 (void)copyout((char *)&vinfo
, (char *)info
, sizeof(struct vc_info
)); /* Copy out the video info */
3293 return KERN_SUCCESS
;
3296 kern_return_t
bmapmap(vm_offset_t va
, vm_offset_t pa
, vm_size_t size
, vm_prot_t prot
, int attr
);
3297 kern_return_t
bmapmap(vm_offset_t va
, vm_offset_t pa
, vm_size_t size
, vm_prot_t prot
, int attr
) {
3299 #ifdef NOTIMPLEMENTED
3300 pmap_map_block(current_act()->task
->map
->pmap
, va
, pa
, size
, prot
, attr
); /* Map it in */
3302 return KERN_SUCCESS
;
3305 kern_return_t
bmapmapr(vm_offset_t va
);
3306 kern_return_t
bmapmapr(vm_offset_t va
) {
3308 #ifdef NOTIMPLEMENTED
3309 mapping_remove(current_act()->task
->map
->pmap
, va
); /* Remove map */
3311 return KERN_SUCCESS
;
3315 /* temporary workaround */
3317 coredumpok(vm_map_t map
, vm_offset_t va
)
3320 ptep
= pmap_pte(map
->pmap
, va
);
3321 if (0 == ptep
) return FALSE
;
3322 return ((*ptep
& (INTEL_PTE_NCACHE
|INTEL_PTE_WIRED
)) != (INTEL_PTE_NCACHE
|INTEL_PTE_WIRED
));