Home | History | Annotate | Line # | Download | only in include
pmap.h revision 1.40
      1 /*	$NetBSD: pmap.h,v 1.40 1999/06/17 00:12:12 thorpej Exp $	*/
      2 
      3 /*
      4  *
      5  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgment:
     18  *      This product includes software developed by Charles D. Cranor and
     19  *      Washington University.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * pmap.h: see pmap.c for the history of this pmap module.
     37  */
     38 
     39 #ifndef	_I386_PMAP_H_
     40 #define	_I386_PMAP_H_
     41 
     42 #if defined(_KERNEL) && !defined(_LKM)
     43 #include "opt_user_ldt.h"
     44 #endif
     45 
     46 #include <machine/cpufunc.h>
     47 #include <machine/pte.h>
     48 #include <machine/segments.h>
     49 #include <uvm/uvm_object.h>
     50 
     51 /*
     52  * see pte.h for a description of i386 MMU terminology and hardware
     53  * interface.
     54  *
     55  * a pmap describes a processes' 4GB virtual address space.  this
     56  * virtual address space can be broken up into 1024 4MB regions which
     57  * are described by PDEs in the PDP.   the PDEs are defined as follows:
     58  *
     59  * (ranges are inclusive -> exclusive, just like vm_map_entry start/end)
     60  * (the following assumes that KERNBASE is 0xf0000000)
     61  *
     62  * PDE#s	VA range		usage
     63  * 0->959	0x0 -> 0xefc00000	user address space, note that the
     64  *					max user address is 0xefbfe000
     65  *					the final two pages in the last 4MB
     66  *					used to be reserved for the UAREA
     67  *					but now are no longer used
     68  * 959		0xefc00000->		recursive mapping of PDP (used for
     69  *			0xf0000000	linear mapping of PTPs)
     70  * 960->1023	0xf0000000->		kernel address space (constant
     71  *			0xffc00000	across all pmap's/processes)
     72  * 1023		0xffc00000->		"alternate" recursive PDP mapping
     73  *			<end>		(for other pmaps)
     74  *
     75  *
     76  * note: a recursive PDP mapping provides a way to map all the PTEs for
     77  * a 4GB address space into a linear chunk of virtual memory.   in other
     78  * words, the PTE for page 0 is the first int mapped into the 4MB recursive
     79  * area.   the PTE for page 1 is the second int.    the very last int in the
     80  * 4MB range is the PTE that maps VA 0xffffe000 (the last page in a 4GB
     81  * address).
     82  *
     83  * all pmap's PD's must have the same values in slots 960->1023 so that
     84  * the kernel is always mapped in every process.   these values are loaded
     85  * into the PD at pmap creation time.
     86  *
     87  * at any one time only one pmap can be active on a processor.   this is
     88  * the pmap whose PDP is pointed to by processor register %cr3.   this pmap
     89  * will have all its PTEs mapped into memory at the recursive mapping
     90  * point (slot #959 as show above).   when the pmap code wants to find the
     91  * PTE for a virtual address, all it has to do is the following:
     92  *
     93  * address of PTE = (959 * 4MB) + (VA / NBPG) * sizeof(pt_entry_t)
     94  *                = 0xefc00000 + (VA /4096) * 4
     95  *
     96  * what happens if the pmap layer is asked to perform an operation
     97  * on a pmap that is not the one which is currently active?   in that
     98  * case we take the PA of the PDP of non-active pmap and put it in
     99  * slot 1023 of the active pmap.   this causes the non-active pmap's
    100  * PTEs to get mapped in the final 4MB of the 4GB address space
    101  * (e.g. starting at 0xffc00000).
    102  *
    103  * the following figure shows the effects of the recursive PDP mapping:
    104  *
    105  *   PDP (%cr3)
    106  *   +----+
    107  *   |   0| -> PTP#0 that maps VA 0x0 -> 0x400000
    108  *   |    |
    109  *   |    |
    110  *   | 959| -> points back to PDP (%cr3) mapping VA 0xefc00000 -> 0xf0000000
    111  *   | 960| -> first kernel PTP (maps 0xf0000000 -> 0xf0400000)
    112  *   |    |
    113  *   |1023| -> points to alternate pmap's PDP (maps 0xffc00000 -> end)
    114  *   +----+
    115  *
    116  * note that the PDE#959 VA (0xefc00000) is defined as "PTE_BASE"
    117  * note that the PDE#1023 VA (0xffc00000) is defined as "APTE_BASE"
    118  *
    119  * starting at VA 0xefc00000 the current active PDP (%cr3) acts as a
    120  * PTP:
    121  *
    122  * PTP#959 == PDP(%cr3) => maps VA 0xefc00000 -> 0xf0000000
    123  *   +----+
    124  *   |   0| -> maps the contents of PTP#0 at VA 0xefc00000->0xefc01000
    125  *   |    |
    126  *   |    |
    127  *   | 959| -> maps contents of PTP#959 (the PDP) at VA 0xeffbf000
    128  *   | 960| -> maps contents of first kernel PTP
    129  *   |    |
    130  *   |1023|
    131  *   +----+
    132  *
    133  * note that mapping of the PDP at PTP#959's VA (0xeffbf000) is
    134  * defined as "PDP_BASE".... within that mapping there are two
    135  * defines:
    136  *   "PDP_PDE" (0xeffbfefc) is the VA of the PDE in the PDP
    137  *      which points back to itself.
    138  *   "APDP_PDE" (0xeffbfffc) is the VA of the PDE in the PDP which
    139  *      establishes the recursive mapping of the alternate pmap.
    140  *      to set the alternate PDP, one just has to put the correct
    141  *	PA info in *APDP_PDE.
    142  *
    143  * note that in the APTE_BASE space, the APDP appears at VA
    144  * "APDP_BASE" (0xfffff000).
    145  */
    146 
    147 /*
    148  * the following defines identify the slots used as described above.
    149  */
    150 
    151 #define PDSLOT_PTE	((KERNBASE/NBPD)-1) /* 959: for recursive PDP map */
    152 #define PDSLOT_KERN	(KERNBASE/NBPD)	    /* 960: start of kernel space */
    153 #define PDSLOT_APTE	((unsigned)1023) /* 1023: alternative recursive slot */
    154 
    155 /*
    156  * the following defines give the virtual addresses of various MMU
    157  * data structures:
    158  * PTE_BASE and APTE_BASE: the base VA of the linear PTE mappings
    159  * PTD_BASE and APTD_BASE: the base VA of the recursive mapping of the PTD
    160  * PDP_PDE and APDP_PDE: the VA of the PDE that points back to the PDP/APDP
    161  */
    162 
    163 #define PTE_BASE	((pt_entry_t *)  (PDSLOT_PTE * NBPD) )
    164 #define APTE_BASE	((pt_entry_t *)  (PDSLOT_APTE * NBPD) )
    165 #define PDP_BASE ((pd_entry_t *)  (((char *)PTE_BASE)  + (PDSLOT_PTE * NBPG)) )
    166 #define APDP_BASE ((pd_entry_t *)  (((char *)APTE_BASE)  + (PDSLOT_APTE * NBPG)) )
    167 #define PDP_PDE		(PDP_BASE + PDSLOT_PTE)
    168 #define APDP_PDE	(PDP_BASE + PDSLOT_APTE)
    169 
    170 /*
    171  * XXXCDC: tmp xlate from old names:
    172  * PTDPTDI -> PDSLOT_PTE
    173  * KPTDI -> PDSLOT_KERN
    174  * APTDPTDI -> PDSLOT_APTE
    175  */
    176 
    177 /*
    178  * the follow define determines how many PTPs should be set up for the
    179  * kernel by locore.s at boot time.   this should be large enough to
    180  * get the VM system running.   once the VM system is running, the
    181  * pmap module can add more PTPs to the kernel area on demand.
    182  */
    183 
    184 #ifndef NKPTP
    185 #define NKPTP		4	/* 16MB to start */
    186 #endif
    187 #define NKPTP_MIN	4	/* smallest value we allow */
    188 #define NKPTP_MAX	(1024 - (KERNBASE/NBPD) - 1)
    189 				/* largest value (-1 for APTP space) */
    190 
    191 /*
    192  * various address macros
    193  *
    194  *  vtopte: return a pointer to the PTE mapping a VA
    195  *  kvtopte: same as above (takes a KVA, but doesn't matter with this pmap)
    196  *  ptetov: given a pointer to a PTE, return the VA that it maps
    197  *  vtophys: translate a VA to the PA mapped to it
    198  *
    199  * plus alternative versions of the above
    200  */
    201 
    202 #define vtopte(VA)	(PTE_BASE + i386_btop(VA))
    203 #define kvtopte(VA)	vtopte(VA)
    204 #define ptetov(PT)	(i386_ptob(PT - PTE_BASE))
    205 #define	vtophys(VA) ((*vtopte(VA) & PG_FRAME) | ((unsigned)(VA) & ~PG_FRAME))
    206 #define	avtopte(VA)	(APTE_BASE + i386_btop(VA))
    207 #define	ptetoav(PT)	(i386_ptob(PT - APTE_BASE))
    208 #define	avtophys(VA) ((*avtopte(VA) & PG_FRAME) | ((unsigned)(VA) & ~PG_FRAME))
    209 
    210 /*
    211  * pdei/ptei: generate index into PDP/PTP from a VA
    212  */
    213 #define	pdei(VA)	(((VA) & PD_MASK) >> PDSHIFT)
    214 #define	ptei(VA)	(((VA) & PT_MASK) >> PGSHIFT)
    215 
    216 /*
    217  * PTP macros:
    218  *   a PTP's index is the PD index of the PDE that points to it
    219  *   a PTP's offset is the byte-offset in the PTE space that this PTP is at
    220  *   a PTP's VA is the first VA mapped by that PTP
    221  *
    222  * note that NBPG == number of bytes in a PTP (4096 bytes == 1024 entries)
    223  *           NBPD == number of bytes a PTP can map (4MB)
    224  */
    225 
    226 #define ptp_i2o(I)	((I) * NBPG)	/* index => offset */
    227 #define ptp_o2i(O)	((O) / NBPG)	/* offset => index */
    228 #define ptp_i2v(I)	((I) * NBPD)	/* index => VA */
    229 #define ptp_v2i(V)	((V) / NBPD)	/* VA => index (same as pdei) */
    230 
    231 /*
    232  * PG_AVAIL usage: we make use of the ignored bits of the PTE
    233  */
    234 
    235 #define PG_W		PG_AVAIL1	/* "wired" mapping */
    236 #define PG_PVLIST	PG_AVAIL2	/* mapping has entry on pvlist */
    237 /* PG_AVAIL3 not used */
    238 
    239 #ifdef _KERNEL
    240 /*
    241  * pmap data structures: see pmap.c for details of locking.
    242  */
    243 
    244 struct pmap;
    245 typedef struct pmap *pmap_t;
    246 
    247 /*
    248  * we maintain a list of all non-kernel pmaps
    249  */
    250 
    251 LIST_HEAD(pmap_head, pmap); /* struct pmap_head: head of a pmap list */
    252 
    253 /*
    254  * the pmap structure
    255  *
    256  * note that the pm_obj contains the simple_lock, the reference count,
    257  * page list, and number of PTPs within the pmap.
    258  */
    259 
    260 struct pmap {
    261   struct uvm_object pm_obj;	/* object (lck by object lock) */
    262 #define	pm_lock	pm_obj.vmobjlock
    263   LIST_ENTRY(pmap) pm_list;	/* list (lck by pm_list lock) */
    264   pd_entry_t *pm_pdir;		/* VA of PD (lck by object lock) */
    265   u_int32_t pm_pdirpa;		/* PA of PD (read-only after create) */
    266   struct vm_page *pm_ptphint;	/* pointer to a random PTP in our pmap */
    267   struct pmap_statistics pm_stats;  /* pmap stats (lck by object lock) */
    268 
    269   int pm_flags;			/* see below */
    270 
    271   union descriptor *pm_ldt;	/* user-set LDT */
    272   int pm_ldt_len;		/* number of LDT entries */
    273   int pm_ldt_sel;		/* LDT selector */
    274 };
    275 
    276 /* pm_flags */
    277 #define	PMF_USER_LDT	0x01	/* pmap has user-set LDT */
    278 
    279 /*
    280  * for each managed physical page we maintain a list of <PMAP,VA>'s
    281  * which it is mapped at.   the list is headed by a pv_head structure.
    282  * there is one pv_head per managed phys page (allocated at boot time).
    283  * the pv_head structure points to a list of pv_entry structures (each
    284  * describes one mapping).
    285  */
    286 
    287 struct pv_entry;
    288 
    289 struct pv_head {
    290   simple_lock_data_t pvh_lock;	/* locks every pv on this list */
    291   struct pv_entry *pvh_list;	/* head of list (locked by pvh_lock) */
    292 };
    293 
    294 struct pv_entry {		/* all fields locked by their pvh_lock */
    295   struct pv_entry *pv_next;	/* next entry */
    296   struct pmap *pv_pmap;		/* the pmap */
    297   vaddr_t pv_va;		/* the virtual address */
    298   struct vm_page *pv_ptp;	/* the vm_page of the PTP */
    299 };
    300 
    301 /*
    302  * pv_entrys are dynamically allocated in chunks from a single page.
    303  * we keep track of how many pv_entrys are in use for each page and
    304  * we can free pv_entry pages if needed.   there is one lock for the
    305  * entire allocation system.
    306  */
    307 
    308 struct pv_page_info {
    309   TAILQ_ENTRY(pv_page) pvpi_list;
    310   struct pv_entry *pvpi_pvfree;
    311   int pvpi_nfree;
    312 };
    313 
    314 /*
    315  * number of pv_entry's in a pv_page
    316  * (note: won't work on systems where NPBG isn't a constant)
    317  */
    318 
    319 #define PVE_PER_PVPAGE ( (NBPG - sizeof(struct pv_page_info)) / \
    320 				sizeof(struct pv_entry) )
    321 
    322 /*
    323  * a pv_page: where pv_entrys are allocated from
    324  */
    325 
    326 struct pv_page {
    327   struct pv_page_info pvinfo;
    328   struct pv_entry pvents[PVE_PER_PVPAGE];
    329 };
    330 
    331 /*
    332  * pmap_remove_record: a record of VAs that have been unmapped, used to
    333  * flush TLB.   if we have more than PMAP_RR_MAX then we stop recording.
    334  */
    335 
    336 #define PMAP_RR_MAX	16	/* max of 16 pages (64K) */
    337 
    338 struct pmap_remove_record {
    339   int prr_npages;
    340   vaddr_t prr_vas[PMAP_RR_MAX];
    341 };
    342 
    343 /*
    344  * pmap_transfer_location: used to pass the current location in the
    345  * pmap between pmap_transfer and pmap_transfer_ptes [e.g. during
    346  * a pmap_copy].
    347  */
    348 
    349 struct pmap_transfer_location {
    350   vaddr_t addr;			/* the address (page-aligned) */
    351   pt_entry_t *pte;		/* the PTE that maps address */
    352   struct vm_page *ptp;		/* the PTP that the PTE lives in */
    353 };
    354 
    355 /*
    356  * global kernel variables
    357  */
    358 
    359 /* PTDpaddr: is the physical address of the kernel's PDP */
    360 extern u_long PTDpaddr;
    361 
    362 extern struct pmap kernel_pmap_store;	/* kernel pmap */
    363 extern int nkpde;			/* current # of PDEs for kernel */
    364 extern int pmap_pg_g;			/* do we support PG_G? */
    365 
    366 /*
    367  * macros
    368  */
    369 
    370 #define	pmap_kernel()			(&kernel_pmap_store)
    371 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
    372 #define	pmap_update()			tlbflush()
    373 
    374 #define pmap_clear_modify(pg)		pmap_change_attrs(pg, 0, PG_M)
    375 #define pmap_clear_reference(pg)	pmap_change_attrs(pg, 0, PG_U)
    376 #define pmap_copy(DP,SP,D,L,S)		pmap_transfer(DP,SP,D,L,S, FALSE)
    377 #define pmap_is_modified(pg)		pmap_test_attrs(pg, PG_M)
    378 #define pmap_is_referenced(pg)		pmap_test_attrs(pg, PG_U)
    379 #define pmap_move(DP,SP,D,L,S)		pmap_transfer(DP,SP,D,L,S, TRUE)
    380 #define pmap_phys_address(ppn)		i386_ptob(ppn)
    381 #define pmap_valid_entry(E) 		((E) & PG_V) /* is PDE or PTE valid? */
    382 
    383 
    384 /*
    385  * prototypes
    386  */
    387 
    388 void		pmap_activate __P((struct proc *));
    389 void		pmap_bootstrap __P((vaddr_t));
    390 boolean_t	pmap_change_attrs __P((struct vm_page *, int, int));
    391 void		pmap_deactivate __P((struct proc *));
    392 static void	pmap_kenter_pa __P((vaddr_t, paddr_t, vm_prot_t));
    393 static void	pmap_page_protect __P((struct vm_page *, vm_prot_t));
    394 void		pmap_page_remove  __P((struct vm_page *));
    395 static void	pmap_protect __P((struct pmap *, vaddr_t,
    396 				vaddr_t, vm_prot_t));
    397 void		pmap_remove __P((struct pmap *, vaddr_t, vaddr_t));
    398 boolean_t	pmap_test_attrs __P((struct vm_page *, int));
    399 void		pmap_transfer __P((struct pmap *, struct pmap *, vaddr_t,
    400 				   vsize_t, vaddr_t, boolean_t));
    401 static void	pmap_update_pg __P((vaddr_t));
    402 static void	pmap_update_2pg __P((vaddr_t,vaddr_t));
    403 void		pmap_write_protect __P((struct pmap *, vaddr_t,
    404 				vaddr_t, vm_prot_t));
    405 
    406 vaddr_t reserve_dumppages __P((vaddr_t)); /* XXX: not a pmap fn */
    407 
    408 #define PMAP_GROWKERNEL		/* turn on pmap_growkernel interface */
    409 
    410 /*
    411  * inline functions
    412  */
    413 
    414 /*
    415  * pmap_update_pg: flush one page from the TLB (or flush the whole thing
    416  *	if hardware doesn't support one-page flushing)
    417  */
    418 
    419 __inline static void pmap_update_pg(va)
    420 
    421 vaddr_t va;
    422 
    423 {
    424 #if defined(I386_CPU)
    425   if (cpu_class == CPUCLASS_386)
    426     pmap_update();
    427   else
    428 #endif
    429     invlpg((u_int) va);
    430 }
    431 
    432 /*
    433  * pmap_update_2pg: flush two pages from the TLB
    434  */
    435 
    436 __inline static void pmap_update_2pg(va, vb)
    437 
    438 vaddr_t va, vb;
    439 
    440 {
    441 #if defined(I386_CPU)
    442   if (cpu_class == CPUCLASS_386)
    443     pmap_update();
    444   else
    445 #endif
    446     {
    447       invlpg((u_int) va);
    448       invlpg((u_int) vb);
    449     }
    450 }
    451 
    452 /*
    453  * pmap_page_protect: change the protection of all recorded mappings
    454  *	of a managed page
    455  *
    456  * => this function is a frontend for pmap_page_remove/pmap_change_attrs
    457  * => we only have to worry about making the page more protected.
    458  *	unprotecting a page is done on-demand at fault time.
    459  */
    460 
    461 __inline static void pmap_page_protect(pg, prot)
    462 
    463 struct vm_page *pg;
    464 vm_prot_t prot;
    465 
    466 {
    467   if ((prot & VM_PROT_WRITE) == 0) {
    468     if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
    469       (void) pmap_change_attrs(pg, PG_RO, PG_RW);
    470     } else {
    471       pmap_page_remove(pg);
    472     }
    473   }
    474 }
    475 
    476 /*
    477  * pmap_protect: change the protection of pages in a pmap
    478  *
    479  * => this function is a frontend for pmap_remove/pmap_write_protect
    480  * => we only have to worry about making the page more protected.
    481  *	unprotecting a page is done on-demand at fault time.
    482  */
    483 
    484 __inline static void pmap_protect(pmap, sva, eva, prot)
    485 
    486 struct pmap *pmap;
    487 vaddr_t sva, eva;
    488 vm_prot_t prot;
    489 
    490 {
    491   if ((prot & VM_PROT_WRITE) == 0) {
    492     if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
    493       pmap_write_protect(pmap, sva, eva, prot);
    494     } else {
    495       pmap_remove(pmap, sva, eva);
    496     }
    497   }
    498 }
    499 
    500 /*
    501  * pmap_kenter_pa: enter a kernel mapping without R/M (pv_entry) tracking
    502  *
    503  * => no need to lock anything, assume va is already allocated
    504  * => should be faster than normal pmap enter function
    505  */
    506 
    507 __inline static void pmap_kenter_pa(va, pa, prot)
    508 
    509 vaddr_t va;
    510 paddr_t pa;
    511 vm_prot_t prot;
    512 
    513 {
    514   struct pmap *pm = pmap_kernel();
    515   pt_entry_t *pte, opte;
    516   int s;
    517 
    518   s = splimp();
    519   simple_lock(&pm->pm_obj.vmobjlock);
    520   pm->pm_stats.resident_count++;
    521   pm->pm_stats.wired_count++;
    522   simple_unlock(&pm->pm_obj.vmobjlock);
    523   splx(s);
    524 
    525   pte = vtopte(va);
    526   opte = *pte;
    527   *pte = pa | ((prot & VM_PROT_WRITE)? PG_RW : PG_RO) |
    528 	      PG_V | pmap_pg_g;         /* zap! */
    529   if (pmap_valid_entry(opte))
    530     pmap_update_pg(va);
    531 }
    532 
    533 vaddr_t	pmap_map __P((vaddr_t, paddr_t, paddr_t, int));
    534 
    535 #if defined(USER_LDT)
    536 void	pmap_ldt_cleanup __P((struct proc *));
    537 #define	PMAP_FORK
    538 #endif /* USER_LDT */
    539 
    540 #endif /* _KERNEL */
    541 #endif	/* _I386_PMAP_H_ */
    542