pmap.h revision 330897
1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991 Regents of the University of California. 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 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * Derived from hp300 version by Mike Hibler, this version by William 36 * Jolitz uses a recursive map [a pde points to the page directory] to 37 * map the page tables using the pagetables themselves. This is done to 38 * reduce the impact on kernel virtual memory for lots of sparse address 39 * space, and to reduce the cost of memory to each process. 40 * 41 * from: hp300: @(#)pmap.h 7.2 (Berkeley) 12/16/90 42 * from: @(#)pmap.h 7.4 (Berkeley) 5/12/91 43 * $FreeBSD: stable/11/sys/i386/include/pmap.h 330897 2018-03-14 03:19:51Z eadler $ 44 */ 45 46#ifndef _MACHINE_PMAP_H_ 47#define _MACHINE_PMAP_H_ 48 49/* 50 * Page-directory and page-table entries follow this format, with a few 51 * of the fields not present here and there, depending on a lot of things. 52 */ 53 /* ---- Intel Nomenclature ---- */ 54#define PG_V 0x001 /* P Valid */ 55#define PG_RW 0x002 /* R/W Read/Write */ 56#define PG_U 0x004 /* U/S User/Supervisor */ 57#define PG_NC_PWT 0x008 /* PWT Write through */ 58#define PG_NC_PCD 0x010 /* PCD Cache disable */ 59#define PG_A 0x020 /* A Accessed */ 60#define PG_M 0x040 /* D Dirty */ 61#define PG_PS 0x080 /* PS Page size (0=4k,1=4M) */ 62#define PG_PTE_PAT 0x080 /* PAT PAT index */ 63#define PG_G 0x100 /* G Global */ 64#define PG_AVAIL1 0x200 /* / Available for system */ 65#define PG_AVAIL2 0x400 /* < programmers use */ 66#define PG_AVAIL3 0x800 /* \ */ 67#define PG_PDE_PAT 0x1000 /* PAT PAT index */ 68#if defined(PAE) || defined(PAE_TABLES) 69#define PG_NX (1ull<<63) /* No-execute */ 70#endif 71 72 73/* Our various interpretations of the above */ 74#define PG_W PG_AVAIL1 /* "Wired" pseudoflag */ 75#define PG_MANAGED PG_AVAIL2 76#define PG_PROMOTED PG_AVAIL3 /* PDE only */ 77#if defined(PAE) || defined(PAE_TABLES) 78#define PG_FRAME (0x000ffffffffff000ull) 79#define PG_PS_FRAME (0x000fffffffe00000ull) 80#else 81#define PG_FRAME (~PAGE_MASK) 82#define PG_PS_FRAME (0xffc00000) 83#endif 84#define PG_PROT (PG_RW|PG_U) /* all protection bits . */ 85#define PG_N (PG_NC_PWT|PG_NC_PCD) /* Non-cacheable */ 86 87/* Page level cache control fields used to determine the PAT type */ 88#define PG_PDE_CACHE (PG_PDE_PAT | PG_NC_PWT | PG_NC_PCD) 89#define PG_PTE_CACHE (PG_PTE_PAT | PG_NC_PWT | PG_NC_PCD) 90 91/* 92 * Promotion to a 2 or 4MB (PDE) page mapping requires that the corresponding 93 * 4KB (PTE) page mappings have identical settings for the following fields: 94 */ 95#define PG_PTE_PROMOTE (PG_MANAGED | PG_W | PG_G | PG_PTE_PAT | \ 96 PG_M | PG_A | PG_NC_PCD | PG_NC_PWT | PG_U | PG_RW | PG_V) 97 98/* 99 * Page Protection Exception bits 100 */ 101 102#define PGEX_P 0x01 /* Protection violation vs. not present */ 103#define PGEX_W 0x02 /* during a Write cycle */ 104#define PGEX_U 0x04 /* access from User mode (UPL) */ 105#define PGEX_RSV 0x08 /* reserved PTE field is non-zero */ 106#define PGEX_I 0x10 /* during an instruction fetch */ 107 108/* 109 * Size of Kernel address space. This is the number of page table pages 110 * (4MB each) to use for the kernel. 256 pages == 1 Gigabyte. 111 * This **MUST** be a multiple of 4 (eg: 252, 256, 260, etc). 112 * For PAE, the page table page unit size is 2MB. This means that 512 pages 113 * is 1 Gigabyte. Double everything. It must be a multiple of 8 for PAE. 114 */ 115#ifndef KVA_PAGES 116#if defined(PAE) || defined(PAE_TABLES) 117#define KVA_PAGES 512 118#else 119#define KVA_PAGES 256 120#endif 121#endif 122 123/* 124 * Pte related macros 125 */ 126#define VADDR(pdi, pti) ((vm_offset_t)(((pdi)<<PDRSHIFT)|((pti)<<PAGE_SHIFT))) 127 128/* 129 * The initial number of kernel page table pages that are constructed 130 * by locore must be sufficient to map vm_page_array. That number can 131 * be calculated as follows: 132 * max_phys / PAGE_SIZE * sizeof(struct vm_page) / NBPDR 133 * PAE: max_phys 16G, sizeof(vm_page) 76, NBPDR 2M, 152 page table pages. 134 * PAE_TABLES: max_phys 4G, sizeof(vm_page) 68, NBPDR 2M, 36 page table pages. 135 * Non-PAE: max_phys 4G, sizeof(vm_page) 68, NBPDR 4M, 18 page table pages. 136 */ 137#ifndef NKPT 138#if defined(PAE) 139#define NKPT 240 140#elif defined(PAE_TABLES) 141#define NKPT 60 142#else 143#define NKPT 30 144#endif 145#endif 146 147#ifndef NKPDE 148#define NKPDE (KVA_PAGES) /* number of page tables/pde's */ 149#endif 150 151/* 152 * The *PTDI values control the layout of virtual memory 153 * 154 * XXX This works for now, but I am not real happy with it, I'll fix it 155 * right after I fix locore.s and the magic 28K hole 156 */ 157#define KPTDI (NPDEPTD-NKPDE) /* start of kernel virtual pde's */ 158#define PTDPTDI (KPTDI-NPGPTD) /* ptd entry that points to ptd! */ 159 160/* 161 * XXX doesn't really belong here I guess... 162 */ 163#define ISA_HOLE_START 0xa0000 164#define ISA_HOLE_LENGTH (0x100000-ISA_HOLE_START) 165 166#ifndef LOCORE 167 168#include <sys/queue.h> 169#include <sys/_cpuset.h> 170#include <sys/_lock.h> 171#include <sys/_mutex.h> 172 173#include <vm/_vm_radix.h> 174 175#if defined(PAE) || defined(PAE_TABLES) 176 177typedef uint64_t pdpt_entry_t; 178typedef uint64_t pd_entry_t; 179typedef uint64_t pt_entry_t; 180 181#define PTESHIFT (3) 182#define PDESHIFT (3) 183 184#else 185 186typedef uint32_t pd_entry_t; 187typedef uint32_t pt_entry_t; 188 189#define PTESHIFT (2) 190#define PDESHIFT (2) 191 192#endif 193 194/* 195 * Address of current address space page table maps and directories. 196 */ 197#ifdef _KERNEL 198extern pt_entry_t PTmap[]; 199extern pd_entry_t PTD[]; 200extern pd_entry_t PTDpde[]; 201 202#if defined(PAE) || defined(PAE_TABLES) 203extern pdpt_entry_t *IdlePDPT; 204#endif 205extern pd_entry_t *IdlePTD; /* physical address of "Idle" state directory */ 206 207/* 208 * Translate a virtual address to the kernel virtual address of its page table 209 * entry (PTE). This can be used recursively. If the address of a PTE as 210 * previously returned by this macro is itself given as the argument, then the 211 * address of the page directory entry (PDE) that maps the PTE will be 212 * returned. 213 * 214 * This macro may be used before pmap_bootstrap() is called. 215 */ 216#define vtopte(va) (PTmap + i386_btop(va)) 217 218/* 219 * Translate a virtual address to its physical address. 220 * 221 * This macro may be used before pmap_bootstrap() is called. 222 */ 223#define vtophys(va) pmap_kextract((vm_offset_t)(va)) 224 225/* 226 * KPTmap is a linear mapping of the kernel page table. It differs from the 227 * recursive mapping in two ways: (1) it only provides access to kernel page 228 * table pages, and not user page table pages, and (2) it provides access to 229 * a kernel page table page after the corresponding virtual addresses have 230 * been promoted to a 2/4MB page mapping. 231 * 232 * KPTmap is first initialized by locore to support just NPKT page table 233 * pages. Later, it is reinitialized by pmap_bootstrap() to allow for 234 * expansion of the kernel page table. 235 */ 236extern pt_entry_t *KPTmap; 237 238/* 239 * Extract from the kernel page table the physical address that is mapped by 240 * the given virtual address "va". 241 * 242 * This function may be used before pmap_bootstrap() is called. 243 */ 244static __inline vm_paddr_t 245pmap_kextract(vm_offset_t va) 246{ 247 vm_paddr_t pa; 248 249 if ((pa = PTD[va >> PDRSHIFT]) & PG_PS) { 250 pa = (pa & PG_PS_FRAME) | (va & PDRMASK); 251 } else { 252 /* 253 * Beware of a concurrent promotion that changes the PDE at 254 * this point! For example, vtopte() must not be used to 255 * access the PTE because it would use the new PDE. It is, 256 * however, safe to use the old PDE because the page table 257 * page is preserved by the promotion. 258 */ 259 pa = KPTmap[i386_btop(va)]; 260 pa = (pa & PG_FRAME) | (va & PAGE_MASK); 261 } 262 return (pa); 263} 264 265#if (defined(PAE) || defined(PAE_TABLES)) 266 267#define pde_cmpset(pdep, old, new) atomic_cmpset_64_i586(pdep, old, new) 268#define pte_load_store(ptep, pte) atomic_swap_64_i586(ptep, pte) 269#define pte_load_clear(ptep) atomic_swap_64_i586(ptep, 0) 270#define pte_store(ptep, pte) atomic_store_rel_64_i586(ptep, pte) 271 272extern pt_entry_t pg_nx; 273 274#else /* !(PAE || PAE_TABLES) */ 275 276#define pde_cmpset(pdep, old, new) atomic_cmpset_int(pdep, old, new) 277#define pte_load_store(ptep, pte) atomic_swap_int(ptep, pte) 278#define pte_load_clear(ptep) atomic_swap_int(ptep, 0) 279#define pte_store(ptep, pte) do { \ 280 *(u_int *)(ptep) = (u_int)(pte); \ 281} while (0) 282 283#endif /* !(PAE || PAE_TABLES) */ 284 285#define pte_clear(ptep) pte_store(ptep, 0) 286 287#define pde_store(pdep, pde) pte_store(pdep, pde) 288 289#endif /* _KERNEL */ 290 291/* 292 * Pmap stuff 293 */ 294struct pv_entry; 295struct pv_chunk; 296 297struct md_page { 298 TAILQ_HEAD(,pv_entry) pv_list; 299 int pat_mode; 300}; 301 302struct pmap { 303 struct mtx pm_mtx; 304 pd_entry_t *pm_pdir; /* KVA of page directory */ 305 TAILQ_HEAD(,pv_chunk) pm_pvchunk; /* list of mappings in pmap */ 306 cpuset_t pm_active; /* active on cpus */ 307 struct pmap_statistics pm_stats; /* pmap statistics */ 308 LIST_ENTRY(pmap) pm_list; /* List of all pmaps */ 309#if defined(PAE) || defined(PAE_TABLES) 310 pdpt_entry_t *pm_pdpt; /* KVA of page directory pointer 311 table */ 312#endif 313 struct vm_radix pm_root; /* spare page table pages */ 314}; 315 316typedef struct pmap *pmap_t; 317 318#ifdef _KERNEL 319extern struct pmap kernel_pmap_store; 320#define kernel_pmap (&kernel_pmap_store) 321 322#define PMAP_LOCK(pmap) mtx_lock(&(pmap)->pm_mtx) 323#define PMAP_LOCK_ASSERT(pmap, type) \ 324 mtx_assert(&(pmap)->pm_mtx, (type)) 325#define PMAP_LOCK_DESTROY(pmap) mtx_destroy(&(pmap)->pm_mtx) 326#define PMAP_LOCK_INIT(pmap) mtx_init(&(pmap)->pm_mtx, "pmap", \ 327 NULL, MTX_DEF | MTX_DUPOK) 328#define PMAP_LOCKED(pmap) mtx_owned(&(pmap)->pm_mtx) 329#define PMAP_MTX(pmap) (&(pmap)->pm_mtx) 330#define PMAP_TRYLOCK(pmap) mtx_trylock(&(pmap)->pm_mtx) 331#define PMAP_UNLOCK(pmap) mtx_unlock(&(pmap)->pm_mtx) 332#endif 333 334/* 335 * For each vm_page_t, there is a list of all currently valid virtual 336 * mappings of that page. An entry is a pv_entry_t, the list is pv_list. 337 */ 338typedef struct pv_entry { 339 vm_offset_t pv_va; /* virtual address for mapping */ 340 TAILQ_ENTRY(pv_entry) pv_next; 341} *pv_entry_t; 342 343/* 344 * pv_entries are allocated in chunks per-process. This avoids the 345 * need to track per-pmap assignments. 346 */ 347#define _NPCM 11 348#define _NPCPV 336 349struct pv_chunk { 350 pmap_t pc_pmap; 351 TAILQ_ENTRY(pv_chunk) pc_list; 352 uint32_t pc_map[_NPCM]; /* bitmap; 1 = free */ 353 TAILQ_ENTRY(pv_chunk) pc_lru; 354 struct pv_entry pc_pventry[_NPCPV]; 355}; 356 357#ifdef _KERNEL 358 359extern caddr_t CADDR3; 360extern pt_entry_t *CMAP3; 361extern vm_paddr_t phys_avail[]; 362extern vm_paddr_t dump_avail[]; 363extern int pseflag; 364extern int pgeflag; 365extern char *ptvmmap; /* poor name! */ 366extern vm_offset_t virtual_avail; 367extern vm_offset_t virtual_end; 368 369#define pmap_page_get_memattr(m) ((vm_memattr_t)(m)->md.pat_mode) 370#define pmap_page_is_write_mapped(m) (((m)->aflags & PGA_WRITEABLE) != 0) 371#define pmap_unmapbios(va, sz) pmap_unmapdev((va), (sz)) 372 373/* 374 * Only the following functions or macros may be used before pmap_bootstrap() 375 * is called: pmap_kenter(), pmap_kextract(), pmap_kremove(), vtophys(), and 376 * vtopte(). 377 */ 378void pmap_bootstrap(vm_paddr_t); 379int pmap_cache_bits(int mode, boolean_t is_pde); 380int pmap_change_attr(vm_offset_t, vm_size_t, int); 381void pmap_init_pat(void); 382void pmap_kenter(vm_offset_t va, vm_paddr_t pa); 383void *pmap_kenter_temporary(vm_paddr_t pa, int i); 384void pmap_kremove(vm_offset_t); 385void *pmap_mapbios(vm_paddr_t, vm_size_t); 386void *pmap_mapdev(vm_paddr_t, vm_size_t); 387void *pmap_mapdev_attr(vm_paddr_t, vm_size_t, int); 388boolean_t pmap_page_is_mapped(vm_page_t m); 389void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma); 390void pmap_unmapdev(vm_offset_t, vm_size_t); 391pt_entry_t *pmap_pte(pmap_t, vm_offset_t) __pure2; 392void pmap_invalidate_page(pmap_t, vm_offset_t); 393void pmap_invalidate_range(pmap_t, vm_offset_t, vm_offset_t); 394void pmap_invalidate_all(pmap_t); 395void pmap_invalidate_cache(void); 396void pmap_invalidate_cache_pages(vm_page_t *pages, int count); 397void pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva, 398 boolean_t force); 399 400void invltlb_glob(void); 401 402#endif /* _KERNEL */ 403 404#endif /* !LOCORE */ 405 406#endif /* !_MACHINE_PMAP_H_ */ 407