xref: /dragonfly/sys/platform/pc64/include/pmap.h (revision ce0e08e2)
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department and William Jolitz of UUNET Technologies Inc.
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 University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * Derived from hp300 version by Mike Hibler, this version by William
39  * Jolitz uses a recursive map [a pde points to the page directory] to
40  * map the page tables using the pagetables themselves. This is done to
41  * reduce the impact on kernel virtual memory for lots of sparse address
42  * space, and to reduce the cost of memory to each process.
43  *
44  * from: hp300: @(#)pmap.h	7.2 (Berkeley) 12/16/90
45  * from: @(#)pmap.h	7.4 (Berkeley) 5/12/91
46  * $FreeBSD: src/sys/i386/include/pmap.h,v 1.65.2.3 2001/10/03 07:15:37 peter Exp $
47  * $DragonFly: src/sys/platform/pc64/include/pmap.h,v 1.1 2008/08/29 17:07:17 dillon Exp $
48  */
49 
50 #ifndef _MACHINE_PMAP_H_
51 #define	_MACHINE_PMAP_H_
52 
53 #include <cpu/pmap.h>
54 
55 /*
56  * Size of Kernel address space.  This is the number of page table pages
57  * (2MB each) to use for the kernel.  256 pages == 512 Megabyte.
58  * This **MUST** be a multiple of 4 (eg: 252, 256, 260, etc).
59  */
60 #ifndef KVA_PAGES
61 #define KVA_PAGES	256
62 #endif
63 
64 /*
65  * Pte related macros
66  */
67 #define VADDR(pdi, pti) ((vm_offset_t)(((pdi)<<PDRSHIFT)|((pti)<<PAGE_SHIFT)))
68 
69 #ifndef NKPT
70 #define	NKPT		30	/* actual number of kernel page tables */
71 #endif
72 #ifndef NKPDE
73 #define NKPDE	(KVA_PAGES - 2)	/* addressable number of page tables/pde's */
74 #endif
75 #if NKPDE > KVA_PAGES - 2
76 #error "Maximum NKPDE is KVA_PAGES - 2"
77 #endif
78 
79 /*
80  * The *PTDI values control the layout of virtual memory
81  *
82  * XXX This works for now, but I am not real happy with it, I'll fix it
83  * right after I fix locore.s and the magic 28K hole
84  *
85  * SMP_PRIVPAGES: The per-cpu address space is 0xff80000 -> 0xffbfffff
86  */
87 #define	APTDPTDI	(NPDEPG-1)	/* alt ptd entry that points to APTD */
88 #define MPPTDI		(APTDPTDI-1)	/* per cpu ptd entry */
89 #define	KPTDI		(MPPTDI-NKPDE)	/* start of kernel virtual pde's */
90 #define	PTDPTDI		(KPTDI-1)	/* ptd entry that points to ptd! */
91 #define	UMAXPTDI	(PTDPTDI-1)	/* ptd entry for user space end */
92 #define	UMAXPTEOFF	(NPTEPG)	/* pte entry for user space end */
93 
94 #define LINKPML4I	0
95 #define LINKPDPI	0
96 
97 /*
98  * XXX doesn't really belong here I guess...
99  */
100 #define ISA_HOLE_START    0xa0000
101 #define ISA_HOLE_LENGTH (0x100000-ISA_HOLE_START)
102 
103 #ifndef LOCORE
104 
105 #ifndef _SYS_TYPES_H_
106 #include <sys/types.h>
107 #endif
108 #ifndef _SYS_QUEUE_H_
109 #include <sys/queue.h>
110 #endif
111 #ifndef _MACHINE_TYPES_H_
112 #include <machine/types.h>
113 #endif
114 #ifndef _MACHINE_PARAM_H_
115 #include <machine/param.h>
116 #endif
117 
118 /*
119  * Address of current and alternate address space page table maps
120  * and directories.
121  */
122 #ifdef _KERNEL
123 extern pt_entry_t PTmap[], APTmap[], Upte;
124 extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
125 
126 extern uint64_t IdlePTD;	/* physical address of "Idle" state directory */
127 
128 extern uint64_t common_lvl4_phys;
129 extern uint64_t common_lvl3_phys;
130 extern pdp_entry_t *link_pdpe;
131 #endif
132 
133 #ifdef _KERNEL
134 /*
135  * virtual address to page table entry and
136  * to physical address. Likewise for alternate address space.
137  * Note: these work recursively, thus vtopte of a pte will give
138  * the corresponding pde that in turn maps it.
139  */
140 #define	vtopte(va)	(PTmap + amd64_btop(va))
141 
142 #define	avtopte(va)	(APTmap + amd64_btop(va))
143 
144 /*
145  *	Routine:	pmap_kextract
146  *	Function:
147  *		Extract the physical page address associated
148  *		kernel virtual address.
149  */
150 static __inline vm_paddr_t
151 pmap_kextract(vm_offset_t va)
152 {
153 	vm_paddr_t pa;
154 
155 	if ((pa = (vm_offset_t) PTD[va >> PDRSHIFT]) & PG_PS) {
156 		pa = (pa & ~(NBPDR - 1)) | (va & (NBPDR - 1));
157 	} else {
158 		pa = *(vm_offset_t *)vtopte(va);
159 		pa = (pa & PG_FRAME) | (va & PAGE_MASK);
160 	}
161 	return pa;
162 }
163 
164 /*
165  * XXX
166  */
167 #define	vtophys(va)	pmap_kextract(((vm_offset_t)(va)))
168 #define	vtophys_pte(va)	((pt_entry_t)pmap_kextract(((vm_offset_t)(va))))
169 
170 #endif
171 
172 #define	pte_load_clear(pte)	atomic_readandclear_long(pte)
173 
174 /*
175  * Pmap stuff
176  */
177 struct pv_entry;
178 struct vm_page;
179 struct vm_object;
180 
181 struct md_page {
182 	int pv_list_count;
183 	TAILQ_HEAD(,pv_entry)	pv_list;
184 };
185 
186 /*
187  * Each machine dependent implementation is expected to
188  * keep certain statistics.  They may do this anyway they
189  * so choose, but are expected to return the statistics
190  * in the following structure.
191  *
192  * NOTE: We try to match the size of the pc32 pmap with the vkernel pmap
193  * so the same utilities (like 'ps') can be used on both.
194  */
195 struct pmap_statistics {
196 	long resident_count;    /* # of pages mapped (total) */
197 	long wired_count;       /* # of pages wired */
198 };
199 typedef struct pmap_statistics *pmap_statistics_t;
200 
201 struct pmap {
202 	pd_entry_t		*pm_pdir;	/* KVA of page directory */
203 	struct vm_page		*pm_pdirm;	/* VM page for pg directory */
204 	struct vm_object	*pm_pteobj;	/* Container for pte's */
205 	TAILQ_ENTRY(pmap)	pm_pmnode;	/* list of pmaps */
206 	TAILQ_HEAD(,pv_entry)	pm_pvlist;	/* list of mappings in pmap */
207 	int			pm_count;	/* reference count */
208 	cpumask_t		pm_active;	/* active on cpus */
209 	int			pm_filler02;	/* (filler sync w/vkernel) */
210 	struct pmap_statistics	pm_stats;	/* pmap statistics */
211 	struct	vm_page		*pm_ptphint;	/* pmap ptp hint */
212 	int			pm_generation;	/* detect pvlist deletions */
213 };
214 
215 #define pmap_resident_count(pmap) (pmap)->pm_stats.resident_count
216 
217 typedef struct pmap	*pmap_t;
218 
219 #ifdef _KERNEL
220 extern struct pmap	kernel_pmap;
221 #endif
222 
223 /*
224  * For each vm_page_t, there is a list of all currently valid virtual
225  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
226  */
227 typedef struct pv_entry {
228 	pmap_t		pv_pmap;	/* pmap where mapping lies */
229 	vm_offset_t	pv_va;		/* virtual address for mapping */
230 	TAILQ_ENTRY(pv_entry)	pv_list;
231 	TAILQ_ENTRY(pv_entry)	pv_plist;
232 	struct vm_page	*pv_ptem;	/* VM page for pte */
233 } *pv_entry_t;
234 
235 #ifdef	_KERNEL
236 
237 #define NPPROVMTRR		8
238 #define PPRO_VMTRRphysBase0	0x200
239 #define PPRO_VMTRRphysMask0	0x201
240 struct ppro_vmtrr {
241 	u_int64_t base, mask;
242 };
243 extern struct ppro_vmtrr PPro_vmtrr[NPPROVMTRR];
244 
245 extern caddr_t	CADDR1;
246 extern pt_entry_t *CMAP1;
247 extern vm_paddr_t avail_end;
248 extern vm_paddr_t avail_start;
249 extern vm_offset_t clean_eva;
250 extern vm_offset_t clean_sva;
251 extern char *ptvmmap;		/* poor name! */
252 
253 void	pmap_bootstrap ( vm_paddr_t *, vm_paddr_t);
254 void	*pmap_mapdev (vm_paddr_t, vm_size_t);
255 void	pmap_unmapdev (vm_offset_t, vm_size_t);
256 pt_entry_t *pmap_pte (pmap_t, vm_offset_t) __pure2;
257 struct vm_page *pmap_use_pt (pmap_t, vm_offset_t);
258 #ifdef SMP
259 void	pmap_set_opt (void);
260 #endif
261 
262 #endif /* _KERNEL */
263 
264 #endif /* !LOCORE */
265 
266 #endif /* !_MACHINE_PMAP_H_ */
267