kernel - Fix some rare pmap races in i386 and x86_64.
[dragonfly.git] / sys / platform / pc32 / include / pmap.h
blob7973fcd68b72316841272c1ab7f628429df5b08d
1 /*
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
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.
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.
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.
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.
43 * from: hp300: @(#)pmap.h 7.2 (Berkeley) 12/16/90
44 * from: @(#)pmap.h 7.4 (Berkeley) 5/12/91
45 * $FreeBSD: src/sys/i386/include/pmap.h,v 1.65.2.3 2001/10/03 07:15:37 peter Exp $
46 * $DragonFly: src/sys/platform/pc32/include/pmap.h,v 1.7 2007/06/08 00:57:04 dillon Exp $
49 #ifndef _MACHINE_PMAP_H_
50 #define _MACHINE_PMAP_H_
52 #include <cpu/pmap.h>
55 * Size of Kernel address space. This is the number of page table pages
56 * (4MB each) to use for the kernel. 256 pages == 1 Gigabyte.
57 * This **MUST** be a multiple of 4 (eg: 252, 256, 260, etc).
59 #ifndef KVA_PAGES
60 #define KVA_PAGES 256
61 #endif
64 * PTE related macros
66 #define VADDR(pdi, pti) ((vm_offset_t)(((pdi)<<PDRSHIFT)|((pti)<<PAGE_SHIFT)))
68 #ifndef NKPT
69 #define NKPT 30 /* starting general kptds */
70 #endif
72 #define NKGDPDE SMP_MAXCPU /* 16 typical */
74 #ifndef NKPDE
75 #define NKPDE (KVA_PAGES - NKGDPDE - 2) /* max general kptds */
76 #endif
77 #if NKPDE > KVA_PAGES - NKGDPDE - 2
78 #error "Maximum NKPDE is KVA_PAGES - NKGDPDE - 2"
79 #endif
82 * The *PTDI values control the layout of virtual memory
84 * NPEDEPG - number of pde's in the page directory (1024)
85 * NKPDE - max general kernel page table pages not including
86 * special PTDs. Typically KVA_PAGES minus the number
87 * of special PTDs.
89 * +---------------+ End of kernel memory
90 * | APTDPTDI | currently unused alt page table map
91 * +---------------+
92 * | MPPTDI | globaldata array
93 * +---------------+
94 * | |
95 * | | per-cpu page table self-maps
96 * |KGDTDI[NKGDPDE]|
97 * +---------------+
98 * | |
99 * | |
100 * | |
101 * | | general kernel page table pages
102 * | |
103 * | KPTDI[NKPDE] |
104 * +---------------+ Start of kernel memory
105 * | PTDPTDI | self-mapping of current pmap
106 * +---------------+
108 * This typically places PTDPTDI at the index corresponding to VM address
109 * (0xc0000000 - 4M) = bfc00000, and that is where PTmap[] is based for
110 * the self-mapped page table. PTD points to the self-mapped page
111 * directory itself and any indexes >= KPTDI will correspond to the
112 * common kernel page directory pages since all pmaps map the same ones.
114 * We no longer use APTmap or APTDpde (corresponding to APTDPTDI). This
115 * was a global page table map for accessing pmaps other then the current
116 * pmap. Instead we now implement an alternative pmap for EACH cpu
117 * use the ptds at KGDTDI.
119 * Even though the maps are per-cpu the PTD entries are stored in the
120 * individual pmaps and obviously not replicated so each process pmap
121 * essentially gets its own per-cpu cache (PxN) making for fairly efficient
122 * access.
124 * UMAXPTDI - highest inclusive ptd index for user space
126 #define APTDPTDI (NPDEPG-1) /* alt ptd entry that points to APTD */
127 #define MPPTDI (APTDPTDI-1) /* globaldata array ptd entry */
128 #define KGDTDI (MPPTDI-NKGDPDE) /* per-cpu page table mappings */
129 #define KPTDI (KGDTDI-NKPDE) /* start of kernel virtual pde's */
130 #define PTDPTDI (KPTDI-1) /* ptd entry that points to ptd! */
131 #define UMAXPTDI (PTDPTDI-1) /* ptd entry for user space end */
134 * XXX doesn't really belong here I guess...
136 #define ISA_HOLE_START 0xa0000
137 #define ISA_HOLE_LENGTH (0x100000-ISA_HOLE_START)
139 #ifndef LOCORE
141 #ifndef _SYS_TYPES_H_
142 #include <sys/types.h>
143 #endif
144 #ifndef _SYS_QUEUE_H_
145 #include <sys/queue.h>
146 #endif
147 #ifndef _MACHINE_TYPES_H_
148 #include <machine/types.h>
149 #endif
150 #ifndef _MACHINE_PARAM_H_
151 #include <machine/param.h>
152 #endif
155 * Address of current and alternate address space page table maps
156 * and directories.
158 #ifdef _KERNEL
159 extern pt_entry_t PTmap[], APTmap[], Upte;
160 extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
162 extern pd_entry_t IdlePTD; /* physical address of "Idle" state directory */
163 #endif
165 #ifdef _KERNEL
167 * virtual address to page table entry and
168 * to physical address. Likewise for alternate address space.
169 * Note: these work recursively, thus vtopte of a pte will give
170 * the corresponding pde that in turn maps it.
172 #define vtopte(va) (PTmap + i386_btop(va))
174 #define avtopte(va) (APTmap + i386_btop(va))
177 * Routine: pmap_kextract
178 * Function:
179 * Extract the physical page address associated
180 * kernel virtual address.
182 static __inline vm_paddr_t
183 pmap_kextract(vm_offset_t va)
185 vm_paddr_t pa;
187 if ((pa = (vm_offset_t) PTD[va >> PDRSHIFT]) & PG_PS) {
188 pa = (pa & ~(NBPDR - 1)) | (va & (NBPDR - 1));
189 } else {
190 pa = *(vm_offset_t *)vtopte(va);
191 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
193 return pa;
197 * XXX
199 #define vtophys(va) pmap_kextract(((vm_offset_t)(va)))
200 #define vtophys_pte(va) ((pt_entry_t)pmap_kextract(((vm_offset_t)(va))))
202 #endif
205 * Pmap stuff
207 struct pv_entry;
208 struct vm_page;
209 struct vm_object;
210 struct vmspace;
212 struct md_page {
213 int pv_list_count;
214 TAILQ_HEAD(,pv_entry) pv_list;
218 * Each machine dependent implementation is expected to
219 * keep certain statistics. They may do this anyway they
220 * so choose, but are expected to return the statistics
221 * in the following structure.
223 * NOTE: We try to match the size of the pc32 pmap with the vkernel pmap
224 * so the same utilities (like 'ps') can be used on both.
226 struct pmap_statistics {
227 long resident_count; /* # of pages mapped (total) */
228 long wired_count; /* # of pages wired */
230 typedef struct pmap_statistics *pmap_statistics_t;
232 struct pmap {
233 pd_entry_t *pm_pdir; /* KVA of page directory */
234 struct vm_page *pm_pdirm; /* VM page for pg directory */
235 struct vm_object *pm_pteobj; /* Container for pte's */
236 TAILQ_ENTRY(pmap) pm_pmnode; /* list of pmaps */
237 TAILQ_HEAD(,pv_entry) pm_pvlist; /* list of mappings in pmap */
238 int pm_count; /* reference count */
239 cpumask_t pm_active; /* active on cpus */
240 cpumask_t pm_cached; /* cached on cpus */
241 int pm_filler02; /* (filler sync w/vkernel) */
242 struct pmap_statistics pm_stats; /* pmap statistics */
243 struct vm_page *pm_ptphint; /* pmap ptp hint */
244 int pm_generation; /* detect pvlist deletions */
247 #define pmap_resident_count(pmap) (pmap)->pm_stats.resident_count
249 #define CPUMASK_LOCK (1 << SMP_MAXCPU)
251 typedef struct pmap *pmap_t;
253 #ifdef _KERNEL
254 extern struct pmap kernel_pmap;
255 #endif
258 * For each vm_page_t, there is a list of all currently valid virtual
259 * mappings of that page. An entry is a pv_entry_t, the list is pv_table.
261 typedef struct pv_entry {
262 pmap_t pv_pmap; /* pmap where mapping lies */
263 vm_offset_t pv_va; /* virtual address for mapping */
264 TAILQ_ENTRY(pv_entry) pv_list;
265 TAILQ_ENTRY(pv_entry) pv_plist;
266 struct vm_page *pv_ptem; /* VM page for pte */
267 #ifdef PMAP_DEBUG
268 struct vm_page *pv_m;
269 #else
270 void *pv_dummy; /* align structure to 32 bytes */
271 #endif
272 } *pv_entry_t;
274 #ifdef _KERNEL
276 #define NPPROVMTRR 8
277 #define PPRO_VMTRRphysBase0 0x200
278 #define PPRO_VMTRRphysMask0 0x201
279 struct ppro_vmtrr {
280 u_int64_t base, mask;
282 extern struct ppro_vmtrr PPro_vmtrr[NPPROVMTRR];
284 extern caddr_t CADDR1;
285 extern pt_entry_t *CMAP1;
286 extern vm_paddr_t dump_avail[];
287 extern vm_paddr_t avail_end;
288 extern vm_paddr_t avail_start;
289 extern vm_offset_t clean_eva;
290 extern vm_offset_t clean_sva;
291 extern char *ptvmmap; /* poor name! */
293 void pmap_interlock_wait (struct vmspace *);
294 void pmap_bootstrap (vm_paddr_t, vm_paddr_t);
295 void *pmap_mapdev (vm_paddr_t, vm_size_t);
296 void pmap_unmapdev (vm_offset_t, vm_size_t);
297 unsigned *pmap_pte (pmap_t, vm_offset_t) __pure2;
298 struct vm_page *pmap_use_pt (pmap_t, vm_offset_t);
299 int pmap_get_pgeflag(void);
300 #ifdef SMP
301 void pmap_set_opt (void);
302 #endif
304 #endif /* _KERNEL */
306 #endif /* !LOCORE */
308 #endif /* !_MACHINE_PMAP_H_ */