xref: /freebsd/sys/amd64/include/pmap.h (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * the Systems Programming Group of the University of Utah Computer
7  * Science Department and William Jolitz of UUNET Technologies Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * Derived from hp300 version by Mike Hibler, this version by William
38  * Jolitz uses a recursive map [a pde points to the page directory] to
39  * map the page tables using the pagetables themselves. This is done to
40  * reduce the impact on kernel virtual memory for lots of sparse address
41  * space, and to reduce the cost of memory to each process.
42  *
43  *	from: hp300: @(#)pmap.h	7.2 (Berkeley) 12/16/90
44  *	from: @(#)pmap.h	7.4 (Berkeley) 5/12/91
45  * 	$Id: pmap.h,v 1.42 1996/07/30 03:08:57 dyson Exp $
46  */
47 
48 #ifndef _MACHINE_PMAP_H_
49 #define	_MACHINE_PMAP_H_
50 
51 
52 /*
53  * Page-directory and page-table entires follow this format, with a few
54  * of the fields not present here and there, depending on a lot of things.
55  */
56 				/* ---- Intel Nomenclature ---- */
57 #define	PG_V		0x001	/* P	Valid			*/
58 #define PG_RW		0x002	/* R/W	Read/Write		*/
59 #define PG_U		0x004	/* U/S  User/Supervisor		*/
60 #define	PG_NC_PWT	0x008	/* PWT	Write through		*/
61 #define	PG_NC_PCD	0x010	/* PCD	Cache disable		*/
62 #define PG_A		0x020	/* A	Accessed		*/
63 #define	PG_M		0x040	/* D	Dirty			*/
64 #define	PG_PS		0x080	/* PS	Page size (0=4k,1=4M)	*/
65 #define	PG_G		0x100	/* G	Global			*/
66 #define	PG_AVAIL1	0x200	/*    /	Available for system	*/
67 #define	PG_AVAIL2	0x400	/*   <	programmers use		*/
68 #define	PG_AVAIL3	0x800	/*    \				*/
69 
70 
71 /* Our various interpretations of the above */
72 #define PG_W		PG_AVAIL1	/* "Wired" pseudoflag */
73 #define	PG_MANAGED	PG_AVAIL2
74 #define	PG_FRAME	(~PAGE_MASK)
75 #define	PG_PROT		(PG_RW|PG_U)	/* all protection bits . */
76 #define PG_N		(PG_NC_PWT|PG_NC_PCD)	/* Non-cacheable */
77 
78 /*
79  * Page Protection Exception bits
80  */
81 
82 #define PGEX_P		0x01	/* Protection violation vs. not present */
83 #define PGEX_W		0x02	/* during a Write cycle */
84 #define PGEX_U		0x04	/* access from User mode (UPL) */
85 
86 /*
87  * Pte related macros
88  */
89 #define VADDR(pdi, pti) ((vm_offset_t)(((pdi)<<PDRSHIFT)|((pti)<<PAGE_SHIFT)))
90 
91 #ifndef NKPT
92 #define	NKPT			9	/* actual number of kernel page tables */
93 #endif
94 #ifndef NKPDE
95 #define NKPDE			63	/* addressable number of page tables/pde's */
96 #endif
97 
98 /*
99  * The *PTDI values control the layout of virtual memory
100  *
101  * XXX This works for now, but I am not real happy with it, I'll fix it
102  * right after I fix locore.s and the magic 28K hole
103  */
104 #define	APTDPTDI	(NPDEPG-1)	/* alt ptd entry that points to APTD */
105 #define	KPTDI		(APTDPTDI-NKPDE)/* start of kernel virtual pde's */
106 #define	PTDPTDI		(KPTDI-1)	/* ptd entry that points to ptd! */
107 #define	KSTKPTDI	(PTDPTDI-1)	/* ptd entry for u./kernel&user stack */
108 #define KSTKPTEOFF	(NPTEPG-UPAGES) /* pte entry for kernel stack */
109 
110 /*
111  * XXX doesn't really belong here I guess...
112  */
113 #define ISA_HOLE_START    0xa0000
114 #define ISA_HOLE_LENGTH (0x100000-ISA_HOLE_START)
115 
116 #ifndef LOCORE
117 
118 #include <sys/queue.h>
119 
120 typedef unsigned int *pd_entry_t;
121 typedef unsigned int *pt_entry_t;
122 
123 #define PDESIZE		sizeof(pd_entry_t) /* for assembly files */
124 #define PTESIZE		sizeof(pt_entry_t) /* for assembly files */
125 
126 /*
127  * Address of current and alternate address space page table maps
128  * and directories.
129  */
130 #ifdef KERNEL
131 extern pt_entry_t PTmap[], APTmap[], Upte;
132 extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
133 
134 extern int	IdlePTD;	/* physical address of "Idle" state directory */
135 #endif
136 
137 /*
138  * virtual address to page table entry and
139  * to physical address. Likewise for alternate address space.
140  * Note: these work recursively, thus vtopte of a pte will give
141  * the corresponding pde that in turn maps it.
142  */
143 #define	vtopte(va)	(PTmap + i386_btop(va))
144 #define	vtophys(va)	(((int) (*vtopte(va))&PG_FRAME) | ((int)(va) & PAGE_MASK))
145 
146 #define	avtopte(va)	(APTmap + i386_btop(va))
147 #define	avtophys(va)	(((int) (*avtopte(va))&PG_FRAME) | ((int)(va) & PAGE_MASK))
148 
149 #ifdef KERNEL
150 /*
151  *	Routine:	pmap_kextract
152  *	Function:
153  *		Extract the physical page address associated
154  *		kernel virtual address.
155  */
156 static __inline vm_offset_t
157 pmap_kextract(vm_offset_t va)
158 {
159 	vm_offset_t pa = *(int *)vtopte(va);
160 	pa = (pa & PG_FRAME) | (va & PAGE_MASK);
161 	return pa;
162 }
163 #endif
164 
165 struct vm_page;
166 
167 /*
168  * Pmap stuff
169  */
170 struct	pv_entry;
171 typedef struct {
172 	int pv_list_count;
173 	struct vm_page		*pv_vm_page;
174 	TAILQ_HEAD(,pv_entry)	pv_list;
175 } pv_table_t;
176 
177 struct pmap {
178 	pd_entry_t		*pm_pdir;	/* KVA of page directory */
179 	vm_object_t		pm_pteobj;	/* Container for pte's */
180 	TAILQ_HEAD(,pv_entry)	pm_pvlist;	/* list of mappings in pmap */
181 	int			pm_count;	/* reference count */
182 	int			pm_flags;	/* pmap flags */
183 	struct pmap_statistics	pm_stats;	/* pmap statistics */
184 	struct	vm_page		*pm_ptphint;	/* pmap ptp hint */
185 };
186 
187 #define PM_FLAG_LOCKED	0x1
188 #define PM_FLAG_WANTED	0x2
189 
190 typedef struct pmap	*pmap_t;
191 
192 #ifdef KERNEL
193 extern pmap_t		kernel_pmap;
194 #endif
195 
196 
197 /*
198  * For each vm_page_t, there is a list of all currently valid virtual
199  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
200  */
201 typedef struct pv_entry {
202 	pmap_t		pv_pmap;	/* pmap where mapping lies */
203 	vm_offset_t	pv_va;		/* virtual address for mapping */
204 	TAILQ_ENTRY(pv_entry)	pv_list;
205 	TAILQ_ENTRY(pv_entry)	pv_plist;
206 	vm_page_t	pv_ptem;	/* VM page for pte */
207 } *pv_entry_t;
208 
209 #define	PV_ENTRY_NULL	((pv_entry_t) 0)
210 
211 #define	PV_CI		0x01	/* all entries must be cache inhibited */
212 #define	PV_PTPAGE	0x02	/* entry maps a page table page */
213 
214 #ifdef	KERNEL
215 
216 extern caddr_t	CADDR1;
217 extern pt_entry_t *CMAP1;
218 extern vm_offset_t avail_end;
219 extern vm_offset_t avail_start;
220 extern vm_offset_t phys_avail[];
221 pv_table_t *pv_table;
222 extern vm_offset_t virtual_avail;
223 extern vm_offset_t virtual_end;
224 
225 #define	pa_index(pa)		atop(pa - vm_first_phys)
226 #define	pa_to_pvh(pa)		(&pv_table[pa_index(pa)])
227 
228 #define	pmap_resident_count(pmap)	((pmap)->pm_stats.resident_count)
229 
230 struct pcb;
231 
232 void	pmap_bootstrap __P(( vm_offset_t, vm_offset_t));
233 pmap_t	pmap_kernel __P((void));
234 void	*pmap_mapdev __P((vm_offset_t, vm_size_t));
235 unsigned * __pure pmap_pte __P((pmap_t, vm_offset_t)) __pure2;
236 int	pmap_unuse_pt __P((pmap_t, vm_offset_t, vm_page_t));
237 vm_page_t pmap_use_pt __P((pmap_t, vm_offset_t));
238 
239 #endif /* KERNEL */
240 #endif /* !LOCORE */
241 
242 #endif /* !_MACHINE_PMAP_H_ */
243