pmap.h revision 1.12
11.12Sad/* $NetBSD: pmap.h,v 1.12 2007/09/27 01:10:11 ad Exp $ */ 21.1Sfvdl 31.1Sfvdl/* 41.1Sfvdl * 51.1Sfvdl * Copyright (c) 1997 Charles D. Cranor and Washington University. 61.1Sfvdl * All rights reserved. 71.1Sfvdl * 81.1Sfvdl * Redistribution and use in source and binary forms, with or without 91.1Sfvdl * modification, are permitted provided that the following conditions 101.1Sfvdl * are met: 111.1Sfvdl * 1. Redistributions of source code must retain the above copyright 121.1Sfvdl * notice, this list of conditions and the following disclaimer. 131.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright 141.1Sfvdl * notice, this list of conditions and the following disclaimer in the 151.1Sfvdl * documentation and/or other materials provided with the distribution. 161.1Sfvdl * 3. All advertising materials mentioning features or use of this software 171.1Sfvdl * must display the following acknowledgment: 181.1Sfvdl * This product includes software developed by Charles D. Cranor and 191.1Sfvdl * Washington University. 201.1Sfvdl * 4. The name of the author may not be used to endorse or promote products 211.1Sfvdl * derived from this software without specific prior written permission. 221.1Sfvdl * 231.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 241.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 251.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 261.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 271.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 281.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 291.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 301.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 311.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 321.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 331.1Sfvdl */ 341.1Sfvdl 351.1Sfvdl/* 361.1Sfvdl * Copyright (c) 2001 Wasabi Systems, Inc. 371.1Sfvdl * All rights reserved. 381.1Sfvdl * 391.1Sfvdl * Written by Frank van der Linden for Wasabi Systems, Inc. 401.1Sfvdl * 411.1Sfvdl * Redistribution and use in source and binary forms, with or without 421.1Sfvdl * modification, are permitted provided that the following conditions 431.1Sfvdl * are met: 441.1Sfvdl * 1. Redistributions of source code must retain the above copyright 451.1Sfvdl * notice, this list of conditions and the following disclaimer. 461.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright 471.1Sfvdl * notice, this list of conditions and the following disclaimer in the 481.1Sfvdl * documentation and/or other materials provided with the distribution. 491.1Sfvdl * 3. All advertising materials mentioning features or use of this software 501.1Sfvdl * must display the following acknowledgement: 511.1Sfvdl * This product includes software developed for the NetBSD Project by 521.1Sfvdl * Wasabi Systems, Inc. 531.1Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse 541.1Sfvdl * or promote products derived from this software without specific prior 551.1Sfvdl * written permission. 561.1Sfvdl * 571.1Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 581.1Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 591.1Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 601.1Sfvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 611.1Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 621.1Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 631.1Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 641.1Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 651.1Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 661.1Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 671.1Sfvdl * POSSIBILITY OF SUCH DAMAGE. 681.1Sfvdl */ 691.1Sfvdl 701.1Sfvdl/* 711.1Sfvdl * pmap.h: see pmap.c for the history of this pmap module. 721.1Sfvdl */ 731.1Sfvdl 741.1Sfvdl#ifndef _AMD64_PMAP_H_ 751.1Sfvdl#define _AMD64_PMAP_H_ 761.1Sfvdl 771.1Sfvdl#ifndef _LOCORE 781.1Sfvdl#if defined(_KERNEL_OPT) 791.1Sfvdl#include "opt_largepages.h" 801.1Sfvdl#endif 811.1Sfvdl 821.1Sfvdl#include <machine/pte.h> 831.1Sfvdl#include <machine/segments.h> 841.11Sad#include <machine/atomic.h> 851.12Sad#ifdef _KERNEL 861.12Sad#include <machine/cpufunc.h> 871.12Sad#endif 881.11Sad 891.1Sfvdl#include <uvm/uvm_object.h> 901.1Sfvdl#endif 911.1Sfvdl 921.1Sfvdl/* 931.1Sfvdl * The x86_64 pmap module closely resembles the i386 one. It uses 941.1Sfvdl * the same recursive entry scheme, and the same alternate area 951.1Sfvdl * trick for accessing non-current pmaps. See the i386 pmap.h 961.1Sfvdl * for a description. The obvious difference is that 3 extra 971.1Sfvdl * levels of page table need to be dealt with. The level 1 page 981.1Sfvdl * table pages are at: 991.1Sfvdl * 1001.1Sfvdl * l1: 0x00007f8000000000 - 0x00007fffffffffff (39 bits, needs PML4 entry) 1011.1Sfvdl * 1021.1Sfvdl * The alternate space is at: 1031.1Sfvdl * 1041.1Sfvdl * l1: 0xffffff8000000000 - 0xffffffffffffffff (39 bits, needs PML4 entry) 1051.1Sfvdl * 1061.1Sfvdl * The rest is kept as physical pages in 3 UVM objects, and is 1071.1Sfvdl * temporarily mapped for virtual access when needed. 1081.1Sfvdl * 1091.1Sfvdl * Note that address space is signed, so the layout for 48 bits is: 1101.1Sfvdl * 1111.1Sfvdl * +---------------------------------+ 0xffffffffffffffff 1121.1Sfvdl * | | 1131.1Sfvdl * | alt.L1 table (PTE pages) | 1141.1Sfvdl * | | 1151.1Sfvdl * +---------------------------------+ 0xffffff8000000000 1161.1Sfvdl * ~ ~ 1171.1Sfvdl * | | 1181.1Sfvdl * | Kernel Space | 1191.1Sfvdl * | | 1201.1Sfvdl * | | 1211.4Syamt * +---------------------------------+ 0xffff800000000000 = 0x0000800000000000 1221.1Sfvdl * | | 1231.1Sfvdl * | alt.L1 table (PTE pages) | 1241.1Sfvdl * | | 1251.1Sfvdl * +---------------------------------+ 0x00007f8000000000 1261.1Sfvdl * ~ ~ 1271.1Sfvdl * | | 1281.1Sfvdl * | User Space | 1291.1Sfvdl * | | 1301.1Sfvdl * | | 1311.1Sfvdl * +---------------------------------+ 0x0000000000000000 1321.1Sfvdl * 1331.4Syamt * In other words, there is a 'VA hole' at 0x0000800000000000 - 1341.1Sfvdl * 0xffff800000000000 which will trap, just as on, for example, 1351.1Sfvdl * sparcv9. 1361.1Sfvdl * 1371.1Sfvdl * The unused space can be used if needed, but it adds a little more 1381.1Sfvdl * complexity to the calculations. 1391.1Sfvdl */ 1401.1Sfvdl 1411.1Sfvdl/* 1421.1Sfvdl * The first generation of Hammer processors can use 48 bits of 1431.1Sfvdl * virtual memory, and 40 bits of physical memory. This will be 1441.1Sfvdl * more for later generations. These defines can be changed to 1451.1Sfvdl * variable names containing the # of bits, extracted from an 1461.1Sfvdl * extended cpuid instruction (variables are harder to use during 1471.1Sfvdl * bootstrap, though) 1481.1Sfvdl */ 1491.1Sfvdl#define VIRT_BITS 48 1501.1Sfvdl#define PHYS_BITS 40 1511.1Sfvdl 1521.1Sfvdl/* 1531.1Sfvdl * Mask to get rid of the sign-extended part of addresses. 1541.1Sfvdl */ 1551.1Sfvdl#define VA_SIGN_MASK 0xffff000000000000 1561.1Sfvdl#define VA_SIGN_NEG(va) ((va) | VA_SIGN_MASK) 1571.1Sfvdl/* 1581.1Sfvdl * XXXfvdl this one's not right. 1591.1Sfvdl */ 1601.1Sfvdl#define VA_SIGN_POS(va) ((va) & ~VA_SIGN_MASK) 1611.1Sfvdl 1621.1Sfvdl#define L4_SLOT_PTE 255 1631.1Sfvdl#define L4_SLOT_KERN 256 1641.1Sfvdl#define L4_SLOT_KERNBASE 511 1651.1Sfvdl#define L4_SLOT_APTE 510 1661.1Sfvdl 1671.1Sfvdl#define PDIR_SLOT_KERN L4_SLOT_KERN 1681.1Sfvdl#define PDIR_SLOT_PTE L4_SLOT_PTE 1691.1Sfvdl#define PDIR_SLOT_APTE L4_SLOT_APTE 1701.1Sfvdl 1711.1Sfvdl/* 1721.1Sfvdl * the following defines give the virtual addresses of various MMU 1731.1Sfvdl * data structures: 1741.1Sfvdl * PTE_BASE and APTE_BASE: the base VA of the linear PTE mappings 1751.1Sfvdl * PTD_BASE and APTD_BASE: the base VA of the recursive mapping of the PTD 1761.1Sfvdl * PDP_PDE and APDP_PDE: the VA of the PDE that points back to the PDP/APDP 1771.1Sfvdl * 1781.1Sfvdl */ 1791.1Sfvdl 1801.1Sfvdl#define PTE_BASE ((pt_entry_t *) (L4_SLOT_PTE * NBPD_L4)) 1811.1Sfvdl#define APTE_BASE ((pt_entry_t *) (VA_SIGN_NEG((L4_SLOT_APTE * NBPD_L4)))) 1821.1Sfvdl 1831.1Sfvdl#define L1_BASE PTE_BASE 1841.1Sfvdl#define AL1_BASE APTE_BASE 1851.1Sfvdl 1861.1Sfvdl#define L2_BASE ((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3)) 1871.1Sfvdl#define L3_BASE ((pd_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2)) 1881.1Sfvdl#define L4_BASE ((pd_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1)) 1891.1Sfvdl 1901.1Sfvdl#define AL2_BASE ((pd_entry_t *)((char *)AL1_BASE + L4_SLOT_PTE * NBPD_L3)) 1911.1Sfvdl#define AL3_BASE ((pd_entry_t *)((char *)AL2_BASE + L4_SLOT_PTE * NBPD_L2)) 1921.1Sfvdl#define AL4_BASE ((pd_entry_t *)((char *)AL3_BASE + L4_SLOT_PTE * NBPD_L1)) 1931.1Sfvdl 1941.1Sfvdl#define PDP_PDE (L4_BASE + PDIR_SLOT_PTE) 1951.1Sfvdl#define APDP_PDE (L4_BASE + PDIR_SLOT_APTE) 1961.1Sfvdl 1971.1Sfvdl#define PDP_BASE L4_BASE 1981.1Sfvdl#define APDP_BASE AL4_BASE 1991.1Sfvdl 2001.1Sfvdl#define NKL4_MAX_ENTRIES (unsigned long)1 2011.1Sfvdl#define NKL3_MAX_ENTRIES (unsigned long)(NKL4_MAX_ENTRIES * 512) 2021.1Sfvdl#define NKL2_MAX_ENTRIES (unsigned long)(NKL3_MAX_ENTRIES * 512) 2031.1Sfvdl#define NKL1_MAX_ENTRIES (unsigned long)(NKL2_MAX_ENTRIES * 512) 2041.1Sfvdl 2051.1Sfvdl#define NKL4_KIMG_ENTRIES 1 2061.1Sfvdl#define NKL3_KIMG_ENTRIES 1 2071.1Sfvdl#define NKL2_KIMG_ENTRIES 8 2081.1Sfvdl 2091.1Sfvdl/* 2101.1Sfvdl * Since kva space is below the kernel in its entirety, we start off 2111.1Sfvdl * with zero entries on each level. 2121.1Sfvdl */ 2131.1Sfvdl#define NKL4_START_ENTRIES 0 2141.1Sfvdl#define NKL3_START_ENTRIES 0 2151.1Sfvdl#define NKL2_START_ENTRIES 0 2161.1Sfvdl#define NKL1_START_ENTRIES 0 /* XXX */ 2171.1Sfvdl 2181.1Sfvdl#define NTOPLEVEL_PDES (PAGE_SIZE / (sizeof (pd_entry_t))) 2191.1Sfvdl 2201.1Sfvdl#define KERNSPACE (NKL4_ENTRIES * NBPD_L4) 2211.1Sfvdl 2221.1Sfvdl#define NPDPG (PAGE_SIZE / sizeof (pd_entry_t)) 2231.1Sfvdl 2241.1Sfvdl#define ptei(VA) (((VA_SIGN_POS(VA)) & L1_MASK) >> L1_SHIFT) 2251.1Sfvdl 2261.1Sfvdl/* 2271.1Sfvdl * pl*_pi: index in the ptp page for a pde mapping a VA. 2281.1Sfvdl * (pl*_i below is the index in the virtual array of all pdes per level) 2291.1Sfvdl */ 2301.1Sfvdl#define pl1_pi(VA) (((VA_SIGN_POS(VA)) & L1_MASK) >> L1_SHIFT) 2311.1Sfvdl#define pl2_pi(VA) (((VA_SIGN_POS(VA)) & L2_MASK) >> L2_SHIFT) 2321.1Sfvdl#define pl3_pi(VA) (((VA_SIGN_POS(VA)) & L3_MASK) >> L3_SHIFT) 2331.1Sfvdl#define pl4_pi(VA) (((VA_SIGN_POS(VA)) & L4_MASK) >> L4_SHIFT) 2341.1Sfvdl 2351.1Sfvdl/* 2361.1Sfvdl * pl*_i: generate index into pde/pte arrays in virtual space 2371.1Sfvdl */ 2381.1Sfvdl#define pl1_i(VA) (((VA_SIGN_POS(VA)) & L1_FRAME) >> L1_SHIFT) 2391.1Sfvdl#define pl2_i(VA) (((VA_SIGN_POS(VA)) & L2_FRAME) >> L2_SHIFT) 2401.1Sfvdl#define pl3_i(VA) (((VA_SIGN_POS(VA)) & L3_FRAME) >> L3_SHIFT) 2411.1Sfvdl#define pl4_i(VA) (((VA_SIGN_POS(VA)) & L4_FRAME) >> L4_SHIFT) 2421.1Sfvdl#define pl_i(va, lvl) \ 2431.1Sfvdl (((VA_SIGN_POS(va)) & ptp_masks[(lvl)-1]) >> ptp_shifts[(lvl)-1]) 2441.1Sfvdl 2451.1Sfvdl#define PTP_MASK_INITIALIZER { L1_FRAME, L2_FRAME, L3_FRAME, L4_FRAME } 2461.1Sfvdl#define PTP_SHIFT_INITIALIZER { L1_SHIFT, L2_SHIFT, L3_SHIFT, L4_SHIFT } 2471.1Sfvdl#define NKPTP_INITIALIZER { NKL1_START_ENTRIES, NKL2_START_ENTRIES, \ 2481.1Sfvdl NKL3_START_ENTRIES, NKL4_START_ENTRIES } 2491.1Sfvdl#define NKPTPMAX_INITIALIZER { NKL1_MAX_ENTRIES, NKL2_MAX_ENTRIES, \ 2501.1Sfvdl NKL3_MAX_ENTRIES, NKL4_MAX_ENTRIES } 2511.1Sfvdl#define NBPD_INITIALIZER { NBPD_L1, NBPD_L2, NBPD_L3, NBPD_L4 } 2521.1Sfvdl#define PDES_INITIALIZER { L2_BASE, L3_BASE, L4_BASE } 2531.1Sfvdl#define APDES_INITIALIZER { AL2_BASE, AL3_BASE, AL4_BASE } 2541.1Sfvdl 2551.1Sfvdl/* 2561.1Sfvdl * PTP macros: 2571.1Sfvdl * a PTP's index is the PD index of the PDE that points to it 2581.1Sfvdl * a PTP's offset is the byte-offset in the PTE space that this PTP is at 2591.1Sfvdl * a PTP's VA is the first VA mapped by that PTP 2601.1Sfvdl * 2611.1Sfvdl * note that PAGE_SIZE == number of bytes in a PTP (4096 bytes == 1024 entries) 2621.1Sfvdl * NBPD == number of bytes a PTP can map (4MB) 2631.1Sfvdl */ 2641.1Sfvdl 2651.1Sfvdl#define ptp_va2o(va, lvl) (pl_i(va, (lvl)+1) * PAGE_SIZE) 2661.1Sfvdl 2671.1Sfvdl#define PTP_LEVELS 4 2681.1Sfvdl 2691.1Sfvdl/* 2701.1Sfvdl * PG_AVAIL usage: we make use of the ignored bits of the PTE 2711.1Sfvdl */ 2721.1Sfvdl 2731.1Sfvdl#define PG_W PG_AVAIL1 /* "wired" mapping */ 2741.1Sfvdl#define PG_PVLIST PG_AVAIL2 /* mapping has entry on pvlist */ 2751.1Sfvdl/* PG_AVAIL3 not used */ 2761.1Sfvdl 2771.1Sfvdl/* 2781.1Sfvdl * Number of PTE's per cache line. 8 byte pte, 64-byte cache line 2791.1Sfvdl * Used to avoid false sharing of cache lines. 2801.1Sfvdl */ 2811.1Sfvdl#define NPTECL 8 2821.1Sfvdl 2831.1Sfvdl 2841.1Sfvdl#if defined(_KERNEL) && !defined(_LOCORE) 2851.1Sfvdl/* 2861.1Sfvdl * pmap data structures: see pmap.c for details of locking. 2871.1Sfvdl */ 2881.1Sfvdl 2891.1Sfvdlstruct pmap; 2901.1Sfvdltypedef struct pmap *pmap_t; 2911.1Sfvdl 2921.1Sfvdl/* 2931.1Sfvdl * we maintain a list of all non-kernel pmaps 2941.1Sfvdl */ 2951.1Sfvdl 2961.1SfvdlLIST_HEAD(pmap_head, pmap); /* struct pmap_head: head of a pmap list */ 2971.1Sfvdl 2981.1Sfvdl/* 2991.1Sfvdl * the pmap structure 3001.1Sfvdl * 3011.1Sfvdl * note that the pm_obj contains the simple_lock, the reference count, 3021.1Sfvdl * page list, and number of PTPs within the pmap. 3031.1Sfvdl * 3041.1Sfvdl * pm_lock is the same as the spinlock for vm object 0. Changes to 3051.1Sfvdl * the other objects may only be made if that lock has been taken 3061.1Sfvdl * (the other object locks are only used when uvm_pagealloc is called) 3071.1Sfvdl */ 3081.1Sfvdl 3091.1Sfvdlstruct pmap { 3101.1Sfvdl struct uvm_object pm_obj[PTP_LEVELS-1]; /* objects for lvl >= 1) */ 3111.1Sfvdl#define pm_lock pm_obj[0].vmobjlock 3121.1Sfvdl#define pm_obj_l1 pm_obj[0] 3131.1Sfvdl#define pm_obj_l2 pm_obj[1] 3141.1Sfvdl#define pm_obj_l3 pm_obj[2] 3151.1Sfvdl LIST_ENTRY(pmap) pm_list; /* list (lck by pm_list lock) */ 3161.1Sfvdl pd_entry_t *pm_pdir; /* VA of PD (lck by object lock) */ 3171.1Sfvdl paddr_t pm_pdirpa; /* PA of PD (read-only after create) */ 3181.1Sfvdl struct vm_page *pm_ptphint[PTP_LEVELS-1]; 3191.1Sfvdl /* pointer to a PTP in our pmap */ 3201.1Sfvdl struct pmap_statistics pm_stats; /* pmap stats (lck by object lock) */ 3211.1Sfvdl 3221.1Sfvdl int pm_flags; /* see below */ 3231.1Sfvdl 3241.1Sfvdl union descriptor *pm_ldt; /* user-set LDT */ 3251.1Sfvdl int pm_ldt_len; /* number of LDT entries */ 3261.1Sfvdl int pm_ldt_sel; /* LDT selector */ 3271.1Sfvdl u_int32_t pm_cpus; /* mask of CPUs using pmap */ 3281.1Sfvdl}; 3291.1Sfvdl 3301.1Sfvdl/* pm_flags */ 3311.1Sfvdl#define PMF_USER_LDT 0x01 /* pmap has user-set LDT */ 3321.1Sfvdl 3331.1Sfvdl/* 3341.1Sfvdl * for each managed physical page we maintain a list of <PMAP,VA>'s 3351.1Sfvdl * which it is mapped at. the list is headed by a pv_head structure. 3361.1Sfvdl * there is one pv_head per managed phys page (allocated at boot time). 3371.1Sfvdl * the pv_head structure points to a list of pv_entry structures (each 3381.1Sfvdl * describes one mapping). 3391.1Sfvdl */ 3401.1Sfvdl 3411.2Ssekiyastruct pv_entry { /* locked by its list's pvh_lock */ 3421.2Ssekiya SPLAY_ENTRY(pv_entry) pv_node; /* splay-tree node */ 3431.2Ssekiya struct pmap *pv_pmap; /* the pmap */ 3441.2Ssekiya vaddr_t pv_va; /* the virtual address */ 3451.2Ssekiya struct vm_page *pv_ptp; /* the vm_page of the PTP */ 3461.11Sad struct pmap_cpu *pv_alloc_cpu; /* CPU allocated from */ 3471.2Ssekiya}; 3481.1Sfvdl 3491.1Sfvdl/* 3501.1Sfvdl * pv_entrys are dynamically allocated in chunks from a single page. 3511.1Sfvdl * we keep track of how many pv_entrys are in use for each page and 3521.1Sfvdl * we can free pv_entry pages if needed. there is one lock for the 3531.1Sfvdl * entire allocation system. 3541.1Sfvdl */ 3551.1Sfvdl 3561.1Sfvdlstruct pv_page_info { 3571.1Sfvdl TAILQ_ENTRY(pv_page) pvpi_list; 3581.1Sfvdl struct pv_entry *pvpi_pvfree; 3591.1Sfvdl int pvpi_nfree; 3601.1Sfvdl}; 3611.1Sfvdl 3621.1Sfvdl/* 3631.1Sfvdl * number of pv_entry's in a pv_page 3641.1Sfvdl * (note: won't work on systems where NPBG isn't a constant) 3651.1Sfvdl */ 3661.1Sfvdl 3671.1Sfvdl#define PVE_PER_PVPAGE ((PAGE_SIZE - sizeof(struct pv_page_info)) / \ 3681.1Sfvdl sizeof(struct pv_entry)) 3691.1Sfvdl 3701.1Sfvdl/* 3711.1Sfvdl * a pv_page: where pv_entrys are allocated from 3721.1Sfvdl */ 3731.1Sfvdl 3741.1Sfvdlstruct pv_page { 3751.1Sfvdl struct pv_page_info pvinfo; 3761.1Sfvdl struct pv_entry pvents[PVE_PER_PVPAGE]; 3771.1Sfvdl}; 3781.1Sfvdl 3791.1Sfvdl/* 3801.1Sfvdl * pmap_remove_record: a record of VAs that have been unmapped, used to 3811.1Sfvdl * flush TLB. if we have more than PMAP_RR_MAX then we stop recording. 3821.1Sfvdl */ 3831.1Sfvdl 3841.1Sfvdl#define PMAP_RR_MAX 16 /* max of 16 pages (64K) */ 3851.1Sfvdl 3861.1Sfvdlstruct pmap_remove_record { 3871.1Sfvdl int prr_npages; 3881.1Sfvdl vaddr_t prr_vas[PMAP_RR_MAX]; 3891.1Sfvdl}; 3901.1Sfvdl 3911.1Sfvdl/* 3921.1Sfvdl * global kernel variables 3931.1Sfvdl */ 3941.1Sfvdl 3951.1Sfvdl/* PTDpaddr: is the physical address of the kernel's PDP */ 3961.1Sfvdlextern u_long PTDpaddr; 3971.1Sfvdl 3981.1Sfvdlextern struct pmap kernel_pmap_store; /* kernel pmap */ 3991.1Sfvdlextern int pmap_pg_g; /* do we support PG_G? */ 4001.1Sfvdl 4011.1Sfvdlextern paddr_t ptp_masks[]; 4021.1Sfvdlextern int ptp_shifts[]; 4031.1Sfvdlextern long nkptp[], nbpd[], nkptpmax[]; 4041.1Sfvdl 4051.1Sfvdl/* 4061.1Sfvdl * macros 4071.1Sfvdl */ 4081.1Sfvdl 4091.1Sfvdl#define pmap_kernel() (&kernel_pmap_store) 4101.1Sfvdl#define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count) 4111.1Sfvdl#define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count) 4121.1Sfvdl 4131.1Sfvdl#define pmap_clear_modify(pg) pmap_clear_attrs(pg, PG_M) 4141.1Sfvdl#define pmap_clear_reference(pg) pmap_clear_attrs(pg, PG_U) 4151.1Sfvdl#define pmap_copy(DP,SP,D,L,S) 4161.1Sfvdl#define pmap_is_modified(pg) pmap_test_attrs(pg, PG_M) 4171.1Sfvdl#define pmap_is_referenced(pg) pmap_test_attrs(pg, PG_U) 4181.1Sfvdl#define pmap_move(DP,SP,D,L,S) 4191.1Sfvdl#define pmap_phys_address(ppn) ptob(ppn) 4201.1Sfvdl#define pmap_valid_entry(E) ((E) & PG_V) /* is PDE or PTE valid? */ 4211.1Sfvdl 4221.1Sfvdl 4231.1Sfvdl/* 4241.1Sfvdl * prototypes 4251.1Sfvdl */ 4261.1Sfvdl 4271.1Sfvdlvoid pmap_activate __P((struct lwp *)); 4281.1Sfvdlvoid pmap_bootstrap __P((vaddr_t)); 4291.10Sthorpejbool pmap_clear_attrs __P((struct vm_page *, unsigned)); 4301.1Sfvdlvoid pmap_deactivate __P((struct lwp *)); 4311.1Sfvdlstatic void pmap_page_protect __P((struct vm_page *, vm_prot_t)); 4321.1Sfvdlvoid pmap_page_remove __P((struct vm_page *)); 4331.1Sfvdlstatic void pmap_protect __P((struct pmap *, vaddr_t, 4341.1Sfvdl vaddr_t, vm_prot_t)); 4351.1Sfvdlvoid pmap_remove __P((struct pmap *, vaddr_t, vaddr_t)); 4361.10Sthorpejbool pmap_test_attrs __P((struct vm_page *, unsigned)); 4371.1Sfvdlstatic void pmap_update_pg __P((vaddr_t)); 4381.1Sfvdlstatic void pmap_update_2pg __P((vaddr_t,vaddr_t)); 4391.1Sfvdlvoid pmap_write_protect __P((struct pmap *, vaddr_t, 4401.1Sfvdl vaddr_t, vm_prot_t)); 4411.3Sfvdlvoid pmap_changeprot_local(vaddr_t, vm_prot_t); 4421.1Sfvdl 4431.1Sfvdlvaddr_t reserve_dumppages __P((vaddr_t)); /* XXX: not a pmap fn */ 4441.1Sfvdl 4451.11Sadvoid pmap_tlb_shootdown __P((pmap_t, vaddr_t, vaddr_t, pt_entry_t)); 4461.11Sadvoid pmap_tlb_shootwait __P((void)); 4471.1Sfvdlvoid pmap_prealloc_lowmem_ptps __P((void)); 4481.1Sfvdl 4491.1Sfvdl#define PMAP_GROWKERNEL /* turn on pmap_growkernel interface */ 4501.1Sfvdl 4511.1Sfvdl/* 4521.1Sfvdl * Do idle page zero'ing uncached to avoid polluting the cache. 4531.1Sfvdl */ 4541.10Sthorpejbool pmap_pageidlezero __P((paddr_t)); 4551.1Sfvdl#define PMAP_PAGEIDLEZERO(pa) pmap_pageidlezero((pa)) 4561.1Sfvdl 4571.1Sfvdl/* 4581.1Sfvdl * inline functions 4591.1Sfvdl */ 4601.1Sfvdl 4611.9Sperrystatic __inline void 4621.1Sfvdlpmap_remove_all(struct pmap *pmap) 4631.1Sfvdl{ 4641.1Sfvdl /* Nothing. */ 4651.1Sfvdl} 4661.1Sfvdl 4671.1Sfvdl/* 4681.1Sfvdl * pmap_update_pg: flush one page from the TLB (or flush the whole thing 4691.1Sfvdl * if hardware doesn't support one-page flushing) 4701.1Sfvdl */ 4711.1Sfvdl 4721.9Sperry__inline static void 4731.1Sfvdlpmap_update_pg(va) 4741.1Sfvdl vaddr_t va; 4751.1Sfvdl{ 4761.1Sfvdl invlpg(va); 4771.1Sfvdl} 4781.1Sfvdl 4791.1Sfvdl/* 4801.1Sfvdl * pmap_update_2pg: flush two pages from the TLB 4811.1Sfvdl */ 4821.1Sfvdl 4831.9Sperry__inline static void 4841.1Sfvdlpmap_update_2pg(va, vb) 4851.1Sfvdl vaddr_t va, vb; 4861.1Sfvdl{ 4871.1Sfvdl invlpg(va); 4881.1Sfvdl invlpg(vb); 4891.1Sfvdl} 4901.1Sfvdl 4911.1Sfvdl/* 4921.1Sfvdl * pmap_page_protect: change the protection of all recorded mappings 4931.1Sfvdl * of a managed page 4941.1Sfvdl * 4951.1Sfvdl * => this function is a frontend for pmap_page_remove/pmap_clear_attrs 4961.1Sfvdl * => we only have to worry about making the page more protected. 4971.1Sfvdl * unprotecting a page is done on-demand at fault time. 4981.1Sfvdl */ 4991.1Sfvdl 5001.9Sperry__inline static void 5011.1Sfvdlpmap_page_protect(struct vm_page *pg, vm_prot_t prot) 5021.1Sfvdl{ 5031.1Sfvdl if ((prot & VM_PROT_WRITE) == 0) { 5041.1Sfvdl if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) { 5051.1Sfvdl (void) pmap_clear_attrs(pg, PG_RW); 5061.1Sfvdl } else { 5071.1Sfvdl pmap_page_remove(pg); 5081.1Sfvdl } 5091.1Sfvdl } 5101.1Sfvdl} 5111.1Sfvdl 5121.1Sfvdl/* 5131.1Sfvdl * pmap_protect: change the protection of pages in a pmap 5141.1Sfvdl * 5151.1Sfvdl * => this function is a frontend for pmap_remove/pmap_write_protect 5161.1Sfvdl * => we only have to worry about making the page more protected. 5171.1Sfvdl * unprotecting a page is done on-demand at fault time. 5181.1Sfvdl */ 5191.1Sfvdl 5201.9Sperry__inline static void 5211.1Sfvdlpmap_protect(pmap, sva, eva, prot) 5221.1Sfvdl struct pmap *pmap; 5231.1Sfvdl vaddr_t sva, eva; 5241.1Sfvdl vm_prot_t prot; 5251.1Sfvdl{ 5261.1Sfvdl if ((prot & VM_PROT_WRITE) == 0) { 5271.1Sfvdl if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) { 5281.1Sfvdl pmap_write_protect(pmap, sva, eva, prot); 5291.1Sfvdl } else { 5301.1Sfvdl pmap_remove(pmap, sva, eva); 5311.1Sfvdl } 5321.1Sfvdl } 5331.1Sfvdl} 5341.1Sfvdl 5351.1Sfvdl/* 5361.1Sfvdl * various address inlines 5371.1Sfvdl * 5381.1Sfvdl * vtopte: return a pointer to the PTE mapping a VA, works only for 5391.1Sfvdl * user and PT addresses 5401.1Sfvdl * 5411.1Sfvdl * kvtopte: return a pointer to the PTE mapping a kernel VA 5421.1Sfvdl */ 5431.1Sfvdl 5441.1Sfvdl#include <lib/libkern/libkern.h> 5451.1Sfvdl 5461.9Sperrystatic __inline pt_entry_t * 5471.1Sfvdlvtopte(vaddr_t va) 5481.1Sfvdl{ 5491.1Sfvdl 5501.1Sfvdl KASSERT(va < (L4_SLOT_KERN * NBPD_L4)); 5511.1Sfvdl 5521.1Sfvdl return (PTE_BASE + pl1_i(va)); 5531.1Sfvdl} 5541.1Sfvdl 5551.9Sperrystatic __inline pt_entry_t * 5561.1Sfvdlkvtopte(vaddr_t va) 5571.1Sfvdl{ 5581.1Sfvdl 5591.1Sfvdl KASSERT(va >= (L4_SLOT_KERN * NBPD_L4)); 5601.1Sfvdl 5611.1Sfvdl#ifdef LARGEPAGES 5621.1Sfvdl { 5631.1Sfvdl pd_entry_t *pde; 5641.1Sfvdl 5651.5Syamt pde = L2_BASE + pl2_i(va); 5661.1Sfvdl if (*pde & PG_PS) 5671.1Sfvdl return ((pt_entry_t *)pde); 5681.1Sfvdl } 5691.1Sfvdl#endif 5701.1Sfvdl 5711.1Sfvdl return (PTE_BASE + pl1_i(va)); 5721.1Sfvdl} 5731.1Sfvdl 5741.1Sfvdl#define pmap_pte_set(p, n) x86_atomic_testset_u64(p, n) 5751.11Sad#define pmap_pte_setbits(p, b) x86_atomic_setbits_u64(p, b) 5761.1Sfvdl#define pmap_pte_clearbits(p, b) x86_atomic_clearbits_u64(p, b) 5771.1Sfvdl#define pmap_cpu_has_pg_n() (1) 5781.1Sfvdl#define pmap_cpu_has_invlpg (1) 5791.1Sfvdl 5801.1Sfvdlpaddr_t vtophys __P((vaddr_t)); 5811.1Sfvdlvaddr_t pmap_map __P((vaddr_t, paddr_t, paddr_t, vm_prot_t)); 5821.11Sadvoid pmap_cpu_init_early(struct cpu_info *); 5831.11Sadvoid pmap_cpu_init_late(struct cpu_info *); 5841.11Sadvoid sse2_zero_page(void *); 5851.11Sadvoid sse2_copy_page(void *, void *); 5861.1Sfvdl 5871.1Sfvdl#if 0 /* XXXfvdl was USER_LDT, need to check if that can be supported */ 5881.1Sfvdlvoid pmap_ldt_cleanup __P((struct lwp *)); 5891.1Sfvdl#define PMAP_FORK 5901.1Sfvdl#endif /* USER_LDT */ 5911.1Sfvdl 5921.1Sfvdl/* 5931.1Sfvdl * Hooks for the pool allocator. 5941.1Sfvdl */ 5951.1Sfvdl#define POOL_VTOPHYS(va) vtophys((vaddr_t) (va)) 5961.1Sfvdl 5971.11Sad/* 5981.11Sad * TLB shootdown mailbox. 5991.11Sad */ 6001.11Sad 6011.11Sadstruct pmap_mbox { 6021.11Sad volatile void *mb_pointer; 6031.11Sad volatile uintptr_t mb_addr1; 6041.11Sad volatile uintptr_t mb_addr2; 6051.11Sad volatile uintptr_t mb_head; 6061.11Sad volatile uintptr_t mb_tail; 6071.11Sad volatile uintptr_t mb_global; 6081.11Sad}; 6091.11Sad 6101.1Sfvdl#endif /* _KERNEL && !_LOCORE */ 6111.1Sfvdl#endif /* _AMD64_PMAP_H_ */ 612