- Port rtw(4) from NetBSD, which supports various RealTek 8180 chip based
[dragonfly.git] / sys / amd64 / include / pmap.h
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $DragonFly: src/sys/amd64/include/Attic/pmap.h,v 1.4 2006/07/27 00:42:46 corecode Exp $
27  */
28 #ifndef _MACHINE_PMAP_H_
29 #define _MACHINE_PMAP_H_
30
31 /*
32  * A four level page table is implemented by the amd64 hardware.  Each
33  * page table represents 9 address bits and eats 4KB of space.  There are
34  * 512 8-byte entries in each table.  The last page table contains PTE's
35  * representing 4K pages (12 bits of address space).
36  *
37  * The page tables are named:
38  *      PML4    Represents 512GB per entry (256TB total)        LEVEL4
39  *      PDP     Represents 1GB per entry                        LEVEL3
40  *      PDE     Represents 2MB per entry                        LEVEL2
41  *      PTE     Represents 4KB per entry                        LEVEL1
42  *
43  * PG_PAE       PAE 2MB extension.  In the PDE.  If 0 there is another level
44  *              of page table and PG_D and PG_G are ignored.  If 1 this is
45  *              the terminating page table and PG_D and PG_G apply.
46  *
47  * PG_PWT       Page write through.  If 1 caching is disabled for data
48  *              represented by the page.
49  * PG_PCD       Page Cache Disable.  If 1 the page table entry will not
50  *              be cached in the data cache.
51  *
52  * Each entry in the PML4 table represents a 512GB VA space.  We use a fixed
53  * PML4 and adjust entries within it to switch user spaces.
54  */
55
56 #define PG_V            0x0001LL                /* P    Present         */
57 #define PG_RW           0x0002LL                /* R/W  Writable        */
58 #define PG_U            0x0004LL                /* U/S  User            */
59 #define PG_PWT          0x0008LL                /* PWT  Page Write Through */
60 #define PG_PCD          0x0010LL                /* PCD  Page Cache Disable */
61 #define PG_A            0x0020LL                /* A    Accessed        */
62 #define PG_D            0x0040LL                /* D    Dirty   (pte only) */
63 #define PG_PS           0x0080LL                /* PAT          (pte only) */
64 #define PG_G            0x0100LL                /* G    Global  (pte only) */
65 #define PG_USR0         0x0200LL                /* available to os */
66 #define PG_USR1         0x0400LL                /* available to os */
67 #define PG_USR2         0x0800LL                /* available to os */
68 #define PG_PTE_PAT      PG_PAE                  /* PAT bit for 4K pages */
69 #define PG_PDE_PAT      0x1000LL                /* PAT bit for 2M pages */
70 #define PG_FRAME        0x000000FFFFFF0000LL    /* 40 bit phys address */
71 #define PG_PHYSRESERVED 0x000FFF0000000000LL    /* reserved for future PA */
72 #define PG_USR3         0x0010000000000000LL    /* avilable to os */
73
74 /*
75  * OS assignments
76  */
77 #define PG_W            PG_USR0                 /* Wired        */
78 #define PG_MANAGED      PG_USR1                 /* Managed      */
79 #define PG_PROT         (PG_RW|PG_U)            /* all protection bits . */
80 #define PG_N            (PG_PWT|PG_PCD)         /* Non-cacheable */
81
82 /*
83  * Page Protection Exception bits
84  */
85
86 #define PGEX_P          0x01    /* Protection violation vs. not present */
87 #define PGEX_W          0x02    /* during a Write cycle */
88 #define PGEX_U          0x04    /* access from User mode (UPL) */
89
90 /*
91  * User space is limited to one PML4 entry (512GB).  Kernel space is also
92  * limited to one PML4 entry.  Other PML4 entries are used to map foreign
93  * user spaces into KVM.  Typically each cpu in the system reserves two
94  * PML4 entries for private use.
95  */
96 #define UVA_MAXMEM      (512LL*1024*1024*1024)
97 #define KVA_MAXMEM      (512LL*1024*1024*1024)
98
99 /*
100  * Pte related macros.  This is complicated by having to deal with
101  * the sign extension of the 48th bit.
102  */
103 #define KVADDR(l4, l3, l2, l1) ( \
104         ((unsigned long)-1 << 47) | \
105         ((unsigned long)(l4) << PML4SHIFT) | \
106         ((unsigned long)(l3) << PDPSHIFT) | \
107         ((unsigned long)(l2) << PDRSHIFT) | \
108         ((unsigned long)(l1) << PAGE_SHIFT))
109
110 #define UVADDR(l4, l3, l2, l1) ( \
111         ((unsigned long)(l4) << PML4SHIFT) | \
112         ((unsigned long)(l3) << PDPSHIFT) | \
113         ((unsigned long)(l2) << PDRSHIFT) | \
114         ((unsigned long)(l1) << PAGE_SHIFT))
115
116
117 #define NKPML4E         1
118 #define NKPDPE          1
119 #define NKPDE           (NKPDPE*NPDEPG)
120
121 #define NUPML4E         (NPML4EPG/2)
122 #define NUPDPE          (NUPML4E*NPDPEPG)
123 #define NUPDE           (NUPDPE*NPDEPG)
124
125
126 /*
127  * The *PTDI values control the layout of virtual memory
128  *
129  * XXX This works for now, but I am not real happy with it, I'll fix it
130  * right after I fix locore.s and the magic 28K hole
131  *
132  * SMP_PRIVPAGES: The per-cpu address space is 0xff80000 -> 0xffbfffff
133  */
134 #define APTDPTDI        (NPDEPG-1)      /* alt ptd entry that points to APTD */
135 #define MPPTDI          (APTDPTDI-1)    /* per cpu ptd entry */
136 #define KPTDI           (MPPTDI-NKPDE)  /* start of kernel virtual pde's */
137 #define PTDPTDI         (KPTDI-1)       /* ptd entry that points to ptd! */
138 #define UMAXPTDI        (PTDPTDI-1)     /* ptd entry for user space end */
139 #define UMAXPTEOFF      (NPTEPG)        /* pte entry for user space end */
140
141 #define KPML4I          (NPML4EPG-1)
142
143 #define KPDPI           (NPDPEPG-2)
144
145 /*
146  * XXX doesn't really belong here I guess...
147  */
148 #define ISA_HOLE_START    0xa0000
149 #define ISA_HOLE_LENGTH (0x100000-ISA_HOLE_START)
150
151 #ifndef LOCORE
152
153 #include <sys/queue.h>
154
155 /*
156  * Address of current and alternate address space page table maps
157  * and directories.
158  */
159 #ifdef _KERNEL
160 extern pt_entry_t PTmap[], APTmap[], Upte;
161 extern pd_entry_t PTD[], APTD[], PTDpde, APTDpde, Upde;
162
163 extern pd_entry_t IdlePTD;      /* physical address of "Idle" state directory */
164 #endif
165
166 #ifdef _KERNEL
167 /*
168  * virtual address to page table entry and
169  * to physical address. Likewise for alternate address space.
170  * Note: these work recursively, thus vtopte of a pte will give
171  * the corresponding pde that in turn maps it.
172  */
173 #define vtopte(va)      (PTmap + i386_btop(va))
174
175 #define avtopte(va)     (APTmap + i386_btop(va))
176
177 /*
178  *      Routine:        pmap_kextract
179  *      Function:
180  *              Extract the physical page address associated
181  *              kernel virtual address.
182  */
183 static __inline vm_paddr_t
184 pmap_kextract(vm_offset_t va)
185 {
186         vm_paddr_t pa;
187
188         if ((pa = (vm_offset_t) PTD[va >> PDRSHIFT]) & PG_PS) {
189                 pa = (pa & ~(NBPDR - 1)) | (va & (NBPDR - 1));
190         } else {
191                 pa = *(vm_offset_t *)vtopte(va);
192                 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
193         }
194         return pa;
195 }
196
197 /*
198  * XXX
199  */
200 #define vtophys(va)     pmap_kextract(((vm_offset_t)(va)))
201 #define vtophys_pte(va) ((pt_entry_t)pmap_kextract(((vm_offset_t)(va))))
202
203 #define avtophys(va)    (((vm_offset_t) (*avtopte(va))&PG_FRAME) | ((vm_offset_t)(va) & PAGE_MASK))
204
205 #endif
206
207 /*
208  * Pmap stuff
209  */
210 struct  pv_entry;
211
212 struct md_page {
213         int pv_list_count;
214         TAILQ_HEAD(,pv_entry)   pv_list;
215 };
216
217 /*
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.
222  */
223 struct pmap_statistics {
224         long resident_count;    /* # of pages mapped (total) */
225         long wired_count;       /* # of pages wired */
226 };
227 typedef struct pmap_statistics *pmap_statistics_t;
228
229 struct pmap {
230         pd_entry_t              *pm_pdir;       /* KVA of page directory */
231         vm_object_t             pm_pteobj;      /* Container for pte's */
232         TAILQ_HEAD(,pv_entry)   pm_pvlist;      /* list of mappings in pmap */
233         int                     pm_count;       /* reference count */
234         cpumask_t               pm_active;      /* active on cpus */
235         struct pmap_statistics  pm_stats;       /* pmap statistics */
236         struct  vm_page         *pm_ptphint;    /* pmap ptp hint */
237 };
238
239 #define pmap_resident_count(pmap) (pmap)->pm_stats.resident_count
240
241 typedef struct pmap     *pmap_t;
242
243 #ifdef _KERNEL
244 extern pmap_t           kernel_pmap;
245 #endif
246
247 /*
248  * For each vm_page_t, there is a list of all currently valid virtual
249  * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
250  */
251 typedef struct pv_entry {
252         pmap_t          pv_pmap;        /* pmap where mapping lies */
253         vm_offset_t     pv_va;          /* virtual address for mapping */
254         TAILQ_ENTRY(pv_entry)   pv_list;
255         TAILQ_ENTRY(pv_entry)   pv_plist;
256         vm_page_t       pv_ptem;        /* VM page for pte */
257 } *pv_entry_t;
258
259 #ifdef  _KERNEL
260
261 #define NPPROVMTRR              8
262 #define PPRO_VMTRRphysBase0     0x200
263 #define PPRO_VMTRRphysMask0     0x201
264 struct ppro_vmtrr {
265         u_int64_t base, mask;
266 };
267 extern struct ppro_vmtrr PPro_vmtrr[NPPROVMTRR];
268
269 extern caddr_t  CADDR1;
270 extern pt_entry_t *CMAP1;
271 extern vm_paddr_t avail_end;
272 extern vm_paddr_t avail_start;
273 extern vm_offset_t clean_eva;
274 extern vm_offset_t clean_sva;
275 extern vm_paddr_t phys_avail[];
276 extern char *ptvmmap;           /* poor name! */
277 extern vm_offset_t virtual_avail;
278 extern vm_offset_t virtual_end;
279
280 void    pmap_bootstrap ( vm_paddr_t, vm_paddr_t);
281 pmap_t  pmap_kernel (void);
282 void    *pmap_mapdev (vm_paddr_t, vm_size_t);
283 void    pmap_unmapdev (vm_offset_t, vm_size_t);
284 unsigned *pmap_pte (pmap_t, vm_offset_t) __pure2;
285 vm_page_t pmap_use_pt (pmap_t, vm_offset_t);
286 #ifdef SMP
287 void    pmap_set_opt (void);
288 #endif
289
290 #endif /* _KERNEL */
291
292 #endif /* !LOCORE */
293
294 #endif /* !_MACHINE_PMAP_H_ */