Home | History | Annotate | Line # | Download | only in include
pmap.h revision 1.39
      1 /*	$NetBSD: pmap.h,v 1.39 1999/05/12 19:28:30 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1991 Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department and William Jolitz of UUNET Technologies Inc.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by the University of
     58  *	California, Berkeley and its contributors.
     59  * 4. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  *	@(#)pmap.h	7.4 (Berkeley) 5/12/91
     76  */
     77 
     78 /*
     79  * Derived from hp300 version by Mike Hibler, this version by William
     80  * Jolitz uses a recursive map [a pde points to the page directory] to
     81  * map the page tables using the pagetables themselves. This is done to
     82  * reduce the impact on kernel virtual memory for lots of sparse address
     83  * space, and to reduce the cost of memory to each process.
     84  *
     85  * from hp300:	@(#)pmap.h	7.2 (Berkeley) 12/16/90
     86  */
     87 
     88 #if defined(_KERNEL) && !defined(_LKM)
     89 #include "opt_pmap_new.h"
     90 #include "opt_user_ldt.h"
     91 #endif
     92 
     93 #ifdef PMAP_NEW			/* redirect */
     94 #include <machine/pmap.new.h>	/* defines _I386_PMAP_H_ */
     95 #endif
     96 
     97 #ifndef	_I386_PMAP_H_
     98 #define	_I386_PMAP_H_
     99 
    100 #include <machine/cpufunc.h>
    101 #include <machine/pte.h>
    102 #include <machine/segments.h>
    103 
    104 /*
    105  * 386 page table entry and page table directory
    106  * W.Jolitz, 8/89
    107  */
    108 
    109 /*
    110  * PG_AVAIL usage ...
    111  */
    112 
    113 #define PG_W         PG_AVAIL1       /* "wired" mapping */
    114 
    115 /*
    116  * One page directory, shared between
    117  * kernel and user modes.
    118  */
    119 #define	PTDPTDI		0x3bf		/* ptd entry that points to ptd! */
    120 #define	KPTDI		0x3c0		/* start of kernel virtual pde's */
    121 #define	NKPDE_BASE	4		/* min. # of kernel PDEs */
    122 #define	NKPDE_MAX	63		/* max. # of kernel PDEs */
    123 #define	NKPDE_SCALE	1		/* # of kernel PDEs to add per meg. */
    124 #define	APTDPTDI	0x3ff		/* start of alternate page directory */
    125 
    126 #define UPT_MIN_ADDRESS	(PTDPTDI<<PDSHIFT)
    127 #define UPT_MAX_ADDRESS	(UPT_MIN_ADDRESS + (PTDPTDI<<PGSHIFT))
    128 
    129 /*
    130  * Address of current and alternate address space page table maps
    131  * and directories.
    132  */
    133 #ifdef _KERNEL
    134 extern pt_entry_t	PTmap[], APTmap[], Upte;
    135 extern pd_entry_t	PTD[], APTD[], PTDpde, APTDpde, Upde;
    136 extern pt_entry_t	*Sysmap;
    137 
    138 extern u_long	PTDpaddr;	/* physical address of kernel PTD */
    139 
    140 void pmap_bootstrap __P((vaddr_t start));
    141 boolean_t pmap_testbit __P((paddr_t, int));
    142 void pmap_changebit __P((paddr_t, int, int));
    143 #endif
    144 
    145 /*
    146  * virtual address to page table entry and
    147  * to physical address. Likewise for alternate address space.
    148  * Note: these work recursively, thus vtopte of a pte will give
    149  * the corresponding pde that in turn maps it.
    150  */
    151 #define	vtopte(va)	(PTmap + i386_btop(va))
    152 #define	kvtopte(va)	vtopte(va)
    153 #define	ptetov(pt)	(i386_ptob(pt - PTmap))
    154 #define	vtophys(va) \
    155 	((*vtopte(va) & PG_FRAME) | ((unsigned)(va) & ~PG_FRAME))
    156 
    157 #define	avtopte(va)	(APTmap + i386_btop(va))
    158 #define	ptetoav(pt)	(i386_ptob(pt - APTmap))
    159 #define	avtophys(va) \
    160 	((*avtopte(va) & PG_FRAME) | ((unsigned)(va) & ~PG_FRAME))
    161 
    162 /*
    163  * macros to generate page directory/table indicies
    164  */
    165 #define	pdei(va)	(((va) & PD_MASK) >> PDSHIFT)
    166 #define	ptei(va)	(((va) & PT_MASK) >> PGSHIFT)
    167 
    168 /*
    169  * Pmap stuff
    170  */
    171 typedef struct pmap {
    172 	pd_entry_t		*pm_pdir;	/* KVA of page directory */
    173 	short			pm_dref;	/* page directory ref count */
    174 	short			pm_count;	/* pmap reference count */
    175 	simple_lock_data_t	pm_lock;	/* lock on pmap */
    176 	struct pmap_statistics	pm_stats;	/* pmap statistics */
    177 	long			pm_ptpages;	/* more stats: PT pages */
    178 
    179 	int			pm_flags;	/* see below */
    180 
    181 	union descriptor	*pm_ldt;	/* user-set LDT entries */
    182 	int			pm_ldt_len;	/* number of LDT entries */
    183 	int			pm_ldt_sel;	/* LDT selector */
    184 } *pmap_t;
    185 
    186 /* pm_flags */
    187 #define	PMF_USER_LDT	0x01	/* pmap has user-set LDT */
    188 
    189 /*
    190  * For each vm_page_t, there is a list of all currently valid virtual
    191  * mappings of that page.  An entry is a pv_entry, the list is pv_table.
    192  */
    193 struct pv_entry {
    194 	struct pv_entry	*pv_next;	/* next pv_entry */
    195 	pmap_t		pv_pmap;	/* pmap where mapping lies */
    196 	vaddr_t		pv_va;		/* virtual address for mapping */
    197 };
    198 
    199 struct pv_page;
    200 
    201 struct pv_page_info {
    202 	TAILQ_ENTRY(pv_page) pgi_list;
    203 	struct pv_entry *pgi_freelist;
    204 	int pgi_nfree;
    205 };
    206 
    207 /*
    208  * This is basically:
    209  * ((NBPG - sizeof(struct pv_page_info)) / sizeof(struct pv_entry))
    210  */
    211 #define	NPVPPG	340
    212 
    213 struct pv_page {
    214 	struct pv_page_info pvp_pgi;
    215 	struct pv_entry pvp_pv[NPVPPG];
    216 };
    217 
    218 #ifdef	_KERNEL
    219 extern int		nkpde;		/* number of kernel page dir. ents */
    220 extern struct pmap	kernel_pmap_store;
    221 
    222 #define	pmap_kernel()			(&kernel_pmap_store)
    223 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
    224 #define	pmap_update()			tlbflush()
    225 
    226 vaddr_t reserve_dumppages __P((vaddr_t));
    227 
    228 static __inline void
    229 pmap_clear_modify(paddr_t pa)
    230 {
    231 	pmap_changebit(pa, 0, ~PG_M);
    232 }
    233 
    234 static __inline void
    235 pmap_clear_reference(paddr_t pa)
    236 {
    237 	pmap_changebit(pa, 0, ~PG_U);
    238 }
    239 
    240 static __inline void
    241 pmap_copy_on_write(paddr_t pa)
    242 {
    243 	pmap_changebit(pa, PG_RO, ~PG_RW);
    244 }
    245 
    246 static __inline boolean_t
    247 pmap_is_modified(paddr_t pa)
    248 {
    249 	return pmap_testbit(pa, PG_M);
    250 }
    251 
    252 static __inline boolean_t
    253 pmap_is_referenced(paddr_t pa)
    254 {
    255 	return pmap_testbit(pa, PG_U);
    256 }
    257 
    258 static __inline paddr_t
    259 pmap_phys_address(int ppn)
    260 {
    261 	return i386_ptob(ppn);
    262 }
    263 
    264 vaddr_t	pmap_map __P((vaddr_t, paddr_t, paddr_t, int));
    265 
    266 #if defined(USER_LDT)
    267 void	pmap_ldt_cleanup __P((struct proc *));
    268 #define	PMAP_FORK
    269 #endif /* USER_LDT */
    270 
    271 #endif	/* _KERNEL */
    272 
    273 #endif /* _I386_PMAP_H_ */
    274