kernel - Rewrite the x86-64 pmap code
[dragonfly.git] / sys / platform / pc64 / include / pmap.h
CommitLineData
c8fe38ae
MD
1/*
2 * Copyright (c) 1991 Regents of the University of California.
48ffc236 3 * Copyright (c) 2003 Peter Wemm.
c8fe38ae
MD
4 * Copyright (c) 2008 The DragonFly Project.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department and William Jolitz of UUNET Technologies Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * Derived from hp300 version by Mike Hibler, this version by William
40 * Jolitz uses a recursive map [a pde points to the page directory] to
41 * map the page tables using the pagetables themselves. This is done to
42 * reduce the impact on kernel virtual memory for lots of sparse address
43 * space, and to reduce the cost of memory to each process.
44 *
45 * from: hp300: @(#)pmap.h 7.2 (Berkeley) 12/16/90
46 * from: @(#)pmap.h 7.4 (Berkeley) 5/12/91
47 * $FreeBSD: src/sys/i386/include/pmap.h,v 1.65.2.3 2001/10/03 07:15:37 peter Exp $
48 * $DragonFly: src/sys/platform/pc64/include/pmap.h,v 1.1 2008/08/29 17:07:17 dillon Exp $
49 */
50
51#ifndef _MACHINE_PMAP_H_
52#define _MACHINE_PMAP_H_
53
54#include <cpu/pmap.h>
55
56/*
57 * Size of Kernel address space. This is the number of page table pages
58 * (2MB each) to use for the kernel. 256 pages == 512 Megabyte.
59 * This **MUST** be a multiple of 4 (eg: 252, 256, 260, etc).
60 */
61#ifndef KVA_PAGES
62#define KVA_PAGES 256
63#endif
64
65/*
48ffc236
JG
66 * Pte related macros. This is complicated by having to deal with
67 * the sign extension of the 48th bit.
c8fe38ae 68 */
48ffc236
JG
69#define KVADDR(l4, l3, l2, l1) ( \
70 ((unsigned long)-1 << 47) | \
71 ((unsigned long)(l4) << PML4SHIFT) | \
72 ((unsigned long)(l3) << PDPSHIFT) | \
73 ((unsigned long)(l2) << PDRSHIFT) | \
74 ((unsigned long)(l1) << PAGE_SHIFT))
75
76#define UVADDR(l4, l3, l2, l1) ( \
77 ((unsigned long)(l4) << PML4SHIFT) | \
78 ((unsigned long)(l3) << PDPSHIFT) | \
79 ((unsigned long)(l2) << PDRSHIFT) | \
80 ((unsigned long)(l1) << PAGE_SHIFT))
81
791c6551 82/*
ad54aa11 83 * NOTE: We no longer hardwire NKPT, it is calculated in create_pagetables()
791c6551 84 */
48ffc236 85#define NKPML4E 1 /* number of kernel PML4 slots */
791c6551 86/* NKPDPE defined in vmparam.h */
48ffc236 87
701c977e
MD
88/*
89 * NUPDPs 512 (256 user) number of PDPs in user page table
90 * NUPDs 512 * 512 number of PDs in user page table
91 * NUPTs 512 * 512 * 512 number of PTs in user page table
92 * NUPTEs 512 * 512 * 512 * 512 number of PTEs in user page table
93 *
94 * NUPDP_USER number of PDPs reserved for userland
95 * NUPTE_USER number of PTEs reserved for userland (big number)
96 */
97#define NUPDP_USER (NPML4EPG/2)
98#define NUPDP_TOTAL (NPML4EPG)
99#define NUPD_TOTAL (NPDPEPG * NUPDP_TOTAL)
100#define NUPT_TOTAL (NPDEPG * NUPD_TOTAL)
101#define NUPTE_TOTAL ((vm_pindex_t)NPTEPG * NUPT_TOTAL)
102#define NUPTE_USER ((vm_pindex_t)NPTEPG * NPDEPG * NPDPEPG * NUPDP_USER)
48ffc236
JG
103
104#define NDMPML4E 1 /* number of dmap PML4 slots */
105
c8fe38ae 106/*
da23a592
MD
107 * The *PML4I values control the layout of virtual memory. Each PML4
108 * entry represents 512G.
c8fe38ae 109 */
48ffc236
JG
110#define PML4PML4I (NPML4EPG/2) /* Index of recursive pml4 mapping */
111
112#define KPML4I (NPML4EPG-1) /* Top 512GB for KVM */
113#define DMPML4I (KPML4I-1) /* Next 512GB down for direct map */
c8fe38ae 114
da23a592
MD
115/*
116 * The location of KERNBASE in the last PD of the kernel's KVM (KPML4I)
117 * space. Each PD represents 1GB. The kernel must be placed here
118 * for the compile/link options to work properly so absolute 32-bit
119 * addressing can be used to access stuff.
120 */
48ffc236
JG
121#define KPDPI (NPDPEPG-2) /* kernbase at -2GB */
122
da23a592
MD
123/*
124 * per-CPU data assume ~64K x SMP_MAXCPU, say up to 256 cpus
125 * in the future or 16MB of space. Each PD represents 2MB so
126 * use NPDEPG-8 to place the per-CPU data.
127 */
48ffc236
JG
128#define MPPML4I KPML4I
129#define MPPDPI KPDPI
da23a592 130#define MPPTDI (NPDEPG-8)
c8fe38ae
MD
131
132/*
133 * XXX doesn't really belong here I guess...
134 */
135#define ISA_HOLE_START 0xa0000
136#define ISA_HOLE_LENGTH (0x100000-ISA_HOLE_START)
137
138#ifndef LOCORE
139
140#ifndef _SYS_TYPES_H_
141#include <sys/types.h>
142#endif
143#ifndef _SYS_QUEUE_H_
144#include <sys/queue.h>
145#endif
701c977e
MD
146#ifndef _SYS_TREE_H_
147#include <sys/tree.h>
148#endif
b12defdc
MD
149#ifndef _SYS_SPINLOCK_H_
150#include <sys/spinlock.h>
151#endif
152#ifndef _SYS_THREAD_H_
153#include <sys/thread.h>
154#endif
c8fe38ae
MD
155#ifndef _MACHINE_TYPES_H_
156#include <machine/types.h>
157#endif
158#ifndef _MACHINE_PARAM_H_
159#include <machine/param.h>
160#endif
161
162/*
163 * Address of current and alternate address space page table maps
164 * and directories.
165 */
166#ifdef _KERNEL
48ffc236
JG
167#define addr_PTmap (KVADDR(PML4PML4I, 0, 0, 0))
168#define addr_PDmap (KVADDR(PML4PML4I, PML4PML4I, 0, 0))
169#define addr_PDPmap (KVADDR(PML4PML4I, PML4PML4I, PML4PML4I, 0))
170#define addr_PML4map (KVADDR(PML4PML4I, PML4PML4I, PML4PML4I, PML4PML4I))
171#define addr_PML4pml4e (addr_PML4map + (PML4PML4I * sizeof(pml4_entry_t)))
172#define PTmap ((pt_entry_t *)(addr_PTmap))
173#define PDmap ((pd_entry_t *)(addr_PDmap))
174#define PDPmap ((pd_entry_t *)(addr_PDPmap))
175#define PML4map ((pd_entry_t *)(addr_PML4map))
176#define PML4pml4e ((pd_entry_t *)(addr_PML4pml4e))
177
178extern u_int64_t KPML4phys; /* physical address of kernel level 4 */
c8fe38ae
MD
179#endif
180
181#ifdef _KERNEL
c8fe38ae
MD
182
183/*
184 * XXX
185 */
186#define vtophys(va) pmap_kextract(((vm_offset_t)(va)))
187#define vtophys_pte(va) ((pt_entry_t)pmap_kextract(((vm_offset_t)(va))))
188
189#endif
190
191#define pte_load_clear(pte) atomic_readandclear_long(pte)
192
48ffc236
JG
193static __inline void
194pte_store(pt_entry_t *ptep, pt_entry_t pte)
195{
48ffc236
JG
196 *ptep = pte;
197}
198
199#define pde_store(pdep, pde) pte_store((pdep), (pde))
200
c8fe38ae
MD
201/*
202 * Pmap stuff
203 */
204struct pv_entry;
205struct vm_page;
206struct vm_object;
c2fb025d 207struct vmspace;
c8fe38ae
MD
208
209struct md_page {
c8fe38ae
MD
210 TAILQ_HEAD(,pv_entry) pv_list;
211};
212
213/*
214 * Each machine dependent implementation is expected to
215 * keep certain statistics. They may do this anyway they
216 * so choose, but are expected to return the statistics
217 * in the following structure.
218 *
219 * NOTE: We try to match the size of the pc32 pmap with the vkernel pmap
220 * so the same utilities (like 'ps') can be used on both.
221 */
222struct pmap_statistics {
223 long resident_count; /* # of pages mapped (total) */
224 long wired_count; /* # of pages wired */
225};
226typedef struct pmap_statistics *pmap_statistics_t;
227
701c977e
MD
228struct pv_entry_rb_tree;
229RB_PROTOTYPE2(pv_entry_rb_tree, pv_entry, pv_entry,
230 pv_entry_compare, vm_pindex_t);
231
c8fe38ae 232struct pmap {
48ffc236 233 pml4_entry_t *pm_pml4; /* KVA of level 4 page table */
701c977e 234 struct pv_entry *pm_pmlpv; /* PV entry for pml4 */
c8fe38ae 235 TAILQ_ENTRY(pmap) pm_pmnode; /* list of pmaps */
701c977e 236 RB_HEAD(pv_entry_rb_tree, pv_entry) pm_pvroot;
c8fe38ae
MD
237 int pm_count; /* reference count */
238 cpumask_t pm_active; /* active on cpus */
239 int pm_filler02; /* (filler sync w/vkernel) */
240 struct pmap_statistics pm_stats; /* pmap statistics */
701c977e 241 struct pv_entry *pm_pvhint; /* pv_entry lookup hint */
c8fe38ae 242 int pm_generation; /* detect pvlist deletions */
b12defdc
MD
243 struct spinlock pm_spin;
244 struct lwkt_token pm_token;
c8fe38ae
MD
245};
246
da23a592
MD
247#define CPUMASK_LOCK CPUMASK(SMP_MAXCPU)
248#define CPUMASK_BIT SMP_MAXCPU /* for 1LLU << SMP_MAXCPU */
c2fb025d 249
c8fe38ae
MD
250#define pmap_resident_count(pmap) (pmap)->pm_stats.resident_count
251
252typedef struct pmap *pmap_t;
253
254#ifdef _KERNEL
255extern struct pmap kernel_pmap;
256#endif
257
258/*
259 * For each vm_page_t, there is a list of all currently valid virtual
260 * mappings of that page. An entry is a pv_entry_t, the list is pv_table.
261 */
262typedef struct pv_entry {
263 pmap_t pv_pmap; /* pmap where mapping lies */
701c977e 264 vm_pindex_t pv_pindex; /* PTE, PT, PD, PDP, or PML4 */
c8fe38ae 265 TAILQ_ENTRY(pv_entry) pv_list;
701c977e
MD
266 RB_ENTRY(pv_entry) pv_entry;
267 struct vm_page *pv_m; /* page being mapped */
268 u_int pv_hold; /* interlock action */
269 u_int pv_unused01;
270#ifdef PMAP_DEBUG
271 const char *pv_func;
272 int pv_line;
273#endif
c8fe38ae
MD
274} *pv_entry_t;
275
701c977e
MD
276#define PV_HOLD_LOCKED 0x80000000U
277#define PV_HOLD_WAITING 0x40000000U
278#define PV_HOLD_DELETED 0x20000000U
279#define PV_HOLD_MASK 0x1FFFFFFFU
280
c8fe38ae
MD
281#ifdef _KERNEL
282
283#define NPPROVMTRR 8
284#define PPRO_VMTRRphysBase0 0x200
285#define PPRO_VMTRRphysMask0 0x201
286struct ppro_vmtrr {
287 u_int64_t base, mask;
288};
289extern struct ppro_vmtrr PPro_vmtrr[NPPROVMTRR];
290
291extern caddr_t CADDR1;
292extern pt_entry_t *CMAP1;
b24cd69c 293extern vm_paddr_t dump_avail[];
c8fe38ae
MD
294extern vm_paddr_t avail_end;
295extern vm_paddr_t avail_start;
296extern vm_offset_t clean_eva;
297extern vm_offset_t clean_sva;
298extern char *ptvmmap; /* poor name! */
299
b12defdc 300void pmap_release(struct pmap *pmap);
c2fb025d
MD
301void pmap_interlock_wait (struct vmspace *);
302void pmap_bootstrap (vm_paddr_t *);
c8fe38ae 303void *pmap_mapdev (vm_paddr_t, vm_size_t);
057877ac 304void *pmap_mapdev_uncacheable(vm_paddr_t, vm_size_t);
c8fe38ae 305void pmap_unmapdev (vm_offset_t, vm_size_t);
c8fe38ae
MD
306struct vm_page *pmap_use_pt (pmap_t, vm_offset_t);
307#ifdef SMP
308void pmap_set_opt (void);
309#endif
48ffc236 310vm_paddr_t pmap_kextract(vm_offset_t);
c8fe38ae
MD
311
312#endif /* _KERNEL */
313
314#endif /* !LOCORE */
315
316#endif /* !_MACHINE_PMAP_H_ */