Merge branch 'master' into amd64
[dragonfly.git] / sys / platform / pc64 / include / pmap.h
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * Copyright (c) 2003 Peter Wemm.
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 /*
66  * Pte related macros.  This is complicated by having to deal with
67  * the sign extension of the 48th bit.
68  */
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
82 /* Initial number of kernel page tables. */
83 #ifndef NKPT
84 #define NKPT            32
85 #endif
86
87 #define NKPML4E         1               /* number of kernel PML4 slots */
88 #define NKPDPE          howmany(NKPT, NPDEPG)/* number of kernel PDP slots */
89
90 #define NUPML4E         (NPML4EPG/2)    /* number of userland PML4 pages */
91 #define NUPDPE          (NUPML4E*NPDPEPG)/* number of userland PDP pages */
92 #define NUPDE           (NUPDPE*NPDEPG) /* number of userland PD entries */
93
94 #define NDMPML4E        1               /* number of dmap PML4 slots */
95
96 /*
97  * The *PML4I values control the layout of virtual memory
98  */
99 #define PML4PML4I       (NPML4EPG/2)    /* Index of recursive pml4 mapping */
100
101 #define KPML4I          (NPML4EPG-1)    /* Top 512GB for KVM */
102 #define DMPML4I         (KPML4I-1)      /* Next 512GB down for direct map */
103
104 #define KPDPI           (NPDPEPG-2)     /* kernbase at -2GB */
105
106 /* per-CPU data is at -2MB */
107 /* XXX can the kernel decide to use this memory for something else? */
108 #define MPPML4I         KPML4I
109 #define MPPDPI          KPDPI
110 #define MPPTDI          (NPDEPG-1)
111
112 /*
113  * XXX doesn't really belong here I guess...
114  */
115 #define ISA_HOLE_START    0xa0000
116 #define ISA_HOLE_LENGTH (0x100000-ISA_HOLE_START)
117
118 #ifndef LOCORE
119
120 #ifndef _SYS_TYPES_H_
121 #include <sys/types.h>
122 #endif
123 #ifndef _SYS_QUEUE_H_
124 #include <sys/queue.h>
125 #endif
126 #ifndef _MACHINE_TYPES_H_
127 #include <machine/types.h>
128 #endif
129 #ifndef _MACHINE_PARAM_H_
130 #include <machine/param.h>
131 #endif
132
133 /*
134  * Address of current and alternate address space page table maps
135  * and directories.
136  */
137 #ifdef _KERNEL
138 #define addr_PTmap      (KVADDR(PML4PML4I, 0, 0, 0))
139 #define addr_PDmap      (KVADDR(PML4PML4I, PML4PML4I, 0, 0))
140 #define addr_PDPmap     (KVADDR(PML4PML4I, PML4PML4I, PML4PML4I, 0))
141 #define addr_PML4map    (KVADDR(PML4PML4I, PML4PML4I, PML4PML4I, PML4PML4I))
142 #define addr_PML4pml4e  (addr_PML4map + (PML4PML4I * sizeof(pml4_entry_t)))
143 #define PTmap           ((pt_entry_t *)(addr_PTmap))
144 #define PDmap           ((pd_entry_t *)(addr_PDmap))
145 #define PDPmap          ((pd_entry_t *)(addr_PDPmap))
146 #define PML4map         ((pd_entry_t *)(addr_PML4map))
147 #define PML4pml4e       ((pd_entry_t *)(addr_PML4pml4e))
148
149 extern u_int64_t KPML4phys;     /* physical address of kernel level 4 */
150 #endif
151
152 #ifdef _KERNEL
153 /*
154  * virtual address to page table entry and
155  * to physical address. Likewise for alternate address space.
156  * Note: these work recursively, thus vtopte of a pte will give
157  * the corresponding pde that in turn maps it.
158  */
159 pt_entry_t *vtopte(vm_offset_t);
160
161 #define avtopte(va)     (APTmap + amd64_btop(va))
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 static __inline void
175 pte_store(pt_entry_t *ptep, pt_entry_t pte)
176 {
177
178         *ptep = pte;
179 }
180
181 #define pde_store(pdep, pde)    pte_store((pdep), (pde))
182
183 /*
184  * Pmap stuff
185  */
186 struct pv_entry;
187 struct vm_page;
188 struct vm_object;
189
190 struct md_page {
191         int pv_list_count;
192         TAILQ_HEAD(,pv_entry)   pv_list;
193 };
194
195 /*
196  * Each machine dependent implementation is expected to
197  * keep certain statistics.  They may do this anyway they
198  * so choose, but are expected to return the statistics
199  * in the following structure.
200  *
201  * NOTE: We try to match the size of the pc32 pmap with the vkernel pmap
202  * so the same utilities (like 'ps') can be used on both.
203  */
204 struct pmap_statistics {
205         long resident_count;    /* # of pages mapped (total) */
206         long wired_count;       /* # of pages wired */
207 };
208 typedef struct pmap_statistics *pmap_statistics_t;
209
210 struct pmap {
211         pml4_entry_t            *pm_pml4;       /* KVA of level 4 page table */
212         struct vm_page          *pm_pdirm;      /* VM page for pg directory */
213         struct vm_object        *pm_pteobj;     /* Container for pte's */
214         TAILQ_ENTRY(pmap)       pm_pmnode;      /* list of pmaps */
215         TAILQ_HEAD(,pv_entry)   pm_pvlist;      /* list of mappings in pmap */
216         int                     pm_count;       /* reference count */
217         cpumask_t               pm_active;      /* active on cpus */
218         int                     pm_filler02;    /* (filler sync w/vkernel) */
219         struct pmap_statistics  pm_stats;       /* pmap statistics */
220         struct  vm_page         *pm_ptphint;    /* pmap ptp hint */
221         int                     pm_generation;  /* detect pvlist deletions */
222 };
223
224 #define pmap_resident_count(pmap) (pmap)->pm_stats.resident_count
225
226 typedef struct pmap     *pmap_t;
227
228 #ifdef _KERNEL
229 extern struct pmap      kernel_pmap;
230 #endif
231
232 /*
233  * For each vm_page_t, there is a list of all currently valid virtual
234  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
235  */
236 typedef struct pv_entry {
237         pmap_t          pv_pmap;        /* pmap where mapping lies */
238         vm_offset_t     pv_va;          /* virtual address for mapping */
239         TAILQ_ENTRY(pv_entry)   pv_list;
240         TAILQ_ENTRY(pv_entry)   pv_plist;
241         struct vm_page  *pv_ptem;       /* VM page for pte */
242 } *pv_entry_t;
243
244 #ifdef  _KERNEL
245
246 #define NPPROVMTRR              8
247 #define PPRO_VMTRRphysBase0     0x200
248 #define PPRO_VMTRRphysMask0     0x201
249 struct ppro_vmtrr {
250         u_int64_t base, mask;
251 };
252 extern struct ppro_vmtrr PPro_vmtrr[NPPROVMTRR];
253
254 extern caddr_t  CADDR1;
255 extern pt_entry_t *CMAP1;
256 extern vm_paddr_t avail_end;
257 extern vm_paddr_t avail_start;
258 extern vm_offset_t clean_eva;
259 extern vm_offset_t clean_sva;
260 extern char *ptvmmap;           /* poor name! */
261
262 void    pmap_bootstrap ( vm_paddr_t *);
263 void    *pmap_mapdev (vm_paddr_t, vm_size_t);
264 void    *pmap_mapdev_uncacheable(vm_paddr_t, vm_size_t);
265 void    pmap_unmapdev (vm_offset_t, vm_size_t);
266 #if JG
267 pt_entry_t *pmap_pte (pmap_t, vm_offset_t) __pure2;
268 #endif
269 struct vm_page *pmap_use_pt (pmap_t, vm_offset_t);
270 #ifdef SMP
271 void    pmap_set_opt (void);
272 #endif
273 vm_paddr_t pmap_kextract(vm_offset_t);
274
275 #endif /* _KERNEL */
276
277 #endif /* !LOCORE */
278
279 #endif /* !_MACHINE_PMAP_H_ */