Home | History | Annotate | Line # | Download | only in include
pmap.h revision 1.82.12.3
      1 /*	$NetBSD: pmap.h,v 1.82.12.3 2007/09/03 14:26:48 yamt 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_OPT)
     43 #include "opt_user_ldt.h"
     44 #include "opt_largepages.h"
     45 #endif
     46 
     47 #include <machine/cpufunc.h>
     48 #include <machine/pte.h>
     49 #include <machine/segments.h>
     50 #include <machine/atomic.h>
     51 
     52 #include <uvm/uvm_object.h>
     53 
     54 /*
     55  * see pte.h for a description of i386 MMU terminology and hardware
     56  * interface.
     57  *
     58  * a pmap describes a processes' 4GB virtual address space.  this
     59  * virtual address space can be broken up into 1024 4MB regions which
     60  * are described by PDEs in the PDP.  the PDEs are defined as follows:
     61  *
     62  * (ranges are inclusive -> exclusive, just like vm_map_entry start/end)
     63  * (the following assumes that KERNBASE is 0xc0000000)
     64  *
     65  * PDE#s	VA range		usage
     66  * 0->766	0x0 -> 0xbfc00000	user address space
     67  * 767		0xbfc00000->		recursive mapping of PDP (used for
     68  *			0xc0000000	linear mapping of PTPs)
     69  * 768->1023	0xc0000000->		kernel address space (constant
     70  *			0xffc00000	across all pmap's/processes)
     71  * 1023		0xffc00000->		"alternate" recursive PDP mapping
     72  *			<end>		(for other pmaps)
     73  *
     74  *
     75  * note: a recursive PDP mapping provides a way to map all the PTEs for
     76  * a 4GB address space into a linear chunk of virtual memory.  in other
     77  * words, the PTE for page 0 is the first int mapped into the 4MB recursive
     78  * area.  the PTE for page 1 is the second int.  the very last int in the
     79  * 4MB range is the PTE that maps VA 0xfffff000 (the last page in a 4GB
     80  * address).
     81  *
     82  * all pmap's PD's must have the same values in slots 768->1023 so that
     83  * the kernel is always mapped in every process.  these values are loaded
     84  * into the PD at pmap creation time.
     85  *
     86  * at any one time only one pmap can be active on a processor.  this is
     87  * the pmap whose PDP is pointed to by processor register %cr3.  this pmap
     88  * will have all its PTEs mapped into memory at the recursive mapping
     89  * point (slot #767 as show above).  when the pmap code wants to find the
     90  * PTE for a virtual address, all it has to do is the following:
     91  *
     92  * address of PTE = (767 * 4MB) + (VA / PAGE_SIZE) * sizeof(pt_entry_t)
     93  *                = 0xbfc00000 + (VA / 4096) * 4
     94  *
     95  * what happens if the pmap layer is asked to perform an operation
     96  * on a pmap that is not the one which is currently active?  in that
     97  * case we take the PA of the PDP of non-active pmap and put it in
     98  * slot 1023 of the active pmap.  this causes the non-active pmap's
     99  * PTEs to get mapped in the final 4MB of the 4GB address space
    100  * (e.g. starting at 0xffc00000).
    101  *
    102  * the following figure shows the effects of the recursive PDP mapping:
    103  *
    104  *   PDP (%cr3)
    105  *   +----+
    106  *   |   0| -> PTP#0 that maps VA 0x0 -> 0x400000
    107  *   |    |
    108  *   |    |
    109  *   | 767| -> points back to PDP (%cr3) mapping VA 0xbfc00000 -> 0xc0000000
    110  *   | 768| -> first kernel PTP (maps 0xc0000000 -> 0xc0400000)
    111  *   |    |
    112  *   |1023| -> points to alternate pmap's PDP (maps 0xffc00000 -> end)
    113  *   +----+
    114  *
    115  * note that the PDE#767 VA (0xbfc00000) is defined as "PTE_BASE"
    116  * note that the PDE#1023 VA (0xffc00000) is defined as "APTE_BASE"
    117  *
    118  * starting at VA 0xbfc00000 the current active PDP (%cr3) acts as a
    119  * PTP:
    120  *
    121  * PTP#767 == PDP(%cr3) => maps VA 0xbfc00000 -> 0xc0000000
    122  *   +----+
    123  *   |   0| -> maps the contents of PTP#0 at VA 0xbfc00000->0xbfc01000
    124  *   |    |
    125  *   |    |
    126  *   | 767| -> maps contents of PTP#767 (the PDP) at VA 0xbfeff000
    127  *   | 768| -> maps contents of first kernel PTP
    128  *   |    |
    129  *   |1023|
    130  *   +----+
    131  *
    132  * note that mapping of the PDP at PTP#767's VA (0xbfeff000) is
    133  * defined as "PDP_BASE".... within that mapping there are two
    134  * defines:
    135  *   "PDP_PDE" (0xbfeffbfc) is the VA of the PDE in the PDP
    136  *      which points back to itself.
    137  *   "APDP_PDE" (0xbfeffffc) is the VA of the PDE in the PDP which
    138  *      establishes the recursive mapping of the alternate pmap.
    139  *      to set the alternate PDP, one just has to put the correct
    140  *	PA info in *APDP_PDE.
    141  *
    142  * note that in the APTE_BASE space, the APDP appears at VA
    143  * "APDP_BASE" (0xfffff000).
    144  */
    145 /* XXX MP should we allocate one APDP_PDE per processor?? */
    146 
    147 /*
    148  * the following defines identify the slots used as described above.
    149  */
    150 
    151 #define PDSLOT_PTE	((KERNBASE/NBPD)-1) /* 767: for recursive PDP map */
    152 #define PDSLOT_KERN	(KERNBASE/NBPD)	    /* 768: 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  * PDP_BASE and APDP_BASE: the base VA of the recursive mapping of the PDP
    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 * PAGE_SIZE)))
    166 #define APDP_BASE ((pd_entry_t *)(((char *)APTE_BASE) + (PDSLOT_APTE * PAGE_SIZE)))
    167 #define PDP_PDE		(PDP_BASE + PDSLOT_PTE)
    168 #define APDP_PDE	(PDP_BASE + PDSLOT_APTE)
    169 
    170 /*
    171  * the follow define determines how many PTPs should be set up for the
    172  * kernel by locore.S at boot time.  this should be large enough to
    173  * get the VM system running.  once the VM system is running, the
    174  * pmap module can add more PTPs to the kernel area on demand.
    175  */
    176 
    177 #ifndef NKPTP
    178 #define NKPTP		0	/* 16MB to start */
    179 #endif
    180 #define NKPTP_MIN	2	/* smallest value we allow */
    181 #define NKPTP_MAX	(1024 - (KERNBASE/NBPD) - 1)
    182 				/* largest value (-1 for APTP space) */
    183 
    184 /*
    185  * pdei/ptei: generate index into PDP/PTP from a VA
    186  */
    187 #define	pdei(VA)	(((VA) & PD_MASK) >> PDSHIFT)
    188 #define	ptei(VA)	(((VA) & PT_MASK) >> PGSHIFT)
    189 
    190 /*
    191  * PTP macros:
    192  *   a PTP's index is the PD index of the PDE that points to it
    193  *   a PTP's offset is the byte-offset in the PTE space that this PTP is at
    194  *   a PTP's VA is the first VA mapped by that PTP
    195  *
    196  * note that PAGE_SIZE == number of bytes in a PTP (4096 bytes == 1024 entries)
    197  *           NBPD == number of bytes a PTP can map (4MB)
    198  */
    199 
    200 #define ptp_i2o(I)	((I) * PAGE_SIZE)	/* index => offset */
    201 #define ptp_o2i(O)	((O) / PAGE_SIZE)	/* offset => index */
    202 #define ptp_i2v(I)	((I) * NBPD)	/* index => VA */
    203 #define ptp_v2i(V)	((V) / NBPD)	/* VA => index (same as pdei) */
    204 
    205 /*
    206  * PG_AVAIL usage: we make use of the ignored bits of the PTE
    207  */
    208 
    209 #define PG_W		PG_AVAIL1	/* "wired" mapping */
    210 #define PG_PVLIST	PG_AVAIL2	/* mapping has entry on pvlist */
    211 #define PG_X		PG_AVAIL3	/* executable mapping */
    212 
    213 /*
    214  * Number of PTE's per cache line.  4 byte pte, 32-byte cache line
    215  * Used to avoid false sharing of cache lines.
    216  */
    217 #define NPTECL			8
    218 
    219 #ifdef _KERNEL
    220 /*
    221  * pmap data structures: see pmap.c for details of locking.
    222  */
    223 
    224 struct pmap;
    225 typedef struct pmap *pmap_t;
    226 
    227 /*
    228  * we maintain a list of all non-kernel pmaps
    229  */
    230 
    231 LIST_HEAD(pmap_head, pmap); /* struct pmap_head: head of a pmap list */
    232 
    233 /*
    234  * the pmap structure
    235  *
    236  * note that the pm_obj contains the simple_lock, the reference count,
    237  * page list, and number of PTPs within the pmap.
    238  *
    239  * XXX If we ever support processor numbers higher than 31, we'll have
    240  * XXX to rethink the CPU mask.
    241  */
    242 
    243 struct pmap {
    244 	struct uvm_object pm_obj;	/* object (lck by object lock) */
    245 #define	pm_lock	pm_obj.vmobjlock
    246 	LIST_ENTRY(pmap) pm_list;	/* list (lck by pm_list lock) */
    247 	pd_entry_t *pm_pdir;		/* VA of PD (lck by object lock) */
    248 	uint32_t pm_pdirpa;		/* PA of PD (read-only after create) */
    249 	struct vm_page *pm_ptphint;	/* pointer to a PTP in our pmap */
    250 	struct pmap_statistics pm_stats;  /* pmap stats (lck by object lock) */
    251 
    252 	vaddr_t pm_hiexec;		/* highest executable mapping */
    253 	int pm_flags;			/* see below */
    254 
    255 	union descriptor *pm_ldt;	/* user-set LDT */
    256 	int pm_ldt_len;			/* number of LDT entries */
    257 	int pm_ldt_sel;			/* LDT selector */
    258 	uint32_t pm_cpus;		/* mask of CPUs using pmap */
    259 	uint32_t pm_kernel_cpus;	/* mask of CPUs using kernel part
    260 					 of pmap */
    261 };
    262 
    263 /* pm_flags */
    264 #define	PMF_USER_LDT	0x01	/* pmap has user-set LDT */
    265 
    266 /*
    267  * for each managed physical page we maintain a list of <PMAP,VA>'s
    268  * which it is mapped at.  the list is headed by a pv_head structure.
    269  * there is one pv_head per managed phys page (allocated at boot time).
    270  * the pv_head structure points to a list of pv_entry structures (each
    271  * describes one mapping).
    272  */
    273 
    274 struct pv_entry {			/* locked by its list's pvh_lock */
    275 	SPLAY_ENTRY(pv_entry) pv_node;	/* splay-tree node */
    276 	struct pmap *pv_pmap;		/* the pmap */
    277 	vaddr_t pv_va;			/* the virtual address */
    278 	struct vm_page *pv_ptp;		/* the vm_page of the PTP */
    279 	struct pmap_cpu *pv_alloc_cpu;	/* CPU allocated from */
    280 };
    281 
    282 /*
    283  * pv_entrys are dynamically allocated in chunks from a single page.
    284  * we keep track of how many pv_entrys are in use for each page and
    285  * we can free pv_entry pages if needed.  there is one lock for the
    286  * entire allocation system.
    287  */
    288 
    289 struct pv_page_info {
    290 	TAILQ_ENTRY(pv_page) pvpi_list;
    291 	struct pv_entry *pvpi_pvfree;
    292 	int pvpi_nfree;
    293 };
    294 
    295 /*
    296  * number of pv_entry's in a pv_page
    297  * (note: won't work on systems where NPBG isn't a constant)
    298  */
    299 
    300 #define PVE_PER_PVPAGE ((PAGE_SIZE - sizeof(struct pv_page_info)) / \
    301 			sizeof(struct pv_entry))
    302 
    303 /*
    304  * a pv_page: where pv_entrys are allocated from
    305  */
    306 
    307 struct pv_page {
    308 	struct pv_page_info pvinfo;
    309 	struct pv_entry pvents[PVE_PER_PVPAGE];
    310 };
    311 
    312 /*
    313  * global kernel variables
    314  */
    315 
    316 /* PDPpaddr: is the physical address of the kernel's PDP */
    317 extern u_long PDPpaddr;
    318 
    319 extern struct pmap kernel_pmap_store;	/* kernel pmap */
    320 extern int nkpde;			/* current # of PDEs for kernel */
    321 extern int pmap_pg_g;			/* do we support PG_G? */
    322 
    323 /*
    324  * macros
    325  */
    326 
    327 #define	pmap_kernel()			(&kernel_pmap_store)
    328 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
    329 #define	pmap_wired_count(pmap)		((pmap)->pm_stats.wired_count)
    330 
    331 #define pmap_clear_modify(pg)		pmap_clear_attrs(pg, PG_M)
    332 #define pmap_clear_reference(pg)	pmap_clear_attrs(pg, PG_U)
    333 #define pmap_copy(DP,SP,D,L,S)
    334 #define pmap_is_modified(pg)		pmap_test_attrs(pg, PG_M)
    335 #define pmap_is_referenced(pg)		pmap_test_attrs(pg, PG_U)
    336 #define pmap_move(DP,SP,D,L,S)
    337 #define pmap_phys_address(ppn)		x86_ptob(ppn)
    338 #define pmap_valid_entry(E) 		((E) & PG_V) /* is PDE or PTE valid? */
    339 
    340 
    341 /*
    342  * prototypes
    343  */
    344 
    345 void		pmap_activate(struct lwp *);
    346 void		pmap_bootstrap(vaddr_t);
    347 bool		pmap_clear_attrs(struct vm_page *, int);
    348 void		pmap_deactivate(struct lwp *);
    349 void		pmap_deactivate2(struct lwp *);
    350 void		pmap_page_remove (struct vm_page *);
    351 void		pmap_remove(struct pmap *, vaddr_t, vaddr_t);
    352 bool		pmap_test_attrs(struct vm_page *, int);
    353 void		pmap_write_protect(struct pmap *, vaddr_t, vaddr_t, vm_prot_t);
    354 int		pmap_exec_fixup(struct vm_map *, struct trapframe *,
    355 		    struct pcb *);
    356 void		pmap_load(void);
    357 
    358 vaddr_t reserve_dumppages(vaddr_t); /* XXX: not a pmap fn */
    359 
    360 void	pmap_tlb_shootdown(pmap_t, vaddr_t, vaddr_t, pt_entry_t);
    361 void	pmap_tlb_shootwait(void);
    362 
    363 #define PMAP_GROWKERNEL		/* turn on pmap_growkernel interface */
    364 
    365 /*
    366  * Do idle page zero'ing uncached to avoid polluting the cache.
    367  */
    368 bool	pmap_pageidlezero(paddr_t);
    369 #define	PMAP_PAGEIDLEZERO(pa)	pmap_pageidlezero((pa))
    370 
    371 /*
    372  * inline functions
    373  */
    374 
    375 /*ARGSUSED*/
    376 static __inline void
    377 pmap_remove_all(struct pmap *pmap)
    378 {
    379 	/* Nothing. */
    380 }
    381 
    382 /*
    383  * pmap_update_pg: flush one page from the TLB (or flush the whole thing
    384  *	if hardware doesn't support one-page flushing)
    385  */
    386 
    387 __inline static void __attribute__((__unused__))
    388 pmap_update_pg(vaddr_t va)
    389 {
    390 #if defined(I386_CPU)
    391 	if (cpu_class == CPUCLASS_386)
    392 		tlbflush();
    393 	else
    394 #endif
    395 		invlpg((u_int) va);
    396 }
    397 
    398 /*
    399  * pmap_update_2pg: flush two pages from the TLB
    400  */
    401 
    402 __inline static void __attribute__((__unused__))
    403 pmap_update_2pg(vaddr_t va, vaddr_t vb)
    404 {
    405 #if defined(I386_CPU)
    406 	if (cpu_class == CPUCLASS_386)
    407 		tlbflush();
    408 	else
    409 #endif
    410 	{
    411 		invlpg((u_int) va);
    412 		invlpg((u_int) vb);
    413 	}
    414 }
    415 
    416 /*
    417  * pmap_page_protect: change the protection of all recorded mappings
    418  *	of a managed page
    419  *
    420  * => this function is a frontend for pmap_page_remove/pmap_clear_attrs
    421  * => we only have to worry about making the page more protected.
    422  *	unprotecting a page is done on-demand at fault time.
    423  */
    424 
    425 __inline static void __attribute__((__unused__))
    426 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
    427 {
    428 	if ((prot & VM_PROT_WRITE) == 0) {
    429 		if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
    430 			(void) pmap_clear_attrs(pg, PG_RW);
    431 		} else {
    432 			pmap_page_remove(pg);
    433 		}
    434 	}
    435 }
    436 
    437 /*
    438  * pmap_protect: change the protection of pages in a pmap
    439  *
    440  * => this function is a frontend for pmap_remove/pmap_write_protect
    441  * => we only have to worry about making the page more protected.
    442  *	unprotecting a page is done on-demand at fault time.
    443  */
    444 
    445 __inline static void __attribute__((__unused__))
    446 pmap_protect(struct pmap *pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
    447 {
    448 	if ((prot & VM_PROT_WRITE) == 0) {
    449 		if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) {
    450 			pmap_write_protect(pmap, sva, eva, prot);
    451 		} else {
    452 			pmap_remove(pmap, sva, eva);
    453 		}
    454 	}
    455 }
    456 
    457 /*
    458  * various address inlines
    459  *
    460  *  vtopte: return a pointer to the PTE mapping a VA, works only for
    461  *  user and PT addresses
    462  *
    463  *  kvtopte: return a pointer to the PTE mapping a kernel VA
    464  */
    465 
    466 #include <lib/libkern/libkern.h>
    467 
    468 static __inline pt_entry_t * __attribute__((__unused__))
    469 vtopte(vaddr_t va)
    470 {
    471 
    472 	KASSERT(va < (PDSLOT_KERN << PDSHIFT));
    473 
    474 	return (PTE_BASE + x86_btop(va));
    475 }
    476 
    477 static __inline pt_entry_t * __attribute__((__unused__))
    478 kvtopte(vaddr_t va)
    479 {
    480 
    481 	KASSERT(va >= (PDSLOT_KERN << PDSHIFT));
    482 
    483 #ifdef LARGEPAGES
    484 	{
    485 		pd_entry_t *pde;
    486 
    487 		pde = PDP_BASE + pdei(va);
    488 		if (*pde & PG_PS)
    489 			return ((pt_entry_t *)pde);
    490 	}
    491 #endif
    492 
    493 	return (PTE_BASE + x86_btop(va));
    494 }
    495 
    496 #define pmap_pte_set(p, n)		x86_atomic_testset_ul(p, n)
    497 #define pmap_pte_setbits(p, b)		x86_atomic_setbits_l(p, b)
    498 #define pmap_pte_clearbits(p, b)	x86_atomic_clearbits_l(p, b)
    499 #define pmap_cpu_has_pg_n()		(cpu_class != CPUCLASS_386)
    500 #define pmap_cpu_has_invlpg()		(cpu_class != CPUCLASS_386)
    501 
    502 paddr_t vtophys(vaddr_t);
    503 vaddr_t	pmap_map(vaddr_t, paddr_t, paddr_t, vm_prot_t);
    504 void	pmap_ldt_cleanup(struct lwp *);
    505 void	pmap_cpu_init_early(struct cpu_info *);
    506 void	pmap_cpu_init_late(struct cpu_info *);
    507 void	sse2_zero_page(void *);
    508 void	sse2_copy_page(void *, void *);
    509 
    510 /*
    511  * Hooks for the pool allocator.
    512  */
    513 #define	POOL_VTOPHYS(va)	vtophys((vaddr_t) (va))
    514 
    515 /*
    516  * TLB shootdown mailbox.
    517  */
    518 
    519 struct pmap_mbox {
    520 	volatile void		*mb_pointer;
    521 	volatile uintptr_t	mb_addr1;
    522 	volatile uintptr_t	mb_addr2;
    523 	volatile uintptr_t	mb_head;
    524 	volatile uintptr_t	mb_tail;
    525 	volatile uintptr_t	mb_global;
    526 };
    527 
    528 #endif /* _KERNEL */
    529 #endif	/* _I386_PMAP_H_ */
    530